Skip to content

Commit

Permalink
Refactor FilePicker.java intent result handling (#5851)
Browse files Browse the repository at this point in the history
* remove unnecessary video flag check

Signed-off-by: parneet-guraya <[email protected]>

* handle when custom selector operation cancelled

Signed-off-by: parneet-guraya <[email protected]>

* dispatch appropriate request code to handle using respective callbacks

Signed-off-by: parneet-guraya <[email protected]>

* remove wrong control statements

Signed-off-by: parneet-guraya <[email protected]>

* refactor gallery picker test

Signed-off-by: parneet-guraya <[email protected]>

---------

Signed-off-by: parneet-guraya <[email protected]>
  • Loading branch information
parneet-guraya authored Oct 16, 2024
1 parent 95e0a0d commit f889ed1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface RequestCodes {
int PICK_PICTURE_FROM_DOCUMENTS = FILE_PICKER_IMAGE_IDENTIFICATOR + (1 << 11);
int PICK_PICTURE_FROM_GALLERY = FILE_PICKER_IMAGE_IDENTIFICATOR + (1 << 12);
int TAKE_PICTURE = FILE_PICKER_IMAGE_IDENTIFICATOR + (1 << 13);
int CAPTURE_VIDEO = FILE_PICKER_IMAGE_IDENTIFICATOR + (1 << 14);

int RECEIVE_DATA_FROM_FULL_SCREEN_MODE = 1 << 9;
}
Expand Down
20 changes: 11 additions & 9 deletions app/src/main/java/fr/free/nrw/commons/filepicker/FilePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ private static int restoreType(@NonNull Context context) {
*/
public static void openGallery(Activity activity, int type, boolean openDocumentIntentPreferred) {
Intent intent = createGalleryIntent(activity, type, openDocumentIntentPreferred);
activity.startActivityForResult(intent, RequestCodes.PICK_PICTURE_FROM_GALLERY);
int requestCode = RequestCodes.PICK_PICTURE_FROM_GALLERY;

if(openDocumentIntentPreferred){
requestCode = RequestCodes.PICK_PICTURE_FROM_DOCUMENTS;
}

activity.startActivityForResult(intent, requestCode);
}

/**
Expand Down Expand Up @@ -157,7 +163,6 @@ public static void handleActivityResult(int requestCode, int resultCode, Intent
requestCode &= ~RequestCodes.SOURCE_CHOOSER;
if (requestCode == RequestCodes.PICK_PICTURE_FROM_GALLERY ||
requestCode == RequestCodes.TAKE_PICTURE ||
requestCode == RequestCodes.CAPTURE_VIDEO ||
requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS ||
requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR) {
if (resultCode == Activity.RESULT_OK) {
Expand All @@ -169,19 +174,16 @@ public static void handleActivityResult(int requestCode, int resultCode, Intent
onPictureReturnedFromCustomSelector(data, activity, callbacks);
} else if (requestCode == RequestCodes.TAKE_PICTURE) {
onPictureReturnedFromCamera(activity, callbacks);
} else if (requestCode == RequestCodes.CAPTURE_VIDEO) {
onVideoReturnedFromCamera(activity, callbacks);
} else if (isPhoto(data)) {
onPictureReturnedFromCamera(activity, callbacks);
} else {
onPictureReturnedFromDocuments(data, activity, callbacks);
}
} else {
if (requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) {
callbacks.onCanceled(FilePicker.ImageSource.DOCUMENTS, restoreType(activity));
} else if (requestCode == RequestCodes.PICK_PICTURE_FROM_GALLERY) {
callbacks.onCanceled(FilePicker.ImageSource.GALLERY, restoreType(activity));
} else {
} else if (requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR){
callbacks.onCanceled(ImageSource.CUSTOM_SELECTOR, restoreType(activity));
}
else {
callbacks.onCanceled(FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ class FilePickerTest {
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
`when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
FilePicker.openGallery(activity, 0, nextBoolean())
val openDocumentPreferred = nextBoolean()
FilePicker.openGallery(activity, 0, openDocumentPreferred)
verify(activity).startActivityForResult(
ArgumentMatchers.any(),
requestCodeCaptor?.capture()?.toInt()!!,
)
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY)
if(openDocumentPreferred){
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_DOCUMENTS)
}else{
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY)
}
}

@Test
Expand Down Expand Up @@ -165,32 +170,6 @@ class FilePickerTest {
method.invoke(mockFilePicker, activity)
}

@Test
fun testTakenCameraVideo() {
val mockFilePicker = mock(FilePicker::class.java)
val method: Method =
FilePicker::class.java.getDeclaredMethod(
"takenCameraVideo",
Context::class.java,
)
method.isAccessible = true
method.invoke(mockFilePicker, context)
}

@Test
fun testTakenCameraVideoCaseTrue() {
val mockFilePicker = mock(FilePicker::class.java)
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
`when`(sharedPref.getString("last_video", null)).thenReturn("")
val method: Method =
FilePicker::class.java.getDeclaredMethod(
"takenCameraVideo",
Context::class.java,
)
method.isAccessible = true
method.invoke(mockFilePicker, activity)
}

@Test
fun testIsPhoto() {
val mockFilePicker = mock(FilePicker::class.java)
Expand Down

0 comments on commit f889ed1

Please sign in to comment.