Allow customization of the search URLs.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / 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;
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.MenuItem;
29
30 public class SettingsFragment extends PreferenceFragment {
31     private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
32     private SharedPreferences savedPreferences;
33
34     @Override
35     public void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         addPreferencesFromResource(R.xml.preferences);
38
39         // Initialize savedPreferences.
40         savedPreferences = getPreferenceScreen().getSharedPreferences();
41
42
43         // Set the JavaScript-disabled search URL as the summary text for the JavaScript-disabled search preference when the preference screen is loaded.
44         // The default is "https://duckduckgo.com/html/?q=".
45         final Preference javaScriptDisabledSearchPreference = findPreference("javascript_disabled_search");
46         javaScriptDisabledSearchPreference.setSummary(savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="));
47
48         // 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".
49         final Preference javaScriptDisabledSearchCustomURLPreference = findPreference("javascript_disabled_search_custom_url");
50         javaScriptDisabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_disabled_search_custom_url", ""));
51         javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchPreference.getSummary().equals("Custom URL"));
52
53         // Set the JavaScript-enabed searchURL as the summary text for the JavaScript-enabled search preference when the preference screen is loaded.
54         // The default is "https://duckduckgo.com/?q=".
55         final Preference javaScriptEnabledSearchPreference = findPreference("javascript_enabled_search");
56         javaScriptEnabledSearchPreference.setSummary(savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="));
57
58         // 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".
59         final Preference javaScriptEnabledSearchCustomURLPreference = findPreference("javascript_enabled_search_custom_url");
60         javaScriptEnabledSearchCustomURLPreference.setSummary(savedPreferences.getString("javascript_enabled_search_custom_url", ""));
61         javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchPreference.getSummary().equals("Custom URL"));
62
63         // 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".
64         final Preference homepagePreference = findPreference("homepage");
65         homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
66
67
68         // Listen for preference changes.
69         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
70             @Override
71             // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
72             @SuppressLint("SetJavaScriptEnabled")
73             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
74
75                 // Several keys need to update the toggleJavaScript icon.
76                 MenuItem toggleJavaScript = MainWebViewActivity.mainMenu.findItem(R.id.toggleJavaScript);
77
78                 switch (key) {
79                     case "javascript_enabled":
80                         // Set javaScriptEnabled to the new state.  The default is false.
81                         MainWebViewActivity.javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
82
83                         // Update mainWebView and reload the website.
84                         MainWebViewActivity.mainWebView.getSettings().setJavaScriptEnabled(MainWebViewActivity.javaScriptEnabled);
85                         MainWebViewActivity.mainWebView.reload();
86
87                         // Update the toggleJavaScript icon.
88                         if (MainWebViewActivity.javaScriptEnabled) {
89                             toggleJavaScript.setIcon(R.drawable.javascript_enabled);
90                         } else {
91                             if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
92                                 toggleJavaScript.setIcon(R.drawable.warning);
93                             } else {
94                                 toggleJavaScript.setIcon(R.drawable.privacy_mode);
95                             }
96                         }
97                         return;
98
99                     case "first_party_cookies_enabled":
100                         // Set firstPartyCookiesEnabled to the new state.  The default is false.
101                         MainWebViewActivity.firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false);
102
103                         // Update the checkbox in the options menu.
104                         MenuItem firstPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleFirstPartyCookies);
105                         firstPartyCookiesMenuItem.setChecked(MainWebViewActivity.firstPartyCookiesEnabled);
106
107                         // Update mainWebView and reload the website.
108                         MainWebViewActivity.cookieManager.setAcceptCookie(MainWebViewActivity.firstPartyCookiesEnabled);
109                         MainWebViewActivity.mainWebView.reload();
110
111                         // Update the toggleJavaScript icon.
112                         if (MainWebViewActivity.javaScriptEnabled) {
113                             toggleJavaScript.setIcon(R.drawable.javascript_enabled);
114                         } else {
115                             if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
116                                 toggleJavaScript.setIcon(R.drawable.warning);
117                             } else {
118                                 toggleJavaScript.setIcon(R.drawable.privacy_mode);
119                             }
120                         }
121                         return;
122
123                     case "third_party_cookies_enabled":
124                         // Set thirdPartyCookiesEnabled to the new state.  The default is false.
125                         MainWebViewActivity.thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false);
126
127                         // Update the checkbox in the options menu.
128                         MenuItem thirdPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleThirdPartyCookies);
129                         thirdPartyCookiesMenuItem.setChecked(MainWebViewActivity.thirdPartyCookiesEnabled);
130
131                         // Update mainWebView and reload the website if API >= 21.
132                         if (Build.VERSION.SDK_INT >= 21) {
133                             MainWebViewActivity.cookieManager.setAcceptThirdPartyCookies(MainWebViewActivity.mainWebView, MainWebViewActivity.thirdPartyCookiesEnabled);
134                             MainWebViewActivity.mainWebView.reload();
135                         }
136                         return;
137
138                     case "dom_storage_enabled":
139                         // Set domStorageEnabled to the new state.  The default is false.
140                         MainWebViewActivity.domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false);
141
142                         // Update the checkbox in the options menu.
143                         MenuItem domStorageMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleDomStorage);
144                         domStorageMenuItem.setChecked(MainWebViewActivity.domStorageEnabled);
145
146                         // Update mainWebView and reload the website.
147                         MainWebViewActivity.mainWebView.getSettings().setDomStorageEnabled(MainWebViewActivity.domStorageEnabled);
148                         MainWebViewActivity.mainWebView.reload();
149
150                         // Update the toggleJavaScript icon.
151                         if (MainWebViewActivity.javaScriptEnabled) {
152                             toggleJavaScript.setIcon(R.drawable.javascript_enabled);
153                         } else {
154                             if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
155                                 toggleJavaScript.setIcon(R.drawable.warning);
156                             } else {
157                                 toggleJavaScript.setIcon(R.drawable.privacy_mode);
158                             }
159                         }
160                         return;
161
162                     case "javascript_disabled_search":
163                         // Set the new search URL as the summary text for the JavaScript-disabled search preference.  The default is "https://duckduckgo.com/html/?q=".
164                         javaScriptDisabledSearchPreference.setSummary(sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q="));
165
166                         // Enable "javascript_disabled_search_custom_url" if "javascript_disabled_search" is set to "Custom URL".
167                         javaScriptDisabledSearchCustomURLPreference.setEnabled(javaScriptDisabledSearchPreference.getSummary().equals("Custom URL"));
168
169                         // Update the javaScriptDisabledSearchURL variable.  The default is "https://duckduckgo.com/html/?q=".
170                         MainWebViewActivity.javaScriptDisabledSearchURL = sharedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
171                         return;
172
173                     case "javascript_disabled_search_custom_url":
174                         // Set the new custom search URL as the summary text for "javascript_disabled_search_custom_url".  The default is "".
175                         javaScriptDisabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_disabled_search_custom_url", ""));
176
177                         // Update javaScriptDisabledSearchCustomURL.  The default is "".
178                         MainWebViewActivity.javaScriptDisabledSearchCustomURL = sharedPreferences.getString("javascript_disabled_search_custom_url", "");
179
180                     case "javascript_enabled_search":
181                         // Set the new search URL as the summary text for the JavaScript-enabled search preference.  The default is "https://duckduckgo.com/?q=".
182                         javaScriptEnabledSearchPreference.setSummary(sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q="));
183
184                         // Enable "javascript_enabled_search_custom_url" if "javascript_enabled_search" is set to "Custom URL".
185                         javaScriptEnabledSearchCustomURLPreference.setEnabled(javaScriptEnabledSearchPreference.getSummary().equals("Custom URL"));
186
187                         // Update the javaScriptEnabledSearchURL variable.  The default is "https://duckduckgo.com/?q=".
188                         MainWebViewActivity.javaScriptEnabledSearchURL = sharedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
189                         return;
190
191                     case "javascript_enabled_search_custom_url":
192                         // Set the new custom search URL as the summary text for "javascript_enabled_search_custom_url".  The default is "".
193                         javaScriptEnabledSearchCustomURLPreference.setSummary(sharedPreferences.getString("javascript_enabled_search_custom_url", ""));
194
195                         // Update javaScriptEnabledSearchCustomURL.  The default is "".
196                         MainWebViewActivity.javaScriptEnabledSearchCustomURL = sharedPreferences.getString("javascript_enabled_search_custom_url", "");
197
198                     case "homepage":
199                         // Set the new homepage URL as the summary text for the Homepage preference.  The default is "https://www.duckduckgo.com".
200                         homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
201
202                         // Update the homepage variable.  The default is "https://www.duckduckgo.com".
203                         MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
204                         return;
205
206                     case "swipe_to_refresh_enabled":
207                         // Set swipeToRefreshEnabled to the new state.  The default is true.
208                         MainWebViewActivity.swipeToRefreshEnabled = sharedPreferences.getBoolean("swipe_to_refresh_enabled", true);
209
210                         // Update swipeRefreshLayout to match the new state.
211                         MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled);
212                         return;
213
214                     // If no match, do nothing.
215                     default:
216                 }
217             }
218         };
219
220         // Register the listener.
221         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
222     }
223
224     // 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
225     // even while running in the foreground.
226     @Override
227     public void onResume() {
228         super.onResume();
229         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
230     }
231
232     @Override
233     public void onPause() {
234         super.onPause();
235         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
236     }
237 }