]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java
Add option to remove Google Analytics from URLs. https://redmine.stoutner.com/issues/414
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / SettingsFragment.java
1 /*
2  * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.fragments;
21
22 import android.annotation.SuppressLint;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.preference.Preference;
30 import android.preference.PreferenceCategory;
31 import android.preference.PreferenceFragment;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.webkit.WebView;
35 import android.widget.ArrayAdapter;
36
37 import com.stoutner.privacybrowser.R;
38 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39
40 public class SettingsFragment extends PreferenceFragment {
41     private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
42     private SharedPreferences savedPreferences;
43
44     @Override
45     public void onCreate(Bundle savedInstanceState) {
46         // Run the default commands.
47         super.onCreate(savedInstanceState);
48
49         // Load the preferences from the XML file.
50         addPreferencesFromResource(R.xml.preferences);
51
52         // Get a handle for the context.
53         Context context = getActivity().getApplicationContext();
54
55         // Initialize savedPreferences.
56         savedPreferences = getPreferenceScreen().getSharedPreferences();
57
58         // Get handles for the preferences.
59         Preference javaScriptPreference = findPreference("javascript");
60         Preference firstPartyCookiesPreference = findPreference("first_party_cookies");
61         Preference thirdPartyCookiesPreference = findPreference("third_party_cookies");
62         Preference domStoragePreference = findPreference("dom_storage");
63         Preference formDataPreference = findPreference("save_form_data");  // The form data preference can be removed once the minimum API >= 26.
64         Preference userAgentPreference = findPreference("user_agent");
65         Preference customUserAgentPreference = findPreference("custom_user_agent");
66         Preference incognitoModePreference = findPreference("incognito_mode");
67         Preference doNotTrackPreference = findPreference("do_not_track");
68         Preference allowScreenshotsPreference = findPreference("allow_screenshots");
69         Preference easyListPreference = findPreference("easylist");
70         Preference easyPrivacyPreference = findPreference("easyprivacy");
71         Preference fanboyAnnoyanceListPreference = findPreference("fanboys_annoyance_list");
72         Preference fanboySocialBlockingListPreference = findPreference("fanboys_social_blocking_list");
73         Preference ultraPrivacyPreference = findPreference("ultraprivacy");
74         Preference blockAllThirdPartyRequestsPreference = findPreference("block_all_third_party_requests");
75         Preference googleAnalyticsPreference = findPreference("google_analytics");
76         Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
77         Preference torHomepagePreference = findPreference("tor_homepage");
78         Preference torSearchPreference = findPreference("tor_search");
79         Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
80         Preference searchPreference = findPreference("search");
81         Preference searchCustomURLPreference = findPreference("search_custom_url");
82         Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
83         Preference hideAppBarPreference = findPreference("hide_app_bar");
84         Preference clearEverythingPreference = findPreference("clear_everything");
85         Preference clearCookiesPreference = findPreference("clear_cookies");
86         Preference clearDomStoragePreference = findPreference("clear_dom_storage");
87         Preference clearFormDataPreference = findPreference("clear_form_data");  // The clear form data preference can be removed once the minimum API >= 26.
88         Preference clearCachePreference = findPreference("clear_cache");
89         Preference homepagePreference = findPreference("homepage");
90         Preference fontSizePreference = findPreference("font_size");
91         Preference openIntentsInNewTabPreference = findPreference("open_intents_in_new_tab");
92         Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
93         Preference scrollAppBarPreference = findPreference("scroll_app_bar");
94         Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
95         Preference downloadWithExternalAppPreference = findPreference("download_with_external_app");
96         Preference darkThemePreference = findPreference("dark_theme");
97         Preference nightModePreference = findPreference("night_mode");
98         Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
99
100         // Set dependencies.
101         torHomepagePreference.setDependency("proxy_through_orbot");
102         torSearchPreference.setDependency("proxy_through_orbot");
103         hideAppBarPreference.setDependency("full_screen_browsing_mode");
104
105         // Get strings from the preferences.
106         String torSearchString = savedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
107         String searchString = savedPreferences.getString("search", getString(R.string.search_default_value));
108
109         // Get booleans that are used in multiple places from the preferences.
110         boolean javaScriptEnabled = savedPreferences.getBoolean("javascript", false);
111         boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies", false);
112         boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies", false);
113         boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboys_annoyance_list", true);
114         boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboys_social_blocking_list", true);
115         boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
116         boolean fullScreenBrowsingMode = savedPreferences.getBoolean("full_screen_browsing_mode", false);
117         boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
118         boolean darkTheme = savedPreferences.getBoolean("dark_theme", false);
119         boolean nightMode = savedPreferences.getBoolean("night_mode", false);
120
121         // Only enable the third-party cookies preference if first-party cookies are enabled and API >= 21.
122         thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabled && (Build.VERSION.SDK_INT >= 21));
123
124         // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
125         domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
126
127         // Remove the form data preferences if the API is >= 26 as they no longer do anything.
128         if (Build.VERSION.SDK_INT >= 26) {
129             // Get the categories.
130             PreferenceCategory privacyCategory = (PreferenceCategory) findPreference("privacy");
131             PreferenceCategory clearAndExitCategory = (PreferenceCategory) findPreference("clear_and_exit");
132
133             // Remove the form data preferences.
134             privacyCategory.removePreference(formDataPreference);
135             clearAndExitCategory.removePreference(clearFormDataPreference);
136         }
137
138         // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
139         fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
140
141         // Inflate a WebView to get the default user agent.
142         LayoutInflater inflater = getActivity().getLayoutInflater();
143         // `@SuppressLint("InflateParams")` removes the warning about using `null` as the `ViewGroup`, which in this case makes sense because the `bare_webview` will not be displayed.
144         @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
145         WebView bareWebView = bareWebViewLayout.findViewById(R.id.bare_webview);
146
147         // Get the user agent arrays.
148         ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(context, R.array.user_agent_names, R.layout.spinner_item);
149         String[] translatedUserAgentNamesArray = getResources().getStringArray(R.array.translated_user_agent_names);
150         String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
151
152         // Get the current user agent name from the preference.
153         String userAgentName = savedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
154
155         // Get the array position of the user agent name.
156         int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
157
158         // Populate the user agent summary.
159         switch (userAgentArrayPosition) {
160             case MainWebViewActivity.UNRECOGNIZED_USER_AGENT:  // The user agent name is not on the canonical list.
161                 // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names.  Use the current user agent entry name as the summary.
162                 userAgentPreference.setSummary(userAgentName);
163                 break;
164
165             case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
166                 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
167                 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + bareWebView.getSettings().getUserAgentString());
168                 break;
169
170             case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
171                 // Set the summary text.
172                 userAgentPreference.setSummary(R.string.custom_user_agent);
173                 break;
174
175             default:
176                 // Get the user agent summary from the user agent data array.
177                 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + userAgentDataArray[userAgentArrayPosition]);
178         }
179
180         // Set the summary text for the custom user agent preference and enable it if user agent preference is set to custom.
181         customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
182         customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals(getString(R.string.custom_user_agent)));
183
184
185         // Set the Tor homepage URL as the summary text for the `tor_homepage` preference when the preference screen is loaded.  The default is Searx: `http://ulrn6sryqaifefld.onion/`.
186         torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
187
188
189         // Set the Tor search URL as the summary text for the Tor preference when the preference screen is loaded.
190         if (torSearchString.equals("Custom URL")) {
191             // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
192             torSearchPreference.setSummary(R.string.custom_url);
193         } else {
194             // Set the array value as the summary text.
195             torSearchPreference.setSummary(torSearchString);
196         }
197
198         // Set the summary text for `tor_search_custom_url`.  The default is `""`.
199         torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", getString(R.string.tor_search_custom_url_default_value)));
200
201         // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
202         torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
203
204
205         // Set the search URL as the summary text for the search preference when the preference screen is loaded.
206         if (searchString.equals("Custom URL")) {
207             // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
208             searchPreference.setSummary(R.string.custom_url);
209         } else {
210             // Set the array value as the summary text.
211             searchPreference.setSummary(searchString);
212         }
213
214         // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
215         searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value)));
216         searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
217
218         // Set the status of the Clear and Exit preferences.
219         clearCookiesPreference.setEnabled(!clearEverything);
220         clearDomStoragePreference.setEnabled(!clearEverything);
221         clearFormDataPreference.setEnabled(!clearEverything);  // The form data line can be removed once the minimum API is >= 26.
222         clearCachePreference.setEnabled(!clearEverything);
223
224         // Set the homepage URL as the summary text for the homepage preference.
225         homepagePreference.setSummary(savedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
226
227         // Set the font size as the summary text for the preference.
228         fontSizePreference.setSummary(savedPreferences.getString("font_size", getString(R.string.font_size_default_value)) + "%%");
229
230         // Disable the JavaScript preference if Night Mode is enabled.  JavaScript will be enabled for all web pages.
231         javaScriptPreference.setEnabled(!nightMode);
232
233         // Set the JavaScript icon.
234         if (javaScriptEnabled || nightMode) {
235             javaScriptPreference.setIcon(R.drawable.javascript_enabled);
236         } else {
237             javaScriptPreference.setIcon(R.drawable.privacy_mode);
238         }
239
240         // Set the first-party cookies icon.
241         if (firstPartyCookiesEnabled) {
242             firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
243         } else {
244             if (darkTheme) {
245                 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
246             } else {
247                 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
248             }
249         }
250
251         // Set the third party cookies icon.
252         if (firstPartyCookiesEnabled && Build.VERSION.SDK_INT >= 21) {
253             if (thirdPartyCookiesEnabled) {
254                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
255             } else {
256                 if (darkTheme) {
257                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
258                 } else {
259                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
260                 }
261             }
262         } else {
263             if (darkTheme) {
264                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
265             } else {
266                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
267             }
268         }
269
270         // Set the DOM storage icon.
271         if (javaScriptEnabled || nightMode) {  // The preference is enabled.
272             if (savedPreferences.getBoolean("dom_storage", false)) {  // DOM storage is enabled.
273                 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
274             } else {  // DOM storage is disabled.
275                 if (darkTheme) {
276                     domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
277                 } else {
278                     domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
279                 }
280             }
281         } else {  // The preference is disabled.  The icon should be ghosted.
282             if (darkTheme) {
283                 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
284             } else {
285                 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
286             }
287         }
288
289         // Set the save form data icon if API < 26.  Save form data has no effect on API >= 26.
290         if (Build.VERSION.SDK_INT < 26) {
291             if (savedPreferences.getBoolean("save_form_data", false)) {
292                 formDataPreference.setIcon(R.drawable.form_data_enabled);
293             } else {
294                 if (darkTheme) {
295                     formDataPreference.setIcon(R.drawable.form_data_disabled_dark);
296                 } else {
297                     formDataPreference.setIcon(R.drawable.form_data_disabled_light);
298                 }
299             }
300         }
301
302         // Set the custom user agent icon.
303         if (customUserAgentPreference.isEnabled()) {
304             if (darkTheme) {
305                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
306             } else {
307                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
308             }
309         } else {
310             if (darkTheme) {
311                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
312             } else {
313                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
314             }
315         }
316
317         // Set the incognito mode icon.
318         if (savedPreferences.getBoolean("incognito_mode", false)) {
319             if (darkTheme) {
320                 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
321             } else {
322                 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
323             }
324         } else {
325             if (darkTheme) {
326                 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
327             } else {
328                 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
329             }
330         }
331
332         // Set the Do Not Track icon.
333         if (savedPreferences.getBoolean("do_not_track", false)) {
334             if (darkTheme) {
335                 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
336             } else {
337                 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
338             }
339         } else {
340             if (darkTheme) {
341                 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
342             } else {
343                 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
344             }
345         }
346
347         // Set the allow screenshots icon.
348         if (savedPreferences.getBoolean("allow_screenshots", false)) {
349             if (darkTheme) {
350                 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
351             } else {
352                 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
353             }
354         } else {
355             if (darkTheme) {
356                 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
357             } else {
358                 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
359             }
360         }
361
362         // Set the EasyList icon.
363         if (savedPreferences.getBoolean("easylist", true)) {
364             if (darkTheme) {
365                 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
366             } else {
367                 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
368             }
369         } else {
370             if (darkTheme) {
371                 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
372             } else {
373                 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
374             }
375         }
376
377         // Set the EasyPrivacy icon.
378         if (savedPreferences.getBoolean("easyprivacy", true)) {
379             if (darkTheme) {
380                 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
381             } else {
382                 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
383             }
384         } else {
385             if (darkTheme) {
386                 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
387             } else {
388                 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
389             }
390         }
391
392         // Set the Fanboy lists icons.
393         if (fanboyAnnoyanceListEnabled) {
394             if (darkTheme) {
395                 // Set the Fanboy annoyance list icon.
396                 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
397
398                 // Set the Fanboy social blocking list icon.
399                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
400             } else {
401                 // Set the Fanboy annoyance list icon.
402                 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
403
404                 // Set the Fanboy social blocking list icon.
405                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
406             }
407         } else {
408             if (darkTheme) {
409                 // Set the Fanboy annoyance list icon.
410                 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
411
412                 // Set the Fanboy social blocking list icon.
413                 if (fanboySocialBlockingEnabled) {
414                     fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
415                 } else {
416                     fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
417                 }
418             } else {
419                 // Set the Fanboy annoyance list icon.
420                 fanboyAnnoyanceListPreference.setIcon(R.drawable.block_ads_disabled_light);
421
422                 // Set the Fanboy social blocking list icon.
423                 if (fanboySocialBlockingEnabled) {
424                     fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
425                 } else {
426                     fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
427                 }
428             }
429         }
430
431         // Set the UltraPrivacy icon.
432         if (savedPreferences.getBoolean("ultraprivacy", true)) {
433             if (darkTheme) {
434                 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
435             } else {
436                 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
437             }
438         } else {
439             if (darkTheme) {
440                 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
441             } else {
442                 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
443             }
444         }
445
446         // Set the block all third-party requests icon.
447         if (savedPreferences.getBoolean("block_all_third_party_requests", false)) {
448             if (darkTheme) {
449                 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
450             } else {
451                 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
452             }
453         } else {
454             if (darkTheme) {
455                 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
456             } else {
457                 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
458             }
459         }
460
461         // Set the Google Analytics icons according to the theme.
462         if (savedPreferences.getBoolean("google_analytics", true)) {
463             if (darkTheme) {
464                 googleAnalyticsPreference.setIcon(R.drawable.modify_url_enabled_dark);
465             } else {
466                 googleAnalyticsPreference.setIcon(R.drawable.modify_url_enabled_light);
467             }
468         } else {
469             if (darkTheme) {
470                 googleAnalyticsPreference.setIcon(R.drawable.modify_url_disabled_dark);
471             } else {
472                 googleAnalyticsPreference.setIcon(R.drawable.modify_url_disabled_light);
473             }
474         }
475
476         // Set the Tor icons according to the theme.
477         if (proxyThroughOrbot) {  // Proxying is enabled.
478             if (darkTheme) {
479                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
480                 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
481                 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
482
483                 // Set the custom search icon.
484                 if (torSearchCustomURLPreference.isEnabled()) {
485                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
486                 } else {
487                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
488                 }
489             } else {
490                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
491                 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
492                 torSearchPreference.setIcon(R.drawable.search_enabled_light);
493
494                 // Set the custom search icon.
495                 if (torSearchCustomURLPreference.isEnabled()) {
496                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
497                 } else {
498                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
499                 }
500             }
501         } else {  // Proxying is disabled.
502             if (darkTheme) {
503                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
504                 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
505                 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
506                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
507             } else {
508                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
509                 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
510                 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
511                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
512             }
513         }
514
515         // Set the search custom URL icon.
516         if (searchCustomURLPreference.isEnabled()) {
517             if (darkTheme) {
518                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
519             } else {
520                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
521             }
522         } else {
523             if (darkTheme) {
524                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
525             } else {
526                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
527             }
528         }
529
530         // Set the full screen browsing mode icons.
531         if (fullScreenBrowsingMode) {  // Full screen browsing mode is enabled.
532             // Set the `fullScreenBrowsingModePreference` icon according to the theme.
533             if (darkTheme) {
534                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
535             } else {
536                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
537             }
538
539             // Set the hide app bar icon.
540             if (savedPreferences.getBoolean("hide_app_bar", true)) {  // Hide app bar is enabled.
541                 // Set the icon according to the theme.
542                 if (darkTheme) {
543                     hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
544                 } else {
545                     hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
546                 }
547             } else {  // Hide app bar is disabled.
548                 // Set the icon according to the theme.
549                 if (darkTheme) {
550                     hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
551                 } else {
552                     hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
553                 }
554             }
555         } else {  // Full screen browsing mode is disabled.
556             // Set the icons according to the theme.
557             if (darkTheme) {
558                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
559                 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_dark);
560             } else {
561                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
562                 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_light);
563             }
564         }
565
566         // Set the clear everything preference icon.
567         if (clearEverything) {
568             if (darkTheme) {
569                 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
570             } else {
571                 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
572             }
573         } else {
574             clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
575         }
576
577         // Set the clear cookies preference icon.
578         if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
579             if (darkTheme) {
580                 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
581             } else {
582                 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
583             }
584         } else {
585             clearCookiesPreference.setIcon(R.drawable.cookies_warning);
586         }
587
588         // Set the clear DOM storage preference icon.
589         if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
590             if (darkTheme) {
591                 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
592             } else {
593                 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
594             }
595         } else {
596             clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
597         }
598
599         // Set the clear form data preference icon if the API < 26.  It has no effect on newer versions of Android.
600         if (Build.VERSION.SDK_INT < 26) {
601             if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
602                 if (darkTheme) {
603                     clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
604                 } else {
605                     clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
606                 }
607             } else {
608                 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
609             }
610         }
611
612         // Set the clear cache preference icon.
613         if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
614             if (darkTheme) {
615                 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
616             } else {
617                 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
618             }
619         } else {
620             clearCachePreference.setIcon(R.drawable.cache_warning);
621         }
622
623         // Set the open intents in new tab preference icon.
624         if (savedPreferences.getBoolean("open_intents_in_new_tab", true)) {
625             if (darkTheme) {
626                 openIntentsInNewTabPreference.setIcon(R.drawable.tab_enabled_dark);
627             } else {
628                 openIntentsInNewTabPreference.setIcon(R.drawable.tab_enabled_light);
629             }
630         } else {
631             if (darkTheme) {
632                 openIntentsInNewTabPreference.setIcon(R.drawable.tab_disabled_dark);
633             } else {
634                 openIntentsInNewTabPreference.setIcon(R.drawable.tab_disabled_light);
635             }
636         }
637
638         // Set the swipe to refresh preference icon.
639         if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
640             if (darkTheme) {
641                 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
642             } else {
643                 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
644             }
645         } else {
646             if (darkTheme) {
647                 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
648             } else {
649                 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
650             }
651         }
652
653         // Set the scroll app bar preference icon.
654         if (savedPreferences.getBoolean("scroll_app_bar", true)) {
655             if (darkTheme) {
656                 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
657             } else {
658                 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
659             }
660         } else {
661             if (darkTheme) {
662                 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
663             } else {
664                 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
665             }
666         }
667
668         // Set the display additional app bar icons preference icon.
669         if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
670             if (darkTheme) {
671                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
672             } else {
673                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
674             }
675         } else {
676             if (darkTheme) {
677                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
678             } else {
679                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
680             }
681         }
682
683         // Set the download with external app preference icon.
684         if (savedPreferences.getBoolean("download_with_external_app", false)) {
685             if (darkTheme) {
686                 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
687             } else {
688                 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
689             }
690         } else {
691             if (darkTheme) {
692                 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
693             } else {
694                 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
695             }
696         }
697
698         // Set the dark theme preference icon.
699         if (savedPreferences.getBoolean("dark_theme", false)) {
700             darkThemePreference.setIcon(R.drawable.theme_dark);
701         } else {
702             darkThemePreference.setIcon(R.drawable.theme_light);
703         }
704
705         // Set the night mode preference icon.
706         if (nightMode) {
707             if (darkTheme) {
708                 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
709             } else {
710                 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
711             }
712         } else {
713             if (darkTheme) {
714                 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
715             } else {
716                 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
717             }
718         }
719
720         // Set the display webpage images preference icon.
721         if (savedPreferences.getBoolean("display_webpage_images", true)) {
722             if (darkTheme) {
723                 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
724             } else {
725                 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
726             }
727         } else {
728             if (darkTheme) {
729                 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
730             } else {
731                 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
732             }
733         }
734
735
736         // Listen for preference changes.
737         preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
738             switch (key) {
739                 case "javascript":
740                     // Update the icons and the DOM storage preference status.
741                     if (sharedPreferences.getBoolean("javascript", false)) {  // The JavaScript preference is enabled.
742                         // Update the icon for the JavaScript preference.
743                         javaScriptPreference.setIcon(R.drawable.javascript_enabled);
744
745                         // Update the status of the DOM storage preference.
746                         domStoragePreference.setEnabled(true);
747
748                         // Update the icon for the DOM storage preference.
749                         if (sharedPreferences.getBoolean("dom_storage", false)) {
750                             domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
751                         } else {
752                             if (darkTheme) {
753                                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
754                             } else {
755                                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
756                             }
757                         }
758                     } else {  // The JavaScript preference is disabled.
759                         // Update the icon for the JavaScript preference.
760                         javaScriptPreference.setIcon(R.drawable.privacy_mode);
761
762                         // Update the status of the DOM storage preference.
763                         domStoragePreference.setEnabled(false);
764
765                         // Set the icon for DOM storage preference to be ghosted.
766                         if (darkTheme) {
767                             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
768                         } else {
769                             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
770                         }
771                     }
772                     break;
773
774                 case "first_party_cookies":
775                     // Update the icons for `first_party_cookies` and `third_party_cookies`.
776                     if (sharedPreferences.getBoolean("first_party_cookies", false)) {
777                         // Set the icon for `first_party_cookies`.
778                         firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
779
780                         // Update the icon for `third_party_cookies`.
781                         if (Build.VERSION.SDK_INT >= 21) {
782                             if (sharedPreferences.getBoolean("third_party_cookies", false)) {
783                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
784                             } else {
785                                 if (darkTheme) {
786                                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
787                                 } else {
788                                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
789                                 }
790                             }
791                         } else {
792                             if (darkTheme) {
793                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
794                             } else {
795                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
796                             }
797                         }
798                     } else {  // `first_party_cookies` is `false`.
799                         // Update the icon for `first_party_cookies`.
800                         if (darkTheme) {
801                             firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
802                         } else {
803                             firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
804                         }
805
806                         // Set the icon for `third_party_cookies` to be ghosted.
807                         if (darkTheme) {
808                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
809                         } else {
810                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
811                         }
812                     }
813
814                     // Enable `third_party_cookies` if `first_party_cookies` is `true` and API >= 21.
815                     thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies", false) && (Build.VERSION.SDK_INT >= 21));
816                     break;
817
818                 case "third_party_cookies":
819                     // Update the icon.
820                     if (sharedPreferences.getBoolean("third_party_cookies", false)) {
821                         thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
822                     } else {
823                         if (darkTheme) {
824                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
825                         } else {
826                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
827                         }
828                     }
829                     break;
830
831                 case "dom_storage":
832                     // Update the icon.
833                     if (sharedPreferences.getBoolean("dom_storage", false)) {
834                         domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
835                     } else {
836                         if (darkTheme) {
837                             domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
838                         } else {
839                             domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
840                         }
841                     }
842                     break;
843
844                 // Save form data can be removed once the minimum API >= 26.
845                 case "save_form_data":
846                     // Update the icon.
847                     if (sharedPreferences.getBoolean("save_form_data", false)) {
848                         formDataPreference.setIcon(R.drawable.form_data_enabled);
849                     } else {
850                         if (darkTheme) {
851                             formDataPreference.setIcon(R.drawable.form_data_disabled_dark);
852                         } else {
853                             formDataPreference.setIcon(R.drawable.form_data_disabled_light);
854                         }
855                     }
856                     break;
857
858                 case "user_agent":
859                     // Get the new user agent name.
860                     String newUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
861
862                     // Get the array position for the new user agent name.
863                     int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
864
865                     // Get the translated new user agent name.
866                     String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
867
868                     // Populate the user agent summary.
869                     switch (newUserAgentArrayPosition) {
870                         case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
871                             // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
872                             userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
873
874                             // Disable the custom user agent preference.
875                             customUserAgentPreference.setEnabled(false);
876
877                             // Set the custom user agent preference icon according to the theme.
878                             if (darkTheme) {
879                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
880                             } else {
881                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
882                             }
883                             break;
884
885                         case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
886                             // Set the summary text.
887                             userAgentPreference.setSummary(R.string.custom_user_agent);
888
889                             // Enable the custom user agent preference.
890                             customUserAgentPreference.setEnabled(true);
891
892                             // Set the custom user agent preference icon according to the theme.
893                             if (darkTheme) {
894                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
895                             } else {
896                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
897                             }
898                             break;
899
900                         default:
901                             // Get the user agent summary from the user agent data array.
902                             userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
903
904                             // Disable the custom user agent preference.
905                             customUserAgentPreference.setEnabled(false);
906
907                             // Set the custom user agent preference icon according to the theme.
908                             if (darkTheme) {
909                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
910                             } else {
911                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
912                             }
913                     }
914                     break;
915
916                 case "custom_user_agent":
917                     // Set the new custom user agent as the summary text for the preference.
918                     customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
919                     break;
920
921                 case "incognito_mode":
922                     // Update the icon.
923                     if (sharedPreferences.getBoolean("incognito_mode", false)) {
924                         if (darkTheme) {
925                             incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
926                         } else {
927                             incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
928                         }
929                     } else {
930                         if (darkTheme) {
931                             incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
932                         } else {
933                             incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
934                         }
935                     }
936                     break;
937
938                 case "do_not_track":
939                     // Update the icon.
940                     if (sharedPreferences.getBoolean("do_not_track", false)) {
941                         if (darkTheme) {
942                             doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
943                         } else {
944                             doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
945                         }
946                     } else {
947                         if (darkTheme) {
948                             doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
949                         } else {
950                             doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
951                         }
952                     }
953
954                     break;
955
956                 case "allow_screenshots":
957                     // Update the icon.
958                     if (sharedPreferences.getBoolean("allow_screenshots", false)) {
959                         if (darkTheme) {
960                             allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
961                         } else {
962                             allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
963                         }
964                     } else {
965                         if (darkTheme) {
966                             allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
967                         } else {
968                             allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
969                         }
970                     }
971
972                     // Create an intent to restart Privacy Browser.
973                     Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
974
975                     // Assert that the intent is not null to remove the lint error below.
976                     assert allowScreenshotsRestartIntent != null;
977
978                     // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack.  It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
979                     allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
980
981                     // Create a handler to restart the activity.
982                     Handler allowScreenshotsRestartHandler = new Handler();
983
984                     // Create a runnable to restart the activity.
985                     Runnable allowScreenshotsRestartRunnable = () -> {
986                         // Restart the activity.
987                         startActivity(allowScreenshotsRestartIntent);
988
989                         // Kill this instance of Privacy Browser.  Otherwise, the app exhibits sporadic behavior after the restart.
990                         System.exit(0);
991                     };
992
993                     // Restart the activity after 100 milliseconds, so that the app has enough time to save the change to the preference.
994                     allowScreenshotsRestartHandler.postDelayed(allowScreenshotsRestartRunnable, 100);
995                     break;
996
997                 case "easylist":
998                     // Update the icon.
999                     if (sharedPreferences.getBoolean("easylist", true)) {
1000                         if (darkTheme) {
1001                             easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
1002                         } else {
1003                             easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
1004                         }
1005                     } else {
1006                         if (darkTheme) {
1007                             easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
1008                         } else {
1009                             easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
1010                         }
1011                     }
1012                     break;
1013
1014                 case "easyprivacy":
1015                     // Update the icon.
1016                     if (sharedPreferences.getBoolean("easyprivacy", true)) {
1017                         if (darkTheme) {
1018                             easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
1019                         } else {
1020                             easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
1021                         }
1022                     } else {
1023                         if (darkTheme) {
1024                             easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
1025                         } else {
1026                             easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
1027                         }
1028                     }
1029                     break;
1030
1031                 case "fanboys_annoyance_list":
1032                     boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
1033                     boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
1034
1035                     // Update the Fanboy icons.
1036                     if (currentFanboyAnnoyanceList) {  // Fanboy's annoyance list is enabled.
1037                         if (darkTheme) {
1038                             // Update the Fanboy's annoyance list icon.
1039                             fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
1040
1041                             // Update the Fanboy's social blocking list icon.
1042                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
1043                         } else {
1044                             // Update the Fanboy's annoyance list icon.
1045                             fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
1046
1047                             // Update the Fanboy's social blocking list icon.
1048                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
1049                         }
1050                     } else {  // Fanboy's annoyance list is disabled.
1051                         if (darkTheme) {
1052                             // Update the Fanboy's annoyance list icon.
1053                             fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
1054
1055                             // Update the Fanboy's social blocking list icon.
1056                             if (currentFanboySocialBlockingList) {
1057                                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1058                             } else {
1059                                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1060                             }
1061                         } else {
1062                             // Update the Fanboy's annoyance list icon.
1063                             fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
1064
1065                             // Update the Fanboy's social blocking list icon.
1066                             if (currentFanboySocialBlockingList) {
1067                                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1068                             } else {
1069                                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1070                             }
1071                         }
1072                     }
1073
1074                     // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
1075                     fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
1076                     break;
1077
1078                 case "fanboys_social_blocking_list":
1079                     // Update the icon.
1080                     if (sharedPreferences.getBoolean("fanboys_social_blocking_list", true)) {
1081                         if (darkTheme) {
1082                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1083                         } else {
1084                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1085                         }
1086                     } else {
1087                         if (darkTheme) {
1088                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1089                         } else {
1090                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1091                         }
1092                     }
1093                     break;
1094
1095                 case "ultraprivacy":
1096                     // Update the icon.
1097                     if (sharedPreferences.getBoolean("ultraprivacy", true)) {
1098                         if (darkTheme) {
1099                             ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
1100                         } else {
1101                             ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
1102                         }
1103                     } else {
1104                         if (darkTheme) {
1105                             ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
1106                         } else {
1107                             ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
1108                         }
1109                     }
1110                     break;
1111
1112                 case "block_all_third_party_requests":
1113                     // Update the icon.
1114                     if (sharedPreferences.getBoolean("block_all_third_party_requests", false)) {
1115                         if (darkTheme) {
1116                             blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
1117                         } else {
1118                             blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
1119                         }
1120                     } else {
1121                         if (darkTheme) {
1122                             blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
1123                         } else {
1124                             blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
1125                         }
1126                     }
1127                     break;
1128
1129                 case "google_analytics":
1130                     // Update the icon.
1131                     if (sharedPreferences.getBoolean("google_analytics", true)) {
1132                         if (darkTheme) {
1133                             googleAnalyticsPreference.setIcon(R.drawable.modify_url_enabled_dark);
1134                         } else {
1135                             googleAnalyticsPreference.setIcon(R.drawable.modify_url_enabled_light);
1136                         }
1137                     } else {
1138                         if (darkTheme) {
1139                             googleAnalyticsPreference.setIcon(R.drawable.modify_url_disabled_dark);
1140                         } else {
1141                             googleAnalyticsPreference.setIcon(R.drawable.modify_url_disabled_light);
1142                         }
1143                     }
1144                     break;
1145
1146                 case "proxy_through_orbot":
1147                     // Get current settings.
1148                     boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
1149                     String currentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1150
1151                     // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
1152                     torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
1153
1154                     // Update the icons.
1155                     if (currentProxyThroughOrbot) {
1156                         // Set the Tor icons according to the theme.
1157                         if (darkTheme) {
1158                             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1159                             torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1160                             torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1161
1162                             // Set the `torSearchCustomURLPreference` icon.
1163                             if (torSearchCustomURLPreference.isEnabled()) {
1164                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1165                             } else {
1166                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1167                             }
1168                         } else {
1169                             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1170                             torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1171                             torSearchPreference.setIcon(R.drawable.search_enabled_light);
1172
1173                             // Set the `torSearchCustomURLPreference` icon.
1174                             if (torSearchCustomURLPreference.isEnabled()) {
1175                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1176                             } else {
1177                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1178                             }
1179                         }
1180                     } else {  // Proxy through Orbot is disabled.
1181                         if (darkTheme) {
1182                             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1183                             torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1184                             torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1185                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1186                         } else {
1187                             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1188                             torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1189                             torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1190                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1191                         }
1192                     }
1193                     break;
1194
1195                 case "tor_homepage":
1196                     // Set the new tor homepage URL as the summary text for the `tor_homepage` preference.  The default is Searx:  `http://ulrn6sryqaifefld.onion/`.
1197                     torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
1198                     break;
1199
1200                 case "tor_search":
1201                     // Get the present search string.
1202                     String presentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1203
1204                     // Update the preferences.
1205                     if (presentTorSearchString.equals("Custom URL")) {
1206                         // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1207                         torSearchPreference.setSummary(R.string.custom_url);
1208
1209                         // Enable `torSearchCustomURLPreference`.
1210                         torSearchCustomURLPreference.setEnabled(true);
1211
1212                         // Update the `torSearchCustomURLPreference` icon.
1213                         if (darkTheme) {
1214                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1215                         } else {
1216                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1217                         }
1218                     } else {
1219                         // Set the array value as the summary text.
1220                         torSearchPreference.setSummary(presentTorSearchString);
1221
1222                         // Disable `torSearchCustomURLPreference`.
1223                         torSearchCustomURLPreference.setEnabled(false);
1224
1225                         // Update the `torSearchCustomURLPreference` icon.
1226                         if (darkTheme) {
1227                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1228                         } else {
1229                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1230                         }
1231                     }
1232                     break;
1233
1234                 case "tor_search_custom_url":
1235                     // Set the summary text for `tor_search_custom_url`.
1236                     torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", getString(R.string.tor_search_custom_url_default_value)));
1237                     break;
1238
1239                 case "search":
1240                     // Store the new search string.
1241                     String newSearchString = sharedPreferences.getString("search", getString(R.string.search_default_value));
1242
1243                     // Update `searchPreference` and `searchCustomURLPreference`.
1244                     if (newSearchString.equals("Custom URL")) {  // `Custom URL` is selected.
1245                         // Set the summary text to `R.string.custom_url`, which is translated.
1246                         searchPreference.setSummary(R.string.custom_url);
1247
1248                         // Enable `searchCustomURLPreference`.
1249                         searchCustomURLPreference.setEnabled(true);
1250
1251                         // Set the `searchCustomURLPreference` according to the theme.
1252                         if (darkTheme) {
1253                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1254                         } else {
1255                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1256                         }
1257                     } else {  // `Custom URL` is not selected.
1258                         // Set the summary text to `newSearchString`.
1259                         searchPreference.setSummary(newSearchString);
1260
1261                         // Disable `searchCustomURLPreference`.
1262                         searchCustomURLPreference.setEnabled(false);
1263
1264                         // Set the `searchCustomURLPreference` according to the theme.
1265                         if (darkTheme) {
1266                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1267                         } else {
1268                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1269                         }
1270                     }
1271                     break;
1272
1273                 case "search_custom_url":
1274                     // Set the new custom search URL as the summary text for `search_custom_url`.  The default is `""`.
1275                     searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value)));
1276                     break;
1277
1278                 case "full_screen_browsing_mode":
1279                     if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {  // Full screen browsing is enabled.
1280                         // Set the full screen browsing mode preference icon according to the theme.
1281                         if (darkTheme) {
1282                             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1283                         } else {
1284                             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1285                         }
1286
1287                         // Set the hide app bar preference icon.
1288                         if (sharedPreferences.getBoolean("hide_app_bar", true)) {  //  Hide app bar is enabled.
1289                             // Set the icon according to the theme.
1290                             if (darkTheme) {
1291                                 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1292                             } else {
1293                                 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1294                             }
1295                         } else {  // Hide app bar is disabled.
1296                             // Set the icon according to the theme.
1297                             if (darkTheme) {
1298                                 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1299                             } else {
1300                                 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1301                             }
1302                         }
1303                     } else {  // Full screen browsing is disabled.
1304                         // Update the icons according to the theme.
1305                         if (darkTheme) {
1306                             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1307                             hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_dark);
1308                         } else {
1309                             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1310                             hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_light);
1311                         }
1312                     }
1313                     break;
1314
1315                 case "hide_app_bar":
1316                     // Update the icon.
1317                     if (sharedPreferences.getBoolean("hide_app_bar", true)) {  // Hide app bar is enabled.
1318                         // Set the icon according to the theme.
1319                         if (darkTheme) {
1320                             hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1321                         } else {
1322                             hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1323                         }
1324                     } else {  // Hide app bar is disabled.
1325                         // Set the icon according to the theme.
1326                         if (darkTheme) {
1327                             hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1328                         } else {
1329                             hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1330                         }
1331                     }
1332                     break;
1333
1334                 case "clear_everything":
1335                     // Store the new `clear_everything` status
1336                     boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1337
1338                     // Update the status of the `Clear and Exit` preferences.
1339                     clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1340                     clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1341                     clearFormDataPreference.setEnabled(!newClearEverythingBoolean);  // This line can be removed once the minimum API >= 26.
1342                     clearCachePreference.setEnabled(!newClearEverythingBoolean);
1343
1344                     // Update the `clearEverythingPreference` icon.
1345                     if (newClearEverythingBoolean) {
1346                         if (darkTheme) {
1347                             clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1348                         } else {
1349                             clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1350                         }
1351                     } else {
1352                         clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1353                     }
1354
1355                     // Update the `clearCookiesPreference` icon.
1356                     if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1357                         if (darkTheme) {
1358                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1359                         } else {
1360                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1361                         }
1362                     } else {
1363                         clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1364                     }
1365
1366                     // Update the `clearDomStoragePreference` icon.
1367                     if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1368                         if (darkTheme) {
1369                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1370                         } else {
1371                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1372                         }
1373                     } else {
1374                         clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1375                     }
1376
1377                     // Update the clear form data preference icon if the API < 26.
1378                     if (Build.VERSION.SDK_INT < 26) {
1379                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1380                             if (darkTheme) {
1381                                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1382                             } else {
1383                                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1384                             }
1385                         } else {
1386                             clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1387                         }
1388                     }
1389
1390                     // Update the `clearCachePreference` icon.
1391                     if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1392                         if (darkTheme) {
1393                             clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1394                         } else {
1395                             clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1396                         }
1397                     } else {
1398                         clearCachePreference.setIcon(R.drawable.cache_warning);
1399                     }
1400                     break;
1401
1402                 case "clear_cookies":
1403                     // Update the icon.
1404                     if (sharedPreferences.getBoolean("clear_cookies", true)) {
1405                         if (darkTheme) {
1406                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1407                         } else {
1408                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1409                         }
1410                     } else {
1411                         clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1412                     }
1413                     break;
1414
1415                 case "clear_dom_storage":
1416                     // Update the icon.
1417                     if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1418                         if (darkTheme) {
1419                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1420                         } else {
1421                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1422                         }
1423                     } else {
1424                         clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1425                     }
1426                     break;
1427
1428                 // This section can be removed once the minimum API >= 26.
1429                 case "clear_form_data":
1430                     // Update the icon.
1431                     if (sharedPreferences.getBoolean("clear_form_data", true)) {
1432                         if (darkTheme) {
1433                             clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1434                         } else {
1435                             clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1436                         }
1437                     } else {
1438                         clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1439                     }
1440                     break;
1441
1442                 case "clear_cache":
1443                     // Update the icon.
1444                     if (sharedPreferences.getBoolean("clear_cache", true)) {
1445                         if (darkTheme) {
1446                             clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1447                         } else {
1448                             clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1449                         }
1450                     } else {
1451                         clearCachePreference.setIcon(R.drawable.cache_warning);
1452                     }
1453                     break;
1454
1455                 case "homepage":
1456                     // Set the new homepage URL as the summary text for the Homepage preference.
1457                     homepagePreference.setSummary(sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
1458                     break;
1459
1460                 case "font_size":
1461                     // Update the font size summary text.
1462                     fontSizePreference.setSummary(sharedPreferences.getString("font_size", getString(R.string.font_size_default_value)) + "%%");
1463                     break;
1464
1465                 case "open_intents_in_new_tab":
1466                     // Update the icon.
1467                     if (sharedPreferences.getBoolean("open_intents_in_new_tab", true)) {
1468                         if (darkTheme) {
1469                             openIntentsInNewTabPreference.setIcon(R.drawable.tab_enabled_dark);
1470                         } else {
1471                             openIntentsInNewTabPreference.setIcon(R.drawable.tab_enabled_light);
1472                         }
1473                     } else {
1474                         if (darkTheme) {
1475                             openIntentsInNewTabPreference.setIcon(R.drawable.tab_disabled_dark);
1476                         } else {
1477                             openIntentsInNewTabPreference.setIcon(R.drawable.tab_disabled_light);
1478                         }
1479                     }
1480                     break;
1481
1482                 case "swipe_to_refresh":
1483                     // Update the icon.
1484                     if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1485                         if (darkTheme) {
1486                             swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1487                         } else {
1488                             swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1489                         }
1490                     } else {
1491                         if (darkTheme) {
1492                             swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1493                         } else {
1494                             swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1495                         }
1496                     }
1497                     break;
1498
1499                 case "scroll_app_bar":
1500                     // Update the icon.
1501                     if (sharedPreferences.getBoolean("scroll_app_bar", true)) {
1502                         if (darkTheme) {
1503                             scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1504                         } else {
1505                             scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1506                         }
1507                     } else {
1508                         if (darkTheme) {
1509                             scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1510                         } else {
1511                             scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1512                         }
1513                     }
1514                     break;
1515
1516                 case "display_additional_app_bar_icons":
1517                     // Update the icon.
1518                     if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1519                         if (darkTheme) {
1520                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1521                         } else {
1522                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1523                         }
1524                     } else {
1525                         if (darkTheme) {
1526                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1527                         } else {
1528                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1529                         }
1530                     }
1531                     break;
1532
1533                 case "download_with_external_app":
1534                     // Update the icon.
1535                     if (sharedPreferences.getBoolean("download_with_external_app", false)) {
1536                         if (darkTheme) {
1537                             downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
1538                         } else {
1539                             downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
1540                         }
1541                     } else {
1542                         if (darkTheme) {
1543                             downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
1544                         } else {
1545                             downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
1546                         }
1547                     }
1548                     break;
1549
1550                 case "dark_theme":
1551                     // Update the icon.
1552                     if (sharedPreferences.getBoolean("dark_theme", false)) {
1553                         darkThemePreference.setIcon(R.drawable.theme_dark);
1554                     } else {
1555                         darkThemePreference.setIcon(R.drawable.theme_light);
1556                     }
1557
1558                     // Create an intent to restart Privacy Browser.
1559                     Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1560
1561                     // Assert that the intent is not null to remove the lint error below.
1562                     assert changeThemeRestartIntent != null;
1563
1564                     // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack.  It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1565                     changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1566
1567                     // Create a handler to restart the activity.
1568                     Handler changeThemeRestartHandler = new Handler();
1569
1570                     // Create a runnable to restart the activity.
1571                     Runnable changeThemeRestartRunnable = () -> {
1572                         // Restart the activity.
1573                         startActivity(changeThemeRestartIntent);
1574
1575                         // Kill this instance of Privacy Browser.  Otherwise, the app exhibits sporadic behavior after the restart.
1576                         System.exit(0);
1577                     };
1578
1579                     // Restart the activity after 100 milliseconds, so that the app has enough time to save the change to the preference.
1580                     changeThemeRestartHandler.postDelayed(changeThemeRestartRunnable, 100);
1581                     break;
1582
1583                 case "night_mode":
1584                     // Store the current night mode status.
1585                     boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1586                     boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript", false);
1587
1588                     // Update the icon.
1589                     if (currentNightModeBoolean) {
1590                         if (darkTheme) {
1591                             nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1592                         } else {
1593                             nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1594                         }
1595                     } else {
1596                         if (darkTheme) {
1597                             nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1598                         } else {
1599                             nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1600                         }
1601                     }
1602
1603                     // Update the status of `javaScriptPreference` and `domStoragePreference`.
1604                     javaScriptPreference.setEnabled(!currentNightModeBoolean);
1605                     domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1606
1607                     // Update the `javaScriptPreference` icon.
1608                     if (currentNightModeBoolean || currentJavaScriptBoolean) {
1609                         javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1610                     } else {
1611                         javaScriptPreference.setIcon(R.drawable.privacy_mode);
1612                     }
1613
1614                     // Update the DOM storage preference icon.
1615                     if (currentNightModeBoolean || currentJavaScriptBoolean) {  // The preference is enabled.
1616                         if (sharedPreferences.getBoolean("dom_storage", false)) {  // DOM storage is enabled.
1617                             domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1618                         } else {  // DOM storage is disabled.
1619                             if (darkTheme) {
1620                                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1621                             } else {
1622                                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1623                             }
1624                         }
1625                     } else {  // The preference is disabled.  The icon should be ghosted.
1626                         if (darkTheme) {
1627                             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1628                         } else {
1629                             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1630                         }
1631                     }
1632                     break;
1633
1634                 case "display_webpage_images":
1635                     if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1636                         // Update the icon.
1637                         if (darkTheme) {
1638                             displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1639                         } else {
1640                             displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1641                         }
1642                     } else {
1643                         // Update the icon.
1644                         if (darkTheme) {
1645                             displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1646                         } else {
1647                             displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1648                         }
1649                     }
1650                     break;
1651             }
1652         };
1653
1654         // Register the listener.
1655         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1656     }
1657
1658     // It is necessary to re-register the listener on every resume or it will randomly stop working because apps can be paused and resumed at any time, even while running in the foreground.
1659     @Override
1660     public void onPause() {
1661         super.onPause();
1662         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1663     }
1664
1665     @Override
1666     public void onResume() {
1667         super.onResume();
1668         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1669     }
1670 }