+ break;
+
+ case R.id.delete_domain:
+ // 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 `currentDomainDatabaseId` 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 `MenuItems`.
+ deleteMenuItem.setEnabled(false);
+ deleteMenuItem.setIcon(R.drawable.delete_blue);
+
+ // Remove the domain settings fragment.
+ fragmentManager.beginTransaction().remove(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.domain_settings_fragment_container))).commit();
+ } else { // Single-paned mode.
+ // Display `DomainsListFragment`.
+ 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 `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 = view.findViewById(R.id.domain_name_textview);
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the handle for the current `domains_listview`.
+ domainsListView = findViewById(R.id.domains_listview);
+
+ // Update the `ListView`.
+ domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
+
+ // Get a handle for the activity.
+ Activity activity = this;
+
+ // 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.
+ // Store the database ID in arguments bundle.
+ Bundle argumentsBundle = new Bundle();
+ argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
+
+ // Add the arguments bundle to the domain settings fragment.
+ 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(getApplicationContext(), 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 = 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`.
+ fragmentManager.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`.
+ fragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
+
+ // Hide the add domain floating action button.
+ addDomainFAB.hide();
+
+ // Show and enable `deleteMenuItem`.
+ deleteMenuItem.setVisible(true);
+
+ // Display `domainSettingsFragment`.
+ 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 `deleteMenuItem` according to the display mode.
+ if (twoPanedMode) { // Two-paned mode.
+ // Enable the delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Get a handle for the shared preferences.
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+
+ // Get the theme preferences.
+ boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
+
+ // Set the delete icon according to the theme.
+ if (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;
+ };
+
+ // 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();
+ break;
+ }
+
+ // Consume the event.
+ return true;
+ }
+
+ @Override
+ protected void onSaveInstanceState(@NonNull 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(coordinatorLayout, resources);
+
+ // Store `DomainSettingsDisplayed`.
+ outState.putBoolean("domain_settings_displayed", true);
+ outState.putInt("domain_settings_database_id", DomainSettingsFragment.databaseId);
+ } else { // `DomainSettingsFragment` is not displayed.
+ outState.putBoolean("domain_settings_displayed", false);
+ outState.putInt("domain_settings_database_id", -1);
+ }
+
+ super.onSaveInstanceState(outState);
+ }
+
+ // Control what the navigation bar back button does.
+ @Override
+ public void onBackPressed() {
+ // Get a handle for the fragment manager.
+ FragmentManager fragmentManager = getSupportFragmentManager();
+
+ 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(coordinatorLayout, resources);