]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/Webview.java
Remove unnecessary imports.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / Webview.java
index 0de84bfb20a85a94c3b09a7442e3cbf3a607de91..70315bf8197f210229b7c7e0a71dfdf89bdae943 100644 (file)
@@ -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);