Don't show a context menu when 0 bookmarks are selected. https://redmine.stoutner...
authorSoren Stoutner <soren@stoutner.com>
Tue, 13 Oct 2020 01:42:29 +0000 (18:42 -0700)
committerSoren Stoutner <soren@stoutner.com>
Tue, 13 Oct 2020 01:42:29 +0000 (18:42 -0700)
app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java
app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java

index e4d960a04aaf102bbdcff856725c5ac951e045e4..e6a4cf48e3257d4f54e149e32dd9cc4c58005124 100644 (file)
@@ -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);
+                    }
                 }
             }
 
index 17c9e879fdec06cad0266b5c9498274729d11dac..d56c5a2a87303bddca432103530e98e26705902b 100644 (file)
@@ -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();