package com.stoutner.privacybrowser.activities;
+import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import com.stoutner.privacybrowser.fragments.DomainsListFragment;
import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
+import java.util.Objects;
+
public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
// `twoPanedMode` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreate()`, `onCreateOptionsMenu()`, and `populateDomainsListView()`.
public static boolean twoPanedMode;
// `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`.
public static boolean dismissingSnackbar;
+ // `closeActivityAfterDismissingSnackbar` is used in `onOptionsItemSelected()`, and `onBackPressed()`.
+ private boolean closeActivityAfterDismissingSnackbar;
+
// `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`.
private Context context;
// `supportFragmentManager` is used in `onCreate()` and `onCreateOptionsMenu()`.
private FragmentManager supportFragmentManager;
- // `domainsDatabaseHelper` is used in `onCreate()` and `saveDomainSettings()`.
+ // `domainsDatabaseHelper` is used in `onCreate()`, `saveDomainSettings()`, and `onDestroy()`.
private static DomainsDatabaseHelper domainsDatabaseHelper;
// `domainsListView` is used in `onCreate()` and `populateDomainsList()`.
final Toolbar domainsAppBar = findViewById(R.id.domains_toolbar);
setSupportActionBar(domainsAppBar);
- // Display the home arrow on `SupportActionBar`.
+ // Display the home arrow on the support action bar.
ActionBar appBar = getSupportActionBar();
assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
appBar.setDisplayHomeAsUpEnabled(true);
- // Initialize the database handler. The two `nulls` do not specify the database name or a `CursorFactory`. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
+ // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
// Determine if we are in two pane mode. `domain_settings_fragment_container` does not exist on devices with a width less than 900dp.
// Dismiss the undo delete `SnackBar` if it is shown.
if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
- undoDeleteSnackbar.dismiss();
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
- // Create a `Runnable` to return to the main activity.
- Runnable navigateHomeRunnable = () -> {
- // Go home.
- NavUtils.navigateUpFromSameTask(this);
- };
-
- // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
- Handler handler = new Handler();
- handler.postDelayed(navigateHomeRunnable, 300);
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
} else {
// Go home.
NavUtils.navigateUpFromSameTask(this);
} else { // The device is in single-paned mode and `DomainsListFragment` is displayed.
// Dismiss the undo delete `SnackBar` if it is shown.
if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
- undoDeleteSnackbar.dismiss();
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
- // Create a `Runnable` to return to the main activity.
- Runnable navigateHomeRunnable = () -> {
- // Go home.
- NavUtils.navigateUpFromSameTask(this);
- };
-
- // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
- Handler handler = new Handler();
- handler.postDelayed(navigateHomeRunnable, 300);
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
} else {
// Go home.
NavUtils.navigateUpFromSameTask(this);
deleteMenuItem.setIcon(R.drawable.delete_blue);
// Remove the domain settings fragment.
- supportFragmentManager.beginTransaction().remove(supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container)).commit();
+ supportFragmentManager.beginTransaction().remove(Objects.requireNonNull(supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container))).commit();
} else { // Single-paned mode.
// Display `DomainsListFragment`.
DomainsListFragment domainsListFragment = new DomainsListFragment();
// 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) -> {
// Delete the selected domain.
domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
- // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed.
+ // 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 = () -> {
dismissingSnackbar = false;
};
- // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database.
+ // 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);
+ }
+
break;
}
}
});
+
+ // Show the Snackbar.
undoDeleteSnackbar.show();
break;
}
// Dismiss the undo delete SnackBar if it is shown.
if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
- undoDeleteSnackbar.dismiss();
-
- // Create a runnable to return to the main activity.
- Runnable navigateHomeRunnable = super::onBackPressed;
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
- // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
- Handler handler = new Handler();
- handler.postDelayed(navigateHomeRunnable, 300);
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
} else {
// Pass `onBackPressed()` to the system.
super.onBackPressed();
} else { // The device is in single-paned mode and the domain list fragment is displayed.
// Dismiss the undo delete SnackBar if it is shown.
if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
- undoDeleteSnackbar.dismiss();
-
- // Create a runnable to return to the main activity.
- Runnable navigateHomeRunnable = super::onBackPressed;
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
- // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
- Handler handler = new Handler();
- handler.postDelayed(navigateHomeRunnable, 300);
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
} else {
// Pass `onBackPressed()` to the system.
super.onBackPressed();
deleteMenuItem.setIcon(R.drawable.delete_blue);
}
}
+
+ @Override
+ public void onDestroy() {
+ // Close the domains database helper.
+ domainsDatabaseHelper.close();
+
+ // Run the default commands.
+ super.onDestroy();
+ }
}
\ No newline at end of file