+
+ // Set the current folder database ID.
+ currentFolderDatabaseId = ALL_FOLDERS_DATABASE_ID;
+
+ // Set a listener to edit a bookmark when it is tapped.
+ bookmarksListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
+ // Convert the database ID to an `int`.
+ int databaseId = (int) id;
+
+ // Show the edit bookmark or edit bookmark folder dialog.
+ if (bookmarksDatabaseHelper.isFolder(databaseId)) {
+ // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
+ oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
+
+ // Show the edit bookmark folder dialog.
+ AppCompatDialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId);
+ editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
+ } else {
+ // Show the edit bookmark dialog.
+ AppCompatDialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId);
+ editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
+ }
+ });
+ }
+
+ @Override
+ public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
+ // Get handles for the views from dialog fragment.
+ RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
+ EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
+ EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
+ Spinner folderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_folder_spinner);
+ EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_display_order_edittext);
+
+ // Extract the bookmark information.
+ String bookmarkNameString = editBookmarkNameEditText.getText().toString();
+ String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
+ int folderDatabaseId = (int) folderSpinner.getSelectedItemId();
+ int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
+
+ // Instantiate the parent folder name `String`.
+ String parentFolderNameString;
+
+ // Set the parent folder name.
+ if (folderDatabaseId == EditBookmarkDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
+ parentFolderNameString = "";
+ } else { // Get the parent folder name from the database.
+ parentFolderNameString = bookmarksDatabaseHelper.getFolderName(folderDatabaseId);
+ }
+
+ // Update the bookmark.
+ if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
+ bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt);
+ } else { // Update the bookmark using the `WebView` favorite icon.
+ // Convert the favorite icon to a byte array. `0` is for lossless compression (the only option for a PNG).
+ ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
+ MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
+ byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
+
+ // Update the bookmark and the favorite icon.
+ bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt, newFavoriteIconByteArray);
+ }
+
+ // Update `bookmarksCursor` with the contents of the current folder.
+ switch (currentFolderDatabaseId) {
+ case ALL_FOLDERS_DATABASE_ID:
+ // Get a cursor with all the bookmarks.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
+ break;
+
+ case HOME_FOLDER_DATABASE_ID:
+ // Get a cursor with all the bookmarks in the home folder.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
+ break;
+
+ default:
+ // Get a cursor with all the bookmarks in the current folder.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
+ }
+
+ // Update the `ListView`.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+ }
+
+ @Override
+ public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
+ // Get handles for the views from dialog fragment.
+ RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
+ RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
+ ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
+ EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
+ Spinner parentFolderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_folder_parent_folder_spinner);
+ EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_display_order_edittext);
+
+ // Extract the folder information.
+ String newFolderNameString = editBookmarkNameEditText.getText().toString();
+ int parentFolderDatabaseId = (int) parentFolderSpinner.getSelectedItemId();
+ int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
+
+ // Instantiate the parent folder name `String`.
+ String parentFolderNameString;
+
+ // Set the parent folder name.
+ if (parentFolderDatabaseId == EditBookmarkFolderDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
+ parentFolderNameString = "";
+ } else { // Get the parent folder name from the database.
+ parentFolderNameString = bookmarksDatabaseHelper.getFolderName(parentFolderDatabaseId);
+ }
+
+ // Update the folder.
+ if (currentBookmarkIconRadioButton.isChecked()) { // Update the folder without changing the favorite icon.
+ bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt);
+ } else { // Update the folder and the icon.
+ // Instantiate the new folder icon `Bitmap`.
+ Bitmap folderIconBitmap;
+
+ // Populate the new folder icon bitmap.
+ if (defaultFolderIconRadioButton.isChecked()) {
+ // Get the default folder icon and convert it to a `Bitmap`.
+ Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
+ BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+ folderIconBitmap = folderIconBitmapDrawable.getBitmap();
+ } else { // Use the `WebView` favorite icon.
+ folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
+ }
+
+ // Convert the folder icon to a byte array. `0` is for lossless compression (the only option for a PNG).
+ ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
+ folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
+ byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
+
+ // Update the folder and the icon.
+ bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt, newFolderIconByteArray);
+ }
+
+ // Update `bookmarksCursor` with the contents of the current folder.
+ switch (currentFolderDatabaseId) {
+ case ALL_FOLDERS_DATABASE_ID:
+ // Get a cursor with all the bookmarks.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
+ break;
+
+ case HOME_FOLDER_DATABASE_ID:
+ // Get a cursor with all the bookmarks in the home folder.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
+ break;
+
+ default:
+ // Get a cursor with all the bookmarks in the current folder.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
+ }
+
+ // Update the `ListView`.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+ }
+
+ @Override
+ public void onDestroy() {
+ // Close the bookmarks cursor and database.
+ bookmarksCursor.close();
+ bookmarksDatabaseHelper.close();
+
+ // Run the default commands.
+ super.onDestroy();