From f535c8b4bb4474ffe0a0765270cbca8d839deca8 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Fri, 8 May 2020 16:34:54 -0700 Subject: [PATCH] Inject ImagePipeline into FrescoModule Summary: This diff refactors the FrescoModule in order to receive an ImagePipeline as a parameter. This is necessary to ensure the same ImagePipeline is used by every RN module changelog: [Internal][Android] Reviewed By: JoshuaGross Differential Revision: D21428346 fbshipit-source-id: 70a6cc57c8585fe74b6d0b0d1fd86c539974ec23 --- .../react/modules/fresco/FrescoModule.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java index df26770d309..ac70eff2adc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java @@ -12,6 +12,7 @@ import androidx.annotation.Nullable; import com.facebook.common.logging.FLog; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory; +import com.facebook.imagepipeline.core.ImagePipeline; import com.facebook.imagepipeline.core.ImagePipelineConfig; import com.facebook.imagepipeline.listener.RequestListener; import com.facebook.react.bridge.LifecycleEventListener; @@ -41,6 +42,7 @@ public class FrescoModule extends ReactContextBaseJavaModule public static final String NAME = "FrescoModule"; private final boolean mClearOnDestroy; private @Nullable ImagePipelineConfig mConfig; + private @Nullable ImagePipeline mImagePipeline; private static boolean sHasBeenInitialized = false; @@ -66,6 +68,20 @@ public class FrescoModule extends ReactContextBaseJavaModule this(reactContext, clearOnDestroy, null); } + /** + * Create a new Fresco module with a default configuration (or the previously given configuration + * via {@link #FrescoModule(ReactApplicationContext, boolean, ImagePipelineConfig)}. + * + * @param clearOnDestroy whether to clear the memory cache in onHostDestroy: this should be {@code + * true} for pure RN apps and {@code false} for apps that use Fresco outside of RN as well + * @param reactContext the context to use + */ + public FrescoModule( + ReactApplicationContext reactContext, ImagePipeline imagePipeline, boolean clearOnDestroy) { + this(reactContext, clearOnDestroy); + mImagePipeline = imagePipeline; + } + /** * Create a new Fresco module with a given ImagePipelineConfig. This should only be called when * the module has not been initialized yet. You can use {@link #hasBeenInitialized()} to check @@ -114,7 +130,7 @@ public class FrescoModule extends ReactContextBaseJavaModule @Override public void clearSensitiveData() { // Clear image cache. - Fresco.getImagePipeline().clearCaches(); + getImagePipeline().clearCaches(); } /** @@ -168,7 +184,14 @@ public class FrescoModule extends ReactContextBaseJavaModule // the 'last' ReactActivity is being destroyed, which effectively means the app is being // backgrounded. if (hasBeenInitialized() && mClearOnDestroy) { - Fresco.getImagePipeline().clearMemoryCaches(); + getImagePipeline().clearMemoryCaches(); } } + + private ImagePipeline getImagePipeline() { + if (mImagePipeline == null) { + mImagePipeline = Fresco.getImagePipeline(); + } + return mImagePipeline; + } }