]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
Add a forward and back history list. Resolves https://redmine.stoutner.com/issues/24.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / MainWebViewActivity.java
index 372d25114d5b20c08946d910836fded6d5b29e36..779d2d69b91a0e1c6d299c4dab2315439a063a75 100644 (file)
@@ -20,9 +20,9 @@
 package com.stoutner.privacybrowser;
 
 import android.annotation.SuppressLint;
-import android.app.Activity;
 import android.app.DialogFragment;
 import android.app.DownloadManager;
+import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
@@ -35,6 +35,8 @@ import android.net.http.SslError;
 import android.os.Build;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
+import android.print.PrintDocumentAdapter;
+import android.print.PrintManager;
 import android.support.annotation.NonNull;
 import android.support.design.widget.NavigationView;
 import android.support.design.widget.Snackbar;
@@ -46,8 +48,12 @@ import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
+import android.support.v7.app.AppCompatDialogFragment;
 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;
@@ -56,6 +62,7 @@ import android.view.inputmethod.InputMethodManager;
 import android.webkit.CookieManager;
 import android.webkit.DownloadListener;
 import android.webkit.SslErrorHandler;
+import android.webkit.WebBackForwardList;
 import android.webkit.WebChromeClient;
 import android.webkit.WebStorage;
 import android.webkit.WebView;
@@ -64,7 +71,10 @@ import android.webkit.WebViewDatabase;
 import android.widget.EditText;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
+import android.widget.LinearLayout;
 import android.widget.ProgressBar;
+import android.widget.RelativeLayout;
+import android.widget.TextView;
 
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
@@ -75,10 +85,10 @@ 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, UrlHistory.UrlHistoryListener {
 
     // `appBar` is public static so it can be accessed from `OrbotProxyHelper`.
-    // It is also used in `onCreate()`.
+    // It is also used in `onCreate()`, `onOptionsItemSelected()`, and `closeFindOnPage()`.
     public static ActionBar appBar;
 
     // `favoriteIcon` is public static so it can be accessed from `CreateHomeScreenShortcut`, `BookmarksActivity`, `CreateBookmark`, `CreateBookmarkFolder`, and `EditBookmark`.
@@ -89,14 +99,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // It is also used in `onCreate()`, `onOptionsItemSelected()`, `onCreateHomeScreenShortcutCreate()`, and `loadUrlFromTextBox()`.
     public static String formattedUrlString;
 
-    // `customHeader` is public static so it can be accessed from `BookmarksActivity`.  It is also used in `onCreate()`, `onOptionsItemSelected()`, and `loadUrlFromTextBox()`.
-    public static Map<String, String> customHeaders = new HashMap<>();
-
     // `sslCertificate` is public static so it can be accessed from `ViewSslCertificate`.  It is also used in `onCreate()`.
     public static SslCertificate sslCertificate;
 
 
-    // 'mainWebView' is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, 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()`.
@@ -105,6 +112,9 @@ 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()`, `onCreateContextMenu()`, and `loadUrlFromTextBox()`.
+    private final Map<String, String> customHeaders = new HashMap<>();
+
     // `javaScriptEnabled` is also used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, `loadUrlFromTextBox()`, and `applySettings()`.
     // It is `Boolean` instead of `boolean` because `applySettings()` needs to know if it is `null`.
     private Boolean javaScriptEnabled;
@@ -151,6 +161,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // `sslErrorHandler` is used in `onCreate()`, `onSslErrorCancel()`, and `onSslErrorProceed`.
     private SslErrorHandler sslErrorHandler;
 
+    // `findOnPageEditText` is used in `onCreate()`, `onOptionsItemSelected()`, and `closeFindOnPage()`.
+    private EditText findOnPageEditText;
+
+    // `inputMethodManager` is used in `onOptionsItemSelected()`, `loadUrlFromTextBox()`, and `closeFindOnPage()`.
+    private InputMethodManager inputMethodManager;
+
     @Override
     // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.  The whole premise of Privacy Browser is built around an understanding of these dangers.
     @SuppressLint("SetJavaScriptEnabled")
@@ -158,6 +174,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main_coordinatorlayout);
 
+        // Get a handle for `inputMethodManager`.
+        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+
         // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21.
         Toolbar supportAppBar = (Toolbar) findViewById(R.id.appBar);
         setSupportActionBar(supportAppBar);
@@ -166,15 +185,16 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // This is needed to get rid of the Android Studio warning that appBar might be null.
         assert appBar != null;
 
-        // Add the custom url_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
-        appBar.setCustomView(R.layout.url_bar);
+        // Add the custom url_app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
+        appBar.setCustomView(R.layout.url_app_bar);
         appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 
         // Set the "go" button on the keyboard to load the URL in urlTextBox.
         urlTextBox = (EditText) appBar.getCustomView().findViewById(R.id.urlTextBox);
         urlTextBox.setOnKeyListener(new View.OnKeyListener() {
+            @Override
             public boolean onKey(View v, int keyCode, KeyEvent event) {
-                // If the event is a key-down event on the "enter" button, load the URL.
+                // If the event is a key-down event on the `enter` button, load the URL.
                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                     // Load the URL into the mainWebView and consume the event.
                     try {
@@ -191,7 +211,66 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             }
         });
 
+        // Get handles for `fullScreenVideoFrameLayout`, `mainWebView`, and `find_on_page_edittext`.
         final FrameLayout fullScreenVideoFrameLayout = (FrameLayout) findViewById(R.id.fullScreenVideoFrameLayout);
+        mainWebView = (WebView) findViewById(R.id.mainWebView);
+        findOnPageEditText = (EditText) findViewById(R.id.find_on_page_edittext);
+
+        // Update `findOnPageCountTextView`.
+        mainWebView.setFindListener(new WebView.FindListener() {
+            // Get a handle for `findOnPageCountTextView`.
+            TextView findOnPageCountTextView = (TextView) findViewById(R.id.find_on_page_count_textview);
+
+            @Override
+            public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
+                if ((isDoneCounting) && (numberOfMatches == 0)) {  // There are no matches.
+                    // Set `findOnPageCountTextView` to `0/0`.
+                    findOnPageCountTextView.setText(R.string.zero_of_zero);
+                } else if (isDoneCounting) {  // There are matches.
+                    // `activeMatchOrdinal` is zero-based.
+                    int activeMatch = activeMatchOrdinal + 1;
+
+                    // Set `findOnPageCountTextView`.
+                    findOnPageCountTextView.setText(activeMatch + "/" + numberOfMatches);
+                }
+            }
+        });
+
+        // Search for the string on the page whenever a character changes in the `findOnPageEditText`.
+        findOnPageEditText.addTextChangedListener(new TextWatcher() {
+            @Override
+            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+                // Do nothing.
+            }
+
+            @Override
+            public void onTextChanged(CharSequence s, int start, int before, int count) {
+                // Do nothing.
+            }
+
+            @Override
+            public void afterTextChanged(Editable s) {
+                // Search for the text in `mainWebView`.
+                mainWebView.findAllAsync(findOnPageEditText.getText().toString());
+            }
+        });
+
+        // Set the `check mark` button for the `findOnPageEditText` keyboard to close the soft keyboard.
+        findOnPageEditText.setOnKeyListener(new View.OnKeyListener() {
+            @Override
+            public boolean onKey(View v, int keyCode, KeyEvent event) {
+                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {  // The `enter` key was pressed.
+                    // Hide the soft keyboard.  `0` indicates no additional flags.
+                    inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
+
+                    // Consume the event.
+                    return true;
+                } else {  // A different key was pressed.
+                    // Do not consume the event.
+                    return false;
+                }
+            }
+        });
 
         // Implement swipe to refresh
         swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
@@ -203,17 +282,47 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             }
         });
 
-        mainWebView = (WebView) findViewById(R.id.mainWebView);
-
         // Create the navigation drawer.
         drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
-        // The DrawerTitle identifies the drawer in accessibility mode.
+        // `DrawerTitle` identifies the drawer in accessibility mode.
         drawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.navigation_drawer));
 
         // Listen for touches on the navigation menu.
         final NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
         navigationView.setNavigationItemSelectedListener(this);
 
+        // Get handles for `navigationMenu` and the back and forward menu items.  The menu is zero-based, so item 1 and 2 and the second and third items in the menu.
+        final Menu navigationMenu = navigationView.getMenu();
+        final MenuItem navigationBackMenuItem = navigationMenu.getItem(1);
+        final MenuItem navigationForwardMenuItem = navigationMenu.getItem(2);
+        final MenuItem navigationHistoryMenuItem = navigationMenu.getItem(3);
+
+        // The `DrawerListener` allows us to update the Navigation Menu.
+        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
+            @Override
+            public void onDrawerSlide(View drawerView, float slideOffset) {
+            }
+
+            @Override
+            public void onDrawerOpened(View drawerView) {
+            }
+
+            @Override
+            public void onDrawerClosed(View drawerView) {
+            }
+
+            @Override
+            public void onDrawerStateChanged(int newState) {
+                // Update the `Back`, `Forward`, and `History` menu items every time the drawer opens.
+                navigationBackMenuItem.setEnabled(mainWebView.canGoBack());
+                navigationForwardMenuItem.setEnabled(mainWebView.canGoForward());
+                navigationHistoryMenuItem.setEnabled((mainWebView.canGoBack() || mainWebView.canGoForward()));
+
+                // Hide the keyboard so we can see the navigation menu.  `0` indicates no additional flags.
+                inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
+            }
+        });
+
         // drawerToggle creates the hamburger icon at the start of the AppBar.
         drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, supportAppBar, R.string.open_navigation, R.string.close_navigation);
 
@@ -274,8 +383,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 sslErrorHandler = handler;
 
                 // Display the SSL error `AlertDialog`.
-                DialogFragment sslCertificateErrorDialogFragment = SslCertificateError.displayDialog(error);
-                sslCertificateErrorDialogFragment.show(getFragmentManager(), getResources().getString(R.string.ssl_certificate_error));
+                AppCompatDialogFragment sslCertificateErrorDialogFragment = SslCertificateError.displayDialog(error);
+                sslCertificateErrorDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.ssl_certificate_error));
             }
         });
 
@@ -344,13 +453,16 @@ 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
             public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                 // Show the `DownloadFile` `AlertDialog` and name this instance `@string/download`.
-                DialogFragment downloadFileDialogFragment = DownloadFile.fromUrl(url, contentDisposition, contentLength);
-                downloadFileDialogFragment.show(getFragmentManager(), getResources().getString(R.string.download));
+                AppCompatDialogFragment downloadFileDialogFragment = DownloadFile.fromUrl(url, contentDisposition, contentLength);
+                downloadFileDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.download));
             }
         });
 
@@ -434,8 +546,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Set mainMenu so it can be used by `onOptionsItemSelected()` and `updatePrivacyIcons`.
         mainMenu = menu;
 
-        // Set the initial status of the privacy icons.
-        updatePrivacyIcons();
+        // Set the initial status of the privacy icons.  `false` does not call `invalidateOptionsMenu` as the last step.
+        updatePrivacyIcons(false);
 
         // Get handles for the menu items.
         MenuItem toggleFirstPartyCookies = menu.findItem(R.id.toggleFirstPartyCookies);
@@ -573,8 +685,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Apply the new JavaScript status.
                 mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled);
 
-                // Update the privacy icon.
-                updatePrivacyIcons();
+                // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+                updatePrivacyIcons(true);
 
                 // Display a `Snackbar`.
                 if (javaScriptEnabled) {  // JavaScrip is enabled.
@@ -599,8 +711,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Apply the new cookie status.
                 cookieManager.setAcceptCookie(firstPartyCookiesEnabled);
 
-                // Update the privacy icon.
-                updatePrivacyIcons();
+                // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+                updatePrivacyIcons(true);
 
                 // Display a `Snackbar`.
                 if (firstPartyCookiesEnabled) {  // First-party cookies are enabled.
@@ -648,6 +760,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Apply the new DOM Storage status.
                 mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
 
+                // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+                updatePrivacyIcons(true);
+
                 // Display a `Snackbar`.
                 if (domStorageEnabled) {
                     Snackbar.make(findViewById(R.id.mainWebView), R.string.dom_storage_enabled, Snackbar.LENGTH_SHORT).show();
@@ -676,6 +791,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                     Snackbar.make(findViewById(R.id.mainWebView), R.string.form_data_disabled, Snackbar.LENGTH_SHORT).show();
                 }
 
+                // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+                updatePrivacyIcons(true);
+
                 // Reload the WebView.
                 mainWebView.reload();
                 return true;
@@ -729,6 +847,31 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 mainWebView.getSettings().setTextZoom(200);
                 return true;
 
+            case R.id.find_on_page:
+                // Hide the URL app bar.
+                Toolbar appBarToolbar = (Toolbar) findViewById(R.id.appBar);
+                appBarToolbar.setVisibility(View.GONE);
+
+                // Show the Find on Page `RelativeLayout`.
+                LinearLayout findOnPageLinearLayout = (LinearLayout) findViewById(R.id.find_on_page_linearlayout);
+                findOnPageLinearLayout.setVisibility(View.VISIBLE);
+
+                // Display the keyboard.  We have to wait 200 ms before running the command to work around a bug in Android.
+                // http://stackoverflow.com/questions/5520085/android-show-softkeyboard-with-showsoftinput-is-not-working
+                findOnPageEditText.postDelayed(new Runnable()
+                {
+                    @Override
+                    public void run()
+                    {
+                        // Set the focus on `findOnPageEditText`.
+                        findOnPageEditText.requestFocus();
+
+                        // Display the keyboard.
+                        inputMethodManager.showSoftInput(findOnPageEditText, 0);
+                    }
+                }, 200);
+                return true;
+
             case R.id.share:
                 Intent shareIntent = new Intent();
                 shareIntent.setAction(Intent.ACTION_SEND);
@@ -738,13 +881,24 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 return true;
 
             case R.id.addToHomescreen:
-                // Show the `CreateHomeScreenShortcut` `AlertDialog` and name this instance `@string/create_shortcut`.
-                DialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcut();
-                createHomeScreenShortcutDialogFragment.show(getFragmentManager(), getResources().getString(R.string.create_shortcut));
+                // Show the `CreateHomeScreenShortcut` `AlertDialog` and name this instance `R.string.create_shortcut`.
+                AppCompatDialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcut();
+                createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.create_shortcut));
 
                 //Everything else will be handled by `CreateHomeScreenShortcut` and the associated listener below.
                 return true;
 
+            case R.id.print:
+                // Get a `PrintManager` instance.
+                PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
+
+                // Convert `mainWebView` to `printDocumentAdapter`.
+                PrintDocumentAdapter printDocumentAdapter = mainWebView.createPrintDocumentAdapter();
+
+                // Print the document.  The print attributes are `null`.
+                printManager.print(getResources().getString(R.string.privacy_browser_web_page), printDocumentAdapter, null);
+                return true;
+
             case R.id.refresh:
                 mainWebView.reload();
                 return true;
@@ -778,6 +932,15 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 }
                 break;
 
+            case R.id.history:
+                // Gte the `WebBackForwardList`.
+                WebBackForwardList webBackForwardList = mainWebView.copyBackForwardList();
+
+                // Show the `UrlHistory` `AlertDialog` and name this instance `R.string.history`.  `this` is the `Context`.
+                AppCompatDialogFragment urlHistoryDialogFragment = UrlHistory.loadBackForwardList(this, webBackForwardList);
+                urlHistoryDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.history));
+                break;
+
             case R.id.bookmarks:
                 // Launch BookmarksActivity.
                 Intent bookmarksIntent = new Intent(this, BookmarksActivity.class);
@@ -843,15 +1006,22 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Clear `customHeaders`.
                 customHeaders.clear();
 
-                // Destroy the internal state of the webview.
+                // Detach all views from `mainWebViewRelativeLayout`.
+                RelativeLayout mainWebViewRelativeLayout = (RelativeLayout) findViewById(R.id.mainWebViewRelativeLayout);
+                mainWebViewRelativeLayout.removeAllViews();
+
+                // Destroy the internal state of `mainWebView`.
                 mainWebView.destroy();
 
-                // Close Privacy Browser.  finishAndRemoveTask also removes Privacy Browser from the recent app list.
+                // Close Privacy Browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
                 if (Build.VERSION.SDK_INT >= 21) {
                     finishAndRemoveTask();
                 } else {
                     finish();
                 }
+
+                // Remove the terminated program from RAM.  The status code is `0`.
+                System.exit(0);
                 break;
 
             default:
@@ -882,11 +1052,139 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         adView = findViewById(R.id.adView);
 
         // `invalidateOptionsMenu` should recalculate the number of action buttons from the menu to display on the app bar, but it doesn't because of the this bug:  https://code.google.com/p/android/issues/detail?id=20493#c8
-        invalidateOptionsMenu();
+        // ActivityCompat.invalidateOptionsMenu(this);
     }
 
     @Override
-    public void onCreateHomeScreenShortcut(DialogFragment dialogFragment) {
+    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.
         EditText shortcutNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.shortcut_name_edittext);
 
@@ -905,17 +1203,55 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     }
 
     @Override
-    public void onDownloadFile(DialogFragment dialogFragment, String downloadUrl) {
+    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`.
         EditText downloadFileNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.download_file_name);
         String fileName = downloadFileNameEditText.getText().toString();
 
-        // Set the download save in the the `DIRECTORY_DOWNLOADS`using `fileName`.
         // Once we have `WRITE_EXTERNAL_STORAGE` permissions we can use `setDestinationInExternalPublicDir`.
-        downloadRequest.setDestinationInExternalFilesDir(this, "/", fileName);
+        if (Build.VERSION.SDK_INT >= 23) { // If API >= 23, set the download save in the the `DIRECTORY_DOWNLOADS` using `fileName`.
+            downloadRequest.setDestinationInExternalFilesDir(this, "/", fileName);
+        } else { // Only set the title using `fileName`.
+            downloadRequest.setTitle(fileName);
+        }
 
         // Allow `MediaScanner` to index the download if it is a media file.
         downloadRequest.allowScanningByMediaScanner();
@@ -926,7 +1262,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);
     }
 
@@ -946,6 +1282,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         sslErrorHandler.proceed();
     }
 
+    @Override
+    public void onUrlHistoryEntrySelected(int moveBackOrForwardSteps) {
+        // Load the history entry.
+        mainWebView.goBackOrForward(moveBackOrForwardSteps);
+    }
+
     // Override onBackPressed to handle the navigation drawer and mainWebView.
     @Override
     public void onBackPressed() {
@@ -988,8 +1330,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Apply the settings from shared preferences, which might have been changed in `SettingsActivity`.
         applySettings();
 
-        // Update the privacy icons.
-        updatePrivacyIcons();
+        // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+        updatePrivacyIcons(true);
 
     }
 
@@ -1037,8 +1379,36 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
 
         mainWebView.loadUrl(formattedUrlString, customHeaders);
 
-        // Hides the keyboard so we can see the webpage.
-        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
+        // Hide the keyboard so we can see the webpage.  `0` indicates no additional flags.
+        inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
+    }
+
+    public void findPreviousOnPage(View view) {
+        // Go to the previous highlighted phrase on the page.  `false` goes backwards instead of forwards.
+        mainWebView.findNext(false);
+    }
+
+    public void findNextOnPage(View view) {
+        // Go to the next highlighted phrase on the page. `true` goes forwards instead of backwards.
+        mainWebView.findNext(true);
+    }
+
+    public void closeFindOnPage(View view) {
+        // Delete the contents of `find_on_page_edittext`.
+        findOnPageEditText.setText(null);
+
+        // Clear the highlighted phrases.
+        mainWebView.clearMatches();
+
+        // Hide the Find on Page `RelativeLayout`.
+        LinearLayout findOnPageLinearLayout = (LinearLayout) findViewById(R.id.find_on_page_linearlayout);
+        findOnPageLinearLayout.setVisibility(View.GONE);
+
+        // Show the URL app bar.
+        Toolbar appBarToolbar = (Toolbar) findViewById(R.id.appBar);
+        appBarToolbar.setVisibility(View.VISIBLE);
+
+        // Hide the keyboard so we can see the webpage.  `0` indicates no additional flags.
         inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
     }
 
@@ -1073,6 +1443,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             cookieManager.setAcceptCookie(firstPartyCookiesEnabled);
             mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
             mainWebView.getSettings().setSaveFormData(saveFormDataEnabled);
+            mainWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString));
 
             // Set third-party cookies status if API >= 21.
             if (Build.VERSION.SDK_INT >= 21) {
@@ -1080,9 +1451,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             }
         }
 
-        // Apply the settings from `sharedPreferences`.
+        // Apply the other settings from `sharedPreferences`.
         homepage = homepageString;
-        mainWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString));
         swipeRefreshLayout.setEnabled(swipeToRefreshEnabled);
 
         // Set the user agent initial status.
@@ -1133,7 +1503,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         }
     }
 
-    private void updatePrivacyIcons() {
+    private void updatePrivacyIcons(boolean runInvalidateOptionsMenu) {
         // Get handles for the icons.
         MenuItem privacyIcon = mainMenu.findItem(R.id.toggleJavaScript);
         MenuItem firstPartyCookiesIcon = mainMenu.findItem(R.id.toggleFirstPartyCookies);
@@ -1159,7 +1529,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Update `domStorageIcon`.
         if (javaScriptEnabled && domStorageEnabled) {  // Both JavaScript and DOM storage are enabled.
             domStorageIcon.setIcon(R.drawable.dom_storage_enabled);
-        } else if (javaScriptEnabled){  // JavaScript is enabled but DOM storage is disabled.
+        } else if (javaScriptEnabled) {  // JavaScript is enabled but DOM storage is disabled.
             domStorageIcon.setIcon(R.drawable.dom_storage_disabled);
         } else {  // JavaScript is disabled, so DOM storage is ghosted.
             domStorageIcon.setIcon(R.drawable.dom_storage_ghosted);
@@ -1172,8 +1542,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             formDataIcon.setIcon(R.drawable.form_data_disabled);
         }
 
-        // `invalidateOptionsMenu` calls `onPrepareOptionsMenu()` and redraws the icons in the `AppBar`.
-        // `this` references the current activity.
-        ActivityCompat.invalidateOptionsMenu(this);
+        // `invalidateOptionsMenu` calls `onPrepareOptionsMenu()` and redraws the icons in the `AppBar`.  `this` references the current activity.
+        if (runInvalidateOptionsMenu) {
+            ActivityCompat.invalidateOptionsMenu(this);
+        }
     }
 }