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