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