]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java
Bump the minimum API to 23. https://redmine.stoutner.com/issues/793
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / SettingsFragment.java
index c6a1493e645e96fd094217fae3df80b891d1c139..bac86c1e1eb3dda46e0346bce93c2d97fba8f1c6 100644 (file)
@@ -94,6 +94,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
     private Preference swipeToRefreshPreference;
     private Preference downloadWithExternalAppPreference;
     private Preference scrollAppBarPreference;
+    private Preference bottomAppBarPreference;
     private Preference displayAdditionalAppBarIconsPreference;
     private Preference appThemePreference;
     private Preference webViewThemePreference;
@@ -117,7 +118,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         // Get the current theme status.
         currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
 
-        // // Get a handle for the shared preferences.
+        // Get a handle for the shared preferences.
         SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
 
         // Get handles for the preferences.
@@ -142,7 +143,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         searchPreference = findPreference("search");
         searchCustomURLPreference = findPreference("search_custom_url");
         proxyPreference = findPreference("proxy");
-        proxyCustomUrlPreference = findPreference("proxy_custom_url");
+        proxyCustomUrlPreference = findPreference(getString(R.string.proxy_custom_url_key));
         fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
         hideAppBarPreference = findPreference("hide_app_bar");
         clearEverythingPreference = findPreference("clear_everything");
@@ -156,7 +157,8 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         openIntentsInNewTabPreference = findPreference("open_intents_in_new_tab");
         swipeToRefreshPreference = findPreference("swipe_to_refresh");
         downloadWithExternalAppPreference = findPreference(getString(R.string.download_with_external_app_key));
-        scrollAppBarPreference = findPreference("scroll_app_bar");
+        scrollAppBarPreference = findPreference(getString(R.string.scroll_app_bar_key));
+        bottomAppBarPreference = findPreference(getString(R.string.bottom_app_bar_key));
         displayAdditionalAppBarIconsPreference = findPreference(getString(R.string.display_additional_app_bar_icons_key));
         appThemePreference = findPreference("app_theme");
         webViewThemePreference = findPreference("webview_theme");
@@ -200,6 +202,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         assert swipeToRefreshPreference != null;
         assert downloadWithExternalAppPreference != null;
         assert scrollAppBarPreference != null;
+        assert bottomAppBarPreference != null;
         assert displayAdditionalAppBarIconsPreference != null;
         assert appThemePreference != null;
         assert webViewThemePreference != null;
@@ -313,11 +316,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
                 break;
 
             case ProxyHelper.TOR:
-                if (Build.VERSION.SDK_INT == 19) {  // Proxying through SOCKS doesn't work on Android KitKat.
-                    proxyPreference.setSummary(getString(R.string.tor_enabled_kitkat));
-                } else {
-                    proxyPreference.setSummary(getString(R.string.tor_enabled));
-                }
+                proxyPreference.setSummary(getString(R.string.tor_enabled));
                 break;
 
             case ProxyHelper.I2P:
@@ -330,10 +329,10 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         }
 
         // Set the summary text for the custom proxy URL.
-        proxyCustomUrlPreference.setSummary(sharedPreferences.getString("proxy_custom_url", getString(R.string.proxy_custom_url_default_value)));
+        proxyCustomUrlPreference.setSummary(sharedPreferences.getString(getString(R.string.proxy_custom_url_key), getString(R.string.proxy_custom_url_default_value)));
 
         // Only enable the custom proxy URL if a custom proxy is selected.
-        proxyCustomUrlPreference.setEnabled(proxyString.equals("Custom"));
+        proxyCustomUrlPreference.setEnabled(proxyString.equals(ProxyHelper.CUSTOM));
 
 
         // Set the status of the clear and exit preferences.
@@ -400,20 +399,8 @@ public class SettingsFragment extends PreferenceFragmentCompat {
             webViewThemeEntryNumber = 0;
         }
 
-        // Set the visibility of the WebView theme preference.
-        if (Build.VERSION.SDK_INT < 21) {  // The device is running API 19.
-            // Get a handle for the general category.
-            PreferenceCategory generalCategory = findPreference("general");
-
-            // Remove the incorrect lint warning below that the general preference category might be null.
-            assert generalCategory != null;
-
-            // Remove the WebView theme preference.
-            generalCategory.removePreference(webViewThemePreference);
-        } else {  // The device is running API >= 21
-            // Set the current theme as the summary text for the preference.
-            webViewThemePreference.setSummary(webViewThemeEntriesStringArray[webViewThemeEntryNumber]);
-        }
+        // Set the current theme as the summary text for the preference.
+        webViewThemePreference.setSummary(webViewThemeEntriesStringArray[webViewThemeEntryNumber]);
 
 
         // Set the JavaScript icon.
@@ -686,7 +673,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         }
 
         // Set the Proxy icons according to the theme and status.
-        if (proxyString.equals("None")) {  // Proxying is disabled.
+        if (proxyString.equals(ProxyHelper.NONE)) {  // Proxying is disabled.
             if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {  // Dark theme.
                 // Set the main proxy icon to be disabled.
                 proxyPreference.setIcon(R.drawable.proxy_disabled_night);
@@ -874,7 +861,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         }
 
         // Set the scroll app bar preference icon.
-        if (sharedPreferences.getBoolean("scroll_app_bar", true)) {
+        if (sharedPreferences.getBoolean(getString(R.string.scroll_app_bar_key), true)) {
             if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
                 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_day);
             } else {
@@ -888,6 +875,21 @@ public class SettingsFragment extends PreferenceFragmentCompat {
             }
         }
 
+        // Set the bottom app bar preference icon.
+        if (sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)) {
+            if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
+                bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled_day);
+            } else {
+                bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled_night);
+            }
+        } else {
+            if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
+                bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled_day);
+            } else {
+                bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled_night);
+            }
+        }
+
         // Set the display additional app bar icons preference icon.
         if (sharedPreferences.getBoolean(getString(R.string.display_additional_app_bar_icons_key), false)) {
             if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
@@ -961,12 +963,6 @@ public class SettingsFragment extends PreferenceFragmentCompat {
                 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_day);
             }
         }
-
-        // Get the shared preferences change listener.
-        sharedPreferenceChangeListener = getSharedPreferencesChangeListener(requireContext());
-
-        // Register the listener.
-        sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
     }
 
     // The listener should be unregistered when the app is paused.
@@ -978,7 +974,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         // Get a handle for the shared preferences.
         SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
 
-        // Unregister the shared preferences listener.
+        // Unregister the shared preference listener.
         sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
     }
 
@@ -988,18 +984,19 @@ public class SettingsFragment extends PreferenceFragmentCompat {
         // Run the default commands.
         super.onResume();
 
-        // Get a new shared preferences change listener.
-        sharedPreferenceChangeListener = getSharedPreferencesChangeListener(requireContext());
+        // Get a new shared preference change listener.
+        sharedPreferenceChangeListener = getSharedPreferenceChangeListener(requireContext());
 
         // Get a handle for the shared preferences.
         SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
 
-        // Re-register the shared preferences listener.
+        // Re-register the shared preference listener.
         sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
     }
 
     // The context must be passed to the shared preference change listener or else any calls to the system `getString()` will crash if the app has been restarted.
-    private SharedPreferences.OnSharedPreferenceChangeListener getSharedPreferencesChangeListener(Context context) {
+    // This can be removed at some future point, perhaps after the switch to PreferenceScreenCompat.  It isn't an issue in Privacy Cell.
+    private SharedPreferences.OnSharedPreferenceChangeListener getSharedPreferenceChangeListener(Context context) {
         // Return the shared preference change listener.
         return (SharedPreferences sharedPreferences, String key) -> {
             switch (key) {
@@ -1174,29 +1171,8 @@ public class SettingsFragment extends PreferenceFragmentCompat {
                         }
                     }
 
-                    // Create an intent to restart Privacy Browser.
-                    Intent allowScreenshotsRestartIntent = requireActivity().getParentActivityIntent();
-
-                    // Assert that the intent is not null to remove the lint error below.
-                    assert allowScreenshotsRestartIntent != null;
-
-                    // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack.  It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
-                    allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
-
-                    // Create a handler to restart the activity.
-                    Handler allowScreenshotsRestartHandler = new Handler(Looper.getMainLooper());
-
-                    // Create a runnable to restart the activity.
-                    Runnable allowScreenshotsRestartRunnable = () -> {
-                        // Restart the activity.
-                        startActivity(allowScreenshotsRestartIntent);
-
-                        // Kill this instance of Privacy Browser.  Otherwise, the app exhibits sporadic behavior after the restart.
-                        System.exit(0);
-                    };
-
-                    // Restart the activity after 150 milliseconds, so that the app has enough time to save the change to the preference.
-                    allowScreenshotsRestartHandler.postDelayed(allowScreenshotsRestartRunnable, 150);
+                    // Restart Privacy Browser.
+                    restartPrivacyBrowser();
                     break;
 
                 case "easylist":
@@ -1449,11 +1425,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
                             break;
 
                         case ProxyHelper.TOR:
-                            if (Build.VERSION.SDK_INT == 19) {  // Proxying through SOCKS doesn't work on Android KitKat.
-                                proxyPreference.setSummary(context.getString(R.string.tor_enabled_kitkat));
-                            } else {
-                                proxyPreference.setSummary(context.getString(R.string.tor_enabled));
-                            }
+                            proxyPreference.setSummary(context.getString(R.string.tor_enabled));
                             break;
 
                         case ProxyHelper.I2P:
@@ -1466,10 +1438,10 @@ public class SettingsFragment extends PreferenceFragmentCompat {
                     }
 
                     // Update the status of the custom URL preference.
-                    proxyCustomUrlPreference.setEnabled(currentProxyString.equals("Custom"));
+                    proxyCustomUrlPreference.setEnabled(currentProxyString.equals(ProxyHelper.CUSTOM));
 
                     // Update the icons.
-                    if (currentProxyString.equals("None")) {  // Proxying is disabled.
+                    if (currentProxyString.equals(ProxyHelper.NONE)) {  // Proxying is disabled.
                         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {  // Dark theme.
                             // Set the main proxy icon to be disabled
                             proxyPreference.setIcon(R.drawable.proxy_disabled_night);
@@ -1510,7 +1482,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
 
                 case "proxy_custom_url":
                     // Set the summary text for the proxy custom URL.
-                    proxyCustomUrlPreference.setSummary(sharedPreferences.getString("proxy_custom_url", context.getString(R.string.proxy_custom_url_default_value)));
+                    proxyCustomUrlPreference.setSummary(sharedPreferences.getString(context.getString(R.string.proxy_custom_url_key), context.getString(R.string.proxy_custom_url_default_value)));
                     break;
 
                 case "full_screen_browsing_mode":
@@ -1778,7 +1750,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
 
                 case "scroll_app_bar":
                     // Update the icon.
-                    if (sharedPreferences.getBoolean("scroll_app_bar", true)) {
+                    if (sharedPreferences.getBoolean(context.getString(R.string.scroll_app_bar_key), true)) {
                         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
                             scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_day);
                         } else {
@@ -1793,6 +1765,26 @@ public class SettingsFragment extends PreferenceFragmentCompat {
                     }
                     break;
 
+                case "bottom_app_bar":
+                    // Update the icon.
+                    if (sharedPreferences.getBoolean(context.getString(R.string.bottom_app_bar_key), false)) {
+                        if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
+                            bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled_day);
+                        } else {
+                            bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled_night);
+                        }
+                    } else {
+                        if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
+                            bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled_day);
+                        } else {
+                            bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled_night);
+                        }
+                    }
+
+                    // Restart Privacy Browser.
+                    restartPrivacyBrowser();
+                    break;
+
                 case "display_additional_app_bar_icons":
                     // Update the icon.
                     if (sharedPreferences.getBoolean(context.getString(R.string.display_additional_app_bar_icons_key), false)) {
@@ -1932,4 +1924,30 @@ public class SettingsFragment extends PreferenceFragmentCompat {
             }
         };
     }
-}
\ No newline at end of file
+
+    private void restartPrivacyBrowser() {
+        // Create an intent to restart Privacy Browser.
+        Intent restartIntent = requireActivity().getParentActivityIntent();
+
+        // Assert that the intent is not null to remove the lint error below.
+        assert restartIntent != null;
+
+        // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack.  It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
+        restartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
+
+        // Create a handler to restart the activity.
+        Handler restartHandler = new Handler(Looper.getMainLooper());
+
+        // Create a runnable to restart the activity.
+        Runnable restartRunnable = () -> {
+            // Restart the activity.
+            startActivity(restartIntent);
+
+            // Kill this instance of Privacy Browser.  Otherwise, the app exhibits sporadic behavior after the restart.
+            System.exit(0);
+        };
+
+        // Restart the activity after 400 milliseconds, so that the app has enough time to save the change to the preference.
+        restartHandler.postDelayed(restartRunnable, 400);
+    }
+}