changes to allow attaching any type of file. mainly for video and audio attachments but nothing to stop any type of file being attached

This commit is contained in:
gavine99
2025-01-26 16:57:36 +11:00
parent 33347fa157
commit 314704a61a
27 changed files with 505 additions and 197 deletions
@@ -47,7 +47,7 @@ class SendScheduledMessage @Inject constructor(
}
.map { message ->
val threadId = TelephonyCompat.getOrCreateThreadId(context, message.recipients)
val attachments = message.attachments.mapNotNull(Uri::parse).map { Attachment.Image(it) }
val attachments = message.attachments.mapNotNull(Uri::parse).map { Attachment(it) }
SendMessage.Params(message.subId, threadId, message.recipients, message.body, attachments)
}
.flatMap(sendMessage::buildObservable)
@@ -21,33 +21,106 @@ package dev.octoshrimpy.quik.model
import android.content.Context
import android.net.Uri
import android.os.Build
import android.provider.OpenableColumns
import androidx.core.view.inputmethod.InputContentInfoCompat
sealed class Attachment {
data class Image(
private val uri: Uri? = null,
private val inputContent: InputContentInfoCompat? = null
) : Attachment() {
class Attachment(
private val uri: Uri? = null,
private val inputContent: InputContentInfoCompat? = null
) {
private var resourceBytes: ByteArray? = null
fun getUri(): Uri? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
inputContent?.contentUri ?: uri
} else {
uri
}
}
fun isGif(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && inputContent != null) {
inputContent.description.hasMimeType("image/gif")
} else {
uri?.let(context.contentResolver::getType) == "image/gif"
}
}
fun getUri(): Uri {
return (
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1)
inputContent?.contentUri ?: uri ?: Uri.EMPTY
else uri ?: Uri.EMPTY
)
}
data class Contact(val vCard: String) : Attachment()
fun getType(context: Context): String {
return context.contentResolver.getType(getUri()) ?: "application/octect-stream"
}
fun getName(context: Context): String {
try {
context.contentResolver.query(
getUri(),
arrayOf(OpenableColumns.DISPLAY_NAME),
null,
null,
null
)
?.use { cursor ->
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
if (nameIndex == -1)
return "unknown"
cursor.moveToFirst()
return cursor.getString(nameIndex) ?: "unknown"
}
}
catch (e: Exception) { /* nothing*/ }
return "unknown"
}
fun isVCard(context: Context): Boolean {
return (getType(context) == "text/x-vcard")
}
fun hasDisplayableImage(context: Context): Boolean {
val mimeType = getType(context)
return (mimeType.startsWith("image/") || mimeType.startsWith("video/"))
}
fun isAudio(context: Context): Boolean {
val mimeType = getType(context)
return (mimeType.startsWith("audio/"))
}
fun isImage(context: Context): Boolean {
val mimeType = getType(context)
return (mimeType.startsWith("image/"))
}
fun getSize(context: Context): Long {
try {
context.contentResolver.query(
getUri(),
arrayOf(OpenableColumns.SIZE),
null,
null,
null
)
?.use {
val sizeIndex = it.getColumnIndex(OpenableColumns.SIZE)
if (sizeIndex == -1)
return -1
it.moveToFirst()
return it.getLong(sizeIndex)
}
}
catch (e: Exception) { /* nothing */ }
return -1
}
fun getResourceBytes(context: Context): ByteArray {
if (resourceBytes != null)
return resourceBytes!!
resourceBytes = ByteArray(0)
try {
context.contentResolver.openInputStream(getUri())?.use {
resourceBytes = it.readBytes()
}
} catch (e: Exception) {
}
return resourceBytes!!
}
}
@@ -40,11 +40,13 @@ open class MmsPart : RealmObject() {
fun getUri() = "content://mms/part/$id".toUri()
fun getSummary(): String? = when {
type == "application/smil" -> null
type == "text/plain" -> text
type == "text/x-vCard" -> "Contact card"
type == "text/x-vcard" -> "Contact card"
type.startsWith("image") -> "Photo"
type.startsWith("video") -> "Video"
else -> null
type.startsWith("audio") -> "Audio"
else -> type.substring(type.indexOf('/') + 1)
}
}