]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java
Change the default user-agent to `PrivacyBrowser/1.0`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / SettingsFragment.java
1 /**
2  * Copyright 2016 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.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;
30
31 import com.stoutner.privacybrowser.R;
32
33 public class SettingsFragment extends PreferenceFragment {
34     private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
35     private SharedPreferences savedPreferences;
36
37     @Override
38     public void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         addPreferencesFromResource(R.xml.preferences);
41
42         // Initialize savedPreferences.
43         savedPreferences = getPreferenceScreen().getSharedPreferences();
44
45         // Allow the user to access "dom_storage_enabled" if "javascript_enabled" is enabled.  The default is false.
46         final Preference domStorageEnabled = findPreference("dom_storage_enabled");
47         domStorageEnabled.setEnabled(savedPreferences.getBoolean("javascript_enabled", false));
48
49         // Allow the user to access "third_party_cookies_enabled" if "first_party_cookies_enabled" is enabled.  The default is false.
50         final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
51         thirdPartyCookiesEnabled.setEnabled(savedPreferences.getBoolean("first_party_cookies_enabled", false));
52
53
54         // We need an inflated `WebView` to get the default user agent.
55         LayoutInflater inflater = getActivity().getLayoutInflater();
56         // `@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.
57         // `false` does not attach the view to the root.
58         @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
59         final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
60
61         // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
62         final Preference userAgentPreference = findPreference("user_agent");
63         switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
64             case "Default user agent":
65                 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
66                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
67                 break;
68
69             case "Custom user agent":
70                 // We can't use the string from the array because it is referenced in code and can't be translated.
71                 userAgentPreference.setSummary(R.string.custom_user_agent);
72                 break;
73
74             default:
75                 // Display the user agent from the preference as the summary text.
76                 userAgentPreference.setSummary(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
77                 break;
78         }
79
80         // 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".
81         final Preference customUserAgent = findPreference("custom_user_agent");
82         customUserAgent.setSummary(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
83         customUserAgent.setEnabled(userAgentPreference.getSummary().equals("Custom user agent"));
84
85
86         // Set the JavaScript-disabled search URL as the summary text for the JavaScript-disabled search preference when the preference screen is loaded.
87         // The default is `https://duckduckgo.com/html/?q=`.
88         final Preference javaScriptDisabledSearchPreference = findPreference("javascript_disabled_search");
89         String javaScriptDisabledSearchString = savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
90         if (javaScriptDisabledSearchString.equals("Custom URL")) {
91             // If set to "Custom URL", use R.string.custom_url, which will be translated, instead of the array value, which will not.
92             javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
93         } else {
94             // Set the array value as the summary text.
95             javaScriptDisabledSearchPreference.setSummary(javaScriptDisabledSearchString);
96         }
97
98         // 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`.
99         final Preference javaScriptDisabledSearchCustomURLPreference = findPreference("javascript_disabled_search_custom_url");
100         javaScriptDisabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_disabled_search_custom_url", ""));
101         javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchString.equals("Custom URL"));
102
103
104         // Set the JavaScript-enabled searchURL as the summary text for the JavaScript-enabled search preference when the preference screen is loaded.
105         // The default is `https://duckduckgo.com/?q=`.
106         final Preference javaScriptEnabledSearchPreference = findPreference("javascript_enabled_search");
107         String javaScriptEnabledSearchString = savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
108         if (javaScriptEnabledSearchString.equals("Custom URL")) {
109             // If set to "Custom URL", use R.string.custom_url, which will be translated, instead of the array value, which will not.
110             javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
111         } else {
112             // Set the array value as the summary text.
113             javaScriptEnabledSearchPreference.setSummary(javaScriptEnabledSearchString);
114         }
115
116         // 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`.
117         final Preference javaScriptEnabledSearchCustomURLPreference = findPreference("javascript_enabled_search_custom_url");
118         javaScriptEnabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_enabled_search_custom_url", ""));
119         javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchString.equals("Custom URL"));
120
121
122         // 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`.
123         final Preference homepagePreference = findPreference("homepage");
124         homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
125
126         // 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`.
127         final Preference defaultFontSizePreference = findPreference("default_font_size");
128         String defaultFontSizeString = savedPreferences.getString("default_font_size", "100");
129         defaultFontSizePreference.setSummary(defaultFontSizeString + "%%");
130
131         // Disable `transparent_navigation_bar` if `hide_system_bars` is enabled.
132         final Preference translucentNavigationBarPreference = findPreference("translucent_navigation_bar");
133         translucentNavigationBarPreference.setEnabled(!savedPreferences.getBoolean("hide_system_bars", false));
134
135
136         // Listen for preference changes.
137         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
138             @Override
139             // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.  We know.
140             @SuppressLint("SetJavaScriptEnabled")
141             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
142
143                 switch (key) {
144                     case "javascript_enabled":
145                         // Toggle the state of the `dom_storage_enabled` preference.  The default is `false`.
146                         final Preference domStorageEnabled = findPreference("dom_storage_enabled");
147                         domStorageEnabled.setEnabled(sharedPreferences.getBoolean("javascript_enabled", false));
148                         break;
149
150                     case "first_party_cookies_enabled":
151                         // Toggle the state of the `third_party_cookies_enabled` preference.  The default is `false`.
152                         final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
153                         thirdPartyCookiesEnabled.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false));
154                         break;
155
156                     case "user_agent":
157                         String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
158
159                         switch (userAgentString) {
160                             case "Default user agent":
161                                 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
162                                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
163                                 customUserAgent.setEnabled(false);
164                                 break;
165
166                             case "Custom user agent":
167                                 // Display "Custom user agent" as the summary text for userAgentPreference, and enable customUserAgent.
168                                 userAgentPreference.setSummary(R.string.custom_user_agent);
169                                 customUserAgent.setEnabled(true);
170                                 break;
171
172                             default:
173                                 // Display the user agent as the summary text for userAgentPreference, and disable customUserAgent.
174                                 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
175                                 customUserAgent.setEnabled(false);
176                                 break;
177                         }
178                         break;
179
180                     case "custom_user_agent":
181                         // Set the new custom user agent as the summary text for `custom_user_agent`.  The default is `PrivacyBrowser/1.0`.
182                         customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
183                         break;
184
185                     case "javascript_disabled_search":
186                         String newJavaScriptDisabledSearchString = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
187                         if (newJavaScriptDisabledSearchString.equals("Custom URL")) {  // Set the summary text to `R.string.custom_url`, which is translated.
188                             javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
189                         } else {  // Set the new search URL as the summary text for the JavaScript-disabled search preference.
190                             javaScriptDisabledSearchPreference.setSummary(newJavaScriptDisabledSearchString);
191                         }
192
193                         // Enable or disable javaScriptDisabledSearchCustomURLPreference.
194                         javaScriptDisabledSearchCustomURLPreference.setEnabled(newJavaScriptDisabledSearchString.equals("Custom URL"));
195                         break;
196
197                     case "javascript_disabled_search_custom_url":
198                         // Set the new custom search URL as the summary text for `javascript_disabled_search_custom_url`.  The default is `""`.
199                         javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", ""));
200                         break;
201
202                     case "javascript_enabled_search":
203                         String newJavaScriptEnabledSearchString = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
204                         if (newJavaScriptEnabledSearchString.equals("Custom URL")) {  // Set the summary text to `R.string.custom_url`, which is translated.
205                             javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
206                         } else {  // Set the new search URL as the summary text for the JavaScript-enabled search preference..
207                             javaScriptEnabledSearchPreference.setSummary(newJavaScriptEnabledSearchString);
208                         }
209
210                         // Enable or disable javaScriptEnabledSearchCustomURLPreference.
211                         javaScriptEnabledSearchCustomURLPreference.setEnabled(newJavaScriptEnabledSearchString.equals("Custom URL"));
212                         break;
213
214                     case "javascript_enabled_search_custom_url":
215                         // Set the new custom search URL as the summary text for `javascript_enabled_search_custom_url`.  The default is `""`.
216                         javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", ""));
217                         break;
218
219                     case "homepage":
220                         // Set the new homepage URL as the summary text for the Homepage preference.  The default is `https://www.duckduckgo.com`.
221                         homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
222                         break;
223
224                     case "default_font_size":
225                         // Get the default font size as a string.  The default is `100`.
226                         String newDefaultFontSizeString = sharedPreferences.getString("default_font_size", "100");
227
228                         // Update the summary text of `default_font_size`.
229                         defaultFontSizePreference.setSummary(newDefaultFontSizeString + "%%");
230                         break;
231
232                     case "hide_system_bars":
233                         // Enable `translucentNavigationBarPreference` if `hide_system_bars` is disabled.
234                         translucentNavigationBarPreference.setEnabled(!sharedPreferences.getBoolean("hide_system_bars", false));
235                         break;
236
237                     default:
238                         // If no match, do nothing.
239                         break;
240                 }
241             }
242         };
243
244         // Register the listener.
245         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
246     }
247
248     // 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
249     // even while running in the foreground.
250     @Override
251     public void onPause() {
252         super.onPause();
253         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
254     }
255
256     @Override
257     public void onResume() {
258         super.onResume();
259         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
260     }
261 }