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 domStorageEnabled = findPreference("dom_storage_enabled");
48 final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
49 final Preference userAgentPreference = findPreference("user_agent");
50 final Preference customUserAgent = findPreference("custom_user_agent");
51 final Preference torHomepagePreference = findPreference("tor_homepage");
52 final Preference torSearchPreference = findPreference("tor_search");
53 final Preference torSearchCustomURLPreference = findPreference("tor_search_custom_url");
54 final Preference searchPreference = findPreference("search");
55 final Preference searchCustomURLPreference = findPreference("search_custom_url");
56 final Preference hideSystemBarsPreference = findPreference("hide_system_bars");
57 final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
58 final Preference homepagePreference = findPreference("homepage");
59 final Preference defaultFontSizePreference = findPreference("default_font_size");
62 domStorageEnabled.setDependency("javascript_enabled");
63 torHomepagePreference.setDependency("proxy_through_orbot");
64 torSearchPreference.setDependency("proxy_through_orbot");
65 hideSystemBarsPreference.setDependency("enable_full_screen_browsing_mode");
67 // Get strings from the preferences.
68 String torSearchString = savedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
69 String searchString = savedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
71 // Only enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
72 thirdPartyCookiesEnabled.setEnabled(savedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
74 // We need to inflated a `WebView` to get the default user agent.
75 LayoutInflater inflater = getActivity().getLayoutInflater();
76 // `@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.
77 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
78 final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
80 // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
81 switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
82 case "WebView default user agent":
83 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
84 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
87 case "Custom user agent":
88 // We can't use the string from the array because it is referenced in code and can't be translated.
89 userAgentPreference.setSummary(R.string.custom_user_agent);
93 // Display the user agent from the preference as the summary text.
94 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
98 // 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".
99 customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
100 customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
103 // 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`.
104 torHomepagePreference.setSummary(savedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
107 // 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=`
108 if (torSearchString.equals("Custom URL")) {
109 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
110 torSearchPreference.setSummary(R.string.custom_url);
112 // Set the array value as the summary text.
113 torSearchPreference.setSummary(torSearchString);
116 // Set the summary text for `tor_search_custom_url`. The default is `""`.
117 torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
119 // Enable the Tor custom URL search options only if proxying through Orbot and the search is set to `Custom URL`.
120 torSearchCustomURLPreference.setEnabled(savedPreferences.getBoolean("proxy_through_orbot", false) && torSearchString.equals("Custom URL"));
123 // 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=`.
124 if (searchString.equals("Custom URL")) {
125 // Use R.string.custom_url, which will be translated, instead of the array value, which will not.
126 searchPreference.setSummary(R.string.custom_url);
128 // Set the array value as the summary text.
129 searchPreference.setSummary(searchString);
132 // Set the summary text for `search_custom_url` (the default is `""`) and enable it if `search` is set to `Custom URL`.
133 searchCustomURLPreference.setSummary(savedPreferences.getString("search_custom_url", ""));
134 searchCustomURLPreference.setEnabled(searchString.equals("Custom URL"));
137 // Enable `transparent_navigation_bar` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
138 translucentNavigationBarPreference.setEnabled(savedPreferences.getBoolean("enable_full_screen_browsing_mode", false) && !savedPreferences.getBoolean("hide_system_bars", false));
141 // Set the homepage URL as the summary text for the `Homepage` preference when the preference screen is loaded. The default is `https://duckduckgo.com`.
142 homepagePreference.setSummary(savedPreferences.getString("homepage", "https://duckduckgo.com"));
144 // 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`.
145 defaultFontSizePreference.setSummary(savedPreferences.getString("default_font_size", "100") + "%%");
148 // Listen for preference changes.
149 preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
151 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. We know.
152 @SuppressLint("SetJavaScriptEnabled")
153 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
156 case "first_party_cookies_enabled":
157 // Enable `third_party_cookies_enabled` if `first_party_cookies_enabled` is `true` and API >= 21.
158 thirdPartyCookiesEnabled.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false) && (Build.VERSION.SDK_INT >= 21));
162 String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
164 switch (userAgentString) {
165 case "WebView default user agent":
166 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
167 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
168 customUserAgent.setEnabled(false);
171 case "Custom user agent":
172 // Display `Custom user agent` as the summary text for `userAgentPreference`, and enable `customUserAgent`.
173 userAgentPreference.setSummary(R.string.custom_user_agent);
174 customUserAgent.setEnabled(true);
178 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
179 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
180 customUserAgent.setEnabled(false);
185 case "custom_user_agent":
186 // Set the new custom user agent as the summary text for `custom_user_agent`. The default is `PrivacyBrowser/1.0`.
187 customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
190 case "proxy_through_orbot":
191 // Get current settings.
192 boolean currentProxyThroughOrbot = sharedPreferences.getBoolean("proxy_through_orbot", false);
193 String currentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
195 // Enable the Tor custom URL search option only if `currentProxyThroughOrbot` is true and the search is set to `Custom URL`.
196 torSearchCustomURLPreference.setEnabled(currentProxyThroughOrbot && currentTorSearchString.equals("Custom URL"));
200 // Set the new tor homepage URL as the summary text for the `tor_homepage` preference. The default is DuckDuckGo: `https://3g2upl4pq6kufc4m.onion`.
201 torHomepagePreference.setSummary(sharedPreferences.getString("tor_homepage", "https://3g2upl4pq6kufc4m.onion"));
205 // Get the present search string.
206 String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
208 // Set the summary text for `tor_search`.
209 if (presentTorSearchString.equals("Custom URL")) {
210 // Use R.string.custom_url, which is translated, instead of the array value, which isn't.
211 torSearchPreference.setSummary(R.string.custom_url);
213 // Set the array value as the summary text.
214 torSearchPreference.setSummary(presentTorSearchString);
217 // Set the status of `torJavaScriptDisabledSearchCustomURLPreference`.
218 torSearchCustomURLPreference.setEnabled(presentTorSearchString.equals("Custom URL"));
221 case "tor_search_custom_url":
222 // Set the summary text for `tor_search_custom_url`.
223 torSearchCustomURLPreference.setSummary(sharedPreferences.getString("tor_search_custom_url", ""));
227 String newSearchString = sharedPreferences.getString("search", "https://duckduckgo.com/html/?q=");
228 if (newSearchString.equals("Custom URL")) { // Set the summary text to `R.string.custom_url`, which is translated.
229 searchPreference.setSummary(R.string.custom_url);
230 } else { // Set the new search URL as the summary text for the JavaScript-disabled search preference.
231 searchPreference.setSummary(newSearchString);
234 // Enable or disable `searchCustomURLPreference`.
235 searchCustomURLPreference.setEnabled(newSearchString.equals("Custom URL"));
238 case "search_custom_url":
239 // Set the new custom search URL as the summary text for `search_custom_url`. The default is `""`.
240 searchCustomURLPreference.setSummary(sharedPreferences.getString("search_custom_url", ""));
243 case "enable_full_screen_browsing_mode":
244 // Enable `transparent_navigation_bar` only if full screen browsing mode is enabled and `hide_system_bars` is disabled.
245 translucentNavigationBarPreference.setEnabled(sharedPreferences.getBoolean("enable_full_screen_browsing_mode", false) && !sharedPreferences.getBoolean("hide_system_bars", false));
248 case "hide_system_bars":
249 // Enable `translucentNavigationBarPreference` if `hide_system_bars` is disabled.
250 translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
254 // Set the new homepage URL as the summary text for the Homepage preference. The default is `https://www.duckduckgo.com`.
255 homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
258 case "default_font_size":
259 // Update the summary text of `default_font_size`.
260 defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
264 // If no match, do nothing.
270 // Register the listener.
271 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
274 // 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
275 // even while running in the foreground.
277 public void onPause() {
279 savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
283 public void onResume() {
285 savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);