]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/commitdiff
Add option to stop loading the WebView. https://redmine.stoutner.com/issues/252
authorSoren Stoutner <soren@stoutner.com>
Sat, 11 Aug 2018 05:14:50 +0000 (22:14 -0700)
committerSoren Stoutner <soren@stoutner.com>
Sat, 11 Aug 2018 05:14:50 +0000 (22:14 -0700)
app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
app/src/main/res/drawable/close.xml [deleted file]
app/src/main/res/drawable/close_dark.xml [new file with mode: 0644]
app/src/main/res/drawable/close_light.xml [new file with mode: 0644]
app/src/main/res/layout/find_on_page_app_bar.xml
app/src/main/res/values/strings.xml

index 99a2ce30806441bbf5f3f9051f95d0d0899a74b3..cc232311a0a8266977cee1da2fd25934bda19e58 100644 (file)
@@ -411,6 +411,12 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
     // `mainMenu` is used in `onCreateOptionsMenu()` and `updatePrivacyIcons()`.
     private Menu mainMenu;
 
+    // `refreshMenuItem` is used in `onCreate()` and `onCreateOptionsItem()`.
+    private MenuItem refreshMenuItem;
+
+    // `displayAdditionalAppBarIcons` is used in `onCreate()` and `onCreateOptionsMenu()`.
+    private boolean displayAdditionalAppBarIcons;
+
     // `drawerToggle` is used in `onCreate()`, `onPostCreate()`, `onConfigurationChanged()`, `onNewIntent()`, and `onNavigationItemSelected()`.
     private ActionBarDrawerToggle drawerToggle;
 
@@ -437,7 +443,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
     // `mainWebViewRelativeLayout` is used in `onCreate()` and `onNavigationItemSelected()`.
     private RelativeLayout mainWebViewRelativeLayout;
 
-    // `urlIsLoading` is used in `onCreate()`, `loadUrl()`, and `applyDomainSettings()`.
+    // `urlIsLoading` is used in `onCreate()`, `onCreateOptionsMenu()`, `loadUrl()`, and `applyDomainSettings()`.
     private boolean urlIsLoading;
 
     // `pinnedDomainSslCertificate` is used in `onCreate()` and `applyDomainSettings()`.
@@ -1513,6 +1519,21 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
 
                     // Set `urlIsLoading` to `true`, so that redirects while loading do not trigger changes in the user agent, which forces another reload of the existing page.
                     urlIsLoading = true;
+
+                    // Replace Refresh with Stop if the menu item has been created.  (The WebView typically begins loading before the menu items are instantiated.)
+                    if (refreshMenuItem != null) {
+                        // Set the title.
+                        refreshMenuItem.setTitle(R.string.stop);
+
+                        // If the icon is displayed in the AppBar, set it according to the theme.
+                        if (displayAdditionalAppBarIcons) {
+                            if (darkTheme) {
+                                refreshMenuItem.setIcon(R.drawable.close_dark);
+                            } else {
+                                refreshMenuItem.setIcon(R.drawable.close_light);
+                            }
+                        }
+                    }
                 }
             }
 
@@ -1524,6 +1545,18 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                     cookieManager.flush();
                 }
 
+                // Reset the Refresh title.
+                refreshMenuItem.setTitle(R.string.refresh);
+
+                // If the icon is displayed in the AppBar, reset it according to the theme.
+                if (displayAdditionalAppBarIcons) {
+                    if (darkTheme) {
+                        refreshMenuItem.setIcon(R.drawable.refresh_enabled_dark);
+                    } else {
+                        refreshMenuItem.setIcon(R.drawable.refresh_enabled_light);
+                    }
+                }
+
                 // Reset `urlIsLoading`, which is used to prevent reloads on redirect if the user agent changes.
                 urlIsLoading = false;
 
@@ -1866,7 +1899,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
         MenuItem toggleDomStorageMenuItem = menu.findItem(R.id.toggle_dom_storage);
         MenuItem toggleSaveFormDataMenuItem = menu.findItem(R.id.toggle_save_form_data);  // Form data can be removed once the minimum API >= 26.
         MenuItem clearFormDataMenuItem = menu.findItem(R.id.clear_form_data);  // Form data can be removed once the minimum API >= 26.
-        MenuItem refreshMenuItem = menu.findItem(R.id.refresh);
+        refreshMenuItem = menu.findItem(R.id.refresh);
         MenuItem adConsentMenuItem = menu.findItem(R.id.ad_consent);
 
         // Only display third-party cookies if API >= 21
@@ -1879,11 +1912,14 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
         // Only show Ad Consent if this is the free flavor.
         adConsentMenuItem.setVisible(BuildConfig.FLAVOR.contentEquals("free"));
 
-        // Get the shared preference values.  `this` references the current context.
+        // Get the shared preference values.
         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
 
+        // Get the status of the additional AppBar icons.
+        displayAdditionalAppBarIcons = sharedPreferences.getBoolean("display_additional_app_bar_icons", false);
+
         // Set the status of the additional app bar icons.  Setting the refresh menu item to `SHOW_AS_ACTION_ALWAYS` makes it appear even on small devices like phones.
-        if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
+        if (displayAdditionalAppBarIcons) {
             toggleFirstPartyCookiesMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
             toggleDomStorageMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
             refreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
@@ -1893,6 +1929,21 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
             refreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
         }
 
+        // Replace Refresh with Stop if a URL is already loading.
+        if (urlIsLoading) {
+            // Set the title.
+            refreshMenuItem.setTitle(R.string.stop);
+
+            // If the icon is displayed in the AppBar, set it according to the theme.
+            if (displayAdditionalAppBarIcons) {
+                if (darkTheme) {
+                    refreshMenuItem.setIcon(R.drawable.close_dark);
+                } else {
+                    refreshMenuItem.setIcon(R.drawable.close_light);
+                }
+            }
+        }
+
         return true;
     }
 
@@ -2500,7 +2551,13 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
                 return true;
 
             case R.id.refresh:
-                mainWebView.reload();
+                if (menuItem.getTitle().equals(getString(R.string.refresh))) {  // The refresh button was pushed.
+                    // Reload the WebView.
+                    mainWebView.reload();
+                } else {  // The stop button was pushed.
+                    // Stop the loading of the WebView.
+                    mainWebView.stopLoading();
+                }
                 return true;
 
             case R.id.ad_consent:
@@ -3517,8 +3574,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook
         // Apply the domain settings.
         applyDomainSettings(url, true, false);
 
-        // Set `urlIsLoading` to prevent changes in the user agent on websites with redirects from reloading the current website.
-        urlIsLoading = true;
+        // If loading a website, set `urlIsLoading` to prevent changes in the user agent on websites with redirects from reloading the current website.
+        urlIsLoading = !url.equals("");
 
         // Load the URL.
         mainWebView.loadUrl(url, customHeaders);
diff --git a/app/src/main/res/drawable/close.xml b/app/src/main/res/drawable/close.xml
deleted file mode 100644 (file)
index 9037b8a..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-<!-- `close.xml` comes from the Android Material icon set, where it is called `close`.  It is released under the Apache License 2.0. -->
-<vector
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:height="24dp"
-    android:width="24dp"
-    android:viewportHeight="24.0"
-    android:viewportWidth="24.0" >
-
-    <!-- We have to use a hard coded color until API >= 21.  Then we can use `@color`. -->
-    <path
-        android:fillColor="#FF000000"
-        android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
-</vector>
diff --git a/app/src/main/res/drawable/close_dark.xml b/app/src/main/res/drawable/close_dark.xml
new file mode 100644 (file)
index 0000000..38a0529
--- /dev/null
@@ -0,0 +1,13 @@
+<!-- `close_dark.xml` comes from the Android Material icon set, where it is called `close`.  It is released under the Apache License 2.0. -->
+<vector
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:height="24dp"
+    android:width="24dp"
+    android:viewportHeight="24.0"
+    android:viewportWidth="24.0" >
+
+    <!-- A hard coded color must be used until API >= 21.  Then `@color` can be used. -->
+    <path
+        android:fillColor="#FF1E88E5"
+        android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
+</vector>
diff --git a/app/src/main/res/drawable/close_light.xml b/app/src/main/res/drawable/close_light.xml
new file mode 100644 (file)
index 0000000..a7e0795
--- /dev/null
@@ -0,0 +1,13 @@
+<!-- `close_light.xml` comes from the Android Material icon set, where it is called `close`.  It is released under the Apache License 2.0. -->
+<vector
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:height="24dp"
+    android:width="24dp"
+    android:viewportHeight="24.0"
+    android:viewportWidth="24.0" >
+
+    <!-- A hard coded color must be used until API >= 21.  Then `@color` can be used. -->
+    <path
+        android:fillColor="#FF1565C0"
+        android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
+</vector>
index d657edcb8ee6d0b49a42060ad5c32d1de4868f70..247bb691182269efaa06411730998ce26060b5f8 100644 (file)
@@ -75,7 +75,7 @@
 
     <ImageView
         android:id="@+id/close_find"
-        android:src="@drawable/close"
+        android:src="@drawable/close_light"
         android:layout_width="35dp"
         android:layout_height="35dp"
         android:layout_marginStart="4dp"
index aefc1747220c7cd89f2d92b59b81586b8b9c6742..ce962578bab13215d4b75f07e256445366fb539d 100644 (file)
         <string name="privacy_browser_web_page">Privacy Browser Web Page</string>
     <string name="add_to_home_screen">Add to Home Screen</string>
     <string name="refresh">Refresh</string>
+    <string name="stop">Stop</string>
 
     <!-- Context Menus. -->
     <string name="load_url">Load URL</string>