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