+ // Check to see if the domain settings were loaded directly for editing of this app in single-paned mode.
+ if (closeOnBack && !twoPanedMode) { // The activity should delete the domain settings and exit straight to the the main WebView activity.
+ // Delete the selected domain.
+ domainsDatabaseHelper.deleteDomain(currentDomainDatabaseId);
+
+ // Go home.
+ NavUtils.navigateUpFromSameTask(activity);
+ } else { // A snackbar should be shown before deleting the domain settings.
+ // Reset close-on-back, which otherwise can cause errors if the system attempts to save settings for a domain that no longer exists.
+ closeOnBack = false;
+
+ // Store a copy of the current domain database ID because it could change while the snackbar is displayed.
+ final int databaseIdToDelete = currentDomainDatabaseId;
+
+ // Update the fragments and menu items.
+ if (twoPanedMode) { // Two-paned mode.
+ // Store the deleted domain position, which is needed if undo is selected in the snackbar.
+ deletedDomainPosition = domainsListView.getCheckedItemPosition();
+
+ // Disable the options menu items.
+ deleteMenuItem.setEnabled(false);
+ deleteMenuItem.setIcon(R.drawable.delete_disabled);
+
+ // Remove the domain settings fragment.
+ fragmentManager.beginTransaction().remove(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.domain_settings_fragment_container))).commit();
+ } else { // Single-paned mode.
+ // Display the domains list fragment.
+ DomainsListFragment domainsListFragment = new DomainsListFragment();
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
+ fragmentManager.executePendingTransactions();
+
+ // Show the add domain floating action button.
+ addDomainFAB.show();
+
+ // Hide `deleteMenuItem`.
+ deleteMenuItem.setVisible(false);
+ }
+
+ // Get a cursor that does not show the domain to be deleted.
+ Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseIdToDelete);
+
+ // Setup the domains pending delete cursor adapter.
+ CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ // Get the domain name string.
+ String domainNameString = cursor.getString(cursor.getColumnIndexOrThrow(DomainsDatabaseHelper.DOMAIN_NAME));
+
+ // Get a handle for the domain name text view.
+ TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
+
+ // Display the domain name.
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the handle for the current domains list view.
+ domainsListView = findViewById(R.id.domains_listview);
+
+ // Update the list view.
+ domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
+
+ // Display a snackbar.
+ undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
+ .setAction(R.string.undo, (View v) -> {
+ // Do nothing because everything will be handled by `onDismissed()` below.
+ })
+ .addCallback(new Snackbar.Callback() {
+ @Override
+ public void onDismissed(Snackbar snackbar, int event) {
+ // Run commands based on the event.
+ if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) { // The user pushed the `Undo` button.
+ // Create an arguments bundle.
+ Bundle argumentsBundle = new Bundle();
+
+ // Store the domains settings in the arguments bundle.
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
+ argumentsBundle.putInt(DomainSettingsFragment.SCROLL_Y, domainSettingsScrollY);
+
+ // Instantiate a new domain settings fragment.
+ DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+
+ // Add the arguments bundle to the domain settings fragment.
+ 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 the domains cursor adapter.
+ CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(getApplicationContext(), undoDeleteDomainsCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ /// Get the domain name string.
+ String domainNameString = cursor.getString(cursor.getColumnIndexOrThrow(DomainsDatabaseHelper.DOMAIN_NAME));
+
+ // Get a handle for the domain name text view.
+ TextView domainNameTextView = view.findViewById(R.id.domain_name_textview);
+
+ // Display the domain name.
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the domains list view.
+ domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
+
+ // Select the previously deleted domain in the list view.
+ domainsListView.setItemChecked(deletedDomainPosition, true);
+
+ // Display the domain settings fragment.
+ fragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
+
+ // Enable the options delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Set the delete menu item icon.
+ deleteMenuItem.setIcon(R.drawable.delete_enabled);
+ } else { // The device in in one-paned mode.
+ // Display the domain settings fragment.
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+
+ // Hide the add domain floating action button.
+ addDomainFAB.hide();
+
+ // Show and enable the delete menu item.
+ deleteMenuItem.setVisible(true);
+
+ // Display the domain settings fragment.
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+ }
+ } else { // The snackbar was dismissed without the undo button being pushed.
+ // Delete the selected domain.
+ domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
+
+ // Enable the delete menu item if the system was waiting for a snackbar to be dismissed.
+ if (dismissingSnackbar) {
+ // Create a `Runnable` to enable the delete menu item.
+ Runnable enableDeleteMenuItemRunnable = () -> {
+ // Enable the delete menu item according to the display mode.
+ if (twoPanedMode) { // Two-paned mode.
+ // Enable the delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Set the delete menu item icon.
+ deleteMenuItem.setIcon(R.drawable.delete_enabled);
+ } else { // Single-paned mode.
+ // Show the delete menu item.
+ deleteMenuItem.setVisible(true);
+ }
+
+ // Reset the dismissing snackbar tracker.
+ dismissingSnackbar = false;
+ };
+
+ // Enable the delete menu icon after 100 milliseconds to make sure that the previous domain has been deleted from the database.
+ Handler handler = new Handler();
+ handler.postDelayed(enableDeleteMenuItemRunnable, 100);
+ }
+
+ // Close the activity if back was pressed.
+ if (closeActivityAfterDismissingSnackbar) {
+ // Go home.
+ NavUtils.navigateUpFromSameTask(activity);
+ }
+ }
+ }
+ });
+
+ // Show the Snackbar.
+ undoDeleteSnackbar.show();
+ }