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