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