]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java
2149df504d7b0ebffa4dcd90d1fe8bca0346f675
[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", "Default user agent")) {
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", "Default user agent"));
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
132         // Listen for preference changes.
133         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
134             @Override
135             // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.  We know.
136             @SuppressLint("SetJavaScriptEnabled")
137             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
138
139                 switch (key) {
140                     case "javascript_enabled":
141                         // Toggle the state of the `dom_storage_enabled` preference.  The default is `false`.
142                         final Preference domStorageEnabled = findPreference("dom_storage_enabled");
143                         domStorageEnabled.setEnabled(sharedPreferences.getBoolean("javascript_enabled", false));
144                         break;
145
146                     case "first_party_cookies_enabled":
147                         // Toggle the state of the `third_party_cookies_enabled` preference.  The default is `false`.
148                         final Preference thirdPartyCookiesEnabled = findPreference("third_party_cookies_enabled");
149                         thirdPartyCookiesEnabled.setEnabled(sharedPreferences.getBoolean("first_party_cookies_enabled", false));
150                         break;
151
152                     case "user_agent":
153                         String userAgentString = sharedPreferences.getString("user_agent", "Default user agent");
154
155                         switch (userAgentString) {
156                             case "Default user agent":
157                                 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
158                                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
159                                 customUserAgent.setEnabled(false);
160                                 break;
161
162                             case "Custom user agent":
163                                 // Display "Custom user agent" as the summary text for userAgentPreference, and enable customUserAgent.
164                                 userAgentPreference.setSummary(R.string.custom_user_agent);
165                                 customUserAgent.setEnabled(true);
166                                 break;
167
168                             default:
169                                 // Display the user agent as the summary text for userAgentPreference, and disable customUserAgent.
170                                 userAgentPreference.setSummary(sharedPreferences.getString("user_agent", "Default user agent"));
171                                 customUserAgent.setEnabled(false);
172                                 break;
173                         }
174                         break;
175
176                     case "custom_user_agent":
177                         // Set the new custom user agent as the summary text for "custom_user_agent".  The default is "PrivacyBrowser/1.0".
178                         customUserAgent.setSummary(sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
179                         break;
180
181                     case "javascript_disabled_search":
182                         String newJavaScriptDisabledSearchString = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
183                         if (newJavaScriptDisabledSearchString.equals("Custom URL")) {  // Set the summary text to `R.string.custom_url`, which is translated.
184                             javaScriptDisabledSearchPreference.setSummary(R.string.custom_url);
185                         } else {  // Set the new search URL as the summary text for the JavaScript-disabled search preference.
186                             javaScriptDisabledSearchPreference.setSummary(newJavaScriptDisabledSearchString);
187                         }
188
189                         // Enable or disable javaScriptDisabledSearchCustomURLPreference.
190                         javaScriptDisabledSearchCustomURLPreference.setEnabled(newJavaScriptDisabledSearchString.equals("Custom URL"));
191                         break;
192
193                     case "javascript_disabled_search_custom_url":
194                         // Set the new custom search URL as the summary text for `javascript_disabled_search_custom_url`.  The default is `""`.
195                         javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", ""));
196                         break;
197
198                     case "javascript_enabled_search":
199                         String newJavaScriptEnabledSearchString = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
200                         if (newJavaScriptEnabledSearchString.equals("Custom URL")) {  // Set the summary text to `R.string.custom_url`, which is translated.
201                             javaScriptEnabledSearchPreference.setSummary(R.string.custom_url);
202                         } else {  // Set the new search URL as the summary text for the JavaScript-enabled search preference..
203                             javaScriptEnabledSearchPreference.setSummary(newJavaScriptEnabledSearchString);
204                         }
205
206                         // Enable or disable javaScriptEnabledSearchCustomURLPreference.
207                         javaScriptEnabledSearchCustomURLPreference.setEnabled(newJavaScriptEnabledSearchString.equals("Custom URL"));
208                         break;
209
210                     case "javascript_enabled_search_custom_url":
211                         // Set the new custom search URL as the summary text for `javascript_enabled_search_custom_url`.  The default is `""`.
212                         javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", ""));
213                         break;
214
215                     case "homepage":
216                         // Set the new homepage URL as the summary text for the Homepage preference.  The default is `https://www.duckduckgo.com`.
217                         homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
218                         break;
219
220                     case "default_font_size":
221                         // Get the default font size as a string.  The default is `100`.
222                         String newDefaultFontSizeString = sharedPreferences.getString("default_font_size", "100");
223
224                         // Update the summary text of `default_font_size`.
225                         defaultFontSizePreference.setSummary(newDefaultFontSizeString + "%%");
226
227                     default:
228                         // If no match, do nothing.
229                         break;
230                 }
231             }
232         };
233
234         // Register the listener.
235         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
236     }
237
238     // 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
239     // even while running in the foreground.
240     @Override
241     public void onPause() {
242         super.onPause();
243         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
244     }
245
246     @Override
247     public void onResume() {
248         super.onResume();
249         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
250     }
251 }