Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Aug 11, 2024
1 parent 85840b7 commit 7e7a198
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/src/main/java/org/nuclearfog/apollo/cache/ImageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public final class ImageCache implements ComponentCallbacks2 {
/**
* LRU cache
*/
@Nullable
private MemoryCache mLruCache;
/**
* Disk LRU cache
Expand Down Expand Up @@ -205,7 +206,9 @@ public void onTrimMemory(int level) {
if (level >= TRIM_MEMORY_MODERATE) {
evictAll();
} else if (level >= TRIM_MEMORY_BACKGROUND) {
mLruCache.trimToSize(mLruCache.size() / 2);
if (mLruCache != null) {
mLruCache.trimToSize(mLruCache.size() / 2);
}
}
}

Expand Down Expand Up @@ -284,7 +287,12 @@ private synchronized void initDiskCache(File cacheFolder) {
*/
public void initLruCache(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int lruCacheSize = Math.round(MEM_CACHE_DIVIDER * activityManager.getMemoryClass() * 1024 * 1024);
int lruCacheSize;
if (activityManager != null) {
lruCacheSize = Math.round(MEM_CACHE_DIVIDER * activityManager.getMemoryClass() * 1024 * 1024);
} else {
lruCacheSize = 16000000;
}
mLruCache = new MemoryCache(lruCacheSize);
// Release some memory as needed
context.registerComponentCallbacks(this);
Expand Down Expand Up @@ -358,7 +366,7 @@ public void addBitmapToMemCache(String data, Bitmap bitmap) {
return;
}
// Add to memory cache
if (getBitmapFromMemCache(data) == null) {
if (mLruCache != null && getBitmapFromMemCache(data) == null) {
mLruCache.put(data, bitmap);
}
}
Expand Down Expand Up @@ -552,8 +560,7 @@ public void run() {
}

/**
* Evicts all of the items from the memory cache and lets the system know
* now would be a good time to garbage collect
* Evicts all of the items from the memory cache
*/
public void evictAll() {
Log.d(TAG, "cleaning image cache");
Expand All @@ -573,7 +580,6 @@ public void removeFromCache(String key) {
if (mLruCache != null) {
mLruCache.remove(key);
}

try {
// Remove the disk entry
if (mDiskCache != null) {
Expand Down

0 comments on commit 7e7a198

Please sign in to comment.