Clear Fresco caches when going low on memory

Reviewed By: astreet

Differential Revision: D2699721

fb-gh-sync-id: 2d4685885ee254546496b0517633e5fe2de840d6
This commit is contained in:
Alexander Blom 2015-11-27 02:05:14 -08:00 committed by facebook-github-bot-7
parent e1adea86c6
commit 699a75b01f
1 changed files with 31 additions and 1 deletions

View File

@ -11,7 +11,9 @@ package com.facebook.react.modules.fresco;
import java.util.HashSet;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.res.Configuration;
import android.support.annotation.Nullable;
import com.facebook.cache.common.CacheKey;
@ -37,7 +39,7 @@ import com.squareup.okhttp.OkHttpClient;
* <p>Does not expose any methods to JavaScript code. For initialization and cleanup only.
*/
public class FrescoModule extends ReactContextBaseJavaModule implements
ModuleDataCleaner.Cleanable {
ModuleDataCleaner.Cleanable, ComponentCallbacks2 {
@Nullable private RequestListener mRequestListener;
@Nullable private DiskCacheConfig mDiskCacheConfig;
@ -88,6 +90,14 @@ public class FrescoModule extends ReactContextBaseJavaModule implements
ImagePipelineConfig config = builder.build();
Fresco.initialize(context, config);
getReactApplicationContext().getApplicationContext().registerComponentCallbacks(this);
}
@Override
public void onCatalystInstanceDestroy() {
getReactApplicationContext().getApplicationContext().unregisterComponentCallbacks(this);
clearMemoryCaches();
}
@Override
@ -105,6 +115,26 @@ public class FrescoModule extends ReactContextBaseJavaModule implements
imagePipelineFactory.getSmallImageDiskStorageCache().clearAll();
}
@Override
public void onTrimMemory(int level) {
if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE ||
level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
clearMemoryCaches();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
}
@Override
public void onLowMemory() {
}
private void clearMemoryCaches() {
Fresco.getImagePipeline().clearMemoryCaches();
}
private static class FrescoHandler implements SoLoaderShim.Handler {
@Override
public void loadLibrary(String libraryName) {