X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Ffragments%2FSettingsFragment.java;h=a43e30bd82279d0b9689dbe20e0cd4015e8d3961;hp=b808c9da0254293b6621989fdadfed512c463c29;hb=484f0b0f0143c53a4722cee3a31e714df0c8c49a;hpb=1650cd6eff9ef807a84263328cb73e755250e3af diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java index b808c9da..a43e30bd 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java @@ -118,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. @@ -143,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"); @@ -157,7 +157,7 @@ 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"); @@ -333,10 +333,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. @@ -689,7 +689,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); @@ -877,7 +877,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 { @@ -979,12 +979,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. @@ -996,7 +990,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); } @@ -1006,18 +1000,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) { @@ -1463,10 +1458,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); @@ -1507,7 +1502,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": @@ -1775,7 +1770,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 { @@ -1972,7 +1967,7 @@ public class SettingsFragment extends PreferenceFragmentCompat { System.exit(0); }; - // Restart the activity after 300 milliseconds, so that the app has enough time to save the change to the preference. - restartHandler.postDelayed(restartRunnable, 300); + // Restart the activity after 400 milliseconds, so that the app has enough time to save the change to the preference. + restartHandler.postDelayed(restartRunnable, 400); } -} \ No newline at end of file +}