Skip to content

Commit 991765c

Browse files
feat. ask before downloading
1 parent 4099e13 commit 991765c

File tree

1 file changed

+34
-25
lines changed
  • src/plugins/browser/android/com/foxdebug/browser

1 file changed

+34
-25
lines changed

src/plugins/browser/android/com/foxdebug/browser/Browser.java

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.io.File;
3939
import java.io.FileReader;
4040
import java.io.IOException;
41+
import android.app.AlertDialog;
4142
import android.app.DownloadManager;
4243
import android.content.Context;
4344
import android.net.Uri;
@@ -47,6 +48,8 @@
4748
import android.webkit.WebView;
4849
import android.widget.Toast;
4950

51+
52+
5053
public class Browser extends LinearLayout {
5154

5255
public int FILE_SELECT_CODE = 1;
@@ -153,31 +156,37 @@ public void onClick(View v) {
153156
webView.setBackgroundColor(0xFFFFFFFF);
154157

155158

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+
181190

182191
fitWebViewTo(0, 0, 1);
183192

0 commit comments

Comments
 (0)