|
1 | 1 | package com.reactnativecommunity.webview
|
2 | 2 |
|
| 3 | +import android.app.AlertDialog |
3 | 4 | import android.app.DownloadManager
|
| 5 | +import android.content.DialogInterface |
4 | 6 | import android.content.pm.ActivityInfo
|
5 | 7 | import android.graphics.Bitmap
|
6 | 8 | import android.graphics.Color
|
@@ -106,34 +108,42 @@ class RNCWebViewManagerImpl {
|
106 | 108 |
|
107 | 109 | val downloadMessage = "Downloading $fileName"
|
108 | 110 |
|
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 | + } |
119 | 125 |
|
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 | + } |
136 | 143 | }
|
| 144 | + builder.setNegativeButton("Cancel") { _: DialogInterface?, _: Int -> } |
| 145 | + val alertDialog = builder.create() |
| 146 | + alertDialog.show() |
137 | 147 | })
|
138 | 148 | return RNCWebViewWrapper(context, webView)
|
139 | 149 | }
|
|
0 commit comments