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