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.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 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));
52 // We need an inflated `WebView` to get the default user agent.
53 LayoutInflater inflater = getActivity().getLayoutInflater();
54 // `@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.
55 // `false` does not attach the view to the root.
56 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
57 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
59 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
60 final Preference userAgentPreference = findPreference("user_agent");
61 switch (savedPreferences.getString("user_agent", "Default user agent")) {
62 case "Default user agent":
63 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
64 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
67 case "Custom user agent":
68 // We can't use the string from the array because it is referenced in code and can't be translated.
69 userAgentPreference.setSummary(R.string.custom_user_agent);
73 // Display the user agent from the preference as the summary text.
74 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "Default user agent"));
78 // 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".
79 final Preference customUserAgent = findPreference("custom_user_agent");
80 customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
81 customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
84 // Set the JavaScript-disabled search URL as the summary text for the JavaScript-disabled search preference when the preference screen is loaded.
85 // The default is "https://duckduckgo.com/html/?q=".
86 final Preference javaScriptDisabledSearchPreference = findPreference("javascript_disabled_search");
87 String javaScriptDisabledSearchString = savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
88 if (javaScriptDisabledSearchString.equals("Custom URL")) {
89 // If set to "Custom URL", use R.string.custom_url, which will be translated, instead of the array value, which will not.
90 javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
92 // Set the array value as the summary text.
93 javaScriptDisabledSearchPreference.setSummary(javaScriptDisabledSearchString);
96 // 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".
97 final Preference javaScriptDisabledSearchCustomURLPreference = findPreference("javascript_disabled_search_custom_url");
98 javaScriptDisabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_disabled_search_custom_url", ""));
99 javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchString.equals("Custom URL"));
102 // Set the JavaScript-enabled searchURL as the summary text for the JavaScript-enabled search preference when the preference screen is loaded.
103 // The default is "https://duckduckgo.com/?q=".
104 final Preference javaScriptEnabledSearchPreference = findPreference("javascript_enabled_search");
105 String javaScriptEnabledSearchString = savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
106 if (javaScriptEnabledSearchString.equals("Custom URL")) {
107 // If set to "Custom URL", use R.string.custom_url, which will be translated, instead of the array value, which will not.
108 javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
110 // Set the array value as the summary text.
111 javaScriptEnabledSearchPreference.setSummary(javaScriptEnabledSearchString);
114 // 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".
115 final Preference javaScriptEnabledSearchCustomURLPreference = findPreference("javascript_enabled_search_custom_url");
116 javaScriptEnabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_enabled_search_custom_url", ""));
117 javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchString.equals("Custom URL"));
120 // 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`.
121 final Preference homepagePreference = findPreference("homepage");
122 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
124 // 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`.
125 final Preference defaultFontSizePreference = findPreference("default_font_size");
126 String defaultFontSizeString = savedPreferences.getString("default_font_size", "100");
127 defaultFontSizePreference.setSummary(defaultFontSizeString + "%%");
130 // Listen for preference changes.
131 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
133 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
134 @SuppressLint("SetJavaScriptEnabled")
135 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
138 case "javascript_enabled":
139 // Toggle the state of the `dom_storage_enabled` preference. The default is `false`.
140 final Preference domStorageEnabled = findPreference("dom_storage_enabled");
141 domStorageEnabled.setEnabled(sharedPreferences.getBoolean("javascript_enabled", false));
144 case "first_party_cookies_enabled":
145 // Toggle the state of the `third_party_cookies_enabled` preference. The default is `false`.
146 final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
147 thirdPartyCookiesEnabled.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false));
151 String userAgentString = sharedPreferences.getString("user_agent", "Default user agent");
153 switch (userAgentString) {
154 case "Default user agent":
155 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
156 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
157 customUserAgent.setEnabled(false);
160 case "Custom user agent":
161 // Display "Custom user agent" as the summary text for userAgentPreference, and enable customUserAgent.
162 userAgentPreference.setSummary(R.string.custom_user_agent);
163 customUserAgent.setEnabled(true);
167 // Display the user agent as the summary text for userAgentPreference, and disable customUserAgent.
168 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "Default user agent"));
169 customUserAgent.setEnabled(false);
174 case "custom_user_agent":
175 // Set the new custom user agent as the summary text for "custom_user_agent". The default is "PrivacyBrowser/1.0".
176 customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
179 case "javascript_disabled_search":
180 String newJavaScriptDisabledSearchString = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
181 if (newJavaScriptDisabledSearchString.equals("Custom URL")) { // Set the summary text to `R.string.custom_url`, which is translated.
182 javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
183 } else { // Set the new search URL as the summary text for the JavaScript-disabled search preference.
184 javaScriptDisabledSearchPreference.setSummary(newJavaScriptDisabledSearchString);
187 // Enable or disable javaScriptDisabledSearchCustomURLPreference.
188 javaScriptDisabledSearchCustomURLPreference.setEnabled(newJavaScriptDisabledSearchString.equals("Custom URL"));
191 case "javascript_disabled_search_custom_url":
192 // Set the new custom search URL as the summary text for `javascript_disabled_search_custom_url`. The default is `""`.
193 javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", ""));
196 case "javascript_enabled_search":
197 String newJavaScriptEnabledSearchString = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
198 if (newJavaScriptEnabledSearchString.equals("Custom URL")) { // Set the summary text to `R.string.custom_url`, which is translated.
199 javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
200 } else { // Set the new search URL as the summary text for the JavaScript-enabled search preference..
201 javaScriptEnabledSearchPreference.setSummary(newJavaScriptEnabledSearchString);
204 // Enable or disable javaScriptEnabledSearchCustomURLPreference.
205 javaScriptEnabledSearchCustomURLPreference.setEnabled(newJavaScriptEnabledSearchString.equals("Custom URL"));
208 case "javascript_enabled_search_custom_url":
209 // Set the new custom search URL as the summary text for `javascript_enabled_search_custom_url`. The default is `""`.
210 javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", ""));
214 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
215 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
218 case "default_font_size":
219 // Get the default font size as a string. The default is `100`.
220 String newDefaultFontSizeString = sharedPreferences.getString("default_font_size", "100");
222 // Update the summary text of `default_font_size`.
223 defaultFontSizePreference.setSummary(newDefaultFontSizeString + "%%");
226 // If no match, do nothing.
232 // Register the listener.
233 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
236 // 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
237 // even while running in the foreground.
239 public void onPause() {
241 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
245 public void onResume() {
247 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);