Skip to content

Commit 4e9997c

Browse files
Download support (Acode-Foundation#1263)
* feat. implement downloading support * feat. ask before downloading
1 parent 12383ff commit 4e9997c

File tree

1 file changed

+45
-0
lines changed
  • src/plugins/browser/android/com/foxdebug/browser

1 file changed

+45
-0
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@
3838
import java.io.File;
3939
import java.io.FileReader;
4040
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+
4152

4253
public class Browser extends LinearLayout {
4354

@@ -143,6 +154,40 @@ public void onClick(View v) {
143154
webView.setFocusable(true);
144155
webView.setFocusableInTouchMode(true);
145156
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+
146191
fitWebViewTo(0, 0, 1);
147192

148193
webView.setWebChromeClient(new BrowserChromeClient(this));

0 commit comments

Comments
 (0)