]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java
Implement Orbot proxy support. Fixes https://redmine.stoutner.com/issues/26.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / SettingsFragment.java
1 /**
2  * Copyright 2016 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;
21
22 import android.annotation.SuppressLint;
23 import android.app.Activity;
24 import android.content.SharedPreferences;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.preference.Preference;
28 import android.preference.PreferenceFragment;
29 import android.view.MenuItem;
30
31 public class SettingsFragment extends PreferenceFragment {
32     private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
33     private SharedPreferences savedPreferences;
34
35     @Override
36     public void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         addPreferencesFromResource(R.xml.preferences);
39
40         // Initialize savedPreferences.
41         savedPreferences = getPreferenceScreen().getSharedPreferences();
42
43         // Allow the user to access "dom_storage_enabled" if "javascript_enabled" is enabled.  The default is false.
44         final Preference domStorageEnabled = findPreference("dom_storage_enabled");
45         domStorageEnabled.setEnabled(savedPreferences.getBoolean("javascript_enabled", false));
46
47         // Allow the user to access "third_party_cookies_enabled" if "first_party_cookies_enabled" is enabled.  The default is false.
48         final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
49         thirdPartyCookiesEnabled.setEnabled(savedPreferences.getBoolean("first_party_cookies_enabled", false));
50
51         // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
52         final Preference userAgentPreference = findPreference("user_agent");
53         switch (savedPreferences.getString("user_agent", "Default user agent")) {
54             case "Default user agent":
55                 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
56                 // Once API >= 17 we can use getDefaultUserAgent() instead of getUserAgentString().
57                 userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString());
58                 break;
59
60             case "Custom user agent":
61                 // We can't use the string from the array because it is referenced in code and can't be translated.
62                 userAgentPreference.setSummary(R.string.custom_user_agent);
63                 break;
64
65             default:
66                 // Display the user agent from the preference as the summary text.
67                 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "Default user agent"));
68                 break;
69         }
70
71         // 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".
72         final Preference customUserAgent = findPreference("custom_user_agent");
73         customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
74         customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
75
76
77         // Set the JavaScript-disabled search URL as the summary text for the JavaScript-disabled search preference when the preference screen is loaded.
78         // The default is "https://duckduckgo.com/html/?q=".
79         final Preference javaScriptDisabledSearchPreference = findPreference("javascript_disabled_search");
80         String javaScriptDisabledSearchString = savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
81         if (javaScriptDisabledSearchString.equals("Custom URL")) {
82             // If set to "Custom URL", use R.string.custom_url, which will be translated, instead of the array value, which will not.
83             javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
84         } else {
85             // Set the array value as the summary text.
86             javaScriptDisabledSearchPreference.setSummary(javaScriptDisabledSearchString);
87         }
88
89         // Set the summary text for "javascript_disabled_search_custom_url" (the default is "") and enable it if "javascript_disabled_search" is set to "Custom URL".
90         final Preference javaScriptDisabledSearchCustomURLPreference = findPreference("javascript_disabled_search_custom_url");
91         javaScriptDisabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_disabled_search_custom_url", ""));
92         javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchString.equals("Custom URL"));
93
94
95         // Set the JavaScript-enabled searchURL as the summary text for the JavaScript-enabled search preference when the preference screen is loaded.
96         // The default is "https://duckduckgo.com/?q=".
97         final Preference javaScriptEnabledSearchPreference = findPreference("javascript_enabled_search");
98         String javaScriptEnabledSearchString = savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
99         if (javaScriptEnabledSearchString.equals("Custom URL")) {
100             // If set to "Custom URL", use R.string.custom_url, which will be translated, instead of the array value, which will not.
101             javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
102         } else {
103             // Set the array value as the summary text.
104             javaScriptEnabledSearchPreference.setSummary(javaScriptEnabledSearchString);
105         }
106
107         // Set the summary text for "javascript_enabled_search_custom_url" (the default is "") and enable it if "javascript_enabled_search" is set to "Custom URL".
108         final Preference javaScriptEnabledSearchCustomURLPreference = findPreference("javascript_enabled_search_custom_url");
109         javaScriptEnabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_enabled_search_custom_url", ""));
110         javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchString.equals("Custom URL"));
111
112
113         // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded.  The default is `https://www.duckduckgo.com`.
114         final Preference homepagePreference = findPreference("homepage");
115         homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
116
117         // 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`.
118         final Preference defaultFontSizePreference = findPreference("default_font_size");
119         String defaultFontSizeString = savedPreferences.getString("default_font_size", "100");
120         defaultFontSizePreference.setSummary(defaultFontSizeString + "%%");
121
122
123         // Listen for preference changes.
124         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
125             @Override
126             // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.  We know.
127             @SuppressLint("SetJavaScriptEnabled")
128             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
129
130                 switch (key) {
131                     case "javascript_enabled":
132                         // Set `javaScriptEnabled` to the new state.  The default is `false`.
133                         MainWebViewActivity.javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
134
135                         // Toggle the state of the `dom_storage_enabled` preference.  The default is `false`.
136                         final Preference domStorageEnabled = findPreference("dom_storage_enabled");
137                         domStorageEnabled.setEnabled(sharedPreferences.getBoolean("javascript_enabled", false));
138
139                         // Update `mainWebView`.
140                         MainWebViewActivity.mainWebView.getSettings().setJavaScriptEnabled(MainWebViewActivity.javaScriptEnabled);
141
142                         // Update the privacy icons.
143                         MainWebViewActivity.updatePrivacyIcons(MainWebViewActivity.privacyBrowserActivity);
144                         break;
145
146                     case "first_party_cookies_enabled":
147                         // Set `firstPartyCookiesEnabled` to the new state.  The default is `false`.
148                         MainWebViewActivity.firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false);
149
150                         // Toggle the state of the `third_party_cookies_enabled` preference.  The default is `false`.
151                         final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
152                         thirdPartyCookiesEnabled.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false));
153
154                         // Update `mainWebView`.
155                         MainWebViewActivity.cookieManager.setAcceptCookie(MainWebViewActivity.firstPartyCookiesEnabled);
156
157                         // Update the checkbox in the options menu.
158                         MenuItem firstPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleFirstPartyCookies);
159                         firstPartyCookiesMenuItem.setChecked(MainWebViewActivity.firstPartyCookiesEnabled);
160
161                         // Update the privacy icons.
162                         MainWebViewActivity.updatePrivacyIcons(MainWebViewActivity.privacyBrowserActivity);
163                         break;
164
165                     case "third_party_cookies_enabled":
166                         // Set `thirdPartyCookiesEnabled` to the new state.  The default is `false`.
167                         MainWebViewActivity.thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false);
168
169                         // Update the checkbox in the options menu.
170                         MenuItem thirdPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleThirdPartyCookies);
171                         thirdPartyCookiesMenuItem.setChecked(MainWebViewActivity.thirdPartyCookiesEnabled);
172
173                         // Update `mainWebView` if API >= 21.
174                         if (Build.VERSION.SDK_INT >= 21) {
175                             MainWebViewActivity.cookieManager.setAcceptThirdPartyCookies(MainWebViewActivity.mainWebView, MainWebViewActivity.thirdPartyCookiesEnabled);
176                         }
177                         break;
178
179                     case "dom_storage_enabled":
180                         // Set `domStorageEnabled` to the new state.  The default is `false`.
181                         MainWebViewActivity.domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false);
182
183                         // Update the checkbox in the options menu.
184                         MenuItem domStorageMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleDomStorage);
185                         domStorageMenuItem.setChecked(MainWebViewActivity.domStorageEnabled);
186
187                         // Update `mainWebView`.
188                         MainWebViewActivity.mainWebView.getSettings().setDomStorageEnabled(MainWebViewActivity.domStorageEnabled);
189
190                         // Update the privacy icons.
191                         MainWebViewActivity.updatePrivacyIcons(MainWebViewActivity.privacyBrowserActivity);
192                         break;
193
194                     case "save_form_data_enabled":
195                         // Set `saveFormDataEnabled` to the new state.  The default is `false`.
196                         MainWebViewActivity.saveFormDataEnabled = sharedPreferences.getBoolean("save_form_data_enabled", false);
197
198                         // Update the checkbox in the options menu.
199                         MenuItem saveFormDataMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleSaveFormData);
200                         saveFormDataMenuItem.setChecked(MainWebViewActivity.saveFormDataEnabled);
201
202                         // Update `mainWebView`.
203                         MainWebViewActivity.mainWebView.getSettings().setSaveFormData(MainWebViewActivity.saveFormDataEnabled);
204
205                         // Update the privacy icons.
206                         MainWebViewActivity.updatePrivacyIcons(MainWebViewActivity.privacyBrowserActivity);
207                         break;
208
209                     case "user_agent":
210                         String userAgentString = sharedPreferences.getString("user_agent", "Default user agent");
211
212                         switch (userAgentString) {
213                             case "Default user agent":
214                                 // Set the default user agent on `mainWebView`, display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
215                                 // Once API >= 17 we can use getDefaultUserAgent().  For now, setUserAgentString("") sets the WebView's default user agent.
216                                 MainWebViewActivity.mainWebView.getSettings().setUserAgentString("");
217                                 userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString());
218                                 customUserAgent.setEnabled(false);
219                                 break;
220
221                             case "Custom user agent":
222                                 // Set the custom user agent on mainWebView, display "Custom user agent" as the summary text for userAgentPreference, and enable customUserAgent.
223                                 MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
224                                 userAgentPreference.setSummary(R.string.custom_user_agent);
225                                 customUserAgent.setEnabled(true);
226                                 break;
227
228                             default:
229                                 // Set the user agent on mainWebView, display the user agent as the summary text for userAgentPreference, and disable customUserAgent.
230                                 MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("user_agent", "Default user agent"));
231                                 userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString());
232                                 customUserAgent.setEnabled(false);
233                                 break;
234                         }
235                         break;
236
237                     case "custom_user_agent":
238                         // Set the new custom user agent as the summary text for "custom_user_agent".  The default is "PrivacyBrowser/1.0".
239                         customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
240
241                         // Update mainWebView's user agent.  The default is "PrivacyBrowser/1.0".
242                         MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
243                         break;
244
245                     case "proxy_through_orbot":
246                         // Get a handle for `settingsActivity`.
247                         Activity settingsActivity = getActivity();
248
249                         // Update the proxy.  The default is `false`
250                         if (sharedPreferences.getBoolean("proxy_through_orbot", false)) {  // Orbot proxies on localhost port 8118.
251                             OrbotProxyHelper.setProxy(MainWebViewActivity.privacyBrowserContext, settingsActivity, "localhost", "8118");
252                         } else {  // Disable the proxy by setting the host to `null` and the port to `0`.
253                             OrbotProxyHelper.setProxy(MainWebViewActivity.privacyBrowserContext, settingsActivity, "", "0");
254                         }
255
256                     case "javascript_disabled_search":
257                         String newJavaScriptDisabledSearchString = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
258                         if (newJavaScriptDisabledSearchString.equals("Custom URL")) {
259                             // Set the summary text to R.string.custom_url, which will be translated.
260                             javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
261
262                             // Update the javaScriptDisabledSearchURL variable.  The default is "".
263                             MainWebViewActivity.javaScriptDisabledSearchURL = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
264                         } else {  // javascript_disabled_search is not set to Custom.
265                             // Set the new search URL as the summary text for the JavaScript-disabled search preference.  The default is "https://duckduckgo.com/html/?q=".
266                             javaScriptDisabledSearchPreference.setSummary(newJavaScriptDisabledSearchString);
267
268                             // Update the javaScriptDisabledSearchURL variable.  The default is "https://duckduckgo.com/html/?q=".
269                             MainWebViewActivity.javaScriptDisabledSearchURL = newJavaScriptDisabledSearchString;
270                         }
271
272                         // Enable or disable javaScriptDisabledSearchCustomURLPreference.
273                         javaScriptDisabledSearchCustomURLPreference.setEnabled(newJavaScriptDisabledSearchString.equals("Custom URL"));
274                         break;
275
276                     case "javascript_disabled_search_custom_url":
277                         // Set the new custom search URL as the summary text for "javascript_disabled_search_custom_url".  The default is "".
278                         javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", ""));
279
280                         // Update javaScriptDisabledSearchCustomURL.  The default is "".
281                         MainWebViewActivity.javaScriptDisabledSearchURL = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
282                         break;
283
284                     case "javascript_enabled_search":
285                         String newJavaScriptEnabledSearchString = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
286                         if (newJavaScriptEnabledSearchString.equals("Custom URL")) {
287                             // Set the summary text to R.string.custom_url, which will be translated.
288                             javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
289
290                             // Update the javaScriptEnabledSearchURL variable.  The default is "".
291                             MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
292                         } else { // javascript_enabled_search is not set to Custom.
293                             // Set the new search URL as the summary text for the JavaScript-enabled search preference.  The default is "https://duckduckgo.com/?q=".
294                             javaScriptEnabledSearchPreference.setSummary(newJavaScriptEnabledSearchString);
295
296                             // Update the javaScriptEnabledSearchURL variable.  The default is "https://duckduckgo.com/?q=".
297                             MainWebViewActivity.javaScriptEnabledSearchURL = newJavaScriptEnabledSearchString;
298                         }
299
300                         // Enable or disable javaScriptEnabledSearchCustomURLPreference.
301                         javaScriptEnabledSearchCustomURLPreference.setEnabled(newJavaScriptEnabledSearchString.equals("Custom URL"));
302                         break;
303
304                     case "javascript_enabled_search_custom_url":
305                         // Set the new custom search URL as the summary text for `javascript_enabled_search_custom_url`.  The default is ``.
306                         javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", ""));
307
308                         // Update javaScriptEnabledSearchCustomURL.  The default is ``.
309                         MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
310                         break;
311
312                     case "do_not_track":
313                         // Update `customHeaders`.  The default is `true`.
314                         if (sharedPreferences.getBoolean("do_not_track", true)) {
315                             MainWebViewActivity.customHeaders.put("DNT", "1");
316                         } else {  // Remove the Do Not Track header.
317                             MainWebViewActivity.customHeaders.remove("DNT");
318                         }
319
320                     case "homepage":
321                         // Set the new homepage URL as the summary text for the Homepage preference.  The default is `https://www.duckduckgo.com`.
322                         homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
323
324                         // Update the homepage variable.  The default is `https://www.duckduckgo.com`.
325                         MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
326                         break;
327
328                     case "default_font_size":
329                         // Get the default font size as a string.  The default is `100`.
330                         String newDefaultFontSizeString = sharedPreferences.getString("default_font_size", "100");
331
332                         // Update the font size on `mainWebView`.  The default is `100`.
333                         MainWebViewActivity.mainWebView.getSettings().setTextZoom(Integer.valueOf(newDefaultFontSizeString));
334
335                         // Update the summary text of `default_font_size`.
336                         defaultFontSizePreference.setSummary(newDefaultFontSizeString + "%%");
337
338                     case "swipe_to_refresh_enabled":
339                         // Set `swipeToRefreshEnabled` to the new state.  The default is `true`.
340                         MainWebViewActivity.swipeToRefreshEnabled = sharedPreferences.getBoolean("swipe_to_refresh_enabled", true);
341
342                         // Update `swipeRefreshLayout` to match the new state.
343                         MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled);
344                         break;
345
346                     default:
347                         // If no match, do nothing.
348                         break;
349                 }
350             }
351         };
352
353         // Register the listener.
354         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
355     }
356
357     // 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
358     // even while running in the foreground.
359     @Override
360     public void onPause() {
361         super.onPause();
362         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
363     }
364
365     @Override
366     public void onResume() {
367         super.onResume();
368         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
369     }
370 }