2 * Copyright © 2016-2018 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_enabled");
59 final Preference firstPartyCookiesPreference = findPreference("first_party_cookies_enabled");
60 final Preference thirdPartyCookiesPreference = findPreference("third_party_cookies_enabled");
61 final Preference domStoragePreference = findPreference("dom_storage_enabled");
62 final Preference saveFormDataPreference = findPreference("save_form_data_enabled"); // 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("fanboy_annoyance_list");
71 final Preference fanboySocialBlockingListPreference = findPreference("fanboy_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 hideSystemBarsPreference = findPreference("hide_system_bars");
82 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
83 final Preference clearEverythingPreference = findPreference("clear_everything");
84 final Preference clearCookiesPreference = findPreference("clear_cookies");
85 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
86 final Preference clearFormDataPreference = findPreference("clear_form_data"); // The clear form data preference can be removed once the minimum API >= 26.
87 final Preference clearCachePreference = findPreference("clear_cache");
88 final Preference homepagePreference = findPreference("homepage");
89 final Preference defaultFontSizePreference = findPreference("default_font_size");
90 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
91 final Preference downloadWithExternalAppPreference = findPreference("download_with_external_app");
92 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
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 hideSystemBarsPreference.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_enabled", false);
108 boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false);
109 boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false);
110 boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboy_annoyance_list", true);
111 boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboy_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 hideSystemBars = savedPreferences.getBoolean("hide_system_bars", false);
115 boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
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"));
216 // Enable the translucent navigation bar preference only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
217 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingMode && !hideSystemBars);
219 // Set the status of the `Clear and Exit` preferences.
220 clearCookiesPreference.setEnabled(!clearEverything);
221 clearDomStoragePreference.setEnabled(!clearEverything);
222 clearFormDataPreference.setEnabled(!clearEverything); // The form data line can be removed once the minimum API is >= 26.
223 clearCachePreference.setEnabled(!clearEverything);
225 // Set the homepage URL as the summary text for the homepage preference.
226 homepagePreference.setSummary(savedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
228 // Set the default font size as the summary text for the preference.
229 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", getString(R.string.font_size_default_value)) + "%%");
231 // Disable the JavaScript preference if Night Mode is enabled. JavaScript will be enabled for all web pages.
232 javaScriptPreference.setEnabled(!nightMode);
234 // Set the JavaScript icon.
235 if (javaScriptEnabled || nightMode) {
236 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
238 javaScriptPreference.setIcon(R.drawable.privacy_mode);
241 // Set the first-party cookies icon.
242 if (firstPartyCookiesEnabled) {
243 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
245 if (MainWebViewActivity.darkTheme) {
246 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
248 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
252 // Set the third party cookies icon.
253 if (firstPartyCookiesEnabled && Build.VERSION.SDK_INT >= 21) {
254 if (thirdPartyCookiesEnabled) {
255 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
257 if (MainWebViewActivity.darkTheme) {
258 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
260 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
264 if (MainWebViewActivity.darkTheme) {
265 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
267 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
271 // Set the DOM storage icon.
272 if (javaScriptEnabled || nightMode) { // The preference is enabled.
273 if (savedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
274 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
275 } else { // DOM storage is disabled.
276 if (MainWebViewActivity.darkTheme) {
277 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
279 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
282 } else { // The preference is disabled. The icon should be ghosted.
283 if (MainWebViewActivity.darkTheme) {
284 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
286 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
290 // Set the save form data icon if API < 26. Save form data has no effect on API >= 26.
291 if (Build.VERSION.SDK_INT < 26) {
292 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
293 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
295 if (MainWebViewActivity.darkTheme) {
296 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
298 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
303 // Set the custom user agent icon.
304 if (customUserAgentPreference.isEnabled()) {
305 if (MainWebViewActivity.darkTheme) {
306 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
308 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
311 if (MainWebViewActivity.darkTheme) {
312 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
314 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
318 // Set the incognito mode icon.
319 if (savedPreferences.getBoolean("incognito_mode", false)) {
320 if (MainWebViewActivity.darkTheme) {
321 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
323 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
326 if (MainWebViewActivity.darkTheme) {
327 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
329 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
333 // Set the Do Not Track icon.
334 if (savedPreferences.getBoolean("do_not_track", false)) {
335 if (MainWebViewActivity.darkTheme) {
336 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
338 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
341 if (MainWebViewActivity.darkTheme) {
342 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
344 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
348 // Set the allow screenshots icon.
349 if (savedPreferences.getBoolean("allow_screenshots", false)) {
350 if (MainWebViewActivity.darkTheme) {
351 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
353 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
356 if (MainWebViewActivity.darkTheme) {
357 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
359 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
363 // Set the EasyList icon.
364 if (savedPreferences.getBoolean("easylist", true)) {
365 if (MainWebViewActivity.darkTheme) {
366 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
368 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
371 if (MainWebViewActivity.darkTheme) {
372 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
374 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
378 // Set the EasyPrivacy icon.
379 if (savedPreferences.getBoolean("easyprivacy", true)) {
380 if (MainWebViewActivity.darkTheme) {
381 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
383 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
386 if (MainWebViewActivity.darkTheme) {
387 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
389 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
393 // Set the Fanboy lists icons.
394 if (fanboyAnnoyanceListEnabled) {
395 if (MainWebViewActivity.darkTheme) {
396 // Set the Fanboy annoyance list icon.
397 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
399 // Set the Fanboy social blocking list icon.
400 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
402 // Set the Fanboy annoyance list icon.
403 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
405 // Set the Fanboy social blocking list icon.
406 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
409 if (MainWebViewActivity.darkTheme) {
410 // Set the Fanboy annoyance list icon.
411 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
413 // Set the Fanboy social blocking list icon.
414 if (fanboySocialBlockingEnabled) {
415 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
417 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
420 // Set the Fanboy annoyance list icon.
421 fanboyAnnoyanceListPreference.setIcon(R.drawable.block_ads_disabled_light);
423 // Set the Fanboy social blocking list icon.
424 if (fanboySocialBlockingEnabled) {
425 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
427 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
432 // Set the UltraPrivacy icon.
433 if (savedPreferences.getBoolean("ultraprivacy", true)) {
434 if (MainWebViewActivity.darkTheme) {
435 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
437 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
440 if (MainWebViewActivity.darkTheme) {
441 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
443 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
447 // Set the block all third-party requests icon.
448 if (savedPreferences.getBoolean("block_all_third_party_requests", false)) {
449 if (MainWebViewActivity.darkTheme) {
450 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
452 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
455 if (MainWebViewActivity.darkTheme) {
456 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
458 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
462 // Set the Tor icons according to the theme.
463 if (proxyThroughOrbot) { // Proxying is enabled.
464 if (MainWebViewActivity.darkTheme) {
465 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
466 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
467 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
469 // Set the custom search icon.
470 if (torSearchCustomURLPreference.isEnabled()) {
471 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
473 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
476 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
477 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
478 torSearchPreference.setIcon(R.drawable.search_enabled_light);
480 // Set the custom search icon.
481 if (torSearchCustomURLPreference.isEnabled()) {
482 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
484 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
487 } else { // Proxying is disabled.
488 if (MainWebViewActivity.darkTheme) {
489 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
490 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
491 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
492 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
494 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
495 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
496 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
497 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
501 // Set the search custom URL icon.
502 if (searchCustomURLPreference.isEnabled()) {
503 if (MainWebViewActivity.darkTheme) {
504 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
506 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
509 if (MainWebViewActivity.darkTheme) {
510 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
512 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
516 // Set the full screen browsing mode icons.
517 if (fullScreenBrowsingMode) { // Full screen browsing mode is enabled.
518 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
519 if (MainWebViewActivity.darkTheme) {
520 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
522 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
525 if (hideSystemBars) { // `hideSystemBarsBoolean` is `true`.
526 // Set the icons according to the theme.
527 if (MainWebViewActivity.darkTheme) {
528 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
529 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
531 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
532 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
534 } else { // `hideSystemBarsBoolean` is `false`.
535 // Set the `hideSystemBarsPreference` icon according to the theme.
536 if (MainWebViewActivity.darkTheme) {
537 // Set the icon for `hideSystemBarsPreference`.
538 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
540 // Set the icon for `translucentNavigationBarPreference`.
541 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
542 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
544 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
547 // Set the icon for `hideSystemBarsPreference`.
548 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
550 // Set the icon for `translucentNavigationBarPreference`.
551 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
552 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
554 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
558 } else { // Full screen browsing mode is disabled.
559 // Set the icons according to the theme.
560 if (MainWebViewActivity.darkTheme) {
561 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
562 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
563 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
565 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
566 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
567 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
571 // Set the clear everything preference icon.
572 if (clearEverything) {
573 if (MainWebViewActivity.darkTheme) {
574 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
576 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
579 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
582 // Set the clear cookies preference icon.
583 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
584 if (MainWebViewActivity.darkTheme) {
585 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
587 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
590 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
593 // Set the clear DOM storage preference icon.
594 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
595 if (MainWebViewActivity.darkTheme) {
596 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
598 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
601 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
604 // Set the clear form data preference icon if the API < 26. It has no effect on newer versions of Android.
605 if (Build.VERSION.SDK_INT < 26) {
606 if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
607 if (MainWebViewActivity.darkTheme) {
608 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
610 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
613 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
617 // Set the clear cache preference icon.
618 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
619 if (MainWebViewActivity.darkTheme) {
620 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
622 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
625 clearCachePreference.setIcon(R.drawable.cache_warning);
628 // Set the swipe to refresh preference icon.
629 if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
630 if (MainWebViewActivity.darkTheme) {
631 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
633 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
636 if (MainWebViewActivity.darkTheme) {
637 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
639 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
643 // Set the download with external app preference icon.
644 if (savedPreferences.getBoolean("download_with_external_app", false)) {
645 if (MainWebViewActivity.darkTheme) {
646 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
648 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
651 if (MainWebViewActivity.darkTheme) {
652 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
654 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
658 // Set the display additional app bar icons preference icon.
659 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
660 if (MainWebViewActivity.darkTheme) {
661 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
663 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
666 if (MainWebViewActivity.darkTheme) {
667 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
669 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
673 // Set the dark theme preference icon.
674 if (savedPreferences.getBoolean("dark_theme", false)) {
675 darkThemePreference.setIcon(R.drawable.theme_dark);
677 darkThemePreference.setIcon(R.drawable.theme_light);
680 // Set the night mode preference icon.
682 if (MainWebViewActivity.darkTheme) {
683 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
685 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
688 if (MainWebViewActivity.darkTheme) {
689 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
691 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
695 // Set the display webpage images preference icon.
696 if (savedPreferences.getBoolean("display_webpage_images", true)) {
697 if (MainWebViewActivity.darkTheme) {
698 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
700 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
703 if (MainWebViewActivity.darkTheme) {
704 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
706 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
711 // Listen for preference changes.
712 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
714 case "javascript_enabled":
715 // Update the icons and the DOM storage preference status.
716 if (sharedPreferences.getBoolean("javascript_enabled", false)) { // The JavaScript preference is enabled.
717 // Update the icon for the JavaScript preference.
718 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
720 // Update the status of the DOM storage preference.
721 domStoragePreference.setEnabled(true);
723 // Update the icon for the DOM storage preference.
724 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
725 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
727 if (MainWebViewActivity.darkTheme) {
728 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
730 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
733 } else { // The JavaScript preference is disabled.
734 // Update the icon for the JavaScript preference.
735 javaScriptPreference.setIcon(R.drawable.privacy_mode);
737 // Update the status of the DOM storage preference.
738 domStoragePreference.setEnabled(false);
740 // Set the icon for DOM storage preference to be ghosted.
741 if (MainWebViewActivity.darkTheme) {
742 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
744 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
749 case "first_party_cookies_enabled":
750 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
751 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
752 // Set the icon for `first_party_cookies_enabled`.
753 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
755 // Update the icon for `third_party_cookies_enabled`.
756 if (Build.VERSION.SDK_INT >= 21) {
757 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
758 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
760 if (MainWebViewActivity.darkTheme) {
761 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
763 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
767 if (MainWebViewActivity.darkTheme) {
768 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
770 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
773 } else { // `first_party_cookies_enabled` is `false`.
774 // Update the icon for `first_party_cookies_enabled`.
775 if (MainWebViewActivity.darkTheme) {
776 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
778 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
781 // Set the icon for `third_party_cookies_enabled` to be ghosted.
782 if (MainWebViewActivity.darkTheme) {
783 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
785 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
789 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
790 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
793 case "third_party_cookies_enabled":
795 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
796 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
798 if (MainWebViewActivity.darkTheme) {
799 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
801 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
806 case "dom_storage_enabled":
808 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
809 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
811 if (MainWebViewActivity.darkTheme) {
812 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
814 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
819 // Save form data can be removed once the minimum API >= 26.
820 case "save_form_data_enabled":
822 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
823 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
825 if (MainWebViewActivity.darkTheme) {
826 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
828 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
833 // Get the new user agent name.
834 String newUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
836 // Get the array position for the new user agent name.
837 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
839 // Get the translated new user agent name.
840 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
842 // Populate the user agent summary.
843 switch (newUserAgentArrayPosition) {
844 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
845 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
846 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
848 // Disable the custom user agent preference.
849 customUserAgentPreference.setEnabled(false);
851 // Set the custom user agent preference icon according to the theme.
852 if (MainWebViewActivity.darkTheme) {
853 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
855 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
859 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
860 // Set the summary text.
861 userAgentPreference.setSummary(R.string.custom_user_agent);
863 // Enable the custom user agent preference.
864 customUserAgentPreference.setEnabled(true);
866 // Set the custom user agent preference icon according to the theme.
867 if (MainWebViewActivity.darkTheme) {
868 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
870 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
875 // Get the user agent summary from the user agent data array.
876 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
878 // Disable the custom user agent preference.
879 customUserAgentPreference.setEnabled(false);
881 // Set the custom user agent preference icon according to the theme.
882 if (MainWebViewActivity.darkTheme) {
883 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
885 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
890 case "custom_user_agent":
891 // Set the new custom user agent as the summary text for the preference.
892 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
895 case "incognito_mode":
897 if (sharedPreferences.getBoolean("incognito_mode", false)) {
898 if (MainWebViewActivity.darkTheme) {
899 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
901 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
904 if (MainWebViewActivity.darkTheme) {
905 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
907 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
914 if (sharedPreferences.getBoolean("do_not_track", false)) {
915 if (MainWebViewActivity.darkTheme) {
916 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
918 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
921 if (MainWebViewActivity.darkTheme) {
922 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
924 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
930 case "allow_screenshots":
932 if (sharedPreferences.getBoolean("allow_screenshots", false)) {
933 if (MainWebViewActivity.darkTheme) {
934 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
936 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
939 if (MainWebViewActivity.darkTheme) {
940 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
942 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
946 // Create an intent to restart Privacy Browser.
947 Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
949 // Assert that the intent is not null to remove the lint error below.
950 assert allowScreenshotsRestartIntent != null;
952 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
953 allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
956 startActivity(allowScreenshotsRestartIntent);
961 if (sharedPreferences.getBoolean("easylist", true)) {
962 if (MainWebViewActivity.darkTheme) {
963 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
965 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
968 if (MainWebViewActivity.darkTheme) {
969 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
971 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
978 if (sharedPreferences.getBoolean("easyprivacy", true)) {
979 if (MainWebViewActivity.darkTheme) {
980 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
982 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
985 if (MainWebViewActivity.darkTheme) {
986 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
988 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
993 case "fanboy_annoyance_list":
994 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboy_annoyance_list", true);
995 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboy_social_blocking_list", true);
997 // Update the Fanboy icons.
998 if (currentFanboyAnnoyanceList) { // Fanboy's annoyance list is enabled.
999 if (MainWebViewActivity.darkTheme) {
1000 // Update the Fanboy's annoyance list icon.
1001 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
1003 // Update the Fanboy's social blocking list icon.
1004 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
1006 // Update the Fanboy's annoyance list icon.
1007 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
1009 // Update the Fanboy's social blocking list icon.
1010 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
1012 } else { // Fanboy's annoyance list is disabled.
1013 if (MainWebViewActivity.darkTheme) {
1014 // Update the Fanboy's annoyance list icon.
1015 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
1017 // Update the Fanboy's social blocking list icon.
1018 if (currentFanboySocialBlockingList) {
1019 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1021 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1024 // Update the Fanboy's annoyance list icon.
1025 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
1027 // Update the Fanboy's social blocking list icon.
1028 if (currentFanboySocialBlockingList) {
1029 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1031 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1036 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
1037 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
1040 case "fanboy_social_blocking_list":
1042 if (sharedPreferences.getBoolean("fanboy_social_blocking_list", true)) {
1043 if (MainWebViewActivity.darkTheme) {
1044 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1046 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1049 if (MainWebViewActivity.darkTheme) {
1050 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1052 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1057 case "ultraprivacy":
1059 if (sharedPreferences.getBoolean("ultraprivacy", true)) {
1060 if (MainWebViewActivity.darkTheme) {
1061 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
1063 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
1066 if (MainWebViewActivity.darkTheme) {
1067 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
1069 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
1074 case "block_all_third_party_requests":
1076 if (sharedPreferences.getBoolean("block_all_third_party_requests", false)) {
1077 if (MainWebViewActivity.darkTheme) {
1078 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
1080 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
1083 if (MainWebViewActivity.darkTheme) {
1084 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
1086 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
1091 case "proxy_through_orbot":
1092 // Get current settings.
1093 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
1094 String currentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1096 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
1097 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
1099 // Update the icons.
1100 if (currentProxyThroughOrbot) {
1101 // Set the Tor icons according to the theme.
1102 if (MainWebViewActivity.darkTheme) {
1103 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1104 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1105 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1107 // Set the `torSearchCustomURLPreference` icon.
1108 if (torSearchCustomURLPreference.isEnabled()) {
1109 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1111 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1114 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1115 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1116 torSearchPreference.setIcon(R.drawable.search_enabled_light);
1118 // Set the `torSearchCustomURLPreference` icon.
1119 if (torSearchCustomURLPreference.isEnabled()) {
1120 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1122 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1125 } else { // Proxy through Orbot is disabled.
1126 if (MainWebViewActivity.darkTheme) {
1127 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1128 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1129 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1130 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1132 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1133 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1134 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1135 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1140 case "tor_homepage":
1141 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is Searx: `http://ulrn6sryqaifefld.onion/`.
1142 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
1146 // Get the present search string.
1147 String presentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1149 // Update the preferences.
1150 if (presentTorSearchString.equals("Custom URL")) {
1151 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1152 torSearchPreference.setSummary(R.string.custom_url);
1154 // Enable `torSearchCustomURLPreference`.
1155 torSearchCustomURLPreference.setEnabled(true);
1157 // Update the `torSearchCustomURLPreference` icon.
1158 if (MainWebViewActivity.darkTheme) {
1159 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1161 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1164 // Set the array value as the summary text.
1165 torSearchPreference.setSummary(presentTorSearchString);
1167 // Disable `torSearchCustomURLPreference`.
1168 torSearchCustomURLPreference.setEnabled(false);
1170 // Update the `torSearchCustomURLPreference` icon.
1171 if (MainWebViewActivity.darkTheme) {
1172 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1174 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1179 case "tor_search_custom_url":
1180 // Set the summary text for `tor_search_custom_url`.
1181 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", getString(R.string.tor_search_custom_url_default_value)));
1185 // Store the new search string.
1186 String newSearchString = sharedPreferences.getString("search", getString(R.string.search_default_value));
1188 // Update `searchPreference` and `searchCustomURLPreference`.
1189 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1190 // Set the summary text to `R.string.custom_url`, which is translated.
1191 searchPreference.setSummary(R.string.custom_url);
1193 // Enable `searchCustomURLPreference`.
1194 searchCustomURLPreference.setEnabled(true);
1196 // Set the `searchCustomURLPreference` according to the theme.
1197 if (MainWebViewActivity.darkTheme) {
1198 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1200 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1202 } else { // `Custom URL` is not selected.
1203 // Set the summary text to `newSearchString`.
1204 searchPreference.setSummary(newSearchString);
1206 // Disable `searchCustomURLPreference`.
1207 searchCustomURLPreference.setEnabled(false);
1209 // Set the `searchCustomURLPreference` according to the theme.
1210 if (MainWebViewActivity.darkTheme) {
1211 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1213 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1218 case "search_custom_url":
1219 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1220 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value)));
1223 case "full_screen_browsing_mode":
1224 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
1225 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
1226 if (MainWebViewActivity.darkTheme) {
1227 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1229 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1232 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
1233 // Disable `translucentNavigationBarPreference`.
1234 translucentNavigationBarPreference.setEnabled(false);
1236 // Set the icons according to the theme.
1237 if (MainWebViewActivity.darkTheme) {
1238 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1239 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1241 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1242 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1244 } else { // `hide_system_bars` is `false`.
1245 // Enable `translucentNavigationBarPreference`.
1246 translucentNavigationBarPreference.setEnabled(true);
1248 // Set the icons according to the theme.
1249 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
1250 // Set the `hideSystemBarsPreference` icon.
1251 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1253 // Set the `translucentNavigationBarPreference` icon.
1254 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1255 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1257 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1259 } else { // Use the light theme.
1260 // Set the `hideSystemBarsPreference` icon.
1261 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1263 // Set the `translucentNavigationBarPreference` icon.
1264 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1265 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1267 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1271 } else { // `full_screen_browsing_mode` is false.
1272 // Disable `translucentNavigationBarPreference`.
1273 translucentNavigationBarPreference.setEnabled(false);
1275 // Update the icons according to the theme.
1276 if (MainWebViewActivity.darkTheme) {
1277 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1278 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
1279 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1281 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1282 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
1283 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1288 case "hide_system_bars":
1289 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
1290 // Disable `translucentNavigationBarPreference`.
1291 translucentNavigationBarPreference.setEnabled(false);
1293 // Set the icons according to the theme.
1294 if (MainWebViewActivity.darkTheme) {
1295 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1296 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1298 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1299 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1301 } else { // `hide_system_bars` is false.
1302 // Enable `translucentNavigationBarPreference`.
1303 translucentNavigationBarPreference.setEnabled(true);
1305 // Set the icons according to the theme.
1306 if (MainWebViewActivity.darkTheme) {
1307 // Set the `hideSystemBarsPreference` icon.
1308 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1310 // Set the `translucentNavigationBarPreference` icon.
1311 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1312 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1314 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1317 // Set the `hideSystemBarsPreference` icon.
1318 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1320 // Set the `translucentNavigationBarPreference` icon.
1321 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1322 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1324 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1330 case "translucent_navigation_bar":
1332 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1333 if (MainWebViewActivity.darkTheme) {
1334 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1336 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1339 if (MainWebViewActivity.darkTheme) {
1340 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1342 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1347 case "clear_everything":
1348 // Store the new `clear_everything` status
1349 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1351 // Update the status of the `Clear and Exit` preferences.
1352 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1353 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1354 clearFormDataPreference.setEnabled(!newClearEverythingBoolean); // This line can be removed once the minimum API >= 26.
1355 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1357 // Update the `clearEverythingPreference` icon.
1358 if (newClearEverythingBoolean) {
1359 if (MainWebViewActivity.darkTheme) {
1360 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1362 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1365 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1368 // Update the `clearCookiesPreference` icon.
1369 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1370 if (MainWebViewActivity.darkTheme) {
1371 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1373 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1376 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1379 // Update the `clearDomStoragePreference` icon.
1380 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1381 if (MainWebViewActivity.darkTheme) {
1382 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1384 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1387 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1390 // Update the clear form data preference icon if the API < 26.
1391 if (Build.VERSION.SDK_INT < 26) {
1392 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1393 if (MainWebViewActivity.darkTheme) {
1394 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1396 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1399 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1403 // Update the `clearCachePreference` icon.
1404 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1405 if (MainWebViewActivity.darkTheme) {
1406 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1408 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1411 clearCachePreference.setIcon(R.drawable.cache_warning);
1415 case "clear_cookies":
1417 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1418 if (MainWebViewActivity.darkTheme) {
1419 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1421 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1424 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1428 case "clear_dom_storage":
1430 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1431 if (MainWebViewActivity.darkTheme) {
1432 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1434 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1437 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1441 // This section can be removed once the minimum API >= 26.
1442 case "clear_form_data":
1444 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1445 if (MainWebViewActivity.darkTheme) {
1446 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1448 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1451 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1457 if (sharedPreferences.getBoolean("clear_cache", true)) {
1458 if (MainWebViewActivity.darkTheme) {
1459 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1461 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1464 clearCachePreference.setIcon(R.drawable.cache_warning);
1469 // Set the new homepage URL as the summary text for the Homepage preference.
1470 homepagePreference.setSummary(sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
1473 case "default_font_size":
1474 // Update the summary text of `default_font_size`.
1475 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", getString(R.string.font_size_default_value)) + "%%");
1478 case "swipe_to_refresh":
1480 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1481 if (MainWebViewActivity.darkTheme) {
1482 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1484 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1487 if (MainWebViewActivity.darkTheme) {
1488 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1490 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1495 case "download_with_external_app":
1497 if (sharedPreferences.getBoolean("download_with_external_app", false)) {
1498 if (MainWebViewActivity.darkTheme) {
1499 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
1501 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
1504 if (MainWebViewActivity.darkTheme) {
1505 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
1507 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
1512 case "display_additional_app_bar_icons":
1514 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1515 if (MainWebViewActivity.darkTheme) {
1516 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1518 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1521 if (MainWebViewActivity.darkTheme) {
1522 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1524 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1531 if (sharedPreferences.getBoolean("dark_theme", false)) {
1532 darkThemePreference.setIcon(R.drawable.theme_dark);
1534 darkThemePreference.setIcon(R.drawable.theme_light);
1537 // Create an intent to restart Privacy Browser.
1538 Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1540 // Assert that the intent is not null to remove the lint error below.
1541 assert changeThemeRestartIntent != null;
1543 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1544 changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1547 startActivity(changeThemeRestartIntent);
1551 // Set the URL to be reloaded on restart to apply the new night mode setting.
1552 MainWebViewActivity.loadUrlOnRestart = true;
1554 // Store the current night mode status.
1555 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1556 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript_enabled", false);
1559 if (currentNightModeBoolean) {
1560 if (MainWebViewActivity.darkTheme) {
1561 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1563 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1566 if (MainWebViewActivity.darkTheme) {
1567 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1569 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1573 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1574 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1575 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1577 // Update the `javaScriptPreference` icon.
1578 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1579 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1581 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1584 // Update the `domStoragePreference` icon.
1585 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1586 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
1587 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1588 } else { // DOM storage is disabled.
1589 if (MainWebViewActivity.darkTheme) {
1590 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1592 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1595 } else { // The preference is disabled. The icon should be ghosted.
1596 if (MainWebViewActivity.darkTheme) {
1597 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1599 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1604 case "display_webpage_images":
1605 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1607 if (MainWebViewActivity.darkTheme) {
1608 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1610 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1613 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1614 MainWebViewActivity.reloadOnRestart = false;
1617 if (MainWebViewActivity.darkTheme) {
1618 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1620 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1623 // Set `mainWebView` to reload on restart to remove the current images.
1624 MainWebViewActivity.reloadOnRestart = true;
1630 // Register the listener.
1631 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1634 // 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
1635 // even while running in the foreground.
1637 public void onPause() {
1639 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1643 public void onResume() {
1645 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);