X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FBookmarksDatabaseViewActivity.java;h=0560c017a0a78deb65ceba3e9bc952103b1bc7f5;hb=44a7a1d29d8e4722e2ea522075878a13b89bcb52;hp=91afa6d64120a2d15d1bbba2b53083b4e7ae22bc;hpb=1b27ac6f2b7c046945fc97e2aff9adbde8a152ce;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java index 91afa6d6..0560c017 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2022 Soren Stoutner . + * Copyright 2016-2022 Soren Stoutner . * * This file is part of Privacy Browser Android . * @@ -34,7 +34,6 @@ import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.preference.PreferenceManager; import android.util.SparseBooleanArray; import android.view.ActionMode; import android.view.Menu; @@ -53,6 +52,7 @@ import android.widget.ResourceCursorAdapter; import android.widget.Spinner; import android.widget.TextView; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; @@ -60,6 +60,7 @@ import androidx.appcompat.widget.Toolbar; // The AndroidX toolbar must be used import androidx.core.content.ContextCompat; import androidx.cursoradapter.widget.CursorAdapter; import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22. +import androidx.preference.PreferenceManager; import com.google.android.material.snackbar.Snackbar; @@ -107,9 +108,6 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); } - // Set the activity theme. - setTheme(R.style.PrivacyBrowser); - // Run the default commands. super.onCreate(savedInstanceState); @@ -153,11 +151,23 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements actionBar.setCustomView(R.layout.spinner); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP); - // Initialize the database handler. The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`. - bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0); + // Control what the system back command does. + OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + // Prepare to finish the activity. + prepareFinish(); + } + }; + + // Register the on back pressed callback. + getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback); + + // Initialize the database handler. + bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this); // Setup a matrix cursor for "All Folders" and "Home Folder". - String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME}; + String[] matrixCursorColumnNames = {BookmarksDatabaseHelper.ID, BookmarksDatabaseHelper.BOOKMARK_NAME}; MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames); matrixCursor.addRow(new Object[]{ALL_FOLDERS_DATABASE_ID, getString(R.string.all_folders)}); matrixCursor.addRow(new Object[]{HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)}); @@ -296,7 +306,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements boolean isFolder = (cursor.getInt(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.IS_FOLDER)) == 1); // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`. - int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper._ID)); + int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.ID)); TextView bookmarkDatabaseIdTextView = view.findViewById(R.id.bookmarks_databaseview_database_id); bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId)); @@ -472,7 +482,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Get the position of the folder in the bookmarks cursor. while ((folderPosition < 0) && (bookmarksCursor.getPosition() < bookmarksCursor.getCount())) { // Check if the folder database ID matches the bookmark database ID. - if (folderDatabaseId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper._ID))) { + if (folderDatabaseId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.ID))) { // Get the folder position. folderPosition = bookmarksCursor.getPosition(); @@ -588,7 +598,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Close the activity if back has been pressed. if (closeActivityAfterDismissingSnackbar) { - onBackPressed(); + finish(); } } }); @@ -632,8 +642,8 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Run the command that corresponds to the selected menu item. if (menuItemId == android.R.id.home) { // Go Home. The home arrow is identified as `android.R.id.home`, not just `R.id.home`. - // Exit the activity. - onBackPressed(); + // Prepare to finish the activity. + prepareFinish(); } else if (menuItemId == R.id.sort) { // Toggle the sort mode. // Update the sort by display order tracker. sortByDisplayOrder = !sortByDisplayOrder; @@ -675,8 +685,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements savedInstanceState.putBoolean(SORT_BY_DISPLAY_ORDER, sortByDisplayOrder); } - @Override - public void onBackPressed() { + private void prepareFinish() { // Check to see if a snackbar is currently displayed. If so, it must be closed before existing so that a pending delete is completed before reloading the list view in the bookmarks activity. if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks deleted snackbar before going home. // Set the close flag. @@ -698,7 +707,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements BookmarksActivity.restartFromBookmarksDatabaseViewActivity = true; // Exit the bookmarks database view activity. - super.onBackPressed(); + finish(); } } @@ -754,7 +763,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements while (folderCursor.getPosition() < folderCursor.getCount()) { // Get the bookmark database ID. - int bookmarkId = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper._ID)); + int bookmarkId = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.ID)); // Move the bookmarks cursor to the first position. bookmarksCursor.moveToFirst(); @@ -765,7 +774,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Get the position of this bookmark in the bookmarks cursor. while ((bookmarkPosition < 0) && (bookmarksCursor.getPosition() < bookmarksCursor.getCount())) { // Check if the bookmark IDs match. - if (bookmarkId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper._ID))) { + if (bookmarkId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.ID))) { // Get the bookmark position. bookmarkPosition = bookmarksCursor.getPosition(); @@ -918,4 +927,4 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Run the default commands. super.onDestroy(); } -} \ No newline at end of file +}