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