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.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;
30 public class SettingsFragment extends PreferenceFragment {
31 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
32 private SharedPreferences savedPreferences;
35 public void onCreate(Bundle savedInstanceState) {
36 super.onCreate(savedInstanceState);
37 addPreferencesFromResource(R.xml.preferences);
39 // Initialize savedPreferences.
40 savedPreferences = getPreferenceScreen().getSharedPreferences();
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());
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);
58 // Display the user agent from the preference as the summary text.
59 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "Default user agent"));
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"));
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);
77 // Set the array value as the summary text.
78 javaScriptDisabledSearchPreference.setSummary(javaScriptDisabledSearchString);
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"));
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);
95 // Set the array value as the summary text.
96 javaScriptEnabledSearchPreference.setSummary(javaScriptEnabledSearchString);
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"));
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"));
110 // Listen for preference changes.
111 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
113 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
114 @SuppressLint("SetJavaScriptEnabled")
115 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
117 // Several keys need to update the toggleJavaScript icon.
118 MenuItem toggleJavaScript = MainWebViewActivity.mainMenu.findItem(R.id.toggleJavaScript);
121 case "javascript_enabled":
122 // Set javaScriptEnabled to the new state. The default is false.
123 MainWebViewActivity.javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
125 // Update mainWebView and reload the website.
126 MainWebViewActivity.mainWebView.getSettings().setJavaScriptEnabled(MainWebViewActivity.javaScriptEnabled);
127 MainWebViewActivity.mainWebView.reload();
129 // Update the toggleJavaScript icon.
130 if (MainWebViewActivity.javaScriptEnabled) {
131 toggleJavaScript.setIcon(R.drawable.javascript_enabled);
133 if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
134 toggleJavaScript.setIcon(R.drawable.warning);
136 toggleJavaScript.setIcon(R.drawable.privacy_mode);
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);
145 // Update the checkbox in the options menu.
146 MenuItem firstPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleFirstPartyCookies);
147 firstPartyCookiesMenuItem.setChecked(MainWebViewActivity.firstPartyCookiesEnabled);
149 // Update mainWebView and reload the website.
150 MainWebViewActivity.cookieManager.setAcceptCookie(MainWebViewActivity.firstPartyCookiesEnabled);
151 MainWebViewActivity.mainWebView.reload();
153 // Update the toggleJavaScript icon.
154 if (MainWebViewActivity.javaScriptEnabled) {
155 toggleJavaScript.setIcon(R.drawable.javascript_enabled);
157 if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
158 toggleJavaScript.setIcon(R.drawable.warning);
160 toggleJavaScript.setIcon(R.drawable.privacy_mode);
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 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();
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);
184 // Update the checkbox in the options menu.
185 MenuItem domStorageMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleDomStorage);
186 domStorageMenuItem.setChecked(MainWebViewActivity.domStorageEnabled);
188 // Update mainWebView and reload the website.
189 MainWebViewActivity.mainWebView.getSettings().setDomStorageEnabled(MainWebViewActivity.domStorageEnabled);
190 MainWebViewActivity.mainWebView.reload();
192 // Update the toggleJavaScript icon.
193 if (MainWebViewActivity.javaScriptEnabled) {
194 toggleJavaScript.setIcon(R.drawable.javascript_enabled);
196 if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
197 toggleJavaScript.setIcon(R.drawable.warning);
199 toggleJavaScript.setIcon(R.drawable.privacy_mode);
205 String userAgentString = sharedPreferences.getString("user_agent", "Default user agent");
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);
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);
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);
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"));
236 // Update mainWebView's user agent. The default is "PrivacyBrowser/1.0".
237 MainWebViewActivity.mainWebView.getSettings().setUserAgentString(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
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);
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);
252 // Update the javaScriptDisabledSearchURL variable. The default is "https://duckduckgo.com/html/?q=".
253 MainWebViewActivity.javaScriptDisabledSearchURL = newJavaScriptDisabledSearchString;
256 // Enable or disable javaScriptDisabledSearchCustomURLPreference.
257 javaScriptDisabledSearchCustomURLPreference.setEnabled(newJavaScriptDisabledSearchString.equals("Custom URL"));
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", ""));
264 // Update javaScriptDisabledSearchCustomURL. The default is "".
265 MainWebViewActivity.javaScriptDisabledSearchURL = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
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);
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);
280 // Update the javaScriptEnabledSearchURL variable. The default is "https://duckduckgo.com/?q=".
281 MainWebViewActivity.javaScriptEnabledSearchURL = newJavaScriptEnabledSearchString;
284 // Enable or disable javaScriptEnabledSearchCustomURLPreference.
285 javaScriptEnabledSearchCustomURLPreference.setEnabled(newJavaScriptEnabledSearchString.equals("Custom URL"));
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", ""));
292 // Update javaScriptEnabledSearchCustomURL. The default is "".
293 MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
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"));
300 // Update the homepage variable. The default is "https://www.duckduckgo.com".
301 MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
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);
308 // Update swipeRefreshLayout to match the new state.
309 MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled);
313 // If no match, do nothing.
319 // Register the listener.
320 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
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.
326 public void onPause() {
328 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
332 public void onResume() {
334 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);