/*
- * Copyright 2017 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2017 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
*
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
+import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
// `context` is used in `onCreate()` and `onOptionsItemSelected()`.
- Context context;
+ private Context context;
// `domainsDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
private static DomainsDatabaseHelper domainsDatabaseHelper;
- // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
+ // `twoPaneMode` is used in `onCreate()` and `updateDomainsListView()`.
+ private boolean twoPaneMode;
+
+ // `domainsListView` is used in `onCreate()`, `onOptionsItemSelected()`, and `updateDomainsListView()`.
private ListView domainsListView;
// `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
@Override
protected void onCreate(Bundle savedInstanceState) {
+ // Set the activity theme.
+ if (MainWebViewActivity.darkTheme) {
+ setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
+ } else {
+ setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
+ }
+
+ // Run the default commands.
super.onCreate(savedInstanceState);
+
+ // Set the content view.
setContentView(R.layout.domains_coordinatorlayout);
// Get a handle for the context.
context = this;
// We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
- final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
- setSupportActionBar(bookmarksAppBar);
+ final Toolbar domainsAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
+ setSupportActionBar(domainsAppBar);
// Display the home arrow on `SupportActionBar`.
ActionBar appBar = getSupportActionBar();
// The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
- // Determine if we are in two pane mode. `domains_settings_linearlayout` is only populated if two panes are present.
- final boolean twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != null);
+ // Determine if we are in two pane mode. `domains_settings_scrollview` is only populated if two panes are present.
+ twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != null);
// Initialize `domainsListView`.
domainsListView = (ListView) findViewById(R.id.domains_listview);
// Display the Domain Settings.
if (twoPaneMode) { // Display a fragment in two paned mode.
- // Display the options `MenuItems`.
- saveMenuItem.setVisible(true);
- deleteMenuItem.setVisible(true);
-
// Store `databaseId` in `argumentsBundle`.
Bundle argumentsBundle = new Bundle();
argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
}
});
-
- // Load the `ListView`.
- updateDomainsListView();
}
@Override
getMenuInflater().inflate(R.menu.domains_options_menu, menu);
// Store the `MenuItems` for future use.
- saveMenuItem = menu.findItem(R.id.save_domain);
deleteMenuItem = menu.findItem(R.id.delete_domain);
+ saveMenuItem = menu.findItem(R.id.save_domain);
+
+ // Only display the options `MenuItems` in two pane mode.
+ deleteMenuItem.setVisible(twoPaneMode);
+ saveMenuItem.setVisible(twoPaneMode);
+
+ // Load the `ListView`. We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `updateDomainsListView()` needs the `MenuItems` to be inflated.
+ updateDomainsListView();
// Success!
return true;
Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
+ Spinner displayWebpageImagesSpinner = (Spinner) findViewById(R.id.domain_settings_display_webpage_images_spinner);
// Extract the data for the domain settings.
String domainNameString = domainNameEditText.getText().toString();
- boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
- boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
- boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
- boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
- boolean formDataEnabled = formDataEnabledSwitch.isChecked();
- int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
- int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
+ boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
+ boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
+ boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
+ boolean domStorageEnabledEnabledBoolean = domStorageEnabledSwitch.isChecked();
+ boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
+ int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
+ int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
+ int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
// Get the data for the `Spinners` from the entry values string arrays.
- String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
- int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
+ String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPositionInt];
+ int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePositionInt]);
// Check to see if we are using a custom user agent.
if (userAgentString.equals("Custom user agent")) {
}
// Save the domain settings.
- domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
+ domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean, userAgentString, fontSizeInt,
+ displayWebpageImagesInt);
+
+ // Display a `Snackbar`.
+ Snackbar.make(domainsListView, R.string.domain_settings_saved, Snackbar.LENGTH_SHORT).show();
+
+ // update the domains `ListView`.
+ updateDomainsListView();
break;
case R.id.delete_domain:
- // Delete the selected domain.
- domainsDatabaseHelper.deleteDomain(databaseId);
+ // Save the `ListView` `currentPosition`.
+ final int currentPosition = domainsListView.getCheckedItemPosition();
+
+ // Get a `Cursor` that does not show the domain to be deleted.
+ Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseId);
+
+ // Setup `domainsPendingDeleteCursorAdapter` with `this` context. `false` disables `autoRequery`.
+ CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout. `false` does not attach it to the root.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ // Set the domain name.
+ String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
+ TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
+ domainNameTextView.setText(domainNameString);
+ }
+ };
- // Detach the domain settings fragment.
- getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
+ // Update the `ListView`.
+ domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
- // Hide the options `MenuItems`.
- saveMenuItem.setVisible(false);
- deleteMenuItem.setVisible(false);
+ // Detach the domain settings `Fragment`.
+ getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
- // Update the `ListView`.
- updateDomainsListView();
+ // Disable the options `MenuItems`.
+ deleteMenuItem.setEnabled(false);
+ deleteMenuItem.setIcon(R.drawable.delete_blue);
+ saveMenuItem.setEnabled(false);
+
+ // Display a `Snackbar`.
+ Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
+ .setAction(R.string.undo, new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ // Do nothing because everything will be handled by `onDismissed()` below.
+ }
+ })
+ .addCallback(new Snackbar.Callback() {
+ @Override
+ public void onDismissed(Snackbar snackbar, int event) {
+ switch (event) {
+ // The user pushed the `Undo` button.
+ case Snackbar.Callback.DISMISS_EVENT_ACTION:
+ // Get a `Cursor` with the current contents of the domains database.
+ Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
+
+ // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
+ CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout. `false` does not attach it to the root.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ // Set the domain name.
+ String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
+ TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the `ListView`.
+ domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
+
+ // Select the entry in the domain list at `currentPosition`.
+ domainsListView.setItemChecked(currentPosition, true);
+
+ // Store `databaseId` in `argumentsBundle`.
+ Bundle argumentsBundle = new Bundle();
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
+
+ // Add `argumentsBundle` to `domainSettingsFragment`.
+ DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+ domainSettingsFragment.setArguments(argumentsBundle);
+
+ // Display `domainSettingsFragment`.
+ getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
+
+ // Enable the options `MenuItems`.
+ deleteMenuItem.setEnabled(true);
+ deleteMenuItem.setIcon(R.drawable.delete_light);
+ saveMenuItem.setEnabled(true);
+ break;
+
+ // The `Snackbar` was dismissed without the `Undo` button being pushed.
+ default:
+ // Delete the selected domain.
+ domainsDatabaseHelper.deleteDomain(databaseId);
+ break;
+ }
+ }
+ })
+ .show();
break;
}
+
+ // Consume the event.
return true;
}
}
private void updateDomainsListView() {
+ // Initialize `currentPosition`.
+ int currentPosition;
+
+ // Store the current position of `domainsListView` if it is already populated.
+ if (domainsListView.getCount() > 0){
+ currentPosition = domainsListView.getCheckedItemPosition();
+ } else {
+ // Set `currentPosition` to 0;
+ currentPosition = 0;
+ }
+
// Get a `Cursor` with the current contents of the domains database.
- Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
+ Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
// Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
}
};
- // Update the `RecyclerView`.
+ // Update the `ListView`.
domainsListView.setAdapter(domainsCursorAdapter);
+
+ // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
+ if (twoPaneMode && (domainsCursor.getCount() > 0)) {
+ // Select the entry in the domain list at `currentPosition`.
+ domainsListView.setItemChecked(currentPosition, true);
+
+ // Get the `databaseId` for `currentPosition`.
+ domainsCursor.moveToPosition(currentPosition);
+ databaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
+
+ // Store `databaseId` in `argumentsBundle`.
+ Bundle argumentsBundle = new Bundle();
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
+
+ // Add `argumentsBundle` to `domainSettingsFragment`.
+ DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+ domainSettingsFragment.setArguments(argumentsBundle);
+
+ // Display `domainSettingsFragment`.
+ getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
+
+ // Enable the options `MenuItems`.
+ deleteMenuItem.setEnabled(true);
+ saveMenuItem.setEnabled(true);
+
+ // Set the delete icon according to the theme.
+ if (MainWebViewActivity.darkTheme) {
+ deleteMenuItem.setIcon(R.drawable.delete_dark);
+ } else {
+ deleteMenuItem.setIcon(R.drawable.delete_light);
+ }
+ } else {
+ // Disable the options `MenuItems`.
+ deleteMenuItem.setEnabled(false);
+ deleteMenuItem.setIcon(R.drawable.delete_blue);
+ saveMenuItem.setEnabled(false);
+ }
}
}
\ No newline at end of file