]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
Updates about_licenses, adding the full text of the Apache License 2.0 and the 3...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
index 0e6832f9a777a1dbf18c3b3ea3ed0a6c751b285e..353a8d16407ddb6f182921d15dc22916b250e951 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015-2017 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2015-2017 Soren Stoutner <soren@stoutner.com>.
  *
  * Download cookie code contributed 2017 Hendrik Knackstedt.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
  *
@@ -194,11 +194,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // 'homepage' is used in `onCreate()`, `onNavigationItemSelected()`, and `applyAppSettings()`.
     private String homepage;
 
-    // `javaScriptDisabledSearchURL` is used in `loadURLFromTextBox()` and `applyAppSettings()`.
-    private String javaScriptDisabledSearchURL;
-
-    // `javaScriptEnabledSearchURL` is used in `loadURLFromTextBox()` and `applyAppSettings()`.
-    private String javaScriptEnabledSearchURL;
+    // `searchURL` is used in `loadURLFromTextBox()` and `applyAppSettings()`.
+    private String searchURL;
 
     // `adBlockerEnabled` is used in `onCreate()` and `applyAppSettings()`.
     private boolean adBlockerEnabled;
@@ -571,8 +568,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             @SuppressWarnings("deprecation")
             @Override
             public boolean shouldOverrideUrlLoading(WebView view, String url) {
-                // Use an external email program if the link begins with `mailto:`.
-                if (url.startsWith("mailto:")) {
+                if (url.startsWith("mailto:")) {  // Load the URL in an external email program because it begins with `mailto:`.
                     // We use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
                     Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
 
@@ -584,10 +580,15 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
 
                     // Make it so.
                     startActivity(emailIntent);
+
+                    // Returning `true` indicates the application is handling the URL.
                     return true;
                 } else {  // Load the URL in Privacy Browser.
-                    loadUrl(url);
-                    return true;
+                    // Apply the domain settings for the new URL.
+                    applyDomainSettings(url);
+
+                    // Returning `false` causes the current `WebView` to handle the URL and prevents it from adding redirects to the history list.
+                    return false;
                 }
             }
 
@@ -666,7 +667,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                         privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/cache");
 
                         // Delete the `app_webview` folder, which contains an additional `WebView` cache.  See `https://code.google.com/p/android/issues/detail?id=233826&thanks=233826&ts=1486670530`.
-                        privacyBrowserRuntime.exec("rm -rf " + privacyBrowserRuntime + "/app_webview");
+                        privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview");
                     } catch (IOException e) {
                         // Do nothing if an error is thrown.
                     }
@@ -825,6 +826,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Hide zoom controls.
         mainWebView.getSettings().setDisplayZoomControls(false);
 
+        // Set `mainWebView` to use a wide viewport.  Otherwise, some web pages will be scrunched and some content will render outside the screen.
+        mainWebView.getSettings().setUseWideViewPort(true);
+
+        // Set `mainWebView` to load in overview mode (zoomed out to the maximum width).
+        mainWebView.getSettings().setLoadWithOverviewMode(true);
+
         // Initialize cookieManager.
         cookieManager = CookieManager.getInstance();
 
@@ -1229,10 +1236,21 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 return true;
 
             case R.id.share:
+                // Setup the share string.
+                String shareString;
+                if (webViewTitle != null) {
+                    shareString = webViewTitle + " – " + urlTextBox.getText().toString();
+                } else {
+                    shareString = urlTextBox.getText().toString();
+                }
+
+                // Create the share intent.
                 Intent shareIntent = new Intent();
                 shareIntent.setAction(Intent.ACTION_SEND);
-                shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString());
+                shareIntent.putExtra(Intent.EXTRA_TEXT, shareString);
                 shareIntent.setType("text/plain");
+
+                // Make it so.
                 startActivity(Intent.createChooser(shareIntent, "Share URL"));
                 return true;
 
@@ -1417,7 +1435,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                     privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/cache");
 
                     // Delete the `app_webview` folder, which contains an additional `WebView` cache.  See `https://code.google.com/p/android/issues/detail?id=233826&thanks=233826&ts=1486670530`.
-                    privacyBrowserRuntime.exec("rm -rf " + privacyBrowserRuntime + "/app_webview");
+                    privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview");
                 } catch (IOException e) {
                     // Do nothing if an error is thrown.
                 }
@@ -1863,7 +1881,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         String unformattedUrlString = urlTextBox.getText().toString().trim();
 
         // Check to see if `unformattedUrlString` is a valid URL.  Otherwise, convert it into a search.
-        if ((Patterns.WEB_URL.matcher(unformattedUrlString).matches()) || (unformattedUrlString.contains("localhost"))) {
+        if ((Patterns.WEB_URL.matcher(unformattedUrlString).matches()) || (unformattedUrlString.startsWith("http://")) || (unformattedUrlString.startsWith("https://"))) {
             // Add `http://` at the beginning if it is missing.  Otherwise the app will segfault.
             if (!unformattedUrlString.startsWith("http")) {
                 unformattedUrlString = "http://" + unformattedUrlString;
@@ -1896,12 +1914,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             // Sanitize the search input and convert it to a search.
             final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
 
-            // Use the correct search URL.
-            if (javaScriptEnabled) {  // JavaScript is enabled.
-                formattedUrlString = javaScriptEnabledSearchURL + encodedUrlString;
-            } else { // JavaScript is disabled.
-                formattedUrlString = javaScriptDisabledSearchURL + encodedUrlString;
-            }
+            // Add the base search URL.
+            formattedUrlString = searchURL + encodedUrlString;
         }
 
         loadUrl(formattedUrlString);
@@ -2130,16 +2144,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
 
         // Store the values from `sharedPreferences` in variables.
-        String javaScriptDisabledSearchString = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
-        String javaScriptDisabledSearchCustomURLString = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
-        String javaScriptEnabledSearchString = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
-        String javaScriptEnabledSearchCustomURLString = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
         String homepageString = sharedPreferences.getString("homepage", "https://duckduckgo.com");
         String torHomepageString = sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion");
-        String torJavaScriptDisabledSearchString = sharedPreferences.getString("tor_javascript_disabled_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
-        String torJavaScriptDisabledSearchCustomURLString = sharedPreferences.getString("tor_javascript_disabled_search_custom_url", "");
-        String torJavaScriptEnabledSearchString = sharedPreferences.getString("tor_javascript_enabled_search", "https://3g2upl4pq6kufc4m.onion/?q=");
-        String torJavaScriptEnabledSearchCustomURLString = sharedPreferences.getString("tor_javascript_enabled_search_custom_url", "");
+        String torSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
+        String torSearchCustomURLString = sharedPreferences.getString("tor_search_custom_url", "");
+        String searchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
+        String searchCustomURLString = sharedPreferences.getString("search_custom_url", "");
         adBlockerEnabled = sharedPreferences.getBoolean("block_ads", true);
         incognitoModeEnabled = sharedPreferences.getBoolean("incognito_mode", false);
         boolean doNotTrackEnabled = sharedPreferences.getBoolean("do_not_track", false);
@@ -2159,18 +2169,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 formattedUrlString = homepage;
             }
 
-            // Set JavaScript disabled search.
-            if (torJavaScriptDisabledSearchString.equals("Custom URL")) {  // Get the custom URL string.
-                javaScriptDisabledSearchURL = torJavaScriptDisabledSearchCustomURLString;
-            } else {  // Use the string from the pre-built list.
-                javaScriptDisabledSearchURL = torJavaScriptDisabledSearchString;
-            }
-
-            // Set JavaScript enabled search.
-            if (torJavaScriptEnabledSearchString.equals("Custom URL")) {  // Get the custom URL string.
-                javaScriptEnabledSearchURL = torJavaScriptEnabledSearchCustomURLString;
+            // Set the search URL.
+            if (torSearchString.equals("Custom URL")) {  // Get the custom URL string.
+                searchURL = torSearchCustomURLString;
             } else {  // Use the string from the pre-built list.
-                javaScriptEnabledSearchURL = torJavaScriptEnabledSearchString;
+                searchURL = torSearchString;
             }
 
             // Set the proxy.  `this` refers to the current activity where an `AlertDialog` might be displayed.
@@ -2193,18 +2196,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 formattedUrlString = homepage;
             }
 
-            // Set JavaScript disabled search.
-            if (javaScriptDisabledSearchString.equals("Custom URL")) {  // Get the custom URL string.
-                javaScriptDisabledSearchURL = javaScriptDisabledSearchCustomURLString;
+            // Set the search URL.
+            if (searchString.equals("Custom URL")) {  // Get the custom URL string.
+                searchURL = searchCustomURLString;
             } else {  // Use the string from the pre-built list.
-                javaScriptDisabledSearchURL = javaScriptDisabledSearchString;
-            }
-
-            // Set JavaScript enabled search.
-            if (javaScriptEnabledSearchString.equals("Custom URL")) {  // Get the custom URL string.
-                javaScriptEnabledSearchURL = javaScriptEnabledSearchCustomURLString;
-            } else {  // Use the string from the pre-built list.
-                javaScriptEnabledSearchURL = javaScriptEnabledSearchString;
+                searchURL = searchString;
             }
 
             // Reset the proxy to default.  The host is `""` and the port is `"0"`.
@@ -2257,8 +2253,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             // Reset `inFullScreenBrowsingMode` to `false`.
             inFullScreenBrowsingMode = false;
 
-            // Show the `appBar`.
-            appBar.show();
+            // Show the `appBar` if `findOnPageLinearLayout` is not visible.
+            if (findOnPageLinearLayout.getVisibility() == View.GONE) {
+                appBar.show();
+            }
 
             // Show the `BannerAd` in the free flavor.
             if (BuildConfig.FLAVOR.contentEquals("free")) {