Skip to content

Commit f889ed1

Browse files
Refactor FilePicker.java intent result handling (#5851)
* 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]>
1 parent 95e0a0d commit f889ed1

File tree

3 files changed

+18
-38
lines changed

3 files changed

+18
-38
lines changed

app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ interface RequestCodes {
1616
int PICK_PICTURE_FROM_DOCUMENTS = FILE_PICKER_IMAGE_IDENTIFICATOR + (1 << 11);
1717
int PICK_PICTURE_FROM_GALLERY = FILE_PICKER_IMAGE_IDENTIFICATOR + (1 << 12);
1818
int TAKE_PICTURE = FILE_PICKER_IMAGE_IDENTIFICATOR + (1 << 13);
19-
int CAPTURE_VIDEO = FILE_PICKER_IMAGE_IDENTIFICATOR + (1 << 14);
2019

2120
int RECEIVE_DATA_FROM_FULL_SCREEN_MODE = 1 << 9;
2221
}

app/src/main/java/fr/free/nrw/commons/filepicker/FilePicker.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@ private static int restoreType(@NonNull Context context) {
109109
*/
110110
public static void openGallery(Activity activity, int type, boolean openDocumentIntentPreferred) {
111111
Intent intent = createGalleryIntent(activity, type, openDocumentIntentPreferred);
112-
activity.startActivityForResult(intent, RequestCodes.PICK_PICTURE_FROM_GALLERY);
112+
int requestCode = RequestCodes.PICK_PICTURE_FROM_GALLERY;
113+
114+
if(openDocumentIntentPreferred){
115+
requestCode = RequestCodes.PICK_PICTURE_FROM_DOCUMENTS;
116+
}
117+
118+
activity.startActivityForResult(intent, requestCode);
113119
}
114120

115121
/**
@@ -157,7 +163,6 @@ public static void handleActivityResult(int requestCode, int resultCode, Intent
157163
requestCode &= ~RequestCodes.SOURCE_CHOOSER;
158164
if (requestCode == RequestCodes.PICK_PICTURE_FROM_GALLERY ||
159165
requestCode == RequestCodes.TAKE_PICTURE ||
160-
requestCode == RequestCodes.CAPTURE_VIDEO ||
161166
requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS ||
162167
requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR) {
163168
if (resultCode == Activity.RESULT_OK) {
@@ -169,19 +174,16 @@ public static void handleActivityResult(int requestCode, int resultCode, Intent
169174
onPictureReturnedFromCustomSelector(data, activity, callbacks);
170175
} else if (requestCode == RequestCodes.TAKE_PICTURE) {
171176
onPictureReturnedFromCamera(activity, callbacks);
172-
} else if (requestCode == RequestCodes.CAPTURE_VIDEO) {
173-
onVideoReturnedFromCamera(activity, callbacks);
174-
} else if (isPhoto(data)) {
175-
onPictureReturnedFromCamera(activity, callbacks);
176-
} else {
177-
onPictureReturnedFromDocuments(data, activity, callbacks);
178177
}
179178
} else {
180179
if (requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) {
181180
callbacks.onCanceled(FilePicker.ImageSource.DOCUMENTS, restoreType(activity));
182181
} else if (requestCode == RequestCodes.PICK_PICTURE_FROM_GALLERY) {
183182
callbacks.onCanceled(FilePicker.ImageSource.GALLERY, restoreType(activity));
184-
} else {
183+
} else if (requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR){
184+
callbacks.onCanceled(ImageSource.CUSTOM_SELECTOR, restoreType(activity));
185+
}
186+
else {
185187
callbacks.onCanceled(FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
186188
}
187189
}

app/src/test/kotlin/fr/free/nrw/commons/filepicker/FilePickerTest.kt

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ class FilePickerTest {
6464
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
6565
`when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
6666
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
67-
FilePicker.openGallery(activity, 0, nextBoolean())
67+
val openDocumentPreferred = nextBoolean()
68+
FilePicker.openGallery(activity, 0, openDocumentPreferred)
6869
verify(activity).startActivityForResult(
6970
ArgumentMatchers.any(),
7071
requestCodeCaptor?.capture()?.toInt()!!,
7172
)
72-
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY)
73+
if(openDocumentPreferred){
74+
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_DOCUMENTS)
75+
}else{
76+
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY)
77+
}
7378
}
7479

7580
@Test
@@ -165,32 +170,6 @@ class FilePickerTest {
165170
method.invoke(mockFilePicker, activity)
166171
}
167172

168-
@Test
169-
fun testTakenCameraVideo() {
170-
val mockFilePicker = mock(FilePicker::class.java)
171-
val method: Method =
172-
FilePicker::class.java.getDeclaredMethod(
173-
"takenCameraVideo",
174-
Context::class.java,
175-
)
176-
method.isAccessible = true
177-
method.invoke(mockFilePicker, context)
178-
}
179-
180-
@Test
181-
fun testTakenCameraVideoCaseTrue() {
182-
val mockFilePicker = mock(FilePicker::class.java)
183-
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
184-
`when`(sharedPref.getString("last_video", null)).thenReturn("")
185-
val method: Method =
186-
FilePicker::class.java.getDeclaredMethod(
187-
"takenCameraVideo",
188-
Context::class.java,
189-
)
190-
method.isAccessible = true
191-
method.invoke(mockFilePicker, activity)
192-
}
193-
194173
@Test
195174
fun testIsPhoto() {
196175
val mockFilePicker = mock(FilePicker::class.java)

0 commit comments

Comments
 (0)