App integration to the new multipicker library.

This commit is contained in:
onurays 2020-03-23 16:31:32 +03:00
parent 5b875e0571
commit f7fd23b153
24 changed files with 265 additions and 549 deletions

View File

@ -16,6 +16,7 @@
package im.vector.matrix.android.api.session.content package im.vector.matrix.android.api.session.content
import android.net.Uri
import android.os.Parcelable import android.os.Parcelable
import androidx.exifinterface.media.ExifInterface import androidx.exifinterface.media.ExifInterface
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
@ -29,8 +30,7 @@ data class ContentAttachmentData(
val width: Long? = 0, val width: Long? = 0,
val exifOrientation: Int = ExifInterface.ORIENTATION_UNDEFINED, val exifOrientation: Int = ExifInterface.ORIENTATION_UNDEFINED,
val name: String? = null, val name: String? = null,
val queryUri: String, val queryUri: Uri,
val path: String,
private val mimeType: String?, private val mimeType: String?,
val type: Type val type: Type
) : Parcelable { ) : Parcelable {

View File

@ -53,9 +53,9 @@ internal class FileUploader @Inject constructor(@Authenticated
suspend fun uploadByteArray(byteArray: ByteArray, suspend fun uploadByteArray(byteArray: ByteArray,
filename: String?, filename: String?,
mimeType: String, mimeType: String?,
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse { progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
val uploadBody = byteArray.toRequestBody(mimeType.toMediaTypeOrNull()) val uploadBody = byteArray.toRequestBody(mimeType?.toMediaTypeOrNull())
return upload(uploadBody, filename, progressListener) return upload(uploadBody, filename, progressListener)
} }

View File

@ -16,12 +16,14 @@
package im.vector.matrix.android.internal.session.content package im.vector.matrix.android.internal.session.content
import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.media.ThumbnailUtils import android.media.MediaMetadataRetriever
import android.provider.MediaStore
import im.vector.matrix.android.api.session.content.ContentAttachmentData import im.vector.matrix.android.api.session.content.ContentAttachmentData
import timber.log.Timber
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.File import kotlin.math.max
import kotlin.math.roundToInt
internal object ThumbnailExtractor { internal object ThumbnailExtractor {
@ -33,34 +35,48 @@ internal object ThumbnailExtractor {
val mimeType: String val mimeType: String
) )
fun extractThumbnail(attachment: ContentAttachmentData): ThumbnailData? { fun extractThumbnail(context: Context, attachment: ContentAttachmentData): ThumbnailData? {
val file = File(attachment.path)
if (!file.exists() || !file.isFile) {
return null
}
return if (attachment.type == ContentAttachmentData.Type.VIDEO) { return if (attachment.type == ContentAttachmentData.Type.VIDEO) {
extractVideoThumbnail(attachment) extractVideoThumbnail(context, attachment)
} else { } else {
null null
} }
} }
private fun extractVideoThumbnail(attachment: ContentAttachmentData): ThumbnailData? { private fun extractVideoThumbnail(context: Context, attachment: ContentAttachmentData): ThumbnailData? {
val thumbnail = ThumbnailUtils.createVideoThumbnail(attachment.path, MediaStore.Video.Thumbnails.MINI_KIND) ?: return null val mediaMetadataRetriever = MediaMetadataRetriever()
val outputStream = ByteArrayOutputStream() try {
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) mediaMetadataRetriever.setDataSource(context, attachment.queryUri)
val thumbnailWidth = thumbnail.width var thumbnail = mediaMetadataRetriever.frameAtTime
val thumbnailHeight = thumbnail.height // Scale down the bitmap if it's too large.
val thumbnailSize = outputStream.size() val width: Int = thumbnail.width
val thumbnailData = ThumbnailData( val height: Int = thumbnail.height
width = thumbnailWidth, val max = max(width, height)
height = thumbnailHeight, if (max > 512) {
size = thumbnailSize.toLong(), val scale = 512f / max
bytes = outputStream.toByteArray(), val w = (scale * width).roundToInt()
mimeType = "image/jpeg" val h = (scale * height).roundToInt()
) thumbnail = Bitmap.createScaledBitmap(thumbnail, w, h, true)
thumbnail.recycle() }
outputStream.reset()
return thumbnailData val outputStream = ByteArrayOutputStream()
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
val thumbnailWidth = thumbnail.width
val thumbnailHeight = thumbnail.height
val thumbnailSize = outputStream.size()
val thumbnailData = ThumbnailData(
width = thumbnailWidth,
height = thumbnailHeight,
size = thumbnailSize.toLong(),
bytes = outputStream.toByteArray(),
mimeType = "image/jpeg"
)
thumbnail.recycle()
outputStream.reset()
return thumbnailData
} catch (e: Exception) {
Timber.e(e, "Cannot extract video thumbnail")
return null
}
} }
} }

View File

@ -17,12 +17,9 @@
package im.vector.matrix.android.internal.session.content package im.vector.matrix.android.internal.session.content
import android.content.Context import android.content.Context
import android.graphics.BitmapFactory
import androidx.work.CoroutineWorker import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import id.zelory.compressor.Compressor
import id.zelory.compressor.constraint.default
import im.vector.matrix.android.api.session.content.ContentAttachmentData import im.vector.matrix.android.api.session.content.ContentAttachmentData
import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.session.events.model.toContent import im.vector.matrix.android.api.session.events.model.toContent
@ -41,8 +38,6 @@ import im.vector.matrix.android.internal.worker.WorkerParamsFactory
import im.vector.matrix.android.internal.worker.getSessionComponent import im.vector.matrix.android.internal.worker.getSessionComponent
import timber.log.Timber import timber.log.Timber
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileInputStream
import javax.inject.Inject import javax.inject.Inject
private data class NewImageAttributes( private data class NewImageAttributes(
@ -94,8 +89,84 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
var newImageAttributes: NewImageAttributes? = null var newImageAttributes: NewImageAttributes? = null
val attachmentFile = try { try {
File(attachment.path) val inputStream = context.contentResolver.openInputStream(attachment.queryUri) ?: return Result.success()
inputStream.use {
var uploadedThumbnailUrl: String? = null
var uploadedThumbnailEncryptedFileInfo: EncryptedFileInfo? = null
ThumbnailExtractor.extractThumbnail(context, params.attachment)?.let { thumbnailData ->
val thumbnailProgressListener = object : ProgressRequestBody.Listener {
override fun onProgress(current: Long, total: Long) {
notifyTracker(params) { contentUploadStateTracker.setProgressThumbnail(it, current, total) }
}
}
try {
val contentUploadResponse = if (params.isEncrypted) {
Timber.v("Encrypt thumbnail")
notifyTracker(params) { contentUploadStateTracker.setEncryptingThumbnail(it) }
val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType)
uploadedThumbnailEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader.uploadByteArray(encryptionResult.encryptedByteArray,
"thumb_${attachment.name}",
"application/octet-stream",
thumbnailProgressListener)
} else {
fileUploader.uploadByteArray(thumbnailData.bytes,
"thumb_${attachment.name}",
thumbnailData.mimeType,
thumbnailProgressListener)
}
uploadedThumbnailUrl = contentUploadResponse.contentUri
} catch (t: Throwable) {
Timber.e(t)
return handleFailure(params, t)
}
}
val progressListener = object : ProgressRequestBody.Listener {
override fun onProgress(current: Long, total: Long) {
notifyTracker(params) {
if (isStopped) {
contentUploadStateTracker.setFailure(it, Throwable("Cancelled"))
} else {
contentUploadStateTracker.setProgress(it, current, total)
}
}
}
}
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
return try {
val contentUploadResponse = if (params.isEncrypted) {
Timber.v("Encrypt file")
notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
val encryptionResult = MXEncryptedAttachments.encryptAttachment(inputStream, attachment.getSafeMimeType())
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader
.uploadByteArray(encryptionResult.encryptedByteArray, attachment.name, "application/octet-stream", progressListener)
} else {
fileUploader
.uploadByteArray(inputStream.readBytes(), attachment.name, attachment.getSafeMimeType(), progressListener)
}
handleSuccess(params,
contentUploadResponse.contentUri,
uploadedFileEncryptedFileInfo,
uploadedThumbnailUrl,
uploadedThumbnailEncryptedFileInfo,
newImageAttributes)
} catch (t: Throwable) {
Timber.e(t)
handleFailure(params, t)
}
}
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e) Timber.e(e)
notifyTracker(params) { contentUploadStateTracker.setFailure(it, e) } notifyTracker(params) { contentUploadStateTracker.setFailure(it, e) }
@ -106,109 +177,6 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
) )
) )
) )
}
.let { originalFile ->
if (attachment.type == ContentAttachmentData.Type.IMAGE) {
if (params.compressBeforeSending) {
Compressor.compress(context, originalFile) {
default(
width = MAX_IMAGE_SIZE,
height = MAX_IMAGE_SIZE
)
}.also { compressedFile ->
// Update the params
val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeFile(compressedFile.absolutePath, options)
val fileSize = compressedFile.length().toInt()
newImageAttributes = NewImageAttributes(
options.outWidth,
options.outHeight,
fileSize
)
}
} else {
// TODO Fix here the image rotation issue
originalFile
}
} else {
// Other type
originalFile
}
}
var uploadedThumbnailUrl: String? = null
var uploadedThumbnailEncryptedFileInfo: EncryptedFileInfo? = null
ThumbnailExtractor.extractThumbnail(params.attachment)?.let { thumbnailData ->
val thumbnailProgressListener = object : ProgressRequestBody.Listener {
override fun onProgress(current: Long, total: Long) {
notifyTracker(params) { contentUploadStateTracker.setProgressThumbnail(it, current, total) }
}
}
try {
val contentUploadResponse = if (params.isEncrypted) {
Timber.v("Encrypt thumbnail")
notifyTracker(params) { contentUploadStateTracker.setEncryptingThumbnail(it) }
val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType)
uploadedThumbnailEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader.uploadByteArray(encryptionResult.encryptedByteArray,
"thumb_${attachment.name}",
"application/octet-stream",
thumbnailProgressListener)
} else {
fileUploader.uploadByteArray(thumbnailData.bytes,
"thumb_${attachment.name}",
thumbnailData.mimeType,
thumbnailProgressListener)
}
uploadedThumbnailUrl = contentUploadResponse.contentUri
} catch (t: Throwable) {
Timber.e(t)
return handleFailure(params, t)
}
}
val progressListener = object : ProgressRequestBody.Listener {
override fun onProgress(current: Long, total: Long) {
notifyTracker(params) {
if (isStopped) {
contentUploadStateTracker.setFailure(it, Throwable("Cancelled"))
} else {
contentUploadStateTracker.setProgress(it, current, total)
}
}
}
}
var uploadedFileEncryptedFileInfo: EncryptedFileInfo? = null
return try {
val contentUploadResponse = if (params.isEncrypted) {
Timber.v("Encrypt file")
notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(attachmentFile), attachment.getSafeMimeType())
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader
.uploadByteArray(encryptionResult.encryptedByteArray, attachment.name, "application/octet-stream", progressListener)
} else {
fileUploader
.uploadFile(attachmentFile, attachment.name, attachment.getSafeMimeType(), progressListener)
}
handleSuccess(params,
contentUploadResponse.contentUri,
uploadedFileEncryptedFileInfo,
uploadedThumbnailUrl,
uploadedThumbnailEncryptedFileInfo,
newImageAttributes)
} catch (t: Throwable) {
Timber.e(t)
handleFailure(params, t)
} }
} }

View File

@ -16,6 +16,7 @@
package im.vector.matrix.android.internal.session.room.send package im.vector.matrix.android.internal.session.room.send
import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.media.MediaMetadataRetriever import android.media.MediaMetadataRetriever
import androidx.exifinterface.media.ExifInterface import androidx.exifinterface.media.ExifInterface
@ -74,6 +75,7 @@ import javax.inject.Inject
* The transactionId is used as loc * The transactionId is used as loc
*/ */
internal class LocalEchoEventFactory @Inject constructor( internal class LocalEchoEventFactory @Inject constructor(
private val context: Context,
@UserId private val userId: String, @UserId private val userId: String,
private val stringProvider: StringProvider, private val stringProvider: StringProvider,
private val textPillsUtils: TextPillsUtils, private val textPillsUtils: TextPillsUtils,
@ -266,14 +268,14 @@ internal class LocalEchoEventFactory @Inject constructor(
height = height?.toInt() ?: 0, height = height?.toInt() ?: 0,
size = attachment.size.toInt() size = attachment.size.toInt()
), ),
url = attachment.path url = attachment.queryUri.toString()
) )
return createEvent(roomId, content) return createEvent(roomId, content)
} }
private fun createVideoEvent(roomId: String, attachment: ContentAttachmentData): Event { private fun createVideoEvent(roomId: String, attachment: ContentAttachmentData): Event {
val mediaDataRetriever = MediaMetadataRetriever() val mediaDataRetriever = MediaMetadataRetriever()
mediaDataRetriever.setDataSource(attachment.path) mediaDataRetriever.setDataSource(context, attachment.queryUri)
// Use frame to calculate height and width as we are sure to get the right ones // Use frame to calculate height and width as we are sure to get the right ones
val firstFrame: Bitmap? = mediaDataRetriever.frameAtTime val firstFrame: Bitmap? = mediaDataRetriever.frameAtTime
@ -281,7 +283,7 @@ internal class LocalEchoEventFactory @Inject constructor(
val width = firstFrame?.width ?: 0 val width = firstFrame?.width ?: 0
mediaDataRetriever.release() mediaDataRetriever.release()
val thumbnailInfo = ThumbnailExtractor.extractThumbnail(attachment)?.let { val thumbnailInfo = ThumbnailExtractor.extractThumbnail(context, attachment)?.let {
ThumbnailInfo( ThumbnailInfo(
width = it.width, width = it.width,
height = it.height, height = it.height,
@ -299,10 +301,10 @@ internal class LocalEchoEventFactory @Inject constructor(
size = attachment.size, size = attachment.size,
duration = attachment.duration?.toInt() ?: 0, duration = attachment.duration?.toInt() ?: 0,
// Glide will be able to use the local path and extract a thumbnail. // Glide will be able to use the local path and extract a thumbnail.
thumbnailUrl = attachment.path, thumbnailUrl = attachment.queryUri.toString(),
thumbnailInfo = thumbnailInfo thumbnailInfo = thumbnailInfo
), ),
url = attachment.path url = attachment.queryUri.toString()
) )
return createEvent(roomId, content) return createEvent(roomId, content)
} }
@ -315,7 +317,7 @@ internal class LocalEchoEventFactory @Inject constructor(
mimeType = attachment.getSafeMimeType()?.takeIf { it.isNotBlank() } ?: "audio/mpeg", mimeType = attachment.getSafeMimeType()?.takeIf { it.isNotBlank() } ?: "audio/mpeg",
size = attachment.size size = attachment.size
), ),
url = attachment.path url = attachment.queryUri.toString()
) )
return createEvent(roomId, content) return createEvent(roomId, content)
} }
@ -329,7 +331,7 @@ internal class LocalEchoEventFactory @Inject constructor(
?: "application/octet-stream", ?: "application/octet-stream",
size = attachment.size size = attachment.size
), ),
url = attachment.path url = attachment.queryUri.toString()
) )
return createEvent(roomId, content) return createEvent(roomId, content)
} }

View File

@ -54,6 +54,7 @@ class AudioPicker(override val requestCode: Int) : Picker<MultiPickerAudioType>(
selectedUriList.add(dataUri) selectedUriList.add(dataUri)
} else { } else {
data?.extras?.get(Intent.EXTRA_STREAM)?.let { data?.extras?.get(Intent.EXTRA_STREAM)?.let {
@Suppress("UNCHECKED_CAST")
when (it) { when (it) {
is List<*> -> selectedUriList.addAll(it as List<Uri>) is List<*> -> selectedUriList.addAll(it as List<Uri>)
else -> selectedUriList.add(it as Uri) else -> selectedUriList.add(it as Uri)

View File

@ -23,7 +23,6 @@ import android.graphics.BitmapFactory
import android.graphics.ImageDecoder import android.graphics.ImageDecoder
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Environment
import android.provider.MediaStore import android.provider.MediaStore
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.exifinterface.media.ExifInterface import androidx.exifinterface.media.ExifInterface

View File

@ -53,6 +53,7 @@ class FilePicker(override val requestCode: Int) : Picker<MultiPickerFileType>(re
selectedUriList.add(dataUri) selectedUriList.add(dataUri)
} else { } else {
data?.extras?.get(Intent.EXTRA_STREAM)?.let { data?.extras?.get(Intent.EXTRA_STREAM)?.let {
@Suppress("UNCHECKED_CAST")
when (it) { when (it) {
is List<*> -> selectedUriList.addAll(it as List<Uri>) is List<*> -> selectedUriList.addAll(it as List<Uri>)
else -> selectedUriList.add(it as Uri) else -> selectedUriList.add(it as Uri)

View File

@ -57,6 +57,7 @@ class ImagePicker(override val requestCode: Int) : Picker<MultiPickerImageType>(
selectedUriList.add(dataUri) selectedUriList.add(dataUri)
} else { } else {
data?.extras?.get(Intent.EXTRA_STREAM)?.let { data?.extras?.get(Intent.EXTRA_STREAM)?.let {
@Suppress("UNCHECKED_CAST")
when (it) { when (it) {
is List<*> -> selectedUriList.addAll(it as List<Uri>) is List<*> -> selectedUriList.addAll(it as List<Uri>)
else -> selectedUriList.add(it as Uri) else -> selectedUriList.add(it as Uri)

View File

@ -18,5 +18,4 @@ package im.vector.riotx.multipicker
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
class MultiPickerFileProvider : FileProvider() { class MultiPickerFileProvider : FileProvider()
}

View File

@ -54,6 +54,7 @@ class VideoPicker(override val requestCode: Int) : Picker<MultiPickerVideoType>(
selectedUriList.add(dataUri) selectedUriList.add(dataUri)
} else { } else {
data?.extras?.get(Intent.EXTRA_STREAM)?.let { data?.extras?.get(Intent.EXTRA_STREAM)?.let {
@Suppress("UNCHECKED_CAST")
when (it) { when (it) {
is List<*> -> selectedUriList.addAll(it as List<Uri>) is List<*> -> selectedUriList.addAll(it as List<Uri>)
else -> selectedUriList.add(it as Uri) else -> selectedUriList.add(it as Uri)

View File

@ -348,9 +348,6 @@ dependencies {
// Badge for compatibility // Badge for compatibility
implementation 'me.leolin:ShortcutBadger:1.1.22@aar' implementation 'me.leolin:ShortcutBadger:1.1.22@aar'
// File picker
implementation 'com.kbeanie:multipicker:1.6@aar'
// DI // DI
implementation "com.google.dagger:dagger:$daggerVersion" implementation "com.google.dagger:dagger:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion" kapt "com.google.dagger:dagger-compiler:$daggerVersion"

View File

@ -18,20 +18,13 @@ package im.vector.riotx.features.attachments
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.kbeanie.multipicker.api.Picker.PICK_AUDIO
import com.kbeanie.multipicker.api.Picker.PICK_CONTACT
import com.kbeanie.multipicker.api.Picker.PICK_FILE
import com.kbeanie.multipicker.api.Picker.PICK_IMAGE_CAMERA
import com.kbeanie.multipicker.api.Picker.PICK_IMAGE_DEVICE
import com.kbeanie.multipicker.core.ImagePickerImpl
import com.kbeanie.multipicker.core.PickerManager
import com.kbeanie.multipicker.utils.IntentUtils
import im.vector.matrix.android.BuildConfig import im.vector.matrix.android.BuildConfig
import im.vector.matrix.android.api.session.content.ContentAttachmentData import im.vector.matrix.android.api.session.content.ContentAttachmentData
import im.vector.riotx.core.platform.Restorable import im.vector.riotx.core.platform.Restorable
import im.vector.riotx.features.attachments.AttachmentsHelper.Callback import im.vector.riotx.multipicker.MultiPicker
import timber.log.Timber import timber.log.Timber
private const val CAPTURE_PATH_KEY = "CAPTURE_PATH_KEY" private const val CAPTURE_PATH_KEY = "CAPTURE_PATH_KEY"
@ -39,20 +32,8 @@ private const val PENDING_TYPE_KEY = "PENDING_TYPE_KEY"
/** /**
* This class helps to handle attachments by providing simple methods. * This class helps to handle attachments by providing simple methods.
* The process is asynchronous and you must implement [Callback] methods to get the data or a failure.
*/ */
class AttachmentsHelper private constructor(private val context: Context, class AttachmentsHelper(val context: Context, val callback: Callback) : Restorable {
private val pickerManagerFactory: PickerManagerFactory) : Restorable {
companion object {
fun create(fragment: Fragment, callback: Callback): AttachmentsHelper {
return AttachmentsHelper(fragment.requireContext(), FragmentPickerManagerFactory(fragment, callback))
}
fun create(activity: Activity, callback: Callback): AttachmentsHelper {
return AttachmentsHelper(activity, ActivityPickerManagerFactory(activity, callback))
}
}
interface Callback { interface Callback {
fun onContactAttachmentReady(contactAttachment: ContactAttachment) { fun onContactAttachmentReady(contactAttachment: ContactAttachment) {
@ -66,39 +47,15 @@ class AttachmentsHelper private constructor(private val context: Context,
} }
// Capture path allows to handle camera image picking. It must be restored if the activity gets killed. // Capture path allows to handle camera image picking. It must be restored if the activity gets killed.
private var capturePath: String? = null private var captureUri: Uri? = null
// The pending type is set if we have to handle permission request. It must be restored if the activity gets killed. // The pending type is set if we have to handle permission request. It must be restored if the activity gets killed.
var pendingType: AttachmentTypeSelectorView.Type? = null var pendingType: AttachmentTypeSelectorView.Type? = null
private val imagePicker by lazy {
pickerManagerFactory.createImagePicker()
}
private val videoPicker by lazy {
pickerManagerFactory.createVideoPicker()
}
private val cameraImagePicker by lazy {
pickerManagerFactory.createCameraImagePicker()
}
private val filePicker by lazy {
pickerManagerFactory.createFilePicker()
}
private val audioPicker by lazy {
pickerManagerFactory.createAudioPicker()
}
private val contactPicker by lazy {
pickerManagerFactory.createContactPicker()
}
// Restorable // Restorable
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
capturePath?.also { captureUri?.also {
outState.putString(CAPTURE_PATH_KEY, it) outState.putParcelable(CAPTURE_PATH_KEY, it)
} }
pendingType?.also { pendingType?.also {
outState.putSerializable(PENDING_TYPE_KEY, it) outState.putSerializable(PENDING_TYPE_KEY, it)
@ -106,10 +63,7 @@ class AttachmentsHelper private constructor(private val context: Context,
} }
override fun onRestoreInstanceState(savedInstanceState: Bundle?) { override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
capturePath = savedInstanceState?.getString(CAPTURE_PATH_KEY) captureUri = savedInstanceState?.getParcelable(CAPTURE_PATH_KEY) as? Uri
if (capturePath != null) {
cameraImagePicker.reinitialize(capturePath)
}
pendingType = savedInstanceState?.getSerializable(PENDING_TYPE_KEY) as? AttachmentTypeSelectorView.Type pendingType = savedInstanceState?.getSerializable(PENDING_TYPE_KEY) as? AttachmentTypeSelectorView.Type
} }
@ -118,36 +72,36 @@ class AttachmentsHelper private constructor(private val context: Context,
/** /**
* Starts the process for handling file picking * Starts the process for handling file picking
*/ */
fun selectFile() { fun selectFile(fragment: Fragment) {
filePicker.pickFile() MultiPicker.get(MultiPicker.FILE).startWith(fragment)
} }
/** /**
* Starts the process for handling image picking * Starts the process for handling image picking
*/ */
fun selectGallery() { fun selectGallery(fragment: Fragment) {
imagePicker.pickImage() MultiPicker.get(MultiPicker.IMAGE).startWith(fragment)
} }
/** /**
* Starts the process for handling audio picking * Starts the process for handling audio picking
*/ */
fun selectAudio() { fun selectAudio(fragment: Fragment) {
audioPicker.pickAudio() MultiPicker.get(MultiPicker.AUDIO).startWith(fragment)
} }
/** /**
* Starts the process for handling capture image picking * Starts the process for handling capture image picking
*/ */
fun openCamera() { fun openCamera(fragment: Fragment) {
capturePath = cameraImagePicker.pickImage() captureUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(fragment)
} }
/** /**
* Starts the process for handling contact picking * Starts the process for handling contact picking
*/ */
fun selectContact() { fun selectContact(fragment: Fragment) {
contactPicker.pickContact() MultiPicker.get(MultiPicker.CONTACT).startWith(fragment)
} }
/** /**
@ -157,14 +111,58 @@ class AttachmentsHelper private constructor(private val context: Context,
*/ */
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean { fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
val pickerManager = getPickerManagerForRequestCode(requestCode) when (requestCode) {
if (pickerManager != null) { MultiPicker.REQUEST_CODE_PICK_FILE -> {
if (pickerManager is ImagePickerImpl) { callback.onContentAttachmentsReady(
pickerManager.reinitialize(capturePath) MultiPicker.get(MultiPicker.FILE)
.getSelectedFiles(context, requestCode, resultCode, data)
.map { it.toContentAttachmentData() }
)
} }
pickerManager.submit(data) MultiPicker.REQUEST_CODE_PICK_AUDIO -> {
return true callback.onContentAttachmentsReady(
MultiPicker.get(MultiPicker.AUDIO)
.getSelectedFiles(context, requestCode, resultCode, data)
.map { it.toContentAttachmentData() }
)
}
MultiPicker.REQUEST_CODE_PICK_CONTACT -> {
MultiPicker.get(MultiPicker.CONTACT)
.getSelectedFiles(context, requestCode, resultCode, data)
.firstOrNull()
?.toContactAttachment()
?.let {
callback.onContactAttachmentReady(it)
}
}
MultiPicker.REQUEST_CODE_PICK_IMAGE -> {
callback.onContentAttachmentsReady(
MultiPicker.get(MultiPicker.IMAGE)
.getSelectedFiles(context, requestCode, resultCode, data)
.map { it.toContentAttachmentData() }
)
}
MultiPicker.REQUEST_CODE_TAKE_PHOTO -> {
captureUri?.let { captureUri ->
MultiPicker.get(MultiPicker.CAMERA)
.getTakenPhoto(context, requestCode, resultCode, captureUri)
?.let {
callback.onContentAttachmentsReady(
listOf(it).map { it.toContentAttachmentData() }
)
}
}
}
MultiPicker.REQUEST_CODE_PICK_VIDEO -> {
callback.onContentAttachmentsReady(
MultiPicker.get(MultiPicker.VIDEO)
.getSelectedFiles(context, requestCode, resultCode, data)
.map { it.toContentAttachmentData() }
)
}
else -> return false
} }
return true
} }
return false return false
} }
@ -174,39 +172,35 @@ class AttachmentsHelper private constructor(private val context: Context,
* *
* @return true if it can handle the intent data, false otherwise * @return true if it can handle the intent data, false otherwise
*/ */
fun handleShareIntent(intent: Intent): Boolean { fun handleShareIntent(context: Context, intent: Intent): Boolean {
val type = intent.resolveType(context) ?: return false val type = intent.resolveType(context) ?: return false
if (type.startsWith("image")) { if (type.startsWith("image")) {
imagePicker.submit(safeShareIntent(intent)) callback.onContentAttachmentsReady(
MultiPicker.get(MultiPicker.IMAGE).getIncomingFiles(context, intent).map {
it.toContentAttachmentData()
}
)
} else if (type.startsWith("video")) { } else if (type.startsWith("video")) {
videoPicker.submit(safeShareIntent(intent)) callback.onContentAttachmentsReady(
MultiPicker.get(MultiPicker.VIDEO).getIncomingFiles(context, intent).map {
it.toContentAttachmentData()
}
)
} else if (type.startsWith("audio")) { } else if (type.startsWith("audio")) {
videoPicker.submit(safeShareIntent(intent)) callback.onContentAttachmentsReady(
MultiPicker.get(MultiPicker.AUDIO).getIncomingFiles(context, intent).map {
it.toContentAttachmentData()
}
)
} else if (type.startsWith("application") || type.startsWith("file") || type.startsWith("*")) { } else if (type.startsWith("application") || type.startsWith("file") || type.startsWith("*")) {
filePicker.submit(safeShareIntent(intent)) callback.onContentAttachmentsReady(
MultiPicker.get(MultiPicker.FILE).getIncomingFiles(context, intent).map {
it.toContentAttachmentData()
}
)
} else { } else {
return false return false
} }
return true return true
} }
private fun safeShareIntent(intent: Intent): Intent {
// Work around for getPickerIntentForSharing doing NPE in android 10
return try {
IntentUtils.getPickerIntentForSharing(intent)
} catch (failure: Throwable) {
intent
}
}
private fun getPickerManagerForRequestCode(requestCode: Int): PickerManager? {
return when (requestCode) {
PICK_IMAGE_DEVICE -> imagePicker
PICK_IMAGE_CAMERA -> cameraImagePicker
PICK_FILE -> filePicker
PICK_CONTACT -> contactPicker
PICK_AUDIO -> audioPicker
else -> null
}
}
} }

View File

@ -16,51 +16,48 @@
package im.vector.riotx.features.attachments package im.vector.riotx.features.attachments
import com.kbeanie.multipicker.api.entity.ChosenAudio
import com.kbeanie.multipicker.api.entity.ChosenContact
import com.kbeanie.multipicker.api.entity.ChosenFile
import com.kbeanie.multipicker.api.entity.ChosenImage
import com.kbeanie.multipicker.api.entity.ChosenVideo
import im.vector.matrix.android.api.session.content.ContentAttachmentData import im.vector.matrix.android.api.session.content.ContentAttachmentData
import im.vector.riotx.multipicker.entity.MultiPickerAudioType
import im.vector.riotx.multipicker.entity.MultiPickerBaseType
import im.vector.riotx.multipicker.entity.MultiPickerContactType
import im.vector.riotx.multipicker.entity.MultiPickerFileType
import im.vector.riotx.multipicker.entity.MultiPickerImageType
import im.vector.riotx.multipicker.entity.MultiPickerVideoType
import timber.log.Timber import timber.log.Timber
fun ChosenContact.toContactAttachment(): ContactAttachment { fun MultiPickerContactType.toContactAttachment(): ContactAttachment {
return ContactAttachment( return ContactAttachment(
displayName = displayName, displayName = displayName,
photoUri = photoUri, photoUri = photoUri,
emails = emails.toList(), emails = emailList.toList(),
phones = phones.toList() phones = phoneNumberList.toList()
) )
} }
fun ChosenFile.toContentAttachmentData(): ContentAttachmentData { fun MultiPickerFileType.toContentAttachmentData(): ContentAttachmentData {
if (mimeType == null) Timber.w("No mimeType") if (mimeType == null) Timber.w("No mimeType")
return ContentAttachmentData( return ContentAttachmentData(
path = originalPath,
mimeType = mimeType, mimeType = mimeType,
type = mapType(), type = mapType(),
size = size, size = size,
date = createdAt?.time ?: System.currentTimeMillis(),
name = displayName, name = displayName,
queryUri = queryUri queryUri = contentUri
) )
} }
fun ChosenAudio.toContentAttachmentData(): ContentAttachmentData { fun MultiPickerAudioType.toContentAttachmentData(): ContentAttachmentData {
if (mimeType == null) Timber.w("No mimeType") if (mimeType == null) Timber.w("No mimeType")
return ContentAttachmentData( return ContentAttachmentData(
path = originalPath,
mimeType = mimeType, mimeType = mimeType,
type = mapType(), type = mapType(),
size = size, size = size,
date = createdAt?.time ?: System.currentTimeMillis(),
name = displayName, name = displayName,
duration = duration, duration = duration,
queryUri = queryUri queryUri = contentUri
) )
} }
private fun ChosenFile.mapType(): ContentAttachmentData.Type { private fun MultiPickerBaseType.mapType(): ContentAttachmentData.Type {
return when { return when {
mimeType?.startsWith("image/") == true -> ContentAttachmentData.Type.IMAGE mimeType?.startsWith("image/") == true -> ContentAttachmentData.Type.IMAGE
mimeType?.startsWith("video/") == true -> ContentAttachmentData.Type.VIDEO mimeType?.startsWith("video/") == true -> ContentAttachmentData.Type.VIDEO
@ -69,10 +66,9 @@ private fun ChosenFile.mapType(): ContentAttachmentData.Type {
} }
} }
fun ChosenImage.toContentAttachmentData(): ContentAttachmentData { fun MultiPickerImageType.toContentAttachmentData(): ContentAttachmentData {
if (mimeType == null) Timber.w("No mimeType") if (mimeType == null) Timber.w("No mimeType")
return ContentAttachmentData( return ContentAttachmentData(
path = originalPath,
mimeType = mimeType, mimeType = mimeType,
type = mapType(), type = mapType(),
name = displayName, name = displayName,
@ -80,23 +76,20 @@ fun ChosenImage.toContentAttachmentData(): ContentAttachmentData {
height = height.toLong(), height = height.toLong(),
width = width.toLong(), width = width.toLong(),
exifOrientation = orientation, exifOrientation = orientation,
date = createdAt?.time ?: System.currentTimeMillis(), queryUri = contentUri
queryUri = queryUri
) )
} }
fun ChosenVideo.toContentAttachmentData(): ContentAttachmentData { fun MultiPickerVideoType.toContentAttachmentData(): ContentAttachmentData {
if (mimeType == null) Timber.w("No mimeType") if (mimeType == null) Timber.w("No mimeType")
return ContentAttachmentData( return ContentAttachmentData(
path = originalPath,
mimeType = mimeType, mimeType = mimeType,
type = ContentAttachmentData.Type.VIDEO, type = ContentAttachmentData.Type.VIDEO,
size = size, size = size,
date = createdAt?.time ?: System.currentTimeMillis(),
height = height.toLong(), height = height.toLong(),
width = width.toLong(), width = width.toLong(),
duration = duration, duration = duration,
name = displayName, name = displayName,
queryUri = queryUri queryUri = contentUri
) )
} }

View File

@ -1,96 +0,0 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotx.features.attachments
import com.kbeanie.multipicker.api.callbacks.AudioPickerCallback
import com.kbeanie.multipicker.api.callbacks.ContactPickerCallback
import com.kbeanie.multipicker.api.callbacks.FilePickerCallback
import com.kbeanie.multipicker.api.callbacks.ImagePickerCallback
import com.kbeanie.multipicker.api.callbacks.VideoPickerCallback
import com.kbeanie.multipicker.api.entity.ChosenAudio
import com.kbeanie.multipicker.api.entity.ChosenContact
import com.kbeanie.multipicker.api.entity.ChosenFile
import com.kbeanie.multipicker.api.entity.ChosenImage
import com.kbeanie.multipicker.api.entity.ChosenVideo
/**
* This class delegates the PickerManager callbacks to an [AttachmentsHelper.Callback]
*/
class AttachmentsPickerCallback(private val callback: AttachmentsHelper.Callback)
: ImagePickerCallback,
FilePickerCallback,
VideoPickerCallback,
AudioPickerCallback,
ContactPickerCallback {
override fun onContactChosen(contact: ChosenContact?) {
if (contact == null) {
callback.onAttachmentsProcessFailed()
} else {
val contactAttachment = contact.toContactAttachment()
callback.onContactAttachmentReady(contactAttachment)
}
}
override fun onAudiosChosen(audios: MutableList<ChosenAudio>?) {
if (audios.isNullOrEmpty()) {
callback.onAttachmentsProcessFailed()
} else {
val attachments = audios.map {
it.toContentAttachmentData()
}
callback.onContentAttachmentsReady(attachments)
}
}
override fun onFilesChosen(files: MutableList<ChosenFile>?) {
if (files.isNullOrEmpty()) {
callback.onAttachmentsProcessFailed()
} else {
val attachments = files.map {
it.toContentAttachmentData()
}
callback.onContentAttachmentsReady(attachments)
}
}
override fun onImagesChosen(images: MutableList<ChosenImage>?) {
if (images.isNullOrEmpty()) {
callback.onAttachmentsProcessFailed()
} else {
val attachments = images.map {
it.toContentAttachmentData()
}
callback.onContentAttachmentsReady(attachments)
}
}
override fun onVideosChosen(videos: MutableList<ChosenVideo>?) {
if (videos.isNullOrEmpty()) {
callback.onAttachmentsProcessFailed()
} else {
val attachments = videos.map {
it.toContentAttachmentData()
}
callback.onContentAttachmentsReady(attachments)
}
}
override fun onError(error: String?) {
callback.onAttachmentsProcessFailed()
}
}

View File

@ -1,134 +0,0 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotx.features.attachments
import android.app.Activity
import androidx.fragment.app.Fragment
import com.kbeanie.multipicker.api.AudioPicker
import com.kbeanie.multipicker.api.CameraImagePicker
import com.kbeanie.multipicker.api.ContactPicker
import com.kbeanie.multipicker.api.FilePicker
import com.kbeanie.multipicker.api.ImagePicker
import com.kbeanie.multipicker.api.VideoPicker
/**
* Factory for creating different pickers. It allows to use with fragment or activity builders.
*/
interface PickerManagerFactory {
fun createImagePicker(): ImagePicker
fun createCameraImagePicker(): CameraImagePicker
fun createVideoPicker(): VideoPicker
fun createFilePicker(): FilePicker
fun createAudioPicker(): AudioPicker
fun createContactPicker(): ContactPicker
}
class ActivityPickerManagerFactory(private val activity: Activity, callback: AttachmentsHelper.Callback) : PickerManagerFactory {
private val attachmentsPickerCallback = AttachmentsPickerCallback(callback)
override fun createImagePicker(): ImagePicker {
return ImagePicker(activity).also {
it.setImagePickerCallback(attachmentsPickerCallback)
it.allowMultiple()
}
}
override fun createCameraImagePicker(): CameraImagePicker {
return CameraImagePicker(activity).also {
it.setImagePickerCallback(attachmentsPickerCallback)
}
}
override fun createVideoPicker(): VideoPicker {
return VideoPicker(activity).also {
it.setVideoPickerCallback(attachmentsPickerCallback)
it.allowMultiple()
}
}
override fun createFilePicker(): FilePicker {
return FilePicker(activity).also {
it.allowMultiple()
it.setFilePickerCallback(attachmentsPickerCallback)
}
}
override fun createAudioPicker(): AudioPicker {
return AudioPicker(activity).also {
it.allowMultiple()
it.setAudioPickerCallback(attachmentsPickerCallback)
}
}
override fun createContactPicker(): ContactPicker {
return ContactPicker(activity).also {
it.setContactPickerCallback(attachmentsPickerCallback)
}
}
}
class FragmentPickerManagerFactory(private val fragment: Fragment, callback: AttachmentsHelper.Callback) : PickerManagerFactory {
private val attachmentsPickerCallback = AttachmentsPickerCallback(callback)
override fun createImagePicker(): ImagePicker {
return ImagePicker(fragment).also {
it.setImagePickerCallback(attachmentsPickerCallback)
it.allowMultiple()
}
}
override fun createCameraImagePicker(): CameraImagePicker {
return CameraImagePicker(fragment).also {
it.setImagePickerCallback(attachmentsPickerCallback)
}
}
override fun createVideoPicker(): VideoPicker {
return VideoPicker(fragment).also {
it.setVideoPickerCallback(attachmentsPickerCallback)
it.allowMultiple()
}
}
override fun createFilePicker(): FilePicker {
return FilePicker(fragment).also {
it.allowMultiple()
it.setFilePickerCallback(attachmentsPickerCallback)
}
}
override fun createAudioPicker(): AudioPicker {
return AudioPicker(fragment).also {
it.allowMultiple()
it.setAudioPickerCallback(attachmentsPickerCallback)
}
}
override fun createContactPicker(): ContactPicker {
return ContactPicker(fragment).also {
it.setContactPickerCallback(attachmentsPickerCallback)
}
}
}

View File

@ -25,7 +25,7 @@ class AttachmentBigPreviewController @Inject constructor() : TypedEpoxyControlle
override fun buildModels(data: AttachmentsPreviewViewState) { override fun buildModels(data: AttachmentsPreviewViewState) {
data.attachments.forEach { data.attachments.forEach {
attachmentBigPreviewItem { attachmentBigPreviewItem {
id(it.path) id(it.queryUri.toString())
attachment(it) attachment(it)
} }
} }
@ -43,7 +43,7 @@ class AttachmentMiniaturePreviewController @Inject constructor() : TypedEpoxyCon
override fun buildModels(data: AttachmentsPreviewViewState) { override fun buildModels(data: AttachmentsPreviewViewState) {
data.attachments.forEachIndexed { index, contentAttachmentData -> data.attachments.forEachIndexed { index, contentAttachmentData ->
attachmentMiniaturePreviewItem { attachmentMiniaturePreviewItem {
id(contentAttachmentData.path) id(contentAttachmentData.queryUri.toString())
attachment(contentAttachmentData) attachment(contentAttachmentData)
checked(data.currentAttachmentIndex == index) checked(data.currentAttachmentIndex == index)
clickListener { _ -> clickListener { _ ->

View File

@ -33,11 +33,10 @@ abstract class AttachmentPreviewItem<H : AttachmentPreviewItem.Holder> : VectorE
abstract val attachment: ContentAttachmentData abstract val attachment: ContentAttachmentData
override fun bind(holder: H) { override fun bind(holder: H) {
val path = attachment.path
if (attachment.type == ContentAttachmentData.Type.VIDEO || attachment.type == ContentAttachmentData.Type.IMAGE) { if (attachment.type == ContentAttachmentData.Type.VIDEO || attachment.type == ContentAttachmentData.Type.IMAGE) {
Glide.with(holder.view.context) Glide.with(holder.view.context)
.asBitmap() .asBitmap()
.load(path) .load(attachment.queryUri)
.apply(RequestOptions().frame(0)) .apply(RequestOptions().frame(0))
.into(holder.imageView) .into(holder.imageView)
} else { } else {

View File

@ -17,10 +17,11 @@
package im.vector.riotx.features.attachments.preview package im.vector.riotx.features.attachments.preview
import android.net.Uri
import im.vector.riotx.core.platform.VectorViewModelAction import im.vector.riotx.core.platform.VectorViewModelAction
sealed class AttachmentsPreviewAction : VectorViewModelAction { sealed class AttachmentsPreviewAction : VectorViewModelAction {
object RemoveCurrentAttachment : AttachmentsPreviewAction() object RemoveCurrentAttachment : AttachmentsPreviewAction()
data class SetCurrentAttachment(val index: Int): AttachmentsPreviewAction() data class SetCurrentAttachment(val index: Int): AttachmentsPreviewAction()
data class UpdatePathOfCurrentAttachment(val newPath: String): AttachmentsPreviewAction() data class UpdatePathOfCurrentAttachment(val newUri: Uri): AttachmentsPreviewAction()
} }

View File

@ -172,9 +172,9 @@ class AttachmentsPreviewFragment @Inject constructor(
} }
private fun handleCropResult(result: Intent) { private fun handleCropResult(result: Intent) {
val resultPath = UCrop.getOutput(result)?.path val resultUri = UCrop.getOutput(result)
if (resultPath != null) { if (resultUri != null) {
viewModel.handle(AttachmentsPreviewAction.UpdatePathOfCurrentAttachment(resultPath)) viewModel.handle(AttachmentsPreviewAction.UpdatePathOfCurrentAttachment(resultUri))
} else { } else {
Toast.makeText(requireContext(), "Cannot retrieve cropped value", Toast.LENGTH_SHORT).show() Toast.makeText(requireContext(), "Cannot retrieve cropped value", Toast.LENGTH_SHORT).show()
} }
@ -203,7 +203,7 @@ class AttachmentsPreviewFragment @Inject constructor(
val currentAttachment = it.attachments.getOrNull(it.currentAttachmentIndex) ?: return@withState val currentAttachment = it.attachments.getOrNull(it.currentAttachmentIndex) ?: return@withState
val destinationFile = File(requireContext().cacheDir, "${currentAttachment.name}_edited_image_${System.currentTimeMillis()}") val destinationFile = File(requireContext().cacheDir, "${currentAttachment.name}_edited_image_${System.currentTimeMillis()}")
// Note: using currentAttachment.queryUri.toUri() make the app crash when sharing from Google Photos // Note: using currentAttachment.queryUri.toUri() make the app crash when sharing from Google Photos
val uri = File(currentAttachment.path).toUri() val uri = currentAttachment.queryUri
UCrop.of(uri, destinationFile.toUri()) UCrop.of(uri, destinationFile.toUri())
.withOptions( .withOptions(
UCrop.Options() UCrop.Options()

View File

@ -62,7 +62,7 @@ class AttachmentsPreviewViewModel @AssistedInject constructor(@Assisted initialS
private fun handleUpdatePathOfCurrentAttachment(action: AttachmentsPreviewAction.UpdatePathOfCurrentAttachment) = withState { private fun handleUpdatePathOfCurrentAttachment(action: AttachmentsPreviewAction.UpdatePathOfCurrentAttachment) = withState {
val attachments = it.attachments.mapIndexed { index, contentAttachmentData -> val attachments = it.attachments.mapIndexed { index, contentAttachmentData ->
if (index == it.currentAttachmentIndex) { if (index == it.currentAttachmentIndex) {
contentAttachmentData.copy(path = action.newPath) contentAttachmentData.copy(queryUri = action.newUri)
} else { } else {
contentAttachmentData contentAttachmentData
} }

View File

@ -251,7 +251,7 @@ class RoomDetailFragment @Inject constructor(
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
sharedActionViewModel = activityViewModelProvider.get(MessageSharedActionViewModel::class.java) sharedActionViewModel = activityViewModelProvider.get(MessageSharedActionViewModel::class.java)
attachmentsHelper = AttachmentsHelper.create(this, this).register() attachmentsHelper = AttachmentsHelper(requireContext(), this).register()
keyboardStateUtils = KeyboardStateUtils(requireActivity()) keyboardStateUtils = KeyboardStateUtils(requireActivity())
setupToolbar(roomToolbar) setupToolbar(roomToolbar)
setupRecyclerView() setupRecyclerView()
@ -517,29 +517,6 @@ class RoomDetailFragment @Inject constructor(
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
MultiPicker.REQUEST_CODE_PICK_IMAGE -> {
MultiPicker.get(MultiPicker.IMAGE).getSelectedFiles(requireContext(), requestCode, resultCode, data)
}
MultiPicker.REQUEST_CODE_PICK_VIDEO -> {
MultiPicker.get(MultiPicker.VIDEO).getSelectedFiles(requireContext(), requestCode, resultCode, data)
}
MultiPicker.REQUEST_CODE_PICK_FILE -> {
MultiPicker.get(MultiPicker.FILE).getSelectedFiles(requireContext(), requestCode, resultCode, data)
}
MultiPicker.REQUEST_CODE_PICK_AUDIO -> {
MultiPicker.get(MultiPicker.AUDIO).getSelectedFiles(requireContext(), requestCode, resultCode, data)
}
MultiPicker.REQUEST_CODE_PICK_CONTACT -> {
MultiPicker.get(MultiPicker.CONTACT).getSelectedFiles(requireContext(), requestCode, resultCode, data)
}
MultiPicker.REQUEST_CODE_TAKE_PHOTO -> {
cameraPhotoUri?.let {
MultiPicker.get(MultiPicker.CAMERA).getTakenPhoto(requireContext(), requestCode, resultCode, it)
}
}
}
val hasBeenHandled = attachmentsHelper.onActivityResult(requestCode, resultCode, data) val hasBeenHandled = attachmentsHelper.onActivityResult(requestCode, resultCode, data)
if (!hasBeenHandled && resultCode == RESULT_OK && data != null) { if (!hasBeenHandled && resultCode == RESULT_OK && data != null) {
when (requestCode) { when (requestCode) {
@ -689,7 +666,7 @@ class RoomDetailFragment @Inject constructor(
private fun sendUri(uri: Uri): Boolean { private fun sendUri(uri: Uri): Boolean {
roomDetailViewModel.preventAttachmentPreview = true roomDetailViewModel.preventAttachmentPreview = true
val shareIntent = Intent(Intent.ACTION_SEND, uri) val shareIntent = Intent(Intent.ACTION_SEND, uri)
val isHandled = attachmentsHelper.handleShareIntent(shareIntent) val isHandled = attachmentsHelper.handleShareIntent(requireContext(), shareIntent)
if (!isHandled) { if (!isHandled) {
roomDetailViewModel.preventAttachmentPreview = false roomDetailViewModel.preventAttachmentPreview = false
Toast.makeText(requireContext(), R.string.error_handling_incoming_share, Toast.LENGTH_SHORT).show() Toast.makeText(requireContext(), R.string.error_handling_incoming_share, Toast.LENGTH_SHORT).show()
@ -1372,16 +1349,13 @@ class RoomDetailFragment @Inject constructor(
} }
} }
private var cameraPhotoUri: Uri? = null
private fun launchAttachmentProcess(type: AttachmentTypeSelectorView.Type) { private fun launchAttachmentProcess(type: AttachmentTypeSelectorView.Type) {
when (type) { when (type) {
AttachmentTypeSelectorView.Type.CAMERA -> { AttachmentTypeSelectorView.Type.CAMERA -> attachmentsHelper.openCamera(this)
cameraPhotoUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(this) AttachmentTypeSelectorView.Type.FILE -> attachmentsHelper.selectFile(this)
} AttachmentTypeSelectorView.Type.GALLERY -> attachmentsHelper.selectGallery(this)
AttachmentTypeSelectorView.Type.FILE -> MultiPicker.get(MultiPicker.FILE).startWith(this) AttachmentTypeSelectorView.Type.AUDIO -> attachmentsHelper.selectAudio(this)
AttachmentTypeSelectorView.Type.GALLERY -> MultiPicker.get(MultiPicker.IMAGE).startWith(this) AttachmentTypeSelectorView.Type.CONTACT -> attachmentsHelper.selectContact(this)
AttachmentTypeSelectorView.Type.AUDIO -> MultiPicker.get(MultiPicker.AUDIO).startWith(this)
AttachmentTypeSelectorView.Type.CONTACT -> MultiPicker.get(MultiPicker.CONTACT).startWith(this)
AttachmentTypeSelectorView.Type.STICKER -> vectorBaseActivity.notImplemented("Adding stickers") AttachmentTypeSelectorView.Type.STICKER -> vectorBaseActivity.notImplemented("Adding stickers")
}.exhaustive }.exhaustive
} }

View File

@ -610,7 +610,7 @@ class RoomDetailViewModel @AssistedInject constructor(
when (val tooBigFile = attachments.find { it.size > maxUploadFileSize }) { when (val tooBigFile = attachments.find { it.size > maxUploadFileSize }) {
null -> room.sendMedias(attachments, action.compressBeforeSending, emptySet()) null -> room.sendMedias(attachments, action.compressBeforeSending, emptySet())
else -> _viewEvents.post(RoomDetailViewEvents.FileTooBigError( else -> _viewEvents.post(RoomDetailViewEvents.FileTooBigError(
tooBigFile.name ?: tooBigFile.path, tooBigFile.name ?: tooBigFile.queryUri.toString(),
tooBigFile.size, tooBigFile.size,
maxUploadFileSize maxUploadFileSize
)) ))

View File

@ -72,18 +72,18 @@ class IncomingShareFragment @Inject constructor(
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
setupRecyclerView() setupRecyclerView()
setupToolbar(incomingShareToolbar) setupToolbar(incomingShareToolbar)
attachmentsHelper = AttachmentsHelper.create(this, this).register() attachmentsHelper = AttachmentsHelper(requireContext(), this).register()
val intent = vectorBaseActivity.intent val intent = vectorBaseActivity.intent
val isShareManaged = when (intent?.action) { val isShareManaged = when (intent?.action) {
Intent.ACTION_SEND -> { Intent.ACTION_SEND -> {
var isShareManaged = attachmentsHelper.handleShareIntent(intent) var isShareManaged = attachmentsHelper.handleShareIntent(requireContext(), intent)
if (!isShareManaged) { if (!isShareManaged) {
isShareManaged = handleTextShare(intent) isShareManaged = handleTextShare(intent)
} }
isShareManaged isShareManaged
} }
Intent.ACTION_SEND_MULTIPLE -> attachmentsHelper.handleShareIntent(intent) Intent.ACTION_SEND_MULTIPLE -> attachmentsHelper.handleShareIntent(requireContext(), intent)
else -> false else -> false
} }