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