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