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 homepagePreference = findPreference("homepage");
67 final Preference defaultFontSizePreference = findPreference("default_font_size");
68 final Preference swipeToRefreshPreference = findPreference("swipe_to_refresh");
69 final Preference displayAdditionalAppBarIconsPreference = findPreference("display_additional_app_bar_icons");
70 final Preference displayWebpageImagesPreference = findPreference("display_webpage_images");
73 domStoragePreference.setDependency("javascript_enabled");
74 torHomepagePreference.setDependency("proxy_through_orbot");
75 torSearchPreference.setDependency("proxy_through_orbot");
76 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
78 // Get strings from the preferences.
79 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
80 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
82 // Get booleans from the preferences.
83 boolean javaScriptEnabledBoolean = savedPreferences.getBoolean("javascript_enabled", false);
84 boolean firstPartyCookiesEnabledBoolean = savedPreferences.getBoolean("first_party_cookies_enabled", false);
85 boolean thirdPartyCookiesEnabledBoolean = savedPreferences.getBoolean("third_party_cookies_enabled", false);
86 boolean proxyThroughOrbotBoolean = savedPreferences.getBoolean("proxy_through_orbot", false);
87 boolean fullScreenBrowsingModeBoolean = savedPreferences.getBoolean("full_screen_browsing_mode", false);
88 boolean hideSystemBarsBoolean = savedPreferences.getBoolean("hide_system_bars", false);
90 // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
91 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
93 // We need to inflated a `WebView` to get the default user agent.
94 LayoutInflater inflater = getActivity().getLayoutInflater();
95 // `@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.
96 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
97 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
99 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
100 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
101 case "WebView default user agent":
102 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
103 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
106 case "Custom user agent":
107 // We can't use the string from the array because it is referenced in code and can't be translated.
108 userAgentPreference.setSummary(R.string.custom_user_agent);
112 // Display the user agent from the preference as the summary text.
113 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
117 // Set the summary text for "customUserAgentPreference" (the default is `PrivacyBrowser/1.0`) and enable it if `userAgentPreference` it set to `Custom user agent`.
118 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
119 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
122 // 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`.
123 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
126 // 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=`
127 if (torSearchString.equals("Custom URL")) {
128 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
129 torSearchPreference.setSummary(R.string.custom_url);
131 // Set the array value as the summary text.
132 torSearchPreference.setSummary(torSearchString);
135 // Set the summary text for `tor_search_custom_url`. The default is `""`.
136 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
138 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
139 torSearchCustomURLPreference.setEnabled(proxyThroughOrbotBoolean && torSearchString.equals("Custom URL"));
142 // 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=`.
143 if (searchString.equals("Custom URL")) {
144 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
145 searchPreference.setSummary(R.string.custom_url);
147 // Set the array value as the summary text.
148 searchPreference.setSummary(searchString);
151 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
152 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
153 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
156 // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
157 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
160 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
161 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
163 // 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`.
164 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
167 // Set the `javaScriptPreference` icon.
168 if (javaScriptEnabledBoolean) {
169 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
171 javaScriptPreference.setIcon(R.drawable.privacy_mode);
174 // Set the `firstPartyCookiesPreference` icon.
175 if (firstPartyCookiesEnabledBoolean) {
176 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
178 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
181 // Set the `thirdPartyCookiesPreference` icon.
182 if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
183 if (thirdPartyCookiesEnabledBoolean) {
184 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
186 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
189 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
192 // Set the `domStoragePreference` icon.
193 if (javaScriptEnabledBoolean) {
194 if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
195 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
197 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
200 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
203 // Set the `saveFormDataPreference` icon.
204 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
205 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
207 saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
210 // Set the `customUserAgentPreference` icon.
211 if (customUserAgentPreference.isEnabled()) {
212 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
214 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
217 // Set the `blockAdsPreference` icon.
218 if (savedPreferences.getBoolean("block_ads", true)) {
219 blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
221 blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
224 // Set the `incognitoModePreference` icon.
225 if (savedPreferences.getBoolean("incognito_mode", false)) {
226 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
228 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
231 // Set the `doNotTrackPreference` icon.
232 if (savedPreferences.getBoolean("do_not_track", false)) {
233 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
235 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
238 // Set the `proxyThroughOrbotPreference` icon.
239 if (proxyThroughOrbotBoolean) {
240 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled);
242 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled);
245 // Set the `torSearchPreference` and `torSearchCustomURLPreference` icons.
246 if (proxyThroughOrbotBoolean) {
247 // Set the `torHomepagePreference` and `torSearchPreference` icons.
248 torHomepagePreference.setIcon(R.drawable.home_enabled);
249 torSearchPreference.setIcon(R.drawable.search_enabled);
251 // Set the `torSearchCustomURLPreference` icon.
252 if (torSearchCustomURLPreference.isEnabled()) {
253 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
255 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
257 } else { // Proxy through Orbot is disabled.
258 torHomepagePreference.setIcon(R.drawable.home_ghosted);
259 torSearchPreference.setIcon(R.drawable.search_ghosted);
260 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
263 // Set the `searchCustomURLPreference` icon.
264 if (searchCustomURLPreference.isEnabled()) {
265 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
267 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
270 // Set the full screen browsing mode icons.
271 if (fullScreenBrowsingModeBoolean) {
272 // Set the `fullScreenBrowsingModePreference` icon.
273 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled);
275 if (hideSystemBarsBoolean) {
276 // Set `hideSystemBarsPreference` to use the enabled icon.
277 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
279 // Set `translucentNavigationBarPreference` to use the ghosted icon.
280 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
281 } else { // `hideSystemBarsBoolean` is false.
282 // Set `hideSystemBarsPreference` to use the disabled icon.
283 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
285 // Set the correct icon for `translucentNavigationBarPreference`.
286 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
287 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
289 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
292 } else { // `fullScreenBrowsingModeBoolean` is false.
293 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled);
294 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted);
295 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
298 // Set the `swipeToRefreshPreference` icon.
299 if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
300 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
302 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
305 // Set the `displayAdditionalAppBarIconsPreference` icon.
306 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
307 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
309 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
312 // Set the `displayWebpageImagesPreference` icon.
313 if (savedPreferences.getBoolean("display_webpage_images", true)) {
314 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled);
316 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled);
320 // Listen for preference changes.
321 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
323 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
324 @SuppressLint("SetJavaScriptEnabled")
325 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
328 case "javascript_enabled":
330 if (sharedPreferences.getBoolean("javascript_enabled", false)) {
331 // Update the icon for `javascript_enabled`.
332 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
334 // Update the icon for `dom_storage_enabled`.
335 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
336 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
338 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
340 } else { // `javascript_enabled` is `false`.
341 // Update the icon for `javascript_enabled`.
342 javaScriptPreference.setIcon(R.drawable.privacy_mode);
344 // Set the icon for `dom_storage_disabled` to be ghosted.
345 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
349 case "first_party_cookies_enabled":
350 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
351 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
352 // Set the icon for `first_party_cookies_enabled`.
353 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
355 // Update the icon for `third_party_cookies_enabled`.
356 if (Build.VERSION.SDK_INT >= 21) {
357 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
358 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
360 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
363 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
365 } else { // `first_party_cookies_enabled` is `false`.
366 // Update the icon for `first_party_cookies_enabled`.
367 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
369 // Set the icon for `third_party_cookies_enabled` to be ghosted.
370 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
373 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
374 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
377 case "third_party_cookies_enabled":
379 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
380 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
382 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
386 case "dom_storage_enabled":
388 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
389 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
391 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
395 case "save_form_data_enabled":
397 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
398 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
400 saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
404 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
406 switch (userAgentString) {
407 case "WebView default user agent":
408 // Display the user agent as the summary text for `userAgentPreference`.
409 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
411 // Update `customUserAgentPreference`.
412 customUserAgentPreference.setEnabled(false);
413 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
416 case "Custom user agent":
417 // Display `Custom user agent` as the summary text for `userAgentPreference`.
418 userAgentPreference.setSummary(R.string.custom_user_agent);
420 // Update `customUserAgentPreference`.
421 customUserAgentPreference.setEnabled(true);
422 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
426 // Display the user agent as the summary text for `userAgentPreference`.
427 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
429 // Update `customUserAgentPreference`.
430 customUserAgentPreference.setEnabled(false);
431 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
436 case "custom_user_agent":
437 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
438 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
443 if (sharedPreferences.getBoolean("block_ads", true)) {
444 blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
446 blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
450 case "incognito_mode":
452 if (sharedPreferences.getBoolean("incognito_mode", false)) {
453 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
455 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
461 if (sharedPreferences.getBoolean("do_not_track", false)) {
462 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
464 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
469 case "proxy_through_orbot":
470 // Get current settings.
471 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
472 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
474 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
475 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
478 if (currentProxyThroughOrbot) {
479 // Set the `proxyThroughOrbotPreference`, `torHomepagePreference`, and `torSearchPreference` icons.
480 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled);
481 torHomepagePreference.setIcon(R.drawable.home_enabled);
482 torSearchPreference.setIcon(R.drawable.search_enabled);
484 // Set the `torSearchCustomURLPreference` icon.
485 if (torSearchCustomURLPreference.isEnabled()) {
486 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
488 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
490 } else { // Proxy through Orbot is disabled.
491 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled);
492 torHomepagePreference.setIcon(R.drawable.home_ghosted);
493 torSearchPreference.setIcon(R.drawable.search_ghosted);
494 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
499 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
500 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
504 // Get the present search string.
505 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
507 // Update the preferences.
508 if (presentTorSearchString.equals("Custom URL")) {
509 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
510 torSearchPreference.setSummary(R.string.custom_url);
512 // Update `torSearchCustomURLPreference`.
513 torSearchCustomURLPreference.setEnabled(true);
514 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
516 // Set the array value as the summary text.
517 torSearchPreference.setSummary(presentTorSearchString);
519 // Update `torSearchCustomURLPreference`.
520 torSearchCustomURLPreference.setEnabled(false);
521 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
525 case "tor_search_custom_url":
526 // Set the summary text for `tor_search_custom_url`.
527 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
531 // Store the new search string.
532 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
534 // Update `searchPreference` and `searchCustomURLPreference`.
535 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
536 // Set the summary text to `R.string.custom_url`, which is translated.
537 searchPreference.setSummary(R.string.custom_url);
539 // Update `searchCustomURLPreference`.
540 searchCustomURLPreference.setEnabled(true);
541 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
542 } else { // `Custom URL` is not selected.
543 // Set the summary text to `newSearchString`.
544 searchPreference.setSummary(newSearchString);
546 // Update `searchCustomURLPreference`.
547 searchCustomURLPreference.setEnabled(false);
548 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
552 case "search_custom_url":
553 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
554 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
557 case "full_screen_browsing_mode":
558 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
559 // Set `fullScreenBrowsingModePreference` to use the enabled icon.
560 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled);
562 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
563 // Set `hideSystemBarsPreference` to use the enabled icon.
564 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
566 // Update `translucentNavigationBarPreference`.
567 translucentNavigationBarPreference.setEnabled(false);
568 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
569 } else { // `hide_system_bars` is false.
570 // Set `hideSystemBarsPreference` to use the disabled icon.
571 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
573 // Update `translucentNavigationBarPreference`.
574 translucentNavigationBarPreference.setEnabled(true);
575 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
576 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
578 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
581 } else { // `full_screen_browsing_mode` is false.
582 // Disable `translucentNavigationBarPreference`.
583 translucentNavigationBarPreference.setEnabled(false);
586 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled);
587 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted);
588 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
592 case "hide_system_bars":
593 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
594 // Set `hideSystemBarsPreference` to use the enabled icon.
595 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
597 // Update `translucentNavigationBarPreference`.
598 translucentNavigationBarPreference.setEnabled(false);
599 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
600 } else { // `hide_system_bars` is false.
601 // Set `hideSystemBarsPreference` to use the disabled icon.
602 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
604 // Update `translucentNavigationBarPreference`.
605 translucentNavigationBarPreference.setEnabled(true);
606 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
607 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
609 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
614 case "translucent_navigation_bar":
616 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
617 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
619 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
624 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
625 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
628 case "default_font_size":
629 // Update the summary text of `default_font_size`.
630 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
633 case "swipe_to_refresh":
635 if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
636 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
638 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
642 case "display_additional_app_bar_icons":
644 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
645 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
647 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
651 case "display_webpage_images":
653 if (sharedPreferences.getBoolean("display_webpage_images", true)) {
654 displayWebpageImagesPreference.setIcon(R.drawable.images_enabled);
656 displayWebpageImagesPreference.setIcon(R.drawable.images_disabled);
661 // If no match, do nothing.
667 // Register the listener.
668 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
671 // 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
672 // even while running in the foreground.
674 public void onPause() {
676 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
680 public void onResume() {
682 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);