2 * Copyright © 2016-2017 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 blockAdsPreference = findPreference("block_ads");
57 final Preference incognitoModePreference = findPreference("incognito_mode");
58 final Preference doNotTrackPreference = findPreference("do_not_track");
59 final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
60 final Preference torHomepagePreference = findPreference("tor_homepage");
61 final Preference torSearchPreference = findPreference("tor_search");
62 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
63 final Preference searchPreference = findPreference("search");
64 final Preference searchCustomURLPreference = findPreference("search_custom_url");
65 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
66 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
67 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
68 final Preference clearEverythingPreference = findPreference("clear_everything");
69 final Preference clearCookiesPreference = findPreference("clear_cookies");
70 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
71 final Preference clearFormDataPreference = findPreference("clear_form_data");
72 final Preference clearCachePreference = findPreference("clear_cache");
73 final Preference homepagePreference = findPreference("homepage");
74 final Preference defaultFontSizePreference = findPreference("default_font_size");
75 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
76 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
77 final Preference darkThemePreference = findPreference("dark_theme");
78 final Preference nightModePreference = findPreference("night_mode");
79 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
82 torHomepagePreference.setDependency("proxy_through_orbot");
83 torSearchPreference.setDependency("proxy_through_orbot");
84 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
86 // Get Strings from the preferences.
87 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
88 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
90 // Get booleans from the preferences.
91 final boolean javaScriptEnabledBoolean = savedPreferences.getBoolean("javascript_enabled", false);
92 boolean firstPartyCookiesEnabledBoolean = savedPreferences.getBoolean("first_party_cookies_enabled", false);
93 boolean thirdPartyCookiesEnabledBoolean = savedPreferences.getBoolean("third_party_cookies_enabled", false);
94 boolean proxyThroughOrbotBoolean = savedPreferences.getBoolean("proxy_through_orbot", false);
95 boolean fullScreenBrowsingModeBoolean = savedPreferences.getBoolean("full_screen_browsing_mode", false);
96 boolean hideSystemBarsBoolean = savedPreferences.getBoolean("hide_system_bars", false);
97 boolean clearEverythingBoolean = savedPreferences.getBoolean("clear_everything", true);
98 final boolean nightModeBoolean = savedPreferences.getBoolean("night_mode", false);
100 // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
101 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
103 // Only enable `domStoragePreference` if either `javaScriptEnabledBoolean` or `nightModeBoolean` is true.
104 domStoragePreference.setEnabled(javaScriptEnabledBoolean || nightModeBoolean);
106 // We need to inflated a `WebView` to get the default user agent.
107 LayoutInflater inflater = getActivity().getLayoutInflater();
108 // `@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. `false` does not attach the view to the root.
109 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
110 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
112 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
113 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
114 case "WebView default user agent":
115 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
116 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
119 case "Custom user agent":
120 // We can't use the string from the array because it is referenced in code and can't be translated.
121 userAgentPreference.setSummary(R.string.custom_user_agent);
125 // Display the user agent from the preference as the summary text.
126 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
130 // Set the summary text for "customUserAgentPreference" (the default is `PrivacyBrowser/1.0`) and enable it if `userAgentPreference` it set to `Custom user agent`.
131 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
132 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
135 // 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`.
136 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
139 // 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=`
140 if (torSearchString.equals("Custom URL")) {
141 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
142 torSearchPreference.setSummary(R.string.custom_url);
144 // Set the array value as the summary text.
145 torSearchPreference.setSummary(torSearchString);
148 // Set the summary text for `tor_search_custom_url`. The default is `""`.
149 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
151 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
152 torSearchCustomURLPreference.setEnabled(proxyThroughOrbotBoolean && torSearchString.equals("Custom URL"));
155 // 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=`.
156 if (searchString.equals("Custom URL")) {
157 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
158 searchPreference.setSummary(R.string.custom_url);
160 // Set the array value as the summary text.
161 searchPreference.setSummary(searchString);
164 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
165 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
166 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
169 // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
170 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
172 // Set the status of the `Clear and Exit` preferences.
173 clearCookiesPreference.setEnabled(!clearEverythingBoolean);
174 clearDomStoragePreference.setEnabled(!clearEverythingBoolean);
175 clearFormDataPreference.setEnabled(!clearEverythingBoolean);
176 clearCachePreference.setEnabled(!clearEverythingBoolean);
178 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
179 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
181 // 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`.
182 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
184 // Disable `javaScriptPreference` if `nightModeBoolean` is true. JavaScript will be enabled for all web pages.
185 javaScriptPreference.setEnabled(!nightModeBoolean);
187 // Set the `javaScriptPreference` icon.
188 if (javaScriptEnabledBoolean || nightModeBoolean) {
189 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
191 javaScriptPreference.setIcon(R.drawable.privacy_mode);
194 // Set the `firstPartyCookiesPreference` icon.
195 if (firstPartyCookiesEnabledBoolean) {
196 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
198 if (MainWebViewActivity.darkTheme) {
199 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
201 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
205 // Set the `thirdPartyCookiesPreference` icon.
206 if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
207 if (thirdPartyCookiesEnabledBoolean) {
208 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
210 if (MainWebViewActivity.darkTheme) {
211 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
213 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
217 if (MainWebViewActivity.darkTheme) {
218 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
220 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
224 // Set the `domStoragePreference` icon.
225 if (javaScriptEnabledBoolean || nightModeBoolean) { // The preference is enabled.
226 if (savedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
227 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
228 } else { // DOM storage is disabled.
229 if (MainWebViewActivity.darkTheme) {
230 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
232 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
235 } else { // The preference is disabled. The icon should be ghosted.
236 if (MainWebViewActivity.darkTheme) {
237 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
239 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
243 // Set the `saveFormDataPreference` icon.
244 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
245 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
247 if (MainWebViewActivity.darkTheme) {
248 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
250 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
254 // Set the `customUserAgentPreference` icon.
255 if (customUserAgentPreference.isEnabled()) {
256 if (MainWebViewActivity.darkTheme) {
257 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
259 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
262 if (MainWebViewActivity.darkTheme) {
263 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
265 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
269 // Set the `blockAdsPreference` icon.
270 if (savedPreferences.getBoolean("block_ads", true)) {
271 if (MainWebViewActivity.darkTheme) {
272 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_dark);
274 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_light);
277 if (MainWebViewActivity.darkTheme) {
278 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_dark);
280 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_light);
284 // Set the `incognitoModePreference` icon.
285 if (savedPreferences.getBoolean("incognito_mode", false)) {
286 if (MainWebViewActivity.darkTheme) {
287 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
289 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
292 if (MainWebViewActivity.darkTheme) {
293 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
295 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
299 // Set the `doNotTrackPreference` icon.
300 if (savedPreferences.getBoolean("do_not_track", false)) {
301 if (MainWebViewActivity.darkTheme) {
302 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_dark);
304 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_light);
307 if (MainWebViewActivity.darkTheme) {
308 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_dark);
310 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_light);
314 // Set the Tor icons according to the theme.
315 if (proxyThroughOrbotBoolean) { // Proxying is enabled.
316 if (MainWebViewActivity.darkTheme) {
317 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
318 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
319 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
321 // Set the custom search icon.
322 if (torSearchCustomURLPreference.isEnabled()) {
323 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
325 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
328 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
329 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
330 torSearchPreference.setIcon(R.drawable.search_enabled_light);
332 // Set the custom search icon.
333 if (torSearchCustomURLPreference.isEnabled()) {
334 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
336 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
339 } else { // Proxying is disabled.
340 if (MainWebViewActivity.darkTheme) {
341 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
342 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
343 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
344 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
346 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
347 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
348 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
349 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
353 // Set the `searchCustomURLPreference` icon.
354 if (searchCustomURLPreference.isEnabled()) {
355 if (MainWebViewActivity.darkTheme) {
356 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
358 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
361 if (MainWebViewActivity.darkTheme) {
362 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
364 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
368 // Set the full screen browsing mode icons.
369 if (fullScreenBrowsingModeBoolean) { // `fullScreenBrowsingModeBoolean` is `true`.
370 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
371 if (MainWebViewActivity.darkTheme) {
372 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
374 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
377 if (hideSystemBarsBoolean) { // `hideSystemBarsBoolean` is `true`.
378 // Set the icons according to the theme.
379 if (MainWebViewActivity.darkTheme) {
380 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
381 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
383 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
384 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
386 } else { // `hideSystemBarsBoolean` is `false`.
387 // Set the `hideSystemBarsPreference` icon according to the theme.
388 if (MainWebViewActivity.darkTheme) {
389 // Set the icon for `hideSystemBarsPreference`.
390 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
392 // Set the icon for `translucentNavigationBarPreference`.
393 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
394 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
396 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
399 // Set the icon for `hideSystemBarsPreference`.
400 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
402 // Set the icon for `translucentNavigationBarPreference`.
403 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
404 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
406 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
410 } else { // `fullScreenBrowsingModeBoolean` is `false`.
411 // Set the icons according to the theme.
412 if (MainWebViewActivity.darkTheme) {
413 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
414 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
415 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
417 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
418 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
419 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
423 // Set the `clearEverythingPreference` icon.
424 if (clearEverythingBoolean) {
425 if (MainWebViewActivity.darkTheme) {
426 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
428 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
431 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
434 // Set the `clearCookiesPreference` icon.
435 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cookies", true)) {
436 if (MainWebViewActivity.darkTheme) {
437 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
439 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
442 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
445 // Set the `clearDomStoragePreference` icon.
446 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_dom_storage", true)) {
447 if (MainWebViewActivity.darkTheme) {
448 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
450 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
453 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
456 // Set the `clearFormDataPreference` icon.
457 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_form_data", true)) {
458 if (MainWebViewActivity.darkTheme) {
459 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
461 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
464 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
467 // Set the `clearCachePreference` icon.
468 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cache", true)) {
469 if (MainWebViewActivity.darkTheme) {
470 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
472 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
475 clearCachePreference.setIcon(R.drawable.cache_warning);
478 // Set the `swipeToRefreshPreference` icon.
479 if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
480 if (MainWebViewActivity.darkTheme) {
481 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
483 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
486 if (MainWebViewActivity.darkTheme) {
487 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
489 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
493 // Set the `displayAdditionalAppBarIconsPreference` icon.
494 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
495 if (MainWebViewActivity.darkTheme) {
496 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
498 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
501 if (MainWebViewActivity.darkTheme) {
502 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
504 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
508 // Set the `darkThemePreference` icon.
509 if (savedPreferences.getBoolean("dark_theme", false)) {
510 darkThemePreference.setIcon(R.drawable.theme_dark);
512 darkThemePreference.setIcon(R.drawable.theme_light);
515 // Set the `nightModePreference` icon.
516 if (nightModeBoolean) {
517 if (MainWebViewActivity.darkTheme) {
518 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
520 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
523 if (MainWebViewActivity.darkTheme) {
524 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
526 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
530 // Set the `displayWebpageImagesPreference` icon.
531 if (savedPreferences.getBoolean("display_webpage_images", true)) {
532 if (MainWebViewActivity.darkTheme) {
533 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
535 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
538 if (MainWebViewActivity.darkTheme) {
539 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
541 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
546 // Listen for preference changes.
547 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
549 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
550 @SuppressLint("SetJavaScriptEnabled")
551 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
554 case "javascript_enabled":
555 // Update the icons and the DOM storage preference status.
556 if (sharedPreferences.getBoolean("javascript_enabled", false)) { // The JavaScript preference is enabled.
557 // Update the icon for the JavaScript preference.
558 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
560 // Update the status of the DOM storage preference.
561 domStoragePreference.setEnabled(true);
563 // Update the icon for the DOM storage preference.
564 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
565 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
567 if (MainWebViewActivity.darkTheme) {
568 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
570 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
573 } else { // The JavaScript preference is disabled.
574 // Update the icon for the JavaScript preference.
575 javaScriptPreference.setIcon(R.drawable.privacy_mode);
577 // Update the status of the DOM storage preference.
578 domStoragePreference.setEnabled(false);
580 // Set the icon for DOM storage preference to be ghosted.
581 if (MainWebViewActivity.darkTheme) {
582 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
584 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
589 case "first_party_cookies_enabled":
590 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
591 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
592 // Set the icon for `first_party_cookies_enabled`.
593 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
595 // Update the icon for `third_party_cookies_enabled`.
596 if (Build.VERSION.SDK_INT >= 21) {
597 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
598 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
600 if (MainWebViewActivity.darkTheme) {
601 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
603 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
607 if (MainWebViewActivity.darkTheme) {
608 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
610 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
613 } else { // `first_party_cookies_enabled` is `false`.
614 // Update the icon for `first_party_cookies_enabled`.
615 if (MainWebViewActivity.darkTheme) {
616 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
618 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
621 // Set the icon for `third_party_cookies_enabled` to be ghosted.
622 if (MainWebViewActivity.darkTheme) {
623 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
625 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
629 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
630 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
633 case "third_party_cookies_enabled":
635 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
636 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
638 if (MainWebViewActivity.darkTheme) {
639 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
641 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
646 case "dom_storage_enabled":
648 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
649 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
651 if (MainWebViewActivity.darkTheme) {
652 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
654 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
659 case "save_form_data_enabled":
661 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
662 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
664 if (MainWebViewActivity.darkTheme) {
665 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
667 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
672 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
674 switch (userAgentString) {
675 case "WebView default user agent":
676 // Display the user agent as the summary text for `userAgentPreference`.
677 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
679 // Disable `customUserAgentPreference`.
680 customUserAgentPreference.setEnabled(false);
682 // Set the `customUserAgentPreference` icon according to the theme.
683 if (MainWebViewActivity.darkTheme) {
684 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
686 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
690 case "Custom user agent":
691 // Display `Custom user agent` as the summary text for `userAgentPreference`.
692 userAgentPreference.setSummary(R.string.custom_user_agent);
694 // Enable `customUserAgentPreference`.
695 customUserAgentPreference.setEnabled(true);
697 // Set the `customUserAgentPreference` icon according to the theme.
698 if (MainWebViewActivity.darkTheme) {
699 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
701 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
706 // Display the user agent as the summary text for `userAgentPreference`.
707 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
709 // Disable `customUserAgentPreference`.
710 customUserAgentPreference.setEnabled(false);
712 // Set the `customUserAgentPreference` icon according to the theme.
713 if (MainWebViewActivity.darkTheme) {
714 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
716 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
722 case "custom_user_agent":
723 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
724 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
729 if (sharedPreferences.getBoolean("block_ads", true)) {
730 if (MainWebViewActivity.darkTheme) {
731 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_dark);
733 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_light);
736 if (MainWebViewActivity.darkTheme) {
737 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_dark);
739 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_light);
744 case "incognito_mode":
746 if (sharedPreferences.getBoolean("incognito_mode", false)) {
747 if (MainWebViewActivity.darkTheme) {
748 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
750 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
753 if (MainWebViewActivity.darkTheme) {
754 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
756 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
763 if (sharedPreferences.getBoolean("do_not_track", false)) {
764 if (MainWebViewActivity.darkTheme) {
765 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_dark);
767 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_light);
770 if (MainWebViewActivity.darkTheme) {
771 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_dark);
773 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_light);
779 case "proxy_through_orbot":
780 // Get current settings.
781 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
782 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
784 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
785 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
788 if (currentProxyThroughOrbot) {
789 // Set the Tor icons according to the theme.
790 if (MainWebViewActivity.darkTheme) {
791 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
792 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
793 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
795 // Set the `torSearchCustomURLPreference` icon.
796 if (torSearchCustomURLPreference.isEnabled()) {
797 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
799 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
802 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
803 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
804 torSearchPreference.setIcon(R.drawable.search_enabled_light);
806 // Set the `torSearchCustomURLPreference` icon.
807 if (torSearchCustomURLPreference.isEnabled()) {
808 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
810 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
813 } else { // Proxy through Orbot is disabled.
814 if (MainWebViewActivity.darkTheme) {
815 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
816 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
817 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
818 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
820 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
821 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
822 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
823 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
829 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
830 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
834 // Get the present search string.
835 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
837 // Update the preferences.
838 if (presentTorSearchString.equals("Custom URL")) {
839 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
840 torSearchPreference.setSummary(R.string.custom_url);
842 // Enable `torSearchCustomURLPreference`.
843 torSearchCustomURLPreference.setEnabled(true);
845 // Update the `torSearchCustomURLPreference` icon.
846 if (MainWebViewActivity.darkTheme) {
847 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
849 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
852 // Set the array value as the summary text.
853 torSearchPreference.setSummary(presentTorSearchString);
855 // Disable `torSearchCustomURLPreference`.
856 torSearchCustomURLPreference.setEnabled(false);
858 // Update the `torSearchCustomURLPreference` icon.
859 if (MainWebViewActivity.darkTheme) {
860 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
862 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
867 case "tor_search_custom_url":
868 // Set the summary text for `tor_search_custom_url`.
869 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
873 // Store the new search string.
874 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
876 // Update `searchPreference` and `searchCustomURLPreference`.
877 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
878 // Set the summary text to `R.string.custom_url`, which is translated.
879 searchPreference.setSummary(R.string.custom_url);
881 // Enable `searchCustomURLPreference`.
882 searchCustomURLPreference.setEnabled(true);
884 // Set the `searchCustomURLPreference` according to the theme.
885 if (MainWebViewActivity.darkTheme) {
886 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
888 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
890 } else { // `Custom URL` is not selected.
891 // Set the summary text to `newSearchString`.
892 searchPreference.setSummary(newSearchString);
894 // Disable `searchCustomURLPreference`.
895 searchCustomURLPreference.setEnabled(false);
897 // Set the `searchCustomURLPreference` according to the theme.
898 if (MainWebViewActivity.darkTheme) {
899 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
901 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
906 case "search_custom_url":
907 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
908 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
911 case "full_screen_browsing_mode":
912 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
913 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
914 if (MainWebViewActivity.darkTheme) {
915 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
917 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
920 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
921 // Disable `translucentNavigationBarPreference`.
922 translucentNavigationBarPreference.setEnabled(false);
924 // Set the icons according to the theme.
925 if (MainWebViewActivity.darkTheme) {
926 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
927 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
929 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
930 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
932 } else { // `hide_system_bars` is `false`.
933 // Enable `translucentNavigationBarPreference`.
934 translucentNavigationBarPreference.setEnabled(true);
936 // Set the icons according to the theme.
937 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
938 // Set the `hideSystemBarsPreference` icon.
939 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
941 // Set the `translucentNavigationBarPreference` icon.
942 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
943 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
945 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
947 } else { // Use the light theme.
948 // Set the `hideSystemBarsPreference` icon.
949 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
951 // Set the `translucentNavigationBarPreference` icon.
952 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
953 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
955 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
959 } else { // `full_screen_browsing_mode` is false.
960 // Disable `translucentNavigationBarPreference`.
961 translucentNavigationBarPreference.setEnabled(false);
963 // Update the icons according to the theme.
964 if (MainWebViewActivity.darkTheme) {
965 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
966 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
967 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
969 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
970 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
971 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
976 case "hide_system_bars":
977 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
978 // Disable `translucentNavigationBarPreference`.
979 translucentNavigationBarPreference.setEnabled(false);
981 // Set the icons according to the theme.
982 if (MainWebViewActivity.darkTheme) {
983 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
984 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
986 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
987 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
989 } else { // `hide_system_bars` is false.
990 // Enable `translucentNavigationBarPreference`.
991 translucentNavigationBarPreference.setEnabled(true);
993 // Set the icons according to the theme.
994 if (MainWebViewActivity.darkTheme) {
995 // Set the `hideSystemBarsPreference` icon.
996 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
998 // Set the `translucentNavigationBarPreference` icon.
999 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1000 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1002 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1005 // Set the `hideSystemBarsPreference` icon.
1006 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1008 // Set the `translucentNavigationBarPreference` icon.
1009 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1010 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1012 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1018 case "translucent_navigation_bar":
1020 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1021 if (MainWebViewActivity.darkTheme) {
1022 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1024 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1027 if (MainWebViewActivity.darkTheme) {
1028 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1030 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1035 case "clear_everything":
1036 // Store the new `clear_everything` status
1037 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1039 // Update the status of the `Clear and Exit` preferences.
1040 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1041 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1042 clearFormDataPreference.setEnabled(!newClearEverythingBoolean);
1043 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1045 // Update the `clearEverythingPreference` icon.
1046 if (newClearEverythingBoolean) {
1047 if (MainWebViewActivity.darkTheme) {
1048 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1050 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1053 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1056 // Update the `clearCookiesPreference` icon.
1057 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1058 if (MainWebViewActivity.darkTheme) {
1059 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1061 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1064 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1067 // Update the `clearDomStoragePreference` icon.
1068 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1069 if (MainWebViewActivity.darkTheme) {
1070 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1072 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1075 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1078 // Update the `clearFormDataPreference` icon.
1079 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1080 if (MainWebViewActivity.darkTheme) {
1081 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1083 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1086 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1089 // Update the `clearCachePreference` icon.
1090 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1091 if (MainWebViewActivity.darkTheme) {
1092 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1094 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1097 clearCachePreference.setIcon(R.drawable.cache_warning);
1101 case "clear_cookies":
1103 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1104 if (MainWebViewActivity.darkTheme) {
1105 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1107 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1110 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1114 case "clear_dom_storage":
1116 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1117 if (MainWebViewActivity.darkTheme) {
1118 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1120 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1123 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1127 case "clear_form_data":
1129 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1130 if (MainWebViewActivity.darkTheme) {
1131 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1133 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1136 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1142 if (sharedPreferences.getBoolean("clear_cache", true)) {
1143 if (MainWebViewActivity.darkTheme) {
1144 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1146 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1149 clearCachePreference.setIcon(R.drawable.cache_warning);
1154 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
1155 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
1158 case "default_font_size":
1159 // Update the summary text of `default_font_size`.
1160 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1163 case "swipe_to_refresh":
1165 if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
1166 if (MainWebViewActivity.darkTheme) {
1167 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1169 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1172 if (MainWebViewActivity.darkTheme) {
1173 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1175 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1180 case "display_additional_app_bar_icons":
1182 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1183 if (MainWebViewActivity.darkTheme) {
1184 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1186 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1189 if (MainWebViewActivity.darkTheme) {
1190 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1192 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1199 if (sharedPreferences.getBoolean("dark_theme", false)) {
1200 darkThemePreference.setIcon(R.drawable.theme_dark);
1202 darkThemePreference.setIcon(R.drawable.theme_light);
1205 // Create an `Intent` to restart Privacy Browser.
1206 Intent intent = getActivity().getParentActivityIntent();
1208 // Assert that `intent` is not `null` to remove the lint error below.
1209 assert intent != null;
1211 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1212 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1215 startActivity(intent);
1219 // Set the URL to be reloaded on restart to apply the new night mode setting.
1220 MainWebViewActivity.loadUrlOnRestart = true;
1222 // Store the current night mode status.
1223 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1224 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript_enabled", false);
1227 if (currentNightModeBoolean) {
1228 if (MainWebViewActivity.darkTheme) {
1229 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1231 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1234 if (MainWebViewActivity.darkTheme) {
1235 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1237 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1241 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1242 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1243 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1245 // Update the `javaScriptPreference` icon.
1246 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1247 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1249 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1252 // Update the `domStoragePreference` icon.
1253 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1254 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
1255 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1256 } else { // DOM storage is disabled.
1257 if (MainWebViewActivity.darkTheme) {
1258 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1260 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1263 } else { // The preference is disabled. The icon should be ghosted.
1264 if (MainWebViewActivity.darkTheme) {
1265 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1267 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1272 case "display_webpage_images":
1273 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1275 if (MainWebViewActivity.darkTheme) {
1276 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1278 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1281 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1282 MainWebViewActivity.reloadOnRestart = false;
1285 if (MainWebViewActivity.darkTheme) {
1286 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1288 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1291 // Set `mainWebView` to reload on restart to remove the current images.
1292 MainWebViewActivity.reloadOnRestart = true;
1299 // Register the listener.
1300 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1303 // 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
1304 // even while running in the foreground.
1306 public void onPause() {
1308 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1312 public void onResume() {
1314 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);