X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2FWebview.java;h=70315bf8197f210229b7c7e0a71dfdf89bdae943;hb=65967bac65a903195ac3b37bb81c69b3fbae35d8;hp=0de84bfb20a85a94c3b09a7442e3cbf3a607de91;hpb=f13613530948c04bf8c8153b441d78ba74f476c8;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/Webview.java b/app/src/main/java/com/stoutner/privacybrowser/Webview.java index 0de84bfb..70315bf8 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/Webview.java +++ b/app/src/main/java/com/stoutner/privacybrowser/Webview.java @@ -3,6 +3,7 @@ package com.stoutner.privacybrowser; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; +import android.app.DownloadManager; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; @@ -19,6 +20,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; +import android.webkit.DownloadListener; import android.webkit.WebChromeClient; import android.webkit.WebResourceError; import android.webkit.WebResourceRequest; @@ -28,7 +30,6 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; - import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; @@ -90,7 +91,7 @@ public class Webview extends AppCompatActivity { } public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { - Toast.makeText(mainWebViewActivity, "Error loading " + request + " Error: " + error, Toast.LENGTH_SHORT).show(); + Toast.makeText(mainWebViewActivity, "Error loading " + request + " Error: " + error, Toast.LENGTH_LONG).show(); } // Update the URL in urlTextBox when the page starts to load. @@ -141,6 +142,26 @@ public class Webview extends AppCompatActivity { } }); + mainWebView.setDownloadListener(new DownloadListener() { + // Launch the Android download manager when a link leads to a download. + @Override + public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { + DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); + DownloadManager.Request requestUri = new DownloadManager.Request(Uri.parse(url)); + + // Add the URL as the description for the download. + requestUri.setDescription(url); + + // Show the download notification after the download is completed if the API is 11 or greater. + if (Build.VERSION.SDK_INT >= 11) { + requestUri.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + } + + downloadManager.enqueue(requestUri); + Toast.makeText(mainWebViewActivity, "Download started", Toast.LENGTH_SHORT).show(); + } + }); + // Allow pinch to zoom. mainWebView.getSettings().setBuiltInZoomControls(true); @@ -240,6 +261,15 @@ public class Webview extends AppCompatActivity { startActivity(Intent.createChooser(shareIntent, "Share URL")); } break; + + case R.id.downloads: + // Launch the system Download Manager. + Intent downloadManangerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); + + // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list. + downloadManangerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + startActivity(downloadManangerIntent); } return super.onOptionsItemSelected(menuItem);