Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.SystemClock
import android.provider.ContactsContract
import android.provider.MediaStore
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
Expand All @@ -45,6 +44,8 @@ import android.view.MenuItem
import android.view.View
import android.widget.SeekBar
import android.widget.Toast
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.app.ActivityCompat
Expand Down Expand Up @@ -164,6 +165,47 @@ class ComposeActivity : QkThemedActivity(), ComposeView {

private var cameraDestination: Uri? = null

private val pickMedia = registerForActivityResult(
ActivityResultContracts.PickMultipleVisualMedia()
) { uris ->
uris.forEach { uri ->
attachAnyFileSelectedIntent.onNext(uri)
}
}

private val pickFilesWithDocsUI = registerForActivityResult(
ActivityResultContracts.GetMultipleContents()
) { uris ->
uris.forEach { uri ->
attachAnyFileSelectedIntent.onNext(uri)
}
}

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).map { clipData.getItemAt(it).uri }
} ?: listOfNotNull(data.data)

uris.forEach { uri ->
attachAnyFileSelectedIntent.onNext(uri)
}
}
}

private val pickContact = registerForActivityResult(
ActivityResultContracts.PickContact()
) { uri ->
if (uri != null) {
contactSelectedIntent.onNext(uri)
}

}

private fun getSeekBarUpdater(): ObservableSubscribeProxy<Long> {
return Observable.interval(500, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.single())
Expand Down Expand Up @@ -610,10 +652,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
}

override fun requestContact() {
val intent = Intent(Intent.ACTION_PICK)
.setType(ContactsContract.Contacts.CONTENT_TYPE)

startActivityForResult(Intent.createChooser(intent, null), ComposeView.ATTACH_CONTACT_REQUEST_CODE)
pickContact.launch(null)
}

override fun showContacts(sharing: Boolean, chips: List<Recipient>) {
Expand Down Expand Up @@ -658,14 +697,29 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
startActivityForResult(Intent.createChooser(intent, null), ComposeView.TAKE_PHOTOS_REQUEST_CODE)
}

override fun requestGallery(mimeType: String, requestCode: Int) {
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(mimeType)
startActivityForResult(Intent.createChooser(intent, null), requestCode)
override fun requestGallery() {
// TODO: Use https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts.PickVisualMedia.DefaultTab.AlbumsTab
// here to make it clearer that videos can be selected as well.
pickMedia.launch(
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to do https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts.PickVisualMedia.DefaultTab.AlbumsTab here, but that would require updating compile sdk and adding a new dependency. So might have to wait.

)
}

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) {
Expand Down Expand Up @@ -760,18 +814,6 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
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 -> {
// check returned results are good
val match = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ interface ComposeView : QkView<ComposeState> {
companion object {
const val SELECT_CONTACT_REQUEST_CODE = 0
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 CAMERA_DESTINATION_KEY = "camera_destination"
Expand Down Expand Up @@ -104,7 +102,8 @@ interface ComposeView : QkView<ComposeState> {
fun themeChanged()
fun showKeyboard()
fun requestCamera()
fun requestGallery(mimeType: String, requestCode: Int)
fun requestGallery()
fun requestFilePicker()
fun requestDatePicker()
fun requestContact()
fun setDraft(draft: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,17 +830,17 @@ class ComposeViewModel @Inject constructor(
view.requestCamera()
}

// pick a photo (specifically) from image provider apps
// pick a photo or video from image provider apps
view.attachImageFileIntent
.doOnNext { newState { copy(attaching = false) } }
.autoDisposable(view.scope())
.subscribe { view.requestGallery("image/*", ComposeView.ATTACH_FILE_REQUEST_CODE) }
.subscribe { view.requestGallery() }

// pick any file from any provider apps
view.attachAnyFileIntent
.doOnNext { newState { copy(attaching = false) } }
.autoDisposable(view.scope())
.subscribe { view.requestGallery("*/*", ComposeView.ATTACH_FILE_REQUEST_CODE) }
.subscribe { view.requestFilePicker() }

// Choose a time to schedule the message
view.scheduleIntent
Expand Down
Loading