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.Intent;
24 import android.content.SharedPreferences;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.preference.Preference;
28 import android.preference.PreferenceFragment;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.webkit.WebView;
33 import com.stoutner.privacybrowser.R;
34 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
36 public class SettingsFragment extends PreferenceFragment {
37 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
38 private SharedPreferences savedPreferences;
41 public void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 addPreferencesFromResource(R.xml.preferences);
45 // Initialize savedPreferences.
46 savedPreferences = getPreferenceScreen().getSharedPreferences();
48 // Get handles for the preferences we need to modify.
49 final Preference javaScriptPreference = findPreference("javascript_enabled");
50 final Preference firstPartyCookiesPreference = findPreference("first_party_cookies_enabled");
51 final Preference thirdPartyCookiesPreference = findPreference("third_party_cookies_enabled");
52 final Preference domStoragePreference = findPreference("dom_storage_enabled");
53 final Preference saveFormDataPreference = findPreference("save_form_data_enabled");
54 final Preference userAgentPreference = findPreference("user_agent");
55 final Preference customUserAgentPreference = findPreference("custom_user_agent");
56 final Preference incognitoModePreference = findPreference("incognito_mode");
57 final Preference doNotTrackPreference = findPreference("do_not_track");
58 final Preference easyListPreference = findPreference("easylist");
59 final Preference easyPrivacyPreference = findPreference("easyprivacy");
60 final Preference fanboyAnnoyanceListPreference = findPreference("fanboy_annoyance_list");
61 final Preference fanboySocialBlockingListPreference = findPreference("fanboy_social_blocking_list");
62 final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
63 final Preference torHomepagePreference = findPreference("tor_homepage");
64 final Preference torSearchPreference = findPreference("tor_search");
65 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
66 final Preference searchPreference = findPreference("search");
67 final Preference searchCustomURLPreference = findPreference("search_custom_url");
68 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
69 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
70 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
71 final Preference clearEverythingPreference = findPreference("clear_everything");
72 final Preference clearCookiesPreference = findPreference("clear_cookies");
73 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
74 final Preference clearFormDataPreference = findPreference("clear_form_data");
75 final Preference clearCachePreference = findPreference("clear_cache");
76 final Preference homepagePreference = findPreference("homepage");
77 final Preference defaultFontSizePreference = findPreference("default_font_size");
78 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
79 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
80 final Preference darkThemePreference = findPreference("dark_theme");
81 final Preference nightModePreference = findPreference("night_mode");
82 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
85 torHomepagePreference.setDependency("proxy_through_orbot");
86 torSearchPreference.setDependency("proxy_through_orbot");
87 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
89 // Get Strings from the preferences.
90 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
91 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
93 // Get booleans from the preferences.
94 final boolean javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
95 boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false);
96 boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false);
97 boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboy_annoyance_list", true);
98 boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboy_social_blocking_list", true);
99 boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
100 boolean fullScreenBrowsingMode = savedPreferences.getBoolean("full_screen_browsing_mode", false);
101 boolean hideSystemBars = savedPreferences.getBoolean("hide_system_bars", false);
102 boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
103 final boolean nightMode = savedPreferences.getBoolean("night_mode", false);
105 // Only enable the third-party preference if first-party cookies are enabled and API >= 21.
106 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabled && (Build.VERSION.SDK_INT >= 21));
108 // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
109 domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
111 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
112 fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
114 // We need to inflated a `WebView` to get the default user agent.
115 LayoutInflater inflater = getActivity().getLayoutInflater();
116 // `@SuppressLint("InflateParams")` removes the warning about using `null` as the `ViewGroup`, which in this case makes sense because we don't want to display `bare_webview` on the screen.
117 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
118 final WebView bareWebView = bareWebViewLayout.findViewById(R.id.bare_webview);
120 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
121 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
122 case "WebView default user agent":
123 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
124 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
127 case "Custom user agent":
128 // We can't use the string from the array because it is referenced in code and can't be translated.
129 userAgentPreference.setSummary(R.string.custom_user_agent);
133 // Display the user agent from the preference as the summary text.
134 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
138 // Set the summary text for "customUserAgentPreference" (the default is `PrivacyBrowser/1.0`) and enable it if `userAgentPreference` it set to `Custom user agent`.
139 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
140 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
143 // 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`.
144 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
147 // 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=`
148 if (torSearchString.equals("Custom URL")) {
149 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
150 torSearchPreference.setSummary(R.string.custom_url);
152 // Set the array value as the summary text.
153 torSearchPreference.setSummary(torSearchString);
156 // Set the summary text for `tor_search_custom_url`. The default is `""`.
157 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
159 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
160 torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
163 // 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=`.
164 if (searchString.equals("Custom URL")) {
165 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
166 searchPreference.setSummary(R.string.custom_url);
168 // Set the array value as the summary text.
169 searchPreference.setSummary(searchString);
172 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
173 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
174 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
177 // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
178 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingMode && !hideSystemBars);
180 // Set the status of the `Clear and Exit` preferences.
181 clearCookiesPreference.setEnabled(!clearEverything);
182 clearDomStoragePreference.setEnabled(!clearEverything);
183 clearFormDataPreference.setEnabled(!clearEverything);
184 clearCachePreference.setEnabled(!clearEverything);
186 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
187 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
189 // Set the default font size as the summary text for the `Default Font Size` preference when the preference screen is loaded. The default is `100`.
190 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
192 // Disable `javaScriptPreference` if `nightModeBoolean` is true. JavaScript will be enabled for all web pages.
193 javaScriptPreference.setEnabled(!nightMode);
195 // Set the `javaScriptPreference` icon.
196 if (javaScriptEnabled || nightMode) {
197 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
199 javaScriptPreference.setIcon(R.drawable.privacy_mode);
202 // Set the `firstPartyCookiesPreference` icon.
203 if (firstPartyCookiesEnabled) {
204 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
206 if (MainWebViewActivity.darkTheme) {
207 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
209 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
213 // Set the `thirdPartyCookiesPreference` icon.
214 if (firstPartyCookiesEnabled && Build.VERSION.SDK_INT >= 21) {
215 if (thirdPartyCookiesEnabled) {
216 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
218 if (MainWebViewActivity.darkTheme) {
219 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
221 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
225 if (MainWebViewActivity.darkTheme) {
226 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
228 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
232 // Set the `domStoragePreference` icon.
233 if (javaScriptEnabled || nightMode) { // The preference is enabled.
234 if (savedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
235 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
236 } else { // DOM storage is disabled.
237 if (MainWebViewActivity.darkTheme) {
238 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
240 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
243 } else { // The preference is disabled. The icon should be ghosted.
244 if (MainWebViewActivity.darkTheme) {
245 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
247 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
251 // Set the `saveFormDataPreference` icon.
252 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
253 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
255 if (MainWebViewActivity.darkTheme) {
256 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
258 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
262 // Set the `customUserAgentPreference` icon.
263 if (customUserAgentPreference.isEnabled()) {
264 if (MainWebViewActivity.darkTheme) {
265 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
267 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
270 if (MainWebViewActivity.darkTheme) {
271 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
273 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
277 // Set the `incognitoModePreference` icon.
278 if (savedPreferences.getBoolean("incognito_mode", false)) {
279 if (MainWebViewActivity.darkTheme) {
280 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
282 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
285 if (MainWebViewActivity.darkTheme) {
286 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
288 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
292 // Set the `doNotTrackPreference` icon.
293 if (savedPreferences.getBoolean("do_not_track", false)) {
294 if (MainWebViewActivity.darkTheme) {
295 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
297 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
300 if (MainWebViewActivity.darkTheme) {
301 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
303 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
307 // Set the EasyList icon.
308 if (savedPreferences.getBoolean("easylist", true)) {
309 if (MainWebViewActivity.darkTheme) {
310 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
312 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
315 if (MainWebViewActivity.darkTheme) {
316 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
318 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
322 // Set the EasyPrivacy icon.
323 if (savedPreferences.getBoolean("easyprivacy", true)) {
324 if (MainWebViewActivity.darkTheme) {
325 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
327 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
330 if (MainWebViewActivity.darkTheme) {
331 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
333 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
337 // Set the Fanboy list icons.
338 if (fanboyAnnoyanceListEnabled) {
339 if (MainWebViewActivity.darkTheme) {
340 // Set the Fanboy annoyance list icon.
341 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
343 // Set the Fanboy social blocking list icon.
344 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
346 // Set the Fanboy annoyance list icon.
347 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
349 // Set the Fanboy social blocking list icon.
350 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
353 if (MainWebViewActivity.darkTheme) {
354 // Set the Fanboy annoyance list icon.
355 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
357 // Set the Fanboy social blocking list icon.
358 if (fanboySocialBlockingEnabled) {
359 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
361 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
364 // Set the Fanboy annoyance list icon.
365 fanboyAnnoyanceListPreference.setIcon(R.drawable.block_ads_disabled_light);
367 // Set the Fanboy social blocking list icon.
368 if (fanboySocialBlockingEnabled) {
369 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
371 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
376 // Set the Tor icons according to the theme.
377 if (proxyThroughOrbot) { // Proxying is enabled.
378 if (MainWebViewActivity.darkTheme) {
379 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
380 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
381 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
383 // Set the custom search icon.
384 if (torSearchCustomURLPreference.isEnabled()) {
385 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
387 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
390 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
391 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
392 torSearchPreference.setIcon(R.drawable.search_enabled_light);
394 // Set the custom search icon.
395 if (torSearchCustomURLPreference.isEnabled()) {
396 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
398 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
401 } else { // Proxying is disabled.
402 if (MainWebViewActivity.darkTheme) {
403 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
404 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
405 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
406 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
408 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
409 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
410 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
411 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
415 // Set the `searchCustomURLPreference` icon.
416 if (searchCustomURLPreference.isEnabled()) {
417 if (MainWebViewActivity.darkTheme) {
418 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
420 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
423 if (MainWebViewActivity.darkTheme) {
424 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
426 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
430 // Set the full screen browsing mode icons.
431 if (fullScreenBrowsingMode) { // `fullScreenBrowsingModeBoolean` is `true`.
432 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
433 if (MainWebViewActivity.darkTheme) {
434 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
436 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
439 if (hideSystemBars) { // `hideSystemBarsBoolean` is `true`.
440 // Set the icons according to the theme.
441 if (MainWebViewActivity.darkTheme) {
442 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
443 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
445 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
446 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
448 } else { // `hideSystemBarsBoolean` is `false`.
449 // Set the `hideSystemBarsPreference` icon according to the theme.
450 if (MainWebViewActivity.darkTheme) {
451 // Set the icon for `hideSystemBarsPreference`.
452 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
454 // Set the icon for `translucentNavigationBarPreference`.
455 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
456 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
458 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
461 // Set the icon for `hideSystemBarsPreference`.
462 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
464 // Set the icon for `translucentNavigationBarPreference`.
465 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
466 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
468 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
472 } else { // `fullScreenBrowsingModeBoolean` is `false`.
473 // Set the icons according to the theme.
474 if (MainWebViewActivity.darkTheme) {
475 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
476 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
477 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
479 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
480 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
481 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
485 // Set the `clearEverythingPreference` icon.
486 if (clearEverything) {
487 if (MainWebViewActivity.darkTheme) {
488 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
490 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
493 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
496 // Set the `clearCookiesPreference` icon.
497 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
498 if (MainWebViewActivity.darkTheme) {
499 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
501 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
504 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
507 // Set the `clearDomStoragePreference` icon.
508 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
509 if (MainWebViewActivity.darkTheme) {
510 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
512 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
515 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
518 // Set the `clearFormDataPreference` icon.
519 if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
520 if (MainWebViewActivity.darkTheme) {
521 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
523 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
526 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
529 // Set the `clearCachePreference` icon.
530 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
531 if (MainWebViewActivity.darkTheme) {
532 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
534 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
537 clearCachePreference.setIcon(R.drawable.cache_warning);
540 // Set the `swipeToRefreshPreference` icon.
541 if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
542 if (MainWebViewActivity.darkTheme) {
543 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
545 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
548 if (MainWebViewActivity.darkTheme) {
549 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
551 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
555 // Set the `displayAdditionalAppBarIconsPreference` icon.
556 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
557 if (MainWebViewActivity.darkTheme) {
558 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
560 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
563 if (MainWebViewActivity.darkTheme) {
564 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
566 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
570 // Set the `darkThemePreference` icon.
571 if (savedPreferences.getBoolean("dark_theme", false)) {
572 darkThemePreference.setIcon(R.drawable.theme_dark);
574 darkThemePreference.setIcon(R.drawable.theme_light);
577 // Set the `nightModePreference` icon.
579 if (MainWebViewActivity.darkTheme) {
580 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
582 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
585 if (MainWebViewActivity.darkTheme) {
586 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
588 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
592 // Set the `displayWebpageImagesPreference` icon.
593 if (savedPreferences.getBoolean("display_webpage_images", true)) {
594 if (MainWebViewActivity.darkTheme) {
595 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
597 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
600 if (MainWebViewActivity.darkTheme) {
601 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
603 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
608 // Listen for preference changes.
609 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
611 case "javascript_enabled":
612 // Update the icons and the DOM storage preference status.
613 if (sharedPreferences.getBoolean("javascript_enabled", false)) { // The JavaScript preference is enabled.
614 // Update the icon for the JavaScript preference.
615 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
617 // Update the status of the DOM storage preference.
618 domStoragePreference.setEnabled(true);
620 // Update the icon for the DOM storage preference.
621 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
622 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
624 if (MainWebViewActivity.darkTheme) {
625 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
627 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
630 } else { // The JavaScript preference is disabled.
631 // Update the icon for the JavaScript preference.
632 javaScriptPreference.setIcon(R.drawable.privacy_mode);
634 // Update the status of the DOM storage preference.
635 domStoragePreference.setEnabled(false);
637 // Set the icon for DOM storage preference to be ghosted.
638 if (MainWebViewActivity.darkTheme) {
639 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
641 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
646 case "first_party_cookies_enabled":
647 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
648 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
649 // Set the icon for `first_party_cookies_enabled`.
650 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
652 // Update the icon for `third_party_cookies_enabled`.
653 if (Build.VERSION.SDK_INT >= 21) {
654 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
655 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
657 if (MainWebViewActivity.darkTheme) {
658 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
660 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
664 if (MainWebViewActivity.darkTheme) {
665 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
667 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
670 } else { // `first_party_cookies_enabled` is `false`.
671 // Update the icon for `first_party_cookies_enabled`.
672 if (MainWebViewActivity.darkTheme) {
673 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
675 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
678 // Set the icon for `third_party_cookies_enabled` to be ghosted.
679 if (MainWebViewActivity.darkTheme) {
680 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
682 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
686 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
687 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
690 case "third_party_cookies_enabled":
692 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
693 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
695 if (MainWebViewActivity.darkTheme) {
696 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
698 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
703 case "dom_storage_enabled":
705 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
706 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
708 if (MainWebViewActivity.darkTheme) {
709 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
711 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
716 case "save_form_data_enabled":
718 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
719 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
721 if (MainWebViewActivity.darkTheme) {
722 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
724 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
729 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
731 switch (userAgentString) {
732 case "WebView default user agent":
733 // Display the user agent as the summary text for `userAgentPreference`.
734 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
736 // Disable `customUserAgentPreference`.
737 customUserAgentPreference.setEnabled(false);
739 // Set the `customUserAgentPreference` icon according to the theme.
740 if (MainWebViewActivity.darkTheme) {
741 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
743 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
747 case "Custom user agent":
748 // Display `Custom user agent` as the summary text for `userAgentPreference`.
749 userAgentPreference.setSummary(R.string.custom_user_agent);
751 // Enable `customUserAgentPreference`.
752 customUserAgentPreference.setEnabled(true);
754 // Set the `customUserAgentPreference` icon according to the theme.
755 if (MainWebViewActivity.darkTheme) {
756 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
758 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
763 // Display the user agent as the summary text for `userAgentPreference`.
764 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
766 // Disable `customUserAgentPreference`.
767 customUserAgentPreference.setEnabled(false);
769 // Set the `customUserAgentPreference` icon according to the theme.
770 if (MainWebViewActivity.darkTheme) {
771 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
773 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
779 case "custom_user_agent":
780 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
781 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
784 case "incognito_mode":
786 if (sharedPreferences.getBoolean("incognito_mode", false)) {
787 if (MainWebViewActivity.darkTheme) {
788 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
790 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
793 if (MainWebViewActivity.darkTheme) {
794 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
796 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
803 if (sharedPreferences.getBoolean("do_not_track", false)) {
804 if (MainWebViewActivity.darkTheme) {
805 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
807 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
810 if (MainWebViewActivity.darkTheme) {
811 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
813 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
821 if (sharedPreferences.getBoolean("easylist", true)) {
822 if (MainWebViewActivity.darkTheme) {
823 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
825 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
828 if (MainWebViewActivity.darkTheme) {
829 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
831 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
838 if (sharedPreferences.getBoolean("easyprivacy", true)) {
839 if (MainWebViewActivity.darkTheme) {
840 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
842 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
845 if (MainWebViewActivity.darkTheme) {
846 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
848 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
853 case "fanboy_annoyance_list":
854 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboy_annoyance_list", true);
855 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboy_social_blocking_list", true);
857 // Update the Fanboy icons.
858 if (sharedPreferences.getBoolean("fanboy_annoyance_list", true)) {
859 if (MainWebViewActivity.darkTheme) {
860 // Update the Fanboy's annoyance list icon.
861 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
863 // Update the Fanboy's social blocking list icon.
864 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
866 // Update the Fanboy's annoyance list icon.
867 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
869 // Update the Fanboy's social blocking list icon.
870 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
873 if (MainWebViewActivity.darkTheme) {
874 // Update the Fanboy's annoyance list icon.
875 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
877 // Update the Fanboy's social blocking list icon.
878 if (currentFanboySocialBlockingList) {
879 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
881 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
884 // Update the Fanboy's annoyance list icon.
885 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
887 // Update the Fanboy's social blocking list icon.
888 if (currentFanboySocialBlockingList) {
889 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
891 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
896 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
897 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
900 case "fanboy_social_blocking_list":
902 if (sharedPreferences.getBoolean("fanboy_social_blocking_list", true)) {
903 if (MainWebViewActivity.darkTheme) {
904 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
906 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
909 if (MainWebViewActivity.darkTheme) {
910 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
912 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
917 case "proxy_through_orbot":
918 // Get current settings.
919 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
920 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
922 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
923 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
926 if (currentProxyThroughOrbot) {
927 // Set the Tor icons according to the theme.
928 if (MainWebViewActivity.darkTheme) {
929 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
930 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
931 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
933 // Set the `torSearchCustomURLPreference` icon.
934 if (torSearchCustomURLPreference.isEnabled()) {
935 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
937 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
940 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
941 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
942 torSearchPreference.setIcon(R.drawable.search_enabled_light);
944 // Set the `torSearchCustomURLPreference` icon.
945 if (torSearchCustomURLPreference.isEnabled()) {
946 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
948 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
951 } else { // Proxy through Orbot is disabled.
952 if (MainWebViewActivity.darkTheme) {
953 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
954 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
955 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
956 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
958 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
959 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
960 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
961 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
967 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
968 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
972 // Get the present search string.
973 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
975 // Update the preferences.
976 if (presentTorSearchString.equals("Custom URL")) {
977 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
978 torSearchPreference.setSummary(R.string.custom_url);
980 // Enable `torSearchCustomURLPreference`.
981 torSearchCustomURLPreference.setEnabled(true);
983 // Update the `torSearchCustomURLPreference` icon.
984 if (MainWebViewActivity.darkTheme) {
985 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
987 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
990 // Set the array value as the summary text.
991 torSearchPreference.setSummary(presentTorSearchString);
993 // Disable `torSearchCustomURLPreference`.
994 torSearchCustomURLPreference.setEnabled(false);
996 // Update the `torSearchCustomURLPreference` icon.
997 if (MainWebViewActivity.darkTheme) {
998 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1000 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1005 case "tor_search_custom_url":
1006 // Set the summary text for `tor_search_custom_url`.
1007 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
1011 // Store the new search string.
1012 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
1014 // Update `searchPreference` and `searchCustomURLPreference`.
1015 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1016 // Set the summary text to `R.string.custom_url`, which is translated.
1017 searchPreference.setSummary(R.string.custom_url);
1019 // Enable `searchCustomURLPreference`.
1020 searchCustomURLPreference.setEnabled(true);
1022 // Set the `searchCustomURLPreference` according to the theme.
1023 if (MainWebViewActivity.darkTheme) {
1024 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1026 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1028 } else { // `Custom URL` is not selected.
1029 // Set the summary text to `newSearchString`.
1030 searchPreference.setSummary(newSearchString);
1032 // Disable `searchCustomURLPreference`.
1033 searchCustomURLPreference.setEnabled(false);
1035 // Set the `searchCustomURLPreference` according to the theme.
1036 if (MainWebViewActivity.darkTheme) {
1037 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1039 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1044 case "search_custom_url":
1045 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1046 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
1049 case "full_screen_browsing_mode":
1050 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
1051 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
1052 if (MainWebViewActivity.darkTheme) {
1053 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1055 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1058 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
1059 // Disable `translucentNavigationBarPreference`.
1060 translucentNavigationBarPreference.setEnabled(false);
1062 // Set the icons according to the theme.
1063 if (MainWebViewActivity.darkTheme) {
1064 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1065 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1067 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1068 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1070 } else { // `hide_system_bars` is `false`.
1071 // Enable `translucentNavigationBarPreference`.
1072 translucentNavigationBarPreference.setEnabled(true);
1074 // Set the icons according to the theme.
1075 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
1076 // Set the `hideSystemBarsPreference` icon.
1077 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1079 // Set the `translucentNavigationBarPreference` icon.
1080 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1081 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1083 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1085 } else { // Use the light theme.
1086 // Set the `hideSystemBarsPreference` icon.
1087 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1089 // Set the `translucentNavigationBarPreference` icon.
1090 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1091 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1093 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1097 } else { // `full_screen_browsing_mode` is false.
1098 // Disable `translucentNavigationBarPreference`.
1099 translucentNavigationBarPreference.setEnabled(false);
1101 // Update the icons according to the theme.
1102 if (MainWebViewActivity.darkTheme) {
1103 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1104 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
1105 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1107 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1108 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
1109 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1114 case "hide_system_bars":
1115 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
1116 // Disable `translucentNavigationBarPreference`.
1117 translucentNavigationBarPreference.setEnabled(false);
1119 // Set the icons according to the theme.
1120 if (MainWebViewActivity.darkTheme) {
1121 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1122 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1124 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1125 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1127 } else { // `hide_system_bars` is false.
1128 // Enable `translucentNavigationBarPreference`.
1129 translucentNavigationBarPreference.setEnabled(true);
1131 // Set the icons according to the theme.
1132 if (MainWebViewActivity.darkTheme) {
1133 // Set the `hideSystemBarsPreference` icon.
1134 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1136 // Set the `translucentNavigationBarPreference` icon.
1137 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1138 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1140 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1143 // Set the `hideSystemBarsPreference` icon.
1144 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1146 // Set the `translucentNavigationBarPreference` icon.
1147 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1148 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1150 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1156 case "translucent_navigation_bar":
1158 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1159 if (MainWebViewActivity.darkTheme) {
1160 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1162 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1165 if (MainWebViewActivity.darkTheme) {
1166 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1168 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1173 case "clear_everything":
1174 // Store the new `clear_everything` status
1175 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1177 // Update the status of the `Clear and Exit` preferences.
1178 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1179 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1180 clearFormDataPreference.setEnabled(!newClearEverythingBoolean);
1181 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1183 // Update the `clearEverythingPreference` icon.
1184 if (newClearEverythingBoolean) {
1185 if (MainWebViewActivity.darkTheme) {
1186 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1188 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1191 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1194 // Update the `clearCookiesPreference` icon.
1195 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1196 if (MainWebViewActivity.darkTheme) {
1197 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1199 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1202 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1205 // Update the `clearDomStoragePreference` icon.
1206 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1207 if (MainWebViewActivity.darkTheme) {
1208 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1210 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1213 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1216 // Update the `clearFormDataPreference` icon.
1217 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1218 if (MainWebViewActivity.darkTheme) {
1219 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1221 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1224 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1227 // Update the `clearCachePreference` icon.
1228 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1229 if (MainWebViewActivity.darkTheme) {
1230 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1232 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1235 clearCachePreference.setIcon(R.drawable.cache_warning);
1239 case "clear_cookies":
1241 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1242 if (MainWebViewActivity.darkTheme) {
1243 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1245 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1248 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1252 case "clear_dom_storage":
1254 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1255 if (MainWebViewActivity.darkTheme) {
1256 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1258 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1261 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1265 case "clear_form_data":
1267 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1268 if (MainWebViewActivity.darkTheme) {
1269 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1271 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1274 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1280 if (sharedPreferences.getBoolean("clear_cache", true)) {
1281 if (MainWebViewActivity.darkTheme) {
1282 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1284 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1287 clearCachePreference.setIcon(R.drawable.cache_warning);
1292 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
1293 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
1296 case "default_font_size":
1297 // Update the summary text of `default_font_size`.
1298 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1301 case "swipe_to_refresh":
1303 if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
1304 if (MainWebViewActivity.darkTheme) {
1305 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1307 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1310 if (MainWebViewActivity.darkTheme) {
1311 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1313 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1318 case "display_additional_app_bar_icons":
1320 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1321 if (MainWebViewActivity.darkTheme) {
1322 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1324 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1327 if (MainWebViewActivity.darkTheme) {
1328 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1330 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1337 if (sharedPreferences.getBoolean("dark_theme", false)) {
1338 darkThemePreference.setIcon(R.drawable.theme_dark);
1340 darkThemePreference.setIcon(R.drawable.theme_light);
1343 // Create an `Intent` to restart Privacy Browser.
1344 Intent intent = getActivity().getParentActivityIntent();
1346 // Assert that `intent` is not `null` to remove the lint error below.
1347 assert intent != null;
1349 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1350 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1353 startActivity(intent);
1357 // Set the URL to be reloaded on restart to apply the new night mode setting.
1358 MainWebViewActivity.loadUrlOnRestart = true;
1360 // Store the current night mode status.
1361 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1362 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript_enabled", false);
1365 if (currentNightModeBoolean) {
1366 if (MainWebViewActivity.darkTheme) {
1367 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1369 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1372 if (MainWebViewActivity.darkTheme) {
1373 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1375 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1379 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1380 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1381 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1383 // Update the `javaScriptPreference` icon.
1384 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1385 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1387 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1390 // Update the `domStoragePreference` icon.
1391 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1392 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
1393 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1394 } else { // DOM storage is disabled.
1395 if (MainWebViewActivity.darkTheme) {
1396 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1398 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1401 } else { // The preference is disabled. The icon should be ghosted.
1402 if (MainWebViewActivity.darkTheme) {
1403 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1405 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1410 case "display_webpage_images":
1411 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1413 if (MainWebViewActivity.darkTheme) {
1414 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1416 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1419 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1420 MainWebViewActivity.reloadOnRestart = false;
1423 if (MainWebViewActivity.darkTheme) {
1424 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1426 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1429 // Set `mainWebView` to reload on restart to remove the current images.
1430 MainWebViewActivity.reloadOnRestart = true;
1436 // Register the listener.
1437 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1440 // 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
1441 // even while running in the foreground.
1443 public void onPause() {
1445 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1449 public void onResume() {
1451 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);