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 JavaScript-disabled search URL as the summary text for the JavaScript-disabled search preference when the preference screen is loaded.
44 // The default is "https://duckduckgo.com/html/?q=".
45 final Preference javaScriptDisabledSearchPreference = findPreference("javascript_disabled_search");
46 javaScriptDisabledSearchPreference.setSummary(savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="));
48 // 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".
49 final Preference javaScriptDisabledSearchCustomURLPreference = findPreference("javascript_disabled_search_custom_url");
50 javaScriptDisabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_disabled_search_custom_url", ""));
51 javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchPreference.getSummary().equals("Custom URL"));
53 // Set the JavaScript-enabed searchURL as the summary text for the JavaScript-enabled search preference when the preference screen is loaded.
54 // The default is "https://duckduckgo.com/?q=".
55 final Preference javaScriptEnabledSearchPreference = findPreference("javascript_enabled_search");
56 javaScriptEnabledSearchPreference.setSummary(savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="));
58 // 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".
59 final Preference javaScriptEnabledSearchCustomURLPreference = findPreference("javascript_enabled_search_custom_url");
60 javaScriptEnabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_enabled_search_custom_url", ""));
61 javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchPreference.getSummary().equals("Custom URL"));
63 // 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".
64 final Preference homepagePreference = findPreference("homepage");
65 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
68 // Listen for preference changes.
69 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
71 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
72 @SuppressLint("SetJavaScriptEnabled")
73 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
75 // Several keys need to update the toggleJavaScript icon.
76 MenuItem toggleJavaScript = MainWebViewActivity.mainMenu.findItem(R.id.toggleJavaScript);
79 case "javascript_enabled":
80 // Set javaScriptEnabled to the new state. The default is false.
81 MainWebViewActivity.javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
83 // Update mainWebView and reload the website.
84 MainWebViewActivity.mainWebView.getSettings().setJavaScriptEnabled(MainWebViewActivity.javaScriptEnabled);
85 MainWebViewActivity.mainWebView.reload();
87 // Update the toggleJavaScript icon.
88 if (MainWebViewActivity.javaScriptEnabled) {
89 toggleJavaScript.setIcon(R.drawable.javascript_enabled);
91 if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
92 toggleJavaScript.setIcon(R.drawable.warning);
94 toggleJavaScript.setIcon(R.drawable.privacy_mode);
99 case "first_party_cookies_enabled":
100 // Set firstPartyCookiesEnabled to the new state. The default is false.
101 MainWebViewActivity.firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false);
103 // Update the checkbox in the options menu.
104 MenuItem firstPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleFirstPartyCookies);
105 firstPartyCookiesMenuItem.setChecked(MainWebViewActivity.firstPartyCookiesEnabled);
107 // Update mainWebView and reload the website.
108 MainWebViewActivity.cookieManager.setAcceptCookie(MainWebViewActivity.firstPartyCookiesEnabled);
109 MainWebViewActivity.mainWebView.reload();
111 // Update the toggleJavaScript icon.
112 if (MainWebViewActivity.javaScriptEnabled) {
113 toggleJavaScript.setIcon(R.drawable.javascript_enabled);
115 if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
116 toggleJavaScript.setIcon(R.drawable.warning);
118 toggleJavaScript.setIcon(R.drawable.privacy_mode);
123 case "third_party_cookies_enabled":
124 // Set thirdPartyCookiesEnabled to the new state. The default is false.
125 MainWebViewActivity.thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false);
127 // Update the checkbox in the options menu.
128 MenuItem thirdPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleThirdPartyCookies);
129 thirdPartyCookiesMenuItem.setChecked(MainWebViewActivity.thirdPartyCookiesEnabled);
131 // Update mainWebView and reload the website if API >= 21.
132 if (Build.VERSION.SDK_INT >= 21) {
133 MainWebViewActivity.cookieManager.setAcceptThirdPartyCookies(MainWebViewActivity.mainWebView, MainWebViewActivity.thirdPartyCookiesEnabled);
134 MainWebViewActivity.mainWebView.reload();
138 case "dom_storage_enabled":
139 // Set domStorageEnabled to the new state. The default is false.
140 MainWebViewActivity.domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false);
142 // Update the checkbox in the options menu.
143 MenuItem domStorageMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleDomStorage);
144 domStorageMenuItem.setChecked(MainWebViewActivity.domStorageEnabled);
146 // Update mainWebView and reload the website.
147 MainWebViewActivity.mainWebView.getSettings().setDomStorageEnabled(MainWebViewActivity.domStorageEnabled);
148 MainWebViewActivity.mainWebView.reload();
150 // Update the toggleJavaScript icon.
151 if (MainWebViewActivity.javaScriptEnabled) {
152 toggleJavaScript.setIcon(R.drawable.javascript_enabled);
154 if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
155 toggleJavaScript.setIcon(R.drawable.warning);
157 toggleJavaScript.setIcon(R.drawable.privacy_mode);
162 case "javascript_disabled_search":
163 // Set the new search URL as the summary text for the JavaScript-disabled search preference. The default is "https://duckduckgo.com/html/?q=".
164 javaScriptDisabledSearchPreference.setSummary(sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="));
166 // Enable "javascript_disabled_search_custom_url" if "javascript_disabled_search" is set to "Custom URL".
167 javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchPreference.getSummary().equals("Custom URL"));
169 // Update the javaScriptDisabledSearchURL variable. The default is "https://duckduckgo.com/html/?q=".
170 MainWebViewActivity.javaScriptDisabledSearchURL = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
173 case "javascript_disabled_search_custom_url":
174 // Set the new custom search URL as the summary text for "javascript_disabled_search_custom_url". The default is "".
175 javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", ""));
177 // Update javaScriptDisabledSearchCustomURL. The default is "".
178 MainWebViewActivity.javaScriptDisabledSearchCustomURL = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
180 case "javascript_enabled_search":
181 // Set the new search URL as the summary text for the JavaScript-enabled search preference. The default is "https://duckduckgo.com/?q=".
182 javaScriptEnabledSearchPreference.setSummary(sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="));
184 // Enable "javascript_enabled_search_custom_url" if "javascript_enabled_search" is set to "Custom URL".
185 javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchPreference.getSummary().equals("Custom URL"));
187 // Update the javaScriptEnabledSearchURL variable. The default is "https://duckduckgo.com/?q=".
188 MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
191 case "javascript_enabled_search_custom_url":
192 // Set the new custom search URL as the summary text for "javascript_enabled_search_custom_url". The default is "".
193 javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", ""));
195 // Update javaScriptEnabledSearchCustomURL. The default is "".
196 MainWebViewActivity.javaScriptEnabledSearchCustomURL = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
199 // Set the new homepage URL as the summary text for the Homepage preference. The default is "https://www.duckduckgo.com".
200 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
202 // Update the homepage variable. The default is "https://www.duckduckgo.com".
203 MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
206 case "swipe_to_refresh_enabled":
207 // Set swipeToRefreshEnabled to the new state. The default is true.
208 MainWebViewActivity.swipeToRefreshEnabled = sharedPreferences.getBoolean("swipe_to_refresh_enabled", true);
210 // Update swipeRefreshLayout to match the new state.
211 MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled);
214 // If no match, do nothing.
220 // Register the listener.
221 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
224 // 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
225 // even while running in the foreground.
227 public void onResume() {
229 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
233 public void onPause() {
235 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);