]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
Add domains `Snackbars` and ghosting of switches.
[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.design.widget.Snackbar;
28 import android.support.v4.app.NavUtils;
29 import android.support.v7.app.ActionBar;
30 import android.support.v7.app.AppCompatActivity;
31 import android.support.v7.app.AppCompatDialogFragment;
32 import android.support.v7.widget.Toolbar;
33 import android.view.Menu;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.AdapterView;
38 import android.widget.CursorAdapter;
39 import android.widget.EditText;
40 import android.widget.ListView;
41 import android.widget.Spinner;
42 import android.widget.Switch;
43 import android.widget.TextView;
44
45 import com.stoutner.privacybrowser.R;
46 import com.stoutner.privacybrowser.dialogs.AddDomainDialog;
47 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
48 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
49
50 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
51     // `context` is used in `onCreate()` and `onOptionsItemSelected()`.
52     private Context context;
53
54     // `domainsDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
55     private static DomainsDatabaseHelper domainsDatabaseHelper;
56
57     // `twoPaneMode` is used in `onCreate()` and `updateDomainsListView()`.
58     private boolean twoPaneMode;
59
60     // `domainsListView` is used in `onCreate()`, `onOptionsItemSelected()`, and `updateDomainsListView()`.
61     private ListView domainsListView;
62
63     // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
64     private int databaseId;
65
66     // `saveMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
67     private MenuItem saveMenuItem;
68
69     // `deleteMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
70     private MenuItem deleteMenuItem;
71
72     @Override
73     protected void onCreate(Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75         setContentView(R.layout.domains_coordinatorlayout);
76
77         // Get a handle for the context.
78         context = this;
79
80         // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
81         final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
82         setSupportActionBar(bookmarksAppBar);
83
84         // Display the home arrow on `SupportActionBar`.
85         ActionBar appBar = getSupportActionBar();
86         assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
87         appBar.setDisplayHomeAsUpEnabled(true);
88
89         // Initialize the database handler.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
90         // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
91         domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
92
93         // Determine if we are in two pane mode.  `domains_settings_linearlayout` is only populated if two panes are present.
94         twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != null);
95
96         // Initialize `domainsListView`.
97         domainsListView = (ListView) findViewById(R.id.domains_listview);
98
99         domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
100             @Override
101             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
102                 // Convert the id from `long` to `int` to match the format of the domains database.
103                 databaseId = (int) id;
104
105                 // Display the Domain Settings.
106                 if (twoPaneMode) {  // Display a fragment in two paned mode.
107                     // Enable the options `MenuItems`.
108                     saveMenuItem.setEnabled(true);
109                     deleteMenuItem.setEnabled(true);
110
111                     // Store `databaseId` in `argumentsBundle`.
112                     Bundle argumentsBundle = new Bundle();
113                     argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
114
115                     // Add `argumentsBundle` to `domainSettingsFragment`.
116                     DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
117                     domainSettingsFragment.setArguments(argumentsBundle);
118
119                     // Display `domainSettingsFragment`.
120                     getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
121
122                     // Enable the options `MenuItems`.
123                     deleteMenuItem.setEnabled(true);
124                     deleteMenuItem.setIcon(R.drawable.delete);
125                     saveMenuItem.setEnabled(true);
126                 } else { // Load the second activity on smaller screens.
127                     // Create `domainSettingsActivityIntent` with the `databaseId`.
128                     Intent domainSettingsActivityIntent = new Intent(context, DomainSettingsActivity.class);
129                     domainSettingsActivityIntent.putExtra(DomainSettingsFragment.DATABASE_ID, databaseId);
130
131                     // Start `DomainSettingsActivity`.
132                     context.startActivity(domainSettingsActivityIntent);
133                 }
134             }
135         });
136
137         FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
138         addDomainFAB.setOnClickListener(new View.OnClickListener() {
139             @Override
140             public void onClick(View view) {
141                 // Show the `AddDomainDialog` `AlertDialog` and name the instance `@string/add_domain`.
142                 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
143                 addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
144             }
145         });
146     }
147
148     @Override
149     public boolean onCreateOptionsMenu(Menu menu) {
150         // Inflate the menu.
151         getMenuInflater().inflate(R.menu.domains_options_menu, menu);
152
153         // Store the `MenuItems` for future use.
154         deleteMenuItem = menu.findItem(R.id.delete_domain);
155         saveMenuItem = menu.findItem(R.id.save_domain);
156
157         // Only display the options `MenuItems` in two pane mode.
158         deleteMenuItem.setVisible(twoPaneMode);
159         saveMenuItem.setVisible(twoPaneMode);
160
161         // Load the `ListView`.  We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `updateDomainsListView()` needs the `MenuItems` to be inflated.
162         updateDomainsListView();
163
164         // Success!
165         return true;
166     }
167
168     @Override
169     public boolean onOptionsItemSelected(MenuItem menuItem) {
170         // Get the ID of the `MenuItem` that was selected.
171         int menuItemID = menuItem.getItemId();
172
173         switch (menuItemID) {
174             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
175                 // Go home.
176                 NavUtils.navigateUpFromSameTask(this);
177                 break;
178
179             case R.id.save_domain:
180                 // Get handles for the domain settings.
181                 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
182                 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
183                 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
184                 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
185                 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
186                 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
187                 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
188                 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
189                 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
190
191                 // Extract the data for the domain settings.
192                 String domainNameString = domainNameEditText.getText().toString();
193                 boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
194                 boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
195                 boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
196                 boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
197                 boolean formDataEnabled = formDataEnabledSwitch.isChecked();
198                 int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
199                 int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
200
201                 // Get the data for the `Spinners` from the entry values string arrays.
202                 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
203                 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
204
205                 // Check to see if we are using a custom user agent.
206                 if (userAgentString.equals("Custom user agent")) {
207                     // Set `userAgentString` to the custom user agent string.
208                     userAgentString = customUserAgentEditText.getText().toString();
209                 }
210
211                 // Save the domain settings.
212                 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
213
214                 // Display a `Snackbar`.
215                 Snackbar.make(domainsListView, R.string.domain_settings_saved, Snackbar.LENGTH_SHORT).show();
216
217                 // update the domains `ListView`.
218                 updateDomainsListView();
219                 break;
220
221             case R.id.delete_domain:
222                 // Save the `ListView` `currentPosition`.
223                 final int currentPosition = domainsListView.getCheckedItemPosition();
224
225                 // Get a `Cursor` that does not show the domain to be deleted.
226                 Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseId);
227
228                 // Setup `domainsPendingDeleteCursorAdapter` with `this` context.  `false` disables `autoRequery`.
229                 CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
230                     @Override
231                     public View newView(Context context, Cursor cursor, ViewGroup parent) {
232                         // Inflate the individual item layout.  `false` does not attach it to the root.
233                         return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
234                     }
235
236                     @Override
237                     public void bindView(View view, Context context, Cursor cursor) {
238                         // Set the domain name.
239                         String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
240                         TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
241                         domainNameTextView.setText(domainNameString);
242                     }
243                 };
244
245                 // Update the `ListView`.
246                 domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
247
248                 // Detach the domain settings `Fragment`.
249                 getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
250
251                 // Disable the options `MenuItems`.
252                 deleteMenuItem.setEnabled(false);
253                 deleteMenuItem.setIcon(R.drawable.delete_blue);
254                 saveMenuItem.setEnabled(false);
255
256                 // Display a `Snackbar`.
257                 Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
258                         .setAction(R.string.undo, new View.OnClickListener() {
259                             @Override
260                             public void onClick(View v) {
261                                 // Do nothing because everything will be handled by `onDismissed()` below.
262                             }
263                         })
264                         .addCallback(new Snackbar.Callback() {
265                             @Override
266                             public void onDismissed(Snackbar snackbar, int event) {
267                                 switch (event) {
268                                     // The user pushed the `Undo` button.
269                                     case Snackbar.Callback.DISMISS_EVENT_ACTION:
270                                         // Get a `Cursor` with the current contents of the domains database.
271                                         Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
272
273                                         // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
274                                         CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) {
275                                             @Override
276                                             public View newView(Context context, Cursor cursor, ViewGroup parent) {
277                                                 // Inflate the individual item layout.  `false` does not attach it to the root.
278                                                 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
279                                             }
280
281                                             @Override
282                                             public void bindView(View view, Context context, Cursor cursor) {
283                                                 // Set the domain name.
284                                                 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
285                                                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
286                                                 domainNameTextView.setText(domainNameString);
287                                             }
288                                         };
289
290                                         // Update the `ListView`.
291                                         domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
292
293                                         // Select the entry in the domain list at `currentPosition`.
294                                         domainsListView.setItemChecked(currentPosition, true);
295
296                                         // Store `databaseId` in `argumentsBundle`.
297                                         Bundle argumentsBundle = new Bundle();
298                                         argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
299
300                                         // Add `argumentsBundle` to `domainSettingsFragment`.
301                                         DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
302                                         domainSettingsFragment.setArguments(argumentsBundle);
303
304                                         // Display `domainSettingsFragment`.
305                                         getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
306
307                                         // Enable the options `MenuItems`.
308                                         deleteMenuItem.setEnabled(true);
309                                         deleteMenuItem.setIcon(R.drawable.delete);
310                                         saveMenuItem.setEnabled(true);
311                                         break;
312
313                                     // The `Snackbar` was dismissed without the `Undo` button being pushed.
314                                     default:
315                                         // Delete the selected domain.
316                                         domainsDatabaseHelper.deleteDomain(databaseId);
317                                         break;
318                                 }
319                             }
320                         })
321                         .show();
322                 break;
323         }
324
325         // Consume the event.
326         return true;
327     }
328
329     @Override
330     public void onAddDomain(AppCompatDialogFragment dialogFragment) {
331         // Get the `domainNameEditText` from `dialogFragment` and extract the string.
332         EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
333         String domainNameString = domainNameEditText.getText().toString();
334
335         // Create the domain.
336         domainsDatabaseHelper.addDomain(domainNameString);
337
338         // Refresh the `ListView`.
339         updateDomainsListView();
340     }
341
342     private void updateDomainsListView() {
343         // Initialize `currentPosition`.
344         int currentPosition;
345
346         // Store the current position of `domainsListView` if it is already populated.
347         if (domainsListView.getCount() > 0){
348             currentPosition = domainsListView.getCheckedItemPosition();
349         } else {
350             // Set `currentPosition` to 0;
351             currentPosition = 0;
352         }
353
354         // Get a `Cursor` with the current contents of the domains database.
355         Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
356
357         // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
358         CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
359             @Override
360             public View newView(Context context, Cursor cursor, ViewGroup parent) {
361                 // Inflate the individual item layout.  `false` does not attach it to the root.
362                 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
363             }
364
365             @Override
366             public void bindView(View view, Context context, Cursor cursor) {
367                 // Set the domain name.
368                 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
369                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
370                 domainNameTextView.setText(domainNameString);
371             }
372         };
373
374         // Update the `ListView`.
375         domainsListView.setAdapter(domainsCursorAdapter);
376
377         // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
378         if (twoPaneMode && (domainsCursor.getCount() > 0)) {
379             // Select the entry in the domain list at `currentPosition`.
380             domainsListView.setItemChecked(currentPosition, true);
381
382             // Get the `databaseId` for `currentPosition`.
383             domainsCursor.moveToPosition(currentPosition);
384             databaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
385
386             // Store `databaseId` in `argumentsBundle`.
387             Bundle argumentsBundle = new Bundle();
388             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
389
390             // Add `argumentsBundle` to `domainSettingsFragment`.
391             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
392             domainSettingsFragment.setArguments(argumentsBundle);
393
394             // Display `domainSettingsFragment`.
395             getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
396
397             // Enable the options `MenuItems`.
398             deleteMenuItem.setEnabled(true);
399             deleteMenuItem.setIcon(R.drawable.delete);
400             saveMenuItem.setEnabled(true);
401         } else {
402             // Disable the options `MenuItems`.
403             deleteMenuItem.setEnabled(false);
404             deleteMenuItem.setIcon(R.drawable.delete_blue);
405             saveMenuItem.setEnabled(false);
406         }
407     }
408 }