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