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