Merge pull request #282 from gavine99/ingest-all-attachments-in-standard-manner

ingest attachments in standard manner
This commit is contained in:
octoshrimpy
2025-02-13 09:29:03 -05:00
committed by GitHub
7 changed files with 58 additions and 76 deletions
@@ -392,8 +392,8 @@ class MessageRepositoryImpl @Inject constructor(
.filter { it.getResourceBytes(context).isNotEmpty() } .filter { it.getResourceBytes(context).isNotEmpty() }
.associateWith { .associateWith {
when (it.getType(context) == "image/gif") { when (it.getType(context) == "image/gif") {
true -> ImageUtils.getScaledGif(context, it.getUri(), maxWidth, maxHeight) true -> ImageUtils.getScaledGif(context, it.uri, maxWidth, maxHeight)
false -> ImageUtils.getScaledImage(context, it.getUri(), maxWidth, maxHeight) false -> ImageUtils.getScaledImage(context, it.uri, maxWidth, maxHeight)
} }
} }
.toMutableMap() .toMutableMap()
@@ -401,7 +401,7 @@ class MessageRepositoryImpl @Inject constructor(
val imageByteCount = imageBytesByAttachment.values.sumOf { it.size } val imageByteCount = imageBytesByAttachment.values.sumOf { it.size }
if (imageByteCount > remainingBytes) { if (imageByteCount > remainingBytes) {
imageBytesByAttachment.forEach { (attachment, originalBytes) -> imageBytesByAttachment.forEach { (attachment, originalBytes) ->
val uri = attachment.getUri() ?: return@forEach val uri = attachment.uri ?: return@forEach
val maxBytes = originalBytes.size / imageByteCount.toFloat() * remainingBytes val maxBytes = originalBytes.size / imageByteCount.toFloat() * remainingBytes
// Get the image dimensions // Get the image dimensions
@@ -429,8 +429,8 @@ class MessageRepositoryImpl @Inject constructor(
attempts++ attempts++
scaledBytes = when (attachment.getType(context) == "image/gif") { scaledBytes = when (attachment.getType(context) == "image/gif") {
true -> ImageUtils.getScaledGif(context, attachment.getUri(), newWidth, newHeight) true -> ImageUtils.getScaledGif(context, attachment.uri, newWidth, newHeight)
false -> ImageUtils.getScaledImage(context, attachment.getUri(), newWidth, newHeight) false -> ImageUtils.getScaledImage(context, attachment.uri, newWidth, newHeight)
} }
Timber.d("Compression attempt $attempts: ${scaledBytes.size / 1024}/${maxBytes.toInt() / 1024}Kb ($width*$height -> $newWidth*$newHeight)") Timber.d("Compression attempt $attempts: ${scaledBytes.size / 1024}/${maxBytes.toInt() / 1024}Kb ($width*$height -> $newWidth*$newHeight)")
@@ -47,7 +47,7 @@ class SendScheduledMessage @Inject constructor(
} }
.map { message -> .map { message ->
val threadId = TelephonyCompat.getOrCreateThreadId(context, message.recipients) val threadId = TelephonyCompat.getOrCreateThreadId(context, message.recipients)
val attachments = message.attachments.mapNotNull(Uri::parse).map { Attachment(it) } val attachments = message.attachments.mapNotNull(Uri::parse).map { Attachment(context, it) }
SendMessage.Params(message.subId, threadId, message.recipients, message.body, attachments) SendMessage.Params(message.subId, threadId, message.recipients, message.body, attachments)
} }
.flatMap(sendMessage::buildObservable) .flatMap(sendMessage::buildObservable)
@@ -18,49 +18,60 @@
*/ */
package dev.octoshrimpy.quik.model package dev.octoshrimpy.quik.model
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.provider.ContactsContract
import android.provider.OpenableColumns import android.provider.OpenableColumns
import androidx.core.view.inputmethod.InputContentInfoCompat import androidx.core.view.inputmethod.InputContentInfoCompat
class Attachment( @SuppressLint("Range")
private val uri: Uri? = null, class Attachment (
private val inputContent: InputContentInfoCompat? = null context: Context,
var uri: Uri = Uri.EMPTY,
inputContent: InputContentInfoCompat? = null
) { ) {
private var resourceBytes: ByteArray? = null private var resourceBytes: ByteArray? = null
fun getUri(): Uri { init {
return ( if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) uri = inputContent?.contentUri ?: uri
inputContent?.contentUri ?: uri ?: Uri.EMPTY
else uri ?: Uri.EMPTY // if constructed with a uri to a contact, convert uri to associated vcard uri
) if (context.contentResolver.getType(uri)?.lowercase() ==
ContactsContract.Contacts.CONTENT_ITEM_TYPE) {
uri = try {
Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_VCARD_URI,
context.contentResolver.query(
uri, null, null, null, null
)?.use {
it.moveToFirst()
it.getString(it.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)) ?: ""
}
)
} catch (e: Exception) {
Uri.EMPTY
}
}
} }
fun getType(context: Context): String { fun getType(context: Context): String {
return context.contentResolver.getType(getUri()) ?: "application/octect-stream" return context.contentResolver.getType(uri) ?: "application/octect-stream"
} }
fun getName(context: Context): String { fun getName(context: Context): String {
try { try {
context.contentResolver.query( context.contentResolver.query(
getUri(), uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null
arrayOf(OpenableColumns.DISPLAY_NAME), )?.use {
null, it.moveToFirst()
null, return it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME)) ?: "unknown"
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*/ } catch (e: Exception) { /* nothing */ }
return "unknown" return "unknown"
} }
@@ -75,30 +86,20 @@ class Attachment(
} }
fun isAudio(context: Context): Boolean { fun isAudio(context: Context): Boolean {
val mimeType = getType(context) return (getType(context).startsWith("audio/"))
return (mimeType.startsWith("audio/"))
} }
fun isImage(context: Context): Boolean { fun isImage(context: Context): Boolean {
val mimeType = getType(context) return (getType(context).startsWith("image/"))
return (mimeType.startsWith("image/"))
} }
fun getSize(context: Context): Long { fun getSize(context: Context): Long {
try { try {
context.contentResolver.query( context.contentResolver.query(
getUri(), uri, arrayOf(OpenableColumns.SIZE), null, null, null
arrayOf(OpenableColumns.SIZE), )?.use {
null,
null,
null
)
?.use {
val sizeIndex = it.getColumnIndex(OpenableColumns.SIZE)
if (sizeIndex == -1)
return -1
it.moveToFirst() it.moveToFirst()
return it.getLong(sizeIndex) return it.getLong(it.getColumnIndex(OpenableColumns.SIZE))
} }
} }
catch (e: Exception) { /* nothing */ } catch (e: Exception) { /* nothing */ }
@@ -110,16 +111,14 @@ class Attachment(
if (resourceBytes != null) if (resourceBytes != null)
return resourceBytes!! return resourceBytes!!
resourceBytes = ByteArray(0)
try { try {
context.contentResolver.openInputStream(getUri())?.use { context.contentResolver.openInputStream(uri)?.use {
resourceBytes = it.readBytes() resourceBytes = it.readBytes()
} }
} catch (e: Exception) { } catch (e: Exception) {
} }
return resourceBytes!! return resourceBytes ?: ByteArray(0)
} }
} }
@@ -93,14 +93,14 @@ class AttachmentAdapter @Inject constructor(
holder.fileName.visibility = View.GONE holder.fileName.visibility = View.GONE
// if attachment uri is missing // if attachment uri is missing
if (!attachment.getUri().resourceExists(context)) { if (!attachment.uri.resourceExists(context)) {
holder.thumbnail.setImageResource(android.R.drawable.ic_delete) holder.thumbnail.setImageResource(android.R.drawable.ic_delete)
holder.fileName.text = context.getString(R.string.attachment_missing) holder.fileName.text = context.getString(R.string.attachment_missing)
holder.fileName.visibility = View.VISIBLE holder.fileName.visibility = View.VISIBLE
return return
} }
val uri = attachment.getUri() val uri = attachment.uri
val mimeType = attachment.getType(context) val mimeType = attachment.getType(context)
// if attachment mime type is image/* or video/*, use image/frame // if attachment mime type is image/* or video/*, use image/frame
@@ -446,7 +446,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
override fun requestContact() { override fun requestContact() {
val intent = Intent(Intent.ACTION_PICK) val intent = Intent(Intent.ACTION_PICK)
.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE) .setType(ContactsContract.Contacts.CONTENT_TYPE)
startActivityForResult(Intent.createChooser(intent, null), ComposeView.AttachContactRequestCode) startActivityForResult(Intent.createChooser(intent, null), ComposeView.AttachContactRequestCode)
} }
@@ -79,7 +79,7 @@ class ComposeActivityModule {
activity.intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)?.run(uris::add) activity.intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)?.run(uris::add)
activity.intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)?.run(uris::addAll) activity.intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)?.run(uris::addAll)
return Attachments(uris.mapNotNull { Attachment(it) }) return Attachments(uris.mapNotNull { Attachment(activity, it) })
} }
@Provides @Provides
@@ -19,9 +19,7 @@
package dev.octoshrimpy.quik.feature.compose package dev.octoshrimpy.quik.feature.compose
import android.content.Context import android.content.Context
import android.net.Uri
import android.os.Vibrator import android.os.Vibrator
import android.provider.ContactsContract
import android.telephony.SmsMessage import android.telephony.SmsMessage
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import com.moez.QKSMS.contentproviders.MmsPartProvider import com.moez.QKSMS.contentproviders.MmsPartProvider
@@ -652,8 +650,8 @@ class ComposeViewModel @Inject constructor(
// a file, photo or otherwise, was picked by the user // a file, photo or otherwise, was picked by the user
Observable.merge( Observable.merge(
view.attachAnyFileSelectedIntent.map { uri -> Attachment(uri) }, view.attachAnyFileSelectedIntent.map { uri -> Attachment(context, uri) },
view.inputContentIntent.map { inputContent -> Attachment(inputContent = inputContent) } view.inputContentIntent.map { inputContent -> Attachment(context, inputContent = inputContent) }
) )
.withLatestFrom(attachments) { attachment, attachments -> attachments + attachment } .withLatestFrom(attachments) { attachment, attachments -> attachments + attachment }
.doOnNext(attachments::onNext) .doOnNext(attachments::onNext)
@@ -677,7 +675,7 @@ class ComposeViewModel @Inject constructor(
// Contact was selected for attachment // Contact was selected for attachment
view.contactSelectedIntent view.contactSelectedIntent
.map { uri -> Attachment(getFullVCardUri(uri)) } .map { uri -> Attachment(context, uri) }
.withLatestFrom(attachments) { attachment, attachments -> attachments + attachment } .withLatestFrom(attachments) { attachment, attachments -> attachments + attachment }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -801,9 +799,7 @@ class ComposeViewModel @Inject constructor(
// Scheduling a message // Scheduling a message
state.scheduled != 0L -> { state.scheduled != 0L -> {
newState { copy(scheduled = 0) } newState { copy(scheduled = 0) }
val uris = attachments val uris = attachments.map { it.uri.toString() }
.map { it.getUri() }
.map { it.toString() }
val params = AddScheduledMessage val params = AddScheduledMessage
.Params(state.scheduled, subId, addresses, sendAsGroup, body, uris) .Params(state.scheduled, subId, addresses, sendAsGroup, body, uris)
addScheduledMessage.execute(params) addScheduledMessage.execute(params)
@@ -881,17 +877,4 @@ class ComposeViewModel @Inject constructor(
} }
private fun getFullVCardUri(contactData: Uri): Uri {
val lookupKey = context.contentResolver.query(contactData, null, null, null, null)?.use {
it.moveToFirst()
val index = it.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)
if (index >= 0)
it.getString(index)
else
""
}
return Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey)
}
} }