Skip to content

Commit

Permalink
Fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Apr 10, 2024
1 parent 8341f46 commit d22e847
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class AudioPicker : Picker<MultiPickerAudioType>() {
* Returns selected audio files or empty list if user did not select any files.
*/
override fun getSelectedFiles(context: Context, data: Intent?): List<MultiPickerAudioType> {
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ class ContactPicker : Picker<MultiPickerContactType>() {
val contactList = mutableListOf<MultiPickerContactType>()

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ class FilePicker : Picker<MultiPickerBaseType>() {
* Returns selected files or empty list if user did not select any files.
*/
override fun getSelectedFiles(context: Context, data: Intent?): List<MultiPickerBaseType> {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class ImagePicker : Picker<MultiPickerImageType>() {
* Returns selected image files or empty list if user did not select any files.
*/
override fun getSelectedFiles(context: Context, data: Intent?): List<MultiPickerImageType> {
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ class MediaPicker : Picker<MultiPickerBaseMediaType>() {
* Returns selected image/video files or empty list if user did not select any files.
*/
override fun getSelectedFiles(context: Context, data: Intent?): List<MultiPickerBaseMediaType> {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,35 @@ abstract class Picker<T> {
activityResultLauncher.launch(createIntent().apply { addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) })
}

protected fun getSelectedUriList(data: Intent?): List<Uri> {
protected fun getSelectedUriList(context: Context, data: Intent?): List<Uri> {
val selectedUriList = mutableListOf<Uri>()
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)
} else {
@Suppress("DEPRECATION")
data?.extras?.get(Intent.EXTRA_STREAM)?.let {
(it as? List<*>)?.filterIsInstance<Uri>()?.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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class VideoPicker : Picker<MultiPickerVideoType>() {
* Returns selected video files or empty list if user did not select any files.
*/
override fun getSelectedFiles(context: Context, data: Intent?): List<MultiPickerVideoType> {
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)
}
}
Expand Down

0 comments on commit d22e847

Please sign in to comment.