+ bookmarksDatabaseHandler.createBookmark(bookmarkNameString, bookmarkUrlString, newBookmarkDisplayOrder, currentFolder, favoriteIconByteArray);
+
+ // Refresh the ListView. `setSelection` scrolls to the bottom of the list.
+ updateBookmarksListView(currentFolder);
+ bookmarksListView.setSelection(newBookmarkDisplayOrder);
+ }
+
+ @Override
+ public void onCancelCreateBookmarkFolder(DialogFragment dialogFragment) {
+ // Do nothing because the user selected `Cancel`.
+ }
+
+ @Override
+ public void onCreateBookmarkFolder(DialogFragment dialogFragment) {
+ // Get `create_folder_name_edit_text` and extract the string.
+ 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 = bookmarksDatabaseHandler.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_radiobuttion);
+ 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.favoriteIcon;
+ }
+
+ // 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);
+ bookmarksDatabaseHandler.updateBookmarkDisplayOrder(databaseId, i + 1);
+ }
+
+ // Create the folder, placing it at the top of the ListView
+ bookmarksDatabaseHandler.createFolder(folderNameString, 0, currentFolder, folderIconByteArray);
+
+ // Refresh the ListView.
+ updateBookmarksListView(currentFolder);
+ }
+ }
+
+ @Override
+ public void onCancelEditBookmark(DialogFragment dialogFragment) {
+ // Do nothing because the user selected `Cancel`.
+ }
+
+ @Override
+ public void onSaveEditBookmark(DialogFragment dialogFragment) {
+ // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
+ long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
+ int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0];
+
+ // Get the `EditText`s from the `editBookmarkDialogFragment` and extract the strings.
+ EditText editBookmarkNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
+ String bookmarkNameString = editBookmarkNameEditText.getText().toString();
+ EditText editBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
+ String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
+
+ // Get `edit_bookmark_current_icon_radiobutton`.
+ RadioButton currentBookmarkIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
+
+ if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
+ bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
+ } else { // Update the bookmark using the `WebView` favorite icon.
+ ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
+ MainWebViewActivity.favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
+ byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
+
+ // Update the bookmark and the favorite icon.
+ bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, newFavoriteIconByteArray);
+ }
+
+ // Close the contextual action mode.
+ contextualActionMode.finish();
+
+ // Refresh the `ListView`. `setSelection` scrolls to the position of the bookmark that was edited.
+ updateBookmarksListView(currentFolder);
+ bookmarksListView.setSelection(selectedBookmarkPosition);
+ }
+
+ @Override
+ public void onCancelEditBookmarkFolder(DialogFragment dialogFragment) {
+ // Do nothing because the user selected `Cancel`.
+ }
+
+ @Override
+ public void onSaveEditBookmarkFolder(DialogFragment dialogFragment) {
+ // Get the new folder name.
+ EditText editFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
+ String newFolderNameString = editFolderNameEditText.getText().toString();
+
+ // Check to see if the new folder name is unique.
+ Cursor bookmarkFolderCursor = bookmarksDatabaseHandler.getFolderCursor(newFolderNameString);
+ int existingFoldersWithNewName = bookmarkFolderCursor.getCount();
+ bookmarkFolderCursor.close();
+ if ( ((existingFoldersWithNewName == 0) || newFolderNameString.equals(oldFolderNameString)) && !newFolderNameString.isEmpty()) {
+ // Get a long array with the the database ID of the selected folder and convert it to an `int`.
+ long[] selectedFolderLongArray = bookmarksListView.getCheckedItemIds();
+ int selectedFolderDatabaseId = (int) selectedFolderLongArray[0];
+
+ // Get the `RadioButtons` from the `Dialog`.
+ RadioButton currentFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
+ RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
+
+ // Check if the favorite icon has changed.
+ if (currentFolderIconRadioButton.isChecked()) {
+ // Update the folder name if it has changed without modifying the favorite icon.
+ if (!newFolderNameString.equals(oldFolderNameString)) {
+ bookmarksDatabaseHandler.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
+
+ // Refresh the `ListView`. `setSelection` scrolls to the position of the folder that was edited.
+ updateBookmarksListView(currentFolder);
+ bookmarksListView.setSelection(selectedBookmarkPosition);
+ }
+ } else { // Update the folder icon.
+ // Get the new folder icon `Bitmap`.
+ Bitmap folderIconBitmap;
+ if (defaultFolderIconRadioButton.isChecked()) {
+ // Get the default folder icon `ImageView` from the `Drawable` and convert it to a `Bitmap`.
+ ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon);
+ Drawable folderIconDrawable = folderIconImageView.getDrawable();
+ BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+ folderIconBitmap = folderIconBitmapDrawable.getBitmap();
+ } else { // Get the web page icon `ImageView` from the `Dialog`.
+ folderIconBitmap = MainWebViewActivity.favoriteIcon;
+ }
+
+ // Convert the folder `Bitmap` 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();
+
+ bookmarksDatabaseHandler.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, folderIconByteArray);
+
+ // Refresh the `ListView`. `setSelection` scrolls to the position of the folder that was edited.
+ updateBookmarksListView(currentFolder);
+ bookmarksListView.setSelection(selectedBookmarkPosition);
+ }
+ } else { // Don't edit the folder because the new name is not unique.
+ String cannot_rename_folder = getResources().getString(R.string.cannot_rename_folder) + " \"" + newFolderNameString + "\"";
+ Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), cannot_rename_folder, Snackbar.LENGTH_INDEFINITE).show();
+ }
+
+ // Close the contextual action mode.
+ contextualActionMode.finish();
+ }
+
+ @Override
+ public void onCancelMoveToFolder(DialogFragment dialogFragment) {
+ // Do nothing because the user selected `Cancel`.
+ }
+
+ @Override
+ public void onMoveToFolder(DialogFragment dialogFragment) {
+ // Get the new folder database id.
+ 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_LONG).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 = bookmarksDatabaseHandler.getFolderName(newFolderDatabaseId);
+ }
+
+ // 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;
+
+ // Move the selected bookmark to the new folder.
+ bookmarksDatabaseHandler.moveToFolder(databaseIdInt, newFolderName);
+ }