X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FBookmarksActivity.kt;h=fe7d080854cd60654136491e3b17d3343a53230c;hb=e6befb69eb16e4c633623df508bfb9de370e204f;hp=b5b3992b686d5f30813236e2ca7bd4044df40ab8;hpb=bc1692b6c9d99726e7c6b7cf6a5c24f67f042b18;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.kt b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.kt index b5b3992b..fe7d0808 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 Soren Stoutner . + * Copyright 2016-2024 Soren Stoutner . * * This file is part of Privacy Browser Android . * @@ -47,6 +47,7 @@ import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.ActionBar import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar +import androidx.core.graphics.drawable.toBitmap import androidx.cursoradapter.widget.CursorAdapter import androidx.fragment.app.DialogFragment import androidx.preference.PreferenceManager @@ -90,6 +91,7 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma // Define the class variables. private var bookmarksDeletedSnackbar: Snackbar? = null + private var checkingManyBookmarks = false private var closeActivityAfterDismissingSnackbar = false private var contextualActionMode: ActionMode? = null @@ -250,46 +252,49 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma return true } - override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) { - // Get the number of selected bookmarks. - val numberOfSelectedBookmarks = bookmarksListView.checkedItemCount - - // Only process commands if at least one bookmark is selected. Otherwise, a context menu with 0 selected bookmarks is briefly displayed. - if (numberOfSelectedBookmarks > 0) { - // Adjust the action mode and the menu according to the number of selected bookmarks. - if (numberOfSelectedBookmarks == 1) { // One bookmark is selected. - // Show the applicable menu items. - moveBookmarkUpMenuItem.isVisible = true - moveBookmarkDownMenuItem.isVisible = true - editBookmarkMenuItem.isVisible = true - - // Update the enabled status of the move icons. - updateMoveIcons() - } else { // More than one bookmark is selected. - // Hide non-applicable `MenuItems`. - moveBookmarkUpMenuItem.isVisible = false - moveBookmarkDownMenuItem.isVisible = false - editBookmarkMenuItem.isVisible = false - } + override fun onItemCheckedStateChanged(actionMode: ActionMode, position: Int, id: Long, checked: Boolean) { + // Only update the UI if not checking many bookmarks. In that case, the flag will be reset on the last bookmark so the UI is only updated once. + if (!checkingManyBookmarks) { + // Get the number of selected bookmarks. + val numberOfSelectedBookmarks = bookmarksListView.checkedItemCount + + // Only process commands if at least one bookmark is selected. Otherwise, a context menu with 0 selected bookmarks is briefly displayed. + if (numberOfSelectedBookmarks > 0) { + // Adjust the action mode and the menu according to the number of selected bookmarks. + if (numberOfSelectedBookmarks == 1) { // One bookmark is selected. + // Show the applicable menu items. + moveBookmarkUpMenuItem.isVisible = true + moveBookmarkDownMenuItem.isVisible = true + editBookmarkMenuItem.isVisible = true + + // Update the enabled status of the move icons. + updateMoveIcons() + } else { // More than one bookmark is selected. + // Hide non-applicable `MenuItems`. + moveBookmarkUpMenuItem.isVisible = false + moveBookmarkDownMenuItem.isVisible = false + editBookmarkMenuItem.isVisible = false + } - // Display the move to folder menu item if at least one other folder exists. - moveToFolderMenuItem.isVisible = bookmarksDatabaseHelper.hasFoldersExceptDatabaseId(bookmarksListView.checkedItemIds) + // Display the move to folder menu item if at least one other folder exists. + moveToFolderMenuItem.isVisible = bookmarksDatabaseHelper.hasFoldersExceptDatabaseId(bookmarksListView.checkedItemIds) - // List the number of selected bookmarks in the subtitle. - mode.subtitle = getString(R.string.selected, numberOfSelectedBookmarks) + // List the number of selected bookmarks in the subtitle. + actionMode.subtitle = getString(R.string.selected, numberOfSelectedBookmarks) - // Show the select all menu item if all the bookmarks are not selected. - selectAllBookmarksMenuItem.isVisible = (numberOfSelectedBookmarks != bookmarksListView.count) + // Show the select all menu item if all the bookmarks are not selected. + selectAllBookmarksMenuItem.isVisible = (numberOfSelectedBookmarks != bookmarksListView.count) + } } } override fun onActionItemClicked(actionMode: ActionMode, menuItem: MenuItem): Boolean { // Declare the variables. - val selectedBookmarkNewPosition: Int - val selectedBookmarksPositionsSparseBooleanArray: SparseBooleanArray + val checkedBookmarkNewPosition: Int + val checkedBookmarksPositionsSparseBooleanArray: SparseBooleanArray - // Initialize the selected bookmark position. - var selectedBookmarkPosition = 0 + // Initialize the checked bookmark position. + var checkedBookmarkPosition = 0 // Get the menu item ID. val menuItemId = menuItem.itemId @@ -297,30 +302,33 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma // Run the commands according to the selected action item. if (menuItemId == R.id.move_bookmark_up) { // Move the bookmark up. // Get the array of checked bookmark positions. - selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.checkedItemPositions + checkedBookmarksPositionsSparseBooleanArray = bookmarksListView.checkedItemPositions // Get the position of the bookmark that is selected. If other bookmarks have previously been selected they will be included in the sparse boolean array with a value of `false`. - for (i in 0 until selectedBookmarksPositionsSparseBooleanArray.size()) { + for (i in 0 until checkedBookmarksPositionsSparseBooleanArray.size()) { // Check to see if the value for the bookmark is true, meaning it is currently selected. - if (selectedBookmarksPositionsSparseBooleanArray.valueAt(i)) { + if (checkedBookmarksPositionsSparseBooleanArray.valueAt(i)) { // Only one bookmark should have a value of `true` when move bookmark up is enabled. - selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(i) + checkedBookmarkPosition = checkedBookmarksPositionsSparseBooleanArray.keyAt(i) } } - // Calculate the new position of the selected bookmark. - selectedBookmarkNewPosition = selectedBookmarkPosition - 1 + // Calculate the new position of the checked bookmark. + checkedBookmarkNewPosition = checkedBookmarkPosition - 1 + + // Get the bookmarks count. + val bookmarksCount = bookmarksListView.count // Iterate through the bookmarks. - for (i in 0 until bookmarksListView.count) { + for (i in 0 until bookmarksCount) { // Get the database ID for the current bookmark. val currentBookmarkDatabaseId = bookmarksListView.getItemIdAtPosition(i).toInt() // Update the display order for the current bookmark. - if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark. + if (i == checkedBookmarkPosition) { // The current bookmark is the selected bookmark. // Move the current bookmark up one. bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1) - } else if ((i + 1) == selectedBookmarkPosition) { // The current bookmark is immediately above the selected bookmark. + } else if ((i + 1) == checkedBookmarkPosition) { // The current bookmark is immediately above the selected bookmark. // Move the current bookmark down one. bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1) } else { // The current bookmark is not changing positions. @@ -340,25 +348,25 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma bookmarksCursorAdapter.changeCursor(bookmarksCursor) // Scroll to the new bookmark position. - scrollBookmarks(selectedBookmarkNewPosition) + scrollBookmarks(checkedBookmarkNewPosition) // Update the enabled status of the move icons. updateMoveIcons() } else if (menuItemId == R.id.move_bookmark_down) { // Move the bookmark down. // Get the array of checked bookmark positions. - selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.checkedItemPositions + checkedBookmarksPositionsSparseBooleanArray = bookmarksListView.checkedItemPositions - // Get the position of the bookmark that is selected. If other bookmarks have previously been selected they will be included in the sparse boolean array with a value of `false`. - for (i in 0 until selectedBookmarksPositionsSparseBooleanArray.size()) { + // Get the position of the bookmark that is selected. If other bookmarks have previously been checked they will be included in the sparse boolean array with a value of `false`. + for (i in 0 until checkedBookmarksPositionsSparseBooleanArray.size()) { // Check to see if the value for the bookmark is true, meaning it is currently selected. - if (selectedBookmarksPositionsSparseBooleanArray.valueAt(i)) { + if (checkedBookmarksPositionsSparseBooleanArray.valueAt(i)) { // Only one bookmark should have a value of `true` when move bookmark down is enabled. - selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(i) + checkedBookmarkPosition = checkedBookmarksPositionsSparseBooleanArray.keyAt(i) } } - // Calculate the new position of the selected bookmark. - selectedBookmarkNewPosition = selectedBookmarkPosition + 1 + // Calculate the new position of the checked bookmark. + checkedBookmarkNewPosition = checkedBookmarkPosition + 1 // Iterate through the bookmarks. for (i in 0 until bookmarksListView.count) { @@ -366,10 +374,10 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma val currentBookmarkDatabaseId = bookmarksListView.getItemIdAtPosition(i).toInt() // Update the display order for the current bookmark. - if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark. + if (i == checkedBookmarkPosition) { // The current bookmark is the checked bookmark. // Move the current bookmark down one. bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1) - } else if (i - 1 == selectedBookmarkPosition) { // The current bookmark is immediately below the selected bookmark. + } else if ((i - 1) == checkedBookmarkPosition) { // The current bookmark is immediately below the checked bookmark. // Move the bookmark below the selected bookmark up one. bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1) } else { // The current bookmark is not changing positions. @@ -390,7 +398,7 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma bookmarksCursorAdapter.changeCursor(bookmarksCursor) // Scroll to the new bookmark position. - scrollBookmarks(selectedBookmarkNewPosition) + scrollBookmarks(checkedBookmarkNewPosition) // Update the enabled status of the move icons. updateMoveIcons() @@ -402,19 +410,19 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma moveToFolderDialog.show(supportFragmentManager, resources.getString(R.string.move_to_folder)) } else if (menuItemId == R.id.edit_bookmark) { // Get the array of checked bookmark positions. - selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.checkedItemPositions + checkedBookmarksPositionsSparseBooleanArray = bookmarksListView.checkedItemPositions // Get the position of the bookmark that is selected. If other bookmarks have previously been selected they will be included in the sparse boolean array with a value of `false`. - for (i in 0 until selectedBookmarksPositionsSparseBooleanArray.size()) { + for (i in 0 until checkedBookmarksPositionsSparseBooleanArray.size()) { // Check to see if the value for the bookmark is true, meaning it is currently selected. - if (selectedBookmarksPositionsSparseBooleanArray.valueAt(i)) { + if (checkedBookmarksPositionsSparseBooleanArray.valueAt(i)) { // Only one bookmark should have a value of `true` when move edit bookmark is enabled. - selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(i) + checkedBookmarkPosition = checkedBookmarksPositionsSparseBooleanArray.keyAt(i) } } // Move the cursor to the selected position. - bookmarksCursor.moveToPosition(selectedBookmarkPosition) + bookmarksCursor.moveToPosition(checkedBookmarkPosition) // Get the selected bookmark database ID. val databaseId = bookmarksCursor.getInt(bookmarksCursor.getColumnIndexOrThrow(ID)) @@ -437,14 +445,14 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes. deletingBookmarks = true - // Get an array of the selected row IDs. - val selectedBookmarksIdsLongArray = bookmarksListView.checkedItemIds + // Get an array of the checked row IDs. + val checkedBookmarksIdsLongArray = bookmarksListView.checkedItemIds // Initialize a variable to count the number of bookmarks to delete. var numberOfBookmarksToDelete = 0 // Count the number of bookmarks to delete. - for (databaseIdLong in selectedBookmarksIdsLongArray) { + for (databaseIdLong in checkedBookmarksIdsLongArray) { // Convert the database ID long to an int. val databaseIdInt = databaseIdLong.toInt() @@ -459,10 +467,10 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma } // Get an array of checked bookmarks. `.clone()` makes a copy that won't change if the list view is reloaded, which is needed for re-selecting the bookmarks on undelete. - selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.checkedItemPositions.clone() + checkedBookmarksPositionsSparseBooleanArray = bookmarksListView.checkedItemPositions.clone() // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs. - bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolderId) + bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(checkedBookmarksIdsLongArray, currentFolderId) // Update the list view. bookmarksCursorAdapter.changeCursor(bookmarksCursor) @@ -479,12 +487,24 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma // Update the list view. bookmarksCursorAdapter.changeCursor(bookmarksCursor) - // Re-select the previously selected bookmarks. - for (i in 0 until selectedBookmarksPositionsSparseBooleanArray.size()) - bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true) + // Get the number of checked bookmarks. + val numberOfCheckedBookmarks = checkedBookmarksPositionsSparseBooleanArray.size() + + // Set the checking many bookmarks flag. + checkingManyBookmarks = true + + // Re-check the previously checked bookmarks. + for (i in 0 until numberOfCheckedBookmarks) { + // Reset the checking many bookmarks flag on the last bookmark so the UI is updated. + if (i == (numberOfCheckedBookmarks - 1)) + checkingManyBookmarks = false + + // Check the bookmark. + bookmarksListView.setItemChecked(checkedBookmarksPositionsSparseBooleanArray.keyAt(i), true) + } } else { // The snackbar was dismissed without the undo button being pushed. // Delete each selected bookmark. - for (databaseIdLong in selectedBookmarksIdsLongArray) { + for (databaseIdLong in checkedBookmarksIdsLongArray) { // Convert the database long ID to an int. val databaseIdInt = databaseIdLong.toInt() @@ -533,8 +553,16 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma // Get the total number of bookmarks. val numberOfBookmarks = bookmarksListView.count + // Set the checking many bookmarks flag. + checkingManyBookmarks = true + // Select them all. for (i in 0 until numberOfBookmarks) { + // Reset the checking many bookmarks flag on the last bookmark so the UI is updated. + if (i == (numberOfBookmarks - 1)) + checkingManyBookmarks = false + + // Check the bookmark. bookmarksListView.setItemChecked(i, true) } } @@ -651,8 +679,16 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma // Get the total number of bookmarks. val numberOfBookmarks = bookmarksListView.count + // Set the checking many bookmarks flag. + checkingManyBookmarks = true + // Select them all. for (i in 0 until numberOfBookmarks) { + // Reset the checking many bookmarks flag on the last bookmark so the UI is updated. + if (i == (numberOfBookmarks - 1)) + checkingManyBookmarks = false + + // Check the bookmark. bookmarksListView.setItemChecked(i, true) } } else if (menuItemId == R.id.bookmarks_database_view) { @@ -671,17 +707,29 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma return true } - override fun createBookmark(dialogFragment: DialogFragment, favoriteIconBitmap: Bitmap) { + override fun createBookmark(dialogFragment: DialogFragment) { // Get the alert dialog from the fragment. val dialog = dialogFragment.dialog!! - // Get the views from the dialog fragment. - val createBookmarkNameEditText = dialog.findViewById(R.id.create_bookmark_name_edittext) - val createBookmarkUrlEditText = dialog.findViewById(R.id.create_bookmark_url_edittext) + // Get handles for the views from the dialog fragment. + val webpageFavoriteIconRadioButton = dialog.findViewById(R.id.webpage_favorite_icon_radiobutton) + val webpageFavoriteIconImageView = dialog.findViewById(R.id.webpage_favorite_icon_imageview) + val customIconImageView = dialog.findViewById(R.id.custom_icon_imageview) + val bookmarkNameEditText = dialog.findViewById(R.id.bookmark_name_edittext) + val bookmarkUrlEditText = dialog.findViewById(R.id.bookmark_url_edittext) - // Extract the strings from the edit texts. - val bookmarkNameString = createBookmarkNameEditText.text.toString() - val bookmarkUrlString = createBookmarkUrlEditText.text.toString() + // Get the strings from the edit texts. + val bookmarkNameString = bookmarkNameEditText.text.toString() + val bookmarkUrlString = bookmarkUrlEditText.text.toString() + + // Get the selected favorite icon drawable. + val favoriteIconDrawable = if (webpageFavoriteIconRadioButton.isChecked) // The webpage favorite icon is checked. + webpageFavoriteIconImageView.drawable + else // The custom favorite icon is checked. + customIconImageView.drawable + + // Convert the favorite icon drawable to a bitmap. Once the minimum API >= 33, this can use Bitmap.Config.RGBA_1010102. + val favoriteIconBitmap = favoriteIconDrawable.toBitmap(128, 128, Bitmap.Config.ARGB_8888) // Create a favorite icon byte array output stream. val favoriteIconByteArrayOutputStream = ByteArrayOutputStream() @@ -708,32 +756,34 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma bookmarksListView.setSelection(newBookmarkDisplayOrder) } - override fun createBookmarkFolder(dialogFragment: DialogFragment, favoriteIconBitmap: Bitmap) { + override fun createBookmarkFolder(dialogFragment: DialogFragment) { // Get the dialog from the dialog fragment. val dialog = dialogFragment.dialog!! // Get handles for the views in the dialog fragment. + val defaultFolderIconRadioButton = dialog.findViewById(R.id.default_folder_icon_radiobutton) + val defaultFolderIconImageView = dialog.findViewById(R.id.default_folder_icon_imageview) + val webpageFavoriteIconRadioButton = dialog.findViewById(R.id.webpage_favorite_icon_radiobutton) + val webpageFavoriteIconImageView = dialog.findViewById(R.id.webpage_favorite_icon_imageview) + val customIconImageView = dialog.findViewById(R.id.custom_icon_imageview) val folderNameEditText = dialog.findViewById(R.id.folder_name_edittext) - val defaultIconRadioButton = dialog.findViewById(R.id.default_icon_radiobutton) - val defaultIconImageView = dialog.findViewById(R.id.default_icon_imageview) - // Get new folder name string. + // Get the folder name string. val folderNameString = folderNameEditText.text.toString() - // Set the folder icon bitmap according to the dialog. - val folderIconBitmap = if (defaultIconRadioButton.isChecked) { // Use the default folder icon. - // Get the default folder icon drawable. - val folderIconDrawable = defaultIconImageView.drawable + // Get the selected folder icon drawable. + val folderIconDrawable = if (defaultFolderIconRadioButton.isChecked) // Use the default folder icon. + defaultFolderIconImageView.drawable + else if (webpageFavoriteIconRadioButton.isChecked) // Use the webpage favorite icon. + webpageFavoriteIconImageView.drawable + else // Use the custom icon. + customIconImageView.drawable - // Convert the folder icon drawable to a bitmap drawable. - val folderIconBitmapDrawable = folderIconDrawable as BitmapDrawable + // Cast the folder icon bitmap to a bitmap drawable. + val folderIconBitmapDrawable = folderIconDrawable as BitmapDrawable - // Convert the folder icon bitmap drawable to a bitmap. - folderIconBitmapDrawable.bitmap - } else { // Use the WebView favorite icon. - // Copy the favorite icon bitmap to the folder icon bitmap. - favoriteIconBitmap - } + // Convert the folder icon bitmap drawable to a bitmap. + val folderIconBitmap = folderIconBitmapDrawable.bitmap // Create a folder icon byte array output stream. val folderIconByteArrayOutputStream = ByteArrayOutputStream() @@ -747,11 +797,11 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma // Move all the bookmarks down one in the display order. for (i in 0 until bookmarksListView.count) { val databaseId = bookmarksListView.getItemIdAtPosition(i).toInt() - bookmarksDatabaseHelper.updateDisplayOrder(databaseId, i + 1) + bookmarksDatabaseHelper.updateDisplayOrder(databaseId, displayOrder = i + 1) } // Create the folder, which will be placed at the top of the list view. - bookmarksDatabaseHelper.createFolder(folderNameString, currentFolderId, folderIconByteArray) + bookmarksDatabaseHelper.createFolder(folderNameString, currentFolderId, displayOrder = 0, folderIconByteArray) // Update the bookmarks cursor with the contents of the current folder. bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolderId) @@ -763,16 +813,19 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma bookmarksListView.setSelection(0) } - override fun onSaveBookmark(dialogFragment: DialogFragment, selectedBookmarkDatabaseId: Int, favoriteIconBitmap: Bitmap) { + override fun saveBookmark(dialogFragment: DialogFragment, selectedBookmarkDatabaseId: Int) { // Get the dialog from the dialog fragment. val dialog = dialogFragment.dialog!! // Get handles for the views from the dialog fragment. + val currentIconRadioButton = dialog.findViewById(R.id.current_icon_radiobutton) + val webpageFavoriteIconRadioButton = dialog.findViewById(R.id.webpage_favorite_icon_radiobutton) + val webpageFavoriteIconImageView = dialog.findViewById(R.id.webpage_favorite_icon_imageview) + val customIconImageView = dialog.findViewById(R.id.custom_icon_imageview) val bookmarkNameEditText = dialog.findViewById(R.id.bookmark_name_edittext) val bookmarkUrlEditText = dialog.findViewById(R.id.bookmark_url_edittext) - val currentIconRadioButton = dialog.findViewById(R.id.current_icon_radiobutton) - // Get the bookmark strings. + // Get the strings from the edit texts. val bookmarkNameString = bookmarkNameEditText.text.toString() val bookmarkUrlString = bookmarkUrlEditText.text.toString() @@ -780,6 +833,15 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma if (currentIconRadioButton.isChecked) { // Update the bookmark without changing the favorite icon. bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString) } else { // Update the bookmark using the WebView favorite icon. + // Get the selected favorite icon drawable. + val favoriteIconDrawable = if (webpageFavoriteIconRadioButton.isChecked) // The webpage favorite icon is checked. + webpageFavoriteIconImageView.drawable + else // The custom icon is checked. + customIconImageView.drawable + + // Convert the favorite icon drawable to a bitmap. Once the minimum API >= 33, this can use Bitmap.Config.RGBA_1010102. + val favoriteIconBitmap = favoriteIconDrawable.toBitmap(128, 128, Bitmap.Config.ARGB_8888) + // Create a favorite icon byte array output stream. val newFavoriteIconByteArrayOutputStream = ByteArrayOutputStream() @@ -803,40 +865,39 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma bookmarksCursorAdapter.changeCursor(bookmarksCursor) } - override fun onSaveBookmarkFolder(dialogFragment: DialogFragment, selectedFolderDatabaseId: Int, favoriteIconBitmap: Bitmap) { + override fun saveBookmarkFolder(dialogFragment: DialogFragment, selectedFolderDatabaseId: Int) { // Get the dialog from the dialog fragment. val dialog = dialogFragment.dialog!! // Get handles for the views from the dialog fragment. val currentFolderIconRadioButton = dialog.findViewById(R.id.current_icon_radiobutton) - val defaultFolderIconRadioButton = dialog.findViewById(R.id.default_icon_radiobutton) - val defaultFolderIconImageView = dialog.findViewById(R.id.default_icon_imageview) - val editFolderNameEditText = dialog.findViewById(R.id.folder_name_edittext) + val defaultFolderIconRadioButton = dialog.findViewById(R.id.default_folder_icon_radiobutton) + val defaultFolderIconImageView = dialog.findViewById(R.id.default_folder_icon_imageview) + val webpageFavoriteIconRadioButton = dialog.findViewById(R.id.webpage_favorite_icon_radiobutton) + val webpageFavoriteIconImageView = dialog.findViewById(R.id.webpage_favorite_icon_imageview) + val customIconImageView = dialog.findViewById(R.id.custom_icon_imageview) + val folderNameEditText = dialog.findViewById(R.id.folder_name_edittext) // Get the new folder name. - val newFolderName = editFolderNameEditText.text.toString() + val newFolderName = folderNameEditText.text.toString() - // Check if the favorite icon has changed. + // Check if the folder icon has changed. if (currentFolderIconRadioButton.isChecked) { // Only the name has changed. // Update the name in the database. bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, newFolderName) } else { // The icon has changed. - // Populate the new folder icon bitmap. - val folderIconBitmap: Bitmap = if (defaultFolderIconRadioButton.isChecked) { - // Get the default folder icon drawable. - val folderIconDrawable = defaultFolderIconImageView.drawable - - // Convert the folder icon drawable to a bitmap drawable. - val folderIconBitmapDrawable = folderIconDrawable as BitmapDrawable - - // Convert the folder icon bitmap drawable to a bitmap. - folderIconBitmapDrawable.bitmap - } else { // Use the WebView favorite icon. - // Copy the favorite icon bitmap to the folder icon bitmap. - favoriteIconBitmap - } - - // Create a folder icon byte array output stream. + // Get the selected folder icon drawable. + val folderIconDrawable = if (defaultFolderIconRadioButton.isChecked) // The default folder icon is checked. + defaultFolderIconImageView.drawable + else if (webpageFavoriteIconRadioButton.isChecked) // The webpage favorite icon is checked. + webpageFavoriteIconImageView.drawable + else // The custom icon is checked. + customIconImageView.drawable + + // Convert the folder icon drawable to a bitmap. Once the minimum API >= 33, this can use Bitmap.Config.RGBA_1010102. + val folderIconBitmap = folderIconDrawable.toBitmap(128, 128, Bitmap.Config.ARGB_8888) + + // Create a new folder icon byte array output stream. val newFolderIconByteArrayOutputStream = ByteArrayOutputStream() // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG). @@ -907,7 +968,7 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma val folderId = bookmarksDatabaseHelper.getFolderId(folderDatabaseId) // Get the contents of the folder. - val folderCursor = bookmarksDatabaseHelper.getBookmarkIds(folderId) + val folderCursor = bookmarksDatabaseHelper.getBookmarkAndFolderIds(folderId) // Initialize the bookmark counter. var bookmarkCounter = 0 @@ -937,7 +998,7 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBookma val folderId = bookmarksDatabaseHelper.getFolderId(folderDatabaseId) // Get the contents of the folder. - val folderCursor = bookmarksDatabaseHelper.getBookmarkIds(folderId) + val folderCursor = bookmarksDatabaseHelper.getBookmarkAndFolderIds(folderId) // Delete each of the bookmarks in the folder. for (i in 0 until folderCursor.count) {