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 super.onCreate(savedInstanceState);
46 addPreferencesFromResource(R.xml.preferences);
48 // Get a handle for the context.
49 Context context = getActivity().getApplicationContext();
51 // Initialize savedPreferences.
52 savedPreferences = getPreferenceScreen().getSharedPreferences();
54 // Get handles for the preferences.
55 final Preference javaScriptPreference = findPreference("javascript_enabled");
56 final Preference firstPartyCookiesPreference = findPreference("first_party_cookies_enabled");
57 final Preference thirdPartyCookiesPreference = findPreference("third_party_cookies_enabled");
58 final Preference domStoragePreference = findPreference("dom_storage_enabled");
59 final Preference saveFormDataPreference = findPreference("save_form_data_enabled"); // The form data preference can be removed once the minimum API >= 26.
60 final Preference userAgentPreference = findPreference("user_agent");
61 final Preference customUserAgentPreference = findPreference("custom_user_agent");
62 final Preference incognitoModePreference = findPreference("incognito_mode");
63 final Preference doNotTrackPreference = findPreference("do_not_track");
64 final Preference allowScreenshotsPreference = findPreference("allow_screenshots");
65 final Preference easyListPreference = findPreference("easylist");
66 final Preference easyPrivacyPreference = findPreference("easyprivacy");
67 final Preference fanboyAnnoyanceListPreference = findPreference("fanboy_annoyance_list");
68 final Preference fanboySocialBlockingListPreference = findPreference("fanboy_social_blocking_list");
69 final Preference ultraPrivacyPreference = findPreference("ultraprivacy");
70 final Preference blockAllThirdPartyRequestsPreference = findPreference("block_all_third_party_requests");
71 final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
72 final Preference torHomepagePreference = findPreference("tor_homepage");
73 final Preference torSearchPreference = findPreference("tor_search");
74 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
75 final Preference searchPreference = findPreference("search");
76 final Preference searchCustomURLPreference = findPreference("search_custom_url");
77 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
78 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
79 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
80 final Preference clearEverythingPreference = findPreference("clear_everything");
81 final Preference clearCookiesPreference = findPreference("clear_cookies");
82 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
83 final Preference clearFormDataPreference = findPreference("clear_form_data"); // The clear form data preference can be removed once the minimum API >= 26.
84 final Preference clearCachePreference = findPreference("clear_cache");
85 final Preference homepagePreference = findPreference("homepage");
86 final Preference defaultFontSizePreference = findPreference("default_font_size");
87 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
88 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
89 final Preference darkThemePreference = findPreference("dark_theme");
90 final Preference nightModePreference = findPreference("night_mode");
91 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
94 torHomepagePreference.setDependency("proxy_through_orbot");
95 torSearchPreference.setDependency("proxy_through_orbot");
96 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
98 // Get Strings from the preferences.
99 String torSearchString = savedPreferences.getString("tor_search", "http://ulrn6sryqaifefld.onion/?q=");
100 String searchString = savedPreferences.getString("search", "https://searx.me/?q=");
102 // Get booleans that are used in multiple places from the preferences.
103 final boolean javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
104 boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false);
105 boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false);
106 boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboy_annoyance_list", true);
107 boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboy_social_blocking_list", true);
108 boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
109 boolean fullScreenBrowsingMode = savedPreferences.getBoolean("full_screen_browsing_mode", false);
110 boolean hideSystemBars = savedPreferences.getBoolean("hide_system_bars", false);
111 boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
112 final boolean nightMode = savedPreferences.getBoolean("night_mode", false);
114 // Only enable the third-party cookies preference if first-party cookies are enabled and API >= 21.
115 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabled && (Build.VERSION.SDK_INT >= 21));
117 // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
118 domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
120 // Remove the form data preferences if the API is >= 26 as they no longer do anything.
121 if (Build.VERSION.SDK_INT >= 26) {
122 // Get the categories.
123 PreferenceCategory privacyCategory = (PreferenceCategory) findPreference("privacy");
124 PreferenceCategory clearAndExitCategory = (PreferenceCategory) findPreference("clear_and_exit");
126 // Remove the form data preferences.
127 privacyCategory.removePreference(saveFormDataPreference);
128 clearAndExitCategory.removePreference(clearFormDataPreference);
131 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
132 fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
134 // Inflate a WebView to get the default user agent.
135 LayoutInflater inflater = getActivity().getLayoutInflater();
136 // `@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.
137 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
138 final WebView bareWebView = bareWebViewLayout.findViewById(R.id.bare_webview);
140 // Get the user agent arrays.
141 ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(context, R.array.user_agent_names, R.layout.domain_settings_spinner_item);
142 String[] translatedUserAgentNamesArray = getResources().getStringArray(R.array.translated_user_agent_names);
143 String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
145 // Get the current user agent name from the preference.
146 String userAgentName = savedPreferences.getString("user_agent", "Privacy Browser");
148 // Get the array position of the user agent name.
149 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
151 // Populate the user agent summary.
152 switch (userAgentArrayPosition) {
153 case MainWebViewActivity.UNRECOGNIZED_USER_AGENT: // The user agent name is not on the canonical list.
154 // 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.
155 userAgentPreference.setSummary(userAgentName);
158 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
159 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
160 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + bareWebView.getSettings().getUserAgentString());
163 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
164 // Set the summary text.
165 userAgentPreference.setSummary(R.string.custom_user_agent);
169 // Get the user agent summary from the user agent data array.
170 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + userAgentDataArray[userAgentArrayPosition]);
173 // Set the summary text for the custom user agent preference and enable it if user agent preference is set to custom.
174 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
175 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals(getString(R.string.custom_user_agent)));
178 // 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/`.
179 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "http://ulrn6sryqaifefld.onion/"));
182 // Set the Tor search URL as the summary text for the Tor preference when the preference screen is loaded.
183 if (torSearchString.equals("Custom URL")) {
184 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
185 torSearchPreference.setSummary(R.string.custom_url);
187 // Set the array value as the summary text.
188 torSearchPreference.setSummary(torSearchString);
191 // Set the summary text for `tor_search_custom_url`. The default is `""`.
192 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
194 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
195 torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
198 // Set the search URL as the summary text for the search preference when the preference screen is loaded.
199 if (searchString.equals("Custom URL")) {
200 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
201 searchPreference.setSummary(R.string.custom_url);
203 // Set the array value as the summary text.
204 searchPreference.setSummary(searchString);
207 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
208 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
209 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
212 // Enable the translucent navigation bar preference only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
213 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingMode && !hideSystemBars);
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", "https://searx.me/"));
224 // Set the default font size as the summary text for the preference.
225 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
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);
241 if (MainWebViewActivity.darkTheme) {
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);
253 if (MainWebViewActivity.darkTheme) {
254 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
256 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
260 if (MainWebViewActivity.darkTheme) {
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_enabled", false)) { // DOM storage is enabled.
270 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
271 } else { // DOM storage is disabled.
272 if (MainWebViewActivity.darkTheme) {
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.
279 if (MainWebViewActivity.darkTheme) {
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_enabled", false)) {
289 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
291 if (MainWebViewActivity.darkTheme) {
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()) {
301 if (MainWebViewActivity.darkTheme) {
302 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
304 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
307 if (MainWebViewActivity.darkTheme) {
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)) {
316 if (MainWebViewActivity.darkTheme) {
317 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
319 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
322 if (MainWebViewActivity.darkTheme) {
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)) {
331 if (MainWebViewActivity.darkTheme) {
332 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
334 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
337 if (MainWebViewActivity.darkTheme) {
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)) {
346 if (MainWebViewActivity.darkTheme) {
347 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
349 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
352 if (MainWebViewActivity.darkTheme) {
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)) {
361 if (MainWebViewActivity.darkTheme) {
362 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
364 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
367 if (MainWebViewActivity.darkTheme) {
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)) {
376 if (MainWebViewActivity.darkTheme) {
377 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
379 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
382 if (MainWebViewActivity.darkTheme) {
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) {
391 if (MainWebViewActivity.darkTheme) {
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);
405 if (MainWebViewActivity.darkTheme) {
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)) {
430 if (MainWebViewActivity.darkTheme) {
431 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
433 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
436 if (MainWebViewActivity.darkTheme) {
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)) {
445 if (MainWebViewActivity.darkTheme) {
446 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
448 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
451 if (MainWebViewActivity.darkTheme) {
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.
460 if (MainWebViewActivity.darkTheme) {
461 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
462 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
463 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
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.
484 if (MainWebViewActivity.darkTheme) {
485 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
486 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
487 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
488 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
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()) {
499 if (MainWebViewActivity.darkTheme) {
500 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
502 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
505 if (MainWebViewActivity.darkTheme) {
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.
515 if (MainWebViewActivity.darkTheme) {
516 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
518 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
521 if (hideSystemBars) { // `hideSystemBarsBoolean` is `true`.
522 // Set the icons according to the theme.
523 if (MainWebViewActivity.darkTheme) {
524 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
525 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
527 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
528 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
530 } else { // `hideSystemBarsBoolean` is `false`.
531 // Set the `hideSystemBarsPreference` icon according to the theme.
532 if (MainWebViewActivity.darkTheme) {
533 // Set the icon for `hideSystemBarsPreference`.
534 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
536 // Set the icon for `translucentNavigationBarPreference`.
537 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
538 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
540 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
543 // Set the icon for `hideSystemBarsPreference`.
544 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
546 // Set the icon for `translucentNavigationBarPreference`.
547 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
548 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
550 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
554 } else { // Full screen browsing mode is disabled.
555 // Set the icons according to the theme.
556 if (MainWebViewActivity.darkTheme) {
557 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
558 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
559 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
561 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
562 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
563 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
567 // Set the clear everything preference icon.
568 if (clearEverything) {
569 if (MainWebViewActivity.darkTheme) {
570 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
572 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
575 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
578 // Set the clear cookies preference icon.
579 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
580 if (MainWebViewActivity.darkTheme) {
581 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
583 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
586 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
589 // Set the clear DOM storage preference icon.
590 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
591 if (MainWebViewActivity.darkTheme) {
592 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
594 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
597 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
600 // Set the clear form data preference icon if the API < 26. It has no effect on newer versions of Android.
601 if (Build.VERSION.SDK_INT < 26) {
602 if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
603 if (MainWebViewActivity.darkTheme) {
604 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
606 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
609 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
613 // Set the clear cache preference icon.
614 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
615 if (MainWebViewActivity.darkTheme) {
616 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
618 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
621 clearCachePreference.setIcon(R.drawable.cache_warning);
624 // Set the swipe to refresh preference icon.
625 if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
626 if (MainWebViewActivity.darkTheme) {
627 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
629 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
632 if (MainWebViewActivity.darkTheme) {
633 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
635 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
639 // Set the display additional app bar icons preference icon.
640 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
641 if (MainWebViewActivity.darkTheme) {
642 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
644 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
647 if (MainWebViewActivity.darkTheme) {
648 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
650 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
654 // Set the dark theme preference icon.
655 if (savedPreferences.getBoolean("dark_theme", false)) {
656 darkThemePreference.setIcon(R.drawable.theme_dark);
658 darkThemePreference.setIcon(R.drawable.theme_light);
661 // Set the night mode preference icon.
663 if (MainWebViewActivity.darkTheme) {
664 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
666 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
669 if (MainWebViewActivity.darkTheme) {
670 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
672 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
676 // Set the display webpage images preference icon.
677 if (savedPreferences.getBoolean("display_webpage_images", true)) {
678 if (MainWebViewActivity.darkTheme) {
679 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
681 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
684 if (MainWebViewActivity.darkTheme) {
685 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
687 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
692 // Listen for preference changes.
693 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
695 case "javascript_enabled":
696 // Update the icons and the DOM storage preference status.
697 if (sharedPreferences.getBoolean("javascript_enabled", false)) { // The JavaScript preference is enabled.
698 // Update the icon for the JavaScript preference.
699 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
701 // Update the status of the DOM storage preference.
702 domStoragePreference.setEnabled(true);
704 // Update the icon for the DOM storage preference.
705 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
706 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
708 if (MainWebViewActivity.darkTheme) {
709 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
711 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
714 } else { // The JavaScript preference is disabled.
715 // Update the icon for the JavaScript preference.
716 javaScriptPreference.setIcon(R.drawable.privacy_mode);
718 // Update the status of the DOM storage preference.
719 domStoragePreference.setEnabled(false);
721 // Set the icon for DOM storage preference to be ghosted.
722 if (MainWebViewActivity.darkTheme) {
723 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
725 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
730 case "first_party_cookies_enabled":
731 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
732 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
733 // Set the icon for `first_party_cookies_enabled`.
734 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
736 // Update the icon for `third_party_cookies_enabled`.
737 if (Build.VERSION.SDK_INT >= 21) {
738 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
739 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
741 if (MainWebViewActivity.darkTheme) {
742 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
744 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
748 if (MainWebViewActivity.darkTheme) {
749 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
751 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
754 } else { // `first_party_cookies_enabled` is `false`.
755 // Update the icon for `first_party_cookies_enabled`.
756 if (MainWebViewActivity.darkTheme) {
757 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
759 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
762 // Set the icon for `third_party_cookies_enabled` to be ghosted.
763 if (MainWebViewActivity.darkTheme) {
764 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
766 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
770 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
771 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
774 case "third_party_cookies_enabled":
776 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
777 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
779 if (MainWebViewActivity.darkTheme) {
780 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
782 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
787 case "dom_storage_enabled":
789 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
790 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
792 if (MainWebViewActivity.darkTheme) {
793 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
795 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
800 // Save form data can be removed once the minimum API >= 26.
801 case "save_form_data_enabled":
803 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
804 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
806 if (MainWebViewActivity.darkTheme) {
807 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
809 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
814 // Get the new user agent name.
815 String newUserAgentName = sharedPreferences.getString("user_agent", "Privacy Browser");
817 // Get the array position for the new user agent name.
818 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
820 // Get the translated new user agent name.
821 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
823 // Populate the user agent summary.
824 switch (newUserAgentArrayPosition) {
825 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
826 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
827 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
829 // Disable the custom user agent preference.
830 customUserAgentPreference.setEnabled(false);
832 // Set the custom user agent preference icon according to the theme.
833 if (MainWebViewActivity.darkTheme) {
834 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
836 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
840 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
841 // Set the summary text.
842 userAgentPreference.setSummary(R.string.custom_user_agent);
844 // Enable the custom user agent preference.
845 customUserAgentPreference.setEnabled(true);
847 // Set the custom user agent preference icon according to the theme.
848 if (MainWebViewActivity.darkTheme) {
849 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
851 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
856 // Get the user agent summary from the user agent data array.
857 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
859 // Disable the custom user agent preference.
860 customUserAgentPreference.setEnabled(false);
862 // Set the custom user agent preference icon according to the theme.
863 if (MainWebViewActivity.darkTheme) {
864 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
866 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
871 case "custom_user_agent":
872 // Set the new custom user agent as the summary text for the preference.
873 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
876 case "incognito_mode":
878 if (sharedPreferences.getBoolean("incognito_mode", false)) {
879 if (MainWebViewActivity.darkTheme) {
880 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
882 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
885 if (MainWebViewActivity.darkTheme) {
886 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
888 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
895 if (sharedPreferences.getBoolean("do_not_track", false)) {
896 if (MainWebViewActivity.darkTheme) {
897 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
899 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
902 if (MainWebViewActivity.darkTheme) {
903 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
905 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
911 case "allow_screenshots":
913 if (sharedPreferences.getBoolean("allow_screenshots", false)) {
914 if (MainWebViewActivity.darkTheme) {
915 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
917 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
920 if (MainWebViewActivity.darkTheme) {
921 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
923 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
927 // Create an intent to restart Privacy Browser.
928 Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
930 // Assert that the intent is not null to remove the lint error below.
931 assert allowScreenshotsRestartIntent != null;
933 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
934 allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
937 startActivity(allowScreenshotsRestartIntent);
942 if (sharedPreferences.getBoolean("easylist", true)) {
943 if (MainWebViewActivity.darkTheme) {
944 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
946 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
949 if (MainWebViewActivity.darkTheme) {
950 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
952 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
959 if (sharedPreferences.getBoolean("easyprivacy", true)) {
960 if (MainWebViewActivity.darkTheme) {
961 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
963 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
966 if (MainWebViewActivity.darkTheme) {
967 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
969 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
974 case "fanboy_annoyance_list":
975 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboy_annoyance_list", true);
976 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboy_social_blocking_list", true);
978 // Update the Fanboy icons.
979 if (currentFanboyAnnoyanceList) { // Fanboy's annoyance list is enabled.
980 if (MainWebViewActivity.darkTheme) {
981 // Update the Fanboy's annoyance list icon.
982 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
984 // Update the Fanboy's social blocking list icon.
985 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
987 // Update the Fanboy's annoyance list icon.
988 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
990 // Update the Fanboy's social blocking list icon.
991 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
993 } else { // Fanboy's annoyance list is disabled.
994 if (MainWebViewActivity.darkTheme) {
995 // Update the Fanboy's annoyance list icon.
996 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
998 // Update the Fanboy's social blocking list icon.
999 if (currentFanboySocialBlockingList) {
1000 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1002 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1005 // Update the Fanboy's annoyance list icon.
1006 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
1008 // Update the Fanboy's social blocking list icon.
1009 if (currentFanboySocialBlockingList) {
1010 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1012 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1017 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
1018 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
1021 case "fanboy_social_blocking_list":
1023 if (sharedPreferences.getBoolean("fanboy_social_blocking_list", true)) {
1024 if (MainWebViewActivity.darkTheme) {
1025 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1027 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1030 if (MainWebViewActivity.darkTheme) {
1031 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1033 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1038 case "ultraprivacy":
1040 if (sharedPreferences.getBoolean("ultraprivacy", true)) {
1041 if (MainWebViewActivity.darkTheme) {
1042 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
1044 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
1047 if (MainWebViewActivity.darkTheme) {
1048 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
1050 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
1055 case "block_all_third_party_requests":
1057 if (sharedPreferences.getBoolean("block_all_third_party_requests", false)) {
1058 if (MainWebViewActivity.darkTheme) {
1059 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
1061 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
1064 if (MainWebViewActivity.darkTheme) {
1065 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
1067 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
1072 case "proxy_through_orbot":
1073 // Get current settings.
1074 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
1075 String currentTorSearchString = sharedPreferences.getString("tor_search", "http://ulrn6sryqaifefld.onion/?q=");
1077 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
1078 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
1080 // Update the icons.
1081 if (currentProxyThroughOrbot) {
1082 // Set the Tor icons according to the theme.
1083 if (MainWebViewActivity.darkTheme) {
1084 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1085 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1086 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1088 // Set the `torSearchCustomURLPreference` icon.
1089 if (torSearchCustomURLPreference.isEnabled()) {
1090 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1092 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1095 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1096 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1097 torSearchPreference.setIcon(R.drawable.search_enabled_light);
1099 // Set the `torSearchCustomURLPreference` icon.
1100 if (torSearchCustomURLPreference.isEnabled()) {
1101 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1103 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1106 } else { // Proxy through Orbot is disabled.
1107 if (MainWebViewActivity.darkTheme) {
1108 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1109 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1110 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1111 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1113 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1114 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1115 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1116 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1121 case "tor_homepage":
1122 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is Searx: `http://ulrn6sryqaifefld.onion/`.
1123 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "http://ulrn6sryqaifefld.onion/"));
1127 // Get the present search string.
1128 String presentTorSearchString = sharedPreferences.getString("tor_search", "http://ulrn6sryqaifefld.onion/?q=");
1130 // Update the preferences.
1131 if (presentTorSearchString.equals("Custom URL")) {
1132 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1133 torSearchPreference.setSummary(R.string.custom_url);
1135 // Enable `torSearchCustomURLPreference`.
1136 torSearchCustomURLPreference.setEnabled(true);
1138 // Update the `torSearchCustomURLPreference` icon.
1139 if (MainWebViewActivity.darkTheme) {
1140 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1142 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1145 // Set the array value as the summary text.
1146 torSearchPreference.setSummary(presentTorSearchString);
1148 // Disable `torSearchCustomURLPreference`.
1149 torSearchCustomURLPreference.setEnabled(false);
1151 // Update the `torSearchCustomURLPreference` icon.
1152 if (MainWebViewActivity.darkTheme) {
1153 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1155 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1160 case "tor_search_custom_url":
1161 // Set the summary text for `tor_search_custom_url`.
1162 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
1166 // Store the new search string.
1167 String newSearchString = sharedPreferences.getString("search", "https://searx.me/?q=");
1169 // Update `searchPreference` and `searchCustomURLPreference`.
1170 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1171 // Set the summary text to `R.string.custom_url`, which is translated.
1172 searchPreference.setSummary(R.string.custom_url);
1174 // Enable `searchCustomURLPreference`.
1175 searchCustomURLPreference.setEnabled(true);
1177 // Set the `searchCustomURLPreference` according to the theme.
1178 if (MainWebViewActivity.darkTheme) {
1179 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1181 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1183 } else { // `Custom URL` is not selected.
1184 // Set the summary text to `newSearchString`.
1185 searchPreference.setSummary(newSearchString);
1187 // Disable `searchCustomURLPreference`.
1188 searchCustomURLPreference.setEnabled(false);
1190 // Set the `searchCustomURLPreference` according to the theme.
1191 if (MainWebViewActivity.darkTheme) {
1192 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1194 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1199 case "search_custom_url":
1200 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1201 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
1204 case "full_screen_browsing_mode":
1205 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
1206 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
1207 if (MainWebViewActivity.darkTheme) {
1208 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1210 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1213 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
1214 // Disable `translucentNavigationBarPreference`.
1215 translucentNavigationBarPreference.setEnabled(false);
1217 // Set the icons according to the theme.
1218 if (MainWebViewActivity.darkTheme) {
1219 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1220 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1222 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1223 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1225 } else { // `hide_system_bars` is `false`.
1226 // Enable `translucentNavigationBarPreference`.
1227 translucentNavigationBarPreference.setEnabled(true);
1229 // Set the icons according to the theme.
1230 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
1231 // Set the `hideSystemBarsPreference` icon.
1232 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1234 // Set the `translucentNavigationBarPreference` icon.
1235 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1236 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1238 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1240 } else { // Use the light theme.
1241 // Set the `hideSystemBarsPreference` icon.
1242 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1244 // Set the `translucentNavigationBarPreference` icon.
1245 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1246 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1248 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1252 } else { // `full_screen_browsing_mode` is false.
1253 // Disable `translucentNavigationBarPreference`.
1254 translucentNavigationBarPreference.setEnabled(false);
1256 // Update the icons according to the theme.
1257 if (MainWebViewActivity.darkTheme) {
1258 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1259 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
1260 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1262 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1263 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
1264 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1269 case "hide_system_bars":
1270 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
1271 // Disable `translucentNavigationBarPreference`.
1272 translucentNavigationBarPreference.setEnabled(false);
1274 // Set the icons according to the theme.
1275 if (MainWebViewActivity.darkTheme) {
1276 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1277 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1279 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1280 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1282 } else { // `hide_system_bars` is false.
1283 // Enable `translucentNavigationBarPreference`.
1284 translucentNavigationBarPreference.setEnabled(true);
1286 // Set the icons according to the theme.
1287 if (MainWebViewActivity.darkTheme) {
1288 // Set the `hideSystemBarsPreference` icon.
1289 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1291 // Set the `translucentNavigationBarPreference` icon.
1292 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1293 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1295 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1298 // Set the `hideSystemBarsPreference` icon.
1299 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1301 // Set the `translucentNavigationBarPreference` icon.
1302 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1303 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1305 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1311 case "translucent_navigation_bar":
1313 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1314 if (MainWebViewActivity.darkTheme) {
1315 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1317 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1320 if (MainWebViewActivity.darkTheme) {
1321 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1323 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1328 case "clear_everything":
1329 // Store the new `clear_everything` status
1330 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1332 // Update the status of the `Clear and Exit` preferences.
1333 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1334 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1335 clearFormDataPreference.setEnabled(!newClearEverythingBoolean); // This line can be removed once the minimum API >= 26.
1336 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1338 // Update the `clearEverythingPreference` icon.
1339 if (newClearEverythingBoolean) {
1340 if (MainWebViewActivity.darkTheme) {
1341 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1343 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1346 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1349 // Update the `clearCookiesPreference` icon.
1350 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1351 if (MainWebViewActivity.darkTheme) {
1352 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1354 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1357 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1360 // Update the `clearDomStoragePreference` icon.
1361 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1362 if (MainWebViewActivity.darkTheme) {
1363 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1365 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1368 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1371 // Update the clear form data preference icon if the API < 26.
1372 if (Build.VERSION.SDK_INT < 26) {
1373 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1374 if (MainWebViewActivity.darkTheme) {
1375 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1377 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1380 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1384 // Update the `clearCachePreference` icon.
1385 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1386 if (MainWebViewActivity.darkTheme) {
1387 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1389 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1392 clearCachePreference.setIcon(R.drawable.cache_warning);
1396 case "clear_cookies":
1398 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1399 if (MainWebViewActivity.darkTheme) {
1400 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1402 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1405 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1409 case "clear_dom_storage":
1411 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1412 if (MainWebViewActivity.darkTheme) {
1413 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1415 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1418 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1422 // This section can be removed once the minimum API >= 26.
1423 case "clear_form_data":
1425 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1426 if (MainWebViewActivity.darkTheme) {
1427 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1429 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1432 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1438 if (sharedPreferences.getBoolean("clear_cache", true)) {
1439 if (MainWebViewActivity.darkTheme) {
1440 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1442 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1445 clearCachePreference.setIcon(R.drawable.cache_warning);
1450 // Set the new homepage URL as the summary text for the Homepage preference.
1451 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://searx.me/"));
1454 case "default_font_size":
1455 // Update the summary text of `default_font_size`.
1456 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1459 case "swipe_to_refresh":
1461 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1462 if (MainWebViewActivity.darkTheme) {
1463 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1465 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1468 if (MainWebViewActivity.darkTheme) {
1469 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1471 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1476 case "display_additional_app_bar_icons":
1478 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1479 if (MainWebViewActivity.darkTheme) {
1480 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1482 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1485 if (MainWebViewActivity.darkTheme) {
1486 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1488 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1495 if (sharedPreferences.getBoolean("dark_theme", false)) {
1496 darkThemePreference.setIcon(R.drawable.theme_dark);
1498 darkThemePreference.setIcon(R.drawable.theme_light);
1501 // Create an intent to restart Privacy Browser.
1502 Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1504 // Assert that the intent is not null to remove the lint error below.
1505 assert changeThemeRestartIntent != null;
1507 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1508 changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1511 startActivity(changeThemeRestartIntent);
1515 // Set the URL to be reloaded on restart to apply the new night mode setting.
1516 MainWebViewActivity.loadUrlOnRestart = true;
1518 // Store the current night mode status.
1519 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1520 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript_enabled", false);
1523 if (currentNightModeBoolean) {
1524 if (MainWebViewActivity.darkTheme) {
1525 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1527 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1530 if (MainWebViewActivity.darkTheme) {
1531 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1533 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1537 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1538 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1539 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1541 // Update the `javaScriptPreference` icon.
1542 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1543 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1545 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1548 // Update the `domStoragePreference` icon.
1549 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1550 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
1551 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1552 } else { // DOM storage is disabled.
1553 if (MainWebViewActivity.darkTheme) {
1554 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1556 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1559 } else { // The preference is disabled. The icon should be ghosted.
1560 if (MainWebViewActivity.darkTheme) {
1561 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1563 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1568 case "display_webpage_images":
1569 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1571 if (MainWebViewActivity.darkTheme) {
1572 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1574 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1577 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1578 MainWebViewActivity.reloadOnRestart = false;
1581 if (MainWebViewActivity.darkTheme) {
1582 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1584 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1587 // Set `mainWebView` to reload on restart to remove the current images.
1588 MainWebViewActivity.reloadOnRestart = true;
1594 // Register the listener.
1595 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1598 // 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
1599 // even while running in the foreground.
1601 public void onPause() {
1603 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1607 public void onResume() {
1609 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);