]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java
Add color changes to domain settings icons.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainSettingsFragment.java
1 /*
2  * Copyright 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.Context;
24 import android.database.Cursor;
25 import android.os.Bundle;
26 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23.  Otherwise we cannot call `getContext()`.
27 import android.support.v4.app.Fragment;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.webkit.WebView;
32 import android.widget.AdapterView;
33 import android.widget.ArrayAdapter;
34 import android.widget.CompoundButton;
35 import android.widget.EditText;
36 import android.widget.ImageView;
37 import android.widget.Spinner;
38 import android.widget.Switch;
39 import android.widget.TextView;
40
41 import com.stoutner.privacybrowser.R;
42 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
43
44 public class DomainSettingsFragment extends Fragment {
45     // `DATABASE_ID` is used by activities calling this fragment.
46     public static final String DATABASE_ID = "database_id";
47
48     // `databaseId` is used in `onCreate()` and `onCreateView()`.
49     private int databaseId;
50
51     @Override
52     public void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54
55         // Store the database id in `databaseId`.
56         databaseId = getArguments().getInt(DATABASE_ID);
57     }
58
59     // We have to use the deprecated `getDrawable()` until the minimum API >= 21.
60     @SuppressWarnings("deprecation")
61     @Override
62     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
63         // Inflate `domain_settings`.  `false` does not attach it to the root `container`.
64         View domainSettingsView = inflater.inflate(R.layout.domain_settings, container, false);
65
66         // Get a handle for the `Context`.
67         Context context = getContext();
68
69         // Get handles for the views in the fragment.
70         EditText domainNameEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_name_edittext);
71         Switch javaScriptEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_javascript_switch);
72         final ImageView javaScriptImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_javascript_imageview);
73         Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_first_party_cookies_switch);
74         final ImageView firstPartyCookiesImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_first_party_cookies_imageview);
75         Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_switch);
76         final ImageView thirdPartyCookiesImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_imageview);
77         Switch domStorageEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_switch);
78         final ImageView domStorageImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_imageview);
79         Switch formDataEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_form_data_switch);
80         final ImageView formDataImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_form_data_imageview);
81         Spinner userAgentSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_user_agent_spinner);
82         final TextView userAgentTextView = (TextView) domainSettingsView.findViewById(R.id.domain_settings_user_agent_textview);
83         final EditText customUserAgentEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
84         Spinner fontSizeSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_font_size_spinner);
85
86         // Initialize the database handler.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
87         // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
88         DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
89
90         // Get the database `Cursor` for this ID and move it to the first row.
91         Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
92         domainCursor.moveToFirst();
93
94         // Save the `Cursor` entries as variables.
95         String domainNameString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
96         int javaScriptEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT));
97         int firstPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES));
98         int thirdPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES));
99         int domStorageEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE));
100         int formDataEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA));
101         final String currentUserAgentString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT));
102         int fontSizeInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
103
104         // Create `ArrayAdapters` for the `Spinners`and their `entry values`.
105         ArrayAdapter<CharSequence> userAgentArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entries, android.R.layout.simple_spinner_item);
106         final ArrayAdapter<CharSequence> userAgentEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entry_values, android.R.layout.simple_spinner_item);
107         ArrayAdapter<CharSequence> fontSizeArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entries, android.R.layout.simple_spinner_item);
108         ArrayAdapter<CharSequence> fontSizeEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entry_values, android.R.layout.simple_spinner_item);
109
110         // Set the drop down style for the `ArrayAdapters`.
111         userAgentArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
112         fontSizeArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
113
114         // Set the `ArrayAdapters` for the `Spinners`.
115         userAgentSpinner.setAdapter(userAgentArrayAdapter);
116         fontSizeSpinner.setAdapter(fontSizeArrayAdapter);
117
118         // Set the domain name from the the database cursor.
119         domainNameEditText.setText(domainNameString);
120
121         // Set the JavaScript status.
122         if (javaScriptEnabledInt == 1) {  // JavaScript is enabled.
123             javaScriptEnabledSwitch.setChecked(true);
124             javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.javascript_enabled));
125         } else {  // JavaScript is disabled.
126             javaScriptEnabledSwitch.setChecked(false);
127             javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.privacy_mode));
128         }
129
130         // Set the first-party cookies status.
131         if (firstPartyCookiesEnabledInt == 1) {  // First-party cookies are enabled.
132             firstPartyCookiesEnabledSwitch.setChecked(true);
133             firstPartyCookiesImageView.setEnabled(true);
134         } else {  // First-party cookies are disabled.
135             firstPartyCookiesEnabledSwitch.setChecked(false);
136             firstPartyCookiesImageView.setEnabled(false);
137         }
138
139         // Set the third-party cookies status.
140         if (thirdPartyCookiesEnabledInt == 1) {  // Third-party cookies are enabled.
141             thirdPartyCookiesEnabledSwitch.setChecked(true);
142             thirdPartyCookiesImageView.setEnabled(true);
143         } else {  // Third-party cookies are disabled.
144             thirdPartyCookiesEnabledSwitch.setChecked(false);
145             thirdPartyCookiesImageView.setEnabled(false);
146         }
147
148         // Set the DOM storage status.
149         if (domStorageEnabledInt == 1) {  // DOM storage is enabled.
150             domStorageEnabledSwitch.setChecked(true);
151             domStorageImageView.setEnabled(true);
152         } else {  // Dom storage is disabled.
153             domStorageEnabledSwitch.setChecked(false);
154             domStorageImageView.setEnabled(false);
155         }
156
157         // Set the form data status.
158         if (formDataEnabledInt == 1) {  // Form data is enabled.
159             formDataEnabledSwitch.setChecked(true);
160             formDataImageView.setEnabled(true);
161         } else {  // Form data is disabled.
162             formDataEnabledSwitch.setChecked(false);
163             formDataImageView.setEnabled(false);
164         }
165
166         // We need to inflated a `WebView` to get the default user agent.
167         // `@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.
168         @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
169         WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
170         final String webViewDefaultUserAgentString = bareWebView.getSettings().getUserAgentString();
171
172         // Get the position of the user agent in `userAgentEntryValuesArrayAdapter`.
173         int userAgentArrayPosition = userAgentEntryValuesArrayAdapter.getPosition(currentUserAgentString);
174
175         // Set the user agent.
176         if (userAgentArrayPosition == -1) {  // We are using a custom `userAgentString`.
177             // Set `userAgentSpinner` to `Custom`.
178             userAgentSpinner.setSelection(userAgentEntryValuesArrayAdapter.getPosition("Custom user agent"));
179
180             // Hide `userAgentTextView`.
181             userAgentTextView.setVisibility(View.GONE);
182
183             // Show `customUserAgentEditText` and set `userAgentString` as the text.
184             customUserAgentEditText.setVisibility(View.VISIBLE);
185             customUserAgentEditText.setText(currentUserAgentString);
186         } else if (currentUserAgentString.equals("WebView default user agent")) {  // We are using the `WebView` default user agent.
187             // Set the `userAgentSpinner` selection.
188             userAgentSpinner.setSelection(userAgentArrayPosition);
189
190             // Show `userAgentTextView` and set the text.
191             userAgentTextView.setVisibility(View.VISIBLE);
192             userAgentTextView.setText(webViewDefaultUserAgentString);
193
194             // Hide `customUserAgentEditText`.
195             customUserAgentEditText.setVisibility(View.GONE);
196         } else {  // We are using a standard user agent.
197             // Set the `userAgentSpinner` selection.
198             userAgentSpinner.setSelection(userAgentArrayPosition);
199
200             // Show `userAgentTextView` and set the text.
201             userAgentTextView.setVisibility(View.VISIBLE);
202             userAgentTextView.setText(currentUserAgentString);
203
204             // Hide `customUserAgentEditText`.
205             customUserAgentEditText.setVisibility(View.GONE);
206         }
207
208         // Set the selected font size.
209         int fontSizeArrayPosition = fontSizeEntryValuesArrayAdapter.getPosition(String.valueOf(fontSizeInt));
210         fontSizeSpinner.setSelection(fontSizeArrayPosition);
211
212         // Set the `javaScriptEnabledSwitch` `OnCheckedChangeListener()`.
213         javaScriptEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
214             @Override
215             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
216                 // Update the icon.
217                 if (isChecked) {
218                     javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.javascript_enabled));
219                 } else {
220                     javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.privacy_mode));
221                 }
222             }
223         });
224
225         // Set the `firstPartyCookiesEnabledSwitch` `OnCheckedChangeListener()`.
226         firstPartyCookiesEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
227             @Override
228             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
229                 // Update the icon.
230                 firstPartyCookiesImageView.setEnabled(isChecked);
231             }
232         });
233
234         // Set the `thirdPartyCookiesEnabledSwitch` `OnCheckedChangeListener()`.
235         thirdPartyCookiesEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
236             @Override
237             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
238                 // Update the icon.
239                 thirdPartyCookiesImageView.setEnabled(isChecked);
240             }
241         });
242
243         // Set the `domStorageEnabledSwitch` `OnCheckedChangeListener()`.
244         domStorageEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
245             @Override
246             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
247                 // Update the icon.
248                 domStorageImageView.setEnabled(isChecked);
249             }
250         });
251
252         // Set the `formDataEnabledSwitch` `OnCheckedChangeListener()`.
253         formDataEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
254             @Override
255             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
256                 // Update the icon.
257                 formDataImageView.setEnabled(isChecked);
258             }
259         });
260
261         // Set the `userAgentSpinner` `onItemClickListener()`.
262         userAgentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
263             @Override
264             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
265                 // Store the new user agent string.
266                 String newUserAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[position];
267
268                 // Set the new user agent.
269                 switch (newUserAgentString) {
270                     case "Custom user agent":
271                         // Hide `userAgentTextView`.
272                         userAgentTextView.setVisibility(View.GONE);
273
274                         // Show `customUserAgentEditText` and set `userAgentString` as the text.
275                         customUserAgentEditText.setVisibility(View.VISIBLE);
276                         customUserAgentEditText.setText(currentUserAgentString);
277                         break;
278
279                     case "WebView default user agent":
280                         // Show `userAgentTextView` and set the text.
281                         userAgentTextView.setVisibility(View.VISIBLE);
282                         userAgentTextView.setText(webViewDefaultUserAgentString);
283
284                         // Hide `customUserAgentEditText`.
285                         customUserAgentEditText.setVisibility(View.GONE);
286                         break;
287
288                     default:
289                         // Show `userAgentTextView` and set the text.
290                         userAgentTextView.setVisibility(View.VISIBLE);
291                         userAgentTextView.setText(getResources().getStringArray(R.array.user_agent_entry_values)[position]);
292
293                         // Hide `customUserAgentEditText`.
294                         customUserAgentEditText.setVisibility(View.GONE);
295                         break;
296                 }
297             }
298
299             @Override
300             public void onNothingSelected(AdapterView<?> parent) {
301                 // Do nothing.
302             }
303         });
304
305         return domainSettingsView;
306     }
307 }