2 * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.fragments;
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;
36 import com.stoutner.privacybrowser.R;
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 public class SettingsFragment extends PreferenceFragment {
40 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
41 private SharedPreferences savedPreferences;
44 public void onCreate(Bundle savedInstanceState) {
45 // Run the default commands.
46 super.onCreate(savedInstanceState);
48 // Load the preferences from the XML file.
49 addPreferencesFromResource(R.xml.preferences);
51 // Get a handle for the context.
52 Context context = getActivity().getApplicationContext();
54 // Initialize savedPreferences.
55 savedPreferences = getPreferenceScreen().getSharedPreferences();
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");
98 torHomepagePreference.setDependency("proxy_through_orbot");
99 torSearchPreference.setDependency("proxy_through_orbot");
100 hideAppBarPreference.setDependency("full_screen_browsing_mode");
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));
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);
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));
121 // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
122 domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
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");
130 // Remove the form data preferences.
131 privacyCategory.removePreference(saveFormDataPreference);
132 clearAndExitCategory.removePreference(clearFormDataPreference);
135 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
136 fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
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);
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);
149 // Get the current user agent name from the preference.
150 String userAgentName = savedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
152 // Get the array position of the user agent name.
153 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
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);
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());
167 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
168 // Set the summary text.
169 userAgentPreference.setSummary(R.string.custom_user_agent);
173 // Get the user agent summary from the user agent data array.
174 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + userAgentDataArray[userAgentArrayPosition]);
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)));
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)));
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);
191 // Set the array value as the summary text.
192 torSearchPreference.setSummary(torSearchString);
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)));
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"));
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);
207 // Set the array value as the summary text.
208 searchPreference.setSummary(searchString);
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"));
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);
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)));
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)) + "%%");
227 // Disable the JavaScript preference if Night Mode is enabled. JavaScript will be enabled for all web pages.
228 javaScriptPreference.setEnabled(!nightMode);
230 // Set the JavaScript icon.
231 if (javaScriptEnabled || nightMode) {
232 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
234 javaScriptPreference.setIcon(R.drawable.privacy_mode);
237 // Set the first-party cookies icon.
238 if (firstPartyCookiesEnabled) {
239 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
242 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
244 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
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);
254 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
256 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
261 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
263 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
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.
273 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
275 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
278 } else { // The preference is disabled. The icon should be ghosted.
280 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
282 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
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);
292 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
294 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
299 // Set the custom user agent icon.
300 if (customUserAgentPreference.isEnabled()) {
302 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
304 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
308 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
310 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
314 // Set the incognito mode icon.
315 if (savedPreferences.getBoolean("incognito_mode", false)) {
317 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
319 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
323 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
325 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
329 // Set the Do Not Track icon.
330 if (savedPreferences.getBoolean("do_not_track", false)) {
332 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
334 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
338 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
340 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
344 // Set the allow screenshots icon.
345 if (savedPreferences.getBoolean("allow_screenshots", false)) {
347 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
349 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
353 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
355 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
359 // Set the EasyList icon.
360 if (savedPreferences.getBoolean("easylist", true)) {
362 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
364 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
368 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
370 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
374 // Set the EasyPrivacy icon.
375 if (savedPreferences.getBoolean("easyprivacy", true)) {
377 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
379 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
383 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
385 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
389 // Set the Fanboy lists icons.
390 if (fanboyAnnoyanceListEnabled) {
392 // Set the Fanboy annoyance list icon.
393 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
395 // Set the Fanboy social blocking list icon.
396 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
398 // Set the Fanboy annoyance list icon.
399 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
401 // Set the Fanboy social blocking list icon.
402 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
406 // Set the Fanboy annoyance list icon.
407 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
409 // Set the Fanboy social blocking list icon.
410 if (fanboySocialBlockingEnabled) {
411 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
413 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
416 // Set the Fanboy annoyance list icon.
417 fanboyAnnoyanceListPreference.setIcon(R.drawable.block_ads_disabled_light);
419 // Set the Fanboy social blocking list icon.
420 if (fanboySocialBlockingEnabled) {
421 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
423 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
428 // Set the UltraPrivacy icon.
429 if (savedPreferences.getBoolean("ultraprivacy", true)) {
431 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
433 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
437 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
439 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
443 // Set the block all third-party requests icon.
444 if (savedPreferences.getBoolean("block_all_third_party_requests", false)) {
446 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
448 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
452 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
454 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
458 // Set the Tor icons according to the theme.
459 if (proxyThroughOrbot) { // Proxying is enabled.
461 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
462 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
463 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
465 // Set the custom search icon.
466 if (torSearchCustomURLPreference.isEnabled()) {
467 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
469 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
472 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
473 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
474 torSearchPreference.setIcon(R.drawable.search_enabled_light);
476 // Set the custom search icon.
477 if (torSearchCustomURLPreference.isEnabled()) {
478 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
480 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
483 } else { // Proxying is disabled.
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);
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);
497 // Set the search custom URL icon.
498 if (searchCustomURLPreference.isEnabled()) {
500 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
502 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
506 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
508 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
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.
516 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
518 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
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.
525 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
527 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
529 } else { // Hide app bar is disabled.
530 // Set the icon according to the theme.
532 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
534 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
537 } else { // Full screen browsing mode is disabled.
538 // Set the icons according to the theme.
540 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
541 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_dark);
543 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
544 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_light);
548 // Set the clear everything preference icon.
549 if (clearEverything) {
551 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
553 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
556 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
559 // Set the clear cookies preference icon.
560 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
562 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
564 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
567 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
570 // Set the clear DOM storage preference icon.
571 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
573 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
575 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
578 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
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)) {
585 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
587 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
590 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
594 // Set the clear cache preference icon.
595 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
597 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
599 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
602 clearCachePreference.setIcon(R.drawable.cache_warning);
605 // Set the swipe to refresh preference icon.
606 if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
608 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
610 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
614 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
616 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
620 // Set the scroll app bar preference icon.
621 if (savedPreferences.getBoolean("scroll_app_bar", true)) {
623 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
625 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
629 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
631 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
635 // Set the display additional app bar icons preference icon.
636 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
638 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
640 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
644 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
646 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
650 // Set the download with external app preference icon.
651 if (savedPreferences.getBoolean("download_with_external_app", false)) {
653 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
655 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
659 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
661 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
665 // Set the dark theme preference icon.
666 if (savedPreferences.getBoolean("dark_theme", false)) {
667 darkThemePreference.setIcon(R.drawable.theme_dark);
669 darkThemePreference.setIcon(R.drawable.theme_light);
672 // Set the night mode preference icon.
675 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
677 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
681 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
683 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
687 // Set the display webpage images preference icon.
688 if (savedPreferences.getBoolean("display_webpage_images", true)) {
690 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
692 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
696 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
698 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
703 // Listen for preference changes.
704 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
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);
712 // Update the status of the DOM storage preference.
713 domStoragePreference.setEnabled(true);
715 // Update the icon for the DOM storage preference.
716 if (sharedPreferences.getBoolean("dom_storage", false)) {
717 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
720 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
722 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
725 } else { // The JavaScript preference is disabled.
726 // Update the icon for the JavaScript preference.
727 javaScriptPreference.setIcon(R.drawable.privacy_mode);
729 // Update the status of the DOM storage preference.
730 domStoragePreference.setEnabled(false);
732 // Set the icon for DOM storage preference to be ghosted.
734 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
736 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
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);
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);
753 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
755 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
760 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
762 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
765 } else { // `first_party_cookies` is `false`.
766 // Update the icon for `first_party_cookies`.
768 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
770 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
773 // Set the icon for `third_party_cookies` to be ghosted.
775 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
777 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
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));
785 case "third_party_cookies":
787 if (sharedPreferences.getBoolean("third_party_cookies", false)) {
788 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
791 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
793 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
800 if (sharedPreferences.getBoolean("dom_storage", false)) {
801 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
804 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
806 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
811 // Save form data can be removed once the minimum API >= 26.
812 case "save_form_data":
814 if (sharedPreferences.getBoolean("save_form_data", false)) {
815 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
818 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
820 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
826 // Get the new user agent name.
827 String newUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
829 // Get the array position for the new user agent name.
830 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
832 // Get the translated new user agent name.
833 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
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());
841 // Disable the custom user agent preference.
842 customUserAgentPreference.setEnabled(false);
844 // Set the custom user agent preference icon according to the theme.
846 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
848 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
852 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
853 // Set the summary text.
854 userAgentPreference.setSummary(R.string.custom_user_agent);
856 // Enable the custom user agent preference.
857 customUserAgentPreference.setEnabled(true);
859 // Set the custom user agent preference icon according to the theme.
861 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
863 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
868 // Get the user agent summary from the user agent data array.
869 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
871 // Disable the custom user agent preference.
872 customUserAgentPreference.setEnabled(false);
874 // Set the custom user agent preference icon according to the theme.
876 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
878 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
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)));
888 case "incognito_mode":
890 if (sharedPreferences.getBoolean("incognito_mode", false)) {
892 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
894 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
898 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
900 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
907 if (sharedPreferences.getBoolean("do_not_track", false)) {
909 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
911 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
915 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
917 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
923 case "allow_screenshots":
925 if (sharedPreferences.getBoolean("allow_screenshots", false)) {
927 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
929 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
933 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
935 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
939 // Create an intent to restart Privacy Browser.
940 Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
942 // Assert that the intent is not null to remove the lint error below.
943 assert allowScreenshotsRestartIntent != null;
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);
949 startActivity(allowScreenshotsRestartIntent);
954 if (sharedPreferences.getBoolean("easylist", true)) {
956 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
958 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
962 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
964 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
971 if (sharedPreferences.getBoolean("easyprivacy", true)) {
973 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
975 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
979 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
981 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
986 case "fanboys_annoyance_list":
987 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
988 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
990 // Update the Fanboy icons.
991 if (currentFanboyAnnoyanceList) { // Fanboy's annoyance list is enabled.
993 // Update the Fanboy's annoyance list icon.
994 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
996 // Update the Fanboy's social blocking list icon.
997 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
999 // Update the Fanboy's annoyance list icon.
1000 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
1002 // Update the Fanboy's social blocking list icon.
1003 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
1005 } else { // Fanboy's annoyance list is disabled.
1007 // Update the Fanboy's annoyance list icon.
1008 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
1010 // Update the Fanboy's social blocking list icon.
1011 if (currentFanboySocialBlockingList) {
1012 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1014 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1017 // Update the Fanboy's annoyance list icon.
1018 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
1020 // Update the Fanboy's social blocking list icon.
1021 if (currentFanboySocialBlockingList) {
1022 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1024 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1029 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
1030 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
1033 case "fanboys_social_blocking_list":
1035 if (sharedPreferences.getBoolean("fanboys_social_blocking_list", true)) {
1037 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1039 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1043 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1045 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1050 case "ultraprivacy":
1052 if (sharedPreferences.getBoolean("ultraprivacy", true)) {
1054 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
1056 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
1060 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
1062 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
1067 case "block_all_third_party_requests":
1069 if (sharedPreferences.getBoolean("block_all_third_party_requests", false)) {
1071 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
1073 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
1077 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
1079 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
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));
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"));
1092 // Update the icons.
1093 if (currentProxyThroughOrbot) {
1094 // Set the Tor icons according to the theme.
1096 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1097 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1098 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1100 // Set the `torSearchCustomURLPreference` icon.
1101 if (torSearchCustomURLPreference.isEnabled()) {
1102 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1104 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1107 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1108 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1109 torSearchPreference.setIcon(R.drawable.search_enabled_light);
1111 // Set the `torSearchCustomURLPreference` icon.
1112 if (torSearchCustomURLPreference.isEnabled()) {
1113 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1115 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1118 } else { // Proxy through Orbot is disabled.
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);
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);
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)));
1139 // Get the present search string.
1140 String presentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
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);
1147 // Enable `torSearchCustomURLPreference`.
1148 torSearchCustomURLPreference.setEnabled(true);
1150 // Update the `torSearchCustomURLPreference` icon.
1152 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1154 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1157 // Set the array value as the summary text.
1158 torSearchPreference.setSummary(presentTorSearchString);
1160 // Disable `torSearchCustomURLPreference`.
1161 torSearchCustomURLPreference.setEnabled(false);
1163 // Update the `torSearchCustomURLPreference` icon.
1165 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1167 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
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)));
1178 // Store the new search string.
1179 String newSearchString = sharedPreferences.getString("search", getString(R.string.search_default_value));
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);
1186 // Enable `searchCustomURLPreference`.
1187 searchCustomURLPreference.setEnabled(true);
1189 // Set the `searchCustomURLPreference` according to the theme.
1191 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1193 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1195 } else { // `Custom URL` is not selected.
1196 // Set the summary text to `newSearchString`.
1197 searchPreference.setSummary(newSearchString);
1199 // Disable `searchCustomURLPreference`.
1200 searchCustomURLPreference.setEnabled(false);
1202 // Set the `searchCustomURLPreference` according to the theme.
1204 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1206 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
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)));
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.
1220 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1222 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
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.
1229 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1231 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1233 } else { // Hide app bar is disabled.
1234 // Set the icon according to the theme.
1236 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1238 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1241 } else { // Full screen browsing is disabled.
1242 // Update the icons according to the theme.
1244 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1245 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_dark);
1247 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1248 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_light);
1253 case "hide_app_bar":
1255 if (sharedPreferences.getBoolean("hide_app_bar", true)) { // Hide app bar is enabled.
1256 // Set the icon according to the theme.
1258 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1260 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1262 } else { // Hide app bar is disabled.
1263 // Set the icon according to the theme.
1265 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1267 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1272 case "clear_everything":
1273 // Store the new `clear_everything` status
1274 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
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);
1282 // Update the `clearEverythingPreference` icon.
1283 if (newClearEverythingBoolean) {
1285 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1287 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1290 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1293 // Update the `clearCookiesPreference` icon.
1294 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1296 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1298 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1301 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1304 // Update the `clearDomStoragePreference` icon.
1305 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1307 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1309 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1312 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
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)) {
1319 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1321 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1324 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1328 // Update the `clearCachePreference` icon.
1329 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1331 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1333 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1336 clearCachePreference.setIcon(R.drawable.cache_warning);
1340 case "clear_cookies":
1342 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1344 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1346 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1349 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1353 case "clear_dom_storage":
1355 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1357 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1359 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1362 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1366 // This section can be removed once the minimum API >= 26.
1367 case "clear_form_data":
1369 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1371 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1373 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1376 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1382 if (sharedPreferences.getBoolean("clear_cache", true)) {
1384 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1386 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1389 clearCachePreference.setIcon(R.drawable.cache_warning);
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)));
1399 // Update the font size summary text.
1400 fontSizePreference.setSummary(sharedPreferences.getString("font_size", getString(R.string.font_size_default_value)) + "%%");
1403 case "swipe_to_refresh":
1405 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1407 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1409 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1413 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1415 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1420 case "scroll_app_bar":
1422 if (sharedPreferences.getBoolean("scroll_app_bar", true)) {
1424 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1426 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1430 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1432 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1437 case "display_additional_app_bar_icons":
1439 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1441 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1443 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1447 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1449 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1454 case "download_with_external_app":
1456 if (sharedPreferences.getBoolean("download_with_external_app", false)) {
1458 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
1460 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
1464 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
1466 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
1473 if (sharedPreferences.getBoolean("dark_theme", false)) {
1474 darkThemePreference.setIcon(R.drawable.theme_dark);
1476 darkThemePreference.setIcon(R.drawable.theme_light);
1479 // Create an intent to restart Privacy Browser.
1480 Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1482 // Assert that the intent is not null to remove the lint error below.
1483 assert changeThemeRestartIntent != null;
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);
1489 startActivity(changeThemeRestartIntent);
1493 // Set the URL to be reloaded on restart to apply the new night mode setting.
1494 MainWebViewActivity.reloadOnRestart = true;
1496 // Store the current night mode status.
1497 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1498 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript", false);
1501 if (currentNightModeBoolean) {
1503 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1505 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1509 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1511 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1515 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1516 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1517 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1519 // Update the `javaScriptPreference` icon.
1520 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1521 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1523 javaScriptPreference.setIcon(R.drawable.privacy_mode);
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.
1532 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1534 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1537 } else { // The preference is disabled. The icon should be ghosted.
1539 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1541 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1546 case "display_webpage_images":
1547 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1550 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1552 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1555 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1556 MainWebViewActivity.reloadOnRestart = false;
1560 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1562 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1565 // Set `mainWebView` to reload on restart to remove the current images.
1566 MainWebViewActivity.reloadOnRestart = true;
1572 // Register the listener.
1573 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
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.
1578 public void onPause() {
1580 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1584 public void onResume() {
1586 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);