Browse Source

Utilizing LRUCache for in memory bitmap caching

master
anthony restaino 8 years ago
parent
commit
d833cceb96
  1. 31
      app/src/main/java/acr/browser/lightning/favicon/FaviconModel.java
  2. 2
      app/src/main/java/acr/browser/lightning/favicon/ImageFetcher.java

31
app/src/main/java/acr/browser/lightning/favicon/FaviconModel.java

@ -4,7 +4,9 @@ import android.app.Application;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import android.util.LruCache;
import com.anthonycr.bonsai.Completable; import com.anthonycr.bonsai.Completable;
import com.anthonycr.bonsai.CompletableAction; import com.anthonycr.bonsai.CompletableAction;
@ -20,6 +22,8 @@ import java.io.IOException;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import acr.browser.lightning.utils.FileUtils;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
/** /**
@ -33,6 +37,12 @@ public class FaviconModel {
@NonNull private final ImageFetcher mImageFetcher; @NonNull private final ImageFetcher mImageFetcher;
@NonNull private final Application mApplication; @NonNull private final Application mApplication;
@NonNull private final LruCache<String, Bitmap> mFaviconCache = new LruCache<String, Bitmap>((int) FileUtils.megabytesToBytes(1)) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
@Inject @Inject
public FaviconModel(@NonNull Application application) { public FaviconModel(@NonNull Application application) {
@ -40,6 +50,22 @@ public class FaviconModel {
mApplication = application; mApplication = application;
} }
@Nullable
private Bitmap getFaviconFromMemCache(@NonNull String url) {
Preconditions.checkNonNull(url);
synchronized (mFaviconCache) {
return mFaviconCache.get(url);
}
}
private void addFaviconToMemCache(@NonNull String url, @NonNull Bitmap bitmap) {
Preconditions.checkNonNull(url);
Preconditions.checkNonNull(bitmap);
synchronized (mFaviconCache) {
mFaviconCache.put(url, bitmap);
}
}
@NonNull @NonNull
private static File createFaviconCacheFile(@NonNull Application app, @NonNull Uri uri) { private static File createFaviconCacheFile(@NonNull Application app, @NonNull Uri uri) {
FaviconUtils.assertUriSafe(uri); FaviconUtils.assertUriSafe(uri);
@ -71,9 +97,9 @@ public class FaviconModel {
File faviconCacheFile = createFaviconCacheFile(mApplication, uri); File faviconCacheFile = createFaviconCacheFile(mApplication, uri);
Bitmap favicon = null; Bitmap favicon = getFaviconFromMemCache(url);
if (faviconCacheFile.exists()) { if (faviconCacheFile.exists() && favicon != null) {
favicon = mImageFetcher.retrieveFaviconFromCache(faviconCacheFile); favicon = mImageFetcher.retrieveFaviconFromCache(faviconCacheFile);
} }
@ -93,6 +119,7 @@ public class FaviconModel {
} }
if (favicon != null) { if (favicon != null) {
addFaviconToMemCache(url, favicon);
cacheFaviconForUrl(favicon, url).subscribe(); cacheFaviconForUrl(favicon, url).subscribe();
} }

2
app/src/main/java/acr/browser/lightning/favicon/ImageFetcher.java

@ -73,8 +73,8 @@ class ImageFetcher {
int size = Utils.dpToPx(24); int size = Utils.dpToPx(24);
mLoaderOptions.inJustDecodeBounds = false;
mLoaderOptions.inSampleSize = Utils.calculateInSampleSize(mLoaderOptions, size, size); mLoaderOptions.inSampleSize = Utils.calculateInSampleSize(mLoaderOptions, size, size);
mLoaderOptions.inJustDecodeBounds = false;
Response imageResponse = mHttpClient.newCall(imageRequest).execute(); Response imageResponse = mHttpClient.newCall(imageRequest).execute();
iconStream = imageResponse.body().byteStream(); iconStream = imageResponse.body().byteStream();

Loading…
Cancel
Save