+ // 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();
+ }