]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java
6d317f06761e517e5cf5f413b518e95e5f157709
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / SettingsFragment.java
1 /*
2  * Copyright © 2016-2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.fragments;
21
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;
31
32 import com.stoutner.privacybrowser.R;
33
34 public class SettingsFragment extends PreferenceFragment {
35     private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
36     private SharedPreferences savedPreferences;
37
38     @Override
39     public void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         addPreferencesFromResource(R.xml.preferences);
42
43         // Initialize savedPreferences.
44         savedPreferences = getPreferenceScreen().getSharedPreferences();
45
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");
76
77         // Set dependencies.
78         domStoragePreference.setDependency("javascript_enabled");
79         torHomepagePreference.setDependency("proxy_through_orbot");
80         torSearchPreference.setDependency("proxy_through_orbot");
81         hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
82
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=");
86
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);
95
96         // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
97         thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
98
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);
104
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());
110                 break;
111
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);
115                 break;
116
117             default:
118                 // Display the user agent from the preference as the summary text.
119                 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
120                 break;
121         }
122
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"));
126
127
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"));
130
131
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);
136         } else {
137             // Set the array value as the summary text.
138             torSearchPreference.setSummary(torSearchString);
139         }
140
141         // Set the summary text for `tor_search_custom_url`.  The default is `""`.
142         torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
143
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"));
146
147
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);
152         } else {
153             // Set the array value as the summary text.
154             searchPreference.setSummary(searchString);
155         }
156
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"));
160
161
162         // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
163         translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
164
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);
170
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"));
173
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") + "%%");
176
177
178         // Set the `javaScriptPreference` icon.
179         if (javaScriptEnabledBoolean) {
180             javaScriptPreference.setIcon(R.drawable.javascript_enabled);
181         } else {
182             javaScriptPreference.setIcon(R.drawable.privacy_mode);
183         }
184
185         // Set the `firstPartyCookiesPreference` icon.
186         if (firstPartyCookiesEnabledBoolean) {
187             firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
188         } else {
189             firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
190         }
191
192         // Set the `thirdPartyCookiesPreference` icon.
193         if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
194             if (thirdPartyCookiesEnabledBoolean) {
195                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
196             } else {
197                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
198             }
199         } else {
200             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
201         }
202
203         // Set the `domStoragePreference` icon.
204         if (javaScriptEnabledBoolean) {
205             if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
206                 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
207             } else {
208                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
209             }
210         } else {
211             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
212         }
213
214         // Set the `saveFormDataPreference` icon.
215         if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
216             saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
217         } else {
218             saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
219         }
220
221         // Set the `customUserAgentPreference` icon.
222         if (customUserAgentPreference.isEnabled()) {
223             customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
224         } else {
225             customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
226         }
227
228         // Set the `blockAdsPreference` icon.
229         if (savedPreferences.getBoolean("block_ads", true)) {
230             blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
231         } else {
232             blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
233         }
234
235         // Set the `incognitoModePreference` icon.
236         if (savedPreferences.getBoolean("incognito_mode", false)) {
237             incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
238         } else {
239             incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
240         }
241
242         // Set the `doNotTrackPreference` icon.
243         if (savedPreferences.getBoolean("do_not_track", false)) {
244             doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
245         } else {
246             doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
247         }
248
249         // Set the `proxyThroughOrbotPreference` icon.
250         if (proxyThroughOrbotBoolean) {
251             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled);
252         } else {
253             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled);
254         }
255
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);
261
262             // Set the `torSearchCustomURLPreference` icon.
263             if (torSearchCustomURLPreference.isEnabled()) {
264                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
265             } else {
266                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
267             }
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);
272         }
273
274         // Set the `searchCustomURLPreference` icon.
275         if (searchCustomURLPreference.isEnabled()) {
276             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
277         } else {
278             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
279         }
280
281         // Set the full screen browsing mode icons.
282         if (fullScreenBrowsingModeBoolean) {
283             // Set the `fullScreenBrowsingModePreference` icon.
284             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled);
285
286             if (hideSystemBarsBoolean) {
287                 // Set `hideSystemBarsPreference` to use the enabled icon.
288                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
289
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);
295
296                 // Set the correct icon for `translucentNavigationBarPreference`.
297                 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
298                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
299                 } else {
300                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
301                 }
302             }
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);
307         }
308
309         // Set the `clearEverythingPreference` icon.
310         if (clearEverythingBoolean) {
311             clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled);
312         } else {
313             clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
314         }
315
316         // Set the `clearCookiesPreference` icon.
317         if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cookies", true)) {
318             clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
319         } else {
320             clearCookiesPreference.setIcon(R.drawable.cookies_warning);
321         }
322
323         // Set the `clearDomStoragePreference` icon.
324         if (clearEverythingBoolean || savedPreferences.getBoolean("clear_dom_storage", true)) {
325             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
326         } else {
327             clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
328         }
329
330         // Set the `clearFormDataPreference` icon.
331         if (clearEverythingBoolean || savedPreferences.getBoolean("clear_form_data", true)) {
332             clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
333         } else {
334             clearFormDataPreference.setIcon(R.drawable.form_data_warning);
335         }
336
337         // Set the `clearCachePreference` icon.
338         if (clearEverythingBoolean || savedPreferences.getBoolean("clear_cache", true)) {
339             clearCachePreference.setIcon(R.drawable.cache_cleared);
340         } else {
341             clearCachePreference.setIcon(R.drawable.cache_warning);
342         }
343
344         // Set the `swipeToRefreshPreference` icon.
345         if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
346             swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
347         } else {
348             swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
349         }
350
351         // Set the `displayAdditionalAppBarIconsPreference` icon.
352         if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
353             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
354         } else {
355             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
356         }
357
358         // Set the `displayWebpageImagesPreference` icon.
359         if (savedPreferences.getBoolean("display_webpage_images", true)) {
360             displayWebpageImagesPreference.setIcon(R.drawable.images_enabled);
361         } else {
362             displayWebpageImagesPreference.setIcon(R.drawable.images_disabled);
363         }
364
365
366         // Listen for preference changes.
367         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
368             @Override
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) {
372
373                 switch (key) {
374                     case "javascript_enabled":
375                         // Update the icons.
376                         if (sharedPreferences.getBoolean("javascript_enabled", false)) {
377                             // Update the icon for `javascript_enabled`.
378                             javaScriptPreference.setIcon(R.drawable.javascript_enabled);
379
380                             // Update the icon for `dom_storage_enabled`.
381                             if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
382                                 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
383                             } else {
384                                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
385                             }
386                         } else {  // `javascript_enabled` is `false`.
387                             // Update the icon for `javascript_enabled`.
388                             javaScriptPreference.setIcon(R.drawable.privacy_mode);
389
390                             // Set the icon for `dom_storage_disabled` to be ghosted.
391                             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
392                         }
393                         break;
394
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);
400
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);
405                                 } else {
406                                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
407                                 }
408                             } else {
409                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
410                             }
411                         } else {  // `first_party_cookies_enabled` is `false`.
412                             // Update the icon for `first_party_cookies_enabled`.
413                             firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
414
415                             // Set the icon for `third_party_cookies_enabled` to be ghosted.
416                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
417                         }
418
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));
421                         break;
422
423                     case "third_party_cookies_enabled":
424                         // Update the icon.
425                         if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
426                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
427                         } else {
428                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
429                         }
430                         break;
431
432                     case "dom_storage_enabled":
433                         // Update the icon.
434                         if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
435                             domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
436                         } else {
437                             domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
438                         }
439                         break;
440
441                     case "save_form_data_enabled":
442                         // Update the icon.
443                         if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
444                             saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
445                         } else {
446                             saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
447                         }
448
449                     case "user_agent":
450                         String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
451
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());
456
457                                 // Update `customUserAgentPreference`.
458                                 customUserAgentPreference.setEnabled(false);
459                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
460                                 break;
461
462                             case "Custom user agent":
463                                 // Display `Custom user agent` as the summary text for `userAgentPreference`.
464                                 userAgentPreference.setSummary(R.string.custom_user_agent);
465
466                                 // Update `customUserAgentPreference`.
467                                 customUserAgentPreference.setEnabled(true);
468                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
469                                 break;
470
471                             default:
472                                 // Display the user agent as the summary text for `userAgentPreference`.
473                                 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
474
475                                 // Update `customUserAgentPreference`.
476                                 customUserAgentPreference.setEnabled(false);
477                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
478                                 break;
479                         }
480                         break;
481
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"));
485                         break;
486
487                     case "block_ads":
488                         // Update the icon.
489                         if (sharedPreferences.getBoolean("block_ads", true)) {
490                             blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
491                         } else {
492                             blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
493                         }
494                         break;
495
496                     case "incognito_mode":
497                         // Update the icon.
498                         if (sharedPreferences.getBoolean("incognito_mode", false)) {
499                             incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
500                         } else {
501                             incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
502                         }
503                         break;
504
505                     case "do_not_track":
506                         // Update the icon.
507                         if (sharedPreferences.getBoolean("do_not_track", false)) {
508                             doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
509                         } else {
510                             doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
511                         }
512
513                         break;
514
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=");
519
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"));
522
523                         // Update the icons.
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);
529
530                             // Set the `torSearchCustomURLPreference` icon.
531                             if (torSearchCustomURLPreference.isEnabled()) {
532                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
533                             } else {
534                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
535                             }
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);
541                         }
542                         break;
543
544                     case "tor_homepage":
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"));
547                         break;
548
549                     case "tor_search":
550                         // Get the present search string.
551                         String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
552
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);
557
558                             // Update `torSearchCustomURLPreference`.
559                             torSearchCustomURLPreference.setEnabled(true);
560                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
561                         } else {
562                             // Set the array value as the summary text.
563                             torSearchPreference.setSummary(presentTorSearchString);
564
565                             // Update `torSearchCustomURLPreference`.
566                             torSearchCustomURLPreference.setEnabled(false);
567                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
568                         }
569                         break;
570
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", ""));
574                         break;
575
576                     case "search":
577                         // Store the new search string.
578                         String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
579
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);
584
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);
591
592                             // Update `searchCustomURLPreference`.
593                             searchCustomURLPreference.setEnabled(false);
594                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
595                         }
596                         break;
597
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", ""));
601                         break;
602
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);
607
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);
611
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);
618
619                                 // Update `translucentNavigationBarPreference`.
620                                 translucentNavigationBarPreference.setEnabled(true);
621                                 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
622                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
623                                 } else {
624                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
625                                 }
626                             }
627                         } else {  // `full_screen_browsing_mode` is false.
628                             // Disable `translucentNavigationBarPreference`.
629                             translucentNavigationBarPreference.setEnabled(false);
630
631                             // Update the icons.
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);
635                         }
636                         break;
637
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);
642
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);
649
650                             // Update `translucentNavigationBarPreference`.
651                             translucentNavigationBarPreference.setEnabled(true);
652                             if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
653                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
654                             } else {
655                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
656                             }
657                         }
658                         break;
659
660                     case "translucent_navigation_bar":
661                         // Update the icon.
662                         if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
663                             translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
664                         } else {
665                             translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
666                         }
667                         break;
668
669                     case "clear_everything":
670                         // Store the new `clear_everything` status
671                         boolean newClearEverythingBoolean = sharedPreferences.getBoolean("clear_everything", true);
672
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);
678
679                         // Update the `clearEverythingPreference` icon.
680                         if (newClearEverythingBoolean) {
681                             clearEverythingPreference.setIcon(R.drawable.clear_everything_enabled);
682                         } else {
683                             clearEverythingPreference.setIcon(R.drawable.clear_everything_disabled);
684                         }
685
686                         // Update the `clearCookiesPreference` icon.
687                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cookies", true)) {
688                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
689                         } else {
690                             clearCookiesPreference.setIcon(R.drawable.cookies_warning);
691                         }
692
693                         // Update the `clearDomStoragePreference` icon.
694                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_dom_storage", true)) {
695                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
696                         } else {
697                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
698                         }
699
700                         // Update the `clearFormDataPreference` icon.
701                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_form_data", true)) {
702                             clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
703                         } else {
704                             clearFormDataPreference.setIcon(R.drawable.form_data_warning);
705                         }
706
707                         // Update the `clearCachePreference` icon.
708                         if (newClearEverythingBoolean || sharedPreferences.getBoolean("clear_cache", true)) {
709                             clearCachePreference.setIcon(R.drawable.cache_cleared);
710                         } else {
711                             clearCachePreference.setIcon(R.drawable.cache_warning);
712                         }
713                         break;
714
715                     case "clear_cookies":
716                         // Update the icon.
717                         if (sharedPreferences.getBoolean("clear_cookies", true)) {
718                             clearCookiesPreference.setIcon(R.drawable.cookies_cleared);
719                         } else {
720                             clearCookiesPreference.setIcon(R.drawable.cookies_warning);
721                         }
722                         break;
723
724                     case "clear_dom_storage":
725                         // Update the icon.
726                         if (sharedPreferences.getBoolean("clear_dom_storage", true)) {
727                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_cleared);
728                         } else {
729                             clearDomStoragePreference.setIcon(R.drawable.dom_storage_warning);
730                         }
731                         break;
732
733                     case "clear_form_data":
734                         // Update the icon.
735                         if (sharedPreferences.getBoolean("clear_form_data", true)) {
736                             clearFormDataPreference.setIcon(R.drawable.form_data_cleared);
737                         } else {
738                             clearFormDataPreference.setIcon(R.drawable.form_data_warning);
739                         }
740                         break;
741
742                     case "clear_cache":
743                         // Update the icon.
744                         if (sharedPreferences.getBoolean("clear_cache", true)) {
745                             clearCachePreference.setIcon(R.drawable.cache_cleared);
746                         } else {
747                             clearCachePreference.setIcon(R.drawable.cache_warning);
748                         }
749                         break;
750
751                     case "homepage":
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"));
754                         break;
755
756                     case "default_font_size":
757                         // Update the summary text of `default_font_size`.
758                         defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
759                         break;
760
761                     case "swipe_to_refresh":
762                         // Update the icon.
763                         if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
764                             swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
765                         } else {
766                             swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
767                         }
768                         break;
769
770                     case "display_additional_app_bar_icons":
771                         // Update the icon.
772                         if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
773                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
774                         } else {
775                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
776                         }
777                         break;
778
779                     case "display_webpage_images":
780                         // Update the icon.
781                         if (sharedPreferences.getBoolean("display_webpage_images", true)) {
782                             displayWebpageImagesPreference.setIcon(R.drawable.images_enabled);
783                         } else {
784                             displayWebpageImagesPreference.setIcon(R.drawable.images_disabled);
785                         }
786                         break;
787
788                     default:
789                         // If no match, do nothing.
790                         break;
791                 }
792             }
793         };
794
795         // Register the listener.
796         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
797     }
798
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.
801     @Override
802     public void onPause() {
803         super.onPause();
804         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
805     }
806
807     @Override
808     public void onResume() {
809         super.onResume();
810         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
811     }
812 }