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);
825 // Get the new user agent name.
826 String newUserAgentName = sharedPreferences.getString("user_agent", getString(R.string.user_agent_default_value));
828 // Get the array position for the new user agent name.
829 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
831 // Get the translated new user agent name.
832 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
834 // Populate the user agent summary.
835 switch (newUserAgentArrayPosition) {
836 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
837 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
838 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
840 // Disable the custom user agent preference.
841 customUserAgentPreference.setEnabled(false);
843 // Set the custom user agent preference icon according to the theme.
844 if (MainWebViewActivity.darkTheme) {
845 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
847 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
851 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
852 // Set the summary text.
853 userAgentPreference.setSummary(R.string.custom_user_agent);
855 // Enable the custom user agent preference.
856 customUserAgentPreference.setEnabled(true);
858 // Set the custom user agent preference icon according to the theme.
859 if (MainWebViewActivity.darkTheme) {
860 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
862 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
867 // Get the user agent summary from the user agent data array.
868 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
870 // Disable the custom user agent preference.
871 customUserAgentPreference.setEnabled(false);
873 // Set the custom user agent preference icon according to the theme.
874 if (MainWebViewActivity.darkTheme) {
875 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
877 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
882 case "custom_user_agent":
883 // Set the new custom user agent as the summary text for the preference.
884 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", getString(R.string.custom_user_agent_default_value)));
887 case "incognito_mode":
889 if (sharedPreferences.getBoolean("incognito_mode", false)) {
890 if (MainWebViewActivity.darkTheme) {
891 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
893 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
896 if (MainWebViewActivity.darkTheme) {
897 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
899 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
906 if (sharedPreferences.getBoolean("do_not_track", false)) {
907 if (MainWebViewActivity.darkTheme) {
908 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
910 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
913 if (MainWebViewActivity.darkTheme) {
914 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
916 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
922 case "allow_screenshots":
924 if (sharedPreferences.getBoolean("allow_screenshots", false)) {
925 if (MainWebViewActivity.darkTheme) {
926 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
928 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
931 if (MainWebViewActivity.darkTheme) {
932 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
934 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
938 // Create an intent to restart Privacy Browser.
939 Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
941 // Assert that the intent is not null to remove the lint error below.
942 assert allowScreenshotsRestartIntent != null;
944 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`. TODO.
945 allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
948 startActivity(allowScreenshotsRestartIntent);
953 if (sharedPreferences.getBoolean("easylist", true)) {
954 if (MainWebViewActivity.darkTheme) {
955 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
957 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
960 if (MainWebViewActivity.darkTheme) {
961 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
963 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
970 if (sharedPreferences.getBoolean("easyprivacy", true)) {
971 if (MainWebViewActivity.darkTheme) {
972 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
974 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
977 if (MainWebViewActivity.darkTheme) {
978 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
980 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
985 case "fanboys_annoyance_list":
986 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboys_annoyance_list", true);
987 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboys_social_blocking_list", true);
989 // Update the Fanboy icons.
990 if (currentFanboyAnnoyanceList) { // Fanboy's annoyance list is enabled.
991 if (MainWebViewActivity.darkTheme) {
992 // Update the Fanboy's annoyance list icon.
993 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
995 // Update the Fanboy's social blocking list icon.
996 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
998 // Update the Fanboy's annoyance list icon.
999 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
1001 // Update the Fanboy's social blocking list icon.
1002 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
1004 } else { // Fanboy's annoyance list is disabled.
1005 if (MainWebViewActivity.darkTheme) {
1006 // Update the Fanboy's annoyance list icon.
1007 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
1009 // Update the Fanboy's social blocking list icon.
1010 if (currentFanboySocialBlockingList) {
1011 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1013 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1016 // Update the Fanboy's annoyance list icon.
1017 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
1019 // Update the Fanboy's social blocking list icon.
1020 if (currentFanboySocialBlockingList) {
1021 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1023 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1028 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
1029 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
1032 case "fanboys_social_blocking_list":
1034 if (sharedPreferences.getBoolean("fanboys_social_blocking_list", true)) {
1035 if (MainWebViewActivity.darkTheme) {
1036 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1038 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1041 if (MainWebViewActivity.darkTheme) {
1042 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1044 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1049 case "ultraprivacy":
1051 if (sharedPreferences.getBoolean("ultraprivacy", true)) {
1052 if (MainWebViewActivity.darkTheme) {
1053 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
1055 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
1058 if (MainWebViewActivity.darkTheme) {
1059 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
1061 ultraPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
1066 case "block_all_third_party_requests":
1068 if (sharedPreferences.getBoolean("block_all_third_party_requests", false)) {
1069 if (MainWebViewActivity.darkTheme) {
1070 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
1072 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
1075 if (MainWebViewActivity.darkTheme) {
1076 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
1078 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
1083 case "proxy_through_orbot":
1084 // Get current settings.
1085 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
1086 String currentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1088 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
1089 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
1091 // Update the icons.
1092 if (currentProxyThroughOrbot) {
1093 // Set the Tor icons according to the theme.
1094 if (MainWebViewActivity.darkTheme) {
1095 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1096 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1097 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1099 // Set the `torSearchCustomURLPreference` icon.
1100 if (torSearchCustomURLPreference.isEnabled()) {
1101 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1103 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1106 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1107 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1108 torSearchPreference.setIcon(R.drawable.search_enabled_light);
1110 // Set the `torSearchCustomURLPreference` icon.
1111 if (torSearchCustomURLPreference.isEnabled()) {
1112 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1114 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1117 } else { // Proxy through Orbot is disabled.
1118 if (MainWebViewActivity.darkTheme) {
1119 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1120 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1121 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1122 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1124 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1125 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1126 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1127 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1132 case "tor_homepage":
1133 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is Searx: `http://ulrn6sryqaifefld.onion/`.
1134 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", getString(R.string.tor_homepage_default_value)));
1138 // Get the present search string.
1139 String presentTorSearchString = sharedPreferences.getString("tor_search", getString(R.string.tor_search_default_value));
1141 // Update the preferences.
1142 if (presentTorSearchString.equals("Custom URL")) {
1143 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1144 torSearchPreference.setSummary(R.string.custom_url);
1146 // Enable `torSearchCustomURLPreference`.
1147 torSearchCustomURLPreference.setEnabled(true);
1149 // Update the `torSearchCustomURLPreference` icon.
1150 if (MainWebViewActivity.darkTheme) {
1151 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1153 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1156 // Set the array value as the summary text.
1157 torSearchPreference.setSummary(presentTorSearchString);
1159 // Disable `torSearchCustomURLPreference`.
1160 torSearchCustomURLPreference.setEnabled(false);
1162 // Update the `torSearchCustomURLPreference` icon.
1163 if (MainWebViewActivity.darkTheme) {
1164 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1166 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1171 case "tor_search_custom_url":
1172 // Set the summary text for `tor_search_custom_url`.
1173 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", getString(R.string.tor_search_custom_url_default_value)));
1177 // Store the new search string.
1178 String newSearchString = sharedPreferences.getString("search", getString(R.string.search_default_value));
1180 // Update `searchPreference` and `searchCustomURLPreference`.
1181 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1182 // Set the summary text to `R.string.custom_url`, which is translated.
1183 searchPreference.setSummary(R.string.custom_url);
1185 // Enable `searchCustomURLPreference`.
1186 searchCustomURLPreference.setEnabled(true);
1188 // Set the `searchCustomURLPreference` according to the theme.
1189 if (MainWebViewActivity.darkTheme) {
1190 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1192 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1194 } else { // `Custom URL` is not selected.
1195 // Set the summary text to `newSearchString`.
1196 searchPreference.setSummary(newSearchString);
1198 // Disable `searchCustomURLPreference`.
1199 searchCustomURLPreference.setEnabled(false);
1201 // Set the `searchCustomURLPreference` according to the theme.
1202 if (MainWebViewActivity.darkTheme) {
1203 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1205 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1210 case "search_custom_url":
1211 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1212 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", getString(R.string.search_custom_url_default_value)));
1215 case "full_screen_browsing_mode":
1216 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) { // Full screen browsing is enabled.
1217 // Set the full screen browsing mode preference icon according to the theme.
1218 if (MainWebViewActivity.darkTheme) {
1219 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1221 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1224 // Set the hide app bar preference icon.
1225 if (sharedPreferences.getBoolean("hide_app_bar", true)) { // Hide app bar is enabled.
1226 // Set the icon according to the theme.
1227 if (MainWebViewActivity.darkTheme) {
1228 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1230 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1232 } else { // Hide app bar is disabled.
1233 // Set the icon according to the theme.
1234 if (MainWebViewActivity.darkTheme) {
1235 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1237 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1240 } else { // Full screen browsing is disabled.
1241 // Update the icons according to the theme.
1242 if (MainWebViewActivity.darkTheme) {
1243 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1244 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_dark);
1246 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1247 hideAppBarPreference.setIcon(R.drawable.app_bar_ghosted_light);
1252 case "hide_app_bar":
1254 if (sharedPreferences.getBoolean("hide_app_bar", true)) { // Hide app bar is enabled.
1255 // Set the icon according to the theme.
1256 if (MainWebViewActivity.darkTheme) {
1257 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1259 hideAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1261 } else { // Hide app bar is disabled.
1262 // Set the icon according to the theme.
1263 if (MainWebViewActivity.darkTheme) {
1264 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1266 hideAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1271 case "clear_everything":
1272 // Store the new `clear_everything` status
1273 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1275 // Update the status of the `Clear and Exit` preferences.
1276 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1277 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1278 clearFormDataPreference.setEnabled(!newClearEverythingBoolean); // This line can be removed once the minimum API >= 26.
1279 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1281 // Update the `clearEverythingPreference` icon.
1282 if (newClearEverythingBoolean) {
1283 if (MainWebViewActivity.darkTheme) {
1284 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1286 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1289 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1292 // Update the `clearCookiesPreference` icon.
1293 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1294 if (MainWebViewActivity.darkTheme) {
1295 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1297 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1300 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1303 // Update the `clearDomStoragePreference` icon.
1304 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1305 if (MainWebViewActivity.darkTheme) {
1306 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1308 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1311 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1314 // Update the clear form data preference icon if the API < 26.
1315 if (Build.VERSION.SDK_INT < 26) {
1316 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1317 if (MainWebViewActivity.darkTheme) {
1318 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1320 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1323 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1327 // Update the `clearCachePreference` icon.
1328 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1329 if (MainWebViewActivity.darkTheme) {
1330 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1332 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1335 clearCachePreference.setIcon(R.drawable.cache_warning);
1339 case "clear_cookies":
1341 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1342 if (MainWebViewActivity.darkTheme) {
1343 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1345 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1348 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1352 case "clear_dom_storage":
1354 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1355 if (MainWebViewActivity.darkTheme) {
1356 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1358 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1361 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1365 // This section can be removed once the minimum API >= 26.
1366 case "clear_form_data":
1368 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1369 if (MainWebViewActivity.darkTheme) {
1370 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1372 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1375 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1381 if (sharedPreferences.getBoolean("clear_cache", true)) {
1382 if (MainWebViewActivity.darkTheme) {
1383 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1385 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1388 clearCachePreference.setIcon(R.drawable.cache_warning);
1393 // Set the new homepage URL as the summary text for the Homepage preference.
1394 homepagePreference.setSummary(sharedPreferences.getString("homepage", getString(R.string.homepage_default_value)));
1398 // Update the font size summary text.
1399 fontSizePreference.setSummary(sharedPreferences.getString("font_size", getString(R.string.font_size_default_value)) + "%%");
1402 case "swipe_to_refresh":
1404 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1405 if (MainWebViewActivity.darkTheme) {
1406 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1408 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1411 if (MainWebViewActivity.darkTheme) {
1412 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1414 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1419 case "scroll_app_bar":
1421 if (sharedPreferences.getBoolean("scroll_app_bar", true)) {
1422 if (MainWebViewActivity.darkTheme) {
1423 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_dark);
1425 scrollAppBarPreference.setIcon(R.drawable.app_bar_enabled_light);
1428 if (MainWebViewActivity.darkTheme) {
1429 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_dark);
1431 scrollAppBarPreference.setIcon(R.drawable.app_bar_disabled_light);
1436 case "display_additional_app_bar_icons":
1438 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1439 if (MainWebViewActivity.darkTheme) {
1440 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1442 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1445 if (MainWebViewActivity.darkTheme) {
1446 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1448 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1453 case "download_with_external_app":
1455 if (sharedPreferences.getBoolean("download_with_external_app", false)) {
1456 if (MainWebViewActivity.darkTheme) {
1457 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_dark);
1459 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_enabled_light);
1462 if (MainWebViewActivity.darkTheme) {
1463 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_dark);
1465 downloadWithExternalAppPreference.setIcon(R.drawable.open_with_external_app_disabled_light);
1472 if (sharedPreferences.getBoolean("dark_theme", false)) {
1473 darkThemePreference.setIcon(R.drawable.theme_dark);
1475 darkThemePreference.setIcon(R.drawable.theme_light);
1478 // Create an intent to restart Privacy Browser.
1479 Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1481 // Assert that the intent is not null to remove the lint error below.
1482 assert changeThemeRestartIntent != null;
1484 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`. TODO.
1485 changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1488 startActivity(changeThemeRestartIntent);
1492 // Set the URL to be reloaded on restart to apply the new night mode setting.
1493 MainWebViewActivity.loadUrlOnRestart = true;
1495 // Store the current night mode status.
1496 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1497 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript", false);
1500 if (currentNightModeBoolean) {
1501 if (MainWebViewActivity.darkTheme) {
1502 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1504 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1507 if (MainWebViewActivity.darkTheme) {
1508 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1510 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1514 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1515 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1516 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1518 // Update the `javaScriptPreference` icon.
1519 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1520 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1522 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1525 // Update the DOM storage preference icon.
1526 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1527 if (sharedPreferences.getBoolean("dom_storage", false)) { // DOM storage is enabled.
1528 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1529 } else { // DOM storage is disabled.
1530 if (MainWebViewActivity.darkTheme) {
1531 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1533 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1536 } else { // The preference is disabled. The icon should be ghosted.
1537 if (MainWebViewActivity.darkTheme) {
1538 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1540 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1545 case "display_webpage_images":
1546 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1548 if (MainWebViewActivity.darkTheme) {
1549 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1551 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1554 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1555 MainWebViewActivity.reloadOnRestart = false;
1558 if (MainWebViewActivity.darkTheme) {
1559 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1561 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1564 // Set `mainWebView` to reload on restart to remove the current images.
1565 MainWebViewActivity.reloadOnRestart = true;
1571 // Register the listener.
1572 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1575 // It is necessary to re-register the listener on every resume or it will randomly stop working because apps can be paused and resumed at any time, even while running in the foreground.
1577 public void onPause() {
1579 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1583 public void onResume() {
1585 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);