Implement context menus for links and images. Fixes https://redmine.stoutner.com...
authorSoren Stoutner <soren@stoutner.com>
Sun, 6 Nov 2016 02:43:37 +0000 (19:43 -0700)
committerSoren Stoutner <soren@stoutner.com>
Sun, 6 Nov 2016 02:43:37 +0000 (19:43 -0700)
app/src/main/java/com/stoutner/privacybrowser/DownloadImage.java [new file with mode: 0644]
app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
app/src/main/res/layout/download_image_dialog.xml [new file with mode: 0644]
app/src/main/res/values/strings.xml

diff --git a/app/src/main/java/com/stoutner/privacybrowser/DownloadImage.java b/app/src/main/java/com/stoutner/privacybrowser/DownloadImage.java
new file mode 100644 (file)
index 0000000..3b9ee79
--- /dev/null
@@ -0,0 +1,162 @@
+/**
+ * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
+ *
+ * Privacy Browser is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Privacy Browser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.stoutner.privacybrowser;
+
+import android.annotation.SuppressLint;
+import android.app.Dialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.net.Uri;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.app.AppCompatDialogFragment;
+import android.view.KeyEvent;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.WindowManager;
+import android.widget.EditText;
+
+// `android.support.v7.app.AlertDialog` uses more of the horizontal screen real estate versus `android.app.AlertDialog's` smaller width.
+// We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
+public class DownloadImage extends AppCompatDialogFragment {
+
+    private String imageUrl;
+    private String imageFileName;
+
+    public static DownloadImage imageUrl(String imageUrlString) {
+        // Create `argumentsBundle`.
+        Bundle argumentsBundle = new Bundle();
+
+        String imageNameString;
+
+        Uri imageUri = Uri.parse(imageUrlString);
+        imageNameString = imageUri.getLastPathSegment();
+
+        // Store the variables in the `Bundle`.
+        argumentsBundle.putString("URL", imageUrlString);
+        argumentsBundle.putString("Image_Name", imageNameString);
+
+        // Add `argumentsBundle` to this instance of `DownloadFile`.
+        DownloadImage thisDownloadFileDialog = new DownloadImage();
+        thisDownloadFileDialog.setArguments(argumentsBundle);
+        return thisDownloadFileDialog;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        // Store the strings in the local class variables.
+        imageUrl = getArguments().getString("URL");
+        imageFileName = getArguments().getString("Image_Name");
+    }
+
+    // The public interface is used to send information back to the parent activity.
+    public interface DownloadImageListener {
+        void onDownloadImage(AppCompatDialogFragment dialogFragment, String downloadUrl);
+    }
+
+    // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
+    private DownloadImageListener downloadImageListener;
+
+    // Check to make sure tha the parent activity implements the listener.
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        try {
+            downloadImageListener = (DownloadImageListener) context;
+        } catch (ClassCastException exception) {
+            throw new ClassCastException(context.toString() + " must implement DownloadImageListener.");
+        }
+    }
+
+    // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
+    @SuppressLint("InflateParams")
+    @Override
+    @NonNull
+    public Dialog onCreateDialog(Bundle savedInstanceState) {
+        // Get the activity's layout inflater.
+        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
+
+        // Use `AlertDialog.Builder` to create the `AlertDialog`.  `R.style.lightAlertDialog` formats the color of the button text.
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
+        dialogBuilder.setTitle(R.string.save_image_as);
+        // The parent view is `null` because it will be assigned by `AlertDialog`.
+        dialogBuilder.setView(layoutInflater.inflate(R.layout.download_image_dialog, null));
+
+        // Set an `onClick()` listener on the negative button.
+        dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                // Do nothing if `Cancel` is clicked.
+            }
+        });
+
+        // Set an `onClick()` listener on the positive button
+        dialogBuilder.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity.
+                downloadImageListener.onDownloadImage(DownloadImage.this, imageUrl);
+            }
+        });
+
+
+        // Create an `AlertDialog` from the `AlertDialog.Builder`.
+        final AlertDialog alertDialog = dialogBuilder.create();
+
+        // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
+        assert alertDialog.getWindow() != null;
+
+        // Show the keyboard when `alertDialog` is displayed on the screen.
+        alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
+
+        // We need to show `alertDialog` before we can modify the contents.
+        alertDialog.show();
+
+        // Set the text for `downloadImageNameTextView`.
+        EditText downloadImageNameTextView = (EditText) alertDialog.findViewById(R.id.download_image_name);
+        assert downloadImageNameTextView != null;  // Remove the warning on the following line that `downloadImageNameTextView` might be `null`.
+        downloadImageNameTextView.setText(imageFileName);
+
+        // Allow the `enter` key on the keyboard to save the file from `downloadImageNameTextView`.
+        downloadImageNameTextView.setOnKeyListener(new View.OnKeyListener() {
+            @Override
+            public boolean onKey (View v, int keyCode, KeyEvent event) {
+                // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download.
+                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
+                    // trigger `onDownloadImage()` and return the `DialogFragment` and the URL to the parent activity.
+                    downloadImageListener.onDownloadImage(DownloadImage.this, imageUrl);
+                    // Manually dismiss `alertDialog`.
+                    alertDialog.dismiss();
+                    // Consume the event.
+                    return true;
+                } else {  // If any other key was pressed, do not consume the event.
+                    return false;
+                }
+            }
+        });
+
+
+        // `onCreateDialog` requires the return of an `AlertDialog`.
+        return alertDialog;
+    }
+}
\ No newline at end of file
index 7928c944a52df874a6aa95f9aad48b39e687b41d..1c20c46d8f92534cf1e09f949aa235ad36c8206c 100644 (file)
@@ -53,6 +53,7 @@ import android.support.v7.widget.Toolbar;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.util.Patterns;
+import android.view.ContextMenu;
 import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -83,7 +84,7 @@ import java.util.Map;
 
 // We need to use AppCompatActivity from android.support.v7.app.AppCompatActivity to have access to the SupportActionBar until the minimum API is >= 21.
 public class MainWebViewActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener,
-        SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener {
+        SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener, DownloadImage.DownloadImageListener {
 
     // `appBar` is public static so it can be accessed from `OrbotProxyHelper`.
     // It is also used in `onCreate()`, `onOptionsItemSelected()`, and `closeFindOnPage()`.
@@ -101,7 +102,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     public static SslCertificate sslCertificate;
 
 
-    // 'mainWebView' is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `findPreviousOnPage()`, `findNextOnPage()`, `closeFindOnPage`, and `loadUrlFromTextBox()`.
+    // 'mainWebView' is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `onCreateContextMenu()`, `findPreviousOnPage()`, `findNextOnPage()`, `closeFindOnPage()`, and `loadUrlFromTextBox()`.
     private WebView mainWebView;
 
     // `swipeRefreshLayout` is used in `onCreate()`, `onPrepareOptionsMenu`, and `onRestart()`.
@@ -110,7 +111,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // `cookieManager` is used in `onCreate()`, `onOptionsItemSelected()`, and `onNavigationItemSelected()`, and `onRestart()`.
     private CookieManager cookieManager;
 
-    // `customHeader` is used in `onCreate()`, `onOptionsItemSelected()`, and `loadUrlFromTextBox()`.
+    // `customHeader` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateContextMenu()`, and `loadUrlFromTextBox()`.
     private final Map<String, String> customHeaders = new HashMap<>();
 
     // `javaScriptEnabled` is also used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, `loadUrlFromTextBox()`, and `applySettings()`.
@@ -419,6 +420,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             }
         });
 
+        // Register `mainWebView` for a context menu.  This is used to see link targets and download images.
+        registerForContextMenu(mainWebView);
+
         // Allow the downloading of files.
         mainWebView.setDownloadListener(new DownloadListener() {
             @Override
@@ -1009,6 +1013,134 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // ActivityCompat.invalidateOptionsMenu(this);
     }
 
+    @Override
+    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
+        // Store the `HitTestResult`.
+        final WebView.HitTestResult hitTestResult = mainWebView.getHitTestResult();
+
+        // Create strings.
+        final String imageUrl;
+        final String linkUrl;
+
+        switch (hitTestResult.getType()) {
+            // `SRC_ANCHOR_TYPE` is a link.
+            case WebView.HitTestResult.SRC_ANCHOR_TYPE:
+                // Get the target URL.
+                linkUrl = hitTestResult.getExtra();
+
+                // Set the target URL as the title of the `ContextMenu`.
+                menu.setHeaderTitle(linkUrl);
+
+                // Add a `Load URL` button.
+                menu.add(R.string.load_url).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+                    @Override
+                    public boolean onMenuItemClick(MenuItem item) {
+                        mainWebView.loadUrl(linkUrl, customHeaders);
+                        return false;
+                    }
+                });
+
+                // Add a `Cancel` button, which by default closes the `ContextMenu`.
+                menu.add(R.string.cancel);
+                break;
+
+            case WebView.HitTestResult.EMAIL_TYPE:
+                // Get the target URL.
+                linkUrl = hitTestResult.getExtra();
+
+                // Set the target URL as the title of the `ContextMenu`.
+                menu.setHeaderTitle(linkUrl);
+
+                // Add a `Write Email` button.
+                menu.add(R.string.write_email).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+                    @Override
+                    public boolean onMenuItemClick(MenuItem item) {
+                        // We use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
+                        Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
+
+                        // Parse the url and set it as the data for the `Intent`.
+                        emailIntent.setData(Uri.parse("mailto:" + linkUrl));
+
+                        // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of Privacy Browser.
+                        emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+                        // Make it so.
+                        startActivity(emailIntent);
+                        return false;
+                    }
+                });
+
+                // Add a `Cancel` button, which by default closes the `ContextMenu`.
+                menu.add(R.string.cancel);
+                break;
+
+            // `IMAGE_TYPE` is an image.
+            case WebView.HitTestResult.IMAGE_TYPE:
+                // Get the image URL.
+                imageUrl = hitTestResult.getExtra();
+
+                // Set the image URL as the title of the `ContextMenu`.
+                menu.setHeaderTitle(imageUrl);
+
+                // Add a `View Image` button.
+                menu.add(R.string.view_image).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+                    @Override
+                    public boolean onMenuItemClick(MenuItem item) {
+                        mainWebView.loadUrl(imageUrl, customHeaders);
+                        return false;
+                    }
+                });
+
+                // Add a `Download Image` button.
+                menu.add(R.string.download_image).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+                    @Override
+                    public boolean onMenuItemClick(MenuItem item) {
+                        // Show the `DownloadImage` `AlertDialog` and name this instance `@string/download`.
+                        AppCompatDialogFragment downloadImageDialogFragment = DownloadImage.imageUrl(imageUrl);
+                        downloadImageDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.download));
+                        return false;
+                    }
+                });
+
+                // Add a `Cancel` button, which by default closes the `ContextMenu`.
+                menu.add(R.string.cancel);
+                break;
+
+
+            // `SRC_IMAGE_ANCHOR_TYPE` is an image that is also a link.
+            case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
+                // Get the image URL.
+                imageUrl = hitTestResult.getExtra();
+
+                // Set the image URL as the title of the `ContextMenu`.
+                menu.setHeaderTitle(imageUrl);
+
+                // Add a `View Image` button.
+                menu.add(R.string.view_image).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+                    @Override
+                    public boolean onMenuItemClick(MenuItem item) {
+                        mainWebView.loadUrl(imageUrl, customHeaders);
+                        return false;
+                    }
+                });
+
+                // Add a `Download Image` button.
+                menu.add(R.string.download_image).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+                    @Override
+                    public boolean onMenuItemClick(MenuItem item) {
+                        // Show the `DownloadImage` `AlertDialog` and name this instance `@string/download`.
+                        AppCompatDialogFragment downloadImageDialogFragment = DownloadImage.imageUrl(imageUrl);
+                        downloadImageDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.download));
+                        return false;
+                    }
+                });
+
+                // Add a `Cancel` button, which by default closes the `ContextMenu`.
+                menu.add(R.string.cancel);
+                break;
+        }
+    }
+
     @Override
     public void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment) {
         // Get shortcutNameEditText from the alert dialog.
@@ -1028,9 +1160,44 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         sendBroadcast(placeBookmarkShortcut);
     }
 
+    @Override
+    public void onDownloadImage(AppCompatDialogFragment dialogFragment, String imageUrl) {
+        // Get a handle for the system `DOWNLOAD_SERVICE`.
+        DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
+
+        // Parse `imageUrl`.
+        DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(imageUrl));
+
+        // Get the file name from `dialogFragment`.
+        EditText downloadImageNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.download_image_name);
+        String imageName = downloadImageNameEditText.getText().toString();
+
+        // Once we have `WRITE_EXTERNAL_STORAGE` permissions we can use `setDestinationInExternalPublicDir`.
+        if (Build.VERSION.SDK_INT >= 23) { // If API >= 23, set the download save in the the `DIRECTORY_DOWNLOADS` using `imageName`.
+            downloadRequest.setDestinationInExternalFilesDir(this, "/", imageName);
+        } else { // Only set the title using `imageName`.
+            downloadRequest.setTitle(imageName);
+        }
+
+        // Allow `MediaScanner` to index the download if it is a media file.
+        downloadRequest.allowScanningByMediaScanner();
+
+        // Add the URL as the description for the download.
+        downloadRequest.setDescription(imageUrl);
+
+        // Show the download notification after the download is completed.
+        downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
+
+        // Initiate the download.
+        downloadManager.enqueue(downloadRequest);
+    }
+
     @Override
     public void onDownloadFile(AppCompatDialogFragment dialogFragment, String downloadUrl) {
+        // Get a handle for the system `DOWNLOAD_SERVICE`.
         DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
+
+        // Parse `downloadUrl`.
         DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(downloadUrl));
 
         // Get the file name from `dialogFragment`.
@@ -1053,7 +1220,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Show the download notification after the download is completed.
         downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
 
-        // Initiate the download and display a Snackbar.
+        // Initiate the download.
         downloadManager.enqueue(downloadRequest);
     }
 
diff --git a/app/src/main/res/layout/download_image_dialog.xml b/app/src/main/res/layout/download_image_dialog.xml
new file mode 100644 (file)
index 0000000..26560c4
--- /dev/null
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  Copyright 2016 Soren Stoutner <soren@stoutner.com>.
+
+  This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
+
+  Privacy Browser is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  Privacy Browser is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>. -->
+
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_height="wrap_content"
+    android:layout_width="match_parent"
+    android:padding="6dp">
+
+
+    <!-- `android.support.design.widget.TextInputLayout` makes the `android:hint` float above the `EditText`. -->
+    <android.support.design.widget.TextInputLayout
+        android:layout_height="wrap_content"
+        android:layout_width="match_parent" >
+
+        <!-- `android:imeOptions="actionsGo"` sets the keyboard to have a `go` key instead of a `new line` key.
+            `android:inputType="textUri"` disables spell check in the `EditText`. -->
+        <android.support.design.widget.TextInputEditText
+            android:id="@+id/download_image_name"
+            android:layout_height="wrap_content"
+            android:layout_width="match_parent"
+            android:hint="@string/image_name"
+            android:imeOptions="actionGo"
+            android:inputType="textUri" />
+    </android.support.design.widget.TextInputLayout>
+</LinearLayout>
\ No newline at end of file
index 1d997bcb23c4964a24856b5a3ceaff30d25c61a7..f92bd8397b31574c9459094616009db85fbc69d3 100644 (file)
@@ -47,7 +47,9 @@
 
     <!-- Save As. -->
     <string name="save_as">Save as</string>
+    <string name="save_image_as">Save image as</string>
     <string name="file_name">File name</string>
+    <string name="image_name">Image name</string>
     <string name="unknown_size">unknown size</string>
     <string name="download">Download</string>
 
         <string name="privacy_browser_web_page">Privacy Browser Web Page</string>
     <string name="refresh">Refresh</string>
 
+    <!-- Context Menus. -->
+    <string name="load_url">Load URL</string>
+    <string name="write_email">Write Email</string>
+    <string name="view_image">View Image</string>
+    <string name="download_image">Download Image</string>
+
     <!-- Find on Page. -->
     <string name="zero_of_zero" translatable="false">0/0</string>
     <string name="previous">Previous</string>