]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java
Finish creating the bookmark drawer. https://redmine.stoutner.com/issues/132
[PrivacyBrowserAndroid.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 = (ListView) 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(new AdapterView.OnItemClickListener() {
58             @Override
59             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
60                 // Dismiss `undoDeleteSnackbar` if it is currently displayed (because a domain has just been deleted).
61                 if ((DomainsActivity.undoDeleteSnackbar != null) && (DomainsActivity.undoDeleteSnackbar.isShown())) {
62                     DomainsActivity.dismissingSnackbar = true;
63
64                     DomainsActivity.undoDeleteSnackbar.dismiss();
65                 }
66
67                 // Save the current domain settings if operating in two-paned mode and a domain is currently selected.
68                 if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) {
69                     View domainSettingsFragmentView = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container).getView();
70                     assert domainSettingsFragmentView != null;
71
72                     // Get handles for the domain settings.
73                     EditText domainNameEditText = (EditText) domainSettingsFragmentView.findViewById(R.id.domain_settings_name_edittext);
74                     Switch javaScriptEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_javascript_switch);
75                     Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_first_party_cookies_switch);
76                     Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_third_party_cookies_switch);
77                     Switch domStorageEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_dom_storage_switch);
78                     Switch formDataEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_form_data_switch);
79                     Spinner userAgentSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_user_agent_spinner);
80                     EditText customUserAgentEditText = (EditText) domainSettingsFragmentView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
81                     Spinner fontSizeSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_font_size_spinner);
82                     Spinner displayWebpageImagesSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_display_webpage_images_spinner);
83                     Spinner nightModeSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_night_mode_spinner);
84                     Switch pinnedSslCertificateSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_pinned_ssl_certificate_switch);
85                     RadioButton savedSslCertificateRadioButton = (RadioButton) domainSettingsFragmentView.findViewById(R.id.saved_ssl_certificate_radiobutton);
86                     RadioButton currentWebsiteCertificateRadioButton = (RadioButton) domainSettingsFragmentView.findViewById(R.id.current_website_certificate_radiobutton);
87
88                     // Extract the data for the domain settings.
89                     String domainNameString = domainNameEditText.getText().toString();
90                     boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
91                     boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
92                     boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
93                     boolean domStorageEnabledEnabledBoolean  = domStorageEnabledSwitch.isChecked();
94                     boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
95                     int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
96                     int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
97                     int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
98                     int nightModeInt = nightModeSpinner.getSelectedItemPosition();
99                     boolean pinnedSslCertificate = pinnedSslCertificateSwitch.isChecked();
100
101                     // Get the data for the `Spinners` from the entry values string arrays.
102                     String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
103                     int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
104
105                     // Check to see if we are using a custom user agent.
106                     if (userAgentString.equals("Custom user agent")) {
107                         // Set `userAgentString` to the custom user agent string.
108                         userAgentString = customUserAgentEditText.getText().toString();
109                     }
110
111                     // Save the domain settings.
112                     if (savedSslCertificateRadioButton.isChecked()) {  // The current certificate is being used.
113                         // Update the database except for the certificate.
114                         domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean,
115                                 formDataEnabledBoolean, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, pinnedSslCertificate);
116                     } else if (currentWebsiteCertificateRadioButton.isChecked()) {  // The certificate is being updated with the current website certificate.
117                         // Get the current website SSL certificate.
118                         SslCertificate currentWebsiteSslCertificate = MainWebViewActivity.sslCertificate;
119
120                         // Store the values from the SSL certificate.
121                         String issuedToCommonName = currentWebsiteSslCertificate.getIssuedTo().getCName();
122                         String issuedToOrganization = currentWebsiteSslCertificate.getIssuedTo().getOName();
123                         String issuedToOrganizationalUnit = currentWebsiteSslCertificate.getIssuedTo().getUName();
124                         String issuedByCommonName = currentWebsiteSslCertificate.getIssuedBy().getCName();
125                         String issuedByOrganization = currentWebsiteSslCertificate.getIssuedBy().getOName();
126                         String issuedByOrganizationalUnit = currentWebsiteSslCertificate.getIssuedBy().getUName();
127                         long startDateLong = currentWebsiteSslCertificate.getValidNotBeforeDate().getTime();
128                         long endDateLong = currentWebsiteSslCertificate.getValidNotAfterDate().getTime();
129
130                         // Update the database.
131                         domainsDatabaseHelper.updateDomainWithCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean,
132                                 formDataEnabledBoolean, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, pinnedSslCertificate, issuedToCommonName, issuedToOrganization, issuedToOrganizationalUnit, issuedByCommonName, issuedByOrganization,
133                                 issuedByOrganizationalUnit, startDateLong, endDateLong);
134                     } else {  // No certificate is selected.
135                         // Update the database, with PINNED_SSL_CERTIFICATE set to false.
136                         domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean,
137                                 formDataEnabledBoolean, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, false);
138                     }
139                 }
140
141                 // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
142                 DomainsActivity.currentDomainDatabaseId = (int) id;
143
144                 // Add `currentDomainDatabaseId` to `argumentsBundle`.
145                 Bundle argumentsBundle = new Bundle();
146                 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
147
148                 // Add `argumentsBundle` to `domainSettingsFragment`.
149                 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
150                 domainSettingsFragment.setArguments(argumentsBundle);
151
152                 // Display the domain settings fragment.
153                 if (DomainsActivity.twoPanedMode) {  // The device in in two-paned mode.
154                     // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
155                     if (!DomainsActivity.dismissingSnackbar) {
156                         // Enable `deleteMenuItem`.
157                         DomainsActivity.deleteMenuItem.setEnabled(true);
158
159                         // Set the delete icon according to the theme.
160                         if (MainWebViewActivity.darkTheme) {
161                             DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
162                         } else {
163                             DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
164                         }
165                     }
166
167                     // Display `domainSettingsFragment`.
168                     supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
169                 } else { // The device in in single-paned mode
170                     // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
171                     if (!DomainsActivity.dismissingSnackbar) {
172                         DomainsActivity.deleteMenuItem.setVisible(true);
173                     }
174
175                     // Hide `add_domain_fab`.
176                     FloatingActionButton addDomainFAB = (FloatingActionButton) getActivity().findViewById(R.id.add_domain_fab);
177                     addDomainFAB.setVisibility(View.GONE);
178
179                     // Display `domainSettingsFragment`.
180                     supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
181                 }
182             }
183         });
184
185         return domainsListFragmentView;
186     }
187 }