Create preference to disable swipe to refresh when not desired. Remove -standard...
[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.content.SharedPreferences;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.preference.Preference;
26 import android.preference.PreferenceFragment;
27 import android.view.MenuItem;
28
29 public class SettingsFragment extends PreferenceFragment {
30     private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
31     private SharedPreferences savedPreferences;
32
33     @Override
34     public void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         addPreferencesFromResource(R.xml.preferences);
37
38         final Preference homepagePreference = findPreference("homepage");
39
40         // Initialize savedPreferences.
41         savedPreferences = getPreferenceScreen().getSharedPreferences();
42
43         // 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".
44         homepagePreference.setSummary(savedPreferences.getString("homepage", "https://www.duckduckgo.com"));
45
46         // Listen for preference changes.
47         preferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
48             @Override
49             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
50
51                 // Several keys need to update the toggleJavaScript icon.
52                 MenuItem toggleJavaScript = MainWebViewActivity.mainMenu.findItem(R.id.toggleJavaScript);
53
54                 switch (key) {
55                     case "javascript_enabled":
56                         // Set javaScriptEnabled to the new state.  The default is false.
57                         MainWebViewActivity.javaScriptEnabled = sharedPreferences.getBoolean("javascript_enabled", false);
58
59                         // Update mainWebView and reload the website.
60                         MainWebViewActivity.mainWebView.getSettings().setJavaScriptEnabled(MainWebViewActivity.javaScriptEnabled);
61                         MainWebViewActivity.mainWebView.reload();
62
63                         // Update the toggleJavaScript icon.
64                         if (MainWebViewActivity.javaScriptEnabled) {
65                             toggleJavaScript.setIcon(R.drawable.javascript_enabled);
66                         } else {
67                             if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
68                                 toggleJavaScript.setIcon(R.drawable.warning);
69                             } else {
70                                 toggleJavaScript.setIcon(R.drawable.privacy_mode);
71                             }
72                         }
73                         return;
74
75                     case "first_party_cookies_enabled":
76                         // Set firstPartyCookiesEnabled to the new state.  The default is false.
77                         MainWebViewActivity.firstPartyCookiesEnabled = sharedPreferences.getBoolean("first_party_cookies_enabled", false);
78
79                         // Update the checkbox in the options menu.
80                         MenuItem firstPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleFirstPartyCookies);
81                         firstPartyCookiesMenuItem.setChecked(MainWebViewActivity.firstPartyCookiesEnabled);
82
83                         // Update mainWebView and reload the website.
84                         MainWebViewActivity.cookieManager.setAcceptCookie(MainWebViewActivity.firstPartyCookiesEnabled);
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 "third_party_cookies_enabled":
100                         // Set thirdPartyCookiesEnabled to the new state.  The default is false.
101                         MainWebViewActivity.thirdPartyCookiesEnabled = sharedPreferences.getBoolean("third_party_cookies_enabled", false);
102
103                         // Update the checkbox in the options menu.
104                         MenuItem thirdPartyCookiesMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleThirdPartyCookies);
105                         thirdPartyCookiesMenuItem.setChecked(MainWebViewActivity.thirdPartyCookiesEnabled);
106
107                         // Update mainWebView and reload the website if API >= 21.
108                         if (Build.VERSION.SDK_INT >= 21) {
109                             MainWebViewActivity.cookieManager.setAcceptThirdPartyCookies(MainWebViewActivity.mainWebView, MainWebViewActivity.thirdPartyCookiesEnabled);
110                             MainWebViewActivity.mainWebView.reload();
111                         }
112                         return;
113
114                     case "dom_storage_enabled":
115                         // Set domStorageEnabled to the new state.  The default is false.
116                         MainWebViewActivity.domStorageEnabled = sharedPreferences.getBoolean("dom_storage_enabled", false);
117
118                         // Update the checkbox in the options menu.
119                         MenuItem domStorageMenuItem = MainWebViewActivity.mainMenu.findItem(R.id.toggleDomStorage);
120                         domStorageMenuItem.setChecked(MainWebViewActivity.domStorageEnabled);
121
122                         // Update mainWebView and reload the website.
123                         MainWebViewActivity.mainWebView.getSettings().setDomStorageEnabled(MainWebViewActivity.domStorageEnabled);
124                         MainWebViewActivity.mainWebView.reload();
125
126                         // Update the toggleJavaScript icon.
127                         if (MainWebViewActivity.javaScriptEnabled) {
128                             toggleJavaScript.setIcon(R.drawable.javascript_enabled);
129                         } else {
130                             if (MainWebViewActivity.firstPartyCookiesEnabled || MainWebViewActivity.domStorageEnabled) {
131                                 toggleJavaScript.setIcon(R.drawable.warning);
132                             } else {
133                                 toggleJavaScript.setIcon(R.drawable.privacy_mode);
134                             }
135                         }
136                         return;
137
138                     case "homepage":
139                         // Set the new homepage URL as the summary text for the Homepage preference.  The default is "https://www.duckduckgo.com".
140                         homepagePreference.setSummary(sharedPreferences.getString("homepage", "https://www.duckduckgo.com"));
141
142                         // Update the homepage variable.  The default is "https://www.duckduckgo.com".
143                         MainWebViewActivity.homepage = sharedPreferences.getString("homepage", "https://www.duckduckgo.com");
144                         return;
145
146                     case "swipe_to_refresh_enabled":
147                         // Set swipeToRefreshEnabled to the new state.  The default is true.
148                         MainWebViewActivity.swipeToRefreshEnabled = sharedPreferences.getBoolean("swipe_to_refresh_enabled", true);
149
150                         // Update swipeRefreshLayout to match the new state.
151                         MainWebViewActivity.swipeToRefresh.setEnabled(MainWebViewActivity.swipeToRefreshEnabled);
152
153                     // If no match, do nothing.
154                     default:
155                 }
156             }
157         };
158
159         // Register the listener.
160         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
161     }
162
163     // It is necessary to reregister the listener on every resume or it will randomly stop working for the user because apps can be paused and resumed at any time,
164     // even when they are in the foreground.
165     @Override
166     public void onResume() {
167         super.onResume();
168         savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
169     }
170
171     @Override
172     public void onPause() {
173         super.onPause();
174         savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener);
175     }
176 }