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