Bug 1116349 - Flush Picasso LRU cache on memory pressure events

This commit is contained in:
Melchiorre Alastra 2015-07-22 18:00:07 -07:00
parent 7a6cacb90c
commit 24d6a9318c
2 changed files with 13 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import org.mozilla.gecko.AppConstants.Versions;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.favicons.Favicons;
import org.mozilla.gecko.home.ImageLoader;
import org.mozilla.gecko.util.ThreadUtils;
import android.content.BroadcastReceiver;
@ -179,6 +180,7 @@ class MemoryMonitor extends BroadcastReceiver {
}
Favicons.clearMemCache();
ImageLoader.clearLruCache();
}
return true;
}

View File

@ -10,6 +10,7 @@ import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import com.squareup.picasso.LruCache;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Downloader.Response;
import com.squareup.picasso.UrlConnectionDownloader;
@ -46,11 +47,14 @@ public class ImageLoader {
}
}
// Picasso instance and LruCache lrucache are protected by synchronization.
private static Picasso instance;
private static LruCache lrucache;
public static synchronized Picasso with(Context context) {
if (instance == null) {
Picasso.Builder builder = new Picasso.Builder(context);
lrucache = new LruCache(context);
Picasso.Builder builder = new Picasso.Builder(context).memoryCache(lrucache);
final Distribution distribution = Distribution.getInstance(context);
builder.downloader(new ImageDownloader(context, distribution));
@ -60,6 +64,12 @@ public class ImageLoader {
return instance;
}
public static synchronized void clearLruCache() {
if (lrucache != null) {
lrucache.evictAll();
}
}
/**
* Custom Downloader built on top of Picasso's UrlConnectionDownloader
* that supports loading images from custom URIs.