]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
Fix a rare crash on resource requests. https://redmine.stoutner.com/issues/645
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / MainWebViewActivity.java
index 1196fb7a84a92a1c8e2a01e96c348f8be595e205..9d001fe0d2c990ae293973c02fc93837ca4aaaa6 100644 (file)
@@ -332,6 +332,13 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
     private TabLayout tabLayout;
     private SwipeRefreshLayout swipeRefreshLayout;
     private ViewPager webViewPager;
+    private NavigationView navigationView;
+
+    // Declare the class menus.
+    private Menu navigationMenu;
+
+    // Declare the class menu items.
+    private MenuItem navigationRequestsMenuItem;
 
     @Override
     // Remove the warning about needing to override `performClick()` when using an `OnTouchListener` with `WebView`.
@@ -404,6 +411,13 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
         tabLayout = findViewById(R.id.tablayout);
         swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
         webViewPager = findViewById(R.id.webviewpager);
+        navigationView = findViewById(R.id.navigationview);
+
+        // Get handles for the class menus.
+        navigationMenu = navigationView.getMenu();
+
+        // Get a handle for the navigation requests menu item.
+        navigationRequestsMenuItem = navigationMenu.findItem(R.id.requests);
 
         // Get a handle for the app compat delegate.
         AppCompatDelegate appCompatDelegate = getDelegate();
@@ -597,14 +611,16 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 // Get the nested scroll WebView from the tab fragment.
                 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
 
-                // Resume the nested scroll WebView JavaScript timers.
-                nestedScrollWebView.resumeTimers();
-
                 // Resume the nested scroll WebView.
                 nestedScrollWebView.onResume();
             }
         }
 
+        // Resume the nested scroll WebView JavaScript timers.  This is a global command that resumes JavaScript timers on all WebViews.
+        if (currentWebView != null) {
+            currentWebView.resumeTimers();
+        }
+
         // Reapply the proxy settings if the system is using a proxy.  This redisplays the appropriate alert dialog.
         if (!proxyMode.equals(ProxyHelper.NONE)) {
             applyProxy(false);
@@ -648,12 +664,14 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
 
                 // Pause the nested scroll WebView.
                 nestedScrollWebView.onPause();
-
-                // Pause the nested scroll WebView JavaScript timers.
-                nestedScrollWebView.pauseTimers();
             }
         }
 
+        // Pause the WebView JavaScript timers.  This is a global command that pauses JavaScript on all WebViews.
+        if (currentWebView != null) {
+            currentWebView.pauseTimers();
+        }
+
         // Pause the ad or it will continue to consume resources in the background on the free flavor.
         if (BuildConfig.FLAVOR.contentEquals("free")) {
             // Pause the ad.
@@ -1710,8 +1728,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
             Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class);
 
             // Add the variables to the intent.
-            viewSourceIntent.putExtra("user_agent", currentWebView.getSettings().getUserAgentString());
-            viewSourceIntent.putExtra("current_url", currentWebView.getUrl());
+            viewSourceIntent.putExtra(ViewSourceActivityKt.CURRENT_URL, currentWebView.getUrl());
+            viewSourceIntent.putExtra(ViewSourceActivityKt.USER_AGENT, currentWebView.getSettings().getUserAgentString());
 
             // Make it so.
             startActivity(viewSourceIntent);
@@ -3246,8 +3264,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
         this.registerReceiver(orbotStatusBroadcastReceiver, new IntentFilter("org.torproject.android.intent.action.STATUS"));
 
         // Get handles for views that need to be modified.
-        NavigationView navigationView = findViewById(R.id.navigationview);
-        SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
         ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
         FloatingActionButton launchBookmarksActivityFab = findViewById(R.id.launch_bookmarks_activity_fab);
         FloatingActionButton createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab);
@@ -3257,12 +3273,10 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
         // Listen for touches on the navigation menu.
         navigationView.setNavigationItemSelectedListener(this);
 
-        // Get handles for the navigation menu and the back and forward menu items.
-        Menu navigationMenu = navigationView.getMenu();
+        // Get handles for the navigation menu items.
         MenuItem navigationBackMenuItem = navigationMenu.findItem(R.id.back);
         MenuItem navigationForwardMenuItem = navigationMenu.findItem(R.id.forward);
         MenuItem navigationHistoryMenuItem = navigationMenu.findItem(R.id.history);
-        MenuItem navigationRequestsMenuItem = navigationMenu.findItem(R.id.requests);
 
         // Update the web view pager every time a tab is modified.
         webViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@@ -4788,12 +4802,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
     }
 
     private void closeCurrentTab() {
-        // Pause the current WebView.
+        // Pause the current WebView.  This prevents buffered audio from playing after the tab is closed.
         currentWebView.onPause();
 
-        // Pause the current WebView JavaScript timers.
-        currentWebView.pauseTimers();
-
         // Get the current tab number.
         int currentTabNumber = tabLayout.getSelectedTabPosition();
 
@@ -5788,15 +5799,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 // Sanitize the URL.
                 url = sanitizeUrl(url);
 
-                // Get a handle for the navigation view.
-                NavigationView navigationView = findViewById(R.id.navigationview);
-
-                // Get a handle for the navigation menu.
-                Menu navigationMenu = navigationView.getMenu();
-
-                // Get a handle for the navigation requests menu item.
-                MenuItem navigationRequestsMenuItem = navigationMenu.findItem(R.id.requests);
-
                 // Create an empty web resource response to be used if the resource request is blocked.
                 WebResourceResponse emptyWebResourceResponse = new WebResourceResponse("text/plain", "utf8", new ByteArrayInputStream("".getBytes()));