]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
Partial Find on Page implementation. Fix crash on SSLCertificateError for API <...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / MainWebViewActivity.java
index 2a2e14f0cbe684707adef078ee8b6731197ae0fd..15531141913b6263aecaf14b805e8f3f33424e79 100644 (file)
@@ -20,7 +20,6 @@
 package com.stoutner.privacybrowser;
 
 import android.annotation.SuppressLint;
-import android.app.Activity;
 import android.app.DialogFragment;
 import android.app.DownloadManager;
 import android.content.Context;
@@ -68,6 +67,7 @@ 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 java.io.UnsupportedEncodingException;
@@ -82,7 +82,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener {
 
     // `appBar` is public static so it can be accessed from `OrbotProxyHelper`.
-    // It is also used in `onCreate()` and `onOptionsItemSelected()`.
+    // 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`.
@@ -155,7 +155,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // `sslErrorHandler` is used in `onCreate()`, `onSslErrorCancel()`, and `onSslErrorProceed`.
     private SslErrorHandler sslErrorHandler;
 
-    private MenuItem toggleJavaScript;
+    // `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.
@@ -164,6 +168,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);
@@ -179,8 +186,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // 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 {
@@ -197,6 +205,27 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             }
         });
 
+        // Get a handle for `find_on_page_edittext`.
+        findOnPageEditText = (EditText) findViewById(R.id.find_on_page_edittext);
+
+        // Set the `go` button on the keyboard to search for the phrase in `find_on_page_edittext`
+        findOnPageEditText.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, search for the phrase.
+                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
+                    // Search for the phrase.
+                    mainWebView.findAllAsync(findOnPageEditText.getText().toString());
+
+                    // Consume the event.
+                    return true;
+                } else {
+                    // Do not consume the event.
+                    return false;
+                }
+            }
+        });
+
         final FrameLayout fullScreenVideoFrameLayout = (FrameLayout) findViewById(R.id.fullScreenVideoFrameLayout);
 
         // Implement swipe to refresh
@@ -280,8 +309,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));
             }
         });
 
@@ -440,11 +469,10 @@ 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.
-        toggleJavaScript = menu.findItem(R.id.toggleJavaScript);
         MenuItem toggleFirstPartyCookies = menu.findItem(R.id.toggleFirstPartyCookies);
         MenuItem toggleThirdPartyCookies = menu.findItem(R.id.toggleThirdPartyCookies);
         MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage);
@@ -580,8 +608,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.
@@ -606,8 +634,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.
@@ -655,6 +683,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();
@@ -683,6 +714,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;
@@ -737,9 +771,27 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 return true;
 
             case R.id.find_on_page:
-                appBar.setCustomView(R.layout.find_on_page_app_bar);
-                toggleJavaScript.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
-                appBar.invalidateOptionsMenu();
+                // 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.
+                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:
@@ -870,12 +922,15 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Destroy the internal state of the webview.
                 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:
@@ -906,7 +961,7 @@ 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
@@ -1015,8 +1070,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);
 
     }
 
@@ -1064,8 +1119,23 @@ 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);
+        // Hides the keyboard so we can see the webpage.  `0` indicates no additional flags.
+        inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
+    }
+
+    public void closeFindOnPage(View view) {
+        // Delete the contents of `find_on_page_edittext`.
+        findOnPageEditText.setText(null);
+
+        // 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);
+
+        // Hides the keyboard so we can see the webpage.  `0` indicates no additional flags.
         inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
     }
 
@@ -1160,7 +1230,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);
@@ -1186,7 +1256,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);
@@ -1199,8 +1269,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);
+        }
     }
 }