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