- break;
-
- case R.id.delete_bookmark:
- // 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.
- final 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.
- selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
-
- // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
- bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolder);
-
- // Update the list view.
- bookmarksCursorAdapter.changeCursor(bookmarksCursor);
-
- // Show a Snackbar with the number of deleted bookmarks.
- bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_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) {
- switch (event) {
- // The user pushed the `Undo` button.
- case Snackbar.Callback.DISMISS_EVENT_ACTION:
- // Update the bookmarks cursor with the current contents of the bookmarks database, including the "deleted" bookmarks.
- bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
-
- // Update the list view.
- bookmarksCursorAdapter.changeCursor(bookmarksCursor);
-
- // Re-select the previously selected bookmarks.
- for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
- bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
- }
- break;
-
- // The Snackbar was dismissed without the `Undo` button being pushed.
- default:
- // Delete each selected bookmark.
- for (long databaseIdLong : selectedBookmarksIdsLongArray) {
- // Convert `databaseIdLong` to an int.
- int databaseIdInt = (int) databaseIdLong;
-
- // Delete the contents of the folder if the selected bookmark is a folder.
- if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
- deleteBookmarkFolderContents(databaseIdInt);
- }
-
- // Delete the selected bookmark.
- bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
- }
-
- // Update the display order.
- for (int i = 0; i < bookmarksListView.getCount(); i++) {
- // Get the database ID for the current bookmark.
- int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
-
- // Move `bookmarksCursor` to the current bookmark position.
- bookmarksCursor.moveToPosition(i);
-
- // Update the display order only if it is not correct in the database.
- if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
- bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
- }
- }
+ }
+
+ // Move the cursor to the selected position.
+ bookmarksCursor.moveToPosition(selectedBookmarkPosition);
+
+ // Find out if this bookmark is a folder.
+ boolean isFolder = (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
+
+ // Get the selected bookmark database ID.
+ int databaseId = bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
+
+ // Show the edit bookmark or edit bookmark folder dialog.
+ if (isFolder) {
+ // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
+ oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
+
+ // Instantiate the edit bookmark folder dialog.
+ DialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId, favoriteIconBitmap);
+
+ // Make it so.
+ editFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
+ } else {
+ // Instantiate the edit bookmark dialog.
+ DialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId, favoriteIconBitmap);
+
+ // Make it so.
+ editBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
+ }
+ } else if (menuItemId == R.id.delete_bookmark) { // Delete bookmark.
+ // 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.
+ final long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
+
+ // Initialize a variable to count the number of bookmarks to delete.
+ int numberOfBookmarksToDelete = 0;
+
+ // Count the number of bookmarks.
+ for (long databaseIdLong : selectedBookmarksIdsLongArray) {
+ // Convert the database ID long to an int.
+ int databaseIdInt = (int) databaseIdLong;
+
+ // Count the contents of the folder if the selected bookmark is a folder.
+ if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
+ // Add the bookmarks from the folder to the running total.
+ numberOfBookmarksToDelete = numberOfBookmarksToDelete + countBookmarkFolderContents(databaseIdInt);
+ }
+
+ // Increment the count of the number of bookmarks to delete.
+ numberOfBookmarksToDelete++;
+ }
+
+ // 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.
+ selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
+
+ // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolder);
+
+ // Update the list view.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+
+ // Create a Snackbar with the number of deleted bookmarks.
+ bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.bookmarks_deleted) + " " + numberOfBookmarksToDelete,
+ 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 cursor with the current contents of the bookmarks database, including the "deleted" bookmarks.
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
+
+ // Update the list view.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+
+ // 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 contents of the folder if the selected bookmark is a folder.
+ if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
+ deleteBookmarkFolderContents(databaseIdInt);
+ }
+
+ // Delete the selected bookmark.
+ bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);