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