]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkDialog.java
Use the Content-Disposition header to get file names for downloads. https://redmine...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateBookmarkDialog.java
index 00e604e05274b02f2a48997773187cf2fe4dd550..f89649bc542d8f9910be6c37f74630a252b01f85 100644 (file)
@@ -44,33 +44,23 @@ 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()`.
     private CreateBookmarkListener createBookmarkListener;
 
-    public void onAttach(Context context) {
+    public void onAttach(@NonNull Context context) {
         // Run the default commands.
         super.onAttach(context);
 
-        // Get a handle for `CreateBookmarkListener` from the launching context.
+        // Get a handle for the create bookmark listener from the launching context.
         createBookmarkListener = (CreateBookmarkListener) context;
     }
 
     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;
@@ -192,40 +176,42 @@ public class CreateBookmarkDialog extends DialogFragment {
         // Set the current `WebView` title as the text for `create_bookmark_name_edittext`.
         createBookmarkNameEditText.setText(title);
 
-        // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_name_edittext`.
-        createBookmarkNameEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> {
-            // 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);
+        // Allow the `enter` key on the keyboard to create the bookmark from the create bookmark name edittext`.
+        createBookmarkNameEditText.setOnKeyListener((View view, int keyCode, KeyEvent keyEvent) -> {
+            // If the event is a key-down on the `enter` key, select the create button.
+            if ((keyCode == KeyEvent.KEYCODE_ENTER) && (keyEvent.getAction() == KeyEvent.ACTION_DOWN)) {
+                // Trigger the create bookmark listener and return the dialog fragment and the favorite icon bitmap to the parent activity.
+                createBookmarkListener.onCreateBookmark(this, favoriteIconBitmap);
 
-                // Manually dismiss the `AlertDialog`.
+                // Manually dismiss the alert dialog.
                 alertDialog.dismiss();
 
                 // Consume the event.
                 return true;
-            } else {  // If any other key was pressed, do not consume the event.
+            } else {  // Some other key was pressed.
+                // Do not consume the event.
                 return false;
             }
         });
 
-        // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`.
+        // Set the formatted URL string as the initial text of the create bookmark URL edit text.
         EditText createBookmarkUrlEditText = alertDialog.findViewById(R.id.create_bookmark_url_edittext);
         createBookmarkUrlEditText.setText(url);
 
-        // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
-        createBookmarkUrlEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
-            // 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);
+        // Allow the enter key on the keyboard to create the bookmark from create bookmark URL edit text.
+        createBookmarkUrlEditText.setOnKeyListener((View v, int keyCode, KeyEvent keyEvent) -> {
+            // If the event is a key-down on the `enter` key, select the create button.
+            if ((keyCode == KeyEvent.KEYCODE_ENTER) && (keyEvent.getAction() == KeyEvent.ACTION_DOWN)) {
+                // Trigger the create bookmark listener and return the dialog fragment and the favorite icon bitmap to the parent activity.
+                createBookmarkListener.onCreateBookmark(this, favoriteIconBitmap);
 
-                // Manually dismiss the `AlertDialog`.
+                // Manually dismiss the alert dialog.
                 alertDialog.dismiss();
 
                 // Consume the event.
                 return true;
-            } else { // If any other key was pressed, do not consume the event.
+            } else { // Some other key was pressed.
+                // Do not consume the event.
                 return false;
             }
         });