|
38 | 38 | import java.io.File; |
39 | 39 | import java.io.FileReader; |
40 | 40 | import java.io.IOException; |
| 41 | +import android.app.AlertDialog; |
| 42 | +import android.app.DownloadManager; |
| 43 | +import android.content.Context; |
| 44 | +import android.net.Uri; |
| 45 | +import android.os.Environment; |
| 46 | +import android.webkit.DownloadListener; |
| 47 | +import android.webkit.URLUtil; |
| 48 | +import android.webkit.WebView; |
| 49 | +import android.widget.Toast; |
| 50 | + |
| 51 | + |
41 | 52 |
|
42 | 53 | public class Browser extends LinearLayout { |
43 | 54 |
|
@@ -143,6 +154,40 @@ public void onClick(View v) { |
143 | 154 | webView.setFocusable(true); |
144 | 155 | webView.setFocusableInTouchMode(true); |
145 | 156 | webView.setBackgroundColor(0xFFFFFFFF); |
| 157 | + |
| 158 | + |
| 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 | + |
| 190 | + |
146 | 191 | fitWebViewTo(0, 0, 1); |
147 | 192 |
|
148 | 193 | webView.setWebChromeClient(new BrowserChromeClient(this)); |
|
0 commit comments