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