]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
Fix a crash if the homepage is null introduced in 2.0.1. Fixes https://redmine...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
index 1ec9e746d7d1e61e021b7055b8d88e40b3b16593..d3951989c25a1cdcaf6c93a3c0578a37767f094d 100644 (file)
@@ -107,6 +107,7 @@ import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -520,13 +521,15 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
 
             @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()));
+                if ((newState == DrawerLayout.STATE_SETTLING) || (newState == DrawerLayout.STATE_DRAGGING)) {  // The drawer is opening or closing.
+                    // Update the `Back`, `Forward`, and `History` menu items.
+                    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);
+                    // Hide the keyboard so we can see the navigation menu.  `0` indicates no additional flags.
+                    inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
+                }
             }
         });
 
@@ -641,11 +644,27 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             public void onPageFinished(WebView view, String url) {
                 // Check to see if we are waiting on Orbot.
                 if (pendingUrl.isEmpty()) {  // we are not waiting on Orbot, so we need to process the URL.
-                    formattedUrlString = url;
+                    // Check to see if `WebView` has set `url` to be `about:blank`.
+                    if (url.equals("about:blank")) {  // `WebView` is blank, so `formattedUrlString` should be `""` and `urlTextBox` should display a hint.
+                        // Set `formattedUrlString` to `""`.
+                        formattedUrlString = "";
 
-                    // Only update urlTextBox if the user is not typing in it.
-                    if (!urlTextBox.hasFocus()) {
+                        // Update `urlTextBox`.
                         urlTextBox.setText(formattedUrlString);
+
+                        // Request focus for `urlTextBox`.
+                        urlTextBox.requestFocus();
+
+                        // Display the keyboard.
+                        inputMethodManager.showSoftInput(urlTextBox, 0);
+                    } else {  // `WebView` has loaded a webpage.
+                        // Set `formattedUrlString`.
+                        formattedUrlString = url;
+
+                        // Only update `urlTextBox` if the user is not typing in it.
+                        if (!urlTextBox.hasFocus()) {
+                            urlTextBox.setText(formattedUrlString);
+                        }
                     }
 
                     // Store the SSL certificate so it can be accessed from `ViewSslCertificateDialog`.
@@ -1786,9 +1805,6 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Get the text from urlTextBox and convert it to a string.  trim() removes white spaces from the beginning and end of the string.
         String unformattedUrlString = urlTextBox.getText().toString().trim();
 
-        URL unformattedUrl = null;
-        Uri.Builder formattedUri = new Uri.Builder();
-
         // Check to see if unformattedUrlString is a valid URL.  Otherwise, convert it into a Duck Duck Go search.
         if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
             // Add http:// at the beginning if it is missing.  Otherwise the app will segfault.
@@ -1796,6 +1812,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 unformattedUrlString = "http://" + unformattedUrlString;
             }
 
+            // Initialize `unformattedUrl`.
+            URL unformattedUrl = null;
+
             // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components.
             try {
                 unformattedUrl = new URL(unformattedUrlString);
@@ -1803,17 +1822,21 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 e.printStackTrace();
             }
 
-            // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value.
+            // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if `.get` was called on a `null` value.
             final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
             final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
             final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
             final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
             final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
 
+            // Build the URI.
+            Uri.Builder formattedUri = new Uri.Builder();
             formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
-            formattedUrlString = formattedUri.build().toString();
+
+            // Decode `formattedUri` as a `String` in `UTF-8`.
+            formattedUrlString = URLDecoder.decode(formattedUri.build().toString(), "UTF-8");
         } else {
-            // Sanitize the search input and convert it to a DuckDuckGo search.
+            // Sanitize the search input and convert it to a search.
             final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
 
             // Use the correct search URL.
@@ -1849,7 +1872,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         String hostname = uri.getHost();
 
         // Only apply the domain settings if `hostname` is not the same as `currentDomain`.  This allows the user to set temporary settings for JavaScript, Cookies, DOM Storage, etc.
-        if (!hostname.equals(currentDomain)) {
+        if (hostname != null && !hostname.equals(currentDomain)) {
             // Set the new `hostname` as the `currentDomain`.
             currentDomain = hostname;