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");
72 domStoragePreference.setDependency("javascript_enabled");
73 torHomepagePreference.setDependency("proxy_through_orbot");
74 torSearchPreference.setDependency("proxy_through_orbot");
75 hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
77 // Get strings from the preferences.
78 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
79 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
81 // Get booleans from the preferences.
82 boolean javaScriptEnabledBoolean = savedPreferences.getBoolean("javascript_enabled", false);
83 boolean firstPartyCookiesEnabledBoolean = savedPreferences.getBoolean("first_party_cookies_enabled", false);
84 boolean thirdPartyCookiesEnabledBoolean = savedPreferences.getBoolean("third_party_cookies_enabled", false);
85 boolean proxyThroughOrbotBoolean = savedPreferences.getBoolean("proxy_through_orbot", false);
86 boolean fullScreenBrowsingModeBoolean = savedPreferences.getBoolean("full_screen_browsing_mode", false);
87 boolean hideSystemBarsBoolean = savedPreferences.getBoolean("hide_system_bars", false);
89 // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
90 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
92 // We need to inflated a `WebView` to get the default user agent.
93 LayoutInflater inflater = getActivity().getLayoutInflater();
94 // `@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.
95 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
96 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
98 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
99 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
100 case "WebView default user agent":
101 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
102 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
105 case "Custom user agent":
106 // We can't use the string from the array because it is referenced in code and can't be translated.
107 userAgentPreference.setSummary(R.string.custom_user_agent);
111 // Display the user agent from the preference as the summary text.
112 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
116 // Set the summary text for "customUserAgentPreference" (the default is `PrivacyBrowser/1.0`) and enable it if `userAgentPreference` it set to `Custom user agent`.
117 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
118 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
121 // 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`.
122 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
125 // 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=`
126 if (torSearchString.equals("Custom URL")) {
127 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
128 torSearchPreference.setSummary(R.string.custom_url);
130 // Set the array value as the summary text.
131 torSearchPreference.setSummary(torSearchString);
134 // Set the summary text for `tor_search_custom_url`. The default is `""`.
135 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
137 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
138 torSearchCustomURLPreference.setEnabled(proxyThroughOrbotBoolean && torSearchString.equals("Custom URL"));
141 // 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=`.
142 if (searchString.equals("Custom URL")) {
143 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
144 searchPreference.setSummary(R.string.custom_url);
146 // Set the array value as the summary text.
147 searchPreference.setSummary(searchString);
150 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
151 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
152 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
155 // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
156 translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
159 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
160 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
162 // 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`.
163 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
166 // Set the `javaScriptPreference` icon.
167 if (javaScriptEnabledBoolean) {
168 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
170 javaScriptPreference.setIcon(R.drawable.privacy_mode);
173 // Set the `firstPartyCookiesPreference` icon.
174 if (firstPartyCookiesEnabledBoolean) {
175 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
177 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
180 // Set the `thirdPartyCookiesPreference` icon.
181 if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
182 if (thirdPartyCookiesEnabledBoolean) {
183 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
185 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
188 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
191 // Set the `domStoragePreference` icon.
192 if (javaScriptEnabledBoolean) {
193 if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
194 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
196 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
199 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
202 // Set the `saveFormDataPreference` icon.
203 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
204 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
206 saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
209 // Set the `customUserAgentPreference` icon.
210 if (customUserAgentPreference.isEnabled()) {
211 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
213 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
216 // Set the `blockAdsPreference` icon.
217 if (savedPreferences.getBoolean("block_ads", true)) {
218 blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
220 blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
223 // Set the `incognitoModePreference` icon.
224 if (savedPreferences.getBoolean("incognito_mode", false)) {
225 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
227 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
230 // Set the `doNotTrackPreference` icon.
231 if (savedPreferences.getBoolean("do_not_track", false)) {
232 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
234 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
237 // Set the `proxyThroughOrbotPreference` icon.
238 if (proxyThroughOrbotBoolean) {
239 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled);
241 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled);
244 // Set the `torSearchPreference` and `torSearchCustomURLPreference` icons.
245 if (proxyThroughOrbotBoolean) {
246 // Set the `torHomepagePreference` and `torSearchPreference` icons.
247 torHomepagePreference.setIcon(R.drawable.home_enabled);
248 torSearchPreference.setIcon(R.drawable.search_enabled);
250 // Set the `torSearchCustomURLPreference` icon.
251 if (torSearchCustomURLPreference.isEnabled()) {
252 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
254 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
256 } else { // Proxy through Orbot is disabled.
257 torHomepagePreference.setIcon(R.drawable.home_ghosted);
258 torSearchPreference.setIcon(R.drawable.search_ghosted);
259 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
262 // Set the `searchCustomURLPreference` icon.
263 if (searchCustomURLPreference.isEnabled()) {
264 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
266 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
269 // Set the full screen browsing mode icons.
270 if (fullScreenBrowsingModeBoolean) {
271 // Set the `fullScreenBrowsingModePreference` icon.
272 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled);
274 if (hideSystemBarsBoolean) {
275 // Set `hideSystemBarsPreference` to use the enabled icon.
276 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
278 // Set `translucentNavigationBarPreference` to use the ghosted icon.
279 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
280 } else { // `hideSystemBarsBoolean` is false.
281 // Set `hideSystemBarsPreference` to use the disabled icon.
282 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
284 // Set the correct icon for `translucentNavigationBarPreference`.
285 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
286 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
288 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
291 } else { // `fullScreenBrwosingModeBoolean` is false.
292 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled);
293 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted);
294 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
297 // Set the `swipeToRefreshPreference` icon.
298 if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
299 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
301 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
304 // Set the `displayAdditionalAppBarIconsPreference` icon.
305 if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
306 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
308 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
311 // Listen for preference changes.
312 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
314 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
315 @SuppressLint("SetJavaScriptEnabled")
316 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
319 case "javascript_enabled":
321 if (sharedPreferences.getBoolean("javascript_enabled", false)) {
322 // Update the icon for `javascript_enabled`.
323 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
325 // Update the icon for `dom_storage_enabled`.
326 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
327 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
329 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
331 } else { // `javascript_enabled` is `false`.
332 // Update the icon for `javascript_enabled`.
333 javaScriptPreference.setIcon(R.drawable.privacy_mode);
335 // Set the icon for `dom_storage_disabled` to be ghosted.
336 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
340 case "first_party_cookies_enabled":
341 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
342 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
343 // Set the icon for `first_party_cookies_enabled`.
344 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
346 // Update the icon for `third_party_cookies_enabled`.
347 if (Build.VERSION.SDK_INT >= 21) {
348 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
349 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
351 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
354 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
356 } else { // `first_party_cookies_enabled` is `false`.
357 // Update the icon for `first_party_cookies_enabled`.
358 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
360 // Set the icon for `third_party_cookies_enabled` to be ghosted.
361 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
364 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
365 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
368 case "third_party_cookies_enabled":
370 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
371 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
373 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
377 case "dom_storage_enabled":
379 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
380 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
382 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
386 case "save_form_data_enabled":
388 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
389 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
391 saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
395 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
397 switch (userAgentString) {
398 case "WebView default user agent":
399 // Display the user agent as the summary text for `userAgentPreference`.
400 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
402 // Update `customUserAgentPreference`.
403 customUserAgentPreference.setEnabled(false);
404 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
407 case "Custom user agent":
408 // Display `Custom user agent` as the summary text for `userAgentPreference`.
409 userAgentPreference.setSummary(R.string.custom_user_agent);
411 // Update `customUserAgentPreference`.
412 customUserAgentPreference.setEnabled(true);
413 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
417 // Display the user agent as the summary text for `userAgentPreference`.
418 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
420 // Update `customUserAgentPreference`.
421 customUserAgentPreference.setEnabled(false);
422 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
427 case "custom_user_agent":
428 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
429 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
434 if (sharedPreferences.getBoolean("block_ads", true)) {
435 blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
437 blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
441 case "incognito_mode":
443 if (sharedPreferences.getBoolean("incognito_mode", false)) {
444 incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
446 incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
452 if (sharedPreferences.getBoolean("do_not_track", false)) {
453 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
455 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
460 case "proxy_through_orbot":
461 // Get current settings.
462 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
463 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
465 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
466 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
469 if (currentProxyThroughOrbot) {
470 // Set the `proxyThroughOrbotPreference`, `torHomepagePreference`, and `torSearchPreference` icons.
471 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled);
472 torHomepagePreference.setIcon(R.drawable.home_enabled);
473 torSearchPreference.setIcon(R.drawable.search_enabled);
475 // Set the `torSearchCustomURLPreference` icon.
476 if (torSearchCustomURLPreference.isEnabled()) {
477 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
479 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
481 } else { // Proxy through Orbot is disabled.
482 proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled);
483 torHomepagePreference.setIcon(R.drawable.home_ghosted);
484 torSearchPreference.setIcon(R.drawable.search_ghosted);
485 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
490 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
491 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
495 // Get the present search string.
496 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
498 // Update the preferences.
499 if (presentTorSearchString.equals("Custom URL")) {
500 // Use `R.string.custom_url`, which is translated, as the summary instead of the array value, which isn't.
501 torSearchPreference.setSummary(R.string.custom_url);
503 // Update `torSearchCustomURLPreference`.
504 torSearchCustomURLPreference.setEnabled(true);
505 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
507 // Set the array value as the summary text.
508 torSearchPreference.setSummary(presentTorSearchString);
510 // Update `torSearchCustomURLPreference`.
511 torSearchCustomURLPreference.setEnabled(false);
512 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
516 case "tor_search_custom_url":
517 // Set the summary text for `tor_search_custom_url`.
518 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
522 // Store the new search string.
523 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
525 // Update `searchPreference` and `searchCustomURLPreference`.
526 if (newSearchString.equals("Custom URL")) { // `Custom URL` is selected.
527 // Set the summary text to `R.string.custom_url`, which is translated.
528 searchPreference.setSummary(R.string.custom_url);
530 // Update `searchCustomURLPreference`.
531 searchCustomURLPreference.setEnabled(true);
532 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
533 } else { // `Custom URL` is not selected.
534 // Set the summary text to `newSearchString`.
535 searchPreference.setSummary(newSearchString);
537 // Update `searchCustomURLPreference`.
538 searchCustomURLPreference.setEnabled(false);
539 searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
543 case "search_custom_url":
544 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
545 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
548 case "full_screen_browsing_mode":
549 if (sharedPreferences.getBoolean("full_screen_browsing_mode", false)) {
550 // Set `fullScreenBrowsingModePreference` to use the enabled icon.
551 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled);
553 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
554 // Set `hideSystemBarsPreference` to use the enabled icon.
555 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
557 // Update `translucentNavigationBarPreference`.
558 translucentNavigationBarPreference.setEnabled(false);
559 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
560 } else { // `hide_system_bars` is false.
561 // Set `hideSystemBarsPreference` to use the disabled icon.
562 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
564 // Update `translucentNavigationBarPreference`.
565 translucentNavigationBarPreference.setEnabled(true);
566 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
567 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
569 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
572 } else { // `full_screen_browsing_mode` is false.
573 // Disable `translucentNavigationBarPreference`.
574 translucentNavigationBarPreference.setEnabled(false);
577 fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_disabled);
578 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_ghosted);
579 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
583 case "hide_system_bars":
584 if (sharedPreferences.getBoolean("hide_system_bars", false)) {
585 // Set `hideSystemBarsPreference` to use the enabled icon.
586 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
588 // Update `translucentNavigationBarPreference`.
589 translucentNavigationBarPreference.setEnabled(false);
590 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_ghosted);
591 } else { // `hide_system_bars` is false.
592 // Set `hideSystemBarsPreference` to use the disabled icon.
593 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_disabled);
595 // Update `translucentNavigationBarPreference`.
596 translucentNavigationBarPreference.setEnabled(true);
597 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
598 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
600 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
605 case "translucent_navigation_bar":
607 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
608 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
610 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
615 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
616 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
619 case "default_font_size":
620 // Update the summary text of `default_font_size`.
621 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
624 case "swipe_to_refresh":
626 if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
627 swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
629 swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
633 case "display_additional_app_bar_icons":
635 if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
636 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
638 displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
643 // If no match, do nothing.
649 // Register the listener.
650 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
653 // 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
654 // even while running in the foreground.
656 public void onPause() {
658 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
662 public void onResume() {
664 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);