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