X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FCreateBookmarkDialog.java;h=a524d266c02c0e34c07591b3f0904ae7442d521b;hp=00e604e05274b02f2a48997773187cf2fe4dd550;hb=ff636c77836983a078f33e60616204518dab61d1;hpb=231b7c038227e36f96ed28e9c2ff8bde793133ec diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkDialog.java index 00e604e0..a524d266 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkDialog.java @@ -44,14 +44,9 @@ import com.stoutner.privacybrowser.R; import java.io.ByteArrayOutputStream; public class CreateBookmarkDialog extends DialogFragment { - // Create the class variables. - String url; - String title; - Bitmap favoriteIconBitmap; - // The public interface is used to send information back to the parent activity. public interface CreateBookmarkListener { - void onCreateBookmark(DialogFragment dialogFragment); + void onCreateBookmark(DialogFragment dialogFragment, Bitmap favoriteIconBitmap); } // The create bookmark listener is initialized in `onAttach()` and used in `onCreateDialog()`. @@ -66,11 +61,6 @@ public class CreateBookmarkDialog extends DialogFragment { } public static CreateBookmarkDialog createBookmark(String url, String title, Bitmap 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); - } - // Create a favorite icon byte array output stream. ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream(); @@ -80,38 +70,38 @@ public class CreateBookmarkDialog extends DialogFragment { // Convert the byte array output stream to a byte array. byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray(); - // Create a bundle. - Bundle bundle = new Bundle(); + // Create an arguments bundle. + Bundle argumentsBundle = new Bundle(); - // Store the variables in the bundle - bundle.putString("url", url); - bundle.putString("title", title); - bundle.putByteArray("favorite_icon_byte_array", favoriteIconByteArray); + // Store the variables in the bundle. + argumentsBundle.putString("url", url); + argumentsBundle.putString("title", title); + argumentsBundle.putByteArray("favorite_icon_byte_array", favoriteIconByteArray); // Create a new instance of the dialog. CreateBookmarkDialog createBookmarkDialog = new CreateBookmarkDialog(); // Add the bundle to the dialog. - createBookmarkDialog.setArguments(bundle); + createBookmarkDialog.setArguments(argumentsBundle); // Return the new dialog. return createBookmarkDialog; } + // `@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) { // Get the arguments. Bundle arguments = getArguments(); // Remove the incorrect lint warning below that the arguments might be null. assert arguments != null; - // Store the contents of the arguments in class variables. - url = arguments.getString("url"); - title = arguments.getString("title"); + // Get the strings from the arguments. + String url = arguments.getString("url"); + String title = arguments.getString("title"); // Get the favorite icon byte array. byte[] favoriteIconByteArray = arguments.getByteArray("favorite_icon_byte_array"); @@ -119,15 +109,9 @@ public class CreateBookmarkDialog extends DialogFragment { // Remove the incorrect lint warning below that the favorite icon byte array might be null. assert favoriteIconByteArray != null; - // Convert the favorite icon byte array to a bitmap and store it in a class variable. - favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); - } + // Convert the favorite icon byte array to a bitmap. + Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); - // `@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) { // Get a handle for the shared preferences. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); @@ -166,11 +150,11 @@ public class CreateBookmarkDialog extends DialogFragment { // Set an `onClick()` listener for the positive button. dialogBuilder.setPositiveButton(R.string.create, (DialogInterface dialog, int which) -> { // Return the `DialogFragment` to the parent activity. - createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this); + createBookmarkListener.onCreateBookmark(this, favoriteIconBitmap); }); // Create an `AlertDialog` from the `AlertDialog.Builder`. - final AlertDialog alertDialog = dialogBuilder.create(); + AlertDialog alertDialog = dialogBuilder.create(); // Remove the warning below that `getWindow()` might be null. assert alertDialog.getWindow() != null; @@ -197,7 +181,7 @@ public class CreateBookmarkDialog extends DialogFragment { // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`. if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) { // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity. - createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this); + createBookmarkListener.onCreateBookmark(this, favoriteIconBitmap); // Manually dismiss the `AlertDialog`. alertDialog.dismiss(); @@ -218,7 +202,7 @@ public class CreateBookmarkDialog extends DialogFragment { // If the event is a key-down on the "enter" key, select the PositiveButton "Create". if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) { // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity. - createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this); + createBookmarkListener.onCreateBookmark(this, favoriteIconBitmap); // Manually dismiss the `AlertDialog`. alertDialog.dismiss();