Repository: commons-app/apps-android-commons (language: kotlin)

Issue:
Remove unnecessary request code checks while handling file picker results
While working on #5849, I saw, there are some problems with handling request codes in `FilePicker.java` 

I chose not to include these changes in the refactor (refer #5849) because these are distinct changes which should be addressed separately and it'd easier to understand from git history.

Problems were ->

- Capture Video flag was still being checked even though we never use it in any intent to launch anything.

- Even when using `Document PIcker` (default) , the `Intent` created for launching it, is passed with `Gallery Picker/Photo Picker` `requestCode` which means for both of the  methods result will be handled by only Gallery picker's callback. Though I'm not sure if this lead to any bug but still we should be using respective callbacks.





Discussion hints:
nan

Pull request: Refactor `FilePicker.java` intent result handling
Fixes #5850

What changes did you make and why?

- Removed `CAPTURE_VIDEO` request code and it's checks, while from what I can understand we do not handle video mime type. Though if I missed something and I'm wrong please let me know, happy to make changes.

- Removed Video's picker test cases.

- Only Gallery picker's callback was handling changes for both Document picker and Gallery picker.  So, passed appropriate flag to make sure respective callbacks get invoked

EDIT: 

- Removed below code 
```
 else if (isPhoto(data)) {
                        onPictureReturnedFromCamera(activity, callbacks);
                    } else {
                        onPictureReturnedFromDocuments(data, activity, callbacks);
                    }
```

isPhoto(data) method checks if the returned data is null or not. Now, it for `Document` and `Gallery` picker results we rely on returned results so we are making sure it's not null. But, later after all the specific checks we had the above code. 

To understand the problem, let's say we got the result of `Document` picker which also checks for `!isPhoto()` to make sure result is not null. But let's say it is then the above mentioned check would invoke the callback for handling camera result but didn't we returned the result for document picker (however very unlikely to get null from document picker we either get cancelled or result ok, which we already take care of before making these checks). 

My understanding is this the check was added under the assumption that we store the image in the persistent storage when using camera's result. So it just made simple check if the result is null then it must be camera's result otherwise document's result. But from above we understand that it would wrong to use camera's image (will return the last stored image which is wrong)  when user has chosen Document/Gallery one (less likely but still we shouldn't have this check)

**Tests performed (required)**
- Ran test for `FilePicker.java` file. 

- manually tested. below is this demo. 

[Screencast from 2024-10-15 02-07-13.webm](https://github.com/user-attachments/assets/a4e16423-83ce-4876-82c3-885aa26c4ec1)


Tested {build variant, e.g. ProdDebug} on {name of device or emulator} with API level {API level}.
Build variant: betaDebug
API level: 34 (Emulator) & 34 Oneplus 9RT 5G



Test command: ./gradlew :app:testBetaDebugUnitTest --tests "fr.free.nrw.commons.filepicker.FilePickerTest"

Fix patch:
diff --git a/app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java b/app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java
index 3c9299c1aa..f907f0a014 100644
--- a/app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java
+++ b/app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java
@@ -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;
     }
diff --git a/app/src/main/java/fr/free/nrw/commons/filepicker/FilePicker.java b/app/src/main/java/fr/free/nrw/commons/filepicker/FilePicker.java
index 08453a95d3..daa29276a8 100644
--- a/app/src/main/java/fr/free/nrw/commons/filepicker/FilePicker.java
+++ b/app/src/main/java/fr/free/nrw/commons/filepicker/FilePicker.java
@@ -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);
     }
 
     /**
@@ -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) {
@@ -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));
                     }
                 }


Test patch:
diff --git a/app/src/test/kotlin/fr/free/nrw/commons/filepicker/FilePickerTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/filepicker/FilePickerTest.kt
index 171e04b4e0..365af27f03 100644
--- a/app/src/test/kotlin/fr/free/nrw/commons/filepicker/FilePickerTest.kt
+++ b/app/src/test/kotlin/fr/free/nrw/commons/filepicker/FilePickerTest.kt
@@ -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)