From 1054eda0dd47e1dc03a243aee74845a2a08e2069 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Fri, 10 Aug 2018 22:14:50 -0700 Subject: [PATCH] Add option to stop loading the WebView. https://redmine.stoutner.com/issues/252 --- .../activities/MainWebViewActivity.java | 71 +++++++++++++++++-- .../drawable/{close.xml => close_dark.xml} | 6 +- app/src/main/res/drawable/close_light.xml | 13 ++++ .../main/res/layout/find_on_page_app_bar.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 5 files changed, 82 insertions(+), 11 deletions(-) rename app/src/main/res/drawable/{close.xml => close_dark.xml} (56%) create mode 100644 app/src/main/res/drawable/close_light.xml diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index 99a2ce30..cc232311 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -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_dark.xml similarity index 56% rename from app/src/main/res/drawable/close.xml rename to app/src/main/res/drawable/close_dark.xml index 9037b8a0..38a05299 100644 --- a/app/src/main/res/drawable/close.xml +++ b/app/src/main/res/drawable/close_dark.xml @@ -1,4 +1,4 @@ - + - + diff --git a/app/src/main/res/drawable/close_light.xml b/app/src/main/res/drawable/close_light.xml new file mode 100644 index 00000000..a7e07956 --- /dev/null +++ b/app/src/main/res/drawable/close_light.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/app/src/main/res/layout/find_on_page_app_bar.xml b/app/src/main/res/layout/find_on_page_app_bar.xml index d657edcb..247bb691 100644 --- a/app/src/main/res/layout/find_on_page_app_bar.xml +++ b/app/src/main/res/layout/find_on_page_app_bar.xml @@ -75,7 +75,7 @@ Privacy Browser Web Page Add to Home Screen Refresh + Stop Load URL -- 2.43.0