From 7c355e9aaeef2a4db12444b5ac59e1ab11822056 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 12 Apr 2020 20:41:31 +0200 Subject: [PATCH] determine the message attachments size at fetching --- .../smsmessenger/activities/ThreadActivity.kt | 24 ++++++++++++++++++- .../smsmessenger/extensions/Context.kt | 2 +- .../smsmessenger/models/Attachment.kt | 2 +- .../main/res/layout/item_received_message.xml | 6 ++--- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index c32fca90..2eb21a15 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.smsmessenger.activities import android.app.Activity import android.app.PendingIntent import android.content.Intent +import android.graphics.BitmapFactory import android.graphics.drawable.Drawable import android.net.Uri import android.os.Bundle @@ -83,8 +84,29 @@ class ThreadActivity : SimpleActivity() { } else { messages.first().participants } - setupAdapter() + messages.filter { it.attachment != null }.forEach { + it.attachment!!.attachments.forEach { + try { + val fileOptions = BitmapFactory.Options() + fileOptions.inJustDecodeBounds = true + BitmapFactory.decodeStream(contentResolver.openInputStream(it.uri), null, fileOptions) + it.width = fileOptions.outWidth + it.height = fileOptions.outHeight + + if (it.width == -1) { + it.width = 0 + } + + if (it.height == -1) { + it.height = 0 + } + } catch (ignored: Exception) { + } + } + } + + setupAdapter() runOnUiThread { supportActionBar?.title = participants.getThreadTitle() } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index f9d0f7de..5aeb4768 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -227,7 +227,7 @@ fun Context.getMmsAttachment(id: Int): MessageAttachment? { if (type == "text/plain") { messageAttachment.text = cursor.getStringValue(Mms.Part.TEXT) ?: "" } else if (type.startsWith("image/") || type.startsWith("video/")) { - val attachment = Attachment(Uri.withAppendedPath(uri, partId), type) + val attachment = Attachment(Uri.withAppendedPath(uri, partId), type, 0, 0) messageAttachment.attachments.add(attachment) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Attachment.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Attachment.kt index 6e166252..d471413a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Attachment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Attachment.kt @@ -2,4 +2,4 @@ package com.simplemobiletools.smsmessenger.models import android.net.Uri -data class Attachment(var uri: Uri, var type: String) +data class Attachment(var uri: Uri, var type: String, var width: Int, var height: Int) diff --git a/app/src/main/res/layout/item_received_message.xml b/app/src/main/res/layout/item_received_message.xml index a3b9e4d7..1d147b55 100644 --- a/app/src/main/res/layout/item_received_message.xml +++ b/app/src/main/res/layout/item_received_message.xml @@ -36,8 +36,8 @@ android:id="@+id/thread_message_play_outline" android:layout_width="@dimen/play_outline_size" android:layout_height="@dimen/play_outline_size" - android:layout_alignEnd="@+id/thread_message_image" - android:layout_alignBottom="@+id/thread_message_image" + android:layout_alignEnd="@+id/thread_mesage_attachments_holder" + android:layout_alignBottom="@+id/thread_mesage_attachments_holder" android:layout_margin="@dimen/medium_margin" android:src="@drawable/ic_play_outline" android:visibility="gone" /> @@ -46,7 +46,7 @@ android:id="@+id/thread_message_body" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_below="@+id/thread_message_image" + android:layout_below="@+id/thread_mesage_attachments_holder" android:layout_toEndOf="@+id/thread_message_photo" android:background="@drawable/item_received_background" android:padding="@dimen/normal_margin"