2 * Copyright 2016-2017 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.Build;
25 import android.os.Bundle;
26 import android.preference.Preference;
27 import android.preference.PreferenceFragment;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.webkit.WebView;
32 import com.stoutner.privacybrowser.R;
34 public class SettingsFragment extends PreferenceFragment {
35 private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
36 private SharedPreferences savedPreferences;
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 addPreferencesFromResource(R.xml.preferences);
43 // Initialize savedPreferences.
44 savedPreferences = getPreferenceScreen().getSharedPreferences();
46 // Get handles for the preferences we need to modify.
47 final Preference javaScriptPreference = findPreference("javascript_enabled");
48 final Preference firstPartyCookiesPreference = findPreference("first_party_cookies_enabled");
49 final Preference thirdPartyCookiesPreference = findPreference("third_party_cookies_enabled");
50 final Preference domStoragePreference = findPreference("dom_storage_enabled");
51 final Preference saveFormDataPreference = findPreference("save_form_data_enabled");
52 final Preference userAgentPreference = findPreference("user_agent");
53 final Preference customUserAgentPreference = findPreference("custom_user_agent");
54 final Preference doNotTrackPreference = findPreference("do_not_track");
55 final Preference torHomepagePreference = findPreference("tor_homepage");
56 final Preference torSearchPreference = findPreference("tor_search");
57 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
58 final Preference searchPreference = findPreference("search");
59 final Preference searchCustomURLPreference = findPreference("search_custom_url");
60 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
61 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
62 final Preference homepagePreference = findPreference("homepage");
63 final Preference defaultFontSizePreference = findPreference("default_font_size");
66 domStoragePreference.setDependency("javascript_enabled");
67 torHomepagePreference.setDependency("proxy_through_orbot");
68 torSearchPreference.setDependency("proxy_through_orbot");
69 hideSystemBarsPreference.setDependency("enable_full_screen_browsing_mode");
71 // Get strings from the preferences.
72 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
73 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
75 // Get booleans from the preferences.
76 boolean javaScriptEnabledBoolean = savedPreferences.getBoolean("javascript_enabled", false);
77 boolean firstPartyCookiesEnabledBoolean = savedPreferences.getBoolean("first_party_cookies_enabled", false);
78 boolean thirdPartyCookiesEnabledBoolean = savedPreferences.getBoolean("third_party_cookies_enabled", false);
80 // Only enable `thirdPartyCookiesPreference` if `firstPartyCookiesEnabledBoolean` is `true` and API >= 21.
81 thirdPartyCookiesPreference.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
83 // We need to inflated a `WebView` to get the default user agent.
84 LayoutInflater inflater = getActivity().getLayoutInflater();
85 // `@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. `false` does not attach the view to the root.
86 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
87 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
89 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
90 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
91 case "WebView default user agent":
92 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
93 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
96 case "Custom user agent":
97 // We can't use the string from the array because it is referenced in code and can't be translated.
98 userAgentPreference.setSummary(R.string.custom_user_agent);
102 // Display the user agent from the preference as the summary text.
103 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
107 // Set the summary text for "customUserAgentPreference" (the default is `PrivacyBrowser/1.0`) and enable it if `userAgentPreference` it set to `Custom user agent`.
108 customUserAgentPreference.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
109 customUserAgentPreference.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
112 // Set the Tor homepage URL as the summary text for the `tor_homepage` preference when the preference screen is loaded. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
113 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
116 // Set the Tor search URL as the summary text for the Tor preference when the preference screen is loaded. The default is `https://3g2upl4pq6kufc4m.onion/html/?q=`
117 if (torSearchString.equals("Custom URL")) {
118 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
119 torSearchPreference.setSummary(R.string.custom_url);
121 // Set the array value as the summary text.
122 torSearchPreference.setSummary(torSearchString);
125 // Set the summary text for `tor_search_custom_url`. The default is `""`.
126 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
128 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
129 torSearchCustomURLPreference.setEnabled(savedPreferences.getBoolean("proxy_through_orbot", false) && torSearchString.equals("Custom URL"));
132 // Set the search URL as the summary text for the search preference when the preference screen is loaded. The default is `https://duckduckgo.com/html/?q=`.
133 if (searchString.equals("Custom URL")) {
134 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
135 searchPreference.setSummary(R.string.custom_url);
137 // Set the array value as the summary text.
138 searchPreference.setSummary(searchString);
141 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
142 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
143 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
146 // Enable `transparent_navigation_bar` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
147 translucentNavigationBarPreference.setEnabled(savedPreferences.getBoolean("enable_full_screen_browsing_mode", false) && !savedPreferences.getBoolean("hide_system_bars", false));
150 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
151 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
153 // 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`.
154 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
157 // Set the `javaScriptPreference` icon.
158 if (javaScriptEnabledBoolean) {
159 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
161 javaScriptPreference.setIcon(R.drawable.privacy_mode);
164 // Set the `firstPartyCookiesPreference` icon.
165 if (firstPartyCookiesEnabledBoolean) {
166 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
168 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
171 // Set the `thirdPartyCookiesPreference` icon.
172 if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
173 if (thirdPartyCookiesEnabledBoolean) {
174 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
176 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
179 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
182 // Set the `domStoragePreference` icon.
183 if (javaScriptEnabledBoolean) {
184 if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
185 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
187 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
190 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
193 // Set the `saveFormDataPreference` icon.
194 if (savedPreferences.getBoolean("save_form_data_enabled", false)) {
195 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
197 saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
200 // Set the `customUserAgentPreference` icon.
201 if (customUserAgentPreference.isEnabled()) {
202 customUserAgentPreference.setIcon(R.drawable.user_agent_enabled);
204 customUserAgentPreference.setIcon(R.drawable.user_agent_ghosted);
207 // Set the `doNotTrackPreference` icon.
208 if (savedPreferences.getBoolean("do_not_track", false)) {
209 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
211 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
214 // Listen for preference changes.
215 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
217 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
218 @SuppressLint("SetJavaScriptEnabled")
219 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
222 case "javascript_enabled":
224 if (sharedPreferences.getBoolean("javascript_enabled", false)) {
225 // Update the icon for `javascript_enabled`.
226 javaScriptPreference.setIcon(R.drawable.javascript_enabled);
228 // Update the icon for `dom_storage_enabled`.
229 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
230 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
232 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
234 } else { // `javascript_enabled` is `false`.
235 // Update the icon for `javascript_enabled`.
236 javaScriptPreference.setIcon(R.drawable.privacy_mode);
238 // Set the icon for `dom_storage_disabled` to be ghosted.
239 domStoragePreference.setIcon(R.drawable.dom_storage_ghosted);
243 case "first_party_cookies_enabled":
244 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
245 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
246 // Set the icon for `first_party_cookies_enabled`.
247 firstPartyCookiesPreference.setIcon(R.drawable.cookies_enabled);
249 // Update the icon for `third_party_cookies_enabled`.
250 if (Build.VERSION.SDK_INT >= 21) {
251 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
252 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
254 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
257 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
259 } else { // `first_party_cookies_enabled` is `false`.
260 // Update the icon for `first_party_cookies_enabled`.
261 firstPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
263 // Set the icon for `third_party_cookies_enabled` to be ghosted.
264 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_ghosted);
267 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
268 thirdPartyCookiesPreference.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
271 case "third_party_cookies_enabled":
273 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
274 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_warning);
276 thirdPartyCookiesPreference.setIcon(R.drawable.cookies_disabled);
280 case "dom_storage_enabled":
282 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
283 domStoragePreference.setIcon(R.drawable.dom_storage_enabled);
285 domStoragePreference.setIcon(R.drawable.dom_storage_disabled);
289 case "save_form_data_enabled":
291 if (sharedPreferences.getBoolean("save_form_data_enabled", false)) {
292 saveFormDataPreference.setIcon(R.drawable.form_data_enabled);
294 saveFormDataPreference.setIcon(R.drawable.form_data_disabled);
298 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
300 switch (userAgentString) {
301 case "WebView default user agent":
302 // Display the user agent as the summary text for `userAgentPreference`.
303 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
305 // Update `customUserAgentPreference`.
306 customUserAgentPreference.setEnabled(false);
307 customUserAgentPreference.setIcon(R.drawable.user_agent_ghosted);
310 case "Custom user agent":
311 // Display `Custom user agent` as the summary text for `userAgentPreference`.
312 userAgentPreference.setSummary(R.string.custom_user_agent);
314 // Update `customUserAgentPreference`.
315 customUserAgentPreference.setEnabled(true);
316 customUserAgentPreference.setIcon(R.drawable.user_agent_enabled);
320 // Display the user agent as the summary text for `userAgentPreference`.
321 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
323 // Update `customUserAgentPreference`.
324 customUserAgentPreference.setEnabled(false);
325 customUserAgentPreference.setIcon(R.drawable.user_agent_ghosted);
330 case "custom_user_agent":
331 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
332 customUserAgentPreference.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
337 if (sharedPreferences.getBoolean("do_not_track", false)) {
338 doNotTrackPreference.setIcon(R.drawable.do_not_track_enabled);
340 doNotTrackPreference.setIcon(R.drawable.do_not_track_disabled);
345 case "proxy_through_orbot":
346 // Get current settings.
347 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
348 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
350 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
351 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
355 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
356 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
360 // Get the present search string.
361 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
363 // Set the summary text for `tor_search`.
364 if (presentTorSearchString.equals("Custom URL")) {
365 // Use R.string.custom_url, which is translated, instead of the array value, which isn't.
366 torSearchPreference.setSummary(R.string.custom_url);
368 // Set the array value as the summary text.
369 torSearchPreference.setSummary(presentTorSearchString);
372 // Set the status of `torJavaScriptDisabledSearchCustomURLPreference`.
373 torSearchCustomURLPreference.setEnabled(presentTorSearchString.equals("Custom URL"));
376 case "tor_search_custom_url":
377 // Set the summary text for `tor_search_custom_url`.
378 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
382 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
383 if (newSearchString.equals("Custom URL")) { // Set the summary text to `R.string.custom_url`, which is translated.
384 searchPreference.setSummary(R.string.custom_url);
385 } else { // Set the new search URL as the summary text for the JavaScript-disabled search preference.
386 searchPreference.setSummary(newSearchString);
389 // Enable or disable `searchCustomURLPreference`.
390 searchCustomURLPreference.setEnabled(newSearchString.equals("Custom URL"));
393 case "search_custom_url":
394 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
395 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
398 case "enable_full_screen_browsing_mode":
399 // Enable `transparent_navigation_bar` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
400 translucentNavigationBarPreference.setEnabled(sharedPreferences.getBoolean("enable_full_screen_browsing_mode", false) && !sharedPreferences.getBoolean("hide_system_bars", false));
403 case "hide_system_bars":
404 // Enable `translucentNavigationBarPreference` if `hide_system_bars` is disabled.
405 translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
409 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
410 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
413 case "default_font_size":
414 // Update the summary text of `default_font_size`.
415 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
419 // If no match, do nothing.
425 // Register the listener.
426 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
429 // 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
430 // even while running in the foreground.
432 public void onPause() {
434 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
438 public void onResume() {
440 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);