diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java index 0ed6f7d20a7..bcfb57b2727 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java @@ -17,7 +17,7 @@ import com.facebook.infer.annotation.Assertions; import com.facebook.react.config.ReactFeatureFlags; import com.facebook.react.devsupport.DoubleTapReloadRecognizer; import com.facebook.react.interfaces.ReactHostInterface; -import com.facebook.react.interfaces.ReactSurfaceInterface; +import com.facebook.react.interfaces.fabric.ReactSurface; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; /** @@ -37,7 +37,7 @@ public class ReactDelegate { @Nullable private ReactNativeHost mReactNativeHost; @Nullable private ReactHostInterface mReactHost; - @Nullable private ReactSurfaceInterface mReactSurface; + @Nullable private ReactSurface mReactSurface; private boolean mFabricEnabled = false; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BridgelessDevSupportManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BridgelessDevSupportManager.java index 70aeee79789..7d1a60ecb94 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BridgelessDevSupportManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BridgelessDevSupportManager.java @@ -134,7 +134,9 @@ class BridgelessDevSupportManager extends DevSupportManagerBase { public View createRootView(String appKey) { Activity currentActivity = getCurrentActivity(); if (currentActivity != null && !reactHost.isSurfaceWithModuleNameAttached(appKey)) { - ReactSurface reactSurface = ReactSurface.createWithView(currentActivity, appKey, null); + ReactSurfaceImpl reactSurface = + ReactSurfaceImpl.createWithView(currentActivity, appKey, null); + reactSurface.attach(reactHost); reactSurface.start(); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHost.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHost.java index b183c77d52f..08db446c7d3 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHost.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHost.java @@ -51,9 +51,9 @@ import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.fabric.ComponentFactory; import com.facebook.react.fabric.FabricUIManager; import com.facebook.react.interfaces.ReactHostInterface; -import com.facebook.react.interfaces.ReactSurfaceInterface; import com.facebook.react.interfaces.TaskInterface; import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler; +import com.facebook.react.interfaces.fabric.ReactSurface; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.uimanager.UIManagerModule; @@ -364,9 +364,9 @@ public class ReactHost implements ReactHostInterface { } @Override - public ReactSurfaceInterface createSurface( + public ReactSurface createSurface( Context context, String moduleName, @Nullable Bundle initialProps) { - ReactSurface surface = new ReactSurface(context, moduleName, initialProps); + ReactSurfaceImpl surface = new ReactSurfaceImpl(context, moduleName, initialProps); surface.attachView(new ReactSurfaceView(context, surface)); surface.attach(this); return surface; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactInstance.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactInstance.java index 3e211192d31..45541c602e6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactInstance.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactInstance.java @@ -45,6 +45,7 @@ import com.facebook.react.fabric.FabricUIManager; import com.facebook.react.fabric.ReactNativeConfig; import com.facebook.react.fabric.events.EventBeatManager; import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler; +import com.facebook.react.interfaces.fabric.ReactSurface; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.modules.core.JavaTimerManager; import com.facebook.react.modules.core.ReactChoreographer; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurface.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurfaceImpl.java similarity index 93% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurface.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurfaceImpl.java index 22e33c9aa1e..59b5fc4acfc 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurface.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurfaceImpl.java @@ -23,8 +23,8 @@ import com.facebook.react.bridge.WritableNativeMap; import com.facebook.react.bridgeless.internal.bolts.Task; import com.facebook.react.common.annotations.VisibleForTesting; import com.facebook.react.fabric.SurfaceHandlerBinding; -import com.facebook.react.interfaces.ReactSurfaceInterface; import com.facebook.react.interfaces.TaskInterface; +import com.facebook.react.interfaces.fabric.ReactSurface; import com.facebook.react.interfaces.fabric.SurfaceHandler; import com.facebook.react.modules.i18nmanager.I18nUtil; import com.facebook.react.uimanager.events.EventDispatcher; @@ -34,7 +34,7 @@ import javax.annotation.Nullable; /** A class responsible for creating and rendering a full-screen React surface. */ @Nullsafe(Nullsafe.Mode.LOCAL) @ThreadSafe -public class ReactSurface implements ReactSurfaceInterface { +public class ReactSurfaceImpl implements ReactSurface { private final AtomicReference mSurfaceView = new AtomicReference<>(null); @@ -50,9 +50,9 @@ public class ReactSurface implements ReactSurfaceInterface { */ private Context mContext; - public static ReactSurface createWithView( + public static ReactSurfaceImpl createWithView( Context context, String moduleName, @Nullable Bundle initialProps) { - ReactSurface surface = new ReactSurface(context, moduleName, initialProps); + ReactSurfaceImpl surface = new ReactSurfaceImpl(context, moduleName, initialProps); surface.attachView(new ReactSurfaceView(context, surface)); return surface; } @@ -62,7 +62,7 @@ public class ReactSurface implements ReactSurfaceInterface { * @param moduleName The string key used to register this surface in JS with SurfaceRegistry * @param initialProps A Bundle of properties to be passed to the root React component */ - public ReactSurface(Context context, String moduleName, @Nullable Bundle initialProps) { + public ReactSurfaceImpl(Context context, String moduleName, @Nullable Bundle initialProps) { this(new SurfaceHandlerBinding(moduleName), context); NativeMap nativeProps = @@ -83,7 +83,7 @@ public class ReactSurface implements ReactSurfaceInterface { } @VisibleForTesting - ReactSurface(SurfaceHandler surfaceHandler, Context context) { + ReactSurfaceImpl(SurfaceHandler surfaceHandler, Context context) { mSurfaceHandler = surfaceHandler; mContext = context; } @@ -128,6 +128,7 @@ public class ReactSurface implements ReactSurfaceInterface { mReactHost.set(null); } + @Override public SurfaceHandler getSurfaceHandler() { return mSurfaceHandler; } @@ -176,14 +177,17 @@ public class ReactSurface implements ReactSurfaceInterface { return host.stopSurface(this); } + @Override public int getSurfaceID() { return mSurfaceHandler.getSurfaceId(); } + @Override public String getModuleName() { return mSurfaceHandler.getModuleName(); } + @Override public void clear() { UiThreadUtil.runOnUiThread( () -> { @@ -222,10 +226,12 @@ public class ReactSurface implements ReactSurfaceInterface { return mReactHost.get() != null; } + @Override public boolean isRunning() { return mSurfaceHandler.isRunning(); } + @Override public Context getContext() { return mContext; } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurfaceView.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurfaceView.java index a379b16f736..fba837f531b 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurfaceView.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactSurfaceView.java @@ -33,7 +33,7 @@ public class ReactSurfaceView extends ReactRootView { private static final String TAG = "ReactSurfaceView"; - private final ReactSurface mSurface; + private final ReactSurfaceImpl mSurface; private final JSTouchDispatcher mJSTouchDispatcher; private @Nullable JSPointerDispatcher mJSPointerDispatcher; @@ -42,7 +42,7 @@ public class ReactSurfaceView extends ReactRootView { private int mWidthMeasureSpec = 0; private int mHeightMeasureSpec = 0; - public ReactSurfaceView(Context context, ReactSurface surface) { + public ReactSurfaceView(Context context, ReactSurfaceImpl surface) { super(context); mSurface = surface; mJSTouchDispatcher = new JSTouchDispatcher(this); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/ReactHostInterface.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/ReactHostInterface.kt index b2ca693a498..df4cce1c025 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/ReactHostInterface.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/ReactHostInterface.kt @@ -15,6 +15,7 @@ import com.facebook.react.bridge.queue.ReactQueueConfiguration import com.facebook.react.common.LifecycleState import com.facebook.react.common.annotations.UnstableReactNativeAPI import com.facebook.react.devsupport.interfaces.DevSupportManager +import com.facebook.react.interfaces.fabric.ReactSurface import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler /** @@ -67,11 +68,7 @@ interface ReactHostInterface { fun onHostDestroy(activity: Activity?) /** To be called to create and setup an ReactSurface. */ - fun createSurface( - context: Context, - moduleName: String, - initialProps: Bundle? - ): ReactSurfaceInterface? + fun createSurface(context: Context, moduleName: String, initialProps: Bundle?): ReactSurface? /** * This function can be used to initialize the ReactInstance in a background thread before a diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/ReactSurfaceInterface.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/ReactSurfaceInterface.kt deleted file mode 100644 index d3fb4b7d946..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/ReactSurfaceInterface.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.interfaces - -import android.view.ViewGroup - -/** Represents a Surface in React Native. */ -interface ReactSurfaceInterface { - // the API of this interface will be completed as we analyze and refactor API of ReactSurface, - // ReactRootView, etc. - - // Prerender this surface - fun prerender(): TaskInterface - - // Start running this surface - fun start(): TaskInterface - - // Stop running this surface - fun stop(): TaskInterface - - // Get React root view of this surface - fun getView(): ViewGroup? -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/fabric/ReactSurface.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/fabric/ReactSurface.kt new file mode 100644 index 00000000000..402d0f2571c --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/interfaces/fabric/ReactSurface.kt @@ -0,0 +1,49 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.interfaces.fabric + +import android.content.Context +import android.view.ViewGroup +import com.facebook.react.interfaces.TaskInterface + +/** Represents a Surface in React Native. */ +interface ReactSurface { + + // the API of this interface will be completed as we analyze and refactor API of ReactSurface, + // ReactRootView, etc. + + // Returns surface ID of this surface + val surfaceID: Int + + // Returns module name of this surface + val moduleName: String + + // Returns whether the surface is running or not + val isRunning: Boolean + + // Returns surface handler + val surfaceHandler: SurfaceHandler + + // Returns React root view of this surface + val view: ViewGroup? + + // Returns context associated with the surface + val context: Context + + // Prerender this surface + fun prerender(): TaskInterface + + // Start running this surface + fun start(): TaskInterface + + // Stop running this surface + fun stop(): TaskInterface + + // Clear surface + fun clear() +} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactSurfaceTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactSurfaceTest.java index e790558bfda..fdb5c0f56eb 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactSurfaceTest.java +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactSurfaceTest.java @@ -40,7 +40,7 @@ public class ReactSurfaceTest { private ReactHost mReactHost; private Context mContext; - private ReactSurface mReactSurface; + private ReactSurfaceImpl mReactSurface; private TestSurfaceHandler mSurfaceHandler; @Before @@ -50,13 +50,13 @@ public class ReactSurfaceTest { mContext = Robolectric.buildActivity(Activity.class).create().get(); mReactHost = spy(new ReactHost(mContext, mReactHostDelegate, null, false, null, false)); - doAnswer(mockedStartSurface()).when(mReactHost).startSurface(any(ReactSurface.class)); - doAnswer(mockedStartSurface()).when(mReactHost).prerenderSurface(any(ReactSurface.class)); - doAnswer(mockedStopSurface()).when(mReactHost).stopSurface(any(ReactSurface.class)); + doAnswer(mockedStartSurface()).when(mReactHost).startSurface(any(ReactSurfaceImpl.class)); + doAnswer(mockedStartSurface()).when(mReactHost).prerenderSurface(any(ReactSurfaceImpl.class)); + doAnswer(mockedStopSurface()).when(mReactHost).stopSurface(any(ReactSurfaceImpl.class)); doReturn(mEventDispatcher).when(mReactHost).getEventDispatcher(); mSurfaceHandler = new TestSurfaceHandler(); - mReactSurface = new ReactSurface(mSurfaceHandler, mContext); + mReactSurface = new ReactSurfaceImpl(mSurfaceHandler, mContext); mReactSurface.attachView(new ReactSurfaceView(mContext, mReactSurface)); }