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