+ // Update the `ListView`.
+ domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
+
+ // Display a `Snackbar`.
+ undoDeleteSnackbar = 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:
+ // Store `databaseId` in `argumentsBundle`.
+ Bundle argumentsBundle = new Bundle();
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
+
+ // Add `argumentsBundle` to `domainSettingsFragment`.
+ DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+ domainSettingsFragment.setArguments(argumentsBundle);
+
+ // Display the correct fragments.
+ if (twoPanedMode) { // The device in in two-paned mode.
+ // 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 previously deleted domain in `domainsListView`.
+ domainsListView.setItemChecked(deletedDomainPosition, true);
+
+ // Display `domainSettingsFragment`.
+ supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
+
+ // Enable the options `MenuItems`.
+ deleteMenuItem.setEnabled(true);
+ deleteMenuItem.setIcon(R.drawable.delete_light);
+ } else { // The device in in one-paned mode.
+ // Display `domainSettingsFragment`.
+ supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+
+ // Hide `add_domain_fab`.
+ FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
+ addDomainFAB.setVisibility(View.GONE);
+
+ // Show and enable `deleteMenuItem`.
+ deleteMenuItem.setVisible(true);
+
+ // Display `domainSettingsFragment`.
+ supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+ }
+ break;
+
+ // The `Snackbar` was dismissed without the `Undo` button being pushed.
+ default:
+ // Delete the selected domain.
+ domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
+
+ // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed.
+ if (dismissingSnackbar) {
+ // Create a `Runnable` to enable the delete menu item.
+ Runnable enableDeleteMenuItemRunnable = new Runnable() {
+ @Override
+ public void run() {
+ // Enable `deleteMenuItem` according to the display mode.
+ if (twoPanedMode) { // Two-paned mode.
+ // Enable `deleteMenuItem`.
+ deleteMenuItem.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 { // Single-paned mode.
+ // Show `deleteMenuItem`.
+ deleteMenuItem.setVisible(true);
+ }
+
+ // Reset `dismissingSnackbar`.
+ dismissingSnackbar = false;
+ }
+ };
+
+ // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database.
+ Handler handler = new Handler();
+ handler.postDelayed(enableDeleteMenuItemRunnable, 100);
+ }
+ break;
+ }
+ }
+ });
+ undoDeleteSnackbar.show();
+ break;
+ }
+
+ // Consume the event.
+ return true;
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ // Store the current `DomainSettingsFragment` state in `outState`.
+ if (findViewById(R.id.domain_settings_scrollview) != null) { // `DomainSettingsFragment` is displayed.
+ // Save any changes that have been made to the domain settings.
+ saveDomainSettings();
+
+ // Store `DomainSettingsDisplayed`.
+ outState.putBoolean("domainSettingsDisplayed", true);
+ outState.putInt("domainSettingsDatabaseId", DomainSettingsFragment.databaseId);
+ } else { // `DomainSettingsFragment` is not displayed.
+ outState.putBoolean("domainSettingsDisplayed", false);
+ outState.putInt("domainSettingsDatabaseId", -1);
+ }
+
+ super.onSaveInstanceState(outState);
+ }
+
+ // Control what the navigation bar back button does.
+ @Override
+ public void onBackPressed() {
+ if (twoPanedMode) { // The device is in two-paned mode.
+ // Save the current domain settings if the domain settings fragment is displayed.
+ if (findViewById(R.id.domain_settings_scrollview) != null) {
+ saveDomainSettings();
+ }
+
+ // Go home.
+ NavUtils.navigateUpFromSameTask(this);
+ } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and `DomainSettingsFragment` is displayed.
+ // Save the current domain settings.
+ saveDomainSettings();
+
+ // Display `DomainsListFragment`.
+ DomainsListFragment domainsListFragment = new DomainsListFragment();
+ supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
+ supportFragmentManager.executePendingTransactions();
+
+ // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
+ populateDomainsListView(-1);
+
+ // Display `addDomainFAB`.
+ addDomainFAB.setVisibility(View.VISIBLE);
+
+ // Hide `deleteMenuItem`.
+ deleteMenuItem.setVisible(false);
+ } else { // The device is in single-paned mode and `DomainsListFragment` is displayed.
+ // Pass `onBackPressed()` to the system.
+ super.onBackPressed();
+ }