From 61cd9767c8015c0cec064e3efa7e92f15c2bc0c8 Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Mon, 10 Jul 2023 17:08:27 -0300 Subject: [PATCH] Ensure bitmaps are not recycled when getting them from the cache. --- .../securesms/wallpaper/UriChatWallpaper.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/wallpaper/UriChatWallpaper.java b/app/src/main/java/org/thoughtcrime/securesms/wallpaper/UriChatWallpaper.java index 751c57ccef..231fb95b92 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/wallpaper/UriChatWallpaper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/wallpaper/UriChatWallpaper.java @@ -13,6 +13,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.target.Target; @@ -64,14 +65,16 @@ final class UriChatWallpaper implements ChatWallpaper, Parcelable { @Override public void loadInto(@NonNull ImageView imageView) { Bitmap cached = CACHE.get(uri); - if (cached != null) { + if (cached != null && !cached.isRecycled()) { Log.d(TAG, "Using cached value."); - imageView.setImageBitmap(CACHE.get(uri)); + imageView.setImageBitmap(cached); } else { - Log.d(TAG, "Not in cache. Fetching using Glide."); - GlideApp.with(imageView) + Log.d(TAG, "Not in cache or recycled. Fetching using Glide."); + GlideApp.with(imageView.getContext().getApplicationContext()) .asBitmap() .load(new DecryptableStreamUriLoader.DecryptableUri(uri)) + .skipMemoryCache(true) + .diskCacheStrategy(DiskCacheStrategy.NONE) .addListener(new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -94,16 +97,18 @@ final class UriChatWallpaper implements ChatWallpaper, Parcelable { @Override public boolean prefetch(@NonNull Context context, long maxWaitTime) { Bitmap cached = CACHE.get(uri); - if (cached != null) { + if (cached != null && !cached.isRecycled()) { Log.d(TAG, "Already cached, skipping prefetch."); return true; } long startTime = System.currentTimeMillis(); try { - Bitmap bitmap = GlideApp.with(context) + Bitmap bitmap = GlideApp.with(context.getApplicationContext()) .asBitmap() .load(new DecryptableStreamUriLoader.DecryptableUri(uri)) + .skipMemoryCache(true) + .diskCacheStrategy(DiskCacheStrategy.NONE) .submit() .get(maxWaitTime, TimeUnit.MILLISECONDS);