import java.util.Objects;
-public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
+public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener, DomainsListFragment.DismissSnackbarInterface {
// `twoPanedMode` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreate()`, `onCreateOptionsMenu()`, and `populateDomainsListView()`.
public static boolean twoPanedMode;
// `deleteMenuItem` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
public static MenuItem deleteMenuItem;
- // `undoDeleteSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()` and `onBackPressed()`.
- public static Snackbar undoDeleteSnackbar;
-
// `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;
+ // The undelete snackbar is used in `onOptionsItemSelected()` and `onBackPressed()`.
+ private Snackbar undoDeleteSnackbar;
// `domainsDatabaseHelper` is used in `onCreate()`, `saveDomainSettings()`, and `onDestroy()`.
private static DomainsDatabaseHelper domainsDatabaseHelper;
// Populate the class variables.
coordinatorLayout = findViewById(R.id.domains_coordinatorlayout);
resources = getResources();
- context = this;
// `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
final Toolbar toolbar = findViewById(R.id.domains_toolbar);
actionBar.setDisplayHomeAsUpEnabled(true);
// 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);
+ domainsDatabaseHelper = new DomainsDatabaseHelper(this, 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.
twoPanedMode = (findViewById(R.id.domain_settings_fragment_container) != null);
Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
// Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
- CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) {
+ 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.
Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
// Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
- CursorAdapter domainsCursorAdapter = new CursorAdapter(context, domainsCursor, false) {
+ CursorAdapter domainsCursorAdapter = new CursorAdapter(getApplicationContext(), domainsCursor, false) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Inflate the individual item layout. `false` does not attach it to the root.
}
}
+ @Override
+ public void dismissSnackbar() {
+ // Dismiss the undo delete snackbar if it is shown.
+ if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
+ }
+ }
+
@Override
public void onDestroy() {
// Close the domains database helper.
package com.stoutner.privacybrowser.fragments;
+import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import com.stoutner.privacybrowser.activities.MainWebViewActivity;
public class DomainsListFragment extends Fragment {
+ // Instantiate the dismiss snackbar interface handle.
+ private DismissSnackbarInterface dismissSnackbarInterface;
+
+ // Define the public dismiss snackbar interface.
+ public interface DismissSnackbarInterface {
+ void dismissSnackbar();
+ }
+
+ public void onAttach(Context context) {
+ // Run the default commands.
+ super.onAttach(context);
+
+ // Get a handle for the dismiss snackbar interface.
+ dismissSnackbarInterface = (DismissSnackbarInterface) context;
+ }
+
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate `domains_list_fragment`. `false` does not attach it to the root `container`.
View domainsListFragmentView = inflater.inflate(R.layout.domains_list_fragment, container, false);
// Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null.
assert getActivity() != null;
- // Get a handle for `supportFragmentManager`.
+ // Get a handle for the support fragment manager.
final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
domainsListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
- // Dismiss `undoDeleteSnackbar` if it is currently displayed (because a domain has just been deleted).
- if ((DomainsActivity.undoDeleteSnackbar != null) && (DomainsActivity.undoDeleteSnackbar.isShown())) {
- DomainsActivity.dismissingSnackbar = true;
-
- DomainsActivity.undoDeleteSnackbar.dismiss();
- }
+ // Dismiss the snackbar if it is visible.
+ dismissSnackbarInterface.dismissSnackbar();
// Save the current domain settings if operating in two-paned mode and a domain is currently selected.
if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) {