-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature/#946] WebView에 사진 다운로드 기능 추가 #947
base: develop
Are you sure you want to change the base?
Changes from all commits
10fa3a0
b8d81f2
439be68
e9382ec
09856e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.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 mozilla.components.support.utils.DownloadUtils | ||
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") | ||
|
@@ -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) | ||
|
@@ -75,6 +90,37 @@ 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 | ||
Comment on lines
+93
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요 부분 아래 함수들(~handle)과 달리 따로 함수화하지 않은 이유가 있으실까요? |
||
) { | ||
val fileName = DownloadUtils.guessFileName( | ||
contentDisposition = URLDecoder.decode(contentDisposition, "utf-8"), | ||
null, | ||
url = url, | ||
mimeType = 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) | ||
} | ||
} | ||
Comment on lines
+119
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요거 특정 횟수(아마 3회) 이상 요청하면 더이상 팝업 안뜨는걸로 아는데 그거 관련 로직 추가하는건 어떨까요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 제 기기에서 테스트가 안되는데 ㅋㅋㅋ 어카죠.. |
||
handleLinkUrl() | ||
handleOnBackPressed() | ||
handleOnPullToRefresh() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분 홈에서 알림 권한 얻어올 때도 쓰던데 따로 분리할 수 있을까요?