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