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