Disable the third-party cookies setting if API < 21.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / SettingsFragment.java
1 /*
2  * Copyright 2016-2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.fragments;
21
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;
31
32 import com.stoutner.privacybrowser.R;
33
34 public class SettingsFragment extends PreferenceFragment {
35     private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
36     private SharedPreferences savedPreferences;
37
38     @Override
39     public void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         addPreferencesFromResource(R.xml.preferences);
42
43         // Initialize savedPreferences.
44         savedPreferences = getPreferenceScreen().getSharedPreferences();
45
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");
60
61         // Set dependencies.
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");
66
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=");
70
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));
73
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);
79
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());
85                 break;
86
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);
90                 break;
91
92             default:
93                 // Display the user agent from the preference as the summary text.
94                 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
95                 break;
96         }
97
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"));
101
102
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"));
105
106
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);
111         } else {
112             // Set the array value as the summary text.
113             torSearchPreference.setSummary(torSearchString);
114         }
115
116         // Set the summary text for `tor_search_custom_url`.  The default is `""`.
117         torSearchCustomURLPreference.setSummary(savedPreferences.getString("tor_search_custom_url", ""));
118
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"));
121
122
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);
127         } else {
128             // Set the array value as the summary text.
129             searchPreference.setSummary(searchString);
130         }
131
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"));
135
136
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));
139
140
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"));
143
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") + "%%");
146
147
148         // Listen for preference changes.
149         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
150             @Override
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) {
154
155                 switch (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));
159                         break;
160
161                     case "user_agent":
162                         String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
163
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);
169                                 break;
170
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);
175                                 break;
176
177                             default:
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);
181                                 break;
182                         }
183                         break;
184
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"));
188                         break;
189
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=");
194
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"));
197                         break;
198
199                     case "tor_homepage":
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"));
202                         break;
203
204                     case "tor_search":
205                         // Get the present search string.
206                         String presentTorSearchString = sharedPreferences.getString("tor_search", "https://3g2upl4pq6kufc4m.onion/html/?q=");
207
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);
212                         } else {
213                             // Set the array value as the summary text.
214                             torSearchPreference.setSummary(presentTorSearchString);
215                         }
216
217                         // Set the status of `torJavaScriptDisabledSearchCustomURLPreference`.
218                         torSearchCustomURLPreference.setEnabled(presentTorSearchString.equals("Custom URL"));
219                         break;
220
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", ""));
224                         break;
225
226                     case "search":
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);
232                         }
233
234                         // Enable or disable `searchCustomURLPreference`.
235                         searchCustomURLPreference.setEnabled(newSearchString.equals("Custom URL"));
236                         break;
237
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", ""));
241                         break;
242
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));
246                         break;
247
248                     case "hide_system_bars":
249                         // Enable `translucentNavigationBarPreference` if `hide_system_bars` is disabled.
250                         translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
251                         break;
252
253                     case "homepage":
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"));
256                         break;
257
258                     case "default_font_size":
259                         // Update the summary text of `default_font_size`.
260                         defaultFontSizePreference.setSummary(sharedPreferences.getString("default_font_size", "100") + "%%");
261                         break;
262
263                     default:
264                         // If no match, do nothing.
265                         break;
266                 }
267             }
268         };
269
270         // Register the listener.
271         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
272     }
273
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.
276     @Override
277     public void onPause() {
278         super.onPause();
279         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
280     }
281
282     @Override
283     public void onResume() {
284         super.onResume();
285         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
286     }
287 }