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 allowScreenshotsPreference = findPreference("allow_screenshots");
64 final Preference easyListPreference = findPreference("easylist");
65 final Preference easyPrivacyPreference = findPreference("easyprivacy");
66 final Preference fanboyAnnoyanceListPreference = findPreference("fanboy_annoyance_list");
67 final Preference fanboySocialBlockingListPreference = findPreference("fanboy_social_blocking_list");
68 final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
69 final Preference torHomepagePreference = findPreference("tor_homepage");
70 final Preference torSearchPreference = findPreference("tor_search");
71 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
72 final Preference searchPreference = findPreference("search");
73 final Preference searchCustomURLPreference = findPreference("search_custom_url");
74 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
75 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
76 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
77 final Preference clearEverythingPreference = findPreference("clear_everything");
78 final Preference clearCookiesPreference = findPreference("clear_cookies");
79 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
80 final Preference clearFormDataPreference = findPreference("clear_form_data");
81 final Preference clearCachePreference = findPreference("clear_cache");
82 final Preference homepagePreference = findPreference("homepage");
83 final Preference defaultFontSizePreference = findPreference("default_font_size");
84 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
85 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
86 final Preference darkThemePreference = findPreference("dark_theme");
87 final Preference nightModePreference = findPreference("night_mode");
88 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
91 torHomepagePreference.setDependency("proxy_through_orbot");
92 torSearchPreference.setDependency("proxy_through_orbot");
93 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
95 // Get Strings from the preferences.
96 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
97 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
99 // Get booleans from the preferences.
100 final boolean javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
101 boolean firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false);
102 boolean thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false);
103 boolean fanboyAnnoyanceListEnabled = savedPreferences.getBoolean("fanboy_annoyance_list", true);
104 boolean fanboySocialBlockingEnabled = savedPreferences.getBoolean("fanboy_social_blocking_list", true);
105 boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
106 boolean fullScreenBrowsingMode = savedPreferences.getBoolean("full_screen_browsing_mode", false);
107 boolean hideSystemBars = savedPreferences.getBoolean("hide_system_bars", false);
108 boolean clearEverything = savedPreferences.getBoolean("clear_everything", true);
109 final boolean nightMode = savedPreferences.getBoolean("night_mode", false);
111 // Only enable the third-party preference if first-party cookies are enabled and API >= 21.
112 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabled && (Build.VERSION.SDK_INT >= 21));
114 // Only enable the DOM storage preference if either JavaScript or Night Mode is enabled.
115 domStoragePreference.setEnabled(javaScriptEnabled || nightMode);
117 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list is disabled.
118 fanboySocialBlockingListPreference.setEnabled(!fanboyAnnoyanceListEnabled);
120 // We need to inflated a `WebView` to get the default user agent.
121 LayoutInflater inflater = getActivity().getLayoutInflater();
122 // `@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.
123 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
124 final WebView bareWebView = bareWebViewLayout.findViewById(R.id.bare_webview);
126 // Get the user agent arrays.
127 ArrayAdapter<CharSequence> userAgentNamesArray = ArrayAdapter.createFromResource(context, R.array.user_agent_names, R.layout.domain_settings_spinner_item);
128 String[] translatedUserAgentNamesArray = getResources().getStringArray(R.array.translated_user_agent_names);
129 String[] userAgentDataArray = getResources().getStringArray(R.array.user_agent_data);
131 // Get the current user agent name from the preference.
132 String userAgentName = savedPreferences.getString("user_agent", "Privacy Browser");
134 // Get the array position of the user agent name.
135 int userAgentArrayPosition = userAgentNamesArray.getPosition(userAgentName);
137 // Populate the user agent summary.
138 switch (userAgentArrayPosition) {
139 case MainWebViewActivity.UNRECOGNIZED_USER_AGENT: // The user agent name is not on the canonical list.
140 // 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.
141 userAgentPreference.setSummary(userAgentName);
144 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
145 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
146 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + bareWebView.getSettings().getUserAgentString());
149 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
150 // Set the summary text.
151 userAgentPreference.setSummary(R.string.custom_user_agent);
155 // Get the user agent summary from the user agent data array.
156 userAgentPreference.setSummary(translatedUserAgentNamesArray[userAgentArrayPosition] + ":\n" + userAgentDataArray[userAgentArrayPosition]);
159 // Set the summary text for the custom user agent preference and enable it if user agent preference is set to custom.
160 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
161 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals(getString(R.string.custom_user_agent)));
164 // 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`.
165 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
168 // 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=`
169 if (torSearchString.equals("Custom URL")) {
170 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
171 torSearchPreference.setSummary(R.string.custom_url);
173 // Set the array value as the summary text.
174 torSearchPreference.setSummary(torSearchString);
177 // Set the summary text for `tor_search_custom_url`. The default is `""`.
178 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
180 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
181 torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
184 // 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=`.
185 if (searchString.equals("Custom URL")) {
186 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
187 searchPreference.setSummary(R.string.custom_url);
189 // Set the array value as the summary text.
190 searchPreference.setSummary(searchString);
193 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
194 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
195 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
198 // Enable the translucent navigation bar preference only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
199 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingMode && !hideSystemBars);
201 // Set the status of the `Clear and Exit` preferences.
202 clearCookiesPreference.setEnabled(!clearEverything);
203 clearDomStoragePreference.setEnabled(!clearEverything);
204 clearFormDataPreference.setEnabled(!clearEverything);
205 clearCachePreference.setEnabled(!clearEverything);
207 // Set the homepage URL as the summary text for the homepage preference.
208 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com/?kao=-1&kak=-1"));
210 // Set the default font size as the summary text for the preference.
211 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
213 // Disable the JavaScript preference if Night Mode is enabled. JavaScript will be enabled for all web pages.
214 javaScriptPreference.setEnabled(!nightMode);
216 // Set the JavaScript icon.
217 if (javaScriptEnabled || nightMode) {
218 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
220 javaScriptPreference.setIcon(R.drawable.privacy_mode);
223 // Set the first-party cookies icon.
224 if (firstPartyCookiesEnabled) {
225 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
227 if (MainWebViewActivity.darkTheme) {
228 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
230 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
234 // Set the third party cookies icon.
235 if (firstPartyCookiesEnabled && Build.VERSION.SDK_INT >= 21) {
236 if (thirdPartyCookiesEnabled) {
237 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
239 if (MainWebViewActivity.darkTheme) {
240 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
242 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
246 if (MainWebViewActivity.darkTheme) {
247 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
249 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
253 // Set the DOM storage icon.
254 if (javaScriptEnabled || nightMode) { // The preference is enabled.
255 if (savedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
256 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
257 } else { // DOM storage is disabled.
258 if (MainWebViewActivity.darkTheme) {
259 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
261 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
264 } else { // The preference is disabled. The icon should be ghosted.
265 if (MainWebViewActivity.darkTheme) {
266 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
268 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
272 // Set the save form data icon.
273 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
274 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
276 if (MainWebViewActivity.darkTheme) {
277 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
279 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
283 // Set the custom user agent icon.
284 if (customUserAgentPreference.isEnabled()) {
285 if (MainWebViewActivity.darkTheme) {
286 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
288 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
291 if (MainWebViewActivity.darkTheme) {
292 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
294 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
298 // Set the incognito mode icon.
299 if (savedPreferences.getBoolean("incognito_mode", false)) {
300 if (MainWebViewActivity.darkTheme) {
301 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
303 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
306 if (MainWebViewActivity.darkTheme) {
307 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
309 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
313 // Set the Do Not Track icon.
314 if (savedPreferences.getBoolean("do_not_track", false)) {
315 if (MainWebViewActivity.darkTheme) {
316 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
318 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
321 if (MainWebViewActivity.darkTheme) {
322 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
324 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
328 // Set the allow screenshots icon.
329 if (savedPreferences.getBoolean("allow_screenshots", false)) {
330 if (MainWebViewActivity.darkTheme) {
331 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
333 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
336 if (MainWebViewActivity.darkTheme) {
337 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
339 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
343 // Set the EasyList icon.
344 if (savedPreferences.getBoolean("easylist", true)) {
345 if (MainWebViewActivity.darkTheme) {
346 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
348 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
351 if (MainWebViewActivity.darkTheme) {
352 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
354 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
358 // Set the EasyPrivacy icon.
359 if (savedPreferences.getBoolean("easyprivacy", true)) {
360 if (MainWebViewActivity.darkTheme) {
361 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
363 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
366 if (MainWebViewActivity.darkTheme) {
367 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
369 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
373 // Set the Fanboy lists icons.
374 if (fanboyAnnoyanceListEnabled) {
375 if (MainWebViewActivity.darkTheme) {
376 // Set the Fanboy annoyance list icon.
377 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
379 // Set the Fanboy social blocking list icon.
380 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
382 // Set the Fanboy annoyance list icon.
383 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
385 // Set the Fanboy social blocking list icon.
386 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
389 if (MainWebViewActivity.darkTheme) {
390 // Set the Fanboy annoyance list icon.
391 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
393 // Set the Fanboy social blocking list icon.
394 if (fanboySocialBlockingEnabled) {
395 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
397 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
400 // Set the Fanboy annoyance list icon.
401 fanboyAnnoyanceListPreference.setIcon(R.drawable.block_ads_disabled_light);
403 // Set the Fanboy social blocking list icon.
404 if (fanboySocialBlockingEnabled) {
405 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
407 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
412 // Set the Tor icons according to the theme.
413 if (proxyThroughOrbot) { // Proxying is enabled.
414 if (MainWebViewActivity.darkTheme) {
415 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
416 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
417 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
419 // Set the custom search icon.
420 if (torSearchCustomURLPreference.isEnabled()) {
421 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
423 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
426 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
427 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
428 torSearchPreference.setIcon(R.drawable.search_enabled_light);
430 // Set the custom search icon.
431 if (torSearchCustomURLPreference.isEnabled()) {
432 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
434 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
437 } else { // Proxying is disabled.
438 if (MainWebViewActivity.darkTheme) {
439 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
440 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
441 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
442 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
444 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
445 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
446 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
447 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
451 // Set the search custom URL icon.
452 if (searchCustomURLPreference.isEnabled()) {
453 if (MainWebViewActivity.darkTheme) {
454 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
456 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
459 if (MainWebViewActivity.darkTheme) {
460 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
462 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
466 // Set the full screen browsing mode icons.
467 if (fullScreenBrowsingMode) { // Full screen browsing mode is enabled.
468 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
469 if (MainWebViewActivity.darkTheme) {
470 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
472 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
475 if (hideSystemBars) { // `hideSystemBarsBoolean` is `true`.
476 // Set the icons according to the theme.
477 if (MainWebViewActivity.darkTheme) {
478 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
479 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
481 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
482 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
484 } else { // `hideSystemBarsBoolean` is `false`.
485 // Set the `hideSystemBarsPreference` icon according to the theme.
486 if (MainWebViewActivity.darkTheme) {
487 // Set the icon for `hideSystemBarsPreference`.
488 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
490 // Set the icon for `translucentNavigationBarPreference`.
491 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
492 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
494 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
497 // Set the icon for `hideSystemBarsPreference`.
498 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
500 // Set the icon for `translucentNavigationBarPreference`.
501 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
502 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
504 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
508 } else { // Full screen browsing mode is disabled.
509 // Set the icons according to the theme.
510 if (MainWebViewActivity.darkTheme) {
511 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
512 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
513 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
515 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
516 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
517 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
521 // Set the clear everything preference icon.
522 if (clearEverything) {
523 if (MainWebViewActivity.darkTheme) {
524 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
526 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
529 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
532 // Set the clear cookies preference icon.
533 if (clearEverything || savedPreferences.getBoolean("clear_cookies", true)) {
534 if (MainWebViewActivity.darkTheme) {
535 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
537 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
540 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
543 // Set the clear DOM storage preference icon.
544 if (clearEverything || savedPreferences.getBoolean("clear_dom_storage", true)) {
545 if (MainWebViewActivity.darkTheme) {
546 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
548 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
551 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
554 // Set the clear form data preference icon.
555 if (clearEverything || savedPreferences.getBoolean("clear_form_data", true)) {
556 if (MainWebViewActivity.darkTheme) {
557 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
559 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
562 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
565 // Set the clear cache preference icon.
566 if (clearEverything || savedPreferences.getBoolean("clear_cache", true)) {
567 if (MainWebViewActivity.darkTheme) {
568 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
570 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
573 clearCachePreference.setIcon(R.drawable.cache_warning);
576 // Set the swipe to refresh preference icon.
577 if (savedPreferences.getBoolean("swipe_to_refresh", true)) {
578 if (MainWebViewActivity.darkTheme) {
579 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
581 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
584 if (MainWebViewActivity.darkTheme) {
585 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
587 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
591 // Set the display additional app bar icons preference icon.
592 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
593 if (MainWebViewActivity.darkTheme) {
594 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
596 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
599 if (MainWebViewActivity.darkTheme) {
600 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
602 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
606 // Set the dark theme preference icon.
607 if (savedPreferences.getBoolean("dark_theme", false)) {
608 darkThemePreference.setIcon(R.drawable.theme_dark);
610 darkThemePreference.setIcon(R.drawable.theme_light);
613 // Set the night mode preference icon.
615 if (MainWebViewActivity.darkTheme) {
616 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
618 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
621 if (MainWebViewActivity.darkTheme) {
622 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
624 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
628 // Set the display webpage images preference icon.
629 if (savedPreferences.getBoolean("display_webpage_images", true)) {
630 if (MainWebViewActivity.darkTheme) {
631 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
633 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
636 if (MainWebViewActivity.darkTheme) {
637 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
639 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
644 // Listen for preference changes.
645 preferencesListener = (SharedPreferences sharedPreferences, String key) -> {
647 case "javascript_enabled":
648 // Update the icons and the DOM storage preference status.
649 if (sharedPreferences.getBoolean("javascript_enabled", false)) { // The JavaScript preference is enabled.
650 // Update the icon for the JavaScript preference.
651 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
653 // Update the status of the DOM storage preference.
654 domStoragePreference.setEnabled(true);
656 // Update the icon for the DOM storage preference.
657 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
658 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
660 if (MainWebViewActivity.darkTheme) {
661 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
663 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
666 } else { // The JavaScript preference is disabled.
667 // Update the icon for the JavaScript preference.
668 javaScriptPreference.setIcon(R.drawable.privacy_mode);
670 // Update the status of the DOM storage preference.
671 domStoragePreference.setEnabled(false);
673 // Set the icon for DOM storage preference to be ghosted.
674 if (MainWebViewActivity.darkTheme) {
675 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
677 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
682 case "first_party_cookies_enabled":
683 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
684 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
685 // Set the icon for `first_party_cookies_enabled`.
686 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
688 // Update the icon for `third_party_cookies_enabled`.
689 if (Build.VERSION.SDK_INT >= 21) {
690 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
691 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
693 if (MainWebViewActivity.darkTheme) {
694 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
696 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
700 if (MainWebViewActivity.darkTheme) {
701 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
703 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
706 } else { // `first_party_cookies_enabled` is `false`.
707 // Update the icon for `first_party_cookies_enabled`.
708 if (MainWebViewActivity.darkTheme) {
709 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
711 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
714 // Set the icon for `third_party_cookies_enabled` to be ghosted.
715 if (MainWebViewActivity.darkTheme) {
716 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_dark);
718 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted_light);
722 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
723 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
726 case "third_party_cookies_enabled":
728 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
729 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
731 if (MainWebViewActivity.darkTheme) {
732 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_dark);
734 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled_light);
739 case "dom_storage_enabled":
741 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
742 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
744 if (MainWebViewActivity.darkTheme) {
745 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
747 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
752 case "save_form_data_enabled":
754 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
755 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
757 if (MainWebViewActivity.darkTheme) {
758 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_dark);
760 saveFormDataPreference.setIcon(R.drawable.form_data_disabled_light);
765 // Get the new user agent name.
766 String newUserAgentName = sharedPreferences.getString("user_agent", "Privacy Browser");
768 // Get the array position for the new user agent name.
769 int newUserAgentArrayPosition = userAgentNamesArray.getPosition(newUserAgentName);
771 // Get the translated new user agent name.
772 String translatedNewUserAgentName = translatedUserAgentNamesArray[newUserAgentArrayPosition];
774 // Populate the user agent summary.
775 switch (newUserAgentArrayPosition) {
776 case MainWebViewActivity.SETTINGS_WEBVIEW_DEFAULT_USER_AGENT:
777 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
778 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + bareWebView.getSettings().getUserAgentString());
780 // Disable the custom user agent preference.
781 customUserAgentPreference.setEnabled(false);
783 // Set the custom user agent preference icon according to the theme.
784 if (MainWebViewActivity.darkTheme) {
785 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
787 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
791 case MainWebViewActivity.SETTINGS_CUSTOM_USER_AGENT:
792 // Set the summary text.
793 userAgentPreference.setSummary(R.string.custom_user_agent);
795 // Enable the custom user agent preference.
796 customUserAgentPreference.setEnabled(true);
798 // Set the custom user agent preference icon according to the theme.
799 if (MainWebViewActivity.darkTheme) {
800 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_dark);
802 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled_light);
807 // Get the user agent summary from the user agent data array.
808 userAgentPreference.setSummary(translatedNewUserAgentName + ":\n" + userAgentDataArray[newUserAgentArrayPosition]);
810 // Disable the custom user agent preference.
811 customUserAgentPreference.setEnabled(false);
813 // Set the custom user agent preference icon according to the theme.
814 if (MainWebViewActivity.darkTheme) {
815 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_dark);
817 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted_light);
822 case "custom_user_agent":
823 // Set the new custom user agent as the summary text for the preference.
824 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
827 case "incognito_mode":
829 if (sharedPreferences.getBoolean("incognito_mode", false)) {
830 if (MainWebViewActivity.darkTheme) {
831 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_dark);
833 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled_light);
836 if (MainWebViewActivity.darkTheme) {
837 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_dark);
839 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled_light);
846 if (sharedPreferences.getBoolean("do_not_track", false)) {
847 if (MainWebViewActivity.darkTheme) {
848 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_dark);
850 doNotTrackPreference.setIcon(R.drawable.block_tracking_enabled_light);
853 if (MainWebViewActivity.darkTheme) {
854 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_dark);
856 doNotTrackPreference.setIcon(R.drawable.block_tracking_disabled_light);
862 case "allow_screenshots":
864 if (sharedPreferences.getBoolean("allow_screenshots", false)) {
865 if (MainWebViewActivity.darkTheme) {
866 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_dark);
868 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_enabled_light);
871 if (MainWebViewActivity.darkTheme) {
872 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_dark);
874 allowScreenshotsPreference.setIcon(R.drawable.allow_screenshots_disabled_light);
878 // Create an intent to restart Privacy Browser.
879 Intent allowScreenshotsRestartIntent = getActivity().getParentActivityIntent();
881 // Assert that the intent is not null to remove the lint error below.
882 assert allowScreenshotsRestartIntent != null;
884 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
885 allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
888 startActivity(allowScreenshotsRestartIntent);
893 if (sharedPreferences.getBoolean("easylist", true)) {
894 if (MainWebViewActivity.darkTheme) {
895 easyListPreference.setIcon(R.drawable.block_ads_enabled_dark);
897 easyListPreference.setIcon(R.drawable.block_ads_enabled_light);
900 if (MainWebViewActivity.darkTheme) {
901 easyListPreference.setIcon(R.drawable.block_ads_disabled_dark);
903 easyListPreference.setIcon(R.drawable.block_ads_disabled_light);
910 if (sharedPreferences.getBoolean("easyprivacy", true)) {
911 if (MainWebViewActivity.darkTheme) {
912 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_dark);
914 easyPrivacyPreference.setIcon(R.drawable.block_tracking_enabled_light);
917 if (MainWebViewActivity.darkTheme) {
918 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_dark);
920 easyPrivacyPreference.setIcon(R.drawable.block_tracking_disabled_light);
925 case "fanboy_annoyance_list":
926 boolean currentFanboyAnnoyanceList = sharedPreferences.getBoolean("fanboy_annoyance_list", true);
927 boolean currentFanboySocialBlockingList = sharedPreferences.getBoolean("fanboy_social_blocking_list", true);
929 // Update the Fanboy icons.
930 if (sharedPreferences.getBoolean("fanboy_annoyance_list", true)) {
931 if (MainWebViewActivity.darkTheme) {
932 // Update the Fanboy's annoyance list icon.
933 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
935 // Update the Fanboy's social blocking list icon.
936 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_dark);
938 // Update the Fanboy's annoyance list icon.
939 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
941 // Update the Fanboy's social blocking list icon.
942 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_ghosted_light);
945 if (MainWebViewActivity.darkTheme) {
946 // Update the Fanboy's annoyance list icon.
947 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
949 // Update the Fanboy's social blocking list icon.
950 if (currentFanboySocialBlockingList) {
951 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_dark);
953 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_dark);
956 // Update the Fanboy's annoyance list icon.
957 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
959 // Update the Fanboy's social blocking list icon.
960 if (currentFanboySocialBlockingList) {
961 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_enabled_light);
963 fanboyAnnoyanceListPreference.setIcon(R.drawable.social_media_disabled_light);
968 // Only enable Fanboy's social blocking list preference if Fanboy's annoyance list preference is disabled.
969 fanboySocialBlockingListPreference.setEnabled(!currentFanboyAnnoyanceList);
972 case "fanboy_social_blocking_list":
974 if (sharedPreferences.getBoolean("fanboy_social_blocking_list", true)) {
975 if (MainWebViewActivity.darkTheme) {
976 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_dark);
978 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_enabled_light);
981 if (MainWebViewActivity.darkTheme) {
982 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_dark);
984 fanboySocialBlockingListPreference.setIcon(R.drawable.social_media_disabled_light);
989 case "proxy_through_orbot":
990 // Get current settings.
991 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
992 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
994 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
995 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
998 if (currentProxyThroughOrbot) {
999 // Set the Tor icons according to the theme.
1000 if (MainWebViewActivity.darkTheme) {
1001 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_dark);
1002 torHomepagePreference.setIcon(R.drawable.home_enabled_dark);
1003 torSearchPreference.setIcon(R.drawable.search_enabled_dark);
1005 // Set the `torSearchCustomURLPreference` icon.
1006 if (torSearchCustomURLPreference.isEnabled()) {
1007 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1009 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1012 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled_light);
1013 torHomepagePreference.setIcon(R.drawable.home_enabled_light);
1014 torSearchPreference.setIcon(R.drawable.search_enabled_light);
1016 // Set the `torSearchCustomURLPreference` icon.
1017 if (torSearchCustomURLPreference.isEnabled()) {
1018 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1020 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1023 } else { // Proxy through Orbot is disabled.
1024 if (MainWebViewActivity.darkTheme) {
1025 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_dark);
1026 torHomepagePreference.setIcon(R.drawable.home_ghosted_dark);
1027 torSearchPreference.setIcon(R.drawable.search_ghosted_dark);
1028 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1030 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled_light);
1031 torHomepagePreference.setIcon(R.drawable.home_ghosted_light);
1032 torSearchPreference.setIcon(R.drawable.search_ghosted_light);
1033 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1038 case "tor_homepage":
1039 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
1040 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
1044 // Get the present search string.
1045 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
1047 // Update the preferences.
1048 if (presentTorSearchString.equals("Custom URL")) {
1049 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
1050 torSearchPreference.setSummary(R.string.custom_url);
1052 // Enable `torSearchCustomURLPreference`.
1053 torSearchCustomURLPreference.setEnabled(true);
1055 // Update the `torSearchCustomURLPreference` icon.
1056 if (MainWebViewActivity.darkTheme) {
1057 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1059 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1062 // Set the array value as the summary text.
1063 torSearchPreference.setSummary(presentTorSearchString);
1065 // Disable `torSearchCustomURLPreference`.
1066 torSearchCustomURLPreference.setEnabled(false);
1068 // Update the `torSearchCustomURLPreference` icon.
1069 if (MainWebViewActivity.darkTheme) {
1070 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1072 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1077 case "tor_search_custom_url":
1078 // Set the summary text for `tor_search_custom_url`.
1079 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
1083 // Store the new search string.
1084 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
1086 // Update `searchPreference` and `searchCustomURLPreference`.
1087 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
1088 // Set the summary text to `R.string.custom_url`, which is translated.
1089 searchPreference.setSummary(R.string.custom_url);
1091 // Enable `searchCustomURLPreference`.
1092 searchCustomURLPreference.setEnabled(true);
1094 // Set the `searchCustomURLPreference` according to the theme.
1095 if (MainWebViewActivity.darkTheme) {
1096 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_dark);
1098 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled_light);
1100 } else { // `Custom URL` is not selected.
1101 // Set the summary text to `newSearchString`.
1102 searchPreference.setSummary(newSearchString);
1104 // Disable `searchCustomURLPreference`.
1105 searchCustomURLPreference.setEnabled(false);
1107 // Set the `searchCustomURLPreference` according to the theme.
1108 if (MainWebViewActivity.darkTheme) {
1109 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_dark);
1111 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted_light);
1116 case "search_custom_url":
1117 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
1118 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
1121 case "full_screen_browsing_mode":
1122 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
1123 // Set the `fullScreenBrowsingModePreference` icon according to the theme.
1124 if (MainWebViewActivity.darkTheme) {
1125 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_dark);
1127 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled_light);
1130 if (sharedPreferences.getBoolean("hide_system_bars", false)) { // `hide_system_bars` is `true`.
1131 // Disable `translucentNavigationBarPreference`.
1132 translucentNavigationBarPreference.setEnabled(false);
1134 // Set the icons according to the theme.
1135 if (MainWebViewActivity.darkTheme) {
1136 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1137 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1139 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1140 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1142 } else { // `hide_system_bars` is `false`.
1143 // Enable `translucentNavigationBarPreference`.
1144 translucentNavigationBarPreference.setEnabled(true);
1146 // Set the icons according to the theme.
1147 if (MainWebViewActivity.darkTheme) { // Use the dark theme.
1148 // Set the `hideSystemBarsPreference` icon.
1149 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1151 // Set the `translucentNavigationBarPreference` icon.
1152 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1153 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1155 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1157 } else { // Use the light theme.
1158 // Set the `hideSystemBarsPreference` icon.
1159 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1161 // Set the `translucentNavigationBarPreference` icon.
1162 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1163 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1165 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1169 } else { // `full_screen_browsing_mode` is false.
1170 // Disable `translucentNavigationBarPreference`.
1171 translucentNavigationBarPreference.setEnabled(false);
1173 // Update the icons according to the theme.
1174 if (MainWebViewActivity.darkTheme) {
1175 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_dark);
1176 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_dark);
1177 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1179 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled_light);
1180 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted_light);
1181 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1186 case "hide_system_bars":
1187 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
1188 // Disable `translucentNavigationBarPreference`.
1189 translucentNavigationBarPreference.setEnabled(false);
1191 // Set the icons according to the theme.
1192 if (MainWebViewActivity.darkTheme) {
1193 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_dark);
1194 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_dark);
1196 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled_light);
1197 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted_light);
1199 } else { // `hide_system_bars` is false.
1200 // Enable `translucentNavigationBarPreference`.
1201 translucentNavigationBarPreference.setEnabled(true);
1203 // Set the icons according to the theme.
1204 if (MainWebViewActivity.darkTheme) {
1205 // Set the `hideSystemBarsPreference` icon.
1206 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_dark);
1208 // Set the `translucentNavigationBarPreference` icon.
1209 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1210 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1212 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1215 // Set the `hideSystemBarsPreference` icon.
1216 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled_light);
1218 // Set the `translucentNavigationBarPreference` icon.
1219 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1220 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1222 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1228 case "translucent_navigation_bar":
1230 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
1231 if (MainWebViewActivity.darkTheme) {
1232 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_dark);
1234 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled_light);
1237 if (MainWebViewActivity.darkTheme) {
1238 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_dark);
1240 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled_light);
1245 case "clear_everything":
1246 // Store the new `clear_everything` status
1247 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
1249 // Update the status of the `Clear and Exit` preferences.
1250 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
1251 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
1252 clearFormDataPreference.setEnabled(!newClearEverythingBoolean);
1253 clearCachePreference.setEnabled(!newClearEverythingBoolean);
1255 // Update the `clearEverythingPreference` icon.
1256 if (newClearEverythingBoolean) {
1257 if (MainWebViewActivity.darkTheme) {
1258 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_dark);
1260 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled_light);
1263 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
1266 // Update the `clearCookiesPreference` icon.
1267 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
1268 if (MainWebViewActivity.darkTheme) {
1269 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1271 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1274 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1277 // Update the `clearDomStoragePreference` icon.
1278 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
1279 if (MainWebViewActivity.darkTheme) {
1280 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1282 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1285 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1288 // Update the `clearFormDataPreference` icon.
1289 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
1290 if (MainWebViewActivity.darkTheme) {
1291 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1293 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1296 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1299 // Update the `clearCachePreference` icon.
1300 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
1301 if (MainWebViewActivity.darkTheme) {
1302 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1304 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1307 clearCachePreference.setIcon(R.drawable.cache_warning);
1311 case "clear_cookies":
1313 if (sharedPreferences.getBoolean("clear_cookies", true)) {
1314 if (MainWebViewActivity.darkTheme) {
1315 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_dark);
1317 clearCookiesPreference.setIcon(R.drawable.cookies_cleared_light);
1320 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
1324 case "clear_dom_storage":
1326 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
1327 if (MainWebViewActivity.darkTheme) {
1328 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_dark);
1330 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared_light);
1333 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
1337 case "clear_form_data":
1339 if (sharedPreferences.getBoolean("clear_form_data", true)) {
1340 if (MainWebViewActivity.darkTheme) {
1341 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_dark);
1343 clearFormDataPreference.setIcon(R.drawable.form_data_cleared_light);
1346 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
1352 if (sharedPreferences.getBoolean("clear_cache", true)) {
1353 if (MainWebViewActivity.darkTheme) {
1354 clearCachePreference.setIcon(R.drawable.cache_cleared_dark);
1356 clearCachePreference.setIcon(R.drawable.cache_cleared_light);
1359 clearCachePreference.setIcon(R.drawable.cache_warning);
1364 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
1365 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
1368 case "default_font_size":
1369 // Update the summary text of `default_font_size`.
1370 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
1373 case "swipe_to_refresh":
1375 if (sharedPreferences.getBoolean("swipe_to_refresh", true)) {
1376 if (MainWebViewActivity.darkTheme) {
1377 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_dark);
1379 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled_light);
1382 if (MainWebViewActivity.darkTheme) {
1383 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_dark);
1385 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled_light);
1390 case "display_additional_app_bar_icons":
1392 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
1393 if (MainWebViewActivity.darkTheme) {
1394 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_dark);
1396 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled_light);
1399 if (MainWebViewActivity.darkTheme) {
1400 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_dark);
1402 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled_light);
1409 if (sharedPreferences.getBoolean("dark_theme", false)) {
1410 darkThemePreference.setIcon(R.drawable.theme_dark);
1412 darkThemePreference.setIcon(R.drawable.theme_light);
1415 // Create an intent to restart Privacy Browser.
1416 Intent changeThemeRestartIntent = getActivity().getParentActivityIntent();
1418 // Assert that the intent is not null to remove the lint error below.
1419 assert changeThemeRestartIntent != null;
1421 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
1422 changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
1425 startActivity(changeThemeRestartIntent);
1429 // Set the URL to be reloaded on restart to apply the new night mode setting.
1430 MainWebViewActivity.loadUrlOnRestart = true;
1432 // Store the current night mode status.
1433 boolean currentNightModeBoolean = sharedPreferences.getBoolean("night_mode", false);
1434 boolean currentJavaScriptBoolean = sharedPreferences.getBoolean("javascript_enabled", false);
1437 if (currentNightModeBoolean) {
1438 if (MainWebViewActivity.darkTheme) {
1439 nightModePreference.setIcon(R.drawable.night_mode_enabled_dark);
1441 nightModePreference.setIcon(R.drawable.night_mode_enabled_light);
1444 if (MainWebViewActivity.darkTheme) {
1445 nightModePreference.setIcon(R.drawable.night_mode_disabled_dark);
1447 nightModePreference.setIcon(R.drawable.night_mode_disabled_light);
1451 // Update the status of `javaScriptPreference` and `domStoragePreference`.
1452 javaScriptPreference.setEnabled(!currentNightModeBoolean);
1453 domStoragePreference.setEnabled(currentNightModeBoolean || currentJavaScriptBoolean);
1455 // Update the `javaScriptPreference` icon.
1456 if (currentNightModeBoolean || currentJavaScriptBoolean) {
1457 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
1459 javaScriptPreference.setIcon(R.drawable.privacy_mode);
1462 // Update the `domStoragePreference` icon.
1463 if (currentNightModeBoolean || currentJavaScriptBoolean) { // The preference is enabled.
1464 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) { // DOM storage is enabled.
1465 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
1466 } else { // DOM storage is disabled.
1467 if (MainWebViewActivity.darkTheme) {
1468 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_dark);
1470 domStoragePreference.setIcon(R.drawable.dom_storage_disabled_light);
1473 } else { // The preference is disabled. The icon should be ghosted.
1474 if (MainWebViewActivity.darkTheme) {
1475 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_dark);
1477 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted_light);
1482 case "display_webpage_images":
1483 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
1485 if (MainWebViewActivity.darkTheme) {
1486 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_dark);
1488 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled_light);
1491 // `mainWebView` does not need to be reloaded because unloaded images will load automatically.
1492 MainWebViewActivity.reloadOnRestart = false;
1495 if (MainWebViewActivity.darkTheme) {
1496 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_dark);
1498 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled_light);
1501 // Set `mainWebView` to reload on restart to remove the current images.
1502 MainWebViewActivity.reloadOnRestart = true;
1508 // Register the listener.
1509 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
1512 // 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
1513 // even while running in the foreground.
1515 public void onPause() {
1517 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
1521 public void onResume() {
1523 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);