From 169ac9d0a08da3ad0acddaf74c013eb2733279f8 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 6 Jul 2022 12:06:17 +0100 Subject: [PATCH] updating the known SDK mimetypes and making use of them for the intent switches --- .../matrix/android/sdk/api/util/MimeTypes.kt | 6 ++++++ .../features/attachments/ShareIntentHandler.kt | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/MimeTypes.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/MimeTypes.kt index ef47775f1b..5ec0dedadf 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/MimeTypes.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/MimeTypes.kt @@ -33,9 +33,15 @@ object MimeTypes { const val Ogg = "audio/ogg" + const val PlainText = "text/plain" + fun String?.normalizeMimeType() = if (this == BadJpg) Jpeg else this fun String?.isMimeTypeImage() = this?.startsWith("image/").orFalse() fun String?.isMimeTypeVideo() = this?.startsWith("video/").orFalse() fun String?.isMimeTypeAudio() = this?.startsWith("audio/").orFalse() + fun String?.isMimeTypeApplication() = this?.startsWith("application/").orFalse() + fun String?.isMimeTypeFile() = this?.startsWith("file/").orFalse() + fun String?.isMimeTypeText() = this?.startsWith("text/").orFalse() + fun String?.isMimeTypeAny() = this?.startsWith("*/").orFalse() } diff --git a/vector/src/main/java/im/vector/app/features/attachments/ShareIntentHandler.kt b/vector/src/main/java/im/vector/app/features/attachments/ShareIntentHandler.kt index 1d722c2341..18caba10d9 100644 --- a/vector/src/main/java/im/vector/app/features/attachments/ShareIntentHandler.kt +++ b/vector/src/main/java/im/vector/app/features/attachments/ShareIntentHandler.kt @@ -19,6 +19,14 @@ package im.vector.app.features.attachments import android.content.Context import android.content.Intent import org.matrix.android.sdk.api.session.content.ContentAttachmentData +import org.matrix.android.sdk.api.util.MimeTypes +import org.matrix.android.sdk.api.util.MimeTypes.isMimeTypeAny +import org.matrix.android.sdk.api.util.MimeTypes.isMimeTypeApplication +import org.matrix.android.sdk.api.util.MimeTypes.isMimeTypeAudio +import org.matrix.android.sdk.api.util.MimeTypes.isMimeTypeFile +import org.matrix.android.sdk.api.util.MimeTypes.isMimeTypeImage +import org.matrix.android.sdk.api.util.MimeTypes.isMimeTypeText +import org.matrix.android.sdk.api.util.MimeTypes.isMimeTypeVideo import javax.inject.Inject class ShareIntentHandler @Inject constructor( @@ -34,11 +42,11 @@ class ShareIntentHandler @Inject constructor( fun handleIncomingShareIntent(intent: Intent, onFile: (List) -> Unit, onPlainText: (String) -> Unit): Boolean { val type = intent.resolveType(context) ?: return false return when { - type == "text/plain" -> handlePlainText(intent, onPlainText) - type.startsWith("image") -> onFile(multiPickerIncomingFiles.image(intent)).let { true } - type.startsWith("video") -> onFile(multiPickerIncomingFiles.video(intent)).let { true } - type.startsWith("audio") -> onFile(multiPickerIncomingFiles.audio(intent)).let { true } - type.startsWith("application") || type.startsWith("file") || type.startsWith("text") || type.startsWith("*") -> { + type == MimeTypes.PlainText -> handlePlainText(intent, onPlainText) + type.isMimeTypeImage() -> onFile(multiPickerIncomingFiles.image(intent)).let { true } + type.isMimeTypeVideo() -> onFile(multiPickerIncomingFiles.video(intent)).let { true } + type.isMimeTypeAudio() -> onFile(multiPickerIncomingFiles.audio(intent)).let { true } + type.isMimeTypeApplication() || type.isMimeTypeFile() || type.isMimeTypeText() || type.isMimeTypeAny() -> { onFile(multiPickerIncomingFiles.file(intent)).let { true } } else -> false