dcc548939e58c356020cf3a0baba24b67161950a
[PrivacyBrowser.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 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
71         // Set dependencies.
72         domStoragePreference.setDependency("javascript_enabled");
73         torHomepagePreference.setDependency("proxy_through_orbot");
74         torSearchPreference.setDependency("proxy_through_orbot");
75         hideSystemBarsPreference.setDependency("full_screen_browsing_mode");
76
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=");
80
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);
88
89         // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
90         thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
91
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);
97
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());
103                 break;
104
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);
108                 break;
109
110             default:
111                 // Display the user agent from the preference as the summary text.
112                 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
113                 break;
114         }
115
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"));
119
120
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"));
123
124
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);
129         } else {
130             // Set the array value as the summary text.
131             torSearchPreference.setSummary(torSearchString);
132         }
133
134         // Set the summary text for `tor_search_custom_url`.  The default is `""`.
135         torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
136
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"));
139
140
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);
145         } else {
146             // Set the array value as the summary text.
147             searchPreference.setSummary(searchString);
148         }
149
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"));
153
154
155         // Enable `translucentNavigationBarPreference` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
156         translucentNavigationBarPreference.setEnabled(fullScreenBrowsingModeBoolean && !hideSystemBarsBoolean);
157
158
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"));
161
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") + "%%");
164
165
166         // Set the `javaScriptPreference` icon.
167         if (javaScriptEnabledBoolean) {
168             javaScriptPreference.setIcon(R.drawable.javascript_enabled);
169         } else {
170             javaScriptPreference.setIcon(R.drawable.privacy_mode);
171         }
172
173         // Set the `firstPartyCookiesPreference` icon.
174         if (firstPartyCookiesEnabledBoolean) {
175             firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
176         } else {
177             firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
178         }
179
180         // Set the `thirdPartyCookiesPreference` icon.
181         if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
182             if (thirdPartyCookiesEnabledBoolean) {
183                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
184             } else {
185                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
186             }
187         } else {
188             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
189         }
190
191         // Set the `domStoragePreference` icon.
192         if (javaScriptEnabledBoolean) {
193             if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
194                 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
195             } else {
196                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
197             }
198         } else {
199             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
200         }
201
202         // Set the `saveFormDataPreference` icon.
203         if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
204             saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
205         } else {
206             saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
207         }
208
209         // Set the `customUserAgentPreference` icon.
210         if (customUserAgentPreference.isEnabled()) {
211             customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
212         } else {
213             customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
214         }
215
216         // Set the `blockAdsPreference` icon.
217         if (savedPreferences.getBoolean("block_ads", true)) {
218             blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
219         } else {
220             blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
221         }
222
223         // Set the `incognitoModePreference` icon.
224         if (savedPreferences.getBoolean("incognito_mode", false)) {
225             incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
226         } else {
227             incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
228         }
229
230         // Set the `doNotTrackPreference` icon.
231         if (savedPreferences.getBoolean("do_not_track", false)) {
232             doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
233         } else {
234             doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
235         }
236
237         // Set the `proxyThroughOrbotPreference` icon.
238         if (proxyThroughOrbotBoolean) {
239             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_enabled);
240         } else {
241             proxyThroughOrbotPreference.setIcon(R.drawable.orbot_disabled);
242         }
243
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);
249
250             // Set the `torSearchCustomURLPreference` icon.
251             if (torSearchCustomURLPreference.isEnabled()) {
252                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
253             } else {
254                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
255             }
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);
260         }
261
262         // Set the `searchCustomURLPreference` icon.
263         if (searchCustomURLPreference.isEnabled()) {
264             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
265         } else {
266             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
267         }
268
269         // Set the full screen browsing mode icons.
270         if (fullScreenBrowsingModeBoolean) {
271             // Set the `fullScreenBrowsingModePreference` icon.
272             fullScreenBrowsingModePreference.setIcon(R.drawable.full_screen_enabled);
273
274             if (hideSystemBarsBoolean) {
275                 // Set `hideSystemBarsPreference` to use the enabled icon.
276                 hideSystemBarsPreference.setIcon(R.drawable.hide_system_bars_enabled);
277
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);
283
284                 // Set the correct icon for `translucentNavigationBarPreference`.
285                 if (savedPreferences.getBoolean("translucent_navigation_bar", true)) {
286                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
287                 } else {
288                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
289                 }
290             }
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);
295         }
296
297         // Set the `swipeToRefreshPreference` icon.
298         if (savedPreferences.getBoolean("swipe_to_refresh", false)) {
299             swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
300         } else {
301             swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
302         }
303
304         // Set the `displayAdditionalAppBarIconsPreference` icon.
305         if (savedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
306             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
307         } else {
308             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
309         }
310
311         // Listen for preference changes.
312         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
313             @Override
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) {
317
318                 switch (key) {
319                     case "javascript_enabled":
320                         // Update the icons.
321                         if (sharedPreferences.getBoolean("javascript_enabled", false)) {
322                             // Update the icon for `javascript_enabled`.
323                             javaScriptPreference.setIcon(R.drawable.javascript_enabled);
324
325                             // Update the icon for `dom_storage_enabled`.
326                             if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
327                                 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
328                             } else {
329                                 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
330                             }
331                         } else {  // `javascript_enabled` is `false`.
332                             // Update the icon for `javascript_enabled`.
333                             javaScriptPreference.setIcon(R.drawable.privacy_mode);
334
335                             // Set the icon for `dom_storage_disabled` to be ghosted.
336                             domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
337                         }
338                         break;
339
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);
345
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);
350                                 } else {
351                                     thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
352                                 }
353                             } else {
354                                 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
355                             }
356                         } else {  // `first_party_cookies_enabled` is `false`.
357                             // Update the icon for `first_party_cookies_enabled`.
358                             firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
359
360                             // Set the icon for `third_party_cookies_enabled` to be ghosted.
361                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
362                         }
363
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));
366                         break;
367
368                     case "third_party_cookies_enabled":
369                         // Update the icon.
370                         if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
371                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
372                         } else {
373                             thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
374                         }
375                         break;
376
377                     case "dom_storage_enabled":
378                         // Update the icon.
379                         if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
380                             domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
381                         } else {
382                             domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
383                         }
384                         break;
385
386                     case "save_form_data_enabled":
387                         // Update the icon.
388                         if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
389                             saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
390                         } else {
391                             saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
392                         }
393
394                     case "user_agent":
395                         String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
396
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());
401
402                                 // Update `customUserAgentPreference`.
403                                 customUserAgentPreference.setEnabled(false);
404                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
405                                 break;
406
407                             case "Custom user agent":
408                                 // Display `Custom user agent` as the summary text for `userAgentPreference`.
409                                 userAgentPreference.setSummary(R.string.custom_user_agent);
410
411                                 // Update `customUserAgentPreference`.
412                                 customUserAgentPreference.setEnabled(true);
413                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_enabled);
414                                 break;
415
416                             default:
417                                 // Display the user agent as the summary text for `userAgentPreference`.
418                                 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
419
420                                 // Update `customUserAgentPreference`.
421                                 customUserAgentPreference.setEnabled(false);
422                                 customUserAgentPreference.setIcon(R.drawable.custom_user_agent_ghosted);
423                                 break;
424                         }
425                         break;
426
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"));
430                         break;
431
432                     case "block_ads":
433                         // Update the icon.
434                         if (sharedPreferences.getBoolean("block_ads", true)) {
435                             blockAdsPreference.setIcon(R.drawable.block_ads_enabled);
436                         } else {
437                             blockAdsPreference.setIcon(R.drawable.block_ads_disabled);
438                         }
439                         break;
440
441                     case "incognito_mode":
442                         // Update the icon.
443                         if (sharedPreferences.getBoolean("incognito_mode", false)) {
444                             incognitoModePreference.setIcon(R.drawable.incognito_mode_enabled);
445                         } else {
446                             incognitoModePreference.setIcon(R.drawable.incognito_mode_disabled);
447                         }
448                         break;
449
450                     case "do_not_track":
451                         // Update the icon.
452                         if (sharedPreferences.getBoolean("do_not_track", false)) {
453                             doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
454                         } else {
455                             doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
456                         }
457
458                         break;
459
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=");
464
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"));
467
468                         // Update the icons.
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);
474
475                             // Set the `torSearchCustomURLPreference` icon.
476                             if (torSearchCustomURLPreference.isEnabled()) {
477                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
478                             } else {
479                                 torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
480                             }
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);
486                         }
487                         break;
488
489                     case "tor_homepage":
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"));
492                         break;
493
494                     case "tor_search":
495                         // Get the present search string.
496                         String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
497
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);
502
503                             // Update `torSearchCustomURLPreference`.
504                             torSearchCustomURLPreference.setEnabled(true);
505                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_enabled);
506                         } else {
507                             // Set the array value as the summary text.
508                             torSearchPreference.setSummary(presentTorSearchString);
509
510                             // Update `torSearchCustomURLPreference`.
511                             torSearchCustomURLPreference.setEnabled(false);
512                             torSearchCustomURLPreference.setIcon(R.drawable.search_custom_url_disabled);
513                         }
514                         break;
515
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", ""));
519                         break;
520
521                     case "search":
522                         // Store the new search string.
523                         String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
524
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);
529
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);
536
537                             // Update `searchCustomURLPreference`.
538                             searchCustomURLPreference.setEnabled(false);
539                             searchCustomURLPreference.setIcon(R.drawable.search_custom_url_ghosted);
540                         }
541                         break;
542
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", ""));
546                         break;
547
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);
552
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);
556
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);
563
564                                 // Update `translucentNavigationBarPreference`.
565                                 translucentNavigationBarPreference.setEnabled(true);
566                                 if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
567                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
568                                 } else {
569                                     translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
570                                 }
571                             }
572                         } else {  // `full_screen_browsing_mode` is false.
573                             // Disable `translucentNavigationBarPreference`.
574                             translucentNavigationBarPreference.setEnabled(false);
575
576                             // Update the icons.
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);
580                         }
581                         break;
582
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);
587
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);
594
595                             // Update `translucentNavigationBarPreference`.
596                             translucentNavigationBarPreference.setEnabled(true);
597                             if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
598                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
599                             } else {
600                                 translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
601                             }
602                         }
603                         break;
604
605                     case "translucent_navigation_bar":
606                         // Update the icon.
607                         if (sharedPreferences.getBoolean("translucent_navigation_bar", true)) {
608                             translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_enabled);
609                         } else {
610                             translucentNavigationBarPreference.setIcon(R.drawable.translucent_bar_disabled);
611                         }
612                         break;
613
614                     case "homepage":
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"));
617                         break;
618
619                     case "default_font_size":
620                         // Update the summary text of `default_font_size`.
621                         defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
622                         break;
623
624                     case "swipe_to_refresh":
625                         // Update the icon.
626                         if (sharedPreferences.getBoolean("swipe_to_refresh", false)) {
627                             swipeToRefreshPreference.setIcon(R.drawable.refresh_enabled);
628                         } else {
629                             swipeToRefreshPreference.setIcon(R.drawable.refresh_disabled);
630                         }
631                         break;
632
633                     case "display_additional_app_bar_icons":
634                         // Update the icon.
635                         if (sharedPreferences.getBoolean("display_additional_app_bar_icons", false)) {
636                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_enabled);
637                         } else {
638                             displayAdditionalAppBarIconsPreference.setIcon(R.drawable.more_disabled);
639                         }
640                         break;
641
642                     default:
643                         // If no match, do nothing.
644                         break;
645                 }
646             }
647         };
648
649         // Register the listener.
650         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
651     }
652
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.
655     @Override
656     public void onPause() {
657         super.onPause();
658         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
659     }
660
661     @Override
662     public void onResume() {
663         super.onResume();
664         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
665     }
666 }