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