refactor(compose): Use registerForActivityResult for picking content

For picking contacts, photos, and files, use the modern API's. Additionally, Android 17 forces using GetMultipleContents rather than ACTION_PICK, so update that.
This commit is contained in:
Inhishonor
2026-06-29 15:10:55 -07:00
parent f46ac415eb
commit 555b8822c6
3 changed files with 65 additions and 31 deletions
@@ -31,7 +31,6 @@ import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.SystemClock import android.os.SystemClock
import android.provider.ContactsContract
import android.provider.MediaStore import android.provider.MediaStore
import android.speech.RecognizerIntent import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer import android.speech.SpeechRecognizer
@@ -45,6 +44,8 @@ import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.SeekBar import android.widget.SeekBar
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.constraintlayout.widget.ConstraintSet import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
@@ -164,6 +165,40 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
private var cameraDestination: Uri? = null private var cameraDestination: Uri? = null
private val pickMedia = registerForActivityResult(
ActivityResultContracts.PickMultipleVisualMedia()
) { uris ->
uris.forEach(attachAnyFileSelectedIntent::onNext)
}
private val pickFilesWithDocsUI = registerForActivityResult(
ActivityResultContracts.GetMultipleContents()
) { uris ->
uris.forEach(attachAnyFileSelectedIntent::onNext)
}
private val pickFilesWithChooser = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
val data = result.data ?: return@registerForActivityResult
val uris = data.clipData?.let { clipData ->
(0 until clipData.itemCount).mapNotNull { clipData.getItemAt(it).uri }
} ?: listOfNotNull(data.data)
uris.forEach { uri ->
attachAnyFileSelectedIntent.onNext(uri)
}
}
}
private val pickContact = registerForActivityResult(
ActivityResultContracts.PickContact()
) { uri ->
uri?.let { contactSelectedIntent.onNext(it) }
}
private fun getSeekBarUpdater(): ObservableSubscribeProxy<Long> { private fun getSeekBarUpdater(): ObservableSubscribeProxy<Long> {
return Observable.interval(500, TimeUnit.MILLISECONDS) return Observable.interval(500, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.single()) .subscribeOn(Schedulers.single())
@@ -610,10 +645,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
} }
override fun requestContact() { override fun requestContact() {
val intent = Intent(Intent.ACTION_PICK) pickContact.launch(null)
.setType(ContactsContract.Contacts.CONTENT_TYPE)
startActivityForResult(Intent.createChooser(intent, null), ComposeView.ATTACH_CONTACT_REQUEST_CODE)
} }
override fun showContacts(sharing: Boolean, chips: List<Recipient>) { override fun showContacts(sharing: Boolean, chips: List<Recipient>) {
@@ -658,14 +690,29 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
startActivityForResult(Intent.createChooser(intent, null), ComposeView.TAKE_PHOTOS_REQUEST_CODE) startActivityForResult(Intent.createChooser(intent, null), ComposeView.TAKE_PHOTOS_REQUEST_CODE)
} }
override fun requestGallery(mimeType: String, requestCode: Int) { override fun requestGallery() {
val intent = Intent(Intent.ACTION_PICK) // TODO: Use https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts.PickVisualMedia.DefaultTab.AlbumsTab
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) // here to make it clearer that videos can be selected as well.
.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) pickMedia.launch(
.putExtra(Intent.EXTRA_LOCAL_ONLY, false) PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) )
.setType(mimeType) }
startActivityForResult(Intent.createChooser(intent, null), requestCode)
override fun requestFilePicker() {
// On older Android versions, let's still use the older method of creating a chooser,
// and allowing the user to pick which file they would like.
// On Android 17 however, we must use Documents UI, so let's do it properly.
if (Build.VERSION.SDK_INT >= 37) {
pickFilesWithDocsUI.launch("*/*")
} else {
val intent = Intent(Intent.ACTION_PICK)
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
.putExtra(Intent.EXTRA_LOCAL_ONLY, false)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.setType("*/*")
pickFilesWithChooser.launch(Intent.createChooser(intent, null))
}
} }
override fun setDraft(draft: String) { override fun setDraft(draft: String) {
@@ -760,18 +807,6 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
cameraDestination?.let(attachAnyFileSelectedIntent::onNext) cameraDestination?.let(attachAnyFileSelectedIntent::onNext)
} }
ComposeView.ATTACH_FILE_REQUEST_CODE -> {
data?.clipData?.itemCount
?.let { count -> 0 until count }
?.mapNotNull { i -> data.clipData?.getItemAt(i)?.uri }
?.forEach(attachAnyFileSelectedIntent::onNext)
?: data?.data?.let(attachAnyFileSelectedIntent::onNext)
}
ComposeView.ATTACH_CONTACT_REQUEST_CODE -> {
data?.data?.let(contactSelectedIntent::onNext)
}
ComposeView.SPEECH_RECOGNITION_REQUEST_CODE -> { ComposeView.SPEECH_RECOGNITION_REQUEST_CODE -> {
// check returned results are good // check returned results are good
val match = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) val match = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
@@ -36,8 +36,6 @@ interface ComposeView : QkView<ComposeState> {
companion object { companion object {
const val SELECT_CONTACT_REQUEST_CODE = 0 const val SELECT_CONTACT_REQUEST_CODE = 0
const val TAKE_PHOTOS_REQUEST_CODE = 1 const val TAKE_PHOTOS_REQUEST_CODE = 1
const val ATTACH_CONTACT_REQUEST_CODE = 3
const val ATTACH_FILE_REQUEST_CODE = 4
const val SPEECH_RECOGNITION_REQUEST_CODE = 5 const val SPEECH_RECOGNITION_REQUEST_CODE = 5
const val CAMERA_DESTINATION_KEY = "camera_destination" const val CAMERA_DESTINATION_KEY = "camera_destination"
@@ -104,7 +102,8 @@ interface ComposeView : QkView<ComposeState> {
fun themeChanged() fun themeChanged()
fun showKeyboard() fun showKeyboard()
fun requestCamera() fun requestCamera()
fun requestGallery(mimeType: String, requestCode: Int) fun requestGallery()
fun requestFilePicker()
fun requestDatePicker() fun requestDatePicker()
fun requestContact() fun requestContact()
fun setDraft(draft: String) fun setDraft(draft: String)
@@ -832,17 +832,17 @@ class ComposeViewModel @Inject constructor(
view.requestCamera() view.requestCamera()
} }
// pick a photo (specifically) from image provider apps // pick a photo or video from image provider apps
view.attachImageFileIntent view.attachImageFileIntent
.doOnNext { newState { copy(attaching = false) } } .doOnNext { newState { copy(attaching = false) } }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { view.requestGallery("image/*", ComposeView.ATTACH_FILE_REQUEST_CODE) } .subscribe { view.requestGallery() }
// pick any file from any provider apps // pick any file from any provider apps
view.attachAnyFileIntent view.attachAnyFileIntent
.doOnNext { newState { copy(attaching = false) } } .doOnNext { newState { copy(attaching = false) } }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { view.requestGallery("*/*", ComposeView.ATTACH_FILE_REQUEST_CODE) } .subscribe { view.requestFilePicker() }
// Choose a time to schedule the message // Choose a time to schedule the message
view.scheduleIntent view.scheduleIntent