From 3dff6df834d582f932795c72f6bdcf6f655ac593 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 12 Oct 2020 18:42:29 -0700 Subject: [PATCH] Don't show a context menu when 0 bookmarks are selected. https://redmine.stoutner.com/issues/592 --- .../activities/BookmarksActivity.java | 55 +++++------ .../BookmarksDatabaseViewActivity.java | 93 ++++++++++--------- 2 files changed, 77 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java index e4d960a0..e6a4cf48 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java @@ -287,33 +287,36 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Get the number of selected bookmarks. int numberOfSelectedBookmarks = bookmarksListView.getCheckedItemCount(); - // Adjust the ActionMode and the menu according to the number of selected bookmarks. - if (numberOfSelectedBookmarks == 1) { // One bookmark is selected. - // List the number of selected bookmarks in the subtitle. - mode.setSubtitle(getString(R.string.selected) + " 1"); - - // Show the `Move Up`, `Move Down`, and `Edit` options. - moveBookmarkUpMenuItem.setVisible(true); - moveBookmarkDownMenuItem.setVisible(true); - editBookmarkMenuItem.setVisible(true); - - // Update the enabled status of the move icons. - updateMoveIcons(); - } else { // More than one bookmark is selected. - // List the number of selected bookmarks in the subtitle. - mode.setSubtitle(getString(R.string.selected) + " " + numberOfSelectedBookmarks); - - // Hide non-applicable `MenuItems`. - moveBookmarkUpMenuItem.setVisible(false); - moveBookmarkDownMenuItem.setVisible(false); - editBookmarkMenuItem.setVisible(false); - } + // Only process commands if at least one bookmark is selected. Otherwise, a context menu with 0 selected bookmarks is briefly displayed. + if (numberOfSelectedBookmarks > 0) { + // Adjust the ActionMode and the menu according to the number of selected bookmarks. + if (numberOfSelectedBookmarks == 1) { // One bookmark is selected. + // List the number of selected bookmarks in the subtitle. + mode.setSubtitle(getString(R.string.selected) + " 1"); + + // Show the `Move Up`, `Move Down`, and `Edit` options. + moveBookmarkUpMenuItem.setVisible(true); + moveBookmarkDownMenuItem.setVisible(true); + editBookmarkMenuItem.setVisible(true); + + // Update the enabled status of the move icons. + updateMoveIcons(); + } else { // More than one bookmark is selected. + // List the number of selected bookmarks in the subtitle. + mode.setSubtitle(getString(R.string.selected) + " " + numberOfSelectedBookmarks); + + // Hide non-applicable `MenuItems`. + moveBookmarkUpMenuItem.setVisible(false); + moveBookmarkDownMenuItem.setVisible(false); + editBookmarkMenuItem.setVisible(false); + } - // Do not show the select all menu item if all the bookmarks are already checked. - if (bookmarksListView.getCheckedItemCount() == bookmarksListView.getCount()) { - selectAllBookmarksMenuItem.setVisible(false); - } else { - selectAllBookmarksMenuItem.setVisible(true); + // Do not show the select all menu item if all the bookmarks are already checked. + if (bookmarksListView.getCheckedItemCount() == bookmarksListView.getCount()) { + selectAllBookmarksMenuItem.setVisible(false); + } else { + selectAllBookmarksMenuItem.setVisible(true); + } } } 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 17c9e879..d56c5a2a 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java @@ -426,61 +426,64 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // 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); - } + // Only run the commands if at least one bookmark is selected. Otherwise, a context menu with 0 selected bookmarks is briefly displayed. + if (numberOfSelectedBookmarks > 0) { + // 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; + // 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); - } + // 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); + // 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); + // 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(); + // Move the bookmarks cursor to the first position. + bookmarksCursor.moveToFirst(); - // Initialize the folder position variable. - int folderPosition = -1; + // 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(); + // 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); + // 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(); + // 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(); + // Increment the bookmarks cursor. + bookmarksCursor.moveToNext(); + } } } } @@ -852,7 +855,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements } @Override - public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedBookmarkDatabaseId, Bitmap favoriteIconBitmap) { + public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedBookmarkDatabaseId, @NonNull Bitmap favoriteIconBitmap) { // Get the dialog from the dialog fragment. Dialog dialog = dialogFragment.getDialog(); -- 2.45.2