2 * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser;
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;
31 public class SettingsFragment extends PreferenceFragment {
32 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
33 private SharedPreferences savedPreferences;
36 public void onCreate(Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
38 addPreferencesFromResource(R.xml.preferences);
40 // Initialize savedPreferences.
41 savedPreferences = getPreferenceScreen().getSharedPreferences();
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));
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));
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());
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);
66 // Display the user agent from the preference as the summary text.
67 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "Default user agent"));
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"));
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);
85 // Set the array value as the summary text.
86 javaScriptDisabledSearchPreference.setSummary(javaScriptDisabledSearchString);
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"));
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);
103 // Set the array value as the summary text.
104 javaScriptEnabledSearchPreference.setSummary(javaScriptEnabledSearchString);
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"));
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"));
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 + "%%");
123 // Listen for preference changes.
124 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
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) {
131 case "javascript_enabled":
132 // Set `javaScriptEnabled` to the new state. The default is `false`.
133 MainWebViewActivity.javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
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));
139 // Update `mainWebView`.
140 MainWebViewActivity.mainWebView.getSettings().setJavaScriptEnabled(MainWebViewActivity.javaScriptEnabled);
142 // Update the privacy icons.
143 MainWebViewActivity.updatePrivacyIcons(MainWebViewActivity.privacyBrowserActivity);
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);
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));
154 // Update `mainWebView`.
155 MainWebViewActivity.cookieManager.setAcceptCookie(MainWebViewActivity.firstPartyCookiesEnabled);
157 // Update the checkbox in the options menu.
158 MenuItem firstPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleFirstPartyCookies);
159 firstPartyCookiesMenuItem.setChecked(MainWebViewActivity.firstPartyCookiesEnabled);
161 // Update the privacy icons.
162 MainWebViewActivity.updatePrivacyIcons(MainWebViewActivity.privacyBrowserActivity);
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);
169 // Update the checkbox in the options menu.
170 MenuItem thirdPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleThirdPartyCookies);
171 thirdPartyCookiesMenuItem.setChecked(MainWebViewActivity.thirdPartyCookiesEnabled);
173 // Update `mainWebView` if API >= 21.
174 if (Build.VERSION.SDK_INT >= 21) {
175 MainWebViewActivity.cookieManager.setAcceptThirdPartyCookies(MainWebViewActivity.mainWebView, MainWebViewActivity.thirdPartyCookiesEnabled);
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);
183 // Update the checkbox in the options menu.
184 MenuItem domStorageMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleDomStorage);
185 domStorageMenuItem.setChecked(MainWebViewActivity.domStorageEnabled);
187 // Update `mainWebView`.
188 MainWebViewActivity.mainWebView.getSettings().setDomStorageEnabled(MainWebViewActivity.domStorageEnabled);
190 // Update the privacy icons.
191 MainWebViewActivity.updatePrivacyIcons(MainWebViewActivity.privacyBrowserActivity);
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);
198 // Update the checkbox in the options menu.
199 MenuItem saveFormDataMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleSaveFormData);
200 saveFormDataMenuItem.setChecked(MainWebViewActivity.saveFormDataEnabled);
202 // Update `mainWebView`.
203 MainWebViewActivity.mainWebView.getSettings().setSaveFormData(MainWebViewActivity.saveFormDataEnabled);
205 // Update the privacy icons.
206 MainWebViewActivity.updatePrivacyIcons(MainWebViewActivity.privacyBrowserActivity);
210 String userAgentString = sharedPreferences.getString("user_agent", "Default user agent");
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);
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);
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);
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"));
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"));
245 case "proxy_through_orbot":
246 // Get a handle for `settingsActivity`.
247 Activity settingsActivity = getActivity();
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");
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);
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);
268 // Update the javaScriptDisabledSearchURL variable. The default is "https://duckduckgo.com/html/?q=".
269 MainWebViewActivity.javaScriptDisabledSearchURL = newJavaScriptDisabledSearchString;
272 // Enable or disable javaScriptDisabledSearchCustomURLPreference.
273 javaScriptDisabledSearchCustomURLPreference.setEnabled(newJavaScriptDisabledSearchString.equals("Custom URL"));
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", ""));
280 // Update javaScriptDisabledSearchCustomURL. The default is "".
281 MainWebViewActivity.javaScriptDisabledSearchURL = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
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);
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);
296 // Update the javaScriptEnabledSearchURL variable. The default is "https://duckduckgo.com/?q=".
297 MainWebViewActivity.javaScriptEnabledSearchURL = newJavaScriptEnabledSearchString;
300 // Enable or disable javaScriptEnabledSearchCustomURLPreference.
301 javaScriptEnabledSearchCustomURLPreference.setEnabled(newJavaScriptEnabledSearchString.equals("Custom URL"));
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", ""));
308 // Update javaScriptEnabledSearchCustomURL. The default is ``.
309 MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
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");
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"));
324 // Update the homepage variable. The default is `https://www.duckduckgo.com`.
325 MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
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");
332 // Update the font size on `mainWebView`. The default is `100`.
333 MainWebViewActivity.mainWebView.getSettings().setTextZoom(Integer.valueOf(newDefaultFontSizeString));
335 // Update the summary text of `default_font_size`.
336 defaultFontSizePreference.setSummary(newDefaultFontSizeString + "%%");
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);
342 // Update `swipeRefreshLayout` to match the new state.
343 MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled);
347 // If no match, do nothing.
353 // Register the listener.
354 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
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.
360 public void onPause() {
362 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
366 public void onResume() {
368 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);