+ // 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.
+ // 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) {
+ case "javascript":
+ // Update the icons and the DOM storage preference status.
+ if (sharedPreferences.getBoolean("javascript", false)) { // The JavaScript preference is enabled.
+ // Update the icon for the JavaScript preference.
+ javaScriptPreference.setIcon(R.drawable.javascript_enabled);
+
+ // Update the status of the DOM storage preference.
+ domStoragePreference.setEnabled(true);
+
+ // Update the icon for the DOM storage preference.
+ if (sharedPreferences.getBoolean("dom_storage", false)) {
+ domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
+ } else {
+ domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
+ }
+ } else { // The JavaScript preference is disabled.
+ // Update the icon for the JavaScript preference.
+ javaScriptPreference.setIcon(R.drawable.privacy_mode);
+
+ // Update the status of the DOM storage preference.
+ domStoragePreference.setEnabled(false);
+
+ // Set the icon for DOM storage preference to be ghosted.
+ domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
+ }
+ break;
+
+ case "cookies":
+ // Update the icon.
+ if (sharedPreferences.getBoolean(context.getString(R.string.cookies_key), false)) {
+ cookiesPreference.setIcon(R.drawable.cookies_enabled);
+ } else {
+ cookiesPreference.setIcon(R.drawable.cookies_disabled);
+ }
+ break;
+
+ case "dom_storage":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("dom_storage", false)) {
+ domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
+ } else {
+ domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
+ }
+ break;
+
+ // Save form data can be removed once the minimum API >= 26.
+ case "save_form_data":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("save_form_data", false)) {
+ formDataPreference.setIcon(R.drawable.form_data_enabled);
+ } else {
+ formDataPreference.setIcon(R.drawable.form_data_disabled);
+ }
+ break;
+
+ case "user_agent":
+ // Get the new user agent name.
+ String newUserAgentName = sharedPreferences.getString(context.getString(R.string.user_agent_key), context.getString(R.string.user_agent_default_value));
+
+ // Get the array position for the new user agent name.
+ int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
+
+ // Get the translated new user agent name.
+ String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
+
+ // Populate the user agent summary.
+ switch (newUserAgentArrayPosition) {
+ case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
+ // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
+ userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + defaultUserAgent);
+
+ // Disable the custom user agent preference.
+ customUserAgentPreference.setEnabled(false);
+
+ // Set the custom user agent preference icon.
+ customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
+ break;
+
+ case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
+ // Set the summary text.
+ userAgentPreference.setSummary(R.string.custom_user_agent);
+
+ // Enable the custom user agent preference.
+ customUserAgentPreference.setEnabled(true);
+
+ // Set the custom user agent preference icon.
+ customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
+ break;
+
+ default:
+ // Get the user agent summary from the user agent data array.
+ userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
+
+ // Disable the custom user agent preference.
+ customUserAgentPreference.setEnabled(false);
+
+ // Set the custom user agent preference icon.
+ customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
+ }
+ break;
+
+ case "custom_user_agent":
+ // Set the new custom user agent as the summary text for the preference.
+ customUserAgentPreference.setSummary(sharedPreferences.getString(context.getString(R.string.custom_user_agent_key), context.getString(R.string.custom_user_agent_default_value)));
+ break;
+
+ case "x_requested_with_header":
+ // Update the icon.
+ if (sharedPreferences.getBoolean(context.getString(R.string.x_requested_with_header_key), true))
+ xRequestedWithHeaderPreference.setIcon(R.drawable.x_requested_with_header_enabled);
+ else
+ xRequestedWithHeaderPreference.setIcon(R.drawable.x_requested_with_header_disabled);
+
+ // Restart Privacy Browser.
+ restartPrivacyBrowser();
+ break;
+
+ case "incognito_mode":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("incognito_mode", false))
+ incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
+ else
+ incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
+ break;
+
+ case "allow_screenshots":
+ // Update the icon.
+ if (sharedPreferences.getBoolean(context.getString(R.string.allow_screenshots_key), false))
+ allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled);
+ else
+ allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled);
+
+ // Restart Privacy Browser.
+ restartPrivacyBrowser();
+ break;
+
+ case "easylist":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("easylist", true))
+ easyListPreference.setIcon(R.drawable.block_ads_enabled);
+ else
+ easyListPreference.setIcon(R.drawable.block_ads_disabled);
+ break;
+
+ case "easyprivacy":
+ // Update the icon.
+ if (sharedPreferences.getBoolean("easyprivacy", true))
+ easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled);
+ else
+ easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled);
+ break;
+
+ case "fanboys_annoyance_list":
+ boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
+ boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
+
+ // Update the Fanboy icons.
+ if (currentFanboyAnnoyanceList) { // Fanboy's annoyance list is enabled.
+ // Update the Fanboy's annoyance list icon.
+ fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled);
+
+ // Update the Fanboy's social blocking list icon.
+ fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted);
+ } else { // Fanboy's annoyance list is disabled.
+ // Update the Fanboy's annoyance list icon.
+ fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled);
+
+ // Update the Fanboy's social blocking list icon.
+ if (currentFanboySocialBlockingList) {
+ fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled);