X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FEditBookmarkDatabaseViewDialog.java;h=b0d401bb6a12cbd38dcead1a05988bfe92424ea7;hb=55169899d6454cd57e40d32a792735df51caee85;hp=028db1b79c7c538cd4bc3b16ce88cfc170fe2f7b;hpb=0a5d2eabceeafb49a957598538aa74d4f11dfce0;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkDatabaseViewDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkDatabaseViewDialog.java index 028db1b7..b0d401bb 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkDatabaseViewDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkDatabaseViewDialog.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2018 Soren Stoutner . + * Copyright © 2016-2019 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -30,10 +30,6 @@ import android.database.MergeCursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; -import android.support.annotation.NonNull; -// `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22. -import android.support.v4.widget.ResourceCursorAdapter; -import android.support.v7.app.AppCompatDialogFragment; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; @@ -45,6 +41,7 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.RadioGroup; +import android.widget.ResourceCursorAdapter; import android.widget.Spinner; import android.widget.TextView; @@ -52,13 +49,16 @@ import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.activities.MainWebViewActivity; import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper; -public class EditBookmarkDatabaseViewDialog extends AppCompatDialogFragment { +import androidx.annotation.NonNull; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22. + +public class EditBookmarkDatabaseViewDialog extends DialogFragment { // Instantiate the constants. public static final int HOME_FOLDER_DATABASE_ID = -1; // Instantiate the class variables. private EditBookmarkDatabaseViewListener editBookmarkDatabaseViewListener; - private int bookmarkDatabaseId; private String currentBookmarkName; private String currentUrl; private int currentFolderDatabaseId; @@ -72,7 +72,7 @@ public class EditBookmarkDatabaseViewDialog extends AppCompatDialogFragment { // The public interface is used to send information back to the parent activity. public interface EditBookmarkDatabaseViewListener { - void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId); + void onSaveBookmark(DialogFragment dialogFragment, int selectedBookmarkDatabaseId); } @Override @@ -80,12 +80,8 @@ public class EditBookmarkDatabaseViewDialog extends AppCompatDialogFragment { // Run the default commands. super.onAttach(context); - // Get a handle for `EditBookmarkDatabaseViewListener` from `context`. - try { - editBookmarkDatabaseViewListener = (EditBookmarkDatabaseViewListener) context; - } catch(ClassCastException exception) { - throw new ClassCastException(context.toString() + " must implement EditBookmarkDatabaseViewListener."); - } + // Get a handle for edit bookmark database view listener from the launching context. + editBookmarkDatabaseViewListener = (EditBookmarkDatabaseViewListener) context; } // Store the database ID in the arguments bundle. @@ -104,28 +100,22 @@ public class EditBookmarkDatabaseViewDialog extends AppCompatDialogFragment { return editBookmarkDatabaseViewDialog; } + // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. + @SuppressLint("InflateParams") @Override - public void onCreate(Bundle savedInstanceState) { - // Run the default commands. - super.onCreate(savedInstanceState); - + @NonNull + public Dialog onCreateDialog(Bundle savedInstanceState) { // Remove the incorrect lint warning below that `getInt()` might be null. assert getArguments() != null; - // Store the bookmark database ID in the class variable. - bookmarkDatabaseId = getArguments().getInt("Database ID"); - } + // Get the bookmark database ID from the bundle. + int bookmarkDatabaseId = getArguments().getInt("Database ID"); - // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. - @SuppressLint("InflateParams") - @Override - @NonNull - public Dialog onCreateDialog(Bundle savedInstanceState) { - // Initialize the database helper. The two `nulls` do not specify the database name or a `CursorFactory`. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`. + // Initialize the database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`. BookmarksDatabaseHelper bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0); // Get a cursor with the selected bookmark and move it to the first position. - Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmarkCursor(bookmarkDatabaseId); + Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(bookmarkDatabaseId); bookmarkCursor.moveToFirst(); // Use an alert dialog builder to create the alert dialog. @@ -169,9 +159,6 @@ public class EditBookmarkDatabaseViewDialog extends AppCompatDialogFragment { alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); } - // Set the keyboard to be hidden when the `AlertDialog` is first shown. If this is not set, the `AlertDialog` will not shrink when the keyboard is displayed. - alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - // The alert dialog must be shown before items in the layout can be modified. alertDialog.show(); @@ -204,20 +191,28 @@ public class EditBookmarkDatabaseViewDialog extends AppCompatDialogFragment { // Display `currentIconBitmap` in `edit_bookmark_current_icon`. currentIconImageView.setImageBitmap(currentIconBitmap); - // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_bookmark_web_page_favorite_icon`. - newFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIconBitmap); + // Get a copy of the favorite icon bitmap. + Bitmap favoriteIconBitmap = MainWebViewActivity.favoriteIconBitmap; + + // Scale the favorite icon bitmap down if it is larger than 256 x 256. Filtering uses bilinear interpolation. + if ((favoriteIconBitmap.getHeight() > 256) || (favoriteIconBitmap.getWidth() > 256)) { + favoriteIconBitmap = Bitmap.createScaledBitmap(favoriteIconBitmap, 256, 256, true); + } + + // Set the new favorite icon bitmap. + newFavoriteIconImageView.setImageBitmap(favoriteIconBitmap); // Populate the bookmark name and URL `EditTexts`. nameEditText.setText(currentBookmarkName); urlEditText.setText(currentUrl); - // Setup a `MatrixCursor` "Home Folder". + // Setup a matrix cursor for "Home Folder". String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME}; MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames); matrixCursor.addRow(new Object[]{HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)}); - // Get a `Cursor` with the list of all the folders. - Cursor foldersCursor = bookmarksDatabaseHelper.getAllFoldersCursor(); + // Get a cursor with the list of all the folders. + Cursor foldersCursor = bookmarksDatabaseHelper.getAllFolders(); // Combine `matrixCursor` and `foldersCursor`. MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor}); @@ -225,20 +220,36 @@ public class EditBookmarkDatabaseViewDialog extends AppCompatDialogFragment { // Remove the incorrect lint warning below that `getContext()` might be null. assert getContext() != null; - // Create a `ResourceCursorAdapter` for the `Spinner`. `0` specifies no flags.; - ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(getContext(), R.layout.edit_bookmark_databaseview_spinner_item, foldersMergeCursor, 0) { + // Create a resource cursor adapter for the spinner. + ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(getContext(), R.layout.databaseview_spinner_item, foldersMergeCursor, 0) { @Override public void bindView(View view, Context context, Cursor cursor) { - // Get a handle for the `Spinner` item `TextView`. + // Get handles for the spinner views. + ImageView spinnerItemImageView = view.findViewById(R.id.spinner_item_imageview); TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview); - // Set the `TextView` to display the folder name. + // Set the folder icon according to the type. + if (foldersMergeCursor.getPosition() == 0) { // Set the `Home Folder` icon. + // Set the gray folder image. `ContextCompat` must be used until the minimum API >= 21. + spinnerItemImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.folder_gray)); + } else { // Set a user folder icon. + // Get the folder icon byte array. + byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)); + + // Convert the byte array to a bitmap beginning at the first byte and ending at the last. + Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length); + + // Set the folder icon. + spinnerItemImageView.setImageBitmap(folderIconBitmap); + } + + // Set the text view to display the folder name. spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME))); } }; // Set the `ResourceCursorAdapter` drop drown view resource. - foldersCursorAdapter.setDropDownViewResource(R.layout.edit_bookmark_databaseview_spinner_dropdown_item); + foldersCursorAdapter.setDropDownViewResource(R.layout.databaseview_spinner_dropdown_items); // Set the adapter for the folder `Spinner`. folderSpinner.setAdapter(foldersCursorAdapter);