From d8e262d4c25ab48c1f3b0b226ebbd581ef8c5198 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 27 Jun 2023 19:30:49 -0500 Subject: [PATCH] No more BitmapCache All drawables forever --- .../persistance/FileBackend.java | 11 +++++----- .../services/XmppConnectionService.java | 21 ++----------------- .../ui/ConversationFragment.java | 1 - 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index a8887082c..ae6a4876b 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -452,11 +452,12 @@ public class FileBackend { public Bitmap getPreviewForUri(Attachment attachment, int size, boolean cacheOnly) { final String key = "attachment_" + attachment.getUuid().toString() + "_" + size; - final LruCache cache = mXmppConnectionService.getBitmapCache(); - Bitmap bitmap = cache.get(key); - if (bitmap != null || cacheOnly) { - return bitmap; + final LruCache cache = mXmppConnectionService.getDrawableCache(); + Drawable drawable = cache.get(key); + if (drawable != null || cacheOnly) { + return drawDrawable(drawable); } + Bitmap bitmap = null; final String mime = attachment.getMime(); if ("application/pdf".equals(mime)) { bitmap = cropCenterSquarePdf(attachment.getUri(), size); @@ -489,7 +490,7 @@ public class FileBackend { } } if (key != null && bitmap != null) { - cache.put(key, bitmap); + cache.put(key, new BitmapDrawable(bitmap)); } return bitmap; } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index e9acea44b..f627c23ee 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -503,7 +503,6 @@ public class XmppConnectionService extends Service { private OpenPgpServiceConnection pgpServiceConnection; private PgpEngine mPgpEngine = null; private WakeLock wakeLock; - private LruCache mBitmapCache; private LruCache mDrawableCache; private final BroadcastReceiver mInternalEventReceiver = new InternalEventReceiver(); private final BroadcastReceiver mInternalScreenEventReceiver = new InternalEventReceiver(); @@ -1265,12 +1264,6 @@ public class XmppConnectionService extends Service { updateMemorizingTrustmanager(); final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); final int cacheSize = maxMemory / 10; - this.mBitmapCache = new LruCache(cacheSize) { - @Override - protected int sizeOf(final String key, final Bitmap bitmap) { - return bitmap.getByteCount() / 1024; - } - }; this.mDrawableCache = new LruCache(cacheSize) { @Override protected int sizeOf(final String key, final Drawable drawable) { @@ -1411,7 +1404,7 @@ public class XmppConnectionService extends Service { super.onTrimMemory(level); if (level >= TRIM_MEMORY_COMPLETE) { Log.d(Config.LOGTAG, "clear cache due to low memory"); - getBitmapCache().evictAll(); + getDrawableCache().evictAll(); } } @@ -2092,7 +2085,7 @@ public class XmppConnectionService extends Service { databaseBackend.readRoster(account.getRoster()); account.initAccountServices(XmppConnectionService.this); //roster needs to be loaded at this stage } - getBitmapCache().evictAll(); + getDrawableCache().evictAll(); loadPhoneContacts(); Log.d(Config.LOGTAG, "restoring messages..."); final long startMessageRestore = SystemClock.elapsedRealtime(); @@ -4635,10 +4628,6 @@ public class XmppConnectionService extends Service { setMemorizingTrustManager(tm); } - public LruCache getBitmapCache() { - return this.mBitmapCache; - } - public LruCache getDrawableCache() { return this.mDrawableCache; } @@ -5178,18 +5167,12 @@ public class XmppConnectionService extends Service { } public void evictPreview(File f) { - if (mBitmapCache.remove(f.getAbsolutePath()) != null) { - Log.d(Config.LOGTAG, "deleted cached preview"); - } if (mDrawableCache.remove(f.getAbsolutePath()) != null) { Log.d(Config.LOGTAG, "deleted cached preview"); } } public void evictPreview(String uuid) { - if (mBitmapCache.remove(uuid) != null) { - Log.d(Config.LOGTAG, "deleted cached preview"); - } if (mDrawableCache.remove(uuid) != null) { Log.d(Config.LOGTAG, "deleted cached preview"); } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 50f001711..a9f0ac86c 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -2155,7 +2155,6 @@ public class ConversationFragment extends XmppFragment } if (writeGranted(grantResults, permissions)) { if (activity != null && activity.xmppConnectionService != null) { - activity.xmppConnectionService.getBitmapCache().evictAll(); activity.xmppConnectionService.getDrawableCache().evictAll(); activity.xmppConnectionService.restartFileObserver(); } -- 2.45.2