Wait to enable the delete icon until the previous domain has been deleted. Fixes...
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / SettingsFragment.java
1 /*
2  * Copyright © 2016-2017 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 blockAdsPreference = findPreference("block_ads");
57         final Preference incognitoModePreference = findPreference("incognito_mode");
58         final Preference doNotTrackPreference = findPreference("do_not_track");
59         final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
60         final Preference torHomepagePreference = findPreference("tor_homepage");
61         final Preference torSearchPreference = findPreference("tor_search");
62         final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
63         final Preference searchPreference = findPreference("search");
64         final Preference searchCustomURLPreference = findPreference("search_custom_url");
65         final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
66         final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
67         final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
68         final Preference clearEverythingPreference = findPreference("clear_everything");
69         final Preference clearCookiesPreference = findPreference("clear_cookies");
70         final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
71         final Preference clearFormDataPreference = findPreference("clear_form_data");
72         final Preference clearCachePreference = findPreference("clear_cache");
73         final Preference homepagePreference = findPreference("homepage");
74         final Preference defaultFontSizePreference = findPreference("default_font_size");
75         final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
76         final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
77         final Preference darkThemePreference = findPreference("dark_theme");
78         final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
79
80         // Set dependencies.
81         domStoragePreference.setDependency("javascript_enabled");
82         torHomepagePreference.setDependency("proxy_through_orbot");
83         torSearchPreference.setDependency("proxy_through_orbot");
84         hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
85
86         // Get strings from the preferences.
87         String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
88         String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
89
90         // Get booleans from the preferences.
91         boolean javaScriptEnabledBoolean = savedPreferences.getBoolean("javascript_enabled", false);
92         boolean firstPartyCookiesEnabledBoolean = savedPreferences.getBoolean("first_party_cookies_enabled", false);
93         boolean thirdPartyCookiesEnabledBoolean = savedPreferences.getBoolean("third_party_cookies_enabled", false);
94         boolean proxyThroughOrbotBoolean = savedPreferences.getBoolean("proxy_through_orbot", false);
95         boolean fullScreenBrowsingModeBoolean = savedPreferences.getBoolean("full_screen_browsing_mode", false);
96         boolean hideSystemBarsBoolean = savedPreferences.getBoolean("hide_system_bars", false);
97         boolean clearEverythingBoolean = savedPreferences.getBoolean("clear_everything", true);
98
99         // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
100         thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
101
102         // We need to inflated a `WebView` to get the default user agent.
103         LayoutInflater inflater = getActivity().getLayoutInflater();
104         // `@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.  `false` does not attach the view to the root.
105         @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
106         final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
107
108         // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
109         switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
110             case "WebView default user agent":
111                 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
112                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
113                 break;
114
115             case "Custom user agent":
116                 // We can't use the string from the array because it is referenced in code and can't be translated.
117                 userAgentPreference.setSummary(R.string.custom_user_agent);
118                 break;
119
120             default:
121                 // Display the user agent from the preference as the summary text.
122                 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
123                 break;
124         }
125
126         // Set the summary text for "customUserAgentPreference" (the default is `PrivacyBrowser/1.0`) and enable it if `userAgentPreference` it set to `Custom user agent`.
127         customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
128         customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
129
130
131         // 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`.
132         torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
133
134
135         // 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=`
136         if (torSearchString.equals("Custom URL")) {
137             // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
138             torSearchPreference.setSummary(R.string.custom_url);
139         } else {
140             // Set the array value as the summary text.
141             torSearchPreference.setSummary(torSearchString);
142         }
143
144         // Set the summary text for `tor_search_custom_url`.  The default is `""`.
145         torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
146
147         // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
148         torSearchCustomURLPreference.setEnabled(proxyThroughOrbotBoolean && torSearchString.equals("Custom URL"));
149
150
151         // 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=`.
152         if (searchString.equals("Custom URL")) {
153             // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
154             searchPreference.setSummary(R.string.custom_url);
155         } else {
156             // Set the array value as the summary text.
157             searchPreference.setSummary(searchString);
158         }
159
160         // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
161         searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
162         searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
163
164
165         // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
166         translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
167
168         // Set the status of the `Clear and Exit` preferences.
169         clearCookiesPreference.setEnabled(!clearEverythingBoolean);
170         clearDomStoragePreference.setEnabled(!clearEverythingBoolean);
171         clearFormDataPreference.setEnabled(!clearEverythingBoolean);
172         clearCachePreference.setEnabled(!clearEverythingBoolean);
173
174         // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded.  The default is `https://duckduckgo.com`.
175         homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
176
177         // 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`.
178         defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
179
180
181         // Set the `javaScriptPreference` icon.
182         if (javaScriptEnabledBoolean) {
183             javaScriptPreference.setIcon(R.drawable.javascript_enabled);
184         } else {
185             javaScriptPreference.setIcon(R.drawable.privacy_mode);
186         }
187
188         // Set the `firstPartyCookiesPreference` icon.
189         if (firstPartyCookiesEnabledBoolean) {
190             firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
191         } else {
192             if (MainWebViewActivity.darkTheme) {
193                 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
194             } else {
195                 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
196             }
197         }
198
199         // Set the `thirdPartyCookiesPreference` icon.
200         if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
201             if (thirdPartyCookiesEnabledBoolean) {
202                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
203             } else {
204                 if (MainWebViewActivity.darkTheme) {
205                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
206                 } else {
207                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
208                 }
209             }
210         } else {
211             if (MainWebViewActivity.darkTheme) {
212                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
213             } else {
214                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
215             }
216         }
217
218         // Set the `domStoragePreference` icon.
219         if (javaScriptEnabledBoolean) {
220             if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
221                 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
222             } else {
223                 if (MainWebViewActivity.darkTheme) {
224                     domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
225                 } else {
226                     domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
227                 }
228             }
229         } else {
230             if (MainWebViewActivity.darkTheme) {
231                 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
232             } else {
233                 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
234             }
235         }
236
237         // Set the `saveFormDataPreference` icon.
238         if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
239             saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
240         } else {
241             if (MainWebViewActivity.darkTheme) {
242                 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
243             } else {
244                 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
245             }
246         }
247
248         // Set the `customUserAgentPreference` icon.
249         if (customUserAgentPreference.isEnabled()) {
250             if (MainWebViewActivity.darkTheme) {
251                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
252             } else {
253                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
254             }
255         } else {
256             if (MainWebViewActivity.darkTheme) {
257                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
258             } else {
259                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
260             }
261         }
262
263         // Set the `blockAdsPreference` icon.
264         if (savedPreferences.getBoolean("block_ads", true)) {
265             if (MainWebViewActivity.darkTheme) {
266                 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_dark);
267             } else {
268                 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_light);
269             }
270         } else {
271             if (MainWebViewActivity.darkTheme) {
272                 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_dark);
273             } else {
274                 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_light);
275             }
276         }
277
278         // Set the `incognitoModePreference` icon.
279         if (savedPreferences.getBoolean("incognito_mode", false)) {
280             if (MainWebViewActivity.darkTheme) {
281                 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
282             } else {
283                 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
284             }
285         } else {
286             if (MainWebViewActivity.darkTheme) {
287                 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
288             } else {
289                 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
290             }
291         }
292
293         // Set the `doNotTrackPreference` icon.
294         if (savedPreferences.getBoolean("do_not_track", false)) {
295             if (MainWebViewActivity.darkTheme) {
296                 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_dark);
297             } else {
298                 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_light);
299             }
300         } else {
301             if (MainWebViewActivity.darkTheme) {
302                 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_dark);
303             } else {
304                 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_light);
305             }
306         }
307
308         // Set the Tor icons according to the theme.
309         if (proxyThroughOrbotBoolean) {  // Proxying is enabled.
310             if (MainWebViewActivity.darkTheme) {
311                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
312                 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
313                 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
314
315                 // Set the custom search icon.
316                 if (torSearchCustomURLPreference.isEnabled()) {
317                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
318                 } else {
319                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
320                 }
321             } else {
322                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
323                 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
324                 torSearchPreference.setIcon(R.drawable.search_enabled_light);
325
326                 // Set the custom search icon.
327                 if (torSearchCustomURLPreference.isEnabled()) {
328                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
329                 } else {
330                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
331                 }
332             }
333         } else {  // Proxying is disabled.
334             if (MainWebViewActivity.darkTheme) {
335                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
336                 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
337                 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
338                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
339             } else {
340                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
341                 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
342                 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
343                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
344             }
345         }
346
347         // Set the `searchCustomURLPreference` icon.
348         if (searchCustomURLPreference.isEnabled()) {
349             if (MainWebViewActivity.darkTheme) {
350                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
351             } else {
352                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
353             }
354         } else {
355             if (MainWebViewActivity.darkTheme) {
356                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
357             } else {
358                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
359             }
360         }
361
362         // Set the full screen browsing mode icons.
363         if (fullScreenBrowsingModeBoolean) {  // `fullScreenBrowsingModeBoolean` is `true`.
364             // Set the `fullScreenBrowsingModePreference` icon according to the theme.
365             if (MainWebViewActivity.darkTheme) {
366                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
367             } else {
368                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
369             }
370
371             if (hideSystemBarsBoolean) {  // `hideSystemBarsBoolean` is `true`.
372                 // Set the icons according to the theme.
373                 if (MainWebViewActivity.darkTheme) {
374                     hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
375                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
376                 } else {
377                     hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
378                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
379                 }
380             } else {  // `hideSystemBarsBoolean` is `false`.
381                 // Set the `hideSystemBarsPreference` icon according to the theme.
382                 if (MainWebViewActivity.darkTheme) {
383                     // Set the icon for `hideSystemBarsPreference`.
384                     hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
385
386                     // Set the icon for `translucentNavigationBarPreference`.
387                     if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
388                         translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
389                     } else {
390                         translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
391                     }
392                 } else {
393                     // Set the icon for `hideSystemBarsPreference`.
394                     hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
395
396                     // Set the icon for `translucentNavigationBarPreference`.
397                     if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
398                         translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
399                     } else {
400                         translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
401                     }
402                 }
403             }
404         } else {  // `fullScreenBrowsingModeBoolean` is `false`.
405             // Set the icons according to the theme.
406             if (MainWebViewActivity.darkTheme) {
407                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
408                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
409                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
410             } else {
411                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
412                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
413                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
414             }
415         }
416
417         // Set the `clearEverythingPreference` icon.
418         if (clearEverythingBoolean) {
419             if (MainWebViewActivity.darkTheme) {
420                 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
421             } else {
422                 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
423             }
424         } else {
425             clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
426         }
427
428         // Set the `clearCookiesPreference` icon.
429         if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cookies", true)) {
430             if (MainWebViewActivity.darkTheme) {
431                 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
432             } else {
433                 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
434             }
435         } else {
436             clearCookiesPreference.setIcon(R.drawable.cookies_warning);
437         }
438
439         // Set the `clearDomStoragePreference` icon.
440         if (clearEverythingBoolean || savedPreferences.getBoolean("clear_dom_storage", true)) {
441             if (MainWebViewActivity.darkTheme) {
442                 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
443             } else {
444                 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
445             }
446         } else {
447             clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
448         }
449
450         // Set the `clearFormDataPreference` icon.
451         if (clearEverythingBoolean || savedPreferences.getBoolean("clear_form_data", true)) {
452             if (MainWebViewActivity.darkTheme) {
453                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
454             } else {
455                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
456             }
457         } else {
458             clearFormDataPreference.setIcon(R.drawable.form_data_warning);
459         }
460
461         // Set the `clearCachePreference` icon.
462         if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cache", true)) {
463             if (MainWebViewActivity.darkTheme) {
464                 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
465             } else {
466                 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
467             }
468         } else {
469             clearCachePreference.setIcon(R.drawable.cache_warning);
470         }
471
472         // Set the `swipeToRefreshPreference` icon.
473         if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
474             if (MainWebViewActivity.darkTheme) {
475                 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
476             } else {
477                 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
478             }
479         } else {
480             if (MainWebViewActivity.darkTheme) {
481                 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
482             } else {
483                 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
484             }
485         }
486
487         // Set the `displayAdditionalAppBarIconsPreference` icon.
488         if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
489             if (MainWebViewActivity.darkTheme) {
490                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
491             } else {
492                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
493             }
494         } else {
495             if (MainWebViewActivity.darkTheme) {
496                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
497             } else {
498                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
499             }
500         }
501
502         // Set the `darkThemePreference` icon.
503         if (savedPreferences.getBoolean("dark_theme", false)) {
504             darkThemePreference.setIcon(R.drawable.theme_dark);
505         } else {
506             darkThemePreference.setIcon(R.drawable.theme_light);
507         }
508
509         // Set the `displayWebpageImagesPreference` icon.
510         if (savedPreferences.getBoolean("display_webpage_images", true)) {
511             if (MainWebViewActivity.darkTheme) {
512                 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
513             } else {
514                 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
515             }
516         } else {
517             if (MainWebViewActivity.darkTheme) {
518                 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
519             } else {
520                 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
521             }
522         }
523
524
525         // Listen for preference changes.
526         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
527             @Override
528             // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.  We know.
529             @SuppressLint("SetJavaScriptEnabled")
530             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
531
532                 switch (key) {
533                     case "javascript_enabled":
534                         // Update the icons.
535                         if (sharedPreferences.getBoolean("javascript_enabled", false)) {
536                             // Update the icon for `javascript_enabled`.
537                             javaScriptPreference.setIcon(R.drawable.javascript_enabled);
538
539                             // Update the icon for `dom_storage_enabled`.
540                             if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
541                                 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
542                             } else {
543                                 if (MainWebViewActivity.darkTheme) {
544                                     domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
545                                 } else {
546                                     domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
547                                 }
548                             }
549                         } else {  // `javascript_enabled` is `false`.
550                             // Update the icon for `javascript_enabled`.
551                             javaScriptPreference.setIcon(R.drawable.privacy_mode);
552
553                             // Set the icon for `dom_storage_disabled` to be ghosted.
554                             if (MainWebViewActivity.darkTheme) {
555                                 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
556                             } else {
557                                 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
558                             }
559                         }
560                         break;
561
562                     case "first_party_cookies_enabled":
563                         // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
564                         if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
565                             // Set the icon for `first_party_cookies_enabled`.
566                             firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
567
568                             // Update the icon for `third_party_cookies_enabled`.
569                             if (Build.VERSION.SDK_INT >= 21) {
570                                 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
571                                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
572                                 } else {
573                                     if (MainWebViewActivity.darkTheme) {
574                                         thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
575                                     } else {
576                                         thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
577                                     }
578                                 }
579                             } else {
580                                 if (MainWebViewActivity.darkTheme) {
581                                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
582                                 } else {
583                                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
584                                 }
585                             }
586                         } else {  // `first_party_cookies_enabled` is `false`.
587                             // Update the icon for `first_party_cookies_enabled`.
588                             if (MainWebViewActivity.darkTheme) {
589                                 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
590                             } else {
591                                 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
592                             }
593
594                             // Set the icon for `third_party_cookies_enabled` to be ghosted.
595                             if (MainWebViewActivity.darkTheme) {
596                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
597                             } else {
598                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
599                             }
600                         }
601
602                         // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
603                         thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
604                         break;
605
606                     case "third_party_cookies_enabled":
607                         // Update the icon.
608                         if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
609                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
610                         } else {
611                             if (MainWebViewActivity.darkTheme) {
612                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
613                             } else {
614                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
615                             }
616                         }
617                         break;
618
619                     case "dom_storage_enabled":
620                         // Update the icon.
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                         break;
631
632                     case "save_form_data_enabled":
633                         // Update the icon.
634                         if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
635                             saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
636                         } else {
637                             if (MainWebViewActivity.darkTheme) {
638                                 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
639                             } else {
640                                 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
641                             }
642                         }
643
644                     case "user_agent":
645                         String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
646
647                         switch (userAgentString) {
648                             case "WebView default user agent":
649                                 // Display the user agent as the summary text for `userAgentPreference`.
650                                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
651
652                                 // Disable `customUserAgentPreference`.
653                                 customUserAgentPreference.setEnabled(false);
654
655                                 // Set the `customUserAgentPreference` icon according to the theme.
656                                 if (MainWebViewActivity.darkTheme) {
657                                     customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
658                                 } else {
659                                     customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
660                                 }
661                                 break;
662
663                             case "Custom user agent":
664                                 // Display `Custom user agent` as the summary text for `userAgentPreference`.
665                                 userAgentPreference.setSummary(R.string.custom_user_agent);
666
667                                 // Enable `customUserAgentPreference`.
668                                 customUserAgentPreference.setEnabled(true);
669
670                                 // Set the `customUserAgentPreference` icon according to the theme.
671                                 if (MainWebViewActivity.darkTheme) {
672                                     customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
673                                 } else {
674                                     customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
675                                 }
676                                 break;
677
678                             default:
679                                 // Display the user agent as the summary text for `userAgentPreference`.
680                                 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
681
682                                 // Disable `customUserAgentPreference`.
683                                 customUserAgentPreference.setEnabled(false);
684
685                                 // Set the `customUserAgentPreference` icon according to the theme.
686                                 if (MainWebViewActivity.darkTheme) {
687                                     customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
688                                 } else {
689                                     customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
690                                 }
691                                 break;
692                         }
693                         break;
694
695                     case "custom_user_agent":
696                         // Set the new custom user agent as the summary text for `custom_user_agent`.  The default is `PrivacyBrowser/1.0`.
697                         customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
698                         break;
699
700                     case "block_ads":
701                         // Update the icon.
702                         if (sharedPreferences.getBoolean("block_ads", true)) {
703                             if (MainWebViewActivity.darkTheme) {
704                                 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_dark);
705                             } else {
706                                 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_light);
707                             }
708                         } else {
709                             if (MainWebViewActivity.darkTheme) {
710                                 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_dark);
711                             } else {
712                                 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_light);
713                             }
714                         }
715                         break;
716
717                     case "incognito_mode":
718                         // Update the icon.
719                         if (sharedPreferences.getBoolean("incognito_mode", false)) {
720                             if (MainWebViewActivity.darkTheme) {
721                                 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
722                             } else {
723                                 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
724                             }
725                         } else {
726                             if (MainWebViewActivity.darkTheme) {
727                                 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
728                             } else {
729                                 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
730                             }
731                         }
732                         break;
733
734                     case "do_not_track":
735                         // Update the icon.
736                         if (sharedPreferences.getBoolean("do_not_track", false)) {
737                             if (MainWebViewActivity.darkTheme) {
738                                 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_dark);
739                             } else {
740                                 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_light);
741                             }
742                         } else {
743                             if (MainWebViewActivity.darkTheme) {
744                                 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_dark);
745                             } else {
746                                 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_light);
747                             }
748                         }
749
750                         break;
751
752                     case "proxy_through_orbot":
753                         // Get current settings.
754                         boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
755                         String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
756
757                         // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
758                         torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
759
760                         // Update the icons.
761                         if (currentProxyThroughOrbot) {
762                             // Set the Tor icons according to the theme.
763                             if (MainWebViewActivity.darkTheme) {
764                                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
765                                 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
766                                 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
767
768                                 // Set the `torSearchCustomURLPreference` icon.
769                                 if (torSearchCustomURLPreference.isEnabled()) {
770                                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
771                                 } else {
772                                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
773                                 }
774                             } else {
775                                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
776                                 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
777                                 torSearchPreference.setIcon(R.drawable.search_enabled_light);
778
779                                 // Set the `torSearchCustomURLPreference` icon.
780                                 if (torSearchCustomURLPreference.isEnabled()) {
781                                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
782                                 } else {
783                                     torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
784                                 }
785                             }
786                         } else {  // Proxy through Orbot is disabled.
787                             if (MainWebViewActivity.darkTheme) {
788                                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
789                                 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
790                                 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
791                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
792                             } else {
793                                 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
794                                 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
795                                 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
796                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
797                             }
798                         }
799                         break;
800
801                     case "tor_homepage":
802                         // Set the new tor homepage URL as the summary text for the `tor_homepage` preference.  The default is DuckDuckGo:  `https://3g2upl4pq6kufc4m.onion`.
803                         torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
804                         break;
805
806                     case "tor_search":
807                         // Get the present search string.
808                         String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
809
810                         // Update the preferences.
811                         if (presentTorSearchString.equals("Custom URL")) {
812                             // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
813                             torSearchPreference.setSummary(R.string.custom_url);
814
815                             // Enable `torSearchCustomURLPreference`.
816                             torSearchCustomURLPreference.setEnabled(true);
817
818                             // Update the `torSearchCustomURLPreference` icon.
819                             if (MainWebViewActivity.darkTheme) {
820                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
821                             } else {
822                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
823                             }
824                         } else {
825                             // Set the array value as the summary text.
826                             torSearchPreference.setSummary(presentTorSearchString);
827
828                             // Disable `torSearchCustomURLPreference`.
829                             torSearchCustomURLPreference.setEnabled(false);
830
831                             // Update the `torSearchCustomURLPreference` icon.
832                             if (MainWebViewActivity.darkTheme) {
833                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
834                             } else {
835                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
836                             }
837                         }
838                         break;
839
840                     case "tor_search_custom_url":
841                         // Set the summary text for `tor_search_custom_url`.
842                         torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
843                         break;
844
845                     case "search":
846                         // Store the new search string.
847                         String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
848
849                         // Update `searchPreference` and `searchCustomURLPreference`.
850                         if (newSearchString.equals("Custom URL")) {  // `Custom URL` is selected.
851                             // Set the summary text to `R.string.custom_url`, which is translated.
852                             searchPreference.setSummary(R.string.custom_url);
853
854                             // Enable `searchCustomURLPreference`.
855                             searchCustomURLPreference.setEnabled(true);
856
857                             // Set the `searchCustomURLPreference` according to the theme.
858                             if (MainWebViewActivity.darkTheme) {
859                                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
860                             } else {
861                                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
862                             }
863                         } else {  // `Custom URL` is not selected.
864                             // Set the summary text to `newSearchString`.
865                             searchPreference.setSummary(newSearchString);
866
867                             // Disable `searchCustomURLPreference`.
868                             searchCustomURLPreference.setEnabled(false);
869
870                             // Set the `searchCustomURLPreference` according to the theme.
871                             if (MainWebViewActivity.darkTheme) {
872                                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
873                             } else {
874                                 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
875                             }
876                         }
877                         break;
878
879                     case "search_custom_url":
880                         // Set the new custom search URL as the summary text for `search_custom_url`.  The default is `""`.
881                         searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
882                         break;
883
884                     case "full_screen_browsing_mode":
885                         if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
886                             // Set the `fullScreenBrowsingModePreference` icon according to the theme.
887                             if (MainWebViewActivity.darkTheme) {
888                                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
889                             } else {
890                                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
891                             }
892
893                             if (sharedPreferences.getBoolean("hide_system_bars", false)) {  // `hide_system_bars` is `true`.
894                                 // Disable `translucentNavigationBarPreference`.
895                                 translucentNavigationBarPreference.setEnabled(false);
896
897                                 // Set the icons according to the theme.
898                                 if (MainWebViewActivity.darkTheme) {
899                                     hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
900                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
901                                 } else {
902                                     hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
903                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
904                                 }
905                             } else {  // `hide_system_bars` is `false`.
906                                 // Enable `translucentNavigationBarPreference`.
907                                 translucentNavigationBarPreference.setEnabled(true);
908
909                                 // Set the icons according to the theme.
910                                 if (MainWebViewActivity.darkTheme) {  // Use the dark theme.
911                                     // Set the `hideSystemBarsPreference` icon.
912                                     hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
913
914                                     // Set the `translucentNavigationBarPreference` icon.
915                                     if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
916                                         translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
917                                     } else {
918                                         translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
919                                     }
920                                 } else {  // Use the light theme.
921                                     // Set the `hideSystemBarsPreference` icon.
922                                     hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
923
924                                     // Set the `translucentNavigationBarPreference` icon.
925                                     if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
926                                         translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
927                                     } else {
928                                         translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
929                                     }
930                                 }
931                             }
932                         } else {  // `full_screen_browsing_mode` is false.
933                             // Disable `translucentNavigationBarPreference`.
934                             translucentNavigationBarPreference.setEnabled(false);
935
936                             // Update the icons according to the theme.
937                             if (MainWebViewActivity.darkTheme) {
938                                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
939                                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
940                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
941                             } else {
942                                 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
943                                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
944                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
945                             }
946                         }
947                         break;
948
949                     case "hide_system_bars":
950                         if (sharedPreferences.getBoolean("hide_system_bars", false)) {
951                             // Disable `translucentNavigationBarPreference`.
952                             translucentNavigationBarPreference.setEnabled(false);
953
954                             // Set the icons according to the theme.
955                             if (MainWebViewActivity.darkTheme) {
956                                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
957                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
958                             } else {
959                                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
960                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
961                             }
962                         } else {  // `hide_system_bars` is false.
963                             // Enable `translucentNavigationBarPreference`.
964                             translucentNavigationBarPreference.setEnabled(true);
965
966                             // Set the icons according to the theme.
967                             if (MainWebViewActivity.darkTheme) {
968                                 // Set the `hideSystemBarsPreference` icon.
969                                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
970
971                                 // Set the `translucentNavigationBarPreference` icon.
972                                 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
973                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
974                                 } else {
975                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
976                                 }
977                             } else {
978                                 // Set the `hideSystemBarsPreference` icon.
979                                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
980
981                                 // Set the `translucentNavigationBarPreference` icon.
982                                 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
983                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
984                                 } else {
985                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
986                                 }
987                             }
988                         }
989                         break;
990
991                     case "translucent_navigation_bar":
992                         // Update the icon.
993                         if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
994                             if (MainWebViewActivity.darkTheme) {
995                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
996                             } else {
997                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
998                             }
999                         } else {
1000                             if (MainWebViewActivity.darkTheme) {
1001                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1002                             } else {
1003                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1004                             }
1005                         }
1006                         break;
1007
1008                     case "clear_everything":
1009                         // Store the new `clear_everything` status
1010                         boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1011
1012                         // Update the status of the `Clear and Exit` preferences.
1013                         clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1014                         clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1015                         clearFormDataPreference.setEnabled(!newClearEverythingBoolean);
1016                         clearCachePreference.setEnabled(!newClearEverythingBoolean);
1017
1018                         // Update the `clearEverythingPreference` icon.
1019                         if (newClearEverythingBoolean) {
1020                             if (MainWebViewActivity.darkTheme) {
1021                                 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1022                             } else {
1023                                 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1024                             }
1025                         } else {
1026                             clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1027                         }
1028
1029                         // Update the `clearCookiesPreference` icon.
1030                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1031                             if (MainWebViewActivity.darkTheme) {
1032                                 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1033                             } else {
1034                                 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1035                             }
1036                         } else {
1037                             clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1038                         }
1039
1040                         // Update the `clearDomStoragePreference` icon.
1041                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1042                             if (MainWebViewActivity.darkTheme) {
1043                                 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1044                             } else {
1045                                 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1046                             }
1047                         } else {
1048                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1049                         }
1050
1051                         // Update the `clearFormDataPreference` icon.
1052                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1053                             if (MainWebViewActivity.darkTheme) {
1054                                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1055                             } else {
1056                                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1057                             }
1058                         } else {
1059                             clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1060                         }
1061
1062                         // Update the `clearCachePreference` icon.
1063                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1064                             if (MainWebViewActivity.darkTheme) {
1065                                 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1066                             } else {
1067                                 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1068                             }
1069                         } else {
1070                             clearCachePreference.setIcon(R.drawable.cache_warning);
1071                         }
1072                         break;
1073
1074                     case "clear_cookies":
1075                         // Update the icon.
1076                         if (sharedPreferences.getBoolean("clear_cookies", true)) {
1077                             if (MainWebViewActivity.darkTheme) {
1078                                 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1079                             } else {
1080                                 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1081                             }
1082                         } else {
1083                             clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1084                         }
1085                         break;
1086
1087                     case "clear_dom_storage":
1088                         // Update the icon.
1089                         if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1090                             if (MainWebViewActivity.darkTheme) {
1091                                 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1092                             } else {
1093                                 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1094                             }
1095                         } else {
1096                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1097                         }
1098                         break;
1099
1100                     case "clear_form_data":
1101                         // Update the icon.
1102                         if (sharedPreferences.getBoolean("clear_form_data", true)) {
1103                             if (MainWebViewActivity.darkTheme) {
1104                                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1105                             } else {
1106                                 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1107                             }
1108                         } else {
1109                             clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1110                         }
1111                         break;
1112
1113                     case "clear_cache":
1114                         // Update the icon.
1115                         if (sharedPreferences.getBoolean("clear_cache", true)) {
1116                             if (MainWebViewActivity.darkTheme) {
1117                                 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1118                             } else {
1119                                 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1120                             }
1121                         } else {
1122                             clearCachePreference.setIcon(R.drawable.cache_warning);
1123                         }
1124                         break;
1125
1126                     case "homepage":
1127                         // Set the new homepage URL as the summary text for the Homepage preference.  The default is `https://www.duckduckgo.com`.
1128                         homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
1129                         break;
1130
1131                     case "default_font_size":
1132                         // Update the summary text of `default_font_size`.
1133                         defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1134                         break;
1135
1136                     case "swipe_to_refresh":
1137                         // Update the icon.
1138                         if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
1139                             if (MainWebViewActivity.darkTheme) {
1140                                 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1141                             } else {
1142                                 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1143                             }
1144                         } else {
1145                             if (MainWebViewActivity.darkTheme) {
1146                                 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1147                             } else {
1148                                 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1149                             }
1150                         }
1151                         break;
1152
1153                     case "display_additional_app_bar_icons":
1154                         // Update the icon.
1155                         if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1156                             if (MainWebViewActivity.darkTheme) {
1157                                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1158                             } else {
1159                                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1160                             }
1161                         } else {
1162                             if (MainWebViewActivity.darkTheme) {
1163                                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1164                             } else {
1165                                 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1166                             }
1167                         }
1168                         break;
1169
1170                     case "dark_theme":
1171                         // Update the icon.
1172                         if (sharedPreferences.getBoolean("dark_theme", false)) {
1173                             darkThemePreference.setIcon(R.drawable.theme_dark);
1174                         } else {
1175                             darkThemePreference.setIcon(R.drawable.theme_light);
1176                         }
1177
1178                         // Create an `Intent` to restart Privacy Browser.
1179                         Intent intent = getActivity().getParentActivityIntent();
1180
1181                         // Assert that `intent` is not `null` to remove the lint error below.
1182                         assert intent != null;
1183
1184                         // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack.  It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1185                         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1186
1187                         // Make it so.
1188                         startActivity(intent);
1189                         break;
1190
1191                     case "display_webpage_images":
1192                         if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1193                             // Update the icon.
1194                             if (MainWebViewActivity.darkTheme) {
1195                                 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1196                             } else {
1197                                 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1198                             }
1199
1200                             // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1201                             MainWebViewActivity.reloadOnRestartBoolean = false;
1202                         } else {
1203                             // Update the icon.
1204                             if (MainWebViewActivity.darkTheme) {
1205                                 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1206                             } else {
1207                                 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1208                             }
1209
1210                             // Set `mainWebView` to reload on restart to remove the current images.
1211                             MainWebViewActivity.reloadOnRestartBoolean = true;
1212                         }
1213                         break;
1214                 }
1215             }
1216         };
1217
1218         // Register the listener.
1219         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1220     }
1221
1222     // 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
1223     // even while running in the foreground.
1224     @Override
1225     public void onPause() {
1226         super.onPause();
1227         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1228     }
1229
1230     @Override
1231     public void onResume() {
1232         super.onResume();
1233         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1234     }
1235 }