]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java
Add preference for customizing the user agent.
[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
43         // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
44         final Preference userAgentPreference = findPreference("user_agent");
45         // Get the user agent text from the webview (which changes based on the version of Android and WebView installed) if we are using the default.
46         if (savedPreferences.getString("user_agent", "Default user agent").equals("Default user agent")) {
47             // Once API >= 17 we can use getDefaultUserAgent() instead of getUserAgentString().
48             userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString());
49         } else { // Display the user-agent from the preference as the summary text.
50             userAgentPreference.setSummary(savedPreferences.getString("user_agent", "Default user agent"));
51         }
52
53         // 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".
54         final Preference customUserAgent = findPreference("custom_user_agent");
55         customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
56         customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
57
58
59         // Set the JavaScript-disabled search URL as the summary text for the JavaScript-disabled search preference when the preference screen is loaded.
60         // The default is "https://duckduckgo.com/html/?q=".
61         final Preference javaScriptDisabledSearchPreference = findPreference("javascript_disabled_search");
62         javaScriptDisabledSearchPreference.setSummary(savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="));
63
64         // 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".
65         final Preference javaScriptDisabledSearchCustomURLPreference = findPreference("javascript_disabled_search_custom_url");
66         javaScriptDisabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_disabled_search_custom_url", ""));
67         javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchPreference.getSummary().equals("Custom URL"));
68
69         // Set the JavaScript-enabed searchURL as the summary text for the JavaScript-enabled search preference when the preference screen is loaded.
70         // The default is "https://duckduckgo.com/?q=".
71         final Preference javaScriptEnabledSearchPreference = findPreference("javascript_enabled_search");
72         javaScriptEnabledSearchPreference.setSummary(savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="));
73
74         // 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".
75         final Preference javaScriptEnabledSearchCustomURLPreference = findPreference("javascript_enabled_search_custom_url");
76         javaScriptEnabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_enabled_search_custom_url", ""));
77         javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchPreference.getSummary().equals("Custom URL"));
78
79
80         // 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".
81         final Preference homepagePreference = findPreference("homepage");
82         homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
83
84
85         // Listen for preference changes.
86         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
87             @Override
88             // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
89             @SuppressLint("SetJavaScriptEnabled")
90             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
91
92                 // Several keys need to update the toggleJavaScript icon.
93                 MenuItem toggleJavaScript = MainWebViewActivity.mainMenu.findItem(R.id.toggleJavaScript);
94
95                 switch (key) {
96                     case "javascript_enabled":
97                         // Set javaScriptEnabled to the new state.  The default is false.
98                         MainWebViewActivity.javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
99
100                         // Update mainWebView and reload the website.
101                         MainWebViewActivity.mainWebView.getSettings().setJavaScriptEnabled(MainWebViewActivity.javaScriptEnabled);
102                         MainWebViewActivity.mainWebView.reload();
103
104                         // Update the toggleJavaScript icon.
105                         if (MainWebViewActivity.javaScriptEnabled) {
106                             toggleJavaScript.setIcon(R.drawable.javascript_enabled);
107                         } else {
108                             if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
109                                 toggleJavaScript.setIcon(R.drawable.warning);
110                             } else {
111                                 toggleJavaScript.setIcon(R.drawable.privacy_mode);
112                             }
113                         }
114                         break;
115
116                     case "first_party_cookies_enabled":
117                         // Set firstPartyCookiesEnabled to the new state.  The default is false.
118                         MainWebViewActivity.firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false);
119
120                         // Update the checkbox in the options menu.
121                         MenuItem firstPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleFirstPartyCookies);
122                         firstPartyCookiesMenuItem.setChecked(MainWebViewActivity.firstPartyCookiesEnabled);
123
124                         // Update mainWebView and reload the website.
125                         MainWebViewActivity.cookieManager.setAcceptCookie(MainWebViewActivity.firstPartyCookiesEnabled);
126                         MainWebViewActivity.mainWebView.reload();
127
128                         // Update the toggleJavaScript icon.
129                         if (MainWebViewActivity.javaScriptEnabled) {
130                             toggleJavaScript.setIcon(R.drawable.javascript_enabled);
131                         } else {
132                             if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
133                                 toggleJavaScript.setIcon(R.drawable.warning);
134                             } else {
135                                 toggleJavaScript.setIcon(R.drawable.privacy_mode);
136                             }
137                         }
138                         break;
139
140                     case "third_party_cookies_enabled":
141                         // Set thirdPartyCookiesEnabled to the new state.  The default is false.
142                         MainWebViewActivity.thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false);
143
144                         // Update the checkbox in the options menu.
145                         MenuItem thirdPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleThirdPartyCookies);
146                         thirdPartyCookiesMenuItem.setChecked(MainWebViewActivity.thirdPartyCookiesEnabled);
147
148                         // Update mainWebView and reload the website if API >= 21.
149                         if (Build.VERSION.SDK_INT >= 21) {
150                             MainWebViewActivity.cookieManager.setAcceptThirdPartyCookies(MainWebViewActivity.mainWebView, MainWebViewActivity.thirdPartyCookiesEnabled);
151                             MainWebViewActivity.mainWebView.reload();
152                         }
153                         break;
154
155                     case "dom_storage_enabled":
156                         // Set domStorageEnabled to the new state.  The default is false.
157                         MainWebViewActivity.domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false);
158
159                         // Update the checkbox in the options menu.
160                         MenuItem domStorageMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleDomStorage);
161                         domStorageMenuItem.setChecked(MainWebViewActivity.domStorageEnabled);
162
163                         // Update mainWebView and reload the website.
164                         MainWebViewActivity.mainWebView.getSettings().setDomStorageEnabled(MainWebViewActivity.domStorageEnabled);
165                         MainWebViewActivity.mainWebView.reload();
166
167                         // Update the toggleJavaScript icon.
168                         if (MainWebViewActivity.javaScriptEnabled) {
169                             toggleJavaScript.setIcon(R.drawable.javascript_enabled);
170                         } else {
171                             if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
172                                 toggleJavaScript.setIcon(R.drawable.warning);
173                             } else {
174                                 toggleJavaScript.setIcon(R.drawable.privacy_mode);
175                             }
176                         }
177                         break;
178
179                     case "user_agent":
180                         String userAgentString = sharedPreferences.getString("user_agent", "Default user agent");
181
182                         switch (userAgentString) {
183                             case "Default user agent":
184                                 // Set the default user agent on mainWebView, display the user agent as the summary text for userAgentPreference, and disable customUserAgent.
185                                 // Once API >= 17 we can use getDefaultUserAgent().  For now, setUserAgentString("") sets the WebView's default user agent.
186                                 MainWebViewActivity.mainWebView.getSettings().setUserAgentString("");
187                                 userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString());
188                                 customUserAgent.setEnabled(false);
189                                 break;
190
191                             case "Custom user agent":
192                                 // Set the custom user agent on mainWebView, display "Custom user agent" as the summary text for userAgentPreference, and enable customUserAgent.
193                                 MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
194                                 userAgentPreference.setSummary("Custom user agent");
195                                 customUserAgent.setEnabled(true);
196                                 break;
197
198                             default:
199                                 // Set the user agent on mainWebView, display the user agent as the summary text for userAgentPreference, and disable customUserAgent.
200                                 MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
201                                 userAgentPreference.setSummary(MainWebViewActivity.mainWebView.getSettings().getUserAgentString());
202                                 customUserAgent.setEnabled(false);
203                                 break;
204                         }
205                         break;
206
207                     case "custom_user_agent":
208                         // Set the new custom user agent as the summary text for "custom_user_agent".  The default is "PrivacyBrowser/1.0".
209                         customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
210
211                         // Update mainWebView's user agent.  The default is "PrivacyBrowser/1.0".
212                         MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
213                         break;
214
215                     case "javascript_disabled_search":
216                         // Set the new search URL as the summary text for the JavaScript-disabled search preference.  The default is "https://duckduckgo.com/html/?q=".
217                         javaScriptDisabledSearchPreference.setSummary(sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="));
218
219                         // Enable "javascript_disabled_search_custom_url" if "javascript_disabled_search" is set to "Custom URL".
220                         javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchPreference.getSummary().equals("Custom URL"));
221
222                         // Update the javaScriptDisabledSearchURL variable.  The default is "https://duckduckgo.com/html/?q=".
223                         MainWebViewActivity.javaScriptDisabledSearchURL = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
224                         break;
225
226                     case "javascript_disabled_search_custom_url":
227                         // Set the new custom search URL as the summary text for "javascript_disabled_search_custom_url".  The default is "".
228                         javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", ""));
229
230                         // Update javaScriptDisabledSearchCustomURL.  The default is "".
231                         MainWebViewActivity.javaScriptDisabledSearchCustomURL = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
232                         break;
233
234                     case "javascript_enabled_search":
235                         // Set the new search URL as the summary text for the JavaScript-enabled search preference.  The default is "https://duckduckgo.com/?q=".
236                         javaScriptEnabledSearchPreference.setSummary(sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="));
237
238                         // Enable "javascript_enabled_search_custom_url" if "javascript_enabled_search" is set to "Custom URL".
239                         javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchPreference.getSummary().equals("Custom URL"));
240
241                         // Update the javaScriptEnabledSearchURL variable.  The default is "https://duckduckgo.com/?q=".
242                         MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
243                         break;
244
245                     case "javascript_enabled_search_custom_url":
246                         // Set the new custom search URL as the summary text for "javascript_enabled_search_custom_url".  The default is "".
247                         javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", ""));
248
249                         // Update javaScriptEnabledSearchCustomURL.  The default is "".
250                         MainWebViewActivity.javaScriptEnabledSearchCustomURL = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
251                         break;
252
253                     case "homepage":
254                         // Set the new homepage URL as the summary text for the Homepage preference.  The default is "https://www.duckduckgo.com".
255                         homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
256
257                         // Update the homepage variable.  The default is "https://www.duckduckgo.com".
258                         MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
259                         break;
260
261                     case "swipe_to_refresh_enabled":
262                         // Set swipeToRefreshEnabled to the new state.  The default is true.
263                         MainWebViewActivity.swipeToRefreshEnabled = sharedPreferences.getBoolean("swipe_to_refresh_enabled", true);
264
265                         // Update swipeRefreshLayout to match the new state.
266                         MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled);
267                         break;
268
269                     default:
270                         // If no match, do nothing.
271                         break;
272                 }
273             }
274         };
275
276         // Register the listener.
277         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
278     }
279
280     // 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
281     // even while running in the foreground.
282     @Override
283     public void onResume() {
284         super.onResume();
285         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
286     }
287
288     @Override
289     public void onPause() {
290         super.onPause();
291         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
292     }
293 }