]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
Hide the keyboard when the options menu is displayed.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
index a0046280162d4c7bb62edad317322c5cb17f6b7d..4200fb5e1efc74c0b5f76566cd06ab8f7c0b8681 100644 (file)
@@ -34,6 +34,7 @@ import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.database.Cursor;
 import android.graphics.Bitmap;
+import android.graphics.Typeface;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
@@ -59,7 +60,10 @@ 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.Spanned;
 import android.text.TextWatcher;
+import android.text.style.ForegroundColorSpan;
+import android.text.style.StyleSpan;
 import android.util.Patterns;
 import android.view.ContextMenu;
 import android.view.GestureDetector;
@@ -261,9 +265,21 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // `supportAppBar` is used in `onCreate()`, `onOptionsItemSelected()`, and `closeFindOnPage()`.
     private Toolbar supportAppBar;
 
-    // `urlTextBox` is used in `onCreate()`, `onOptionsItemSelected()`, `loadUrlFromTextBox()`, and `loadUrl()`.
+    // `urlTextBox` is used in `onCreate()`, `onOptionsItemSelected()`, `loadUrlFromTextBox()`, `loadUrl()`, and `highlightUrlText()`.
     private EditText urlTextBox;
 
+    // `redColorSpan` is used in `onCreate()` and `highlightUrlText()`.
+    private ForegroundColorSpan redColorSpan;
+
+    // `initialGrayColorSpan` is sued in `onCreate()` and `highlightUrlText()`.
+    private ForegroundColorSpan initialGrayColorSpan;
+
+    // `finalGrayColorSpam` is used in `onCreate()` and `highlightUrlText()`.
+    private ForegroundColorSpan finalGrayColorSpan;
+
+    // `boldStyleSpan` is used in `onCreate()` and `highlightUrlText()`.
+    private StyleSpan boldStyleSpan;
+
     // `adView` is used in `onCreate()` and `onConfigurationChanged()`.
     private View adView;
 
@@ -279,6 +295,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     @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")
+    // Remove Android Studio's warning about deprecations.  We have to use the deprecated `getColor()` until API >= 23.
+    @SuppressWarnings("deprecation")
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main_drawerlayout);
@@ -298,8 +316,36 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         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.
+        // Initialize the `ForegroundColorSpans` and `StyleSpan` for highlighting `urlTextBox`.  We have to use the deprecated `getColor()` until API >= 23.
+        redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
+        initialGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500));
+        finalGrayColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.gray_500));
+        boldStyleSpan = new StyleSpan(Typeface.BOLD);
+
+        // Get a handle for `urlTextBox`.
         urlTextBox = (EditText) appBar.getCustomView().findViewById(R.id.url_edittext);
+
+        // Remove the formatting from `urlTextBar` when the user is editing the text.
+        urlTextBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
+            @Override
+            public void onFocusChange(View v, boolean hasFocus) {
+                if (hasFocus) {  // The user is editing `urlTextBox`.
+                    // Remove the highlighting.
+                    urlTextBox.getText().removeSpan(redColorSpan);
+                    urlTextBox.getText().removeSpan(initialGrayColorSpan);
+                    urlTextBox.getText().removeSpan(finalGrayColorSpan);
+                    urlTextBox.getText().removeSpan(boldStyleSpan);
+                } else {  // The user has stopped editing `urlTextBox`.
+                    // Reapply the highlighting.
+                    highlightUrlText();
+
+                    // Scroll to the beginning of the text.
+                    urlTextBox.setScrollX(0);
+                }
+            }
+        });
+
+        // Set the `Go` button on the keyboard to load the URL in `urlTextBox`.
         urlTextBox.setOnKeyListener(new View.OnKeyListener() {
             @Override
             public boolean onKey(View v, int keyCode, KeyEvent event) {
@@ -547,8 +593,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                     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.
+                    // Hide the keyboard (if displayed) so we can see the navigation menu.  `0` indicates no additional flags.
                     inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
+
+                    // Clear the focus from `urlTextBox` if it has it.
+                    urlTextBox.clearFocus();
                 }
             }
         });
@@ -656,8 +705,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                     // We need to update `formattedUrlString` at the beginning of the load, so that if the user toggles JavaScript during the load the new website is reloaded.
                     formattedUrlString = url;
 
-                    // Display the loading URL is the URL text box.
-                    urlTextBox.setText(url);
+                    // Display the formatted URL text.
+                    urlTextBox.setText(formattedUrlString);
+
+                    // Apply text highlighting to `urlTextBox`.
+                    highlightUrlText();
 
                     // Apply any custom domain settings if the URL was loaded by navigating history.
                     if (navigatingHistory) {
@@ -696,7 +748,6 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                         // Set `formattedUrlString` to `""`.
                         formattedUrlString = "";
 
-                        // Update `urlTextBox`.
                         urlTextBox.setText(formattedUrlString);
 
                         // Request focus for `urlTextBox`.
@@ -705,7 +756,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                         // Display the keyboard.
                         inputMethodManager.showSoftInput(urlTextBox, 0);
 
-                        // Apply the domain settings.
+                        // Apply the domain settings.  This clears any settings from the previous domain.
                         applyDomainSettings(formattedUrlString);
                     } else {  // `WebView` has loaded a webpage.
                         // Set `formattedUrlString`.
@@ -713,7 +764,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
 
                         // Only update `urlTextBox` if the user is not typing in it.
                         if (!urlTextBox.hasFocus()) {
+                            // Display the formatted URL text.
                             urlTextBox.setText(formattedUrlString);
+
+                            // Apply text highlighting to `urlTextBox`.
+                            highlightUrlText();
                         }
                     }
 
@@ -1027,6 +1082,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
 
     @Override
     public boolean onPrepareOptionsMenu(Menu menu) {
+        // Hide the keyboard (if displayed) so we can see the options menu.  `0` indicates no additional flags.
+        inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
+
         // Get handles for the menu items.
         MenuItem toggleFirstPartyCookiesMenuItem = menu.findItem(R.id.toggle_first_party_cookies);
         MenuItem toggleThirdPartyCookiesMenuItem = menu.findItem(R.id.toggle_third_party_cookies);
@@ -2512,4 +2570,23 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             ActivityCompat.invalidateOptionsMenu(this);
         }
     }
+
+    private void highlightUrlText() {
+        String urlString = urlTextBox.getText().toString();
+
+        if (urlString.startsWith("http://")) {  // Highlight connections that are not encrypted.
+            urlTextBox.getText().setSpan(redColorSpan, 0, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+            urlTextBox.getText().setSpan(boldStyleSpan, 0, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+        } else if (urlString.startsWith("https://")) {  // Highlight connections that are encrypted.
+            urlTextBox.getText().setSpan(initialGrayColorSpan, 0, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+        }
+
+        // Get the index of the `/` immediately after the domain name.
+        int endOfDomainName = urlString.indexOf("/", (urlString.indexOf("//") + 2));
+
+        // De-emphasize the text after the domain name.
+        if (endOfDomainName > 0) {
+            urlTextBox.getText().setSpan(finalGrayColorSpan, endOfDomainName, urlString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+        }
+    }
 }