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