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