2 * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.fragments;
22 import android.annotation.SuppressLint;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.preference.Preference;
29 import android.preference.PreferenceCategory;
30 import android.preference.PreferenceFragment;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.webkit.WebView;
34 import android.widget.ArrayAdapter;
36 import com.stoutner.privacybrowser.R;
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 public class SettingsFragment extends PreferenceFragment {
40 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
41 private SharedPreferences savedPreferences;
44 public void onCreate(Bundle savedInstanceState) {
45 // Run the default commands.
46 super.onCreate(savedInstanceState);
48 // Load the preferences from the XML file.
49 addPreferencesFromResource(R.xml.preferences);
51 // Get a handle for the context.
52 Context context = getActivity().getApplicationContext();
54 // Initialize savedPreferences.
55 savedPreferences = getPreferenceScreen().getSharedPreferences();
57 // Get handles for the preferences.
58 final Preference javaScriptPreference = findPreference("javascript");
59 final Preference firstPartyCookiesPreference = findPreference("first_party_cookies");
60 final Preference thirdPartyCookiesPreference = findPreference("third_party_cookies");
61 final Preference domStoragePreference = findPreference("dom_storage");
62 final Preference saveFormDataPreference = findPreference("save_form_data"); // The form data preference can be removed once the minimum API >= 26.
63 final Preference userAgentPreference = findPreference("user_agent");
64 final Preference customUserAgentPreference = findPreference("custom_user_agent");
65 final Preference incognitoModePreference = findPreference("incognito_mode");
66 final Preference doNotTrackPreference = findPreference("do_not_track");
67 final Preference allowScreenshotsPreference = findPreference("allow_screenshots");
68 final Preference easyListPreference = findPreference("easylist");
69 final Preference easyPrivacyPreference = findPreference("easyprivacy");
70 final Preference fanboyAnnoyanceListPreference = findPreference("fanboys_annoyance_list");
71 final Preference fanboySocialBlockingListPreference = findPreference("fanboys_social_blocking_list");
72 final Preference ultraPrivacyPreference = findPreference("ultraprivacy");
73 final Preference blockAllThirdPartyRequestsPreference = findPreference("block_all_third_party_requests");
74 final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
75 final Preference torHomepagePreference = findPreference("tor_homepage");
76 final Preference torSearchPreference = findPreference("tor_search");
77 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
78 final Preference searchPreference = findPreference("search");
79 final Preference searchCustomURLPreference = findPreference("search_custom_url");
80 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
81 final Preference hideAppBarPreference = findPreference("hide_app_bar");
82 final Preference clearEverythingPreference = findPreference("clear_everything");
83 final Preference clearCookiesPreference = findPreference("clear_cookies");
84 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
85 final Preference clearFormDataPreference = findPreference("clear_form_data"); // The clear form data preference can be removed once the minimum API >= 26.
86 final Preference clearCachePreference = findPreference("clear_cache");
87 final Preference homepagePreference = findPreference("homepage");
88 final Preference fontSizePreference = findPreference("font_size");
89 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
90 final Preference scrollAppBarPreference = findPreference("scroll_app_bar");
91 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
92 final Preference downloadWithExternalAppPreference = findPreference("download_with_external_app");
93 final Preference darkThemePreference = findPreference("dark_theme");
94 final Preference nightModePreference = findPreference("night_mode");
95 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
98 torHomepagePreference.setDependency("proxy_through_orbot");
99 torSearchPreference.setDependency("proxy_through_orbot");
100 hideAppBarPreference.setDependency("full_screen_browsing_mode");
102 // Get strings from the preferences.
103 String torSearchString = savedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
104 String searchString = savedPreferences.getString("search", getString(R.string.search_default_value));
106 // Get booleans that are used in multiple places from the preferences.
107 final boolean javaScriptEnabled = savedPreferences.getBoolean("javascript", false);
108 boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies", false);
109 boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies", false);
110 boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboys_annoyance_list", true);
111 boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboys_social_blocking_list", true);
112 boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
113 boolean fullScreenBrowsingMode = savedPreferences.getBoolean("full_screen_browsing_mode", false);
114 boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
115 final boolean nightMode = savedPreferences.getBoolean("night_mode", false);
117 // Only enable the third-party cookies preference if first-party cookies are enabled and API >= 21.
118 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabled && (Build.VERSION.SDK_INT >= 21));
120 // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
121 domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
123 // Remove the form data preferences if the API is >= 26 as they no longer do anything.
124 if (Build.VERSION.SDK_INT >= 26) {
125 // Get the categories.
126 PreferenceCategory privacyCategory = (PreferenceCategory) findPreference("privacy");
127 PreferenceCategory clearAndExitCategory = (PreferenceCategory) findPreference("clear_and_exit");
129 // Remove the form data preferences.
130 privacyCategory.removePreference(saveFormDataPreference);
131 clearAndExitCategory.removePreference(clearFormDataPreference);
134 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
135 fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
137 // Inflate a WebView to get the default user agent.
138 LayoutInflater inflater = getActivity().getLayoutInflater();
139 // `@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.
140 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
141 final WebView bareWebView = bareWebViewLayout.findViewById(R.id.bare_webview);
143 // Get the user agent arrays.
144 ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(context, R.array.user_agent_names, R.layout.spinner_item);
145 String[] translatedUserAgentNamesArray = getResources().getStringArray(R.array.translated_user_agent_names);
146 String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
148 // Get the current user agent name from the preference.
149 String userAgentName = savedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
151 // Get the array position of the user agent name.
152 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
154 // Populate the user agent summary.
155 switch (userAgentArrayPosition) {
156 case MainWebViewActivity.UNRECOGNIZED_USER_AGENT: // The user agent name is not on the canonical list.
157 // 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.
158 userAgentPreference.setSummary(userAgentName);
161 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
162 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
163 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + bareWebView.getSettings().getUserAgentString());
166 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
167 // Set the summary text.
168 userAgentPreference.setSummary(R.string.custom_user_agent);
172 // Get the user agent summary from the user agent data array.
173 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + userAgentDataArray[userAgentArrayPosition]);
176 // Set the summary text for the custom user agent preference and enable it if user agent preference is set to custom.
177 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
178 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals(getString(R.string.custom_user_agent)));
181 // 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/`.
182 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
185 // Set the Tor search URL as the summary text for the Tor preference when the preference screen is loaded.
186 if (torSearchString.equals("Custom URL")) {
187 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
188 torSearchPreference.setSummary(R.string.custom_url);
190 // Set the array value as the summary text.
191 torSearchPreference.setSummary(torSearchString);
194 // Set the summary text for `tor_search_custom_url`. The default is `""`.
195 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", getString(R.string.tor_search_custom_url_default_value)));
197 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
198 torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
201 // Set the search URL as the summary text for the search preference when the preference screen is loaded.
202 if (searchString.equals("Custom URL")) {
203 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
204 searchPreference.setSummary(R.string.custom_url);
206 // Set the array value as the summary text.
207 searchPreference.setSummary(searchString);
210 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
211 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value)));
212 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
214 // Set the status of the Clear and Exit preferences.
215 clearCookiesPreference.setEnabled(!clearEverything);
216 clearDomStoragePreference.setEnabled(!clearEverything);
217 clearFormDataPreference.setEnabled(!clearEverything); // The form data line can be removed once the minimum API is >= 26.
218 clearCachePreference.setEnabled(!clearEverything);
220 // Set the homepage URL as the summary text for the homepage preference.
221 homepagePreference.setSummary(savedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
223 // Set the font size as the summary text for the preference.
224 fontSizePreference.setSummary(savedPreferences.getString("font_size", getString(R.string.font_size_default_value)) + "%%");
226 // Disable the JavaScript preference if Night Mode is enabled. JavaScript will be enabled for all web pages.
227 javaScriptPreference.setEnabled(!nightMode);
229 // Set the JavaScript icon.
230 if (javaScriptEnabled || nightMode) {
231 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
233 javaScriptPreference.setIcon(R.drawable.privacy_mode);
236 // Set the first-party cookies icon.
237 if (firstPartyCookiesEnabled) {
238 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
240 if (MainWebViewActivity.darkTheme) {
241 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
243 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
247 // Set the third party cookies icon.
248 if (firstPartyCookiesEnabled && Build.VERSION.SDK_INT >= 21) {
249 if (thirdPartyCookiesEnabled) {
250 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
252 if (MainWebViewActivity.darkTheme) {
253 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
255 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
259 if (MainWebViewActivity.darkTheme) {
260 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
262 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
266 // Set the DOM storage icon.
267 if (javaScriptEnabled || nightMode) { // The preference is enabled.
268 if (savedPreferences.getBoolean("dom_storage", false)) { // DOM storage is enabled.
269 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
270 } else { // DOM storage is disabled.
271 if (MainWebViewActivity.darkTheme) {
272 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
274 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
277 } else { // The preference is disabled. The icon should be ghosted.
278 if (MainWebViewActivity.darkTheme) {
279 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
281 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
285 // Set the save form data icon if API < 26. Save form data has no effect on API >= 26.
286 if (Build.VERSION.SDK_INT < 26) {
287 if (savedPreferences.getBoolean("save_form_data", false)) {
288 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
290 if (MainWebViewActivity.darkTheme) {
291 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
293 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
298 // Set the custom user agent icon.
299 if (customUserAgentPreference.isEnabled()) {
300 if (MainWebViewActivity.darkTheme) {
301 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
303 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
306 if (MainWebViewActivity.darkTheme) {
307 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
309 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
313 // Set the incognito mode icon.
314 if (savedPreferences.getBoolean("incognito_mode", false)) {
315 if (MainWebViewActivity.darkTheme) {
316 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
318 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
321 if (MainWebViewActivity.darkTheme) {
322 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
324 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
328 // Set the Do Not Track icon.
329 if (savedPreferences.getBoolean("do_not_track", false)) {
330 if (MainWebViewActivity.darkTheme) {
331 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
333 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
336 if (MainWebViewActivity.darkTheme) {
337 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
339 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
343 // Set the allow screenshots icon.
344 if (savedPreferences.getBoolean("allow_screenshots", false)) {
345 if (MainWebViewActivity.darkTheme) {
346 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
348 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
351 if (MainWebViewActivity.darkTheme) {
352 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
354 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
358 // Set the EasyList icon.
359 if (savedPreferences.getBoolean("easylist", true)) {
360 if (MainWebViewActivity.darkTheme) {
361 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
363 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
366 if (MainWebViewActivity.darkTheme) {
367 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
369 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
373 // Set the EasyPrivacy icon.
374 if (savedPreferences.getBoolean("easyprivacy", true)) {
375 if (MainWebViewActivity.darkTheme) {
376 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
378 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
381 if (MainWebViewActivity.darkTheme) {
382 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
384 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
388 // Set the Fanboy lists icons.
389 if (fanboyAnnoyanceListEnabled) {
390 if (MainWebViewActivity.darkTheme) {
391 // Set the Fanboy annoyance list icon.
392 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
394 // Set the Fanboy social blocking list icon.
395 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
397 // Set the Fanboy annoyance list icon.
398 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
400 // Set the Fanboy social blocking list icon.
401 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
404 if (MainWebViewActivity.darkTheme) {
405 // Set the Fanboy annoyance list icon.
406 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
408 // Set the Fanboy social blocking list icon.
409 if (fanboySocialBlockingEnabled) {
410 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
412 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
415 // Set the Fanboy annoyance list icon.
416 fanboyAnnoyanceListPreference.setIcon(R.drawable.block_ads_disabled_light);
418 // Set the Fanboy social blocking list icon.
419 if (fanboySocialBlockingEnabled) {
420 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
422 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
427 // Set the UltraPrivacy icon.
428 if (savedPreferences.getBoolean("ultraprivacy", true)) {
429 if (MainWebViewActivity.darkTheme) {
430 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
432 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
435 if (MainWebViewActivity.darkTheme) {
436 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
438 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
442 // Set the block all third-party requests icon.
443 if (savedPreferences.getBoolean("block_all_third_party_requests", false)) {
444 if (MainWebViewActivity.darkTheme) {
445 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
447 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
450 if (MainWebViewActivity.darkTheme) {
451 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
453 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
457 // Set the Tor icons according to the theme.
458 if (proxyThroughOrbot) { // Proxying is enabled.
459 if (MainWebViewActivity.darkTheme) {
460 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
461 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
462 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
464 // Set the custom search icon.
465 if (torSearchCustomURLPreference.isEnabled()) {
466 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
468 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
471 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
472 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
473 torSearchPreference.setIcon(R.drawable.search_enabled_light);
475 // Set the custom search icon.
476 if (torSearchCustomURLPreference.isEnabled()) {
477 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
479 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
482 } else { // Proxying is disabled.
483 if (MainWebViewActivity.darkTheme) {
484 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
485 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
486 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
487 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
489 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
490 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
491 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
492 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
496 // Set the search custom URL icon.
497 if (searchCustomURLPreference.isEnabled()) {
498 if (MainWebViewActivity.darkTheme) {
499 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
501 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
504 if (MainWebViewActivity.darkTheme) {
505 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
507 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
511 // Set the full screen browsing mode icons.
512 if (fullScreenBrowsingMode) { // Full screen browsing mode is enabled.
513 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
514 if (MainWebViewActivity.darkTheme) {
515 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
517 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
520 // Set the hide app bar icon.
521 if (savedPreferences.getBoolean("hide_app_bar", true)) { // Hide app bar is enabled.
522 // Set the icon according to the theme.
523 if (MainWebViewActivity.darkTheme) {
524 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
526 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
528 } else { // Hide app bar is disabled.
529 // Set the icon according to the theme.
530 if (MainWebViewActivity.darkTheme) {
531 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
533 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
536 } else { // Full screen browsing mode is disabled.
537 // Set the icons according to the theme.
538 if (MainWebViewActivity.darkTheme) {
539 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
540 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_dark);
542 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
543 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_light);
547 // Set the clear everything preference icon.
548 if (clearEverything) {
549 if (MainWebViewActivity.darkTheme) {
550 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
552 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
555 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
558 // Set the clear cookies preference icon.
559 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
560 if (MainWebViewActivity.darkTheme) {
561 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
563 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
566 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
569 // Set the clear DOM storage preference icon.
570 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
571 if (MainWebViewActivity.darkTheme) {
572 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
574 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
577 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
580 // Set the clear form data preference icon if the API < 26. It has no effect on newer versions of Android.
581 if (Build.VERSION.SDK_INT < 26) {
582 if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
583 if (MainWebViewActivity.darkTheme) {
584 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
586 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
589 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
593 // Set the clear cache preference icon.
594 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
595 if (MainWebViewActivity.darkTheme) {
596 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
598 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
601 clearCachePreference.setIcon(R.drawable.cache_warning);
604 // Set the swipe to refresh preference icon.
605 if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
606 if (MainWebViewActivity.darkTheme) {
607 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
609 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
612 if (MainWebViewActivity.darkTheme) {
613 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
615 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
619 // Set the scroll app bar preference icon.
620 if (savedPreferences.getBoolean("scroll_app_bar", true)) {
621 if (MainWebViewActivity.darkTheme) {
622 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
624 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
627 if (MainWebViewActivity.darkTheme) {
628 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
630 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
634 // Set the display additional app bar icons preference icon.
635 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
636 if (MainWebViewActivity.darkTheme) {
637 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
639 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
642 if (MainWebViewActivity.darkTheme) {
643 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
645 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
649 // Set the download with external app preference icon.
650 if (savedPreferences.getBoolean("download_with_external_app", false)) {
651 if (MainWebViewActivity.darkTheme) {
652 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
654 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
657 if (MainWebViewActivity.darkTheme) {
658 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
660 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
664 // Set the dark theme preference icon.
665 if (savedPreferences.getBoolean("dark_theme", false)) {
666 darkThemePreference.setIcon(R.drawable.theme_dark);
668 darkThemePreference.setIcon(R.drawable.theme_light);
671 // Set the night mode preference icon.
673 if (MainWebViewActivity.darkTheme) {
674 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
676 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
679 if (MainWebViewActivity.darkTheme) {
680 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
682 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
686 // Set the display webpage images preference icon.
687 if (savedPreferences.getBoolean("display_webpage_images", true)) {
688 if (MainWebViewActivity.darkTheme) {
689 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
691 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
694 if (MainWebViewActivity.darkTheme) {
695 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
697 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
702 // Listen for preference changes.
703 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
706 // Update the icons and the DOM storage preference status.
707 if (sharedPreferences.getBoolean("javascript", false)) { // The JavaScript preference is enabled.
708 // Update the icon for the JavaScript preference.
709 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
711 // Update the status of the DOM storage preference.
712 domStoragePreference.setEnabled(true);
714 // Update the icon for the DOM storage preference.
715 if (sharedPreferences.getBoolean("dom_storage", false)) {
716 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
718 if (MainWebViewActivity.darkTheme) {
719 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
721 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
724 } else { // The JavaScript preference is disabled.
725 // Update the icon for the JavaScript preference.
726 javaScriptPreference.setIcon(R.drawable.privacy_mode);
728 // Update the status of the DOM storage preference.
729 domStoragePreference.setEnabled(false);
731 // Set the icon for DOM storage preference to be ghosted.
732 if (MainWebViewActivity.darkTheme) {
733 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
735 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
740 case "first_party_cookies":
741 // Update the icons for `first_party_cookies` and `third_party_cookies`.
742 if (sharedPreferences.getBoolean("first_party_cookies", false)) {
743 // Set the icon for `first_party_cookies`.
744 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
746 // Update the icon for `third_party_cookies`.
747 if (Build.VERSION.SDK_INT >= 21) {
748 if (sharedPreferences.getBoolean("third_party_cookies", false)) {
749 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
751 if (MainWebViewActivity.darkTheme) {
752 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
754 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
758 if (MainWebViewActivity.darkTheme) {
759 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
761 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
764 } else { // `first_party_cookies` is `false`.
765 // Update the icon for `first_party_cookies`.
766 if (MainWebViewActivity.darkTheme) {
767 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
769 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
772 // Set the icon for `third_party_cookies` to be ghosted.
773 if (MainWebViewActivity.darkTheme) {
774 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
776 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
780 // Enable `third_party_cookies` if `first_party_cookies` is `true` and API >= 21.
781 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies", false) && (Build.VERSION.SDK_INT >= 21));
784 case "third_party_cookies":
786 if (sharedPreferences.getBoolean("third_party_cookies", false)) {
787 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
789 if (MainWebViewActivity.darkTheme) {
790 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
792 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
799 if (sharedPreferences.getBoolean("dom_storage", false)) {
800 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
802 if (MainWebViewActivity.darkTheme) {
803 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
805 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
810 // Save form data can be removed once the minimum API >= 26.
811 case "save_form_data":
813 if (sharedPreferences.getBoolean("save_form_data", false)) {
814 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
816 if (MainWebViewActivity.darkTheme) {
817 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
819 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
824 // Get the new user agent name.
825 String newUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
827 // Get the array position for the new user agent name.
828 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
830 // Get the translated new user agent name.
831 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
833 // Populate the user agent summary.
834 switch (newUserAgentArrayPosition) {
835 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
836 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
837 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
839 // Disable the custom user agent preference.
840 customUserAgentPreference.setEnabled(false);
842 // Set the custom user agent preference icon according to the theme.
843 if (MainWebViewActivity.darkTheme) {
844 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
846 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
850 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
851 // Set the summary text.
852 userAgentPreference.setSummary(R.string.custom_user_agent);
854 // Enable the custom user agent preference.
855 customUserAgentPreference.setEnabled(true);
857 // Set the custom user agent preference icon according to the theme.
858 if (MainWebViewActivity.darkTheme) {
859 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
861 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
866 // Get the user agent summary from the user agent data array.
867 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
869 // Disable the custom user agent preference.
870 customUserAgentPreference.setEnabled(false);
872 // Set the custom user agent preference icon according to the theme.
873 if (MainWebViewActivity.darkTheme) {
874 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
876 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
881 case "custom_user_agent":
882 // Set the new custom user agent as the summary text for the preference.
883 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
886 case "incognito_mode":
888 if (sharedPreferences.getBoolean("incognito_mode", false)) {
889 if (MainWebViewActivity.darkTheme) {
890 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
892 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
895 if (MainWebViewActivity.darkTheme) {
896 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
898 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
905 if (sharedPreferences.getBoolean("do_not_track", false)) {
906 if (MainWebViewActivity.darkTheme) {
907 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
909 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
912 if (MainWebViewActivity.darkTheme) {
913 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
915 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
921 case "allow_screenshots":
923 if (sharedPreferences.getBoolean("allow_screenshots", false)) {
924 if (MainWebViewActivity.darkTheme) {
925 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
927 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
930 if (MainWebViewActivity.darkTheme) {
931 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
933 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
937 // Create an intent to restart Privacy Browser.
938 Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
940 // Assert that the intent is not null to remove the lint error below.
941 assert allowScreenshotsRestartIntent != null;
943 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
944 allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
947 startActivity(allowScreenshotsRestartIntent);
952 if (sharedPreferences.getBoolean("easylist", true)) {
953 if (MainWebViewActivity.darkTheme) {
954 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
956 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
959 if (MainWebViewActivity.darkTheme) {
960 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
962 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
969 if (sharedPreferences.getBoolean("easyprivacy", true)) {
970 if (MainWebViewActivity.darkTheme) {
971 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
973 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
976 if (MainWebViewActivity.darkTheme) {
977 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
979 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
984 case "fanboys_annoyance_list":
985 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
986 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
988 // Update the Fanboy icons.
989 if (currentFanboyAnnoyanceList) { // Fanboy's annoyance list is enabled.
990 if (MainWebViewActivity.darkTheme) {
991 // Update the Fanboy's annoyance list icon.
992 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
994 // Update the Fanboy's social blocking list icon.
995 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
997 // Update the Fanboy's annoyance list icon.
998 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
1000 // Update the Fanboy's social blocking list icon.
1001 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
1003 } else { // Fanboy's annoyance list is disabled.
1004 if (MainWebViewActivity.darkTheme) {
1005 // Update the Fanboy's annoyance list icon.
1006 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
1008 // Update the Fanboy's social blocking list icon.
1009 if (currentFanboySocialBlockingList) {
1010 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1012 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1015 // Update the Fanboy's annoyance list icon.
1016 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
1018 // Update the Fanboy's social blocking list icon.
1019 if (currentFanboySocialBlockingList) {
1020 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1022 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1027 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
1028 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
1031 case "fanboys_social_blocking_list":
1033 if (sharedPreferences.getBoolean("fanboys_social_blocking_list", true)) {
1034 if (MainWebViewActivity.darkTheme) {
1035 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1037 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1040 if (MainWebViewActivity.darkTheme) {
1041 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1043 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1048 case "ultraprivacy":
1050 if (sharedPreferences.getBoolean("ultraprivacy", true)) {
1051 if (MainWebViewActivity.darkTheme) {
1052 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
1054 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
1057 if (MainWebViewActivity.darkTheme) {
1058 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
1060 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
1065 case "block_all_third_party_requests":
1067 if (sharedPreferences.getBoolean("block_all_third_party_requests", false)) {
1068 if (MainWebViewActivity.darkTheme) {
1069 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
1071 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
1074 if (MainWebViewActivity.darkTheme) {
1075 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
1077 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
1082 case "proxy_through_orbot":
1083 // Get current settings.
1084 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
1085 String currentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1087 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
1088 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
1090 // Update the icons.
1091 if (currentProxyThroughOrbot) {
1092 // Set the Tor icons according to the theme.
1093 if (MainWebViewActivity.darkTheme) {
1094 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1095 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1096 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1098 // Set the `torSearchCustomURLPreference` icon.
1099 if (torSearchCustomURLPreference.isEnabled()) {
1100 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1102 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1105 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1106 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1107 torSearchPreference.setIcon(R.drawable.search_enabled_light);
1109 // Set the `torSearchCustomURLPreference` icon.
1110 if (torSearchCustomURLPreference.isEnabled()) {
1111 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1113 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1116 } else { // Proxy through Orbot is disabled.
1117 if (MainWebViewActivity.darkTheme) {
1118 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1119 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1120 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1121 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1123 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1124 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1125 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1126 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1131 case "tor_homepage":
1132 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is Searx: `http://ulrn6sryqaifefld.onion/`.
1133 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
1137 // Get the present search string.
1138 String presentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1140 // Update the preferences.
1141 if (presentTorSearchString.equals("Custom URL")) {
1142 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1143 torSearchPreference.setSummary(R.string.custom_url);
1145 // Enable `torSearchCustomURLPreference`.
1146 torSearchCustomURLPreference.setEnabled(true);
1148 // Update the `torSearchCustomURLPreference` icon.
1149 if (MainWebViewActivity.darkTheme) {
1150 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1152 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1155 // Set the array value as the summary text.
1156 torSearchPreference.setSummary(presentTorSearchString);
1158 // Disable `torSearchCustomURLPreference`.
1159 torSearchCustomURLPreference.setEnabled(false);
1161 // Update the `torSearchCustomURLPreference` icon.
1162 if (MainWebViewActivity.darkTheme) {
1163 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1165 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1170 case "tor_search_custom_url":
1171 // Set the summary text for `tor_search_custom_url`.
1172 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", getString(R.string.tor_search_custom_url_default_value)));
1176 // Store the new search string.
1177 String newSearchString = sharedPreferences.getString("search", getString(R.string.search_default_value));
1179 // Update `searchPreference` and `searchCustomURLPreference`.
1180 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1181 // Set the summary text to `R.string.custom_url`, which is translated.
1182 searchPreference.setSummary(R.string.custom_url);
1184 // Enable `searchCustomURLPreference`.
1185 searchCustomURLPreference.setEnabled(true);
1187 // Set the `searchCustomURLPreference` according to the theme.
1188 if (MainWebViewActivity.darkTheme) {
1189 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1191 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1193 } else { // `Custom URL` is not selected.
1194 // Set the summary text to `newSearchString`.
1195 searchPreference.setSummary(newSearchString);
1197 // Disable `searchCustomURLPreference`.
1198 searchCustomURLPreference.setEnabled(false);
1200 // Set the `searchCustomURLPreference` according to the theme.
1201 if (MainWebViewActivity.darkTheme) {
1202 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1204 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1209 case "search_custom_url":
1210 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1211 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value)));
1214 case "full_screen_browsing_mode":
1215 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) { // Full screen browsing is enabled.
1216 // Set the full screen browsing mode preference icon according to the theme.
1217 if (MainWebViewActivity.darkTheme) {
1218 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1220 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1223 // Set the hide app bar preference icon.
1224 if (sharedPreferences.getBoolean("hide_app_bar", true)) { // Hide app bar is enabled.
1225 // Set the icon according to the theme.
1226 if (MainWebViewActivity.darkTheme) {
1227 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1229 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1231 } else { // Hide app bar is disabled.
1232 // Set the icon according to the theme.
1233 if (MainWebViewActivity.darkTheme) {
1234 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1236 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1239 } else { // Full screen browsing is disabled.
1240 // Update the icons according to the theme.
1241 if (MainWebViewActivity.darkTheme) {
1242 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1243 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_dark);
1245 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1246 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_light);
1251 case "hide_app_bar":
1253 if (sharedPreferences.getBoolean("hide_app_bar", true)) { // Hide app bar is enabled.
1254 // Set the icon according to the theme.
1255 if (MainWebViewActivity.darkTheme) {
1256 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1258 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1260 } else { // Hide app bar is disabled.
1261 // Set the icon according to the theme.
1262 if (MainWebViewActivity.darkTheme) {
1263 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1265 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1270 case "clear_everything":
1271 // Store the new `clear_everything` status
1272 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1274 // Update the status of the `Clear and Exit` preferences.
1275 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1276 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1277 clearFormDataPreference.setEnabled(!newClearEverythingBoolean); // This line can be removed once the minimum API >= 26.
1278 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1280 // Update the `clearEverythingPreference` icon.
1281 if (newClearEverythingBoolean) {
1282 if (MainWebViewActivity.darkTheme) {
1283 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1285 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1288 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1291 // Update the `clearCookiesPreference` icon.
1292 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1293 if (MainWebViewActivity.darkTheme) {
1294 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1296 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1299 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1302 // Update the `clearDomStoragePreference` icon.
1303 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1304 if (MainWebViewActivity.darkTheme) {
1305 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1307 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1310 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1313 // Update the clear form data preference icon if the API < 26.
1314 if (Build.VERSION.SDK_INT < 26) {
1315 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1316 if (MainWebViewActivity.darkTheme) {
1317 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1319 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1322 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1326 // Update the `clearCachePreference` icon.
1327 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1328 if (MainWebViewActivity.darkTheme) {
1329 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1331 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1334 clearCachePreference.setIcon(R.drawable.cache_warning);
1338 case "clear_cookies":
1340 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1341 if (MainWebViewActivity.darkTheme) {
1342 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1344 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1347 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1351 case "clear_dom_storage":
1353 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1354 if (MainWebViewActivity.darkTheme) {
1355 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1357 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1360 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1364 // This section can be removed once the minimum API >= 26.
1365 case "clear_form_data":
1367 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1368 if (MainWebViewActivity.darkTheme) {
1369 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1371 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1374 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1380 if (sharedPreferences.getBoolean("clear_cache", true)) {
1381 if (MainWebViewActivity.darkTheme) {
1382 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1384 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1387 clearCachePreference.setIcon(R.drawable.cache_warning);
1392 // Set the new homepage URL as the summary text for the Homepage preference.
1393 homepagePreference.setSummary(sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
1397 // Update the font size summary text.
1398 fontSizePreference.setSummary(sharedPreferences.getString("font_size", getString(R.string.font_size_default_value)) + "%%");
1401 case "swipe_to_refresh":
1403 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1404 if (MainWebViewActivity.darkTheme) {
1405 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1407 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1410 if (MainWebViewActivity.darkTheme) {
1411 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1413 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1418 case "scroll_app_bar":
1420 if (sharedPreferences.getBoolean("scroll_app_bar", true)) {
1421 if (MainWebViewActivity.darkTheme) {
1422 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1424 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1427 if (MainWebViewActivity.darkTheme) {
1428 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1430 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1435 case "display_additional_app_bar_icons":
1437 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1438 if (MainWebViewActivity.darkTheme) {
1439 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1441 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1444 if (MainWebViewActivity.darkTheme) {
1445 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1447 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1452 case "download_with_external_app":
1454 if (sharedPreferences.getBoolean("download_with_external_app", false)) {
1455 if (MainWebViewActivity.darkTheme) {
1456 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
1458 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
1461 if (MainWebViewActivity.darkTheme) {
1462 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
1464 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
1471 if (sharedPreferences.getBoolean("dark_theme", false)) {
1472 darkThemePreference.setIcon(R.drawable.theme_dark);
1474 darkThemePreference.setIcon(R.drawable.theme_light);
1477 // Create an intent to restart Privacy Browser.
1478 Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1480 // Assert that the intent is not null to remove the lint error below.
1481 assert changeThemeRestartIntent != null;
1483 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1484 changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1487 startActivity(changeThemeRestartIntent);
1491 // Set the URL to be reloaded on restart to apply the new night mode setting.
1492 MainWebViewActivity.loadUrlOnRestart = true;
1494 // Store the current night mode status.
1495 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1496 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript", false);
1499 if (currentNightModeBoolean) {
1500 if (MainWebViewActivity.darkTheme) {
1501 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1503 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1506 if (MainWebViewActivity.darkTheme) {
1507 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1509 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1513 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1514 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1515 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1517 // Update the `javaScriptPreference` icon.
1518 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1519 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1521 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1524 // Update the DOM storage preference icon.
1525 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1526 if (sharedPreferences.getBoolean("dom_storage", false)) { // DOM storage is enabled.
1527 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1528 } else { // DOM storage is disabled.
1529 if (MainWebViewActivity.darkTheme) {
1530 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1532 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1535 } else { // The preference is disabled. The icon should be ghosted.
1536 if (MainWebViewActivity.darkTheme) {
1537 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1539 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1544 case "display_webpage_images":
1545 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1547 if (MainWebViewActivity.darkTheme) {
1548 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1550 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1553 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1554 MainWebViewActivity.reloadOnRestart = false;
1557 if (MainWebViewActivity.darkTheme) {
1558 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1560 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1563 // Set `mainWebView` to reload on restart to remove the current images.
1564 MainWebViewActivity.reloadOnRestart = true;
1570 // Register the listener.
1571 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1574 // It is necessary to re-register the listener on every resume or it will randomly stop working because apps can be paused and resumed at any time, even while running in the foreground.
1576 public void onPause() {
1578 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1582 public void onResume() {
1584 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);