From fc285f2f0650ec1977a4a2168968649ea7313d72 Mon Sep 17 00:00:00 2001 From: Emily Janzer Date: Wed, 15 Jul 2020 19:32:43 -0700 Subject: [PATCH] Add support for bridgeless mode to UIManagerHelper.getUIManager() Summary: `UIManagerHelper.getUIManager()` relies on the bridge (CatalystInstance) to get the proper UIManager depending on which renderer is being used. Unfortunately, this means it will always return null in bridgeless mode, where the CatalystInstance doesn't exist. This diff replaces the implementation of `BridgelessReactContext.getJSIModule()` to return the FabricUIManager from the ReactHost/Instance. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D22480968 fbshipit-source-id: 640e3f22a5b39b315ed2f0397be3cba39e80529a --- .../com/facebook/react/bridge/ReactContext.java | 2 +- .../react/uimanager/ThemedReactContext.java | 10 ++++++++++ .../react/uimanager/UIManagerHelper.java | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java index c9b4f15c835..7db7e1c16fe 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java @@ -445,7 +445,7 @@ public class ReactContext extends ContextWrapper { return mCatalystInstance.getJavaScriptContextHolder(); } - public JSIModule getJSIModule(JSIModuleType moduleType) { + public @Nullable JSIModule getJSIModule(JSIModuleType moduleType) { if (!hasActiveCatalystInstance()) { throw new IllegalStateException( "Unable to retrieve a JSIModule if CatalystInstance is not active."); diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java index 039798dcaf3..834208a962e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java @@ -10,6 +10,8 @@ package com.facebook.react.uimanager; import android.app.Activity; import android.content.Context; import androidx.annotation.Nullable; +import com.facebook.react.bridge.JSIModule; +import com.facebook.react.bridge.JSIModuleType; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; @@ -77,4 +79,12 @@ public class ThemedReactContext extends ReactContext { public boolean isBridgeless() { return mReactApplicationContext.isBridgeless(); } + + @Override + public JSIModule getJSIModule(JSIModuleType moduleType) { + if (isBridgeless()) { + return mReactApplicationContext.getJSIModule(moduleType); + } + return super.getJSIModule(moduleType); + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java index 30133a53888..954dfe95edf 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java @@ -51,6 +51,22 @@ public class UIManagerHelper { ReactContext context, @UIManagerType int uiManagerType, boolean returnNullIfCatalystIsInactive) { + if (context.isBridgeless()) { + @Nullable + UIManager uiManager = + context.getJSIModule(JSIModuleType.UIManager) != null + ? (UIManager) context.getJSIModule(JSIModuleType.UIManager) + : null; + if (uiManager == null) { + ReactSoftException.logSoftException( + "UIManagerHelper", + new ReactNoCrashSoftException( + "Cannot get UIManager because the instance hasn't been initialized yet.")); + return null; + } + return uiManager; + } + if (!context.hasCatalystInstance()) { ReactSoftException.logSoftException( "UIManagerHelper",