3710531751d1b1041ea468a1f45070669439757b
[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.Bundle;
25 import android.preference.Preference;
26 import android.preference.PreferenceFragment;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.webkit.WebView;
30
31 import com.stoutner.privacybrowser.R;
32
33 public class SettingsFragment extends PreferenceFragment {
34     private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
35     private SharedPreferences savedPreferences;
36
37     @Override
38     public void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         addPreferencesFromResource(R.xml.preferences);
41
42         // Initialize savedPreferences.
43         savedPreferences = getPreferenceScreen().getSharedPreferences();
44
45         // Get handles for the preferences we need to modify.
46         final Preference domStorageEnabled = findPreference("dom_storage_enabled");
47         final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
48         final Preference userAgentPreference = findPreference("user_agent");
49         final Preference customUserAgent = findPreference("custom_user_agent");
50         final Preference torHomepagePreference = findPreference("tor_homepage");
51         final Preference torSearchPreference = findPreference("tor_search");
52         final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
53         final Preference searchPreference = findPreference("search");
54         final Preference searchCustomURLPreference = findPreference("search_custom_url");
55         final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
56         final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
57         final Preference homepagePreference = findPreference("homepage");
58         final Preference defaultFontSizePreference = findPreference("default_font_size");
59
60         // Get booleans from the preferences.
61         final boolean fullScreenBrowsingModeEnabled = savedPreferences.getBoolean("enable_full_screen_browsing_mode", false);
62         final boolean proxyThroughOrbot = savedPreferences.getBoolean("proxy_through_orbot", false);
63
64         // Get strings from the preferences.
65         String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
66         String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
67         String defaultFontSizeString = savedPreferences.getString("default_font_size", "100");
68
69         // Allow the user to access "dom_storage_enabled" if "javascript_enabled" is enabled.  The default is false.
70         domStorageEnabled.setEnabled(savedPreferences.getBoolean("javascript_enabled", false));
71
72         // Allow the user to access "third_party_cookies_enabled" if "first_party_cookies_enabled" is enabled.  The default is false.
73         thirdPartyCookiesEnabled.setEnabled(savedPreferences.getBoolean("first_party_cookies_enabled", false));
74
75
76         // We need to inflated a `WebView` to get the default user agent.
77         LayoutInflater inflater = getActivity().getLayoutInflater();
78         // `@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.
79         @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
80         final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
81
82         // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
83         switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
84             case "WebView default user agent":
85                 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
86                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
87                 break;
88
89             case "Custom user agent":
90                 // We can't use the string from the array because it is referenced in code and can't be translated.
91                 userAgentPreference.setSummary(R.string.custom_user_agent);
92                 break;
93
94             default:
95                 // Display the user agent from the preference as the summary text.
96                 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
97                 break;
98         }
99
100         // Set the summary text for "custom_user_agent" (the default is "PrivacyBrowser/1.0") and enable it if "user_agent" it set to "Custom user agent".
101         customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
102         customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
103
104
105         // 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`.
106         torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
107
108
109         // 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=`
110         if (torSearchString.equals("Custom URL")) {
111             // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
112             torSearchPreference.setSummary(R.string.custom_url);
113         } else {
114             // Set the array value as the summary text.
115             torSearchPreference.setSummary(torSearchString);
116         }
117
118         // Set the summary text for `torsearch_custom_url`.  The default is `""`.
119         torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
120
121         // Enable the Tor preferences only if `proxy_through_orbot` is enabled.  The default is `false`.
122         torHomepagePreference.setEnabled(proxyThroughOrbot);
123         torSearchPreference.setEnabled(proxyThroughOrbot);
124
125         // Enable the Tor custom URL search options only if `proxyThroughOrbot` is true and the search is set to `Custom URL`.
126         torSearchCustomURLPreference.setEnabled(proxyThroughOrbot && torSearchString.equals("Custom URL"));
127
128
129         // 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=`.
130         if (searchString.equals("Custom URL")) {
131             // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
132             searchPreference.setSummary(R.string.custom_url);
133         } else {
134             // Set the array value as the summary text.
135             searchPreference.setSummary(searchString);
136         }
137
138         // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
139         searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
140         searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
141
142
143         // Enable the full screen options if full screen browsing mode is enabled.
144         if (!fullScreenBrowsingModeEnabled) {
145             // Disable the full screen options.
146             hideSystemBarsPreference.setEnabled(false);
147             translucentNavigationBarPreference.setEnabled(false);
148         } else {
149             // Disable `transparent_navigation_bar` if `hide_system_bars` is `true`.
150             translucentNavigationBarPreference.setEnabled(!savedPreferences.getBoolean("hide_system_bars", false));
151         }
152
153
154         // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded.  The default is `https://duckduckgo.com`.
155         homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
156
157         // 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`.
158         defaultFontSizePreference.setSummary(defaultFontSizeString + "%%");
159
160
161         // Listen for preference changes.
162         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
163             @Override
164             // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.  We know.
165             @SuppressLint("SetJavaScriptEnabled")
166             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
167
168                 switch (key) {
169                     case "javascript_enabled":
170                         // Toggle the state of the `dom_storage_enabled` preference.  The default is `false`.
171                         final Preference domStorageEnabled = findPreference("dom_storage_enabled");
172                         domStorageEnabled.setEnabled(sharedPreferences.getBoolean("javascript_enabled", false));
173                         break;
174
175                     case "first_party_cookies_enabled":
176                         // Toggle the state of the `third_party_cookies_enabled` preference.  The default is `false`.
177                         final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
178                         thirdPartyCookiesEnabled.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false));
179                         break;
180
181                     case "user_agent":
182                         String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
183
184                         switch (userAgentString) {
185                             case "WebView default user agent":
186                                 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
187                                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
188                                 customUserAgent.setEnabled(false);
189                                 break;
190
191                             case "Custom user agent":
192                                 // Display "Custom user agent" as the summary text for userAgentPreference, and enable customUserAgent.
193                                 userAgentPreference.setSummary(R.string.custom_user_agent);
194                                 customUserAgent.setEnabled(true);
195                                 break;
196
197                             default:
198                                 // Display the user agent as the summary text for userAgentPreference, and disable customUserAgent.
199                                 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
200                                 customUserAgent.setEnabled(false);
201                                 break;
202                         }
203                         break;
204
205                     case "custom_user_agent":
206                         // Set the new custom user agent as the summary text for `custom_user_agent`.  The default is `PrivacyBrowser/1.0`.
207                         customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
208                         break;
209
210                     case "proxy_through_orbot":
211                         // Get current settings.
212                         boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
213                         String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
214
215                         // Enable the Tor preferences only if `proxy_through_orbot` is enabled.  The default is `false`.
216                         torHomepagePreference.setEnabled(currentProxyThroughOrbot);
217                         torSearchPreference.setEnabled(currentProxyThroughOrbot);
218
219                         // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
220                         torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
221                         break;
222
223                     case "tor_homepage":
224                         // Set the new tor homepage URL as the summary text for the `tor_homepage` preference.  The default is DuckDuckGo:  `https://3g2upl4pq6kufc4m.onion`.
225                         torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
226                         break;
227
228                     case "tor_search":
229                         // Get the present search string.
230                         String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
231
232                         // Set the summary text for `tor_search`.
233                         if (presentTorSearchString.equals("Custom URL")) {
234                             // Use R.string.custom_url, which is translated, instead of the array value, which isn't.
235                             torSearchPreference.setSummary(R.string.custom_url);
236                         } else {
237                             // Set the array value as the summary text.
238                             torSearchPreference.setSummary(presentTorSearchString);
239                         }
240
241                         // Set the status of `torJavaScriptDisabledSearchCustomURLPreference`.
242                         torSearchCustomURLPreference.setEnabled(presentTorSearchString.equals("Custom URL"));
243                         break;
244
245                     case "tor_search_custom_url":
246                         // Set the summary text for `tor_search_custom_url`.
247                         torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
248                         break;
249
250                     case "search":
251                         String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
252                         if (newSearchString.equals("Custom URL")) {  // Set the summary text to `R.string.custom_url`, which is translated.
253                             searchPreference.setSummary(R.string.custom_url);
254                         } else {  // Set the new search URL as the summary text for the JavaScript-disabled search preference.
255                             searchPreference.setSummary(newSearchString);
256                         }
257
258                         // Enable or disable `searchCustomURLPreference`.
259                         searchCustomURLPreference.setEnabled(newSearchString.equals("Custom URL"));
260                         break;
261
262                     case "search_custom_url":
263                         // Set the new custom search URL as the summary text for `search_custom_url`.  The default is `""`.
264                         searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
265                         break;
266
267                     case "enable_full_screen_browsing_mode":
268                         boolean newFullScreenBrowsingModeEnabled = sharedPreferences.getBoolean("enable_full_screen_browsing_mode", false);
269                         if (newFullScreenBrowsingModeEnabled) {
270                             // Enable `hideSystemBarsPreference`.
271                             hideSystemBarsPreference.setEnabled(true);
272
273                             // Only enable `transparent_navigation_bar` if `hide_system_bars` is `false`.
274                             translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
275                         } else {
276                             // Disable the full screen options.
277                             hideSystemBarsPreference.setEnabled(false);
278                             translucentNavigationBarPreference.setEnabled(false);
279                         }
280                         break;
281
282                     case "homepage":
283                         // Set the new homepage URL as the summary text for the Homepage preference.  The default is `https://www.duckduckgo.com`.
284                         homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
285                         break;
286
287                     case "default_font_size":
288                         // Get the default font size as a string.  The default is `100`.
289                         String newDefaultFontSizeString = sharedPreferences.getString("default_font_size", "100");
290
291                         // Update the summary text of `default_font_size`.
292                         defaultFontSizePreference.setSummary(newDefaultFontSizeString + "%%");
293                         break;
294
295                     case "hide_system_bars":
296                         // Enable `translucentNavigationBarPreference` if `hide_system_bars` is disabled.
297                         translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
298                         break;
299
300                     default:
301                         // If no match, do nothing.
302                         break;
303                 }
304             }
305         };
306
307         // Register the listener.
308         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
309     }
310
311     // 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
312     // even while running in the foreground.
313     @Override
314     public void onPause() {
315         super.onPause();
316         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
317     }
318
319     @Override
320     public void onResume() {
321         super.onResume();
322         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
323     }
324 }