mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Refactor FilePicker.java intent result handling (#5851)
* remove unnecessary video flag check Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * handle when custom selector operation cancelled Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * dispatch appropriate request code to handle using respective callbacks Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * remove wrong control statements Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * refactor gallery picker test Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> --------- Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parent
95e0a0d143
commit
f889ed1821
3 changed files with 18 additions and 38 deletions
|
|
@ -16,7 +16,6 @@ public interface Constants {
|
|||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,7 +109,13 @@ public class FilePicker implements Constants {
|
|||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -157,7 +163,6 @@ public class FilePicker implements Constants {
|
|||
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) {
|
||||
|
|
@ -169,19 +174,16 @@ public class FilePicker implements Constants {
|
|||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue