Fix potential glide lifecycle issue with transition animation.

This commit is contained in:
Greyson Parrelli
2026-04-01 11:51:20 -04:00
parent 7320a0ef46
commit cc847cb229
2 changed files with 24 additions and 1 deletions

View File

@@ -90,7 +90,7 @@ class MediaPreviewV2Activity : PassphraseRequiredActivity(), VoiceNoteMediaContr
setContentView(R.layout.activity_mediapreview_v2)
transitionImageView = findViewById(R.id.transition_image_view)
val cacheDrawable = MediaPreviewCache.drawable
val cacheDrawable = MediaPreviewCache.drawable?.let { RecycledBitmapGuardDrawable(it) }
if (cacheDrawable != null && !args.skipSharedElementTransition) {
val bounds = cacheDrawable.bounds
val aspectRatio = bounds.width().toFloat() / bounds.height()

View File

@@ -0,0 +1,23 @@
package org.thoughtcrime.securesms.mediapreview
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.graphics.drawable.DrawableWrapper
/**
* A wrapper that skips drawing upon failure. This is to guard against situations where we may
* be using a bitmap from Glide that could be recycled at a time outside our control
*
* If you ever truly need the bitmap in this case, you should save it yourself. But there are situations
* (like transition animations) where having a bitmap isn't strictly necessary, and we'd rather
* show nothing than crash or have to manage the bitmap lifecycle ourselves.
*/
class RecycledBitmapGuardDrawable(drawable: Drawable) : DrawableWrapper(drawable) {
override fun draw(canvas: Canvas) {
try {
super.draw(canvas)
} catch (_: RuntimeException) {
// Bitmap was recycled — nothing to draw.
}
}
}