diff --git a/packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerDelegate.java b/packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerDelegate.java index 778c7c3ff460..37b08a523fc3 100644 --- a/packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerDelegate.java +++ b/packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerDelegate.java @@ -548,31 +548,32 @@ public boolean onRequestPermissionsResult( @Override public boolean onActivityResult(final int requestCode, final int resultCode, final Intent data) { // Off-load result handling to a separate thread as it almost always involves I/O operations. - executor.execute(() -> { - switch (requestCode) { - case REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY: - handleChooseImageResult(resultCode, data); - break; - case REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY: - handleChooseMultiImageResult(resultCode, data); - break; - case REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA: - handleCaptureImageResult(resultCode); - break; - case REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY: - handleChooseVideoResult(resultCode, data); - break; - case REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA: - handleCaptureVideoResult(resultCode); - break; - } - }); + executor.execute( + () -> { + switch (requestCode) { + case REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY: + handleChooseImageResult(resultCode, data); + break; + case REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY: + handleChooseMultiImageResult(resultCode, data); + break; + case REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA: + handleCaptureImageResult(resultCode); + break; + case REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY: + handleChooseVideoResult(resultCode, data); + break; + case REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA: + handleCaptureVideoResult(resultCode); + break; + } + }); - return requestCode == REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY || - requestCode == REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY || - requestCode == REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA || - requestCode == REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY || - requestCode == REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA; + return requestCode == REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY + || requestCode == REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY + || requestCode == REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA + || requestCode == REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY + || requestCode == REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA; } private void handleChooseImageResult(int resultCode, Intent data) { diff --git a/packages/image_picker/image_picker_android/android/src/test/java/io/flutter/plugins/imagepicker/ImagePickerDelegateTest.java b/packages/image_picker/image_picker_android/android/src/test/java/io/flutter/plugins/imagepicker/ImagePickerDelegateTest.java index 9afca839ff13..4648e6d45056 100644 --- a/packages/image_picker/image_picker_android/android/src/test/java/io/flutter/plugins/imagepicker/ImagePickerDelegateTest.java +++ b/packages/image_picker/image_picker_android/android/src/test/java/io/flutter/plugins/imagepicker/ImagePickerDelegateTest.java @@ -35,7 +35,6 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; - import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -354,15 +353,18 @@ public void onRequestPermissionsResult_whenCameraPermissionDenied_finishesWithEr @Test public void onActivityResult_whenPickFromGalleryCanceled_finishesWithEmptyList() { - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = - createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null); + createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null); delegate.onActivityResult( - ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_CANCELED, null); + ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_CANCELED, null); @SuppressWarnings("unchecked") ArgumentCaptor> pathListCapture = ArgumentCaptor.forClass(List.class); @@ -373,14 +375,17 @@ public void onActivityResult_whenPickFromGalleryCanceled_finishesWithEmptyList() @Test public void onActivityResult_whenPickFromGalleryCanceled_storesNothingInCache() { - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = createDelegate(); delegate.onActivityResult( - ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_CANCELED, null); + ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_CANCELED, null); verify(cache, never()).saveResult(any(), any(), any()); } @@ -388,12 +393,15 @@ public void onActivityResult_whenPickFromGalleryCanceled_storesNothingInCache() @Test public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_finishesWithImagePath() { - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = - createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null); + createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null); delegate.onActivityResult( ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent); @@ -407,14 +415,17 @@ public void onActivityResult_whenPickFromGalleryCanceled_storesNothingInCache() @Test public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_storesImageInCache() { - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = createDelegate(); delegate.onActivityResult( - ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent); + ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent); @SuppressWarnings("unchecked") ArgumentCaptor> pathListCapture = ArgumentCaptor.forClass(ArrayList.class); @@ -425,12 +436,15 @@ public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_stores @Test public void onActivityResult_whenImagePickedFromGallery_andResizeNeeded_finishesWithScaledImagePath() { - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = - createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null); + createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null); delegate.onActivityResult( ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent); @@ -444,16 +458,19 @@ public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_stores @Test public void - onActivityResult_whenVideoPickedFromGallery_andResizeParametersSupplied_finishesWithFilePath() { - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + onActivityResult_whenVideoPickedFromGallery_andResizeParametersSupplied_finishesWithFilePath() { + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = - createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null); + createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null); delegate.onActivityResult( - ImagePickerDelegate.REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY, Activity.RESULT_OK, mockIntent); + ImagePickerDelegate.REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY, Activity.RESULT_OK, mockIntent); @SuppressWarnings("unchecked") ArgumentCaptor> pathListCapture = ArgumentCaptor.forClass(List.class); @@ -464,15 +481,18 @@ public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_stores @Test public void onActivityResult_whenTakeImageWithCameraCanceled_finishesWithEmptyList() { - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = - createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null); + createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null); delegate.onActivityResult( - ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA, Activity.RESULT_CANCELED, null); + ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA, Activity.RESULT_CANCELED, null); @SuppressWarnings("unchecked") ArgumentCaptor> pathListCapture = ArgumentCaptor.forClass(List.class); @@ -484,10 +504,13 @@ public void onActivityResult_whenTakeImageWithCameraCanceled_finishesWithEmptyLi @Test public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishesWithImagePath() { when(cache.retrievePendingCameraMediaUriPath()).thenReturn("testString"); - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null); @@ -505,10 +528,13 @@ public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishes public void onActivityResult_whenImageTakenWithCamera_andResizeNeeded_finishesWithScaledImagePath() { when(cache.retrievePendingCameraMediaUriPath()).thenReturn("testString"); - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null); @@ -524,17 +550,20 @@ public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishes @Test public void - onActivityResult_whenVideoTakenWithCamera_andResizeParametersSupplied_finishesWithFilePath() { + onActivityResult_whenVideoTakenWithCamera_andResizeParametersSupplied_finishesWithFilePath() { when(cache.retrievePendingCameraMediaUriPath()).thenReturn("testString"); - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = - createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null); + createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null); delegate.onActivityResult( - ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA, Activity.RESULT_OK, mockIntent); + ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA, Activity.RESULT_OK, mockIntent); @SuppressWarnings("unchecked") ArgumentCaptor> pathListCapture = ArgumentCaptor.forClass(List.class); @@ -547,10 +576,13 @@ public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishes public void onActivityResult_whenVideoTakenWithCamera_andMaxDurationParametersSupplied_finishesWithFilePath() { when(cache.retrievePendingCameraMediaUriPath()).thenReturn("testString"); - Mockito.doAnswer(invocation -> { - ((Runnable) invocation.getArgument(0)).run(); - return null; - }).when(mockExecutor).execute(any(Runnable.class)); + Mockito.doAnswer( + invocation -> { + ((Runnable) invocation.getArgument(0)).run(); + return null; + }) + .when(mockExecutor) + .execute(any(Runnable.class)); ImagePickerDelegate delegate = createDelegateWithPendingResultAndOptions( null, new VideoSelectionOptions.Builder().setMaxDurationSeconds(MAX_DURATION).build()); @@ -569,7 +601,11 @@ public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishes public void onActivityResult_whenImagePickedFromGallery_returnsTrue() { ImagePickerDelegate delegate = createDelegate(); - boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent); + boolean isHandled = + delegate.onActivityResult( + ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, + Activity.RESULT_OK, + mockIntent); assertTrue(isHandled); } @@ -578,7 +614,11 @@ public void onActivityResult_whenImagePickedFromGallery_returnsTrue() { public void onActivityResult_whenMultipleImagesPickedFromGallery_returnsTrue() { ImagePickerDelegate delegate = createDelegate(); - boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent); + boolean isHandled = + delegate.onActivityResult( + ImagePickerDelegate.REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY, + Activity.RESULT_OK, + mockIntent); assertTrue(isHandled); } @@ -587,7 +627,11 @@ public void onActivityResult_whenMultipleImagesPickedFromGallery_returnsTrue() { public void onActivityResult_whenVideoPickerFromGallery_returnsTrue() { ImagePickerDelegate delegate = createDelegate(); - boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY, Activity.RESULT_OK, mockIntent); + boolean isHandled = + delegate.onActivityResult( + ImagePickerDelegate.REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY, + Activity.RESULT_OK, + mockIntent); assertTrue(isHandled); } @@ -596,7 +640,11 @@ public void onActivityResult_whenVideoPickerFromGallery_returnsTrue() { public void onActivityResult_whenImageTakenWithCamera_returnsTrue() { ImagePickerDelegate delegate = createDelegate(); - boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA, Activity.RESULT_OK, mockIntent); + boolean isHandled = + delegate.onActivityResult( + ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA, + Activity.RESULT_OK, + mockIntent); assertTrue(isHandled); } @@ -605,7 +653,11 @@ public void onActivityResult_whenImageTakenWithCamera_returnsTrue() { public void onActivityResult_whenVideoTakenWithCamera_returnsTrue() { ImagePickerDelegate delegate = createDelegate(); - boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA, Activity.RESULT_OK, mockIntent); + boolean isHandled = + delegate.onActivityResult( + ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA, + Activity.RESULT_OK, + mockIntent); assertTrue(isHandled); }