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.fragments;
22 import android.annotation.SuppressLint;
23 import android.content.SharedPreferences;
24 import android.os.Bundle;
25 import android.preference.Preference;
26 import android.preference.PreferenceFragment;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.webkit.WebView;
31 import com.stoutner.privacybrowser.R;
33 public class SettingsFragment extends PreferenceFragment {
34 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
35 private SharedPreferences savedPreferences;
38 public void onCreate(Bundle savedInstanceState) {
39 super.onCreate(savedInstanceState);
40 addPreferencesFromResource(R.xml.preferences);
42 // Initialize savedPreferences.
43 savedPreferences = getPreferenceScreen().getSharedPreferences();
45 // Allow the user to access "dom_storage_enabled" if "javascript_enabled" is enabled. The default is false.
46 final Preference domStorageEnabled = findPreference("dom_storage_enabled");
47 domStorageEnabled.setEnabled(savedPreferences.getBoolean("javascript_enabled", false));
49 // Allow the user to access "third_party_cookies_enabled" if "first_party_cookies_enabled" is enabled. The default is false.
50 final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
51 thirdPartyCookiesEnabled.setEnabled(savedPreferences.getBoolean("first_party_cookies_enabled", false));
54 // We need an inflated `WebView` to get the default user agent.
55 LayoutInflater inflater = getActivity().getLayoutInflater();
56 // `@SuppressLint("InflateParams")` removes the warning about using `null` as the `ViewGroup`, which in this case makes sense because we don't want to display `bare_webview` on the screen.
57 // `false` does not attach the view to the root.
58 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
59 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
61 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
62 final Preference userAgentPreference = findPreference("user_agent");
63 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
64 case "Default user agent":
65 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
66 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
69 case "Custom user agent":
70 // We can't use the string from the array because it is referenced in code and can't be translated.
71 userAgentPreference.setSummary(R.string.custom_user_agent);
75 // Display the user agent from the preference as the summary text.
76 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
80 // 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".
81 final Preference customUserAgent = findPreference("custom_user_agent");
82 customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
83 customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
86 // Set the JavaScript-disabled search URL as the summary text for the JavaScript-disabled search preference when the preference screen is loaded.
87 // The default is `https://duckduckgo.com/html/?q=`.
88 final Preference javaScriptDisabledSearchPreference = findPreference("javascript_disabled_search");
89 String javaScriptDisabledSearchString = savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
90 if (javaScriptDisabledSearchString.equals("Custom URL")) {
91 // If set to "Custom URL", use R.string.custom_url, which will be translated, instead of the array value, which will not.
92 javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
94 // Set the array value as the summary text.
95 javaScriptDisabledSearchPreference.setSummary(javaScriptDisabledSearchString);
98 // 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`.
99 final Preference javaScriptDisabledSearchCustomURLPreference = findPreference("javascript_disabled_search_custom_url");
100 javaScriptDisabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_disabled_search_custom_url", ""));
101 javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchString.equals("Custom URL"));
104 // Set the JavaScript-enabled searchURL as the summary text for the JavaScript-enabled search preference when the preference screen is loaded.
105 // The default is `https://duckduckgo.com/?q=`.
106 final Preference javaScriptEnabledSearchPreference = findPreference("javascript_enabled_search");
107 String javaScriptEnabledSearchString = savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
108 if (javaScriptEnabledSearchString.equals("Custom URL")) {
109 // If set to "Custom URL", use R.string.custom_url, which will be translated, instead of the array value, which will not.
110 javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
112 // Set the array value as the summary text.
113 javaScriptEnabledSearchPreference.setSummary(javaScriptEnabledSearchString);
116 // 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`.
117 final Preference javaScriptEnabledSearchCustomURLPreference = findPreference("javascript_enabled_search_custom_url");
118 javaScriptEnabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_enabled_search_custom_url", ""));
119 javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchString.equals("Custom URL"));
122 // Enable the full screen options if full screen browsing mode is enabled.
123 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
124 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
125 final boolean fullScreenBrowsingModeEnabled = savedPreferences.getBoolean("enable_full_screen_browsing_mode", false);
126 if (!fullScreenBrowsingModeEnabled) {
127 // Disable the full screen options.
128 hideSystemBarsPreference.setEnabled(false);
129 translucentNavigationBarPreference.setEnabled(false);
131 // Disable `transparent_navigation_bar` if `hide_system_bars` is `true`.
132 translucentNavigationBarPreference.setEnabled(!savedPreferences.getBoolean("hide_system_bars", false));
136 // 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`.
137 final Preference homepagePreference = findPreference("homepage");
138 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
140 // 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`.
141 final Preference defaultFontSizePreference = findPreference("default_font_size");
142 String defaultFontSizeString = savedPreferences.getString("default_font_size", "100");
143 defaultFontSizePreference.setSummary(defaultFontSizeString + "%%");
146 // Listen for preference changes.
147 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
149 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
150 @SuppressLint("SetJavaScriptEnabled")
151 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
154 case "javascript_enabled":
155 // Toggle the state of the `dom_storage_enabled` preference. The default is `false`.
156 final Preference domStorageEnabled = findPreference("dom_storage_enabled");
157 domStorageEnabled.setEnabled(sharedPreferences.getBoolean("javascript_enabled", false));
160 case "first_party_cookies_enabled":
161 // Toggle the state of the `third_party_cookies_enabled` preference. The default is `false`.
162 final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
163 thirdPartyCookiesEnabled.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false));
167 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
169 switch (userAgentString) {
170 case "Default user agent":
171 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
172 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
173 customUserAgent.setEnabled(false);
176 case "Custom user agent":
177 // Display "Custom user agent" as the summary text for userAgentPreference, and enable customUserAgent.
178 userAgentPreference.setSummary(R.string.custom_user_agent);
179 customUserAgent.setEnabled(true);
183 // Display the user agent as the summary text for userAgentPreference, and disable customUserAgent.
184 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
185 customUserAgent.setEnabled(false);
190 case "custom_user_agent":
191 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
192 customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
195 case "javascript_disabled_search":
196 String newJavaScriptDisabledSearchString = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
197 if (newJavaScriptDisabledSearchString.equals("Custom URL")) { // Set the summary text to `R.string.custom_url`, which is translated.
198 javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
199 } else { // Set the new search URL as the summary text for the JavaScript-disabled search preference.
200 javaScriptDisabledSearchPreference.setSummary(newJavaScriptDisabledSearchString);
203 // Enable or disable javaScriptDisabledSearchCustomURLPreference.
204 javaScriptDisabledSearchCustomURLPreference.setEnabled(newJavaScriptDisabledSearchString.equals("Custom URL"));
207 case "javascript_disabled_search_custom_url":
208 // Set the new custom search URL as the summary text for `javascript_disabled_search_custom_url`. The default is `""`.
209 javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", ""));
212 case "javascript_enabled_search":
213 String newJavaScriptEnabledSearchString = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
214 if (newJavaScriptEnabledSearchString.equals("Custom URL")) { // Set the summary text to `R.string.custom_url`, which is translated.
215 javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
216 } else { // Set the new search URL as the summary text for the JavaScript-enabled search preference..
217 javaScriptEnabledSearchPreference.setSummary(newJavaScriptEnabledSearchString);
220 // Enable or disable javaScriptEnabledSearchCustomURLPreference.
221 javaScriptEnabledSearchCustomURLPreference.setEnabled(newJavaScriptEnabledSearchString.equals("Custom URL"));
224 case "javascript_enabled_search_custom_url":
225 // Set the new custom search URL as the summary text for `javascript_enabled_search_custom_url`. The default is `""`.
226 javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", ""));
229 case "enable_full_screen_browsing_mode":
230 boolean newFullScreenBrowsingModeEnabled = sharedPreferences.getBoolean("enable_full_screen_browsing_mode", false);
231 if (newFullScreenBrowsingModeEnabled) {
232 // Enable `hideSystemBarsPreference`.
233 hideSystemBarsPreference.setEnabled(true);
235 // Only enable `transparent_navigation_bar` if `hide_system_bars` is `false`.
236 translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
238 // Disable the full screen options.
239 hideSystemBarsPreference.setEnabled(false);
240 translucentNavigationBarPreference.setEnabled(false);
245 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
246 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
249 case "default_font_size":
250 // Get the default font size as a string. The default is `100`.
251 String newDefaultFontSizeString = sharedPreferences.getString("default_font_size", "100");
253 // Update the summary text of `default_font_size`.
254 defaultFontSizePreference.setSummary(newDefaultFontSizeString + "%%");
257 case "hide_system_bars":
258 // Enable `translucentNavigationBarPreference` if `hide_system_bars` is disabled.
259 translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
263 // If no match, do nothing.
269 // Register the listener.
270 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
273 // 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
274 // even while running in the foreground.
276 public void onPause() {
278 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
282 public void onResume() {
284 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);