Skip to content

Commit c41b213

Browse files
committed
show prompt before download starts
1 parent 9386bb7 commit c41b213

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.reactnativecommunity.webview
22

3+
import android.app.AlertDialog
34
import android.app.DownloadManager
5+
import android.content.DialogInterface
46
import android.content.pm.ActivityInfo
57
import android.graphics.Bitmap
68
import android.graphics.Color
@@ -106,34 +108,42 @@ class RNCWebViewManagerImpl {
106108

107109
val downloadMessage = "Downloading $fileName"
108110

109-
//Attempt to add cookie, if it exists
110-
var urlObj: URL? = null
111-
try {
112-
urlObj = URL(url)
113-
val baseUrl = urlObj.protocol + "://" + urlObj.host
114-
val cookie = CookieManager.getInstance().getCookie(baseUrl)
115-
request.addRequestHeader("Cookie", cookie)
116-
} catch (e: MalformedURLException) {
117-
Log.w(TAG, "Error getting cookie for DownloadManager", e)
118-
}
111+
val builder = AlertDialog.Builder(webView.context)
112+
builder.setMessage("Do you want to download \n$fileName?")
113+
builder.setCancelable(false)
114+
builder.setPositiveButton("Download") { _, _ ->
115+
//Attempt to add cookie, if it exists
116+
var urlObj: URL? = null
117+
try {
118+
urlObj = URL(url)
119+
val baseUrl = urlObj.protocol + "://" + urlObj.host
120+
val cookie = CookieManager.getInstance().getCookie(baseUrl)
121+
request.addRequestHeader("Cookie", cookie)
122+
} catch (e: MalformedURLException) {
123+
Log.w(TAG, "Error getting cookie for DownloadManager", e)
124+
}
119125

120-
//Finish setting up request
121-
request.addRequestHeader("User-Agent", userAgent)
122-
request.setTitle(fileName)
123-
request.setDescription(downloadMessage)
124-
request.allowScanningByMediaScanner()
125-
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
126-
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
127-
module.setDownloadRequest(request)
128-
if (module.grantFileDownloaderPermissions(
129-
getDownloadingMessageOrDefault(),
130-
getLackPermissionToDownloadMessageOrDefault()
131-
)
132-
) {
133-
module.downloadFile(
134-
getDownloadingMessageOrDefault()
135-
)
126+
//Finish setting up request
127+
request.addRequestHeader("User-Agent", userAgent)
128+
request.setTitle(fileName)
129+
request.setDescription(downloadMessage)
130+
request.allowScanningByMediaScanner()
131+
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
132+
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
133+
module.setDownloadRequest(request)
134+
if (module.grantFileDownloaderPermissions(
135+
getDownloadingMessageOrDefault(),
136+
getLackPermissionToDownloadMessageOrDefault()
137+
)
138+
) {
139+
module.downloadFile(
140+
getDownloadingMessageOrDefault()
141+
)
142+
}
136143
}
144+
builder.setNegativeButton("Cancel") { _: DialogInterface?, _: Int -> }
145+
val alertDialog = builder.create()
146+
alertDialog.show()
137147
})
138148
return RNCWebViewWrapper(context, webView)
139149
}

0 commit comments

Comments
 (0)