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 we need to modify.
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 proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
70 final Preference torHomepagePreference = findPreference("tor_homepage");
71 final Preference torSearchPreference = findPreference("tor_search");
72 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
73 final Preference searchPreference = findPreference("search");
74 final Preference searchCustomURLPreference = findPreference("search_custom_url");
75 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
76 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
77 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
78 final Preference clearEverythingPreference = findPreference("clear_everything");
79 final Preference clearCookiesPreference = findPreference("clear_cookies");
80 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
81 final Preference clearFormDataPreference = findPreference("clear_form_data"); // The clear form data preference can be removed once the minimum API >= 26.
82 final Preference clearCachePreference = findPreference("clear_cache");
83 final Preference homepagePreference = findPreference("homepage");
84 final Preference defaultFontSizePreference = findPreference("default_font_size");
85 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
86 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
87 final Preference darkThemePreference = findPreference("dark_theme");
88 final Preference nightModePreference = findPreference("night_mode");
89 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
92 torHomepagePreference.setDependency("proxy_through_orbot");
93 torSearchPreference.setDependency("proxy_through_orbot");
94 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
96 // Get Strings from the preferences.
97 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
98 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
100 // Get booleans that are used in multiple places from the preferences.
101 final boolean javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
102 boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false);
103 boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false);
104 boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboy_annoyance_list", true);
105 boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboy_social_blocking_list", true);
106 boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
107 boolean fullScreenBrowsingMode = savedPreferences.getBoolean("full_screen_browsing_mode", false);
108 boolean hideSystemBars = savedPreferences.getBoolean("hide_system_bars", false);
109 boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
110 final boolean nightMode = savedPreferences.getBoolean("night_mode", false);
112 // Only enable the third-party cookies preference if first-party cookies are enabled and API >= 21.
113 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabled && (Build.VERSION.SDK_INT >= 21));
115 // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
116 domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
118 // Remove the form data preferences if the API is >= 26 as they no longer do anything.
119 if (Build.VERSION.SDK_INT >= 26) {
120 // Get the categories.
121 PreferenceCategory privacyCategory = (PreferenceCategory) findPreference("privacy");
122 PreferenceCategory clearAndExitCategory = (PreferenceCategory) findPreference("clear_and_exit");
124 // Remove the form data preferences.
125 privacyCategory.removePreference(saveFormDataPreference);
126 clearAndExitCategory.removePreference(clearFormDataPreference);
129 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
130 fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
132 // Inflate a WebView to get the default user agent.
133 LayoutInflater inflater = getActivity().getLayoutInflater();
134 // `@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.
135 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
136 final WebView bareWebView = bareWebViewLayout.findViewById(R.id.bare_webview);
138 // Get the user agent arrays.
139 ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(context, R.array.user_agent_names, R.layout.domain_settings_spinner_item);
140 String[] translatedUserAgentNamesArray = getResources().getStringArray(R.array.translated_user_agent_names);
141 String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
143 // Get the current user agent name from the preference.
144 String userAgentName = savedPreferences.getString("user_agent", "Privacy Browser");
146 // Get the array position of the user agent name.
147 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
149 // Populate the user agent summary.
150 switch (userAgentArrayPosition) {
151 case MainWebViewActivity.UNRECOGNIZED_USER_AGENT: // The user agent name is not on the canonical list.
152 // 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.
153 userAgentPreference.setSummary(userAgentName);
156 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
157 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
158 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + bareWebView.getSettings().getUserAgentString());
161 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
162 // Set the summary text.
163 userAgentPreference.setSummary(R.string.custom_user_agent);
167 // Get the user agent summary from the user agent data array.
168 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + userAgentDataArray[userAgentArrayPosition]);
171 // Set the summary text for the custom user agent preference and enable it if user agent preference is set to custom.
172 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
173 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals(getString(R.string.custom_user_agent)));
176 // 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`.
177 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
180 // 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=`
181 if (torSearchString.equals("Custom URL")) {
182 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
183 torSearchPreference.setSummary(R.string.custom_url);
185 // Set the array value as the summary text.
186 torSearchPreference.setSummary(torSearchString);
189 // Set the summary text for `tor_search_custom_url`. The default is `""`.
190 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
192 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
193 torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
196 // 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=`.
197 if (searchString.equals("Custom URL")) {
198 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
199 searchPreference.setSummary(R.string.custom_url);
201 // Set the array value as the summary text.
202 searchPreference.setSummary(searchString);
205 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
206 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
207 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
210 // Enable the translucent navigation bar preference only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
211 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingMode && !hideSystemBars);
213 // Set the status of the `Clear and Exit` preferences.
214 clearCookiesPreference.setEnabled(!clearEverything);
215 clearDomStoragePreference.setEnabled(!clearEverything);
216 clearFormDataPreference.setEnabled(!clearEverything); // The form data line can be removed once the minimum API is >= 26.
217 clearCachePreference.setEnabled(!clearEverything);
219 // Set the homepage URL as the summary text for the homepage preference.
220 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com/?kao=-1&kak=-1"));
222 // Set the default font size as the summary text for the preference.
223 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
225 // Disable the JavaScript preference if Night Mode is enabled. JavaScript will be enabled for all web pages.
226 javaScriptPreference.setEnabled(!nightMode);
228 // Set the JavaScript icon.
229 if (javaScriptEnabled || nightMode) {
230 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
232 javaScriptPreference.setIcon(R.drawable.privacy_mode);
235 // Set the first-party cookies icon.
236 if (firstPartyCookiesEnabled) {
237 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
239 if (MainWebViewActivity.darkTheme) {
240 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
242 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
246 // Set the third party cookies icon.
247 if (firstPartyCookiesEnabled && Build.VERSION.SDK_INT >= 21) {
248 if (thirdPartyCookiesEnabled) {
249 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
251 if (MainWebViewActivity.darkTheme) {
252 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
254 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
258 if (MainWebViewActivity.darkTheme) {
259 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
261 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
265 // Set the DOM storage icon.
266 if (javaScriptEnabled || nightMode) { // The preference is enabled.
267 if (savedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
268 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
269 } else { // DOM storage is disabled.
270 if (MainWebViewActivity.darkTheme) {
271 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
273 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
276 } else { // The preference is disabled. The icon should be ghosted.
277 if (MainWebViewActivity.darkTheme) {
278 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
280 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
284 // Set the save form data icon if API < 26. Save form data has no effect on API >= 26.
285 if (Build.VERSION.SDK_INT < 26) {
286 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
287 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
289 if (MainWebViewActivity.darkTheme) {
290 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
292 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
297 // Set the custom user agent icon.
298 if (customUserAgentPreference.isEnabled()) {
299 if (MainWebViewActivity.darkTheme) {
300 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
302 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
305 if (MainWebViewActivity.darkTheme) {
306 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
308 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
312 // Set the incognito mode icon.
313 if (savedPreferences.getBoolean("incognito_mode", false)) {
314 if (MainWebViewActivity.darkTheme) {
315 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
317 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
320 if (MainWebViewActivity.darkTheme) {
321 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
323 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
327 // Set the Do Not Track icon.
328 if (savedPreferences.getBoolean("do_not_track", false)) {
329 if (MainWebViewActivity.darkTheme) {
330 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
332 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
335 if (MainWebViewActivity.darkTheme) {
336 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
338 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
342 // Set the allow screenshots icon.
343 if (savedPreferences.getBoolean("allow_screenshots", false)) {
344 if (MainWebViewActivity.darkTheme) {
345 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
347 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
350 if (MainWebViewActivity.darkTheme) {
351 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
353 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
357 // Set the EasyList icon.
358 if (savedPreferences.getBoolean("easylist", true)) {
359 if (MainWebViewActivity.darkTheme) {
360 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
362 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
365 if (MainWebViewActivity.darkTheme) {
366 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
368 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
372 // Set the EasyPrivacy icon.
373 if (savedPreferences.getBoolean("easyprivacy", true)) {
374 if (MainWebViewActivity.darkTheme) {
375 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
377 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
380 if (MainWebViewActivity.darkTheme) {
381 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
383 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
387 // Set the Fanboy lists icons.
388 if (fanboyAnnoyanceListEnabled) {
389 if (MainWebViewActivity.darkTheme) {
390 // Set the Fanboy annoyance list icon.
391 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
393 // Set the Fanboy social blocking list icon.
394 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
396 // Set the Fanboy annoyance list icon.
397 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
399 // Set the Fanboy social blocking list icon.
400 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
403 if (MainWebViewActivity.darkTheme) {
404 // Set the Fanboy annoyance list icon.
405 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
407 // Set the Fanboy social blocking list icon.
408 if (fanboySocialBlockingEnabled) {
409 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
411 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
414 // Set the Fanboy annoyance list icon.
415 fanboyAnnoyanceListPreference.setIcon(R.drawable.block_ads_disabled_light);
417 // Set the Fanboy social blocking list icon.
418 if (fanboySocialBlockingEnabled) {
419 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
421 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
426 // Set the Tor icons according to the theme.
427 if (proxyThroughOrbot) { // Proxying is enabled.
428 if (MainWebViewActivity.darkTheme) {
429 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
430 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
431 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
433 // Set the custom search icon.
434 if (torSearchCustomURLPreference.isEnabled()) {
435 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
437 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
440 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
441 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
442 torSearchPreference.setIcon(R.drawable.search_enabled_light);
444 // Set the custom search icon.
445 if (torSearchCustomURLPreference.isEnabled()) {
446 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
448 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
451 } else { // Proxying is disabled.
452 if (MainWebViewActivity.darkTheme) {
453 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
454 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
455 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
456 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
458 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
459 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
460 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
461 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
465 // Set the search custom URL icon.
466 if (searchCustomURLPreference.isEnabled()) {
467 if (MainWebViewActivity.darkTheme) {
468 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
470 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
473 if (MainWebViewActivity.darkTheme) {
474 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
476 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
480 // Set the full screen browsing mode icons.
481 if (fullScreenBrowsingMode) { // Full screen browsing mode is enabled.
482 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
483 if (MainWebViewActivity.darkTheme) {
484 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
486 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
489 if (hideSystemBars) { // `hideSystemBarsBoolean` is `true`.
490 // Set the icons according to the theme.
491 if (MainWebViewActivity.darkTheme) {
492 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
493 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
495 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
496 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
498 } else { // `hideSystemBarsBoolean` is `false`.
499 // Set the `hideSystemBarsPreference` icon according to the theme.
500 if (MainWebViewActivity.darkTheme) {
501 // Set the icon for `hideSystemBarsPreference`.
502 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
504 // Set the icon for `translucentNavigationBarPreference`.
505 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
506 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
508 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
511 // Set the icon for `hideSystemBarsPreference`.
512 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
514 // Set the icon for `translucentNavigationBarPreference`.
515 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
516 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
518 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
522 } else { // Full screen browsing mode is disabled.
523 // Set the icons according to the theme.
524 if (MainWebViewActivity.darkTheme) {
525 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
526 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
527 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
529 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
530 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
531 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
535 // Set the clear everything preference icon.
536 if (clearEverything) {
537 if (MainWebViewActivity.darkTheme) {
538 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
540 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
543 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
546 // Set the clear cookies preference icon.
547 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
548 if (MainWebViewActivity.darkTheme) {
549 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
551 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
554 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
557 // Set the clear DOM storage preference icon.
558 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
559 if (MainWebViewActivity.darkTheme) {
560 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
562 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
565 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
568 // Set the clear form data preference icon if the API < 26. It has no effect on newer versions of Android.
569 if (Build.VERSION.SDK_INT < 26) {
570 if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
571 if (MainWebViewActivity.darkTheme) {
572 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
574 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
577 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
581 // Set the clear cache preference icon.
582 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
583 if (MainWebViewActivity.darkTheme) {
584 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
586 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
589 clearCachePreference.setIcon(R.drawable.cache_warning);
592 // Set the swipe to refresh preference icon.
593 if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
594 if (MainWebViewActivity.darkTheme) {
595 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
597 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
600 if (MainWebViewActivity.darkTheme) {
601 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
603 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
607 // Set the display additional app bar icons preference icon.
608 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
609 if (MainWebViewActivity.darkTheme) {
610 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
612 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
615 if (MainWebViewActivity.darkTheme) {
616 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
618 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
622 // Set the dark theme preference icon.
623 if (savedPreferences.getBoolean("dark_theme", false)) {
624 darkThemePreference.setIcon(R.drawable.theme_dark);
626 darkThemePreference.setIcon(R.drawable.theme_light);
629 // Set the night mode preference icon.
631 if (MainWebViewActivity.darkTheme) {
632 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
634 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
637 if (MainWebViewActivity.darkTheme) {
638 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
640 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
644 // Set the display webpage images preference icon.
645 if (savedPreferences.getBoolean("display_webpage_images", true)) {
646 if (MainWebViewActivity.darkTheme) {
647 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
649 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
652 if (MainWebViewActivity.darkTheme) {
653 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
655 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
660 // Listen for preference changes.
661 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
663 case "javascript_enabled":
664 // Update the icons and the DOM storage preference status.
665 if (sharedPreferences.getBoolean("javascript_enabled", false)) { // The JavaScript preference is enabled.
666 // Update the icon for the JavaScript preference.
667 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
669 // Update the status of the DOM storage preference.
670 domStoragePreference.setEnabled(true);
672 // Update the icon for the DOM storage preference.
673 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
674 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
676 if (MainWebViewActivity.darkTheme) {
677 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
679 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
682 } else { // The JavaScript preference is disabled.
683 // Update the icon for the JavaScript preference.
684 javaScriptPreference.setIcon(R.drawable.privacy_mode);
686 // Update the status of the DOM storage preference.
687 domStoragePreference.setEnabled(false);
689 // Set the icon for DOM storage preference to be ghosted.
690 if (MainWebViewActivity.darkTheme) {
691 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
693 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
698 case "first_party_cookies_enabled":
699 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
700 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
701 // Set the icon for `first_party_cookies_enabled`.
702 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
704 // Update the icon for `third_party_cookies_enabled`.
705 if (Build.VERSION.SDK_INT >= 21) {
706 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
707 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
709 if (MainWebViewActivity.darkTheme) {
710 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
712 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
716 if (MainWebViewActivity.darkTheme) {
717 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
719 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
722 } else { // `first_party_cookies_enabled` is `false`.
723 // Update the icon for `first_party_cookies_enabled`.
724 if (MainWebViewActivity.darkTheme) {
725 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
727 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
730 // Set the icon for `third_party_cookies_enabled` to be ghosted.
731 if (MainWebViewActivity.darkTheme) {
732 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
734 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
738 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
739 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
742 case "third_party_cookies_enabled":
744 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
745 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
747 if (MainWebViewActivity.darkTheme) {
748 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
750 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
755 case "dom_storage_enabled":
757 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
758 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
760 if (MainWebViewActivity.darkTheme) {
761 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
763 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
768 // Save form data can be removed once the minimum API >= 26.
769 case "save_form_data_enabled":
771 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
772 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
774 if (MainWebViewActivity.darkTheme) {
775 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
777 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
782 // Get the new user agent name.
783 String newUserAgentName = sharedPreferences.getString("user_agent", "Privacy Browser");
785 // Get the array position for the new user agent name.
786 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
788 // Get the translated new user agent name.
789 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
791 // Populate the user agent summary.
792 switch (newUserAgentArrayPosition) {
793 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
794 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
795 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
797 // Disable the custom user agent preference.
798 customUserAgentPreference.setEnabled(false);
800 // Set the custom user agent preference icon according to the theme.
801 if (MainWebViewActivity.darkTheme) {
802 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
804 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
808 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
809 // Set the summary text.
810 userAgentPreference.setSummary(R.string.custom_user_agent);
812 // Enable the custom user agent preference.
813 customUserAgentPreference.setEnabled(true);
815 // Set the custom user agent preference icon according to the theme.
816 if (MainWebViewActivity.darkTheme) {
817 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
819 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
824 // Get the user agent summary from the user agent data array.
825 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
827 // Disable the custom user agent preference.
828 customUserAgentPreference.setEnabled(false);
830 // Set the custom user agent preference icon according to the theme.
831 if (MainWebViewActivity.darkTheme) {
832 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
834 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
839 case "custom_user_agent":
840 // Set the new custom user agent as the summary text for the preference.
841 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
844 case "incognito_mode":
846 if (sharedPreferences.getBoolean("incognito_mode", false)) {
847 if (MainWebViewActivity.darkTheme) {
848 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
850 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
853 if (MainWebViewActivity.darkTheme) {
854 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
856 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
863 if (sharedPreferences.getBoolean("do_not_track", false)) {
864 if (MainWebViewActivity.darkTheme) {
865 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
867 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
870 if (MainWebViewActivity.darkTheme) {
871 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
873 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
879 case "allow_screenshots":
881 if (sharedPreferences.getBoolean("allow_screenshots", false)) {
882 if (MainWebViewActivity.darkTheme) {
883 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
885 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
888 if (MainWebViewActivity.darkTheme) {
889 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
891 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
895 // Create an intent to restart Privacy Browser.
896 Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
898 // Assert that the intent is not null to remove the lint error below.
899 assert allowScreenshotsRestartIntent != null;
901 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
902 allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
905 startActivity(allowScreenshotsRestartIntent);
910 if (sharedPreferences.getBoolean("easylist", true)) {
911 if (MainWebViewActivity.darkTheme) {
912 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
914 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
917 if (MainWebViewActivity.darkTheme) {
918 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
920 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
927 if (sharedPreferences.getBoolean("easyprivacy", true)) {
928 if (MainWebViewActivity.darkTheme) {
929 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
931 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
934 if (MainWebViewActivity.darkTheme) {
935 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
937 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
942 case "fanboy_annoyance_list":
943 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboy_annoyance_list", true);
944 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboy_social_blocking_list", true);
946 // Update the Fanboy icons.
947 if (currentFanboyAnnoyanceList) { // Fanboy's annoyance list is enabled.
948 if (MainWebViewActivity.darkTheme) {
949 // Update the Fanboy's annoyance list icon.
950 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
952 // Update the Fanboy's social blocking list icon.
953 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
955 // Update the Fanboy's annoyance list icon.
956 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
958 // Update the Fanboy's social blocking list icon.
959 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
961 } else { // Fanboy's annoyance list is disabled.
962 if (MainWebViewActivity.darkTheme) {
963 // Update the Fanboy's annoyance list icon.
964 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
966 // Update the Fanboy's social blocking list icon.
967 if (currentFanboySocialBlockingList) {
968 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
970 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
973 // Update the Fanboy's annoyance list icon.
974 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
976 // Update the Fanboy's social blocking list icon.
977 if (currentFanboySocialBlockingList) {
978 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
980 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
985 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
986 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
989 case "fanboy_social_blocking_list":
991 if (sharedPreferences.getBoolean("fanboy_social_blocking_list", true)) {
992 if (MainWebViewActivity.darkTheme) {
993 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
995 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
998 if (MainWebViewActivity.darkTheme) {
999 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
1001 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
1006 case "proxy_through_orbot":
1007 // Get current settings.
1008 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
1009 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
1011 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
1012 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
1014 // Update the icons.
1015 if (currentProxyThroughOrbot) {
1016 // Set the Tor icons according to the theme.
1017 if (MainWebViewActivity.darkTheme) {
1018 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1019 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1020 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1022 // Set the `torSearchCustomURLPreference` icon.
1023 if (torSearchCustomURLPreference.isEnabled()) {
1024 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1026 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1029 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1030 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1031 torSearchPreference.setIcon(R.drawable.search_enabled_light);
1033 // Set the `torSearchCustomURLPreference` icon.
1034 if (torSearchCustomURLPreference.isEnabled()) {
1035 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1037 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1040 } else { // Proxy through Orbot is disabled.
1041 if (MainWebViewActivity.darkTheme) {
1042 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1043 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1044 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1045 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1047 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1048 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1049 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1050 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1055 case "tor_homepage":
1056 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
1057 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
1061 // Get the present search string.
1062 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
1064 // Update the preferences.
1065 if (presentTorSearchString.equals("Custom URL")) {
1066 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1067 torSearchPreference.setSummary(R.string.custom_url);
1069 // Enable `torSearchCustomURLPreference`.
1070 torSearchCustomURLPreference.setEnabled(true);
1072 // Update the `torSearchCustomURLPreference` icon.
1073 if (MainWebViewActivity.darkTheme) {
1074 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1076 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1079 // Set the array value as the summary text.
1080 torSearchPreference.setSummary(presentTorSearchString);
1082 // Disable `torSearchCustomURLPreference`.
1083 torSearchCustomURLPreference.setEnabled(false);
1085 // Update the `torSearchCustomURLPreference` icon.
1086 if (MainWebViewActivity.darkTheme) {
1087 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1089 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1094 case "tor_search_custom_url":
1095 // Set the summary text for `tor_search_custom_url`.
1096 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
1100 // Store the new search string.
1101 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
1103 // Update `searchPreference` and `searchCustomURLPreference`.
1104 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1105 // Set the summary text to `R.string.custom_url`, which is translated.
1106 searchPreference.setSummary(R.string.custom_url);
1108 // Enable `searchCustomURLPreference`.
1109 searchCustomURLPreference.setEnabled(true);
1111 // Set the `searchCustomURLPreference` according to the theme.
1112 if (MainWebViewActivity.darkTheme) {
1113 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1115 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1117 } else { // `Custom URL` is not selected.
1118 // Set the summary text to `newSearchString`.
1119 searchPreference.setSummary(newSearchString);
1121 // Disable `searchCustomURLPreference`.
1122 searchCustomURLPreference.setEnabled(false);
1124 // Set the `searchCustomURLPreference` according to the theme.
1125 if (MainWebViewActivity.darkTheme) {
1126 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1128 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1133 case "search_custom_url":
1134 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1135 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
1138 case "full_screen_browsing_mode":
1139 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
1140 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
1141 if (MainWebViewActivity.darkTheme) {
1142 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1144 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1147 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
1148 // Disable `translucentNavigationBarPreference`.
1149 translucentNavigationBarPreference.setEnabled(false);
1151 // Set the icons according to the theme.
1152 if (MainWebViewActivity.darkTheme) {
1153 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1154 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1156 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1157 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1159 } else { // `hide_system_bars` is `false`.
1160 // Enable `translucentNavigationBarPreference`.
1161 translucentNavigationBarPreference.setEnabled(true);
1163 // Set the icons according to the theme.
1164 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
1165 // Set the `hideSystemBarsPreference` icon.
1166 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1168 // Set the `translucentNavigationBarPreference` icon.
1169 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1170 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1172 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1174 } else { // Use the light theme.
1175 // Set the `hideSystemBarsPreference` icon.
1176 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1178 // Set the `translucentNavigationBarPreference` icon.
1179 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1180 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1182 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1186 } else { // `full_screen_browsing_mode` is false.
1187 // Disable `translucentNavigationBarPreference`.
1188 translucentNavigationBarPreference.setEnabled(false);
1190 // Update the icons according to the theme.
1191 if (MainWebViewActivity.darkTheme) {
1192 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1193 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
1194 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1196 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1197 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
1198 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1203 case "hide_system_bars":
1204 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
1205 // Disable `translucentNavigationBarPreference`.
1206 translucentNavigationBarPreference.setEnabled(false);
1208 // Set the icons according to the theme.
1209 if (MainWebViewActivity.darkTheme) {
1210 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1211 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1213 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1214 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1216 } else { // `hide_system_bars` is false.
1217 // Enable `translucentNavigationBarPreference`.
1218 translucentNavigationBarPreference.setEnabled(true);
1220 // Set the icons according to the theme.
1221 if (MainWebViewActivity.darkTheme) {
1222 // Set the `hideSystemBarsPreference` icon.
1223 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1225 // Set the `translucentNavigationBarPreference` icon.
1226 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1227 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1229 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1232 // Set the `hideSystemBarsPreference` icon.
1233 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1235 // Set the `translucentNavigationBarPreference` icon.
1236 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1237 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1239 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1245 case "translucent_navigation_bar":
1247 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1248 if (MainWebViewActivity.darkTheme) {
1249 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1251 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1254 if (MainWebViewActivity.darkTheme) {
1255 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1257 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1262 case "clear_everything":
1263 // Store the new `clear_everything` status
1264 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1266 // Update the status of the `Clear and Exit` preferences.
1267 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1268 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1269 clearFormDataPreference.setEnabled(!newClearEverythingBoolean); // This line can be removed once the minimum API >= 26.
1270 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1272 // Update the `clearEverythingPreference` icon.
1273 if (newClearEverythingBoolean) {
1274 if (MainWebViewActivity.darkTheme) {
1275 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1277 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1280 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1283 // Update the `clearCookiesPreference` icon.
1284 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1285 if (MainWebViewActivity.darkTheme) {
1286 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1288 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1291 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1294 // Update the `clearDomStoragePreference` icon.
1295 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1296 if (MainWebViewActivity.darkTheme) {
1297 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1299 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1302 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1305 // Update the clear form data preference icon if the API < 26.
1306 if (Build.VERSION.SDK_INT < 26) {
1307 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1308 if (MainWebViewActivity.darkTheme) {
1309 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1311 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1314 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1318 // Update the `clearCachePreference` icon.
1319 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1320 if (MainWebViewActivity.darkTheme) {
1321 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1323 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1326 clearCachePreference.setIcon(R.drawable.cache_warning);
1330 case "clear_cookies":
1332 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1333 if (MainWebViewActivity.darkTheme) {
1334 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1336 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1339 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1343 case "clear_dom_storage":
1345 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1346 if (MainWebViewActivity.darkTheme) {
1347 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1349 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1352 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1356 // This section can be removed once the minimum API >= 26.
1357 case "clear_form_data":
1359 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1360 if (MainWebViewActivity.darkTheme) {
1361 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1363 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1366 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1372 if (sharedPreferences.getBoolean("clear_cache", true)) {
1373 if (MainWebViewActivity.darkTheme) {
1374 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1376 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1379 clearCachePreference.setIcon(R.drawable.cache_warning);
1384 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
1385 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
1388 case "default_font_size":
1389 // Update the summary text of `default_font_size`.
1390 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1393 case "swipe_to_refresh":
1395 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1396 if (MainWebViewActivity.darkTheme) {
1397 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1399 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1402 if (MainWebViewActivity.darkTheme) {
1403 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1405 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1410 case "display_additional_app_bar_icons":
1412 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1413 if (MainWebViewActivity.darkTheme) {
1414 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1416 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1419 if (MainWebViewActivity.darkTheme) {
1420 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1422 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1429 if (sharedPreferences.getBoolean("dark_theme", false)) {
1430 darkThemePreference.setIcon(R.drawable.theme_dark);
1432 darkThemePreference.setIcon(R.drawable.theme_light);
1435 // Create an intent to restart Privacy Browser.
1436 Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1438 // Assert that the intent is not null to remove the lint error below.
1439 assert changeThemeRestartIntent != null;
1441 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1442 changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1445 startActivity(changeThemeRestartIntent);
1449 // Set the URL to be reloaded on restart to apply the new night mode setting.
1450 MainWebViewActivity.loadUrlOnRestart = true;
1452 // Store the current night mode status.
1453 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1454 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript_enabled", false);
1457 if (currentNightModeBoolean) {
1458 if (MainWebViewActivity.darkTheme) {
1459 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1461 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1464 if (MainWebViewActivity.darkTheme) {
1465 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1467 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1471 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1472 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1473 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1475 // Update the `javaScriptPreference` icon.
1476 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1477 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1479 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1482 // Update the `domStoragePreference` icon.
1483 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1484 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
1485 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1486 } else { // DOM storage is disabled.
1487 if (MainWebViewActivity.darkTheme) {
1488 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1490 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1493 } else { // The preference is disabled. The icon should be ghosted.
1494 if (MainWebViewActivity.darkTheme) {
1495 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1497 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1502 case "display_webpage_images":
1503 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1505 if (MainWebViewActivity.darkTheme) {
1506 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1508 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1511 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1512 MainWebViewActivity.reloadOnRestart = false;
1515 if (MainWebViewActivity.darkTheme) {
1516 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1518 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1521 // Set `mainWebView` to reload on restart to remove the current images.
1522 MainWebViewActivity.reloadOnRestart = true;
1528 // Register the listener.
1529 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1532 // 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
1533 // even while running in the foreground.
1535 public void onPause() {
1537 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1541 public void onResume() {
1543 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);