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
This commit is contained in:
David Vacca
2020-05-08 16:34:54 -07:00
committed by Facebook GitHub Bot
parent a4a47b9a1e
commit f535c8b4bb
@@ -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;
}
}