From 3e02a4c1e5eb1639caad22b0ff229a6b4bc4aecd Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 2 Dec 2015 15:05:01 -0700 Subject: [PATCH] Enable downloading of files. --- app/src/main/AndroidManifest.xml | 9 ++--- .../com/stoutner/privacybrowser/Webview.java | 39 ++++++++++++++++++- app/src/main/res/menu/menu_webview.xml | 20 ++++++---- app/src/main/res/values/strings.xml | 1 + 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1f558d1d..1f1aed55 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,9 +3,6 @@ package="com.stoutner.privacybrowser" > - - - - - + + - + diff --git a/app/src/main/java/com/stoutner/privacybrowser/Webview.java b/app/src/main/java/com/stoutner/privacybrowser/Webview.java index 0de84bfb..116360d0 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/Webview.java +++ b/app/src/main/java/com/stoutner/privacybrowser/Webview.java @@ -1,16 +1,22 @@ package com.stoutner.privacybrowser; +import android.Manifest; 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; import android.content.Intent; +import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Environment; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.util.Patterns; @@ -19,6 +25,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; @@ -29,6 +36,7 @@ import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; +import java.io.File; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; @@ -90,7 +98,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 +149,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 +268,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); diff --git a/app/src/main/res/menu/menu_webview.xml b/app/src/main/res/menu/menu_webview.xml index 57346e84..a1cf47d9 100644 --- a/app/src/main/res/menu/menu_webview.xml +++ b/app/src/main/res/menu/menu_webview.xml @@ -7,27 +7,27 @@ @@ -35,7 +35,7 @@ @@ -43,13 +43,19 @@ + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2e838ee3..1afee767 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,4 +13,5 @@ Copy URL Paste URL Share URL + Downloads -- 2.45.2