~singpolyma/cheogram-android

d8e262d4c25ab48c1f3b0b226ebbd581ef8c5198 — Stephen Paul Weber 10 months ago 85df7b7
No more BitmapCache

All drawables forever
M src/main/java/eu/siacs/conversations/persistance/FileBackend.java => src/main/java/eu/siacs/conversations/persistance/FileBackend.java +6 -5
@@ 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<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
        Bitmap bitmap = cache.get(key);
        if (bitmap != null || cacheOnly) {
            return bitmap;
        final LruCache<String, Drawable> 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;
    }

M src/main/java/eu/siacs/conversations/services/XmppConnectionService.java => src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +2 -19
@@ 503,7 503,6 @@ public class XmppConnectionService extends Service {
    private OpenPgpServiceConnection pgpServiceConnection;
    private PgpEngine mPgpEngine = null;
    private WakeLock wakeLock;
    private LruCache<String, Bitmap> mBitmapCache;
    private LruCache<String, Drawable> 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<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(final String key, final Bitmap bitmap) {
                return bitmap.getByteCount() / 1024;
            }
        };
        this.mDrawableCache = new LruCache<String, Drawable>(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<String, Bitmap> getBitmapCache() {
        return this.mBitmapCache;
    }

    public LruCache<String, Drawable> 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");
        }

M src/main/java/eu/siacs/conversations/ui/ConversationFragment.java => src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +0 -1
@@ 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();
            }