diff --git a/library/multipicker/src/main/java/im/vector/lib/multipicker/AudioPicker.kt b/library/multipicker/src/main/java/im/vector/lib/multipicker/AudioPicker.kt index 6e0eed28cc..7ca9a2550f 100644 --- a/library/multipicker/src/main/java/im/vector/lib/multipicker/AudioPicker.kt +++ b/library/multipicker/src/main/java/im/vector/lib/multipicker/AudioPicker.kt @@ -31,9 +31,8 @@ class AudioPicker : Picker() { * Returns selected audio files or empty list if user did not select any files. */ override fun getSelectedFiles(context: Context, data: Intent?): List { - return getSelectedUriList(data).mapNotNull { selectedUri -> - // Tchap: Grant permission to access the selected file. - context.grantUriPermission(context.applicationContext.packageName, selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + // Tchap: Grant permission to access the selected file. + return getSelectedUriList(context, data).mapNotNull { selectedUri -> selectedUri.toMultiPickerAudioType(context) } } diff --git a/library/multipicker/src/main/java/im/vector/lib/multipicker/ContactPicker.kt b/library/multipicker/src/main/java/im/vector/lib/multipicker/ContactPicker.kt index 4fd170935c..04dc8feb7b 100644 --- a/library/multipicker/src/main/java/im/vector/lib/multipicker/ContactPicker.kt +++ b/library/multipicker/src/main/java/im/vector/lib/multipicker/ContactPicker.kt @@ -40,9 +40,6 @@ class ContactPicker : Picker() { val contactList = mutableListOf() data?.data?.let { selectedUri -> - // Tchap: Grant permission to access the selected URI. - context.grantUriPermission(context.applicationContext.packageName, selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) - val projection = arrayOf( ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_URI, diff --git a/library/multipicker/src/main/java/im/vector/lib/multipicker/FilePicker.kt b/library/multipicker/src/main/java/im/vector/lib/multipicker/FilePicker.kt index c939efdd19..021260fd00 100644 --- a/library/multipicker/src/main/java/im/vector/lib/multipicker/FilePicker.kt +++ b/library/multipicker/src/main/java/im/vector/lib/multipicker/FilePicker.kt @@ -41,10 +41,8 @@ class FilePicker : Picker() { * Returns selected files or empty list if user did not select any files. */ override fun getSelectedFiles(context: Context, data: Intent?): List { - return getSelectedUriList(data).mapNotNull { selectedUri -> - // Tchap: Grant permission to access the selected file. - context.grantUriPermission(context.applicationContext.packageName, selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) - + // Tchap: Grant permission to access the selected file. + return getSelectedUriList(context, data).mapNotNull { selectedUri -> val type = context.contentResolver.getType(selectedUri) when { diff --git a/library/multipicker/src/main/java/im/vector/lib/multipicker/ImagePicker.kt b/library/multipicker/src/main/java/im/vector/lib/multipicker/ImagePicker.kt index b5ed5374f2..87b03a0fb8 100644 --- a/library/multipicker/src/main/java/im/vector/lib/multipicker/ImagePicker.kt +++ b/library/multipicker/src/main/java/im/vector/lib/multipicker/ImagePicker.kt @@ -31,9 +31,8 @@ class ImagePicker : Picker() { * Returns selected image files or empty list if user did not select any files. */ override fun getSelectedFiles(context: Context, data: Intent?): List { - return getSelectedUriList(data).mapNotNull { selectedUri -> - // Tchap: Grant permission to access the selected file. - context.grantUriPermission(context.applicationContext.packageName, selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + // Tchap: Grant permission to access the selected file. + return getSelectedUriList(context, data).mapNotNull { selectedUri -> selectedUri.toMultiPickerImageType(context) } } diff --git a/library/multipicker/src/main/java/im/vector/lib/multipicker/MediaPicker.kt b/library/multipicker/src/main/java/im/vector/lib/multipicker/MediaPicker.kt index 8d94e602e3..b76ce3bc53 100644 --- a/library/multipicker/src/main/java/im/vector/lib/multipicker/MediaPicker.kt +++ b/library/multipicker/src/main/java/im/vector/lib/multipicker/MediaPicker.kt @@ -33,10 +33,8 @@ class MediaPicker : Picker() { * Returns selected image/video files or empty list if user did not select any files. */ override fun getSelectedFiles(context: Context, data: Intent?): List { - return getSelectedUriList(data).mapNotNull { selectedUri -> - // Tchap: Grant permission to access the selected file. - context.grantUriPermission(context.applicationContext.packageName, selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) - + // Tchap: Grant permission to access the selected file. + return getSelectedUriList(context, data).mapNotNull { selectedUri -> val mimeType = context.contentResolver.getType(selectedUri) if (mimeType.isMimeTypeVideo()) { diff --git a/library/multipicker/src/main/java/im/vector/lib/multipicker/Picker.kt b/library/multipicker/src/main/java/im/vector/lib/multipicker/Picker.kt index 5275528c57..e54bf410de 100644 --- a/library/multipicker/src/main/java/im/vector/lib/multipicker/Picker.kt +++ b/library/multipicker/src/main/java/im/vector/lib/multipicker/Picker.kt @@ -91,14 +91,19 @@ abstract class Picker { activityResultLauncher.launch(createIntent().apply { addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) }) } - protected fun getSelectedUriList(data: Intent?): List { + protected fun getSelectedUriList(context: Context, data: Intent?): List { val selectedUriList = mutableListOf() val dataUri = data?.data val clipData = data?.clipData + val packageName = context.applicationContext.packageName if (clipData != null) { - for (i in 0 until clipData.itemCount) { - selectedUriList.add(clipData.getItemAt(i).uri) + // Tchap: Grant permission to access the selected file. + 0.until(clipData.itemCount).map { + clipData.getItemAt(it).uri.let { selectedUri -> + context.grantUriPermission(packageName, selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + selectedUriList.add(selectedUri) + } } } else if (dataUri != null) { selectedUriList.add(dataUri) @@ -106,9 +111,15 @@ abstract class Picker { @Suppress("DEPRECATION") data?.extras?.get(Intent.EXTRA_STREAM)?.let { (it as? List<*>)?.filterIsInstance()?.let { uriList -> - selectedUriList.addAll(uriList) + // Tchap: Grant permission to access the selected file. + uriList.map { selectedUri -> + context.grantUriPermission(packageName, selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + selectedUriList.add(selectedUri) + } } if (it is Uri) { + // Tchap: Grant permission to access the selected file. + context.grantUriPermission(packageName, it, Intent.FLAG_GRANT_READ_URI_PERMISSION) selectedUriList.add(it) } } diff --git a/library/multipicker/src/main/java/im/vector/lib/multipicker/VideoPicker.kt b/library/multipicker/src/main/java/im/vector/lib/multipicker/VideoPicker.kt index 9b8e99bf9b..37d287ed3c 100644 --- a/library/multipicker/src/main/java/im/vector/lib/multipicker/VideoPicker.kt +++ b/library/multipicker/src/main/java/im/vector/lib/multipicker/VideoPicker.kt @@ -31,9 +31,8 @@ class VideoPicker : Picker() { * Returns selected video files or empty list if user did not select any files. */ override fun getSelectedFiles(context: Context, data: Intent?): List { - return getSelectedUriList(data).mapNotNull { selectedUri -> - // Tchap: Grant permission to access the selected file. - context.grantUriPermission(context.applicationContext.packageName, selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + // Tchap: Grant permission to access the selected file. + return getSelectedUriList(context, data).mapNotNull { selectedUri -> selectedUri.toMultiPickerVideoType(context) } }