Skip to content

Commit

Permalink
[feature/#946] WebView에 사진 다운로드 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
l2hyunwoo committed Nov 14, 2024
1 parent 4c1ee3c commit 1807ece
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
MIT License
Copyright (c) 2022-2024 SOPT Makers
Expand Down Expand Up @@ -28,6 +27,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<queries>
<package android:name="com.android.chrome" />
Expand All @@ -41,6 +41,7 @@
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SOPT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@
package org.sopt.official.webview.view

import android.app.Activity
import android.app.DownloadManager
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.webkit.CookieManager
import android.webkit.URLUtil
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.activity.addCallback
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import com.airbnb.deeplinkdispatch.DeepLink
import dagger.hilt.android.AndroidEntryPoint
import org.sopt.official.common.util.viewBinding
import org.sopt.official.common.view.toast
import org.sopt.official.webview.databinding.ActivityWebViewBinding
import java.net.URLDecoder

@AndroidEntryPoint
@DeepLink("sopt://web")
Expand All @@ -55,6 +63,13 @@ class WebViewActivity : AppCompatActivity() {
filePathCallback = null
}
}
private val permissionRequestLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (!isGranted) {
toast("권한을 받아오지 못했습니다.")
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -75,6 +90,33 @@ class WebViewActivity : AppCompatActivity() {
return true
}
}
binding.webView.setDownloadListener { url, userAgent, contentDisposition, mimetype, _ ->
if (ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
) == android.content.pm.PackageManager.PERMISSION_GRANTED
) {

val fileName = URLUtil.guessFileName(url, URLDecoder.decode(contentDisposition, "utf-8"), mimetype)
val downloadManager = getSystemService<DownloadManager>() ?: return@setDownloadListener
val request = DownloadManager.Request(Uri.parse(url))
.setMimeType(mimetype)
.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url))
.addRequestHeader("User-Agent", userAgent)
.setTitle(fileName)
.apply {
allowScanningByMediaScanner()
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
fileName
)
}
downloadManager.enqueue(request)
} else {
permissionRequestLauncher.launch(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
}
handleLinkUrl()
handleOnBackPressed()
handleOnPullToRefresh()
Expand Down

0 comments on commit 1807ece

Please sign in to comment.