]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
Use a boolean to track if we are waiting for Orbot. Fixes https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
index 292292fa0f3565a1c3f92ca2d698d8f7ed58716b..70f8581d98879d80b760b0eb4ee78f7e020381d8 100644 (file)
@@ -203,6 +203,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // `adBlockerEnabled` is used in `onCreate()` and `applyAppSettings()`.
     private boolean adBlockerEnabled;
 
+    // `privacyBrowserRuntime` is used in `onCreate()` and `applyAppSettings()`.
+    Runtime privacyBrowserRuntime;
+
+    // `incognitoModeEnabled` is used in `onCreate()` and `applyAppSettings()`.
+    private boolean incognitoModeEnabled;
+
     // `fullScreenBrowsingModeEnabled` is used in `onCreate()` and `applyAppSettings()`.
     private boolean fullScreenBrowsingModeEnabled;
 
@@ -221,12 +227,15 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // `currentDomainName` is used in `onCreate(), `onNavigationItemSelected()`, and `applyDomainSettings()`.
     private String currentDomainName;
 
-    // `pendingUrl` is used in `onCreate()` and `applyAppSettings()`.
-    private static String pendingUrl;
+    // `waitingForOrbot` is used in `onCreate()` and `applyAppSettings()`.
+    private boolean waitingForOrbot;
 
     // `waitingForOrbotData` is used in `onCreate()` and `applyAppSettings()`.
     private String waitingForOrbotHTMLString;
 
+    // `privateDataDirectoryString` is used in `onCreate()` and `onNavigationItemSelected()`.
+    private String privateDataDirectoryString;
+
     // `findOnPageLinearLayout` is used in `onCreate()`, `onOptionsItemSelected()`, and `closeFindOnPage()`.
     private LinearLayout findOnPageLinearLayout;
 
@@ -304,10 +313,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Set `waitingForOrbotHTMLString`.
         waitingForOrbotHTMLString = "<html><body><br/><center><h1>" + getString(R.string.waiting_for_orbot) + "</h1></center></body></html>";
 
-        // Initialize `currentDomainName`, `pendingUrl`, and `orbotStatus`.
+        // Initialize `currentDomainName`, `orbotStatus`, and `waitingForOrbot`.
         currentDomainName = "";
-        pendingUrl = "";
         orbotStatus = "unknown";
+        waitingForOrbot = false;
 
         // Create an Orbot status `BroadcastReceiver`.
         BroadcastReceiver orbotStatusBroadcastReceiver = new BroadcastReceiver() {
@@ -316,19 +325,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Store the content of the status message in `orbotStatus`.
                 orbotStatus = intent.getStringExtra("org.torproject.android.intent.extra.STATUS");
 
-                // If we are waiting on `pendingUrl`, load it now that Orbot is connected.
-                if (orbotStatus.equals("ON") && !pendingUrl.isEmpty()) {
-
-                    // Wait 500 milliseconds, because Orbot isn't really ready yet.
-                    try {
-                        Thread.sleep(500);
-                    } catch (InterruptedException exception) {
-                        // Do nothing.
-                    }
-
-                    // Copy `pendingUrl` to `formattedUrlString` and reset `pendingUrl` to be empty.
-                    formattedUrlString = pendingUrl;
-                    pendingUrl = "";
+                // If we are waiting on Orbot, load the website now that Orbot is connected.
+                if (orbotStatus.equals("ON") && waitingForOrbot) {
+                    // Reset `waitingForOrbot`.
+                    waitingForOrbot = false;
 
                     // Load `formattedUrlString
                     loadUrl(formattedUrlString);
@@ -638,7 +638,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 webViewTitle = getString(R.string.no_title);
 
                 // 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.
+                if (!waitingForOrbot) {  // We are not waiting on Orbot, so we need to process the URL.
                     // 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;
 
@@ -655,8 +655,28 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             // Update formattedUrlString and urlTextBox.  It is necessary to do this after the page finishes loading because the final URL can change during load.
             @Override
             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.
+                // Clear the cache and history if Incognito Mode is enabled.
+                if (incognitoModeEnabled) {
+                    // Clear the cache.  `true` includes disk files.
+                    mainWebView.clearCache(true);
+
+                    // Clear the back/forward history.
+                    mainWebView.clearHistory();
+
+                    // Manually delete cache folders.
+                    try {
+                        // Delete the main `cache` folder.
+                        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");
+                    } catch (IOException e) {
+                        // Do nothing if an error is thrown.
+                    }
+                }
+
+                // Update `urlTextBox` and apply domain settings if not waiting on Orbot.
+                if (!waitingForOrbot && !url.startsWith("data:text/html,<html><body><br/><center><h1>")) {  // Sometimes `waitingForOrbot` is reset while the Orbot message `onPageFinished()` is running, causing a race condition.  For this reason we check both.
                     // 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 `""`.
@@ -828,6 +848,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             formattedUrlString = launchingIntentUriData.toString();
         }
 
+        // Get a handle for the `Runtime`.
+        privacyBrowserRuntime = Runtime.getRuntime();
+
+        // Store the application's private data directory.
+        privateDataDirectoryString = getApplicationInfo().dataDir;  // `dataDir` will vary, but will be something like `/data/user/0/com.stoutner.privacybrowser.standard`, which links to `/data/data/com.stoutner.privacybrowser.standard`.
+
         // Initialize `inFullScreenBrowsingMode`, which is always false at this point because Privacy Browser never starts in full screen browsing mode.
         inFullScreenBrowsingMode = false;
 
@@ -855,12 +881,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // If the favorite icon is null, load the default.
         if (favoriteIconBitmap == null) {
             favoriteIconBitmap = favoriteIconDefaultBitmap;
+        }
 
-        // Load `formattedUrlString` if we are not proxying through Orbot and waiting for Orbot to connect.
-        if (!(proxyThroughOrbot && !orbotStatus.equals("ON"))) {
+        // Load `formattedUrlString` if we are not waiting for Orbot to connect.
+        if (!waitingForOrbot) {
             loadUrl(formattedUrlString);
         }
-        }
     }
 
 
@@ -1358,7 +1384,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 WebViewDatabase webViewDatabase = WebViewDatabase.getInstance(this);
                 webViewDatabase.clearFormData();
 
-                // Clear cache.  The argument of "true" includes disk files.
+                // Clear the cache.  `true` includes disk files.
                 mainWebView.clearCache(true);
 
                 // Clear the back/forward history.
@@ -1379,15 +1405,13 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Destroy the internal state of `mainWebView`.
                 mainWebView.destroy();
 
-                // Manually delete folders.
-                Runtime runtime = Runtime.getRuntime();
-                String dataDirString = getApplicationInfo().dataDir;  // `dataDir` will vary, but will be something like `/data/user/0/com.stoutner.privacybrowser.standard`, which links to `/data/data/com.stoutner.privacybrowser.standard`.
+                // Manually delete cache folders.
                 try {
                     // Delete the main `cache` folder.
-                    runtime.exec("rm -rf " + dataDirString + "/cache");
+                    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`.
-                    runtime.exec("rm -rf " + dataDirString + "/app_webview");
+                    privacyBrowserRuntime.exec("rm -rf " + privacyBrowserRuntime + "/app_webview");
                 } catch (IOException e) {
                     // Do nothing if an error is thrown.
                 }
@@ -2110,13 +2134,14 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         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", "");
-        swipeToRefreshEnabled = sharedPreferences.getBoolean("swipe_to_refresh_enabled", false);
         adBlockerEnabled = sharedPreferences.getBoolean("block_ads", true);
+        incognitoModeEnabled = sharedPreferences.getBoolean("incognito_mode", false);
         boolean doNotTrackEnabled = sharedPreferences.getBoolean("do_not_track", false);
         proxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
         fullScreenBrowsingModeEnabled = sharedPreferences.getBoolean("enable_full_screen_browsing_mode", false);
         hideSystemBarsOnFullscreen = sharedPreferences.getBoolean("hide_system_bars", false);
         translucentNavigationBarOnFullscreen = sharedPreferences.getBoolean("translucent_navigation_bar", true);
+        swipeToRefreshEnabled = sharedPreferences.getBoolean("swipe_to_refresh", false);
 
         // Set the homepage, search, and proxy options.
         if (proxyThroughOrbot) {  // Set the Tor options.
@@ -2147,8 +2172,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
 
             // Display a message to the user if we are waiting on Orbot.
             if (!orbotStatus.equals("ON")) {
-                // Save `formattedUrlString` in `pendingUrl`.
-                pendingUrl = formattedUrlString;
+                // Set `waitingForOrbot`.
+                waitingForOrbot = true;
 
                 // Load a waiting page.  `null` specifies no encoding, which defaults to ASCII.
                 mainWebView.loadData(waitingForOrbotHTMLString, "text/html", null);
@@ -2179,11 +2204,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             // Reset the proxy to default.  The host is `""` and the port is `"0"`.
             OrbotProxyHelper.setProxy(getApplicationContext(), this, "", "0");
 
-            // Reset `pendingUrl` if we are currently waiting for Orbot to connect.
-            if (!pendingUrl.isEmpty()) {
-                formattedUrlString = pendingUrl;
-                pendingUrl = "";
-            }
+            // Reset `waitingForOrbot.
+            waitingForOrbot = false;
         }
 
         // Set swipe to refresh.