@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
- return false;
+ // Get a handle for the move to folder menu item.
+ MenuItem moveToFolderMenuItem = menu.findItem(R.id.move_to_folder);
+
+ // Get a `Cursor` with all of the folders.
+ Cursor folderCursor = bookmarksDatabaseHelper.getAllFoldersCursor();
+
+ // Enable the move to folder menu item if at least one folder exists.
+ moveToFolderMenuItem.setVisible(folderCursor.getCount() > 0);
+
+ // `return true` indicates that the menu has been updated.
+ return true;
}
@Override
ListView folderListView = (ListView) dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview);
long[] newFolderLongArray = folderListView.getCheckedItemIds();
- if (newFolderLongArray.length == 0) { // No new folder was selected.
- Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.cannot_move_bookmarks), Snackbar.LENGTH_INDEFINITE).show();
- } else { // Move the selected bookmarks.
- // Get the new folder database ID.
- int newFolderDatabaseId = (int) newFolderLongArray[0];
-
- // Instantiate `newFolderName`.
- String newFolderName;
-
- if (newFolderDatabaseId == 0) {
- // The new folder is the home folder, represented as `""` in the database.
- newFolderName = "";
- } else {
- // Get the new folder name from the database.
- newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId);
- }
+ // Get the new folder database ID.
+ int newFolderDatabaseId = (int) newFolderLongArray[0];
- // Get a long array with the the database ID of the selected bookmarks.
- long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
- for (long databaseIdLong : selectedBookmarksLongArray) {
- // Get `databaseIdInt` for each selected bookmark.
- int databaseIdInt = (int) databaseIdLong;
+ // Instantiate `newFolderName`.
+ String newFolderName;
- // Move the selected bookmark to the new folder.
- bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName);
- }
+ if (newFolderDatabaseId == 0) {
+ // The new folder is the home folder, represented as `""` in the database.
+ newFolderName = "";
+ } else {
+ // Get the new folder name from the database.
+ newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId);
+ }
- // Refresh the `ListView`.
- updateBookmarksListView(currentFolder);
+ // Get a long array with the the database ID of the selected bookmarks.
+ long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
+ for (long databaseIdLong : selectedBookmarksLongArray) {
+ // Get `databaseIdInt` for each selected bookmark.
+ int databaseIdInt = (int) databaseIdLong;
- // Close the contextual app bar.
- contextualActionMode.finish();
+ // Move the selected bookmark to the new folder.
+ bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName);
}
+
+ // Refresh the `ListView`.
+ updateBookmarksListView(currentFolder);
+
+ // Close the contextual app bar.
+ contextualActionMode.finish();
}
private void updateBookmarksListView(String folderName) {
return parentFolder;
}
+ public Cursor getAllFoldersCursor() {
+ // Get a readable database handle.
+ SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
+
+ // Prepare the SQL statement to get the `Cursor` for all the folders.
+ final String GET_ALL_FOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE +
+ " WHERE " + IS_FOLDER + " = " + 1;
+
+ // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`.
+ // We can't close the `Cursor` because we need to use it in the parent activity.
+ return bookmarksDatabase.rawQuery(GET_ALL_FOLDERS, null);
+ }
+
public Cursor getAllBookmarksCursor() {
// Get a readable database handle.
SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();