Add blocklist controls to domain settings. https://redmine.stoutner.com/issues/223
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainsListFragment.java
1 /*
2  * Copyright © 2017-2018 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.net.http.SslCertificate;
23 import android.os.Bundle;
24 import android.support.annotation.NonNull;
25 import android.support.design.widget.FloatingActionButton;
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.support.v4.app.FragmentManager;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.EditText;
34 import android.widget.ListView;
35 import android.widget.RadioButton;
36 import android.widget.Spinner;
37 import android.widget.Switch;
38
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.activities.DomainsActivity;
41 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
42 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
43
44 public class DomainsListFragment extends Fragment {
45     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
46         // Inflate `domains_list_fragment`.  `false` does not attach it to the root `container`.
47         View domainsListFragmentView = inflater.inflate(R.layout.domains_list_fragment, container, false);
48
49         // Initialize `domainsListView`.
50         ListView domainsListView = domainsListFragmentView.findViewById(R.id.domains_listview);
51
52         // Initialize the database handler.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
53         final DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
54
55         // Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null.
56         assert getActivity() != null;
57
58         // Get a handle for `supportFragmentManager`.
59         final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
60
61         domainsListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
62             // Dismiss `undoDeleteSnackbar` if it is currently displayed (because a domain has just been deleted).
63             if ((DomainsActivity.undoDeleteSnackbar != null) && (DomainsActivity.undoDeleteSnackbar.isShown())) {
64                 DomainsActivity.dismissingSnackbar = true;
65
66                 DomainsActivity.undoDeleteSnackbar.dismiss();
67             }
68
69             // Save the current domain settings if operating in two-paned mode and a domain is currently selected.
70             if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) {
71                 View domainSettingsFragmentView = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container).getView();
72                 assert domainSettingsFragmentView != null;
73
74                 // Get handles for the domain settings.
75                 EditText domainNameEditText = domainSettingsFragmentView.findViewById(R.id.domain_settings_name_edittext);
76                 Switch javaScriptSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_javascript_switch);
77                 Switch firstPartyCookiesSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_first_party_cookies_switch);
78                 Switch thirdPartyCookiesSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_third_party_cookies_switch);
79                 Switch domStorageSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_dom_storage_switch);
80                 Switch formDataSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_form_data_switch);
81                 Switch easyListSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_easylist_switch);
82                 Switch easyPrivacySwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_easyprivacy_switch);
83                 Switch fanboysAnnoyanceSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_fanboys_annoyance_list_switch);
84                 Switch fanboysSocialBlockingSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_fanboys_social_blocking_list_switch);
85                 Spinner userAgentSpinner = domainSettingsFragmentView.findViewById(R.id.domain_settings_user_agent_spinner);
86                 EditText customUserAgentEditText = domainSettingsFragmentView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
87                 Spinner fontSizeSpinner = domainSettingsFragmentView.findViewById(R.id.domain_settings_font_size_spinner);
88                 Spinner displayWebpageImagesSpinner = domainSettingsFragmentView.findViewById(R.id.domain_settings_display_webpage_images_spinner);
89                 Spinner nightModeSpinner = domainSettingsFragmentView.findViewById(R.id.domain_settings_night_mode_spinner);
90                 Switch pinnedSslCertificateSwitch = domainSettingsFragmentView.findViewById(R.id.domain_settings_pinned_ssl_certificate_switch);
91                 RadioButton savedSslCertificateRadioButton = domainSettingsFragmentView.findViewById(R.id.saved_ssl_certificate_radiobutton);
92                 RadioButton currentWebsiteCertificateRadioButton = domainSettingsFragmentView.findViewById(R.id.current_website_certificate_radiobutton);
93
94                 // Extract the data for the domain settings.
95                 String domainNameString = domainNameEditText.getText().toString();
96                 boolean javaScriptEnabled = javaScriptSwitch.isChecked();
97                 boolean firstPartyCookiesEnabled = firstPartyCookiesSwitch.isChecked();
98                 boolean thirdPartyCookiesEnabled = thirdPartyCookiesSwitch.isChecked();
99                 boolean domStorageEnabled  = domStorageSwitch.isChecked();
100                 boolean formDataEnabled = formDataSwitch.isChecked();
101                 boolean easyListEnabled = easyListSwitch.isChecked();
102                 boolean easyPrivacyEnabled = easyPrivacySwitch.isChecked();
103                 boolean fanboysAnnoyanceEnabled = fanboysAnnoyanceSwitch.isChecked();
104                 boolean fanboysSocialBlockingEnabled = fanboysSocialBlockingSwitch.isChecked();
105                 int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
106                 int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
107                 int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
108                 int nightModeInt = nightModeSpinner.getSelectedItemPosition();
109                 boolean pinnedSslCertificate = pinnedSslCertificateSwitch.isChecked();
110
111                 // Get the data for the `Spinners` from the entry values string arrays.
112                 String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
113                 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
114
115                 // Check to see if we are using a custom user agent.
116                 if (userAgentString.equals("Custom user agent")) {
117                     // Set `userAgentString` to the custom user agent string.
118                     userAgentString = customUserAgentEditText.getText().toString();
119                 }
120
121                 // Save the domain settings.
122                 if (savedSslCertificateRadioButton.isChecked()) {  // The current certificate is being used.
123                     // Update the database except for the certificate.
124                     domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled,
125                             domStorageEnabled, formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt,
126                             nightModeInt, pinnedSslCertificate);
127                 } else if (currentWebsiteCertificateRadioButton.isChecked()) {  // The certificate is being updated with the current website certificate.
128                     // Get the current website SSL certificate.
129                     SslCertificate currentWebsiteSslCertificate = MainWebViewActivity.sslCertificate;
130
131                     // Store the values from the SSL certificate.
132                     String issuedToCommonName = currentWebsiteSslCertificate.getIssuedTo().getCName();
133                     String issuedToOrganization = currentWebsiteSslCertificate.getIssuedTo().getOName();
134                     String issuedToOrganizationalUnit = currentWebsiteSslCertificate.getIssuedTo().getUName();
135                     String issuedByCommonName = currentWebsiteSslCertificate.getIssuedBy().getCName();
136                     String issuedByOrganization = currentWebsiteSslCertificate.getIssuedBy().getOName();
137                     String issuedByOrganizationalUnit = currentWebsiteSslCertificate.getIssuedBy().getUName();
138                     long startDateLong = currentWebsiteSslCertificate.getValidNotBeforeDate().getTime();
139                     long endDateLong = currentWebsiteSslCertificate.getValidNotAfterDate().getTime();
140
141                     // Update the database.
142                     domainsDatabaseHelper.updateDomainWithCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled,
143                             domStorageEnabled, formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt,
144                             nightModeInt, pinnedSslCertificate, issuedToCommonName, issuedToOrganization, issuedToOrganizationalUnit, issuedByCommonName, issuedByOrganization,
145                             issuedByOrganizationalUnit, startDateLong, endDateLong);
146                 } else {  // No certificate is selected.
147                     // Update the database, with PINNED_SSL_CERTIFICATE set to false.
148                     domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled,
149                             domStorageEnabled, formDataEnabled, easyListEnabled, easyPrivacyEnabled, fanboysAnnoyanceEnabled, fanboysSocialBlockingEnabled, userAgentString, fontSizeInt, displayWebpageImagesInt,
150                             nightModeInt, false);
151                 }
152             }
153
154             // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
155             DomainsActivity.currentDomainDatabaseId = (int) id;
156
157             // Add `currentDomainDatabaseId` to `argumentsBundle`.
158             Bundle argumentsBundle = new Bundle();
159             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
160
161             // Add `argumentsBundle` to `domainSettingsFragment`.
162             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
163             domainSettingsFragment.setArguments(argumentsBundle);
164
165             // Display the domain settings fragment.
166             if (DomainsActivity.twoPanedMode) {  // The device in in two-paned mode.
167                 // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
168                 if (!DomainsActivity.dismissingSnackbar) {
169                     // Enable `deleteMenuItem`.
170                     DomainsActivity.deleteMenuItem.setEnabled(true);
171
172                     // Set the delete icon according to the theme.
173                     if (MainWebViewActivity.darkTheme) {
174                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
175                     } else {
176                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
177                     }
178                 }
179
180                 // Display `domainSettingsFragment`.
181                 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
182             } else { // The device in in single-paned mode
183                 // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
184                 if (!DomainsActivity.dismissingSnackbar) {
185                     DomainsActivity.deleteMenuItem.setVisible(true);
186                 }
187
188                 // Hide `add_domain_fab`.
189                 FloatingActionButton addDomainFAB = getActivity().findViewById(R.id.add_domain_fab);
190                 addDomainFAB.setVisibility(View.GONE);
191
192                 // Display `domainSettingsFragment`.
193                 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
194             }
195         });
196
197         return domainsListFragmentView;
198     }
199 }