2 * Copyright © 2017 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.activities;
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;
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;
50 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
51 // `context` is used in `onCreate()` and `onOptionsItemSelected()`.
52 private Context context;
54 // `domainsDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
55 private static DomainsDatabaseHelper domainsDatabaseHelper;
57 // `twoPaneMode` is used in `onCreate()` and `updateDomainsListView()`.
58 private boolean twoPaneMode;
60 // `domainsListView` is used in `onCreate()`, `onOptionsItemSelected()`, and `updateDomainsListView()`.
61 private ListView domainsListView;
63 // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
64 private int databaseId;
66 // `saveMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
67 private MenuItem saveMenuItem;
69 // `deleteMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
70 private MenuItem deleteMenuItem;
73 protected void onCreate(Bundle savedInstanceState) {
74 super.onCreate(savedInstanceState);
75 setContentView(R.layout.domains_coordinatorlayout);
77 // Get a handle for the context.
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);
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);
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);
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);
96 // Initialize `domainsListView`.
97 domainsListView = (ListView) findViewById(R.id.domains_listview);
99 domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
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;
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);
111 // Store `databaseId` in `argumentsBundle`.
112 Bundle argumentsBundle = new Bundle();
113 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
115 // Add `argumentsBundle` to `domainSettingsFragment`.
116 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
117 domainSettingsFragment.setArguments(argumentsBundle);
119 // Display `domainSettingsFragment`.
120 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
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);
131 // Start `DomainSettingsActivity`.
132 context.startActivity(domainSettingsActivityIntent);
137 FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
138 addDomainFAB.setOnClickListener(new View.OnClickListener() {
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));
149 public boolean onCreateOptionsMenu(Menu menu) {
151 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
153 // Store the `MenuItems` for future use.
154 deleteMenuItem = menu.findItem(R.id.delete_domain);
155 saveMenuItem = menu.findItem(R.id.save_domain);
157 // Only display the options `MenuItems` in two pane mode.
158 deleteMenuItem.setVisible(twoPaneMode);
159 saveMenuItem.setVisible(twoPaneMode);
161 // Load the `ListView`. We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `updateDomainsListView()` needs the `MenuItems` to be inflated.
162 updateDomainsListView();
169 public boolean onOptionsItemSelected(MenuItem menuItem) {
170 // Get the ID of the `MenuItem` that was selected.
171 int menuItemID = menuItem.getItemId();
173 switch (menuItemID) {
174 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
176 NavUtils.navigateUpFromSameTask(this);
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);
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();
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]);
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();
211 // Save the domain settings.
212 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
214 // Display a `Snackbar`.
215 Snackbar.make(domainsListView, R.string.domain_settings_saved, Snackbar.LENGTH_SHORT).show();
217 // update the domains `ListView`.
218 updateDomainsListView();
221 case R.id.delete_domain:
222 // Save the `ListView` `currentPosition`.
223 final int currentPosition = domainsListView.getCheckedItemPosition();
225 // Get a `Cursor` that does not show the domain to be deleted.
226 Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseId);
228 // Setup `domainsPendingDeleteCursorAdapter` with `this` context. `false` disables `autoRequery`.
229 CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
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);
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);
245 // Update the `ListView`.
246 domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
248 // Detach the domain settings `Fragment`.
249 getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
251 // Disable the options `MenuItems`.
252 deleteMenuItem.setEnabled(false);
253 deleteMenuItem.setIcon(R.drawable.delete_blue);
254 saveMenuItem.setEnabled(false);
256 // Display a `Snackbar`.
257 Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
258 .setAction(R.string.undo, new View.OnClickListener() {
260 public void onClick(View v) {
261 // Do nothing because everything will be handled by `onDismissed()` below.
264 .addCallback(new Snackbar.Callback() {
266 public void onDismissed(Snackbar snackbar, int 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();
273 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
274 CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) {
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);
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);
290 // Update the `ListView`.
291 domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
293 // Select the entry in the domain list at `currentPosition`.
294 domainsListView.setItemChecked(currentPosition, true);
296 // Store `databaseId` in `argumentsBundle`.
297 Bundle argumentsBundle = new Bundle();
298 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
300 // Add `argumentsBundle` to `domainSettingsFragment`.
301 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
302 domainSettingsFragment.setArguments(argumentsBundle);
304 // Display `domainSettingsFragment`.
305 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
307 // Enable the options `MenuItems`.
308 deleteMenuItem.setEnabled(true);
309 deleteMenuItem.setIcon(R.drawable.delete);
310 saveMenuItem.setEnabled(true);
313 // The `Snackbar` was dismissed without the `Undo` button being pushed.
315 // Delete the selected domain.
316 domainsDatabaseHelper.deleteDomain(databaseId);
325 // Consume the event.
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();
335 // Create the domain.
336 domainsDatabaseHelper.addDomain(domainNameString);
338 // Refresh the `ListView`.
339 updateDomainsListView();
342 private void updateDomainsListView() {
343 // Initialize `currentPosition`.
346 // Store the current position of `domainsListView` if it is already populated.
347 if (domainsListView.getCount() > 0){
348 currentPosition = domainsListView.getCheckedItemPosition();
350 // Set `currentPosition` to 0;
354 // Get a `Cursor` with the current contents of the domains database.
355 Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
357 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
358 CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
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);
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);
374 // Update the `ListView`.
375 domainsListView.setAdapter(domainsCursorAdapter);
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);
382 // Get the `databaseId` for `currentPosition`.
383 domainsCursor.moveToPosition(currentPosition);
384 databaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
386 // Store `databaseId` in `argumentsBundle`.
387 Bundle argumentsBundle = new Bundle();
388 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
390 // Add `argumentsBundle` to `domainSettingsFragment`.
391 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
392 domainSettingsFragment.setArguments(argumentsBundle);
394 // Display `domainSettingsFragment`.
395 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
397 // Enable the options `MenuItems`.
398 deleteMenuItem.setEnabled(true);
399 deleteMenuItem.setIcon(R.drawable.delete);
400 saveMenuItem.setEnabled(true);
402 // Disable the options `MenuItems`.
403 deleteMenuItem.setEnabled(false);
404 deleteMenuItem.setIcon(R.drawable.delete_blue);
405 saveMenuItem.setEnabled(false);