2 * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.fragments;
22 import android.annotation.SuppressLint;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.preference.Preference;
29 import android.preference.PreferenceFragment;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.webkit.WebView;
33 import android.widget.ArrayAdapter;
35 import com.stoutner.privacybrowser.R;
36 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
38 public class SettingsFragment extends PreferenceFragment {
39 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
40 private SharedPreferences savedPreferences;
43 public void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 addPreferencesFromResource(R.xml.preferences);
47 // Get a handle for the context.
48 Context context = getActivity().getApplicationContext();
50 // Initialize savedPreferences.
51 savedPreferences = getPreferenceScreen().getSharedPreferences();
53 // Get handles for the preferences we need to modify.
54 final Preference javaScriptPreference = findPreference("javascript_enabled");
55 final Preference firstPartyCookiesPreference = findPreference("first_party_cookies_enabled");
56 final Preference thirdPartyCookiesPreference = findPreference("third_party_cookies_enabled");
57 final Preference domStoragePreference = findPreference("dom_storage_enabled");
58 final Preference saveFormDataPreference = findPreference("save_form_data_enabled");
59 final Preference userAgentPreference = findPreference("user_agent");
60 final Preference customUserAgentPreference = findPreference("custom_user_agent");
61 final Preference incognitoModePreference = findPreference("incognito_mode");
62 final Preference doNotTrackPreference = findPreference("do_not_track");
63 final Preference easyListPreference = findPreference("easylist");
64 final Preference easyPrivacyPreference = findPreference("easyprivacy");
65 final Preference fanboyAnnoyanceListPreference = findPreference("fanboy_annoyance_list");
66 final Preference fanboySocialBlockingListPreference = findPreference("fanboy_social_blocking_list");
67 final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
68 final Preference torHomepagePreference = findPreference("tor_homepage");
69 final Preference torSearchPreference = findPreference("tor_search");
70 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
71 final Preference searchPreference = findPreference("search");
72 final Preference searchCustomURLPreference = findPreference("search_custom_url");
73 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
74 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
75 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
76 final Preference clearEverythingPreference = findPreference("clear_everything");
77 final Preference clearCookiesPreference = findPreference("clear_cookies");
78 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
79 final Preference clearFormDataPreference = findPreference("clear_form_data");
80 final Preference clearCachePreference = findPreference("clear_cache");
81 final Preference homepagePreference = findPreference("homepage");
82 final Preference defaultFontSizePreference = findPreference("default_font_size");
83 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
84 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
85 final Preference darkThemePreference = findPreference("dark_theme");
86 final Preference nightModePreference = findPreference("night_mode");
87 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
90 torHomepagePreference.setDependency("proxy_through_orbot");
91 torSearchPreference.setDependency("proxy_through_orbot");
92 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
94 // Get Strings from the preferences.
95 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
96 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
98 // Get booleans from the preferences.
99 final boolean javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
100 boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false);
101 boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false);
102 boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboy_annoyance_list", true);
103 boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboy_social_blocking_list", true);
104 boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
105 boolean fullScreenBrowsingMode = savedPreferences.getBoolean("full_screen_browsing_mode", false);
106 boolean hideSystemBars = savedPreferences.getBoolean("hide_system_bars", false);
107 boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
108 final boolean nightMode = savedPreferences.getBoolean("night_mode", false);
110 // Only enable the third-party preference if first-party cookies are enabled and API >= 21.
111 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabled && (Build.VERSION.SDK_INT >= 21));
113 // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
114 domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
116 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
117 fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
119 // We need to inflated a `WebView` to get the default user agent.
120 LayoutInflater inflater = getActivity().getLayoutInflater();
121 // `@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.
122 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
123 final WebView bareWebView = bareWebViewLayout.findViewById(R.id.bare_webview);
125 // Get the user agent arrays.
126 ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(context, R.array.user_agent_names, R.layout.domain_settings_spinner_item);
127 String[] translatedUserAgentNamesArray = getResources().getStringArray(R.array.translated_user_agent_names);
128 String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
130 // Get the current user agent name from the preference.
131 String userAgentName = savedPreferences.getString("user_agent", "Privacy Browser");
133 // Get the array position of the user agent name.
134 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
136 // Populate the user agent summary.
137 switch (userAgentArrayPosition) {
138 case MainWebViewActivity.UNRECOGNIZED_USER_AGENT: // The user agent name is not on the canonical list.
139 // This is probably because it was set in an older version of Privacy Browser before the switch to persistent user agent names. Use the current user agent entry name as the summary.
140 userAgentPreference.setSummary(userAgentName);
143 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
144 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
145 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + bareWebView.getSettings().getUserAgentString());
148 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
149 // Set the summary text.
150 userAgentPreference.setSummary(R.string.custom_user_agent);
154 // Get the user agent summary from the user agent data array.
155 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + userAgentDataArray[userAgentArrayPosition]);
158 // Set the summary text for the custom user agent preference and enable it if user agent preference is set to custom.
159 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
160 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals(getString(R.string.custom_user_agent)));
163 // 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`.
164 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
167 // 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=`
168 if (torSearchString.equals("Custom URL")) {
169 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
170 torSearchPreference.setSummary(R.string.custom_url);
172 // Set the array value as the summary text.
173 torSearchPreference.setSummary(torSearchString);
176 // Set the summary text for `tor_search_custom_url`. The default is `""`.
177 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
179 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
180 torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
183 // 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=`.
184 if (searchString.equals("Custom URL")) {
185 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
186 searchPreference.setSummary(R.string.custom_url);
188 // Set the array value as the summary text.
189 searchPreference.setSummary(searchString);
192 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
193 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
194 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
197 // Enable the translucent navigation bar preference only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
198 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingMode && !hideSystemBars);
200 // Set the status of the `Clear and Exit` preferences.
201 clearCookiesPreference.setEnabled(!clearEverything);
202 clearDomStoragePreference.setEnabled(!clearEverything);
203 clearFormDataPreference.setEnabled(!clearEverything);
204 clearCachePreference.setEnabled(!clearEverything);
206 // Set the homepage URL as the summary text for the homepage preference.
207 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com/?kao=-1&kak=-1"));
209 // Set the default font size as the summary text for the preference.
210 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
212 // Disable `javaScriptPreference` if `nightModeBoolean` is true. JavaScript will be enabled for all web pages.
213 javaScriptPreference.setEnabled(!nightMode);
215 // Set the `javaScriptPreference` icon.
216 if (javaScriptEnabled || nightMode) {
217 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
219 javaScriptPreference.setIcon(R.drawable.privacy_mode);
222 // Set the `firstPartyCookiesPreference` icon.
223 if (firstPartyCookiesEnabled) {
224 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
226 if (MainWebViewActivity.darkTheme) {
227 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
229 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
233 // Set the `thirdPartyCookiesPreference` icon.
234 if (firstPartyCookiesEnabled && Build.VERSION.SDK_INT >= 21) {
235 if (thirdPartyCookiesEnabled) {
236 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
238 if (MainWebViewActivity.darkTheme) {
239 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
241 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
245 if (MainWebViewActivity.darkTheme) {
246 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
248 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
252 // Set the `domStoragePreference` icon.
253 if (javaScriptEnabled || nightMode) { // The preference is enabled.
254 if (savedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
255 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
256 } else { // DOM storage is disabled.
257 if (MainWebViewActivity.darkTheme) {
258 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
260 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
263 } else { // The preference is disabled. The icon should be ghosted.
264 if (MainWebViewActivity.darkTheme) {
265 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
267 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
271 // Set the `saveFormDataPreference` icon.
272 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
273 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
275 if (MainWebViewActivity.darkTheme) {
276 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
278 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
282 // Set the `customUserAgentPreference` icon.
283 if (customUserAgentPreference.isEnabled()) {
284 if (MainWebViewActivity.darkTheme) {
285 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
287 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
290 if (MainWebViewActivity.darkTheme) {
291 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
293 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
297 // Set the `incognitoModePreference` icon.
298 if (savedPreferences.getBoolean("incognito_mode", false)) {
299 if (MainWebViewActivity.darkTheme) {
300 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
302 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
305 if (MainWebViewActivity.darkTheme) {
306 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
308 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
312 // Set the `doNotTrackPreference` icon.
313 if (savedPreferences.getBoolean("do_not_track", false)) {
314 if (MainWebViewActivity.darkTheme) {
315 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
317 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
320 if (MainWebViewActivity.darkTheme) {
321 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
323 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
327 // Set the EasyList icon.
328 if (savedPreferences.getBoolean("easylist", true)) {
329 if (MainWebViewActivity.darkTheme) {
330 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
332 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
335 if (MainWebViewActivity.darkTheme) {
336 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
338 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
342 // Set the EasyPrivacy icon.
343 if (savedPreferences.getBoolean("easyprivacy", true)) {
344 if (MainWebViewActivity.darkTheme) {
345 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
347 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
350 if (MainWebViewActivity.darkTheme) {
351 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
353 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
357 // Set the Fanboy list icons.
358 if (fanboyAnnoyanceListEnabled) {
359 if (MainWebViewActivity.darkTheme) {
360 // Set the Fanboy annoyance list icon.
361 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
363 // Set the Fanboy social blocking list icon.
364 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
366 // Set the Fanboy annoyance list icon.
367 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
369 // Set the Fanboy social blocking list icon.
370 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
373 if (MainWebViewActivity.darkTheme) {
374 // Set the Fanboy annoyance list icon.
375 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
377 // Set the Fanboy social blocking list icon.
378 if (fanboySocialBlockingEnabled) {
379 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
381 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
384 // Set the Fanboy annoyance list icon.
385 fanboyAnnoyanceListPreference.setIcon(R.drawable.block_ads_disabled_light);
387 // Set the Fanboy social blocking list icon.
388 if (fanboySocialBlockingEnabled) {
389 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
391 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
396 // Set the Tor icons according to the theme.
397 if (proxyThroughOrbot) { // Proxying is enabled.
398 if (MainWebViewActivity.darkTheme) {
399 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
400 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
401 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
403 // Set the custom search icon.
404 if (torSearchCustomURLPreference.isEnabled()) {
405 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
407 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
410 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
411 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
412 torSearchPreference.setIcon(R.drawable.search_enabled_light);
414 // Set the custom search icon.
415 if (torSearchCustomURLPreference.isEnabled()) {
416 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
418 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
421 } else { // Proxying is disabled.
422 if (MainWebViewActivity.darkTheme) {
423 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
424 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
425 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
426 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
428 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
429 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
430 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
431 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
435 // Set the `searchCustomURLPreference` icon.
436 if (searchCustomURLPreference.isEnabled()) {
437 if (MainWebViewActivity.darkTheme) {
438 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
440 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
443 if (MainWebViewActivity.darkTheme) {
444 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
446 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
450 // Set the full screen browsing mode icons.
451 if (fullScreenBrowsingMode) { // Full screen browsing mode is enabled.
452 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
453 if (MainWebViewActivity.darkTheme) {
454 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
456 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
459 if (hideSystemBars) { // `hideSystemBarsBoolean` is `true`.
460 // Set the icons according to the theme.
461 if (MainWebViewActivity.darkTheme) {
462 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
463 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
465 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
466 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
468 } else { // `hideSystemBarsBoolean` is `false`.
469 // Set the `hideSystemBarsPreference` icon according to the theme.
470 if (MainWebViewActivity.darkTheme) {
471 // Set the icon for `hideSystemBarsPreference`.
472 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
474 // Set the icon for `translucentNavigationBarPreference`.
475 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
476 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
478 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
481 // Set the icon for `hideSystemBarsPreference`.
482 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
484 // Set the icon for `translucentNavigationBarPreference`.
485 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
486 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
488 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
492 } else { // Full screen browsing mode is disabled.
493 // Set the icons according to the theme.
494 if (MainWebViewActivity.darkTheme) {
495 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
496 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
497 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
499 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
500 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
501 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
505 // Set the clear everything preference icon.
506 if (clearEverything) {
507 if (MainWebViewActivity.darkTheme) {
508 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
510 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
513 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
516 // Set the clear cookies preference icon.
517 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
518 if (MainWebViewActivity.darkTheme) {
519 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
521 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
524 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
527 // Set the clear DOM storage preference icon.
528 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
529 if (MainWebViewActivity.darkTheme) {
530 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
532 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
535 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
538 // Set the clear form data preference icon.
539 if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
540 if (MainWebViewActivity.darkTheme) {
541 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
543 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
546 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
549 // Set the clear cache preference icon.
550 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
551 if (MainWebViewActivity.darkTheme) {
552 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
554 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
557 clearCachePreference.setIcon(R.drawable.cache_warning);
560 // Set the swipe to refresh preference icon.
561 if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
562 if (MainWebViewActivity.darkTheme) {
563 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
565 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
568 if (MainWebViewActivity.darkTheme) {
569 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
571 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
575 // Set the display additional app bar icons preference icon.
576 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
577 if (MainWebViewActivity.darkTheme) {
578 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
580 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
583 if (MainWebViewActivity.darkTheme) {
584 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
586 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
590 // Set the dark theme preference icon.
591 if (savedPreferences.getBoolean("dark_theme", false)) {
592 darkThemePreference.setIcon(R.drawable.theme_dark);
594 darkThemePreference.setIcon(R.drawable.theme_light);
597 // Set the night mode preference icon.
599 if (MainWebViewActivity.darkTheme) {
600 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
602 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
605 if (MainWebViewActivity.darkTheme) {
606 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
608 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
612 // Set the display webpage images preference icon.
613 if (savedPreferences.getBoolean("display_webpage_images", true)) {
614 if (MainWebViewActivity.darkTheme) {
615 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
617 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
620 if (MainWebViewActivity.darkTheme) {
621 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
623 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
628 // Listen for preference changes.
629 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
631 case "javascript_enabled":
632 // Update the icons and the DOM storage preference status.
633 if (sharedPreferences.getBoolean("javascript_enabled", false)) { // The JavaScript preference is enabled.
634 // Update the icon for the JavaScript preference.
635 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
637 // Update the status of the DOM storage preference.
638 domStoragePreference.setEnabled(true);
640 // Update the icon for the DOM storage preference.
641 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
642 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
644 if (MainWebViewActivity.darkTheme) {
645 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
647 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
650 } else { // The JavaScript preference is disabled.
651 // Update the icon for the JavaScript preference.
652 javaScriptPreference.setIcon(R.drawable.privacy_mode);
654 // Update the status of the DOM storage preference.
655 domStoragePreference.setEnabled(false);
657 // Set the icon for DOM storage preference to be ghosted.
658 if (MainWebViewActivity.darkTheme) {
659 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
661 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
666 case "first_party_cookies_enabled":
667 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
668 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
669 // Set the icon for `first_party_cookies_enabled`.
670 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
672 // Update the icon for `third_party_cookies_enabled`.
673 if (Build.VERSION.SDK_INT >= 21) {
674 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
675 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
677 if (MainWebViewActivity.darkTheme) {
678 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
680 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
684 if (MainWebViewActivity.darkTheme) {
685 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
687 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
690 } else { // `first_party_cookies_enabled` is `false`.
691 // Update the icon for `first_party_cookies_enabled`.
692 if (MainWebViewActivity.darkTheme) {
693 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
695 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
698 // Set the icon for `third_party_cookies_enabled` to be ghosted.
699 if (MainWebViewActivity.darkTheme) {
700 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
702 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
706 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
707 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
710 case "third_party_cookies_enabled":
712 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
713 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
715 if (MainWebViewActivity.darkTheme) {
716 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
718 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
723 case "dom_storage_enabled":
725 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
726 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
728 if (MainWebViewActivity.darkTheme) {
729 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
731 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
736 case "save_form_data_enabled":
738 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
739 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
741 if (MainWebViewActivity.darkTheme) {
742 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
744 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
749 // Get the new user agent name.
750 String newUserAgentName = sharedPreferences.getString("user_agent", "Privacy Browser");
752 // Get the array position for the new user agent name.
753 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
755 // Get the translated new user agent name.
756 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
758 // Populate the user agent summary.
759 switch (newUserAgentArrayPosition) {
760 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
761 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
762 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
764 // Disable the custom user agent preference.
765 customUserAgentPreference.setEnabled(false);
767 // Set the custom user agent preference icon according to the theme.
768 if (MainWebViewActivity.darkTheme) {
769 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
771 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
775 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
776 // Set the summary text.
777 userAgentPreference.setSummary(R.string.custom_user_agent);
779 // Enable the custom user agent preference.
780 customUserAgentPreference.setEnabled(true);
782 // Set the custom user agent preference icon according to the theme.
783 if (MainWebViewActivity.darkTheme) {
784 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
786 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
791 // Get the user agent summary from the user agent data array.
792 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
794 // Disable the custom user agent preference.
795 customUserAgentPreference.setEnabled(false);
797 // Set the custom user agent preference icon according to the theme.
798 if (MainWebViewActivity.darkTheme) {
799 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
801 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
806 case "custom_user_agent":
807 // Set the new custom user agent as the summary text for the preference.
808 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
811 case "incognito_mode":
813 if (sharedPreferences.getBoolean("incognito_mode", false)) {
814 if (MainWebViewActivity.darkTheme) {
815 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
817 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
820 if (MainWebViewActivity.darkTheme) {
821 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
823 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
830 if (sharedPreferences.getBoolean("do_not_track", false)) {
831 if (MainWebViewActivity.darkTheme) {
832 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
834 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
837 if (MainWebViewActivity.darkTheme) {
838 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
840 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
848 if (sharedPreferences.getBoolean("easylist", true)) {
849 if (MainWebViewActivity.darkTheme) {
850 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
852 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
855 if (MainWebViewActivity.darkTheme) {
856 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
858 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
865 if (sharedPreferences.getBoolean("easyprivacy", true)) {
866 if (MainWebViewActivity.darkTheme) {
867 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
869 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
872 if (MainWebViewActivity.darkTheme) {
873 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
875 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
880 case "fanboy_annoyance_list":
881 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboy_annoyance_list", true);
882 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboy_social_blocking_list", true);
884 // Update the Fanboy icons.
885 if (sharedPreferences.getBoolean("fanboy_annoyance_list", true)) {
886 if (MainWebViewActivity.darkTheme) {
887 // Update the Fanboy's annoyance list icon.
888 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
890 // Update the Fanboy's social blocking list icon.
891 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
893 // Update the Fanboy's annoyance list icon.
894 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
896 // Update the Fanboy's social blocking list icon.
897 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
900 if (MainWebViewActivity.darkTheme) {
901 // Update the Fanboy's annoyance list icon.
902 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
904 // Update the Fanboy's social blocking list icon.
905 if (currentFanboySocialBlockingList) {
906 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
908 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
911 // Update the Fanboy's annoyance list icon.
912 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
914 // Update the Fanboy's social blocking list icon.
915 if (currentFanboySocialBlockingList) {
916 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
918 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
923 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
924 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
927 case "fanboy_social_blocking_list":
929 if (sharedPreferences.getBoolean("fanboy_social_blocking_list", true)) {
930 if (MainWebViewActivity.darkTheme) {
931 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
933 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
936 if (MainWebViewActivity.darkTheme) {
937 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
939 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
944 case "proxy_through_orbot":
945 // Get current settings.
946 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
947 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
949 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
950 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
953 if (currentProxyThroughOrbot) {
954 // Set the Tor icons according to the theme.
955 if (MainWebViewActivity.darkTheme) {
956 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
957 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
958 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
960 // Set the `torSearchCustomURLPreference` icon.
961 if (torSearchCustomURLPreference.isEnabled()) {
962 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
964 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
967 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
968 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
969 torSearchPreference.setIcon(R.drawable.search_enabled_light);
971 // Set the `torSearchCustomURLPreference` icon.
972 if (torSearchCustomURLPreference.isEnabled()) {
973 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
975 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
978 } else { // Proxy through Orbot is disabled.
979 if (MainWebViewActivity.darkTheme) {
980 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
981 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
982 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
983 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
985 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
986 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
987 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
988 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
994 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
995 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
999 // Get the present search string.
1000 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
1002 // Update the preferences.
1003 if (presentTorSearchString.equals("Custom URL")) {
1004 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1005 torSearchPreference.setSummary(R.string.custom_url);
1007 // Enable `torSearchCustomURLPreference`.
1008 torSearchCustomURLPreference.setEnabled(true);
1010 // Update the `torSearchCustomURLPreference` icon.
1011 if (MainWebViewActivity.darkTheme) {
1012 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1014 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1017 // Set the array value as the summary text.
1018 torSearchPreference.setSummary(presentTorSearchString);
1020 // Disable `torSearchCustomURLPreference`.
1021 torSearchCustomURLPreference.setEnabled(false);
1023 // Update the `torSearchCustomURLPreference` icon.
1024 if (MainWebViewActivity.darkTheme) {
1025 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1027 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1032 case "tor_search_custom_url":
1033 // Set the summary text for `tor_search_custom_url`.
1034 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
1038 // Store the new search string.
1039 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
1041 // Update `searchPreference` and `searchCustomURLPreference`.
1042 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1043 // Set the summary text to `R.string.custom_url`, which is translated.
1044 searchPreference.setSummary(R.string.custom_url);
1046 // Enable `searchCustomURLPreference`.
1047 searchCustomURLPreference.setEnabled(true);
1049 // Set the `searchCustomURLPreference` according to the theme.
1050 if (MainWebViewActivity.darkTheme) {
1051 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1053 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1055 } else { // `Custom URL` is not selected.
1056 // Set the summary text to `newSearchString`.
1057 searchPreference.setSummary(newSearchString);
1059 // Disable `searchCustomURLPreference`.
1060 searchCustomURLPreference.setEnabled(false);
1062 // Set the `searchCustomURLPreference` according to the theme.
1063 if (MainWebViewActivity.darkTheme) {
1064 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1066 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1071 case "search_custom_url":
1072 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1073 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
1076 case "full_screen_browsing_mode":
1077 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
1078 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
1079 if (MainWebViewActivity.darkTheme) {
1080 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1082 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1085 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
1086 // Disable `translucentNavigationBarPreference`.
1087 translucentNavigationBarPreference.setEnabled(false);
1089 // Set the icons according to the theme.
1090 if (MainWebViewActivity.darkTheme) {
1091 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1092 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1094 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1095 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1097 } else { // `hide_system_bars` is `false`.
1098 // Enable `translucentNavigationBarPreference`.
1099 translucentNavigationBarPreference.setEnabled(true);
1101 // Set the icons according to the theme.
1102 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
1103 // Set the `hideSystemBarsPreference` icon.
1104 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1106 // Set the `translucentNavigationBarPreference` icon.
1107 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1108 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1110 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1112 } else { // Use the light theme.
1113 // Set the `hideSystemBarsPreference` icon.
1114 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1116 // Set the `translucentNavigationBarPreference` icon.
1117 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1118 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1120 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1124 } else { // `full_screen_browsing_mode` is false.
1125 // Disable `translucentNavigationBarPreference`.
1126 translucentNavigationBarPreference.setEnabled(false);
1128 // Update the icons according to the theme.
1129 if (MainWebViewActivity.darkTheme) {
1130 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1131 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
1132 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1134 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1135 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
1136 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1141 case "hide_system_bars":
1142 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
1143 // Disable `translucentNavigationBarPreference`.
1144 translucentNavigationBarPreference.setEnabled(false);
1146 // Set the icons according to the theme.
1147 if (MainWebViewActivity.darkTheme) {
1148 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1149 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1151 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1152 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1154 } else { // `hide_system_bars` is false.
1155 // Enable `translucentNavigationBarPreference`.
1156 translucentNavigationBarPreference.setEnabled(true);
1158 // Set the icons according to the theme.
1159 if (MainWebViewActivity.darkTheme) {
1160 // Set the `hideSystemBarsPreference` icon.
1161 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1163 // Set the `translucentNavigationBarPreference` icon.
1164 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1165 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1167 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1170 // Set the `hideSystemBarsPreference` icon.
1171 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1173 // Set the `translucentNavigationBarPreference` icon.
1174 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1175 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1177 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1183 case "translucent_navigation_bar":
1185 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1186 if (MainWebViewActivity.darkTheme) {
1187 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1189 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1192 if (MainWebViewActivity.darkTheme) {
1193 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1195 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1200 case "clear_everything":
1201 // Store the new `clear_everything` status
1202 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1204 // Update the status of the `Clear and Exit` preferences.
1205 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1206 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1207 clearFormDataPreference.setEnabled(!newClearEverythingBoolean);
1208 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1210 // Update the `clearEverythingPreference` icon.
1211 if (newClearEverythingBoolean) {
1212 if (MainWebViewActivity.darkTheme) {
1213 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1215 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1218 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1221 // Update the `clearCookiesPreference` icon.
1222 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1223 if (MainWebViewActivity.darkTheme) {
1224 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1226 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1229 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1232 // Update the `clearDomStoragePreference` icon.
1233 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1234 if (MainWebViewActivity.darkTheme) {
1235 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1237 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1240 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1243 // Update the `clearFormDataPreference` icon.
1244 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1245 if (MainWebViewActivity.darkTheme) {
1246 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1248 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1251 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1254 // Update the `clearCachePreference` icon.
1255 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1256 if (MainWebViewActivity.darkTheme) {
1257 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1259 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1262 clearCachePreference.setIcon(R.drawable.cache_warning);
1266 case "clear_cookies":
1268 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1269 if (MainWebViewActivity.darkTheme) {
1270 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1272 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1275 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1279 case "clear_dom_storage":
1281 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1282 if (MainWebViewActivity.darkTheme) {
1283 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1285 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1288 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1292 case "clear_form_data":
1294 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1295 if (MainWebViewActivity.darkTheme) {
1296 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1298 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1301 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1307 if (sharedPreferences.getBoolean("clear_cache", true)) {
1308 if (MainWebViewActivity.darkTheme) {
1309 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1311 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1314 clearCachePreference.setIcon(R.drawable.cache_warning);
1319 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
1320 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
1323 case "default_font_size":
1324 // Update the summary text of `default_font_size`.
1325 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1328 case "swipe_to_refresh":
1330 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1331 if (MainWebViewActivity.darkTheme) {
1332 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1334 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1337 if (MainWebViewActivity.darkTheme) {
1338 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1340 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1345 case "display_additional_app_bar_icons":
1347 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1348 if (MainWebViewActivity.darkTheme) {
1349 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1351 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1354 if (MainWebViewActivity.darkTheme) {
1355 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1357 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1364 if (sharedPreferences.getBoolean("dark_theme", false)) {
1365 darkThemePreference.setIcon(R.drawable.theme_dark);
1367 darkThemePreference.setIcon(R.drawable.theme_light);
1370 // Create an `Intent` to restart Privacy Browser.
1371 Intent intent = getActivity().getParentActivityIntent();
1373 // Assert that `intent` is not `null` to remove the lint error below.
1374 assert intent != null;
1376 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1377 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1380 startActivity(intent);
1384 // Set the URL to be reloaded on restart to apply the new night mode setting.
1385 MainWebViewActivity.loadUrlOnRestart = true;
1387 // Store the current night mode status.
1388 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1389 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript_enabled", false);
1392 if (currentNightModeBoolean) {
1393 if (MainWebViewActivity.darkTheme) {
1394 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1396 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1399 if (MainWebViewActivity.darkTheme) {
1400 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1402 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1406 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1407 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1408 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1410 // Update the `javaScriptPreference` icon.
1411 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1412 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1414 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1417 // Update the `domStoragePreference` icon.
1418 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1419 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
1420 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1421 } else { // DOM storage is disabled.
1422 if (MainWebViewActivity.darkTheme) {
1423 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1425 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1428 } else { // The preference is disabled. The icon should be ghosted.
1429 if (MainWebViewActivity.darkTheme) {
1430 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1432 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1437 case "display_webpage_images":
1438 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1440 if (MainWebViewActivity.darkTheme) {
1441 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1443 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1446 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1447 MainWebViewActivity.reloadOnRestart = false;
1450 if (MainWebViewActivity.darkTheme) {
1451 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1453 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1456 // Set `mainWebView` to reload on restart to remove the current images.
1457 MainWebViewActivity.reloadOnRestart = true;
1463 // Register the listener.
1464 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1467 // 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
1468 // even while running in the foreground.
1470 public void onPause() {
1472 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1476 public void onResume() {
1478 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);