From aecaec87730fcb8ffa67f5b5bcf6630cd02a166c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 12 Apr 2020 21:43:42 +0200 Subject: [PATCH] crop too tall mms attachments --- .../smsmessenger/activities/ThreadActivity.kt | 22 +++++++++++++------ .../smsmessenger/adapters/ThreadAdapter.kt | 14 ++++++++---- .../main/res/layout/item_received_message.xml | 3 ++- app/src/main/res/layout/item_sent_message.xml | 3 ++- 4 files changed, 29 insertions(+), 13 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 2eb21a15..a7b0454b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -5,6 +5,7 @@ import android.app.PendingIntent import android.content.Intent import android.graphics.BitmapFactory import android.graphics.drawable.Drawable +import android.media.MediaMetadataRetriever import android.net.Uri import android.os.Bundle import android.provider.MediaStore @@ -88,17 +89,24 @@ class ThreadActivity : SimpleActivity() { 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.type.startsWith("image/")) { + val fileOptions = BitmapFactory.Options() + fileOptions.inJustDecodeBounds = true + BitmapFactory.decodeStream(contentResolver.openInputStream(it.uri), null, fileOptions) + it.width = fileOptions.outWidth + it.height = fileOptions.outHeight + } else if (it.type.startsWith("video/")) { + val metaRetriever = MediaMetadataRetriever() + metaRetriever.setDataSource(this, it.uri) + it.width = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH).toInt() + it.height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT).toInt() + } - if (it.width == -1) { + if (it.width < 0) { it.width = 0 } - if (it.height == -1) { + if (it.height < 0) { it.height = 0 } } catch (ignored: Exception) { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt index c88238a6..6d3b8df5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt @@ -9,6 +9,7 @@ import com.bumptech.glide.Glide import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.GlideException +import com.bumptech.glide.load.resource.bitmap.CenterCrop import com.bumptech.glide.load.resource.bitmap.FitCenter import com.bumptech.glide.load.resource.bitmap.RoundedCorners import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions @@ -198,11 +199,13 @@ class ThreadAdapter( val type = attachment.type if (type.startsWith("image/") || type.startsWith("video/")) { val uri = attachment.uri + val isTallImage = attachment.height > attachment.width + val transformation = if (isTallImage) CenterCrop() else FitCenter() val options = RequestOptions() .diskCacheStrategy(DiskCacheStrategy.NONE) - .transform(FitCenter(), RoundedCorners(roundedCornersRadius)) + .transform(transformation, RoundedCorners(roundedCornersRadius)) - Glide.with(context) + var builder = Glide.with(context) .load(uri) .transition(DrawableTransitionOptions.withCrossFade()) .apply(options) @@ -215,10 +218,13 @@ class ThreadAdapter( override fun onResourceReady(dr: Drawable?, a: Any?, t: Target?, d: DataSource?, i: Boolean) = false - }) - .into(imageView.attachment_image) + if (isTallImage) { + builder = builder.override(attachment.width, attachment.width) + } + + builder.into(imageView.attachment_image) attachment_image.setOnClickListener { Intent().apply { action = Intent.ACTION_VIEW diff --git a/app/src/main/res/layout/item_received_message.xml b/app/src/main/res/layout/item_received_message.xml index 1d147b55..447d848e 100644 --- a/app/src/main/res/layout/item_received_message.xml +++ b/app/src/main/res/layout/item_received_message.xml @@ -38,7 +38,8 @@ android:layout_height="@dimen/play_outline_size" android:layout_alignEnd="@+id/thread_mesage_attachments_holder" android:layout_alignBottom="@+id/thread_mesage_attachments_holder" - android:layout_margin="@dimen/medium_margin" + android:layout_marginStart="@dimen/medium_margin" + android:layout_marginBottom="@dimen/activity_margin" android:src="@drawable/ic_play_outline" android:visibility="gone" /> diff --git a/app/src/main/res/layout/item_sent_message.xml b/app/src/main/res/layout/item_sent_message.xml index 6244355b..fa5a3a83 100644 --- a/app/src/main/res/layout/item_sent_message.xml +++ b/app/src/main/res/layout/item_sent_message.xml @@ -29,7 +29,8 @@ android:layout_height="@dimen/play_outline_size" android:layout_alignEnd="@+id/thread_mesage_attachments_holder" android:layout_alignBottom="@+id/thread_mesage_attachments_holder" - android:layout_margin="@dimen/medium_margin" + android:layout_marginEnd="@dimen/medium_margin" + android:layout_marginBottom="@dimen/activity_margin" android:src="@drawable/ic_play_outline" android:visibility="gone" />