Skip to content

Commit 1485960

Browse files
committed
limit shown attachment sizes in thread to avoid OOM
1 parent 44a6cfb commit 1485960

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.graphics.Typeface
88
import android.graphics.drawable.ColorDrawable
99
import android.graphics.drawable.Drawable
1010
import android.net.Uri
11+
import android.util.Size
1112
import android.util.TypedValue
1213
import android.view.Menu
1314
import android.view.View
@@ -60,6 +61,7 @@ class ThreadAdapter(
6061

6162
@SuppressLint("MissingPermission")
6263
private val hasMultipleSIMCards = (activity.subscriptionManagerCompat().activeSubscriptionInfoList?.size ?: 0) > 1
64+
private val maxChatBubbleWidth = activity.usableScreenSize.x * 0.8f
6365

6466
init {
6567
setupDragListener(true)
@@ -374,14 +376,20 @@ class ThreadAdapter(
374376
return false
375377
}
376378

377-
override fun onResourceReady(dr: Drawable?, a: Any?, t: Target<Drawable>?, d: DataSource?, i: Boolean) =
378-
false
379+
override fun onResourceReady(dr: Drawable?, a: Any?, t: Target<Drawable>?, d: DataSource?, i: Boolean) = false
379380
})
380381

382+
// limit attachment sizes to avoid causing OOM
383+
var wantedAttachmentSize = Size(attachment.width, attachment.height)
384+
if (wantedAttachmentSize.width > maxChatBubbleWidth) {
385+
val newHeight = wantedAttachmentSize.height / (wantedAttachmentSize.width / maxChatBubbleWidth)
386+
wantedAttachmentSize = Size(maxChatBubbleWidth.toInt(), newHeight.toInt())
387+
}
388+
381389
builder = if (isTallImage) {
382-
builder.override(attachment.width, attachment.width)
390+
builder.override(wantedAttachmentSize.width, wantedAttachmentSize.width)
383391
} else {
384-
builder.override(attachment.width, attachment.height)
392+
builder.override(wantedAttachmentSize.width, wantedAttachmentSize.height)
385393
}
386394

387395
try {

0 commit comments

Comments
 (0)