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