+ @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 and the favorite icon.
+ // Get the new favorite icon from the `Dialog` and convert it into a `Bitmap`.
+ ImageView newFavoriteIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_web_page_favorite_icon);
+ Drawable newFavoriteIconDrawable = newFavoriteIconImageView.getDrawable();
+ Bitmap newFavoriteIconBitmap = ((BitmapDrawable) newFavoriteIconDrawable).getBitmap();
+
+ // Convert `newFavoriteIconBitmap` into a Byte Array.
+ ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
+ newFavoriteIconBitmap.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 databaseId 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);
+ Bitmap folderIconBitmap;
+
+ // Prepare the favorite icon.
+ if (currentFolderIconRadioButton.isChecked()) {
+ // Update the folder name if it has changed.
+ 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 { // Prepare the new favorite icon.
+ if (defaultFolderIconRadioButton.isChecked()) {
+ // Get the default folder icon drawable and convert it to a `Bitmap`. `this` specifies the current context.
+ Drawable folderIconDrawable = ContextCompat.getDrawable(this, R.drawable.folder_blue_bitmap);
+ BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+ folderIconBitmap = folderIconBitmapDrawable.getBitmap();
+ } else { // Use the web page favorite icon.
+ 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();
+ }
+ }
+
+ private void updateBookmarksListView(String folderName) {
+ // Get a `Cursor` with the current contents of the bookmarks database.
+ bookmarksCursor = bookmarksDatabaseHandler.getAllBookmarksCursorByDisplayOrder(folderName);
+
+ // Setup `bookmarksCursorAdapter` with `this` context. The `false` disables autoRequery.