From 37e509f2b695818d19358fcd3eaf8fa0452adce3 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Tue, 31 Oct 2023 11:51:09 -0700 Subject: [PATCH] rn-android | Allow injecting a custom ChoreographerProvider via ReactNativeHost into construction of ReactInstanceManager. Summary: Further propagating extension to the Android choreographer, now allowing to override it from the perspective of ReactNativeHost/ReactInstanceManager(Builder). Changelog: [Android][Added] ReactChoreographer can now use an implementation substitution instead of relying on android.view.Choreographer directly. Reviewed By: javache Differential Revision: D50827973 fbshipit-source-id: 42efaa3ece2c2b45fe4ee04a4bbc87c9d59132c8 --- .../com/facebook/react/ReactInstanceManager.java | 9 +++++++-- .../facebook/react/ReactInstanceManagerBuilder.java | 11 ++++++++++- .../java/com/facebook/react/ReactNativeHost.java | 12 +++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index 419e60486af536..98257a80d8ec1b 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -88,6 +88,7 @@ import com.facebook.react.devsupport.interfaces.PackagerStatusCallback; import com.facebook.react.devsupport.interfaces.RedBoxHandler; import com.facebook.react.internal.AndroidChoreographerProvider; +import com.facebook.react.internal.ChoreographerProvider; import com.facebook.react.internal.turbomodule.core.TurboModuleManager; import com.facebook.react.internal.turbomodule.core.TurboModuleManagerDelegate; import com.facebook.react.modules.appearance.AppearanceModule; @@ -236,7 +237,8 @@ public static ReactInstanceManagerBuilder builder() { @Nullable Map customPackagerCommandHandlers, @Nullable ReactPackageTurboModuleManagerDelegate.Builder tmmDelegateBuilder, @Nullable SurfaceDelegateFactory surfaceDelegateFactory, - @Nullable DevLoadingViewManager devLoadingViewManager) { + @Nullable DevLoadingViewManager devLoadingViewManager, + @Nullable ChoreographerProvider choreographerProvider) { FLog.d(TAG, "ReactInstanceManager.ctor()"); initializeSoLoaderIfNecessary(applicationContext); @@ -294,7 +296,10 @@ public void invokeDefaultOnBackPressed() { mJSIModulePackage = jsiModulePackage; // Instantiate ReactChoreographer in UI thread. - ReactChoreographer.initialize(AndroidChoreographerProvider.getInstance()); + ReactChoreographer.initialize( + choreographerProvider != null + ? choreographerProvider + : AndroidChoreographerProvider.getInstance()); if (mUseDeveloperSupport) { mDevSupportManager.startInspector(); } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java index 3d8eb071cdcba9..99bf611233ee7c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java @@ -32,6 +32,7 @@ import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.devsupport.interfaces.RedBoxHandler; +import com.facebook.react.internal.ChoreographerProvider; import com.facebook.react.jscexecutor.JSCExecutor; import com.facebook.react.jscexecutor.JSCExecutorFactory; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; @@ -72,6 +73,7 @@ public class ReactInstanceManagerBuilder { private @Nullable SurfaceDelegateFactory mSurfaceDelegateFactory; private @Nullable DevLoadingViewManager mDevLoadingViewManager; private @Nullable JSEngineResolutionAlgorithm mJSEngineResolutionAlgorithm = null; + private @Nullable ChoreographerProvider mChoreographerProvider = null; /* package protected */ ReactInstanceManagerBuilder() {} @@ -287,6 +289,12 @@ public ReactInstanceManagerBuilder setReactPackageTurboModuleManagerDelegateBuil return this; } + public ReactInstanceManagerBuilder setChoreographerProvider( + @Nullable ChoreographerProvider choreographerProvider) { + mChoreographerProvider = choreographerProvider; + return this; + } + /** * Instantiates a new {@link ReactInstanceManager}. Before calling {@code build}, the following * must be called: @@ -350,7 +358,8 @@ public ReactInstanceManager build() { mCustomPackagerCommandHandlers, mTMMDelegateBuilder, mSurfaceDelegateFactory, - mDevLoadingViewManager); + mDevLoadingViewManager, + mChoreographerProvider); } private JavaScriptExecutorFactory getDefaultJSExecutorFactory( diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java index 9e5d3f6d2d50bd..e1e0564c10fd62 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java @@ -21,6 +21,7 @@ import com.facebook.react.devsupport.DevSupportManagerFactory; import com.facebook.react.devsupport.interfaces.DevLoadingViewManager; import com.facebook.react.devsupport.interfaces.RedBoxHandler; +import com.facebook.react.internal.ChoreographerProvider; import java.util.List; /** @@ -87,7 +88,8 @@ protected ReactInstanceManager createReactInstanceManager() { .setInitialLifecycleState(LifecycleState.BEFORE_CREATE) .setReactPackageTurboModuleManagerDelegateBuilder( getReactPackageTurboModuleManagerDelegateBuilder()) - .setJSEngineResolutionAlgorithm(getJSEngineResolutionAlgorithm()); + .setJSEngineResolutionAlgorithm(getJSEngineResolutionAlgorithm()) + .setChoreographerProvider(getChoreographerProvider()); for (ReactPackage reactPackage : getPackages()) { builder.addPackage(reactPackage); @@ -213,4 +215,12 @@ protected String getJSMainModuleName() { protected @Nullable JSEngineResolutionAlgorithm getJSEngineResolutionAlgorithm() { return null; } + + /** + * Returns a custom implementation of ChoreographerProvider to be used this host. If null - React + * will use default direct android.view.Choreographer-based provider. + */ + protected @Nullable ChoreographerProvider getChoreographerProvider() { + return null; + } }