- @Override
- public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedFolderDatabaseId, @NonNull Bitmap favoriteIconBitmap) {
- // Remove the incorrect lint warning below that the dialog fragment might be null.
- assert dialogFragment != null;
-
- // Get the dialog.
- Dialog dialog = dialogFragment.getDialog();
-
- // Remove the incorrect lint warning below that the dialog might be null.
- assert dialog != null;
-
- // Get handles for the views from the dialog.
- RadioButton currentFolderIconRadioButton = dialog.findViewById(R.id.current_icon_radiobutton);
- RadioButton defaultFolderIconRadioButton = dialog.findViewById(R.id.default_icon_radiobutton);
- ImageView defaultFolderIconImageView = dialog.findViewById(R.id.default_icon_imageview);
- EditText editFolderNameEditText = dialog.findViewById(R.id.folder_name_edittext);
-
- // Get the new folder name.
- String newFolderNameString = editFolderNameEditText.getText().toString();
-
- // Check if the favorite icon has changed.
- if (currentFolderIconRadioButton.isChecked()) { // Only the name has changed.
- // Update the name in the database.
- bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
- } else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) { // Only the icon has changed.
- // Create the new folder icon Bitmap.
- Bitmap folderIconBitmap;
-
- // Populate the new folder icon bitmap.
- if (defaultFolderIconRadioButton.isChecked()) {
- // Get the default folder icon drawable.
- Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
-
- // Convert the folder icon drawable to a bitmap drawable.
- BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
-
- // Convert the folder icon bitmap drawable to a bitmap.
- folderIconBitmap = folderIconBitmapDrawable.getBitmap();
- } else { // Use the `WebView` favorite icon.
- // Copy the favorite icon bitmap to the folder icon bitmap.
- folderIconBitmap = favoriteIconBitmap;
- }
-
- // Create a folder icon byte array output stream.
- ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
-
- // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
- folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
-
- // Convert the folder icon byte array stream to a byte array.
- byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
-
- // Update the folder icon in the database.
- bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, newFolderIconByteArray);
- } else { // The folder icon and the name have changed.
- // Get the new folder icon bitmap.
- Bitmap folderIconBitmap;
- if (defaultFolderIconRadioButton.isChecked()) {
- // Get the default folder icon drawable.
- Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
-
- // Convert the folder icon drawable to a bitmap drawable.
- BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
-
- // Convert the folder icon bitmap drawable to a bitmap.
- folderIconBitmap = folderIconBitmapDrawable.getBitmap();
- } else { // Use the `WebView` favorite icon.
- // Copy the favorite icon bitmap to the folder icon bitmap.
- folderIconBitmap = favoriteIconBitmap;
- }
-
- // Create a folder icon byte array output stream.
- ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
-
- // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
- folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
-
- // Convert the folder icon byte array stream to a byte array.
- byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
-
- // Update the folder name and icon in the database.
- bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, newFolderIconByteArray);
- }
-
- // Update the bookmarks cursor with the current contents of this folder.
- bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentBookmarksFolder);
-
- // Update the list view.
- bookmarksCursorAdapter.changeCursor(bookmarksCursor);
- }
-