2 * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.fragments;
22 import android.annotation.SuppressLint;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.preference.Preference;
29 import android.preference.PreferenceCategory;
30 import android.preference.PreferenceFragment;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.webkit.WebView;
34 import android.widget.ArrayAdapter;
36 import com.stoutner.privacybrowser.R;
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 public class SettingsFragment extends PreferenceFragment {
40 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
41 private SharedPreferences savedPreferences;
44 public void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
46 addPreferencesFromResource(R.xml.preferences);
48 // Get a handle for the context.
49 Context context = getActivity().getApplicationContext();
51 // Initialize savedPreferences.
52 savedPreferences = getPreferenceScreen().getSharedPreferences();
54 // Get handles for the preferences.
55 final Preference javaScriptPreference = findPreference("javascript_enabled");
56 final Preference firstPartyCookiesPreference = findPreference("first_party_cookies_enabled");
57 final Preference thirdPartyCookiesPreference = findPreference("third_party_cookies_enabled");
58 final Preference domStoragePreference = findPreference("dom_storage_enabled");
59 final Preference saveFormDataPreference = findPreference("save_form_data_enabled"); // The form data preference can be removed once the minimum API >= 26.
60 final Preference userAgentPreference = findPreference("user_agent");
61 final Preference customUserAgentPreference = findPreference("custom_user_agent");
62 final Preference incognitoModePreference = findPreference("incognito_mode");
63 final Preference doNotTrackPreference = findPreference("do_not_track");
64 final Preference allowScreenshotsPreference = findPreference("allow_screenshots");
65 final Preference easyListPreference = findPreference("easylist");
66 final Preference easyPrivacyPreference = findPreference("easyprivacy");
67 final Preference fanboyAnnoyanceListPreference = findPreference("fanboy_annoyance_list");
68 final Preference fanboySocialBlockingListPreference = findPreference("fanboy_social_blocking_list");
69 final Preference blockAllThirdPartyRequestsPreference = findPreference("block_all_third_party_requests");
70 final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
71 final Preference torHomepagePreference = findPreference("tor_homepage");
72 final Preference torSearchPreference = findPreference("tor_search");
73 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
74 final Preference searchPreference = findPreference("search");
75 final Preference searchCustomURLPreference = findPreference("search_custom_url");
76 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
77 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
78 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
79 final Preference clearEverythingPreference = findPreference("clear_everything");
80 final Preference clearCookiesPreference = findPreference("clear_cookies");
81 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
82 final Preference clearFormDataPreference = findPreference("clear_form_data"); // The clear form data preference can be removed once the minimum API >= 26.
83 final Preference clearCachePreference = findPreference("clear_cache");
84 final Preference homepagePreference = findPreference("homepage");
85 final Preference defaultFontSizePreference = findPreference("default_font_size");
86 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
87 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
88 final Preference darkThemePreference = findPreference("dark_theme");
89 final Preference nightModePreference = findPreference("night_mode");
90 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
93 torHomepagePreference.setDependency("proxy_through_orbot");
94 torSearchPreference.setDependency("proxy_through_orbot");
95 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
97 // Get Strings from the preferences.
98 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
99 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
101 // Get booleans that are used in multiple places from the preferences.
102 final boolean javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
103 boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false);
104 boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false);
105 boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboy_annoyance_list", true);
106 boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboy_social_blocking_list", true);
107 boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
108 boolean fullScreenBrowsingMode = savedPreferences.getBoolean("full_screen_browsing_mode", false);
109 boolean hideSystemBars = savedPreferences.getBoolean("hide_system_bars", false);
110 boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
111 final boolean nightMode = savedPreferences.getBoolean("night_mode", false);
113 // Only enable the third-party cookies preference if first-party cookies are enabled and API >= 21.
114 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabled && (Build.VERSION.SDK_INT >= 21));
116 // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
117 domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
119 // Remove the form data preferences if the API is >= 26 as they no longer do anything.
120 if (Build.VERSION.SDK_INT >= 26) {
121 // Get the categories.
122 PreferenceCategory privacyCategory = (PreferenceCategory) findPreference("privacy");
123 PreferenceCategory clearAndExitCategory = (PreferenceCategory) findPreference("clear_and_exit");
125 // Remove the form data preferences.
126 privacyCategory.removePreference(saveFormDataPreference);
127 clearAndExitCategory.removePreference(clearFormDataPreference);
130 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
131 fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
133 // Inflate a WebView to get the default user agent.
134 LayoutInflater inflater = getActivity().getLayoutInflater();
135 // `@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.
136 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
137 final WebView bareWebView = bareWebViewLayout.findViewById(R.id.bare_webview);
139 // Get the user agent arrays.
140 ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(context, R.array.user_agent_names, R.layout.domain_settings_spinner_item);
141 String[] translatedUserAgentNamesArray = getResources().getStringArray(R.array.translated_user_agent_names);
142 String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
144 // Get the current user agent name from the preference.
145 String userAgentName = savedPreferences.getString("user_agent", "Privacy Browser");
147 // Get the array position of the user agent name.
148 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
150 // Populate the user agent summary.
151 switch (userAgentArrayPosition) {
152 case MainWebViewActivity.UNRECOGNIZED_USER_AGENT: // The user agent name is not on the canonical list.
153 // 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.
154 userAgentPreference.setSummary(userAgentName);
157 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
158 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
159 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + bareWebView.getSettings().getUserAgentString());
162 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
163 // Set the summary text.
164 userAgentPreference.setSummary(R.string.custom_user_agent);
168 // Get the user agent summary from the user agent data array.
169 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + userAgentDataArray[userAgentArrayPosition]);
172 // Set the summary text for the custom user agent preference and enable it if user agent preference is set to custom.
173 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
174 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals(getString(R.string.custom_user_agent)));
177 // Set the Tor homepage URL as the summary text for the `tor_homepage` preference when the preference screen is loaded. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
178 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
181 // Set the Tor search URL as the summary text for the Tor preference when the preference screen is loaded. The default is `https://3g2upl4pq6kufc4m.onion/html/?q=`
182 if (torSearchString.equals("Custom URL")) {
183 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
184 torSearchPreference.setSummary(R.string.custom_url);
186 // Set the array value as the summary text.
187 torSearchPreference.setSummary(torSearchString);
190 // Set the summary text for `tor_search_custom_url`. The default is `""`.
191 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
193 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
194 torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
197 // Set the search URL as the summary text for the search preference when the preference screen is loaded. The default is `https://duckduckgo.com/html/?q=`.
198 if (searchString.equals("Custom URL")) {
199 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
200 searchPreference.setSummary(R.string.custom_url);
202 // Set the array value as the summary text.
203 searchPreference.setSummary(searchString);
206 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
207 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
208 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
211 // Enable the translucent navigation bar preference only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
212 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingMode && !hideSystemBars);
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", "https://duckduckgo.com/?kao=-1&kak=-1"));
223 // Set the default font size as the summary text for the preference.
224 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
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_enabled", 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_enabled", 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 block all third-party requests icon.
428 if (savedPreferences.getBoolean("block_all_third_party_requests", false)) {
429 if (MainWebViewActivity.darkTheme) {
430 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
432 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
435 if (MainWebViewActivity.darkTheme) {
436 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
438 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
442 // Set the Tor icons according to the theme.
443 if (proxyThroughOrbot) { // Proxying is enabled.
444 if (MainWebViewActivity.darkTheme) {
445 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
446 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
447 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
449 // Set the custom search icon.
450 if (torSearchCustomURLPreference.isEnabled()) {
451 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
453 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
456 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
457 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
458 torSearchPreference.setIcon(R.drawable.search_enabled_light);
460 // Set the custom search icon.
461 if (torSearchCustomURLPreference.isEnabled()) {
462 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
464 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
467 } else { // Proxying is disabled.
468 if (MainWebViewActivity.darkTheme) {
469 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
470 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
471 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
472 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
474 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
475 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
476 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
477 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
481 // Set the search custom URL icon.
482 if (searchCustomURLPreference.isEnabled()) {
483 if (MainWebViewActivity.darkTheme) {
484 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
486 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
489 if (MainWebViewActivity.darkTheme) {
490 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
492 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
496 // Set the full screen browsing mode icons.
497 if (fullScreenBrowsingMode) { // Full screen browsing mode is enabled.
498 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
499 if (MainWebViewActivity.darkTheme) {
500 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
502 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
505 if (hideSystemBars) { // `hideSystemBarsBoolean` is `true`.
506 // Set the icons according to the theme.
507 if (MainWebViewActivity.darkTheme) {
508 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
509 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
511 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
512 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
514 } else { // `hideSystemBarsBoolean` is `false`.
515 // Set the `hideSystemBarsPreference` icon according to the theme.
516 if (MainWebViewActivity.darkTheme) {
517 // Set the icon for `hideSystemBarsPreference`.
518 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
520 // Set the icon for `translucentNavigationBarPreference`.
521 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
522 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
524 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
527 // Set the icon for `hideSystemBarsPreference`.
528 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
530 // Set the icon for `translucentNavigationBarPreference`.
531 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
532 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
534 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
538 } else { // Full screen browsing mode is disabled.
539 // Set the icons according to the theme.
540 if (MainWebViewActivity.darkTheme) {
541 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
542 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
543 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
545 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
546 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
547 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
551 // Set the clear everything preference icon.
552 if (clearEverything) {
553 if (MainWebViewActivity.darkTheme) {
554 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
556 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
559 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
562 // Set the clear cookies preference icon.
563 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
564 if (MainWebViewActivity.darkTheme) {
565 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
567 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
570 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
573 // Set the clear DOM storage preference icon.
574 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
575 if (MainWebViewActivity.darkTheme) {
576 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
578 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
581 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
584 // Set the clear form data preference icon if the API < 26. It has no effect on newer versions of Android.
585 if (Build.VERSION.SDK_INT < 26) {
586 if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
587 if (MainWebViewActivity.darkTheme) {
588 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
590 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
593 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
597 // Set the clear cache preference icon.
598 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
599 if (MainWebViewActivity.darkTheme) {
600 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
602 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
605 clearCachePreference.setIcon(R.drawable.cache_warning);
608 // Set the swipe to refresh preference icon.
609 if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
610 if (MainWebViewActivity.darkTheme) {
611 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
613 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
616 if (MainWebViewActivity.darkTheme) {
617 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
619 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
623 // Set the display additional app bar icons preference icon.
624 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
625 if (MainWebViewActivity.darkTheme) {
626 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
628 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
631 if (MainWebViewActivity.darkTheme) {
632 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
634 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
638 // Set the dark theme preference icon.
639 if (savedPreferences.getBoolean("dark_theme", false)) {
640 darkThemePreference.setIcon(R.drawable.theme_dark);
642 darkThemePreference.setIcon(R.drawable.theme_light);
645 // Set the night mode preference icon.
647 if (MainWebViewActivity.darkTheme) {
648 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
650 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
653 if (MainWebViewActivity.darkTheme) {
654 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
656 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
660 // Set the display webpage images preference icon.
661 if (savedPreferences.getBoolean("display_webpage_images", true)) {
662 if (MainWebViewActivity.darkTheme) {
663 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
665 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
668 if (MainWebViewActivity.darkTheme) {
669 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
671 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
676 // Listen for preference changes.
677 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
679 case "javascript_enabled":
680 // Update the icons and the DOM storage preference status.
681 if (sharedPreferences.getBoolean("javascript_enabled", false)) { // The JavaScript preference is enabled.
682 // Update the icon for the JavaScript preference.
683 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
685 // Update the status of the DOM storage preference.
686 domStoragePreference.setEnabled(true);
688 // Update the icon for the DOM storage preference.
689 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
690 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
692 if (MainWebViewActivity.darkTheme) {
693 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
695 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
698 } else { // The JavaScript preference is disabled.
699 // Update the icon for the JavaScript preference.
700 javaScriptPreference.setIcon(R.drawable.privacy_mode);
702 // Update the status of the DOM storage preference.
703 domStoragePreference.setEnabled(false);
705 // Set the icon for DOM storage preference to be ghosted.
706 if (MainWebViewActivity.darkTheme) {
707 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
709 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
714 case "first_party_cookies_enabled":
715 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
716 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
717 // Set the icon for `first_party_cookies_enabled`.
718 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
720 // Update the icon for `third_party_cookies_enabled`.
721 if (Build.VERSION.SDK_INT >= 21) {
722 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
723 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
725 if (MainWebViewActivity.darkTheme) {
726 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
728 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
732 if (MainWebViewActivity.darkTheme) {
733 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
735 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
738 } else { // `first_party_cookies_enabled` is `false`.
739 // Update the icon for `first_party_cookies_enabled`.
740 if (MainWebViewActivity.darkTheme) {
741 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
743 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
746 // Set the icon for `third_party_cookies_enabled` to be ghosted.
747 if (MainWebViewActivity.darkTheme) {
748 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
750 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
754 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
755 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
758 case "third_party_cookies_enabled":
760 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
761 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
763 if (MainWebViewActivity.darkTheme) {
764 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
766 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
771 case "dom_storage_enabled":
773 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
774 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
776 if (MainWebViewActivity.darkTheme) {
777 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
779 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
784 // Save form data can be removed once the minimum API >= 26.
785 case "save_form_data_enabled":
787 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
788 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
790 if (MainWebViewActivity.darkTheme) {
791 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
793 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
798 // Get the new user agent name.
799 String newUserAgentName = sharedPreferences.getString("user_agent", "Privacy Browser");
801 // Get the array position for the new user agent name.
802 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
804 // Get the translated new user agent name.
805 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
807 // Populate the user agent summary.
808 switch (newUserAgentArrayPosition) {
809 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
810 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
811 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
813 // Disable the custom user agent preference.
814 customUserAgentPreference.setEnabled(false);
816 // Set the custom user agent preference icon according to the theme.
817 if (MainWebViewActivity.darkTheme) {
818 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
820 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
824 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
825 // Set the summary text.
826 userAgentPreference.setSummary(R.string.custom_user_agent);
828 // Enable the custom user agent preference.
829 customUserAgentPreference.setEnabled(true);
831 // Set the custom user agent preference icon according to the theme.
832 if (MainWebViewActivity.darkTheme) {
833 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
835 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
840 // Get the user agent summary from the user agent data array.
841 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
843 // Disable the custom user agent preference.
844 customUserAgentPreference.setEnabled(false);
846 // Set the custom user agent preference icon according to the theme.
847 if (MainWebViewActivity.darkTheme) {
848 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
850 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
855 case "custom_user_agent":
856 // Set the new custom user agent as the summary text for the preference.
857 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
860 case "incognito_mode":
862 if (sharedPreferences.getBoolean("incognito_mode", false)) {
863 if (MainWebViewActivity.darkTheme) {
864 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
866 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
869 if (MainWebViewActivity.darkTheme) {
870 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
872 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
879 if (sharedPreferences.getBoolean("do_not_track", false)) {
880 if (MainWebViewActivity.darkTheme) {
881 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
883 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
886 if (MainWebViewActivity.darkTheme) {
887 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
889 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
895 case "allow_screenshots":
897 if (sharedPreferences.getBoolean("allow_screenshots", false)) {
898 if (MainWebViewActivity.darkTheme) {
899 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
901 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
904 if (MainWebViewActivity.darkTheme) {
905 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
907 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
911 // Create an intent to restart Privacy Browser.
912 Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
914 // Assert that the intent is not null to remove the lint error below.
915 assert allowScreenshotsRestartIntent != null;
917 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
918 allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
921 startActivity(allowScreenshotsRestartIntent);
926 if (sharedPreferences.getBoolean("easylist", true)) {
927 if (MainWebViewActivity.darkTheme) {
928 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
930 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
933 if (MainWebViewActivity.darkTheme) {
934 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
936 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
943 if (sharedPreferences.getBoolean("easyprivacy", true)) {
944 if (MainWebViewActivity.darkTheme) {
945 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
947 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
950 if (MainWebViewActivity.darkTheme) {
951 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
953 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
958 case "fanboy_annoyance_list":
959 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboy_annoyance_list", true);
960 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboy_social_blocking_list", true);
962 // Update the Fanboy icons.
963 if (currentFanboyAnnoyanceList) { // Fanboy's annoyance list is enabled.
964 if (MainWebViewActivity.darkTheme) {
965 // Update the Fanboy's annoyance list icon.
966 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
968 // Update the Fanboy's social blocking list icon.
969 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
971 // Update the Fanboy's annoyance list icon.
972 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
974 // Update the Fanboy's social blocking list icon.
975 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
977 } else { // Fanboy's annoyance list is disabled.
978 if (MainWebViewActivity.darkTheme) {
979 // Update the Fanboy's annoyance list icon.
980 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
982 // Update the Fanboy's social blocking list icon.
983 if (currentFanboySocialBlockingList) {
984 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
986 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
989 // Update the Fanboy's annoyance list icon.
990 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
992 // Update the Fanboy's social blocking list icon.
993 if (currentFanboySocialBlockingList) {
994 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
996 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1001 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
1002 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
1005 case "fanboy_social_blocking_list":
1007 if (sharedPreferences.getBoolean("fanboy_social_blocking_list", true)) {
1008 if (MainWebViewActivity.darkTheme) {
1009 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
1011 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
1014 if (MainWebViewActivity.darkTheme) {
1015 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1017 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1022 case "block_all_third_party_requests":
1024 if (sharedPreferences.getBoolean("block_all_third_party_requests", false)) {
1025 if (MainWebViewActivity.darkTheme) {
1026 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_dark);
1028 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_enabled_light);
1031 if (MainWebViewActivity.darkTheme) {
1032 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_dark);
1034 blockAllThirdPartyRequestsPreference.setIcon(R.drawable.block_all_third_party_requests_disabled_light);
1039 case "proxy_through_orbot":
1040 // Get current settings.
1041 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
1042 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
1044 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
1045 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
1047 // Update the icons.
1048 if (currentProxyThroughOrbot) {
1049 // Set the Tor icons according to the theme.
1050 if (MainWebViewActivity.darkTheme) {
1051 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1052 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1053 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1055 // Set the `torSearchCustomURLPreference` icon.
1056 if (torSearchCustomURLPreference.isEnabled()) {
1057 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1059 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1062 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1063 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1064 torSearchPreference.setIcon(R.drawable.search_enabled_light);
1066 // Set the `torSearchCustomURLPreference` icon.
1067 if (torSearchCustomURLPreference.isEnabled()) {
1068 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1070 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1073 } else { // Proxy through Orbot is disabled.
1074 if (MainWebViewActivity.darkTheme) {
1075 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1076 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1077 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1078 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1080 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1081 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1082 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1083 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1088 case "tor_homepage":
1089 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
1090 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
1094 // Get the present search string.
1095 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
1097 // Update the preferences.
1098 if (presentTorSearchString.equals("Custom URL")) {
1099 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1100 torSearchPreference.setSummary(R.string.custom_url);
1102 // Enable `torSearchCustomURLPreference`.
1103 torSearchCustomURLPreference.setEnabled(true);
1105 // Update the `torSearchCustomURLPreference` icon.
1106 if (MainWebViewActivity.darkTheme) {
1107 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1109 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1112 // Set the array value as the summary text.
1113 torSearchPreference.setSummary(presentTorSearchString);
1115 // Disable `torSearchCustomURLPreference`.
1116 torSearchCustomURLPreference.setEnabled(false);
1118 // Update the `torSearchCustomURLPreference` icon.
1119 if (MainWebViewActivity.darkTheme) {
1120 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1122 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1127 case "tor_search_custom_url":
1128 // Set the summary text for `tor_search_custom_url`.
1129 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
1133 // Store the new search string.
1134 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
1136 // Update `searchPreference` and `searchCustomURLPreference`.
1137 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1138 // Set the summary text to `R.string.custom_url`, which is translated.
1139 searchPreference.setSummary(R.string.custom_url);
1141 // Enable `searchCustomURLPreference`.
1142 searchCustomURLPreference.setEnabled(true);
1144 // Set the `searchCustomURLPreference` according to the theme.
1145 if (MainWebViewActivity.darkTheme) {
1146 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1148 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1150 } else { // `Custom URL` is not selected.
1151 // Set the summary text to `newSearchString`.
1152 searchPreference.setSummary(newSearchString);
1154 // Disable `searchCustomURLPreference`.
1155 searchCustomURLPreference.setEnabled(false);
1157 // Set the `searchCustomURLPreference` according to the theme.
1158 if (MainWebViewActivity.darkTheme) {
1159 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1161 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1166 case "search_custom_url":
1167 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1168 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
1171 case "full_screen_browsing_mode":
1172 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
1173 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
1174 if (MainWebViewActivity.darkTheme) {
1175 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1177 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1180 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
1181 // Disable `translucentNavigationBarPreference`.
1182 translucentNavigationBarPreference.setEnabled(false);
1184 // Set the icons according to the theme.
1185 if (MainWebViewActivity.darkTheme) {
1186 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1187 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1189 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1190 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1192 } else { // `hide_system_bars` is `false`.
1193 // Enable `translucentNavigationBarPreference`.
1194 translucentNavigationBarPreference.setEnabled(true);
1196 // Set the icons according to the theme.
1197 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
1198 // Set the `hideSystemBarsPreference` icon.
1199 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1201 // Set the `translucentNavigationBarPreference` icon.
1202 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1203 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1205 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1207 } else { // Use the light theme.
1208 // Set the `hideSystemBarsPreference` icon.
1209 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1211 // Set the `translucentNavigationBarPreference` icon.
1212 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1213 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1215 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1219 } else { // `full_screen_browsing_mode` is false.
1220 // Disable `translucentNavigationBarPreference`.
1221 translucentNavigationBarPreference.setEnabled(false);
1223 // Update the icons according to the theme.
1224 if (MainWebViewActivity.darkTheme) {
1225 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1226 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
1227 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1229 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1230 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
1231 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1236 case "hide_system_bars":
1237 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
1238 // Disable `translucentNavigationBarPreference`.
1239 translucentNavigationBarPreference.setEnabled(false);
1241 // Set the icons according to the theme.
1242 if (MainWebViewActivity.darkTheme) {
1243 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1244 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1246 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1247 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1249 } else { // `hide_system_bars` is false.
1250 // Enable `translucentNavigationBarPreference`.
1251 translucentNavigationBarPreference.setEnabled(true);
1253 // Set the icons according to the theme.
1254 if (MainWebViewActivity.darkTheme) {
1255 // Set the `hideSystemBarsPreference` icon.
1256 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1258 // Set the `translucentNavigationBarPreference` icon.
1259 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1260 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1262 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1265 // Set the `hideSystemBarsPreference` icon.
1266 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1268 // Set the `translucentNavigationBarPreference` icon.
1269 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1270 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1272 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1278 case "translucent_navigation_bar":
1280 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1281 if (MainWebViewActivity.darkTheme) {
1282 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1284 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1287 if (MainWebViewActivity.darkTheme) {
1288 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1290 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1295 case "clear_everything":
1296 // Store the new `clear_everything` status
1297 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1299 // Update the status of the `Clear and Exit` preferences.
1300 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1301 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1302 clearFormDataPreference.setEnabled(!newClearEverythingBoolean); // This line can be removed once the minimum API >= 26.
1303 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1305 // Update the `clearEverythingPreference` icon.
1306 if (newClearEverythingBoolean) {
1307 if (MainWebViewActivity.darkTheme) {
1308 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1310 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1313 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1316 // Update the `clearCookiesPreference` icon.
1317 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1318 if (MainWebViewActivity.darkTheme) {
1319 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1321 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1324 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1327 // Update the `clearDomStoragePreference` icon.
1328 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1329 if (MainWebViewActivity.darkTheme) {
1330 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1332 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1335 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1338 // Update the clear form data preference icon if the API < 26.
1339 if (Build.VERSION.SDK_INT < 26) {
1340 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1341 if (MainWebViewActivity.darkTheme) {
1342 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1344 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1347 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1351 // Update the `clearCachePreference` icon.
1352 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1353 if (MainWebViewActivity.darkTheme) {
1354 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1356 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1359 clearCachePreference.setIcon(R.drawable.cache_warning);
1363 case "clear_cookies":
1365 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1366 if (MainWebViewActivity.darkTheme) {
1367 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1369 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1372 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1376 case "clear_dom_storage":
1378 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1379 if (MainWebViewActivity.darkTheme) {
1380 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1382 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1385 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1389 // This section can be removed once the minimum API >= 26.
1390 case "clear_form_data":
1392 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1393 if (MainWebViewActivity.darkTheme) {
1394 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1396 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1399 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1405 if (sharedPreferences.getBoolean("clear_cache", true)) {
1406 if (MainWebViewActivity.darkTheme) {
1407 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1409 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1412 clearCachePreference.setIcon(R.drawable.cache_warning);
1417 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
1418 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
1421 case "default_font_size":
1422 // Update the summary text of `default_font_size`.
1423 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1426 case "swipe_to_refresh":
1428 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1429 if (MainWebViewActivity.darkTheme) {
1430 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1432 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1435 if (MainWebViewActivity.darkTheme) {
1436 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1438 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1443 case "display_additional_app_bar_icons":
1445 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1446 if (MainWebViewActivity.darkTheme) {
1447 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1449 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1452 if (MainWebViewActivity.darkTheme) {
1453 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1455 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1462 if (sharedPreferences.getBoolean("dark_theme", false)) {
1463 darkThemePreference.setIcon(R.drawable.theme_dark);
1465 darkThemePreference.setIcon(R.drawable.theme_light);
1468 // Create an intent to restart Privacy Browser.
1469 Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1471 // Assert that the intent is not null to remove the lint error below.
1472 assert changeThemeRestartIntent != null;
1474 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1475 changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1478 startActivity(changeThemeRestartIntent);
1482 // Set the URL to be reloaded on restart to apply the new night mode setting.
1483 MainWebViewActivity.loadUrlOnRestart = true;
1485 // Store the current night mode status.
1486 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1487 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript_enabled", false);
1490 if (currentNightModeBoolean) {
1491 if (MainWebViewActivity.darkTheme) {
1492 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1494 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1497 if (MainWebViewActivity.darkTheme) {
1498 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1500 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1504 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1505 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1506 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1508 // Update the `javaScriptPreference` icon.
1509 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1510 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1512 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1515 // Update the `domStoragePreference` icon.
1516 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1517 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
1518 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1519 } else { // DOM storage is disabled.
1520 if (MainWebViewActivity.darkTheme) {
1521 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1523 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1526 } else { // The preference is disabled. The icon should be ghosted.
1527 if (MainWebViewActivity.darkTheme) {
1528 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1530 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1535 case "display_webpage_images":
1536 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1538 if (MainWebViewActivity.darkTheme) {
1539 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1541 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1544 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1545 MainWebViewActivity.reloadOnRestart = false;
1548 if (MainWebViewActivity.darkTheme) {
1549 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1551 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1554 // Set `mainWebView` to reload on restart to remove the current images.
1555 MainWebViewActivity.reloadOnRestart = true;
1561 // Register the listener.
1562 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1565 // 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
1566 // even while running in the foreground.
1568 public void onPause() {
1570 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1574 public void onResume() {
1576 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);