]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
Add color changes to domain settings icons.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainsActivity.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.activities;
21
22 import android.content.Context;
23 import android.content.Intent;
24 import android.database.Cursor;
25 import android.os.Bundle;
26 import android.support.design.widget.FloatingActionButton;
27 import android.support.v4.app.NavUtils;
28 import android.support.v7.app.ActionBar;
29 import android.support.v7.app.AppCompatActivity;
30 import android.support.v7.app.AppCompatDialogFragment;
31 import android.support.v7.widget.Toolbar;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.AdapterView;
37 import android.widget.CursorAdapter;
38 import android.widget.EditText;
39 import android.widget.ListView;
40 import android.widget.Spinner;
41 import android.widget.Switch;
42 import android.widget.TextView;
43
44 import com.stoutner.privacybrowser.R;
45 import com.stoutner.privacybrowser.dialogs.AddDomainDialog;
46 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
47 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
48
49 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
50     // `context` is used in `onCreate()` and `onOptionsItemSelected()`.
51     Context context;
52
53     // `domainsDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
54     private static DomainsDatabaseHelper domainsDatabaseHelper;
55
56     // `twoPaneMode` is used in `onCreate()` and `updateDomainsListView()`.
57     private boolean twoPaneMode;
58
59     // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
60     private ListView domainsListView;
61
62     // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
63     private int databaseId;
64
65     // `saveMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
66     private MenuItem saveMenuItem;
67
68     // `deleteMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
69     private MenuItem deleteMenuItem;
70
71     @Override
72     protected void onCreate(Bundle savedInstanceState) {
73         super.onCreate(savedInstanceState);
74         setContentView(R.layout.domains_coordinatorlayout);
75
76         // Get a handle for the context.
77         context = this;
78
79         // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
80         final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
81         setSupportActionBar(bookmarksAppBar);
82
83         // Display the home arrow on `SupportActionBar`.
84         ActionBar appBar = getSupportActionBar();
85         assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
86         appBar.setDisplayHomeAsUpEnabled(true);
87
88         // Initialize the database handler.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
89         // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
90         domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
91
92         // Determine if we are in two pane mode.  `domains_settings_linearlayout` is only populated if two panes are present.
93         twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != null);
94
95         // Initialize `domainsListView`.
96         domainsListView = (ListView) findViewById(R.id.domains_listview);
97
98         domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
99             @Override
100             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
101                 // Convert the id from `long` to `int` to match the format of the domains database.
102                 databaseId = (int) id;
103
104                 // Display the Domain Settings.
105                 if (twoPaneMode) {  // Display a fragment in two paned mode.
106                     // Enable the options `MenuItems`.
107                     saveMenuItem.setEnabled(true);
108                     deleteMenuItem.setEnabled(true);
109
110                     // Store `databaseId` in `argumentsBundle`.
111                     Bundle argumentsBundle = new Bundle();
112                     argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
113
114                     // Add `argumentsBundle` to `domainSettingsFragment`.
115                     DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
116                     domainSettingsFragment.setArguments(argumentsBundle);
117
118                     // Display `domainSettingsFragment`.
119                     getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
120                 } else { // Load the second activity on smaller screens.
121                     // Create `domainSettingsActivityIntent` with the `databaseId`.
122                     Intent domainSettingsActivityIntent = new Intent(context, DomainSettingsActivity.class);
123                     domainSettingsActivityIntent.putExtra(DomainSettingsFragment.DATABASE_ID, databaseId);
124
125                     // Start `DomainSettingsActivity`.
126                     context.startActivity(domainSettingsActivityIntent);
127                 }
128             }
129         });
130
131         FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
132         addDomainFAB.setOnClickListener(new View.OnClickListener() {
133             @Override
134             public void onClick(View view) {
135                 // Show the `AddDomainDialog` `AlertDialog` and name the instance `@string/add_domain`.
136                 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
137                 addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
138             }
139         });
140     }
141
142     @Override
143     public boolean onCreateOptionsMenu(Menu menu) {
144         // Inflate the menu.
145         getMenuInflater().inflate(R.menu.domains_options_menu, menu);
146
147         // Store the `MenuItems` for future use.
148         deleteMenuItem = menu.findItem(R.id.delete_domain);
149         saveMenuItem = menu.findItem(R.id.save_domain);
150
151         // Only display the options `MenuItems` in two pane mode.
152         deleteMenuItem.setVisible(twoPaneMode);
153         saveMenuItem.setVisible(twoPaneMode);
154
155         // Load the `ListView`.  We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `updateDomainsListView()` needs the `MenuItems` to be inflated.
156         updateDomainsListView();
157
158         // Success!
159         return true;
160     }
161
162     @Override
163     public boolean onOptionsItemSelected(MenuItem menuItem) {
164         // Get the ID of the `MenuItem` that was selected.
165         int menuItemID = menuItem.getItemId();
166
167         switch (menuItemID) {
168             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
169                 // Go home.
170                 NavUtils.navigateUpFromSameTask(this);
171                 break;
172
173             case R.id.save_domain:
174                 // Get handles for the domain settings.
175                 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
176                 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
177                 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
178                 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
179                 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
180                 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
181                 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
182                 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
183                 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
184
185                 // Extract the data for the domain settings.
186                 String domainNameString = domainNameEditText.getText().toString();
187                 boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
188                 boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
189                 boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
190                 boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
191                 boolean formDataEnabled = formDataEnabledSwitch.isChecked();
192                 int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
193                 int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
194
195                 // Get the data for the `Spinners` from the entry values string arrays.
196                 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
197                 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
198
199                 // Check to see if we are using a custom user agent.
200                 if (userAgentString.equals("Custom user agent")) {
201                     // Set `userAgentString` to the custom user agent string.
202                     userAgentString = customUserAgentEditText.getText().toString();
203                 }
204
205                 // Save the domain settings.
206                 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
207                 break;
208
209             case R.id.delete_domain:
210                 // Delete the selected domain.
211                 domainsDatabaseHelper.deleteDomain(databaseId);
212
213                 // Detach the domain settings fragment.
214                 getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
215
216                 // Update the `ListView`.
217                 updateDomainsListView();
218                 break;
219         }
220         return true;
221     }
222
223     @Override
224     public void onAddDomain(AppCompatDialogFragment dialogFragment) {
225         // Get the `domainNameEditText` from `dialogFragment` and extract the string.
226         EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
227         String domainNameString = domainNameEditText.getText().toString();
228
229         // Create the domain.
230         domainsDatabaseHelper.addDomain(domainNameString);
231
232         // Refresh the `ListView`.
233         updateDomainsListView();
234     }
235
236     private void updateDomainsListView() {
237         // Get a `Cursor` with the current contents of the domains database.
238         Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
239
240         // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
241         CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
242             @Override
243             public View newView(Context context, Cursor cursor, ViewGroup parent) {
244                 // Inflate the individual item layout.  `false` does not attach it to the root.
245                 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
246             }
247
248             @Override
249             public void bindView(View view, Context context, Cursor cursor) {
250                 // Set the domain name.
251                 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
252                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
253                 domainNameTextView.setText(domainNameString);
254             }
255         };
256
257         // Update the `ListView`.
258         domainsListView.setAdapter(domainsCursorAdapter);
259
260         // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
261         if (twoPaneMode && (domainsCursor.getCount() > 0)) {
262             // Select the first domain.
263             domainsListView.setItemChecked(0, true);
264
265             // Get the `databaseId` of the first item.
266             domainsCursor.moveToFirst();
267             databaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
268
269             // Store `databaseId` in `argumentsBundle`.
270             Bundle argumentsBundle = new Bundle();
271             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
272
273             // Add `argumentsBundle` to `domainSettingsFragment`.
274             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
275             domainSettingsFragment.setArguments(argumentsBundle);
276
277             // Display `domainSettingsFragment`.
278             getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
279
280             // Enable the options `MenuItems`.
281             deleteMenuItem.setEnabled(true);
282             deleteMenuItem.setIcon(R.drawable.delete);
283             saveMenuItem.setEnabled(true);
284         } else {
285             // Disable the options `MenuItems`.
286             deleteMenuItem.setEnabled(false);
287             deleteMenuItem.setIcon(R.drawable.delete_blue);
288             saveMenuItem.setEnabled(false);
289         }
290     }
291 }