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