2 * Copyright © 2016-2017 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.fragments;
22 import android.annotation.SuppressLint;
23 import android.content.SharedPreferences;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.preference.Preference;
27 import android.preference.PreferenceFragment;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.webkit.WebView;
32 import com.stoutner.privacybrowser.R;
34 public class SettingsFragment extends PreferenceFragment {
35 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
36 private SharedPreferences savedPreferences;
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 addPreferencesFromResource(R.xml.preferences);
43 // Initialize savedPreferences.
44 savedPreferences = getPreferenceScreen().getSharedPreferences();
46 // Get handles for the preferences we need to modify.
47 final Preference javaScriptPreference = findPreference("javascript_enabled");
48 final Preference firstPartyCookiesPreference = findPreference("first_party_cookies_enabled");
49 final Preference thirdPartyCookiesPreference = findPreference("third_party_cookies_enabled");
50 final Preference domStoragePreference = findPreference("dom_storage_enabled");
51 final Preference saveFormDataPreference = findPreference("save_form_data_enabled");
52 final Preference userAgentPreference = findPreference("user_agent");
53 final Preference customUserAgentPreference = findPreference("custom_user_agent");
54 final Preference blockAdsPreference = findPreference("block_ads");
55 final Preference incognitoModePreference = findPreference("incognito_mode");
56 final Preference doNotTrackPreference = findPreference("do_not_track");
57 final Preference proxyThroughOrbotPreference = findPreference("proxy_through_orbot");
58 final Preference torHomepagePreference = findPreference("tor_homepage");
59 final Preference torSearchPreference = findPreference("tor_search");
60 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
61 final Preference searchPreference = findPreference("search");
62 final Preference searchCustomURLPreference = findPreference("search_custom_url");
63 final Preference fullScreenBrowsingModePreference = findPreference("full_screen_browsing_mode");
64 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
65 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
66 final Preference clearEverythingPreference = findPreference("clear_everything");
67 final Preference clearCookiesPreference = findPreference("clear_cookies");
68 final Preference clearDomStoragePreference = findPreference("clear_dom_storage");
69 final Preference clearFormDataPreference = findPreference("clear_form_data");
70 final Preference clearCachePreference = findPreference("clear_cache");
71 final Preference homepagePreference = findPreference("homepage");
72 final Preference defaultFontSizePreference = findPreference("default_font_size");
73 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
74 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
75 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
78 domStoragePreference.setDependency("javascript_enabled");
79 torHomepagePreference.setDependency("proxy_through_orbot");
80 torSearchPreference.setDependency("proxy_through_orbot");
81 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
83 // Get strings from the preferences.
84 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
85 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
87 // Get booleans from the preferences.
88 boolean javaScriptEnabledBoolean = savedPreferences.getBoolean("javascript_enabled", false);
89 boolean firstPartyCookiesEnabledBoolean = savedPreferences.getBoolean("first_party_cookies_enabled", false);
90 boolean thirdPartyCookiesEnabledBoolean = savedPreferences.getBoolean("third_party_cookies_enabled", false);
91 boolean proxyThroughOrbotBoolean = savedPreferences.getBoolean("proxy_through_orbot", false);
92 boolean fullScreenBrowsingModeBoolean = savedPreferences.getBoolean("full_screen_browsing_mode", false);
93 boolean hideSystemBarsBoolean = savedPreferences.getBoolean("hide_system_bars", false);
94 boolean clearEverythingBoolean = savedPreferences.getBoolean("clear_everything", true);
96 // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
97 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
99 // We need to inflated a `WebView` to get the default user agent.
100 LayoutInflater inflater = getActivity().getLayoutInflater();
101 // `@SuppressLint("InflateParams")` removes the warning about using `null` as the `ViewGroup`, which in this case makes sense because we don't want to display `bare_webview` on the screen. `false` does not attach the view to the root.
102 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
103 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
105 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
106 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
107 case "WebView default user agent":
108 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
109 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
112 case "Custom user agent":
113 // We can't use the string from the array because it is referenced in code and can't be translated.
114 userAgentPreference.setSummary(R.string.custom_user_agent);
118 // Display the user agent from the preference as the summary text.
119 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
123 // Set the summary text for "customUserAgentPreference" (the default is `PrivacyBrowser/1.0`) and enable it if `userAgentPreference` it set to `Custom user agent`.
124 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
125 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
128 // 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`.
129 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
132 // 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=`
133 if (torSearchString.equals("Custom URL")) {
134 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
135 torSearchPreference.setSummary(R.string.custom_url);
137 // Set the array value as the summary text.
138 torSearchPreference.setSummary(torSearchString);
141 // Set the summary text for `tor_search_custom_url`. The default is `""`.
142 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
144 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
145 torSearchCustomURLPreference.setEnabled(proxyThroughOrbotBoolean && torSearchString.equals("Custom URL"));
148 // 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=`.
149 if (searchString.equals("Custom URL")) {
150 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
151 searchPreference.setSummary(R.string.custom_url);
153 // Set the array value as the summary text.
154 searchPreference.setSummary(searchString);
157 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
158 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
159 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
162 // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
163 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
165 // Set the status of the `Clear and Exit` preferences.
166 clearCookiesPreference.setEnabled(!clearEverythingBoolean);
167 clearDomStoragePreference.setEnabled(!clearEverythingBoolean);
168 clearFormDataPreference.setEnabled(!clearEverythingBoolean);
169 clearCachePreference.setEnabled(!clearEverythingBoolean);
171 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
172 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
174 // Set the default font size as the summary text for the `Default Font Size` preference when the preference screen is loaded. The default is `100`.
175 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
178 // Set the `javaScriptPreference` icon.
179 if (javaScriptEnabledBoolean) {
180 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
182 javaScriptPreference.setIcon(R.drawable.privacy_mode);
185 // Set the `firstPartyCookiesPreference` icon.
186 if (firstPartyCookiesEnabledBoolean) {
187 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
189 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
192 // Set the `thirdPartyCookiesPreference` icon.
193 if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
194 if (thirdPartyCookiesEnabledBoolean) {
195 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
197 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
200 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
203 // Set the `domStoragePreference` icon.
204 if (javaScriptEnabledBoolean) {
205 if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
206 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
208 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
211 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
214 // Set the `saveFormDataPreference` icon.
215 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
216 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
218 saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
221 // Set the `customUserAgentPreference` icon.
222 if (customUserAgentPreference.isEnabled()) {
223 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
225 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
228 // Set the `blockAdsPreference` icon.
229 if (savedPreferences.getBoolean("block_ads", true)) {
230 blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
232 blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
235 // Set the `incognitoModePreference` icon.
236 if (savedPreferences.getBoolean("incognito_mode", false)) {
237 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
239 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
242 // Set the `doNotTrackPreference` icon.
243 if (savedPreferences.getBoolean("do_not_track", false)) {
244 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
246 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
249 // Set the `proxyThroughOrbotPreference` icon.
250 if (proxyThroughOrbotBoolean) {
251 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled);
253 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled);
256 // Set the `torSearchPreference` and `torSearchCustomURLPreference` icons.
257 if (proxyThroughOrbotBoolean) {
258 // Set the `torHomepagePreference` and `torSearchPreference` icons.
259 torHomepagePreference.setIcon(R.drawable.home_enabled);
260 torSearchPreference.setIcon(R.drawable.search_enabled);
262 // Set the `torSearchCustomURLPreference` icon.
263 if (torSearchCustomURLPreference.isEnabled()) {
264 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
266 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
268 } else { // Proxy through Orbot is disabled.
269 torHomepagePreference.setIcon(R.drawable.home_ghosted);
270 torSearchPreference.setIcon(R.drawable.search_ghosted);
271 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
274 // Set the `searchCustomURLPreference` icon.
275 if (searchCustomURLPreference.isEnabled()) {
276 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
278 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
281 // Set the full screen browsing mode icons.
282 if (fullScreenBrowsingModeBoolean) {
283 // Set the `fullScreenBrowsingModePreference` icon.
284 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled);
286 if (hideSystemBarsBoolean) {
287 // Set `hideSystemBarsPreference` to use the enabled icon.
288 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
290 // Set `translucentNavigationBarPreference` to use the ghosted icon.
291 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
292 } else { // `hideSystemBarsBoolean` is false.
293 // Set `hideSystemBarsPreference` to use the disabled icon.
294 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
296 // Set the correct icon for `translucentNavigationBarPreference`.
297 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
298 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
300 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
303 } else { // `fullScreenBrowsingModeBoolean` is false.
304 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled);
305 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted);
306 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
309 // Set the `clearEverythingPreference` icon.
310 if (clearEverythingBoolean) {
311 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled);
313 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
316 // Set the `clearCookiesPreference` icon.
317 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cookies", true)) {
318 clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
320 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
323 // Set the `clearDomStoragePreference` icon.
324 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_dom_storage", true)) {
325 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
327 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
330 // Set the `clearFormDataPreference` icon.
331 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_form_data", true)) {
332 clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
334 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
337 // Set the `clearCachePreference` icon.
338 if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cache", true)) {
339 clearCachePreference.setIcon(R.drawable.cache_cleared);
341 clearCachePreference.setIcon(R.drawable.cache_warning);
344 // Set the `swipeToRefreshPreference` icon.
345 if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
346 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
348 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
351 // Set the `displayAdditionalAppBarIconsPreference` icon.
352 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
353 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
355 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
358 // Set the `displayWebpageImagesPreference` icon.
359 if (savedPreferences.getBoolean("display_webpage_images", true)) {
360 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled);
362 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled);
366 // Listen for preference changes.
367 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
369 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
370 @SuppressLint("SetJavaScriptEnabled")
371 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
374 case "javascript_enabled":
376 if (sharedPreferences.getBoolean("javascript_enabled", false)) {
377 // Update the icon for `javascript_enabled`.
378 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
380 // Update the icon for `dom_storage_enabled`.
381 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
382 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
384 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
386 } else { // `javascript_enabled` is `false`.
387 // Update the icon for `javascript_enabled`.
388 javaScriptPreference.setIcon(R.drawable.privacy_mode);
390 // Set the icon for `dom_storage_disabled` to be ghosted.
391 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
395 case "first_party_cookies_enabled":
396 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
397 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
398 // Set the icon for `first_party_cookies_enabled`.
399 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
401 // Update the icon for `third_party_cookies_enabled`.
402 if (Build.VERSION.SDK_INT >= 21) {
403 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
404 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
406 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
409 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
411 } else { // `first_party_cookies_enabled` is `false`.
412 // Update the icon for `first_party_cookies_enabled`.
413 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
415 // Set the icon for `third_party_cookies_enabled` to be ghosted.
416 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
419 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
420 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
423 case "third_party_cookies_enabled":
425 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
426 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
428 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
432 case "dom_storage_enabled":
434 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
435 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
437 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
441 case "save_form_data_enabled":
443 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
444 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
446 saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
450 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
452 switch (userAgentString) {
453 case "WebView default user agent":
454 // Display the user agent as the summary text for `userAgentPreference`.
455 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
457 // Update `customUserAgentPreference`.
458 customUserAgentPreference.setEnabled(false);
459 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
462 case "Custom user agent":
463 // Display `Custom user agent` as the summary text for `userAgentPreference`.
464 userAgentPreference.setSummary(R.string.custom_user_agent);
466 // Update `customUserAgentPreference`.
467 customUserAgentPreference.setEnabled(true);
468 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
472 // Display the user agent as the summary text for `userAgentPreference`.
473 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
475 // Update `customUserAgentPreference`.
476 customUserAgentPreference.setEnabled(false);
477 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
482 case "custom_user_agent":
483 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
484 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
489 if (sharedPreferences.getBoolean("block_ads", true)) {
490 blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
492 blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
496 case "incognito_mode":
498 if (sharedPreferences.getBoolean("incognito_mode", false)) {
499 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
501 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
507 if (sharedPreferences.getBoolean("do_not_track", false)) {
508 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
510 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
515 case "proxy_through_orbot":
516 // Get current settings.
517 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
518 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
520 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
521 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
524 if (currentProxyThroughOrbot) {
525 // Set the `proxyThroughOrbotPreference`, `torHomepagePreference`, and `torSearchPreference` icons.
526 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled);
527 torHomepagePreference.setIcon(R.drawable.home_enabled);
528 torSearchPreference.setIcon(R.drawable.search_enabled);
530 // Set the `torSearchCustomURLPreference` icon.
531 if (torSearchCustomURLPreference.isEnabled()) {
532 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
534 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
536 } else { // Proxy through Orbot is disabled.
537 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled);
538 torHomepagePreference.setIcon(R.drawable.home_ghosted);
539 torSearchPreference.setIcon(R.drawable.search_ghosted);
540 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
545 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
546 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
550 // Get the present search string.
551 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
553 // Update the preferences.
554 if (presentTorSearchString.equals("Custom URL")) {
555 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
556 torSearchPreference.setSummary(R.string.custom_url);
558 // Update `torSearchCustomURLPreference`.
559 torSearchCustomURLPreference.setEnabled(true);
560 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
562 // Set the array value as the summary text.
563 torSearchPreference.setSummary(presentTorSearchString);
565 // Update `torSearchCustomURLPreference`.
566 torSearchCustomURLPreference.setEnabled(false);
567 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
571 case "tor_search_custom_url":
572 // Set the summary text for `tor_search_custom_url`.
573 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
577 // Store the new search string.
578 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
580 // Update `searchPreference` and `searchCustomURLPreference`.
581 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
582 // Set the summary text to `R.string.custom_url`, which is translated.
583 searchPreference.setSummary(R.string.custom_url);
585 // Update `searchCustomURLPreference`.
586 searchCustomURLPreference.setEnabled(true);
587 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
588 } else { // `Custom URL` is not selected.
589 // Set the summary text to `newSearchString`.
590 searchPreference.setSummary(newSearchString);
592 // Update `searchCustomURLPreference`.
593 searchCustomURLPreference.setEnabled(false);
594 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
598 case "search_custom_url":
599 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
600 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
603 case "full_screen_browsing_mode":
604 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
605 // Set `fullScreenBrowsingModePreference` to use the enabled icon.
606 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled);
608 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
609 // Set `hideSystemBarsPreference` to use the enabled icon.
610 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
612 // Update `translucentNavigationBarPreference`.
613 translucentNavigationBarPreference.setEnabled(false);
614 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
615 } else { // `hide_system_bars` is false.
616 // Set `hideSystemBarsPreference` to use the disabled icon.
617 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
619 // Update `translucentNavigationBarPreference`.
620 translucentNavigationBarPreference.setEnabled(true);
621 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
622 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
624 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
627 } else { // `full_screen_browsing_mode` is false.
628 // Disable `translucentNavigationBarPreference`.
629 translucentNavigationBarPreference.setEnabled(false);
632 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled);
633 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted);
634 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
638 case "hide_system_bars":
639 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
640 // Set `hideSystemBarsPreference` to use the enabled icon.
641 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
643 // Update `translucentNavigationBarPreference`.
644 translucentNavigationBarPreference.setEnabled(false);
645 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
646 } else { // `hide_system_bars` is false.
647 // Set `hideSystemBarsPreference` to use the disabled icon.
648 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
650 // Update `translucentNavigationBarPreference`.
651 translucentNavigationBarPreference.setEnabled(true);
652 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
653 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
655 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
660 case "translucent_navigation_bar":
662 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
663 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
665 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
669 case "clear_everything":
670 // Store the new `clear_everything` status
671 boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
673 // Update the status of the `Clear and Exit` preferences.
674 clearCookiesPreference.setEnabled(!newClearEverythingBoolean);
675 clearDomStoragePreference.setEnabled(!newClearEverythingBoolean);
676 clearFormDataPreference.setEnabled(!newClearEverythingBoolean);
677 clearCachePreference.setEnabled(!newClearEverythingBoolean);
679 // Update the `clearEverythingPreference` icon.
680 if (newClearEverythingBoolean) {
681 clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled);
683 clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
686 // Update the `clearCookiesPreference` icon.
687 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
688 clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
690 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
693 // Update the `clearDomStoragePreference` icon.
694 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
695 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
697 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
700 // Update the `clearFormDataPreference` icon.
701 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
702 clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
704 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
707 // Update the `clearCachePreference` icon.
708 if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
709 clearCachePreference.setIcon(R.drawable.cache_cleared);
711 clearCachePreference.setIcon(R.drawable.cache_warning);
715 case "clear_cookies":
717 if (sharedPreferences.getBoolean("clear_cookies", true)) {
718 clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
720 clearCookiesPreference.setIcon(R.drawable.cookies_warning);
724 case "clear_dom_storage":
726 if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
727 clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
729 clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
733 case "clear_form_data":
735 if (sharedPreferences.getBoolean("clear_form_data", true)) {
736 clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
738 clearFormDataPreference.setIcon(R.drawable.form_data_warning);
744 if (sharedPreferences.getBoolean("clear_cache", true)) {
745 clearCachePreference.setIcon(R.drawable.cache_cleared);
747 clearCachePreference.setIcon(R.drawable.cache_warning);
752 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
753 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
756 case "default_font_size":
757 // Update the summary text of `default_font_size`.
758 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
761 case "swipe_to_refresh":
763 if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
764 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
766 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
770 case "display_additional_app_bar_icons":
772 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
773 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
775 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
779 case "display_webpage_images":
781 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
782 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled);
784 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled);
789 // If no match, do nothing.
795 // Register the listener.
796 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
799 // 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
800 // even while running in the foreground.
802 public void onPause() {
804 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
808 public void onResume() {
810 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);