- // Get a handle for `CreateBookmarkListener` from `context`.
- try {
- createBookmarkListener = (CreateBookmarkListener) context;
- } catch(ClassCastException exception) {
- throw new ClassCastException(context.toString() + " must implement `CreateBookmarkListener`.");
- }
+ // Get a handle for `CreateBookmarkListener` from the launching context.
+ createBookmarkListener = (CreateBookmarkListener) context;
+ }
+
+ public static CreateBookmarkDialog createBookmark(String url, String title, Bitmap favoriteIconBitmap) {
+ // Create a favorite icon byte array output stream.
+ ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
+
+ // Convert the favorite icon to a PNG and place it in the byte array output stream. `0` is for lossless compression (the only option for a PNG).
+ favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
+
+ // Convert the byte array output stream to a byte array.
+ byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
+
+ // Create an arguments bundle.
+ Bundle argumentsBundle = new Bundle();
+
+ // 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(argumentsBundle);
+
+ // Return the new dialog.
+ return createBookmarkDialog;