Hide the move to folder menu item if no folders exist. https://redmine.stoutner...
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / activities / BookmarksActivity.java
index efdb06a72e03e4f2c2e69dc8639f7d0e3e2aa7f1..4536001b1554b4f586ac7468f5600a8a9d879358 100644 (file)
@@ -203,7 +203,17 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
 
             @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
@@ -626,44 +636,35 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         EditText createFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext);
         String folderNameString = createFolderNameEditText.getText().toString();
 
-        // Check to see if the folder already exists.
-        Cursor bookmarkFolderCursor = bookmarksDatabaseHelper.getFolderCursor(folderNameString);
-        int existingFoldersWithNewName = bookmarkFolderCursor.getCount();
-        bookmarkFolderCursor.close();
-        if (folderNameString.isEmpty() || (existingFoldersWithNewName > 0)) {
-            String cannotCreateFolder = getResources().getString(R.string.cannot_create_folder) + " \"" + folderNameString + "\"";
-            Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), cannotCreateFolder, Snackbar.LENGTH_INDEFINITE).show();
-        } else {  // Create the folder.
-            // Get the new folder icon `Bitmap`.
-            RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton);
-            Bitmap folderIconBitmap;
-            if (defaultFolderIconRadioButton.isChecked()) {
-                // Get the default folder icon `ImageView` from the `Dialog` and convert it to a `Bitmap`.
-                ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon);
-                Drawable folderIconDrawable = folderIconImageView.getDrawable();
-                BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
-                folderIconBitmap = folderIconBitmapDrawable.getBitmap();
-            } else {  // Assign `favoriteIcon` from the `WebView`.
-                folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
-            }
+        // Get the new folder icon `Bitmap`.
+        RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton);
+        Bitmap folderIconBitmap;
+        if (defaultFolderIconRadioButton.isChecked()) {
+            // Get the default folder icon `ImageView` from the `Dialog` and convert it to a `Bitmap`.
+            ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon);
+            Drawable folderIconDrawable = folderIconImageView.getDrawable();
+            BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+            folderIconBitmap = folderIconBitmapDrawable.getBitmap();
+        } else {  // Assign `favoriteIcon` from the `WebView`.
+            folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
+        }
 
-            // Convert `folderIconBitmap` to a byte array.  `0` is for lossless compression (the only option for a PNG).
-            ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
-            folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
-            byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
+        // Convert `folderIconBitmap` to a byte array.  `0` is for lossless compression (the only option for a PNG).
+        ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
+        folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
+        byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
 
-            // Move all the bookmarks down one in the display order.
-            for (int i = 0; i < bookmarksListView.getCount(); i++) {
-                int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
-                bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i + 1);
-            }
+        // Move all the bookmarks down one in the display order.
+        for (int i = 0; i < bookmarksListView.getCount(); i++) {
+            int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
+            bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i + 1);
+        }
 
-            // Create the folder, placing it at the top of the ListView
-            bookmarksDatabaseHelper.createFolder(folderNameString, 0, currentFolder, folderIconByteArray);
+        // Create the folder, placing it at the top of the ListView
+        bookmarksDatabaseHelper.createFolder(folderNameString, 0, currentFolder, folderIconByteArray);
 
-            // Refresh the ListView.
-            updateBookmarksListView(currentFolder);
-        }
+        // Refresh the ListView.
+        updateBookmarksListView(currentFolder);
     }
 
     @Override
@@ -768,39 +769,35 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         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) {