Add domains `Snackbars` and ghosting of switches.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainSettingsActivity.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.app.Activity;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.support.design.widget.BaseTransientBottomBar;
26 import android.support.design.widget.CoordinatorLayout;
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.widget.Toolbar;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.widget.EditText;
36 import android.widget.Spinner;
37 import android.widget.Switch;
38
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
41 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
42
43 public class DomainSettingsActivity extends AppCompatActivity {
44     // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
45     int databaseId;
46
47     @Override
48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         setContentView(R.layout.domain_settings_coordinatorlayout);
51
52         // We ned to use `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
53         Toolbar domainSettingsAppBar = (Toolbar) findViewById(R.id.domain_settings_toolbar);
54         setSupportActionBar(domainSettingsAppBar);
55
56         // Display the home arrow on `appBar`.
57         final ActionBar appBar = getSupportActionBar();
58         assert appBar != null;  // This assert removes the incorrect lint warning in Android Studio on the following line that `appBar` might be `null`.
59         appBar.setDisplayHomeAsUpEnabled(true);
60
61         // Get the intent that started the activity.
62         final Intent launchingIntent = getIntent();
63
64         // Extract the `databaseID`.  The default value is `1`.
65         databaseId = launchingIntent.getIntExtra(DomainSettingsFragment.DATABASE_ID, 1);
66
67         // Store `databaseId` in `argumentsBundle`.
68         Bundle argumentsBundle = new Bundle();
69         argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
70
71         // Add `argumentsBundle` to `domainSettingsFragment`.
72         DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
73         domainSettingsFragment.setArguments(argumentsBundle);
74
75         // Display `domainSettingsFragment`.
76         getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
77     }
78
79     @Override
80     public boolean onCreateOptionsMenu(Menu menu) {
81         // Inflate the menu.
82         getMenuInflater().inflate(R.menu.domains_options_menu, menu);
83
84         // Success!
85         return true;
86     }
87
88     @Override
89     public boolean onOptionsItemSelected(MenuItem menuItem) {
90         // Get the ID of the `MenuItem` that was selected.
91         int menuItemID = menuItem.getItemId();
92
93         // Initialize the database handler.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
94         // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
95         final DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getApplicationContext(), null, null, 0);
96
97         switch (menuItemID) {
98             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
99                 // Go home.
100                 NavUtils.navigateUpFromSameTask(this);
101                 break;
102
103             case R.id.save_domain:
104                 // Get handles for the domain settings.
105                 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
106                 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
107                 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
108                 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
109                 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
110                 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
111                 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
112                 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
113                 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
114
115                 // Extract the data for the domain settings.
116                 String domainNameString = domainNameEditText.getText().toString();
117                 boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
118                 boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
119                 boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
120                 boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
121                 boolean formDataEnabled = formDataEnabledSwitch.isChecked();
122                 int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
123                 int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
124
125                 // Get the data for the `Spinners` from the entry values string arrays.
126                 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
127                 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
128
129                 // Check to see if we are using a custom user agent.
130                 if (userAgentString.equals("Custom user agent")) {
131                     // Set `userAgentString` to the custom user agent string.
132                     userAgentString = customUserAgentEditText.getText().toString();
133                 }
134
135                 // Save the domain settings.
136                 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
137
138                 // Navigate to `DomainsActivity`.
139                 NavUtils.navigateUpFromSameTask(this);
140                 break;
141
142             case R.id.delete_domain:
143                 // Get a handle for the current activity.
144                 final Activity activity = this;
145
146                 // Get a handle for `domain_settings_coordinatorlayout` so we can display a `SnackBar` later.
147                 CoordinatorLayout domainSettingsCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.domain_settings_coordinatorlayout);
148
149                 // Detach `domain_settings_scrollview`.
150                 getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
151
152                 Snackbar.make(domainSettingsCoordinatorLayout, R.string.domain_deleted, Snackbar.LENGTH_SHORT)
153                         .setAction(R.string.undo, new View.OnClickListener() {
154                             @Override
155                             public void onClick(View v) {
156                                 // Do nothing because everything will be handled by `onDismiss` below.
157                             }
158                         })
159                         .addCallback(new Snackbar.Callback() {
160                             @Override
161                             public void onDismissed(Snackbar snackbar, int event) {
162                                 switch (event) {
163                                     // The user pushed the `undo` button.
164                                     case Snackbar.Callback.DISMISS_EVENT_ACTION:
165                                         // Store `databaseId` in `argumentsBundle`.
166                                         Bundle argumentsBundle = new Bundle();
167                                         argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
168
169                                         // Add `argumentsBundle` to `domainSettingsFragment`.
170                                         DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
171                                         domainSettingsFragment.setArguments(argumentsBundle);
172
173                                         // Display `domainSettingsFragment`.
174                                         getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
175                                         break;
176
177                                     // The `Snackbar` was dismissed without the `Undo` button being pushed.
178                                     default:
179                                         // Delete the selected domain.
180                                         domainsDatabaseHelper.deleteDomain(databaseId);
181
182                                         // Navigate to `DomainsActivity`.
183                                         NavUtils.navigateUpFromSameTask(activity);
184                                         break;
185                                 }
186                             }
187                         })
188                         .show();
189                 break;
190         }
191         return true;
192     }
193 }