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 javaScriptEnabled = findPreference("javascript_enabled");
48 final Preference firstPartyCookiesEnabled = findPreference("first_party_cookies_enabled");
49 final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
50 final Preference domStorageEnabled = findPreference("dom_storage_enabled");
51 final Preference userAgentPreference = findPreference("user_agent");
52 final Preference customUserAgent = findPreference("custom_user_agent");
53 final Preference torHomepagePreference = findPreference("tor_homepage");
54 final Preference torSearchPreference = findPreference("tor_search");
55 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
56 final Preference searchPreference = findPreference("search");
57 final Preference searchCustomURLPreference = findPreference("search_custom_url");
58 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
59 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
60 final Preference homepagePreference = findPreference("homepage");
61 final Preference defaultFontSizePreference = findPreference("default_font_size");
64 domStorageEnabled.setDependency("javascript_enabled");
65 torHomepagePreference.setDependency("proxy_through_orbot");
66 torSearchPreference.setDependency("proxy_through_orbot");
67 hideSystemBarsPreference.setDependency("enable_full_screen_browsing_mode");
69 // Get strings from the preferences.
70 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
71 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
73 // Get booleans from the preferences.
74 boolean javaScriptEnabledBoolean = savedPreferences.getBoolean("javascript_enabled", false);
75 boolean firstPartyCookiesEnabledBoolean = savedPreferences.getBoolean("first_party_cookies_enabled", false);
76 boolean thirdPartyCookiesEnabledBoolean = savedPreferences.getBoolean("third_party_cookies_enabled", false);
78 // Only enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
79 thirdPartyCookiesEnabled.setEnabled(firstPartyCookiesEnabledBoolean && (Build.VERSION.SDK_INT >= 21));
81 // We need to inflated a `WebView` to get the default user agent.
82 LayoutInflater inflater = getActivity().getLayoutInflater();
83 // `@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.
84 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
85 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
87 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
88 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
89 case "WebView default user agent":
90 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
91 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
94 case "Custom user agent":
95 // We can't use the string from the array because it is referenced in code and can't be translated.
96 userAgentPreference.setSummary(R.string.custom_user_agent);
100 // Display the user agent from the preference as the summary text.
101 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
105 // 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".
106 customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
107 customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
110 // 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`.
111 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
114 // 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=`
115 if (torSearchString.equals("Custom URL")) {
116 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
117 torSearchPreference.setSummary(R.string.custom_url);
119 // Set the array value as the summary text.
120 torSearchPreference.setSummary(torSearchString);
123 // Set the summary text for `tor_search_custom_url`. The default is `""`.
124 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
126 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
127 torSearchCustomURLPreference.setEnabled(savedPreferences.getBoolean("proxy_through_orbot", false) && torSearchString.equals("Custom URL"));
130 // 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=`.
131 if (searchString.equals("Custom URL")) {
132 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
133 searchPreference.setSummary(R.string.custom_url);
135 // Set the array value as the summary text.
136 searchPreference.setSummary(searchString);
139 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
140 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
141 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
144 // Enable `transparent_navigation_bar` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
145 translucentNavigationBarPreference.setEnabled(savedPreferences.getBoolean("enable_full_screen_browsing_mode", false) && !savedPreferences.getBoolean("hide_system_bars", false));
148 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
149 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
151 // 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`.
152 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
155 // Set the `javascript_enabled` icon.
156 if (javaScriptEnabledBoolean) {
157 javaScriptEnabled.setIcon(R.drawable.javascript_enabled);
159 javaScriptEnabled.setIcon(R.drawable.privacy_mode);
162 // Set the `first_party_cookies_enabled` icon.
163 if (firstPartyCookiesEnabledBoolean) {
164 firstPartyCookiesEnabled.setIcon(R.drawable.cookies_enabled);
166 firstPartyCookiesEnabled.setIcon(R.drawable.cookies_disabled);
169 // Set the `third_party_cookies_enabled` icon.
170 if (firstPartyCookiesEnabledBoolean && Build.VERSION.SDK_INT >= 21) {
171 if (thirdPartyCookiesEnabledBoolean) {
172 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_warning);
174 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_disabled);
177 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_ghosted);
180 // Set the `dom_storage_enabled` icon.
181 if (javaScriptEnabledBoolean) {
182 if (savedPreferences.getBoolean("dom_storage_enabled", false)) {
183 domStorageEnabled.setIcon(R.drawable.dom_storage_enabled);
185 domStorageEnabled.setIcon(R.drawable.dom_storage_disabled);
188 domStorageEnabled.setIcon(R.drawable.dom_storage_ghosted);
191 // Listen for preference changes.
192 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
194 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
195 @SuppressLint("SetJavaScriptEnabled")
196 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
199 case "javascript_enabled":
201 if (sharedPreferences.getBoolean("javascript_enabled", false)) {
202 // Update the icon for `javascript_enabled`.
203 javaScriptEnabled.setIcon(R.drawable.javascript_enabled);
205 // Update the icon for `dom_storage_enabled`.
206 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
207 domStorageEnabled.setIcon(R.drawable.dom_storage_enabled);
209 domStorageEnabled.setIcon(R.drawable.dom_storage_disabled);
211 } else { // `javascript_enabled` is `false`.
212 // Update the icon for `javascript_enabled`.
213 javaScriptEnabled.setIcon(R.drawable.privacy_mode);
215 // Set the icon for `dom_storage_disabled` to be ghosted.
216 domStorageEnabled.setIcon(R.drawable.dom_storage_ghosted);
220 case "first_party_cookies_enabled":
221 // Update the icons for `first_party_cookies_enabled` and `third_party_cookies_enabled`.
222 if (sharedPreferences.getBoolean("first_party_cookies_enabled", false)) {
223 // Set the icon for `first_party_cookies_enabled`.
224 firstPartyCookiesEnabled.setIcon(R.drawable.cookies_enabled);
226 // Update the icon for `third_party_cookies_enabled`.
227 if (Build.VERSION.SDK_INT >= 21) {
228 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
229 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_warning);
231 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_disabled);
234 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_ghosted);
236 } else { // `first_party_cookies_enabled` is `false`.
237 // Update the icon for `first_party_cookies_enabled`.
238 firstPartyCookiesEnabled.setIcon(R.drawable.cookies_disabled);
240 // Set the icon for `third_party_cookies_enabled` to be ghosted.
241 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_ghosted);
244 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
245 thirdPartyCookiesEnabled.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
248 case "third_party_cookies_enabled":
250 if (sharedPreferences.getBoolean("third_party_cookies_enabled", false)) {
251 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_warning);
253 thirdPartyCookiesEnabled.setIcon(R.drawable.cookies_disabled);
257 case "dom_storage_enabled":
259 if (sharedPreferences.getBoolean("dom_storage_enabled", false)) {
260 domStorageEnabled.setIcon(R.drawable.dom_storage_enabled);
262 domStorageEnabled.setIcon(R.drawable.dom_storage_disabled);
267 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
269 switch (userAgentString) {
270 case "WebView default user agent":
271 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
272 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
273 customUserAgent.setEnabled(false);
276 case "Custom user agent":
277 // Display `Custom user agent` as the summary text for `userAgentPreference`, and enable `customUserAgent`.
278 userAgentPreference.setSummary(R.string.custom_user_agent);
279 customUserAgent.setEnabled(true);
283 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
284 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
285 customUserAgent.setEnabled(false);
290 case "custom_user_agent":
291 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
292 customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
295 case "proxy_through_orbot":
296 // Get current settings.
297 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
298 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
300 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
301 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
305 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
306 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
310 // Get the present search string.
311 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
313 // Set the summary text for `tor_search`.
314 if (presentTorSearchString.equals("Custom URL")) {
315 // Use R.string.custom_url, which is translated, instead of the array value, which isn't.
316 torSearchPreference.setSummary(R.string.custom_url);
318 // Set the array value as the summary text.
319 torSearchPreference.setSummary(presentTorSearchString);
322 // Set the status of `torJavaScriptDisabledSearchCustomURLPreference`.
323 torSearchCustomURLPreference.setEnabled(presentTorSearchString.equals("Custom URL"));
326 case "tor_search_custom_url":
327 // Set the summary text for `tor_search_custom_url`.
328 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
332 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
333 if (newSearchString.equals("Custom URL")) { // Set the summary text to `R.string.custom_url`, which is translated.
334 searchPreference.setSummary(R.string.custom_url);
335 } else { // Set the new search URL as the summary text for the JavaScript-disabled search preference.
336 searchPreference.setSummary(newSearchString);
339 // Enable or disable `searchCustomURLPreference`.
340 searchCustomURLPreference.setEnabled(newSearchString.equals("Custom URL"));
343 case "search_custom_url":
344 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
345 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
348 case "enable_full_screen_browsing_mode":
349 // Enable `transparent_navigation_bar` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
350 translucentNavigationBarPreference.setEnabled(sharedPreferences.getBoolean("enable_full_screen_browsing_mode", false) && !sharedPreferences.getBoolean("hide_system_bars", false));
353 case "hide_system_bars":
354 // Enable `translucentNavigationBarPreference` if `hide_system_bars` is disabled.
355 translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
359 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
360 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
363 case "default_font_size":
364 // Update the summary text of `default_font_size`.
365 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
369 // If no match, do nothing.
375 // Register the listener.
376 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
379 // 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
380 // even while running in the foreground.
382 public void onPause() {
384 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
388 public void onResume() {
390 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);