Wait to enable the delete icon until the previous domain has been deleted. Fixes...
[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.os.Bundle;
23 import android.support.design.widget.FloatingActionButton;
24 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23.  Otherwise we cannot call `getContext()`.
25 import android.support.v4.app.Fragment;
26 import android.support.v4.app.FragmentManager;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.AdapterView;
31 import android.widget.EditText;
32 import android.widget.ListView;
33 import android.widget.Spinner;
34 import android.widget.Switch;
35
36 import com.stoutner.privacybrowser.R;
37 import com.stoutner.privacybrowser.activities.DomainsActivity;
38 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
40
41 public class DomainsListFragment extends Fragment {
42     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
43         // Inflate `domains_list_fragment`.  `false` does not attach it to the root `container`.
44         View domainsListFragmentView = inflater.inflate(R.layout.domains_list_fragment, container, false);
45
46         // Initialize `domainsListView`.
47         ListView domainsListView = (ListView) domainsListFragmentView.findViewById(R.id.domains_listview);
48
49         // 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`.
50         final DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
51
52         // Get a handle for `supportFragmentManager`.
53         final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
54
55         domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
56             @Override
57             public void onItemClick(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 = (EditText) domainSettingsFragmentView.findViewById(R.id.domain_settings_name_edittext);
72                     Switch javaScriptEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_javascript_switch);
73                     Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_first_party_cookies_switch);
74                     Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_third_party_cookies_switch);
75                     Switch domStorageEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_dom_storage_switch);
76                     Switch formDataEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_form_data_switch);
77                     Spinner userAgentSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_user_agent_spinner);
78                     EditText customUserAgentEditText = (EditText) domainSettingsFragmentView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
79                     Spinner fontSizeSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_font_size_spinner);
80                     Spinner displayWebpageImagesSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_display_webpage_images_spinner);
81
82                     // Extract the data for the domain settings.
83                     String domainNameString = domainNameEditText.getText().toString();
84                     boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
85                     boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
86                     boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
87                     boolean domStorageEnabledEnabledBoolean  = domStorageEnabledSwitch.isChecked();
88                     boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
89                     int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
90                     int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
91                     int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
92
93                     // Get the data for the `Spinners` from the entry values string arrays.
94                     String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
95                     int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
96
97                     // Check to see if we are using a custom user agent.
98                     if (userAgentString.equals("Custom user agent")) {
99                         // Set `userAgentString` to the custom user agent string.
100                         userAgentString = customUserAgentEditText.getText().toString();
101                     }
102
103                     // Save the domain settings.
104                     domainsDatabaseHelper.saveDomain(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean, userAgentString, fontSizeInt,
105                             displayWebpageImagesInt);
106                 }
107
108                 // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
109                 DomainsActivity.currentDomainDatabaseId = (int) id;
110
111                 // Add `currentDomainDatabaseId` to `argumentsBundle`.
112                 Bundle argumentsBundle = new Bundle();
113                 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
114
115                 // Add `argumentsBundle` to `domainSettingsFragment`.
116                 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
117                 domainSettingsFragment.setArguments(argumentsBundle);
118
119                 // Display the domain settings fragment.
120                 if (DomainsActivity.twoPanedMode) {  // The device in in two-paned mode.
121                     // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
122                     if (!DomainsActivity.dismissingSnackbar) {
123                         // Enable `deleteMenuItem`.
124                         DomainsActivity.deleteMenuItem.setEnabled(true);
125
126                         // Set the delete icon according to the theme.
127                         if (MainWebViewActivity.darkTheme) {
128                             DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
129                         } else {
130                             DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
131                         }
132                     }
133
134                     // Display `domainSettingsFragment`.
135                     supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
136                 } else { // The device in in single-paned mode
137                     // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
138                     if (!DomainsActivity.dismissingSnackbar) {
139                         DomainsActivity.deleteMenuItem.setVisible(true);
140                     }
141
142                     // Hide `add_domain_fab`.
143                     FloatingActionButton addDomainFAB = (FloatingActionButton) getActivity().findViewById(R.id.add_domain_fab);
144                     addDomainFAB.setVisibility(View.GONE);
145
146                     // Set `domainSettingsFragmentDisplayed`.
147                     DomainsActivity.domainSettingsFragmentDisplayed = true;
148
149                     // Display `domainSettingsFragment`.
150                     supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
151                 }
152             }
153         });
154
155         return domainsListFragmentView;
156     }
157 }