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