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 displayWebpageImagesPreference = findPreference("display_webpage_images");
81 domStoragePreference.setDependency("javascript_enabled");
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 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);
99 // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
100 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
102 // We need to inflated a `WebView` to get the default user agent.
103 LayoutInflater inflater = getActivity().getLayoutInflater();
104 // `@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.
105 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
106 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
108 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
109 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
110 case "WebView default user agent":
111 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
112 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
115 case "Custom user agent":
116 // We can't use the string from the array because it is referenced in code and can't be translated.
117 userAgentPreference.setSummary(R.string.custom_user_agent);
121 // Display the user agent from the preference as the summary text.
122 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
126 // Set the summary text for "customUserAgentPreference" (the default is `PrivacyBrowser/1.0`) and enable it if `userAgentPreference` it set to `Custom user agent`.
127 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
128 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
131 // 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`.
132 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
135 // 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=`
136 if (torSearchString.equals("Custom URL")) {
137 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
138 torSearchPreference.setSummary(R.string.custom_url);
140 // Set the array value as the summary text.
141 torSearchPreference.setSummary(torSearchString);
144 // Set the summary text for `tor_search_custom_url`. The default is `""`.
145 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
147 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
148 torSearchCustomURLPreference.setEnabled(proxyThroughOrbotBoolean && torSearchString.equals("Custom URL"));
151 // 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=`.
152 if (searchString.equals("Custom URL")) {
153 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
154 searchPreference.setSummary(R.string.custom_url);
156 // Set the array value as the summary text.
157 searchPreference.setSummary(searchString);
160 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
161 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
162 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
165 // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
166 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
168 // Set the status of the `Clear and Exit` preferences.
169 clearCookiesPreference.setEnabled(!clearEverythingBoolean);
170 clearDomStoragePreference.setEnabled(!clearEverythingBoolean);
171 clearFormDataPreference.setEnabled(!clearEverythingBoolean);
172 clearCachePreference.setEnabled(!clearEverythingBoolean);
174 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
175 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
177 // 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`.
178 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
181 // Set the `javaScriptPreference` icon.
182 if (javaScriptEnabledBoolean) {
183 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
185 javaScriptPreference.setIcon(R.drawable.privacy_mode);
188 // Set the `firstPartyCookiesPreference` icon.
189 if (firstPartyCookiesEnabledBoolean) {
190 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
192 if (MainWebViewActivity.darkTheme) {
193 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
195 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
199 // Set the `thirdPartyCookiesPreference` icon.
200 if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
201 if (thirdPartyCookiesEnabledBoolean) {
202 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
204 if (MainWebViewActivity.darkTheme) {
205 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
207 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
211 if (MainWebViewActivity.darkTheme) {
212 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
214 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
218 // Set the `domStoragePreference` icon.
219 if (javaScriptEnabledBoolean) {
220 if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
221 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
223 if (MainWebViewActivity.darkTheme) {
224 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
226 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
230 if (MainWebViewActivity.darkTheme) {
231 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
233 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
237 // Set the `saveFormDataPreference` icon.
238 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
239 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
241 if (MainWebViewActivity.darkTheme) {
242 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
244 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
248 // Set the `customUserAgentPreference` icon.
249 if (customUserAgentPreference.isEnabled()) {
250 if (MainWebViewActivity.darkTheme) {
251 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
253 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
256 if (MainWebViewActivity.darkTheme) {
257 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
259 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
263 // Set the `blockAdsPreference` icon.
264 if (savedPreferences.getBoolean("block_ads", true)) {
265 if (MainWebViewActivity.darkTheme) {
266 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_dark);
268 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_light);
271 if (MainWebViewActivity.darkTheme) {
272 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_dark);
274 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_light);
278 // Set the `incognitoModePreference` icon.
279 if (savedPreferences.getBoolean("incognito_mode", false)) {
280 if (MainWebViewActivity.darkTheme) {
281 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
283 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
286 if (MainWebViewActivity.darkTheme) {
287 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
289 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
293 // Set the `doNotTrackPreference` icon.
294 if (savedPreferences.getBoolean("do_not_track", false)) {
295 if (MainWebViewActivity.darkTheme) {
296 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_dark);
298 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_light);
301 if (MainWebViewActivity.darkTheme) {
302 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_dark);
304 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_light);
308 // Set the Tor icons according to the theme.
309 if (proxyThroughOrbotBoolean) { // Proxying is enabled.
310 if (MainWebViewActivity.darkTheme) {
311 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
312 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
313 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
315 // Set the custom search icon.
316 if (torSearchCustomURLPreference.isEnabled()) {
317 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
319 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
322 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
323 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
324 torSearchPreference.setIcon(R.drawable.search_enabled_light);
326 // Set the custom search icon.
327 if (torSearchCustomURLPreference.isEnabled()) {
328 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
330 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
333 } else { // Proxying is disabled.
334 if (MainWebViewActivity.darkTheme) {
335 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
336 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
337 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
338 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
340 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
341 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
342 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
343 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
347 // Set the `searchCustomURLPreference` icon.
348 if (searchCustomURLPreference.isEnabled()) {
349 if (MainWebViewActivity.darkTheme) {
350 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
352 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
355 if (MainWebViewActivity.darkTheme) {
356 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
358 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
362 // Set the full screen browsing mode icons.
363 if (fullScreenBrowsingModeBoolean) { // `fullScreenBrowsingModeBoolean` is `true`.
364 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
365 if (MainWebViewActivity.darkTheme) {
366 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
368 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
371 if (hideSystemBarsBoolean) { // `hideSystemBarsBoolean` is `true`.
372 // Set the icons according to the theme.
373 if (MainWebViewActivity.darkTheme) {
374 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
375 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
377 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
378 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
380 } else { // `hideSystemBarsBoolean` is `false`.
381 // Set the `hideSystemBarsPreference` icon according to the theme.
382 if (MainWebViewActivity.darkTheme) {
383 // Set the icon for `hideSystemBarsPreference`.
384 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
386 // Set the icon for `translucentNavigationBarPreference`.
387 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
388 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
390 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
393 // Set the icon for `hideSystemBarsPreference`.
394 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
396 // Set the icon for `translucentNavigationBarPreference`.
397 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
398 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
400 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
404 } else { // `fullScreenBrowsingModeBoolean` is `false`.
405 // Set the icons according to the theme.
406 if (MainWebViewActivity.darkTheme) {
407 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
408 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
409 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
411 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
412 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
413 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
417 // Set the `clearEverythingPreference` icon.
418 if (clearEverythingBoolean) {
419 if (MainWebViewActivity.darkTheme) {
420 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
422 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
425 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
428 // Set the `clearCookiesPreference` icon.
429 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cookies", true)) {
430 if (MainWebViewActivity.darkTheme) {
431 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
433 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
436 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
439 // Set the `clearDomStoragePreference` icon.
440 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_dom_storage", true)) {
441 if (MainWebViewActivity.darkTheme) {
442 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
444 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
447 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
450 // Set the `clearFormDataPreference` icon.
451 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_form_data", true)) {
452 if (MainWebViewActivity.darkTheme) {
453 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
455 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
458 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
461 // Set the `clearCachePreference` icon.
462 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cache", true)) {
463 if (MainWebViewActivity.darkTheme) {
464 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
466 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
469 clearCachePreference.setIcon(R.drawable.cache_warning);
472 // Set the `swipeToRefreshPreference` icon.
473 if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
474 if (MainWebViewActivity.darkTheme) {
475 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
477 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
480 if (MainWebViewActivity.darkTheme) {
481 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
483 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
487 // Set the `displayAdditionalAppBarIconsPreference` icon.
488 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
489 if (MainWebViewActivity.darkTheme) {
490 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
492 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
495 if (MainWebViewActivity.darkTheme) {
496 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
498 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
502 // Set the `darkThemePreference` icon.
503 if (savedPreferences.getBoolean("dark_theme", false)) {
504 darkThemePreference.setIcon(R.drawable.theme_dark);
506 darkThemePreference.setIcon(R.drawable.theme_light);
509 // Set the `displayWebpageImagesPreference` icon.
510 if (savedPreferences.getBoolean("display_webpage_images", true)) {
511 if (MainWebViewActivity.darkTheme) {
512 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
514 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
517 if (MainWebViewActivity.darkTheme) {
518 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
520 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
525 // Listen for preference changes.
526 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
528 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
529 @SuppressLint("SetJavaScriptEnabled")
530 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
533 case "javascript_enabled":
535 if (sharedPreferences.getBoolean("javascript_enabled", false)) {
536 // Update the icon for `javascript_enabled`.
537 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
539 // Update the icon for `dom_storage_enabled`.
540 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
541 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
543 if (MainWebViewActivity.darkTheme) {
544 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
546 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
549 } else { // `javascript_enabled` is `false`.
550 // Update the icon for `javascript_enabled`.
551 javaScriptPreference.setIcon(R.drawable.privacy_mode);
553 // Set the icon for `dom_storage_disabled` to be ghosted.
554 if (MainWebViewActivity.darkTheme) {
555 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
557 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
562 case "first_party_cookies_enabled":
563 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
564 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
565 // Set the icon for `first_party_cookies_enabled`.
566 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
568 // Update the icon for `third_party_cookies_enabled`.
569 if (Build.VERSION.SDK_INT >= 21) {
570 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
571 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
573 if (MainWebViewActivity.darkTheme) {
574 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
576 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
580 if (MainWebViewActivity.darkTheme) {
581 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
583 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
586 } else { // `first_party_cookies_enabled` is `false`.
587 // Update the icon for `first_party_cookies_enabled`.
588 if (MainWebViewActivity.darkTheme) {
589 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
591 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
594 // Set the icon for `third_party_cookies_enabled` to be ghosted.
595 if (MainWebViewActivity.darkTheme) {
596 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
598 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
602 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
603 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
606 case "third_party_cookies_enabled":
608 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
609 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
611 if (MainWebViewActivity.darkTheme) {
612 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
614 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
619 case "dom_storage_enabled":
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);
632 case "save_form_data_enabled":
634 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
635 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
637 if (MainWebViewActivity.darkTheme) {
638 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
640 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
645 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
647 switch (userAgentString) {
648 case "WebView default user agent":
649 // Display the user agent as the summary text for `userAgentPreference`.
650 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
652 // Disable `customUserAgentPreference`.
653 customUserAgentPreference.setEnabled(false);
655 // Set the `customUserAgentPreference` icon according to the theme.
656 if (MainWebViewActivity.darkTheme) {
657 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
659 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
663 case "Custom user agent":
664 // Display `Custom user agent` as the summary text for `userAgentPreference`.
665 userAgentPreference.setSummary(R.string.custom_user_agent);
667 // Enable `customUserAgentPreference`.
668 customUserAgentPreference.setEnabled(true);
670 // Set the `customUserAgentPreference` icon according to the theme.
671 if (MainWebViewActivity.darkTheme) {
672 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
674 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
679 // Display the user agent as the summary text for `userAgentPreference`.
680 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
682 // Disable `customUserAgentPreference`.
683 customUserAgentPreference.setEnabled(false);
685 // Set the `customUserAgentPreference` icon according to the theme.
686 if (MainWebViewActivity.darkTheme) {
687 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
689 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
695 case "custom_user_agent":
696 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
697 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
702 if (sharedPreferences.getBoolean("block_ads", true)) {
703 if (MainWebViewActivity.darkTheme) {
704 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_dark);
706 blockAdsPreference.setIcon(R.drawable.block_ads_enabled_light);
709 if (MainWebViewActivity.darkTheme) {
710 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_dark);
712 blockAdsPreference.setIcon(R.drawable.block_ads_disabled_light);
717 case "incognito_mode":
719 if (sharedPreferences.getBoolean("incognito_mode", false)) {
720 if (MainWebViewActivity.darkTheme) {
721 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
723 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
726 if (MainWebViewActivity.darkTheme) {
727 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
729 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
736 if (sharedPreferences.getBoolean("do_not_track", false)) {
737 if (MainWebViewActivity.darkTheme) {
738 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_dark);
740 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled_light);
743 if (MainWebViewActivity.darkTheme) {
744 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_dark);
746 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled_light);
752 case "proxy_through_orbot":
753 // Get current settings.
754 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
755 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
757 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
758 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
761 if (currentProxyThroughOrbot) {
762 // Set the Tor icons according to the theme.
763 if (MainWebViewActivity.darkTheme) {
764 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
765 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
766 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
768 // Set the `torSearchCustomURLPreference` icon.
769 if (torSearchCustomURLPreference.isEnabled()) {
770 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
772 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
775 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
776 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
777 torSearchPreference.setIcon(R.drawable.search_enabled_light);
779 // Set the `torSearchCustomURLPreference` icon.
780 if (torSearchCustomURLPreference.isEnabled()) {
781 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
783 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
786 } else { // Proxy through Orbot is disabled.
787 if (MainWebViewActivity.darkTheme) {
788 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
789 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
790 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
791 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
793 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
794 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
795 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
796 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
802 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
803 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
807 // Get the present search string.
808 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
810 // Update the preferences.
811 if (presentTorSearchString.equals("Custom URL")) {
812 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
813 torSearchPreference.setSummary(R.string.custom_url);
815 // Enable `torSearchCustomURLPreference`.
816 torSearchCustomURLPreference.setEnabled(true);
818 // Update the `torSearchCustomURLPreference` icon.
819 if (MainWebViewActivity.darkTheme) {
820 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
822 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
825 // Set the array value as the summary text.
826 torSearchPreference.setSummary(presentTorSearchString);
828 // Disable `torSearchCustomURLPreference`.
829 torSearchCustomURLPreference.setEnabled(false);
831 // Update the `torSearchCustomURLPreference` icon.
832 if (MainWebViewActivity.darkTheme) {
833 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
835 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
840 case "tor_search_custom_url":
841 // Set the summary text for `tor_search_custom_url`.
842 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
846 // Store the new search string.
847 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
849 // Update `searchPreference` and `searchCustomURLPreference`.
850 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
851 // Set the summary text to `R.string.custom_url`, which is translated.
852 searchPreference.setSummary(R.string.custom_url);
854 // Enable `searchCustomURLPreference`.
855 searchCustomURLPreference.setEnabled(true);
857 // Set the `searchCustomURLPreference` according to the theme.
858 if (MainWebViewActivity.darkTheme) {
859 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
861 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
863 } else { // `Custom URL` is not selected.
864 // Set the summary text to `newSearchString`.
865 searchPreference.setSummary(newSearchString);
867 // Disable `searchCustomURLPreference`.
868 searchCustomURLPreference.setEnabled(false);
870 // Set the `searchCustomURLPreference` according to the theme.
871 if (MainWebViewActivity.darkTheme) {
872 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
874 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
879 case "search_custom_url":
880 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
881 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
884 case "full_screen_browsing_mode":
885 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
886 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
887 if (MainWebViewActivity.darkTheme) {
888 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
890 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
893 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
894 // Disable `translucentNavigationBarPreference`.
895 translucentNavigationBarPreference.setEnabled(false);
897 // Set the icons according to the theme.
898 if (MainWebViewActivity.darkTheme) {
899 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
900 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
902 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
903 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
905 } else { // `hide_system_bars` is `false`.
906 // Enable `translucentNavigationBarPreference`.
907 translucentNavigationBarPreference.setEnabled(true);
909 // Set the icons according to the theme.
910 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
911 // Set the `hideSystemBarsPreference` icon.
912 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
914 // Set the `translucentNavigationBarPreference` icon.
915 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
916 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
918 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
920 } else { // Use the light theme.
921 // Set the `hideSystemBarsPreference` icon.
922 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
924 // Set the `translucentNavigationBarPreference` icon.
925 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
926 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
928 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
932 } else { // `full_screen_browsing_mode` is false.
933 // Disable `translucentNavigationBarPreference`.
934 translucentNavigationBarPreference.setEnabled(false);
936 // Update the icons according to the theme.
937 if (MainWebViewActivity.darkTheme) {
938 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
939 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
940 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
942 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
943 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
944 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
949 case "hide_system_bars":
950 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
951 // Disable `translucentNavigationBarPreference`.
952 translucentNavigationBarPreference.setEnabled(false);
954 // Set the icons according to the theme.
955 if (MainWebViewActivity.darkTheme) {
956 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
957 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
959 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
960 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
962 } else { // `hide_system_bars` is false.
963 // Enable `translucentNavigationBarPreference`.
964 translucentNavigationBarPreference.setEnabled(true);
966 // Set the icons according to the theme.
967 if (MainWebViewActivity.darkTheme) {
968 // Set the `hideSystemBarsPreference` icon.
969 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
971 // Set the `translucentNavigationBarPreference` icon.
972 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
973 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
975 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
978 // Set the `hideSystemBarsPreference` icon.
979 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
981 // Set the `translucentNavigationBarPreference` icon.
982 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
983 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
985 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
991 case "translucent_navigation_bar":
993 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
994 if (MainWebViewActivity.darkTheme) {
995 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
997 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1000 if (MainWebViewActivity.darkTheme) {
1001 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1003 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1008 case "clear_everything":
1009 // Store the new `clear_everything` status
1010 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1012 // Update the status of the `Clear and Exit` preferences.
1013 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1014 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1015 clearFormDataPreference.setEnabled(!newClearEverythingBoolean);
1016 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1018 // Update the `clearEverythingPreference` icon.
1019 if (newClearEverythingBoolean) {
1020 if (MainWebViewActivity.darkTheme) {
1021 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1023 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1026 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1029 // Update the `clearCookiesPreference` icon.
1030 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1031 if (MainWebViewActivity.darkTheme) {
1032 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1034 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1037 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1040 // Update the `clearDomStoragePreference` icon.
1041 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1042 if (MainWebViewActivity.darkTheme) {
1043 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1045 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1048 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1051 // Update the `clearFormDataPreference` icon.
1052 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1053 if (MainWebViewActivity.darkTheme) {
1054 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1056 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1059 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1062 // Update the `clearCachePreference` icon.
1063 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1064 if (MainWebViewActivity.darkTheme) {
1065 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1067 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1070 clearCachePreference.setIcon(R.drawable.cache_warning);
1074 case "clear_cookies":
1076 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1077 if (MainWebViewActivity.darkTheme) {
1078 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1080 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1083 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1087 case "clear_dom_storage":
1089 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1090 if (MainWebViewActivity.darkTheme) {
1091 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1093 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1096 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1100 case "clear_form_data":
1102 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1103 if (MainWebViewActivity.darkTheme) {
1104 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1106 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1109 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1115 if (sharedPreferences.getBoolean("clear_cache", true)) {
1116 if (MainWebViewActivity.darkTheme) {
1117 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1119 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1122 clearCachePreference.setIcon(R.drawable.cache_warning);
1127 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
1128 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
1131 case "default_font_size":
1132 // Update the summary text of `default_font_size`.
1133 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1136 case "swipe_to_refresh":
1138 if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
1139 if (MainWebViewActivity.darkTheme) {
1140 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1142 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1145 if (MainWebViewActivity.darkTheme) {
1146 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1148 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1153 case "display_additional_app_bar_icons":
1155 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1156 if (MainWebViewActivity.darkTheme) {
1157 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1159 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1162 if (MainWebViewActivity.darkTheme) {
1163 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1165 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1172 if (sharedPreferences.getBoolean("dark_theme", false)) {
1173 darkThemePreference.setIcon(R.drawable.theme_dark);
1175 darkThemePreference.setIcon(R.drawable.theme_light);
1178 // Create an `Intent` to restart Privacy Browser.
1179 Intent intent = getActivity().getParentActivityIntent();
1181 // Assert that `intent` is not `null` to remove the lint error below.
1182 assert intent != null;
1184 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1185 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1188 startActivity(intent);
1191 case "display_webpage_images":
1192 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1194 if (MainWebViewActivity.darkTheme) {
1195 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1197 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1200 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1201 MainWebViewActivity.reloadOnRestartBoolean = false;
1204 if (MainWebViewActivity.darkTheme) {
1205 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1207 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1210 // Set `mainWebView` to reload on restart to remove the current images.
1211 MainWebViewActivity.reloadOnRestartBoolean = true;
1218 // Register the listener.
1219 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1222 // 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
1223 // even while running in the foreground.
1225 public void onPause() {
1227 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1231 public void onResume() {
1233 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);