+
+ // Handle long presses on the list view.
+ bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
+ // Instantiate the common variables.
+ MenuItem selectAllMenuItem;
+ MenuItem deleteMenuItem;
+ boolean deletingBookmarks;
+
+ @Override
+ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
+ // Inflate the menu for the contextual app bar.
+ getMenuInflater().inflate(R.menu.bookmarks_databaseview_context_menu, menu);
+
+ // Set the title.
+ mode.setTitle(R.string.bookmarks);
+
+ // Get handles for the menu items.
+ selectAllMenuItem = menu.findItem(R.id.select_all);
+ deleteMenuItem = menu.findItem(R.id.delete);
+
+ // Disable the delete menu item if a delete is pending.
+ deleteMenuItem.setEnabled(!deletingBookmarks);
+
+ // Get the number of currently selected bookmarks.
+ int numberOfSelectedBookmarks = bookmarksListView.getCheckedItemCount();
+
+ // Set the action mode subtitle according to the number of selected bookmarks. This must be set here or it will be missing if the activity is restarted.
+ mode.setSubtitle(getString(R.string.selected) + " " + numberOfSelectedBookmarks);
+
+ // Do not show the select all menu item if all the bookmarks are already checked.
+ if (bookmarksListView.getCheckedItemCount() == bookmarksListView.getCount()) {
+ selectAllMenuItem.setVisible(false);
+ }
+
+ // Make it so.
+ return true;
+ }
+
+ @Override
+ public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
+ // Do nothing.
+ return false;
+ }
+
+ @Override
+ public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
+ // Calculate the number of selected bookmarks.
+ int numberOfSelectedBookmarks = bookmarksListView.getCheckedItemCount();
+
+ // Update the action mode subtitle according to the number of selected bookmarks.
+ mode.setSubtitle(getString(R.string.selected) + " " + numberOfSelectedBookmarks);
+
+ // Update the visibility of the the select all menu.
+ if (bookmarksListView.getCheckedItemCount() == bookmarksListView.getCount()) { // All of the bookmarks are checked.
+ // Hide the select all menu item.
+ selectAllMenuItem.setVisible(false);
+ } else { // Not all of the bookmarks are checked.
+ // Show the select all menu item.
+ selectAllMenuItem.setVisible(true);
+ }
+
+ // Convert the database ID to an int.
+ int databaseId = (int) id;
+
+ // If a folder was selected, also select all the contents.
+ if (checked && bookmarksDatabaseHelper.isFolder(databaseId)) {
+ selectAllBookmarksInFolder(databaseId);
+ }
+
+ // Do not allow a bookmark to be deselected if the folder is selected.
+ if (!checked) {
+ // Get the folder name.
+ String folderName = bookmarksDatabaseHelper.getParentFolderName((int) id);
+
+ // If the bookmark is not in the root folder, check to see if the folder is selected.
+ if (!folderName.isEmpty()) {
+ // Get the database ID of the folder.
+ int folderDatabaseId = bookmarksDatabaseHelper.getFolderDatabaseId(folderName);
+
+ // Move the bookmarks cursor to the first position.
+ bookmarksCursor.moveToFirst();
+
+ // Initialize the folder position variable.
+ int folderPosition = -1;
+
+ // 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.getColumnIndex(BookmarksDatabaseHelper._ID)))) {
+ // Get the folder position.
+ folderPosition = bookmarksCursor.getPosition();
+
+ // Check if the folder is selected.
+ if (bookmarksListView.isItemChecked(folderPosition)) {
+ // Reselect the bookmark.
+ bookmarksListView.setItemChecked(position, true);
+
+ // Display a snackbar explaining why the bookmark cannot be deselected.
+ Snackbar.make(bookmarksListView, R.string.cannot_deselect_bookmark, Snackbar.LENGTH_LONG).show();
+ }
+ }
+
+ // Increment the bookmarks cursor.
+ bookmarksCursor.moveToNext();
+ }
+ }
+ }
+ }
+
+ @Override
+ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
+ switch (item.getItemId()) {
+ case R.id.select_all:
+ // Get the total number of bookmarks.
+ int numberOfBookmarks = bookmarksListView.getCount();
+
+ // Select them all.
+ for (int i = 0; i < numberOfBookmarks; i++) {
+ bookmarksListView.setItemChecked(i, true);
+ }
+ break;
+
+ case R.id.delete:
+ // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
+ deletingBookmarks = true;
+
+ // Get an array of the selected row IDs.
+ long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
+
+ // Get an array of checked bookmarks. `.clone()` makes a copy that won't change if the list view is reloaded, which is needed for re-selecting the bookmarks on undelete.
+ SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
+
+ // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
+ switch (currentFolderDatabaseId) {
+ // Get a cursor with all the folders.
+ case ALL_FOLDERS_DATABASE_ID:
+ if (sortByDisplayOrder) {
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray);
+ } else {
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksExcept(selectedBookmarksIdsLongArray);
+ }
+ break;
+
+ // Get a cursor for the home folder.
+ case HOME_FOLDER_DATABASE_ID:
+ if (sortByDisplayOrder) {
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, "");
+ } else {
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksExcept(selectedBookmarksIdsLongArray, "");
+ }
+ break;
+
+ // Display the selected folder.
+ default:
+ // Get a cursor for the selected folder.
+ if (sortByDisplayOrder) {
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolderName);
+ } else {
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksExcept(selectedBookmarksIdsLongArray, currentFolderName);
+ }
+ }
+
+ // Update the list view.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+
+ // Create a Snackbar with the number of deleted bookmarks.
+ bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_databaseview_coordinatorlayout),
+ getString(R.string.bookmarks_deleted) + " " + selectedBookmarksIdsLongArray.length, Snackbar.LENGTH_LONG)
+ .setAction(R.string.undo, view -> {
+ // Do nothing because everything will be handled by `onDismissed()` below.
+ })
+ .addCallback(new Snackbar.Callback() {
+ @SuppressLint("SwitchIntDef") // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
+ @Override
+ public void onDismissed(Snackbar snackbar, int event) {
+ if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) { // The user pushed the undo button.
+ // Update the bookmarks list view with the current contents of the bookmarks database, including the "deleted bookmarks.
+ updateBookmarksListView();
+
+ // Re-select the previously selected bookmarks.
+ for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
+ bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
+ }
+ } else { // The Snackbar was dismissed without the undo button being pushed.
+ // Delete each selected bookmark.
+ for (long databaseIdLong : selectedBookmarksIdsLongArray) {
+ // Convert `databaseIdLong` to an int.
+ int databaseIdInt = (int) databaseIdLong;
+
+ // Delete the selected bookmark.
+ bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
+ }
+ }
+
+ // Reset the deleting bookmarks flag.
+ deletingBookmarks = false;
+
+ // Enable the delete menu item.
+ deleteMenuItem.setEnabled(true);
+
+ // Close the activity if back has been pressed.
+ if (closeActivityAfterDismissingSnackbar) {
+ onBackPressed();
+ }
+ }
+ });
+
+ // Show the Snackbar.
+ bookmarksDeletedSnackbar.show();
+ break;
+ }
+
+ // Consume the click.
+ return false;
+ }
+
+ @Override
+ public void onDestroyActionMode(ActionMode mode) {
+ // Do nothing.
+ }
+ });