]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java
Load the new domain after it is created in two-paned mode. Implements https://redmin...
[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.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                 // Save the current domain settings if operating in two-paned mode a domain is currently selected.
59                 if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) {
60                     View domainSettingsFragmentView = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container).getView();
61                     assert domainSettingsFragmentView != null;
62
63                     // Get handles for the domain settings.
64                     EditText domainNameEditText = (EditText) domainSettingsFragmentView.findViewById(R.id.domain_settings_name_edittext);
65                     Switch javaScriptEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_javascript_switch);
66                     Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_first_party_cookies_switch);
67                     Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_third_party_cookies_switch);
68                     Switch domStorageEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_dom_storage_switch);
69                     Switch formDataEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_form_data_switch);
70                     Spinner userAgentSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_user_agent_spinner);
71                     EditText customUserAgentEditText = (EditText) domainSettingsFragmentView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
72                     Spinner fontSizeSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_font_size_spinner);
73                     Spinner displayWebpageImagesSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_display_webpage_images_spinner);
74
75                     // Extract the data for the domain settings.
76                     String domainNameString = domainNameEditText.getText().toString();
77                     boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
78                     boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
79                     boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
80                     boolean domStorageEnabledEnabledBoolean  = domStorageEnabledSwitch.isChecked();
81                     boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
82                     int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
83                     int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
84                     int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
85
86                     // Get the data for the `Spinners` from the entry values string arrays.
87                     String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
88                     int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
89
90                     // Check to see if we are using a custom user agent.
91                     if (userAgentString.equals("Custom user agent")) {
92                         // Set `userAgentString` to the custom user agent string.
93                         userAgentString = customUserAgentEditText.getText().toString();
94                     }
95
96                     // Save the domain settings.
97                     domainsDatabaseHelper.saveDomain(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean, userAgentString, fontSizeInt,
98                             displayWebpageImagesInt);
99                 }
100
101                 // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
102                 DomainsActivity.currentDomainDatabaseId = (int) id;
103
104                 // Add `currentDomainDatabaseId` to `argumentsBundle`.
105                 Bundle argumentsBundle = new Bundle();
106                 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
107
108                 // Add `argumentsBundle` to `domainSettingsFragment`.
109                 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
110                 domainSettingsFragment.setArguments(argumentsBundle);
111
112                 // Display the domain settings fragment.
113                 if (DomainsActivity.twoPanedMode) {  // The device in in two-paned mode.
114                     // Enable the options `MenuItems`.
115                     DomainsActivity.deleteMenuItem.setEnabled(true);
116
117                     // Set the delete icon according to the theme.
118                     if (MainWebViewActivity.darkTheme) {
119                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
120                     } else {
121                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
122                     }
123
124                     // Display `domainSettingsFragment`.
125                     supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
126                 } else { // The device in in single-paned mode
127                     // Hide `add_domain_fab`.
128                     FloatingActionButton addDomainFAB = (FloatingActionButton) getActivity().findViewById(R.id.add_domain_fab);
129                     addDomainFAB.setVisibility(View.GONE);
130
131                     // Show and enable `deleteMenuItem`.
132                     DomainsActivity.deleteMenuItem.setVisible(true);
133
134                     // Set `domainSettingsFragmentDisplayed`.
135                     DomainsActivity.domainSettingsFragmentDisplayed = true;
136
137                     // Display `domainSettingsFragment`.
138                     supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
139                 }
140             }
141         });
142
143         return domainsListFragmentView;
144     }
145 }