|
38 | 38 | import java.io.File; |
39 | 39 | import java.io.FileReader; |
40 | 40 | import java.io.IOException; |
| 41 | +import android.app.AlertDialog; |
41 | 42 | import android.app.DownloadManager; |
42 | 43 | import android.content.Context; |
43 | 44 | import android.net.Uri; |
|
47 | 48 | import android.webkit.WebView; |
48 | 49 | import android.widget.Toast; |
49 | 50 |
|
| 51 | + |
| 52 | + |
50 | 53 | public class Browser extends LinearLayout { |
51 | 54 |
|
52 | 55 | public int FILE_SELECT_CODE = 1; |
@@ -153,31 +156,37 @@ public void onClick(View v) { |
153 | 156 | webView.setBackgroundColor(0xFFFFFFFF); |
154 | 157 |
|
155 | 158 |
|
156 | | - // Enable download support |
157 | | - webView.setDownloadListener(new DownloadListener() { |
158 | | - @Override |
159 | | - public void onDownloadStart(String url, String userAgent, |
160 | | - String contentDisposition, String mimeType, |
161 | | - long contentLength) { |
162 | | - |
163 | | - DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); |
164 | | - request.setMimeType(mimeType); |
165 | | - request.addRequestHeader("User-Agent", userAgent); |
166 | | - request.setDescription("Downloading file..."); |
167 | | - String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType); |
168 | | - request.setTitle(fileName); |
169 | | - request.allowScanningByMediaScanner(); |
170 | | - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); |
171 | | - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); |
172 | | - |
173 | | - DownloadManager dm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE); |
174 | | - |
175 | | - dm.enqueue(request); |
176 | | - |
177 | | - Toast.makeText(getContext(), "Download started...", Toast.LENGTH_SHORT).show(); |
178 | | - |
179 | | - } |
180 | | - }); |
| 159 | + webView.setDownloadListener(new DownloadListener() { |
| 160 | + @Override |
| 161 | + public void onDownloadStart(String url, String userAgent, |
| 162 | + String contentDisposition, String mimeType, |
| 163 | + long contentLength) { |
| 164 | + |
| 165 | + String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType); |
| 166 | + |
| 167 | + new AlertDialog.Builder(getContext()) |
| 168 | + .setTitle("Download file") |
| 169 | + .setMessage("Do you want to download \"" + fileName + "\"?") |
| 170 | + .setPositiveButton("Yes", (dialog, which) -> { |
| 171 | + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); |
| 172 | + request.setMimeType(mimeType); |
| 173 | + request.addRequestHeader("User-Agent", userAgent); |
| 174 | + request.setDescription("Downloading file..."); |
| 175 | + request.setTitle(fileName); |
| 176 | + request.allowScanningByMediaScanner(); |
| 177 | + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); |
| 178 | + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); |
| 179 | + |
| 180 | + DownloadManager dm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE); |
| 181 | + dm.enqueue(request); |
| 182 | + |
| 183 | + Toast.makeText(getContext(), "Download started...", Toast.LENGTH_SHORT).show(); |
| 184 | + }) |
| 185 | + .setNegativeButton("Cancel", null) |
| 186 | + .show(); |
| 187 | + } |
| 188 | + }); |
| 189 | + |
181 | 190 |
|
182 | 191 | fitWebViewTo(0, 0, 1); |
183 | 192 |
|
|
0 commit comments