Add a context menu entry to Open in New Tab.
[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                     break;
823
824                 case "user_agent":
825                     // Get the new user agent name.
826                     String newUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
827
828                     // Get the array position for the new user agent name.
829                     int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
830
831                     // Get the translated new user agent name.
832                     String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
833
834                     // Populate the user agent summary.
835                     switch (newUserAgentArrayPosition) {
836                         case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
837                             // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
838                             userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
839
840                             // Disable the custom user agent preference.
841                             customUserAgentPreference.setEnabled(false);
842
843                             // Set the custom user agent preference icon according to the theme.
844                             if (MainWebViewActivity.darkTheme) {
845                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
846                             } else {
847                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
848                             }
849                             break;
850
851                         case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
852                             // Set the summary text.
853                             userAgentPreference.setSummary(R.string.custom_user_agent);
854
855                             // Enable the custom user agent preference.
856                             customUserAgentPreference.setEnabled(true);
857
858                             // Set the custom user agent preference icon according to the theme.
859                             if (MainWebViewActivity.darkTheme) {
860                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
861                             } else {
862                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
863                             }
864                             break;
865
866                         default:
867                             // Get the user agent summary from the user agent data array.
868                             userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
869
870                             // Disable the custom user agent preference.
871                             customUserAgentPreference.setEnabled(false);
872
873                             // Set the custom user agent preference icon according to the theme.
874                             if (MainWebViewActivity.darkTheme) {
875                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
876                             } else {
877                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
878                             }
879                     }
880                     break;
881
882                 case "custom_user_agent":
883                     // Set the new custom user agent as the summary text for the preference.
884                     customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
885                     break;
886
887                 case "incognito_mode":
888                     // Update the icon.
889                     if (sharedPreferences.getBoolean("incognito_mode", false)) {
890                         if (MainWebViewActivity.darkTheme) {
891                             incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
892                         } else {
893                             incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
894                         }
895                     } else {
896                         if (MainWebViewActivity.darkTheme) {
897                             incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
898                         } else {
899                             incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
900                         }
901                     }
902                     break;
903
904                 case "do_not_track":
905                     // Update the icon.
906                     if (sharedPreferences.getBoolean("do_not_track", false)) {
907                         if (MainWebViewActivity.darkTheme) {
908                             doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
909                         } else {
910                             doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
911                         }
912                     } else {
913                         if (MainWebViewActivity.darkTheme) {
914                             doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
915                         } else {
916                             doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
917                         }
918                     }
919
920                     break;
921
922                 case "allow_screenshots":
923                     // Update the icon.
924                     if (sharedPreferences.getBoolean("allow_screenshots", false)) {
925                         if (MainWebViewActivity.darkTheme) {
926                             allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
927                         } else {
928                             allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
929                         }
930                     } else {
931                         if (MainWebViewActivity.darkTheme) {
932                             allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
933                         } else {
934                             allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
935                         }
936                     }
937
938                     // Create an intent to restart Privacy Browser.
939                     Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
940
941                     // Assert that the intent is not null to remove the lint error below.
942                     assert allowScreenshotsRestartIntent != null;
943
944                     // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack.  It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.  TODO.
945                     allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
946
947                     // Make it so.
948                     startActivity(allowScreenshotsRestartIntent);
949                     break;
950
951                 case "easylist":
952                     // Update the icon.
953                     if (sharedPreferences.getBoolean("easylist", true)) {
954                         if (MainWebViewActivity.darkTheme) {
955                             easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
956                         } else {
957                             easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
958                         }
959                     } else {
960                         if (MainWebViewActivity.darkTheme) {
961                             easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
962                         } else {
963                             easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
964                         }
965                     }
966                     break;
967
968                 case "easyprivacy":
969                     // Update the icon.
970                     if (sharedPreferences.getBoolean("easyprivacy", true)) {
971                         if (MainWebViewActivity.darkTheme) {
972                             easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
973                         } else {
974                             easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
975                         }
976                     } else {
977                         if (MainWebViewActivity.darkTheme) {
978                             easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
979                         } else {
980                             easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
981                         }
982                     }
983                     break;
984
985                 case "fanboys_annoyance_list":
986                     boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
987                     boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
988
989                     // Update the Fanboy icons.
990                     if (currentFanboyAnnoyanceList) {  // Fanboy's annoyance list is enabled.
991                         if (MainWebViewActivity.darkTheme) {
992                             // Update the Fanboy's annoyance list icon.
993                             fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
994
995                             // Update the Fanboy's social blocking list icon.
996                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
997                         } else {
998                             // Update the Fanboy's annoyance list icon.
999                             fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
1000
1001                             // Update the Fanboy's social blocking list icon.
1002                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
1003                         }
1004                     } else {  // Fanboy's annoyance list is disabled.
1005                         if (MainWebViewActivity.darkTheme) {
1006                             // Update the Fanboy's annoyance list icon.
1007                             fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
1008
1009                             // Update the Fanboy's social blocking list icon.
1010                             if (currentFanboySocialBlockingList) {
1011                                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1012                             } else {
1013                                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1014                             }
1015                         } else {
1016                             // Update the Fanboy's annoyance list icon.
1017                             fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
1018
1019                             // Update the Fanboy's social blocking list icon.
1020                             if (currentFanboySocialBlockingList) {
1021                                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1022                             } else {
1023                                 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1024                             }
1025                         }
1026                     }
1027
1028                     // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
1029                     fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
1030                     break;
1031
1032                 case "fanboys_social_blocking_list":
1033                     // Update the icon.
1034                     if (sharedPreferences.getBoolean("fanboys_social_blocking_list", true)) {
1035                         if (MainWebViewActivity.darkTheme) {
1036                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1037                         } else {
1038                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1039                         }
1040                     } else {
1041                         if (MainWebViewActivity.darkTheme) {
1042                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1043                         } else {
1044                             fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1045                         }
1046                     }
1047                     break;
1048
1049                 case "ultraprivacy":
1050                     // Update the icon.
1051                     if (sharedPreferences.getBoolean("ultraprivacy", true)) {
1052                         if (MainWebViewActivity.darkTheme) {
1053                             ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
1054                         } else {
1055                             ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
1056                         }
1057                     } else {
1058                         if (MainWebViewActivity.darkTheme) {
1059                             ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
1060                         } else {
1061                             ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
1062                         }
1063                     }
1064                     break;
1065
1066                 case "block_all_third_party_requests":
1067                     // Update the icon.
1068                     if (sharedPreferences.getBoolean("block_all_third_party_requests", false)) {
1069                         if (MainWebViewActivity.darkTheme) {
1070                             blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
1071                         } else {
1072                             blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
1073                         }
1074                     } else {
1075                         if (MainWebViewActivity.darkTheme) {
1076                             blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
1077                         } else {
1078                             blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
1079                         }
1080                     }
1081                     break;
1082
1083                 case "proxy_through_orbot":
1084                     // Get current settings.
1085                     boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
1086                     String currentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1087
1088                     // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
1089                     torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
1090
1091                     // Update the icons.
1092                     if (currentProxyThroughOrbot) {
1093                         // Set the Tor icons according to the theme.
1094                         if (MainWebViewActivity.darkTheme) {
1095                             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1096                             torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1097                             torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1098
1099                             // Set the `torSearchCustomURLPreference` icon.
1100                             if (torSearchCustomURLPreference.isEnabled()) {
1101                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1102                             } else {
1103                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1104                             }
1105                         } else {
1106                             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1107                             torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1108                             torSearchPreference.setIcon(R.drawable.search_enabled_light);
1109
1110                             // Set the `torSearchCustomURLPreference` icon.
1111                             if (torSearchCustomURLPreference.isEnabled()) {
1112                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1113                             } else {
1114                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1115                             }
1116                         }
1117                     } else {  // Proxy through Orbot is disabled.
1118                         if (MainWebViewActivity.darkTheme) {
1119                             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1120                             torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1121                             torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1122                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1123                         } else {
1124                             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1125                             torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1126                             torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1127                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1128                         }
1129                     }
1130                     break;
1131
1132                 case "tor_homepage":
1133                     // Set the new tor homepage URL as the summary text for the `tor_homepage` preference.  The default is Searx:  `http://ulrn6sryqaifefld.onion/`.
1134                     torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
1135                     break;
1136
1137                 case "tor_search":
1138                     // Get the present search string.
1139                     String presentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1140
1141                     // Update the preferences.
1142                     if (presentTorSearchString.equals("Custom URL")) {
1143                         // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1144                         torSearchPreference.setSummary(R.string.custom_url);
1145
1146                         // Enable `torSearchCustomURLPreference`.
1147                         torSearchCustomURLPreference.setEnabled(true);
1148
1149                         // Update the `torSearchCustomURLPreference` icon.
1150                         if (MainWebViewActivity.darkTheme) {
1151                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1152                         } else {
1153                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1154                         }
1155                     } else {
1156                         // Set the array value as the summary text.
1157                         torSearchPreference.setSummary(presentTorSearchString);
1158
1159                         // Disable `torSearchCustomURLPreference`.
1160                         torSearchCustomURLPreference.setEnabled(false);
1161
1162                         // Update the `torSearchCustomURLPreference` icon.
1163                         if (MainWebViewActivity.darkTheme) {
1164                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1165                         } else {
1166                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1167                         }
1168                     }
1169                     break;
1170
1171                 case "tor_search_custom_url":
1172                     // Set the summary text for `tor_search_custom_url`.
1173                     torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", getString(R.string.tor_search_custom_url_default_value)));
1174                     break;
1175
1176                 case "search":
1177                     // Store the new search string.
1178                     String newSearchString = sharedPreferences.getString("search", getString(R.string.search_default_value));
1179
1180                     // Update `searchPreference` and `searchCustomURLPreference`.
1181                     if (newSearchString.equals("Custom URL")) {  // `Custom URL` is selected.
1182                         // Set the summary text to `R.string.custom_url`, which is translated.
1183                         searchPreference.setSummary(R.string.custom_url);
1184
1185                         // Enable `searchCustomURLPreference`.
1186                         searchCustomURLPreference.setEnabled(true);
1187
1188                         // Set the `searchCustomURLPreference` according to the theme.
1189                         if (MainWebViewActivity.darkTheme) {
1190                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1191                         } else {
1192                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1193                         }
1194                     } else {  // `Custom URL` is not selected.
1195                         // Set the summary text to `newSearchString`.
1196                         searchPreference.setSummary(newSearchString);
1197
1198                         // Disable `searchCustomURLPreference`.
1199                         searchCustomURLPreference.setEnabled(false);
1200
1201                         // Set the `searchCustomURLPreference` according to the theme.
1202                         if (MainWebViewActivity.darkTheme) {
1203                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1204                         } else {
1205                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1206                         }
1207                     }
1208                     break;
1209
1210                 case "search_custom_url":
1211                     // Set the new custom search URL as the summary text for `search_custom_url`.  The default is `""`.
1212                     searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value)));
1213                     break;
1214
1215                 case "full_screen_browsing_mode":
1216                     if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {  // Full screen browsing is enabled.
1217                         // Set the full screen browsing mode preference icon according to the theme.
1218                         if (MainWebViewActivity.darkTheme) {
1219                             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1220                         } else {
1221                             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1222                         }
1223
1224                         // Set the hide app bar preference icon.
1225                         if (sharedPreferences.getBoolean("hide_app_bar", true)) {  //  Hide app bar is enabled.
1226                             // Set the icon according to the theme.
1227                             if (MainWebViewActivity.darkTheme) {
1228                                 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1229                             } else {
1230                                 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1231                             }
1232                         } else {  // Hide app bar is disabled.
1233                             // Set the icon according to the theme.
1234                             if (MainWebViewActivity.darkTheme) {
1235                                 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1236                             } else {
1237                                 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1238                             }
1239                         }
1240                     } else {  // Full screen browsing is disabled.
1241                         // Update the icons according to the theme.
1242                         if (MainWebViewActivity.darkTheme) {
1243                             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1244                             hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_dark);
1245                         } else {
1246                             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1247                             hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_light);
1248                         }
1249                     }
1250                     break;
1251
1252                 case "hide_app_bar":
1253                     // Update the icon.
1254                     if (sharedPreferences.getBoolean("hide_app_bar", true)) {  // Hide app bar is enabled.
1255                         // Set the icon according to the theme.
1256                         if (MainWebViewActivity.darkTheme) {
1257                             hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1258                         } else {
1259                             hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1260                         }
1261                     } else {  // Hide app bar is disabled.
1262                         // Set the icon according to the theme.
1263                         if (MainWebViewActivity.darkTheme) {
1264                             hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1265                         } else {
1266                             hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1267                         }
1268                     }
1269                     break;
1270
1271                 case "clear_everything":
1272                     // Store the new `clear_everything` status
1273                     boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1274
1275                     // Update the status of the `Clear and Exit` preferences.
1276                     clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1277                     clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1278                     clearFormDataPreference.setEnabled(!newClearEverythingBoolean);  // This line can be removed once the minimum API >= 26.
1279                     clearCachePreference.setEnabled(!newClearEverythingBoolean);
1280
1281                     // Update the `clearEverythingPreference` icon.
1282                     if (newClearEverythingBoolean) {
1283                         if (MainWebViewActivity.darkTheme) {
1284                             clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1285                         } else {
1286                             clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1287                         }
1288                     } else {
1289                         clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1290                     }
1291
1292                     // Update the `clearCookiesPreference` icon.
1293                     if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1294                         if (MainWebViewActivity.darkTheme) {
1295                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1296                         } else {
1297                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1298                         }
1299                     } else {
1300                         clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1301                     }
1302
1303                     // Update the `clearDomStoragePreference` icon.
1304                     if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1305                         if (MainWebViewActivity.darkTheme) {
1306                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1307                         } else {
1308                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1309                         }
1310                     } else {
1311                         clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1312                     }
1313
1314                     // Update the clear form data preference icon if the API < 26.
1315                     if (Build.VERSION.SDK_INT < 26) {
1316                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1317                             if (MainWebViewActivity.darkTheme) {
1318                                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1319                             } else {
1320                                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1321                             }
1322                         } else {
1323                             clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1324                         }
1325                     }
1326
1327                     // Update the `clearCachePreference` icon.
1328                     if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1329                         if (MainWebViewActivity.darkTheme) {
1330                             clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1331                         } else {
1332                             clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1333                         }
1334                     } else {
1335                         clearCachePreference.setIcon(R.drawable.cache_warning);
1336                     }
1337                     break;
1338
1339                 case "clear_cookies":
1340                     // Update the icon.
1341                     if (sharedPreferences.getBoolean("clear_cookies", true)) {
1342                         if (MainWebViewActivity.darkTheme) {
1343                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1344                         } else {
1345                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1346                         }
1347                     } else {
1348                         clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1349                     }
1350                     break;
1351
1352                 case "clear_dom_storage":
1353                     // Update the icon.
1354                     if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1355                         if (MainWebViewActivity.darkTheme) {
1356                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1357                         } else {
1358                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1359                         }
1360                     } else {
1361                         clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1362                     }
1363                     break;
1364
1365                 // This section can be removed once the minimum API >= 26.
1366                 case "clear_form_data":
1367                     // Update the icon.
1368                     if (sharedPreferences.getBoolean("clear_form_data", true)) {
1369                         if (MainWebViewActivity.darkTheme) {
1370                             clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1371                         } else {
1372                             clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1373                         }
1374                     } else {
1375                         clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1376                     }
1377                     break;
1378
1379                 case "clear_cache":
1380                     // Update the icon.
1381                     if (sharedPreferences.getBoolean("clear_cache", true)) {
1382                         if (MainWebViewActivity.darkTheme) {
1383                             clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1384                         } else {
1385                             clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1386                         }
1387                     } else {
1388                         clearCachePreference.setIcon(R.drawable.cache_warning);
1389                     }
1390                     break;
1391
1392                 case "homepage":
1393                     // Set the new homepage URL as the summary text for the Homepage preference.
1394                     homepagePreference.setSummary(sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
1395                     break;
1396
1397                 case "font_size":
1398                     // Update the font size summary text.
1399                     fontSizePreference.setSummary(sharedPreferences.getString("font_size", getString(R.string.font_size_default_value)) + "%%");
1400                     break;
1401
1402                 case "swipe_to_refresh":
1403                     // Update the icon.
1404                     if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1405                         if (MainWebViewActivity.darkTheme) {
1406                             swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1407                         } else {
1408                             swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1409                         }
1410                     } else {
1411                         if (MainWebViewActivity.darkTheme) {
1412                             swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1413                         } else {
1414                             swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1415                         }
1416                     }
1417                     break;
1418
1419                 case "scroll_app_bar":
1420                     // Update the icon.
1421                     if (sharedPreferences.getBoolean("scroll_app_bar", true)) {
1422                         if (MainWebViewActivity.darkTheme) {
1423                             scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1424                         } else {
1425                             scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1426                         }
1427                     } else {
1428                         if (MainWebViewActivity.darkTheme) {
1429                             scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1430                         } else {
1431                             scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1432                         }
1433                     }
1434                     break;
1435
1436                 case "display_additional_app_bar_icons":
1437                     // Update the icon.
1438                     if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1439                         if (MainWebViewActivity.darkTheme) {
1440                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1441                         } else {
1442                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1443                         }
1444                     } else {
1445                         if (MainWebViewActivity.darkTheme) {
1446                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1447                         } else {
1448                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1449                         }
1450                     }
1451                     break;
1452
1453                 case "download_with_external_app":
1454                     // Update the icon.
1455                     if (sharedPreferences.getBoolean("download_with_external_app", false)) {
1456                         if (MainWebViewActivity.darkTheme) {
1457                             downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
1458                         } else {
1459                             downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
1460                         }
1461                     } else {
1462                         if (MainWebViewActivity.darkTheme) {
1463                             downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
1464                         } else {
1465                             downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
1466                         }
1467                     }
1468                     break;
1469
1470                 case "dark_theme":
1471                     // Update the icon.
1472                     if (sharedPreferences.getBoolean("dark_theme", false)) {
1473                         darkThemePreference.setIcon(R.drawable.theme_dark);
1474                     } else {
1475                         darkThemePreference.setIcon(R.drawable.theme_light);
1476                     }
1477
1478                     // Create an intent to restart Privacy Browser.
1479                     Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1480
1481                     // Assert that the intent is not null to remove the lint error below.
1482                     assert changeThemeRestartIntent != null;
1483
1484                     // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack.  It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.  TODO.
1485                     changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1486
1487                     // Make it so.
1488                     startActivity(changeThemeRestartIntent);
1489                     break;
1490
1491                 case "night_mode":
1492                     // Set the URL to be reloaded on restart to apply the new night mode setting.
1493                     MainWebViewActivity.reloadOnRestart = true;
1494
1495                     // Store the current night mode status.
1496                     boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1497                     boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript", false);
1498
1499                     // Update the icon.
1500                     if (currentNightModeBoolean) {
1501                         if (MainWebViewActivity.darkTheme) {
1502                             nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1503                         } else {
1504                             nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1505                         }
1506                     } else {
1507                         if (MainWebViewActivity.darkTheme) {
1508                             nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1509                         } else {
1510                             nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1511                         }
1512                     }
1513
1514                     // Update the status of `javaScriptPreference` and `domStoragePreference`.
1515                     javaScriptPreference.setEnabled(!currentNightModeBoolean);
1516                     domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1517
1518                     // Update the `javaScriptPreference` icon.
1519                     if (currentNightModeBoolean || currentJavaScriptBoolean) {
1520                         javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1521                     } else {
1522                         javaScriptPreference.setIcon(R.drawable.privacy_mode);
1523                     }
1524
1525                     // Update the DOM storage preference icon.
1526                     if (currentNightModeBoolean || currentJavaScriptBoolean) {  // The preference is enabled.
1527                         if (sharedPreferences.getBoolean("dom_storage", false)) {  // DOM storage is enabled.
1528                             domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1529                         } else {  // DOM storage is disabled.
1530                             if (MainWebViewActivity.darkTheme) {
1531                                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1532                             } else {
1533                                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1534                             }
1535                         }
1536                     } else {  // The preference is disabled.  The icon should be ghosted.
1537                         if (MainWebViewActivity.darkTheme) {
1538                             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1539                         } else {
1540                             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1541                         }
1542                     }
1543                     break;
1544
1545                 case "display_webpage_images":
1546                     if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1547                         // Update the icon.
1548                         if (MainWebViewActivity.darkTheme) {
1549                             displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1550                         } else {
1551                             displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1552                         }
1553
1554                         // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1555                         MainWebViewActivity.reloadOnRestart = false;
1556                     } else {
1557                         // Update the icon.
1558                         if (MainWebViewActivity.darkTheme) {
1559                             displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1560                         } else {
1561                             displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1562                         }
1563
1564                         // Set `mainWebView` to reload on restart to remove the current images.
1565                         MainWebViewActivity.reloadOnRestart = true;
1566                     }
1567                     break;
1568             }
1569         };
1570
1571         // Register the listener.
1572         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1573     }
1574
1575     // 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.
1576     @Override
1577     public void onPause() {
1578         super.onPause();
1579         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1580     }
1581
1582     @Override
1583     public void onResume() {
1584         super.onResume();
1585         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1586     }
1587 }