X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FEditBookmarkFolderDatabaseViewDialog.java;h=bb02fda23a650b6d1d019b700f86d7dc0ea13454;hp=41bfbd05962f7e93dd4c9ec07b9654637d12926c;hb=55169899d6454cd57e40d32a792735df51caee85;hpb=9f551f25b53a30cca7b19b6e6bfc2d2520d9aa1b diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkFolderDatabaseViewDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkFolderDatabaseViewDialog.java index 41bfbd05..bb02fda2 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkFolderDatabaseViewDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkFolderDatabaseViewDialog.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 Soren Stoutner . + * Copyright © 2016-2019 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -31,10 +31,6 @@ import android.database.MergeCursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v4.widget.ResourceCursorAdapter; -// `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22. -import android.support.v7.app.AppCompatDialogFragment; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; @@ -46,21 +42,25 @@ 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; +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. + import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.activities.MainWebViewActivity; import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper; -public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragment { +public class EditBookmarkFolderDatabaseViewDialog extends DialogFragment { // Instantiate the constants. public static final int HOME_FOLDER_DATABASE_ID = -1; // Instantiate the class variables. private EditBookmarkFolderDatabaseViewListener editBookmarkFolderDatabaseViewListener; private BookmarksDatabaseHelper bookmarksDatabaseHelper; - private int folderDatabaseId; private StringBuilder exceptFolders; private String currentFolderName; private int currentParentFolderDatabaseId; @@ -73,19 +73,15 @@ public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragmen // The public interface is used to send information back to the parent activity. public interface EditBookmarkFolderDatabaseViewListener { - void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedFolderDatabaseId); + void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedFolderDatabaseId); } public void onAttach(Context context) { // Run the default commands. super.onAttach(context); - // Get a handle for `EditBookmarkDatabaseViewListener` from `context`. - try { - editBookmarkFolderDatabaseViewListener = (EditBookmarkFolderDatabaseViewListener) context; - } catch(ClassCastException exception) { - throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderDatabaseViewListener."); - } + // Get a handle for `EditBookmarkDatabaseViewListener` from the launching context. + editBookmarkFolderDatabaseViewListener = (EditBookmarkFolderDatabaseViewListener) context; } // Store the database ID in the arguments bundle. @@ -104,28 +100,25 @@ public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragmen return editBookmarkFolderDatabaseViewDialog; } - @Override - public void onCreate(Bundle savedInstanceState) { - // Run the default commands. - super.onCreate(savedInstanceState); - - // Store the bookmark database ID in the class variable. - folderDatabaseId = 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) { + // Remove the incorrect lint warning that `getInt()` might be null. + assert getArguments() != null; + + // Get the bookmark database ID from the bundle. + int folderDatabaseId = getArguments().getInt("Database ID"); + // 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`. bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0); // Get a `Cursor` with the selected bookmark and move it to the first position. - Cursor folderCursor = bookmarksDatabaseHelper.getBookmarkCursor(folderDatabaseId); + Cursor folderCursor = bookmarksDatabaseHelper.getBookmark(folderDatabaseId); folderCursor.moveToFirst(); - // Use `AlertDialog.Builder` to create the `AlertDialog`. + // Use an alert dialog builder to create the alert dialog. AlertDialog.Builder dialogBuilder; // Set the style according to the theme. @@ -138,30 +131,35 @@ public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragmen // Set the title. dialogBuilder.setTitle(R.string.edit_folder); + // Remove the incorrect lint warning that `getActivity()` might be null. + assert getActivity() != null; + // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`. dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_folder_databaseview_dialog, null)); - // Set an `onClick()` listener for the negative button. + // Set the listener for the negative button. dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> { // Do nothing. The `AlertDialog` will close automatically. }); - // Set the `onClick()` listener fo the positive button. + // Set the listener fo the positive button. dialogBuilder.setPositiveButton(R.string.save, (DialogInterface dialog, int which) -> { // Return the `DialogFragment` to the parent activity on save. editBookmarkFolderDatabaseViewListener.onSaveBookmarkFolder(EditBookmarkFolderDatabaseViewDialog.this, folderDatabaseId); }); - // Create an `AlertDialog` from the `AlertDialog.Builder`. + // Create an alert dialog from the alert dialog builder. final AlertDialog alertDialog = dialogBuilder.create(); - // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`. + // Remove the warning below that `getWindow()` might be null. assert alertDialog.getWindow() != null; - // 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); + // Disable screenshots if not allowed. + if (!MainWebViewActivity.allowScreenshots) { + alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); + } - // The `AlertDialog` must be shown before items in the layout can be modified. + // The alert dialog must be shown before items in the layout can be modified. alertDialog.show(); // Get handles for the layout items. @@ -189,46 +187,73 @@ public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragmen // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length); - // Display `currentIconBitmap` in `edit_bookmark_current_icon`. + // Display the current icon bitmap 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); + } - // Populate the folder name `EditText`. + // Set the new favorite icon bitmap. + newFavoriteIconImageView.setImageBitmap(favoriteIconBitmap); + + // Populate the folder name edit text. nameEditText.setText(currentFolderName); - // 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)}); - // Initialize a `StringBuilder` to track the folders not to display in the `Spinner` and populate it with the current folder. + // Initialize a string builder to track the folders not to display in the spinner and populate it with the current folder. exceptFolders = new StringBuilder(DatabaseUtils.sqlEscapeString(currentFolderName)); // Add all subfolders of the current folder to the list of folders not to display. addSubfoldersToExceptFolders(currentFolderName); - // Get a `Cursor` with the list of all the folders. - Cursor foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString()); + // Get a cursor with the list of all the folders. + Cursor foldersCursor = bookmarksDatabaseHelper.getFoldersExcept(exceptFolders.toString()); - // Combine `matrixCursor` and `foldersCursor`. + // Combine the matrix cursor and the folders cursor. MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor}); - // Create a `ResourceCursorAdapter` for the `Spinner`. `0` specifies no flags.; - ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(getContext(), R.layout.edit_bookmark_databaseview_spinner_item, foldersMergeCursor, 0) { + // Remove the incorrect lint warning that `getContext()` might be null. + assert getContext() != null; + + // 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); @@ -236,7 +261,7 @@ public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragmen // Select the current folder in the `Spinner` if the bookmark isn't in the "Home Folder". if (!parentFolder.equals("")) { // Get the database ID of the parent folder. - int folderDatabaseId = bookmarksDatabaseHelper.getFolderDatabaseId(folderCursor.getString(folderCursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER))); + int parentFolderDatabaseId = bookmarksDatabaseHelper.getFolderDatabaseId(folderCursor.getString(folderCursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER))); // Initialize `parentFolderPosition` and the iteration variable. int parentFolderPosition = 0; @@ -244,7 +269,7 @@ public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragmen // Find the parent folder position in folders `ResourceCursorAdapter`. do { - if (foldersCursorAdapter.getItemId(i) == folderDatabaseId) { + if (foldersCursorAdapter.getItemId(i) == parentFolderDatabaseId) { // Store the current position for the parent folder. parentFolderPosition = i; } else { @@ -370,7 +395,7 @@ public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragmen String newDisplayOrder = displayOrderEditText.getText().toString(); // Get a cursor for the new folder name if it exists. - Cursor folderExistsCursor = bookmarksDatabaseHelper.getFolderCursor(newFolderName); + Cursor folderExistsCursor = bookmarksDatabaseHelper.getFolder(newFolderName); // Is the new folder name empty? boolean folderNameNotEmpty = !newFolderName.isEmpty(); @@ -399,7 +424,7 @@ public class EditBookmarkFolderDatabaseViewDialog extends AppCompatDialogFragmen private void addSubfoldersToExceptFolders(String folderName) { // Get a `Cursor` will all the immediate subfolders. - Cursor subfoldersCursor = bookmarksDatabaseHelper.getSubfoldersCursor(folderName); + Cursor subfoldersCursor = bookmarksDatabaseHelper.getSubfolders(folderName); for (int i = 0; i < subfoldersCursor.getCount(); i++) { // Move `subfolderCursor` to the current item.