diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 7f5260e97f0..d109ddb32ab 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3345,12 +3345,12 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React public fun start ()Lcom/facebook/react/interfaces/TaskInterface; } -public class com/facebook/react/runtime/ReactSurfaceImpl : com/facebook/react/interfaces/fabric/ReactSurface { +public final class com/facebook/react/runtime/ReactSurfaceImpl : com/facebook/react/interfaces/fabric/ReactSurface { public fun (Landroid/content/Context;Ljava/lang/String;Landroid/os/Bundle;)V - public fun attach (Lcom/facebook/react/ReactHost;)V - public fun attachView (Lcom/facebook/react/runtime/ReactSurfaceView;)V + public final fun attach (Lcom/facebook/react/ReactHost;)V + public final fun attachView (Lcom/facebook/react/runtime/ReactSurfaceView;)V public fun clear ()V - public static fun createWithView (Landroid/content/Context;Ljava/lang/String;Landroid/os/Bundle;)Lcom/facebook/react/runtime/ReactSurfaceImpl; + public static final fun createWithView (Landroid/content/Context;Ljava/lang/String;Landroid/os/Bundle;)Lcom/facebook/react/runtime/ReactSurfaceImpl; public fun detach ()V public fun getContext ()Landroid/content/Context; public fun getModuleName ()Ljava/lang/String; @@ -3360,7 +3360,7 @@ public class com/facebook/react/runtime/ReactSurfaceImpl : com/facebook/react/in public fun prerender ()Lcom/facebook/react/interfaces/TaskInterface; public fun start ()Lcom/facebook/react/interfaces/TaskInterface; public fun stop ()Lcom/facebook/react/interfaces/TaskInterface; - public fun updateInitProps (Landroid/os/Bundle;)V + public final fun updateInitProps (Landroid/os/Bundle;)V } public final class com/facebook/react/runtime/ReactSurfaceView : com/facebook/react/ReactRootView { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceImpl.java deleted file mode 100644 index 55a780175e9..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceImpl.java +++ /dev/null @@ -1,251 +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.runtime; - -import android.content.Context; -import android.os.Bundle; -import android.util.DisplayMetrics; -import android.view.View; -import android.view.View.MeasureSpec; -import android.view.ViewGroup; -import androidx.annotation.UiThread; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.infer.annotation.ThreadSafe; -import com.facebook.react.ReactHost; -import com.facebook.react.bridge.Arguments; -import com.facebook.react.bridge.NativeMap; -import com.facebook.react.bridge.UiThreadUtil; -import com.facebook.react.common.annotations.VisibleForTesting; -import com.facebook.react.fabric.SurfaceHandlerBinding; -import com.facebook.react.interfaces.TaskInterface; -import com.facebook.react.interfaces.fabric.ReactSurface; -import com.facebook.react.modules.i18nmanager.I18nUtil; -import com.facebook.react.runtime.internal.bolts.Task; -import com.facebook.react.uimanager.events.EventDispatcher; -import java.util.concurrent.atomic.AtomicReference; -import javax.annotation.Nullable; - -/** A class responsible for creating and rendering a full-screen React surface. */ -@Nullsafe(Nullsafe.Mode.LOCAL) -@ThreadSafe -public class ReactSurfaceImpl implements ReactSurface { - - private final AtomicReference mSurfaceView = new AtomicReference<>(null); - - private final AtomicReference mReactHost = new AtomicReference<>(null); - - private final SurfaceHandlerBinding mSurfaceHandler; - - /** - * Surface can hold two types of context: one used to init the surface without view (e.g. during - * prerendering), and themed view context later, whenever it is available. The assumption is that - * custom theming is applied to both the same way, so changing them SHOULD NOT change the visual - * output. - */ - private Context mContext; - - public static ReactSurfaceImpl createWithView( - Context context, String moduleName, @Nullable Bundle initialProps) { - ReactSurfaceImpl surface = new ReactSurfaceImpl(context, moduleName, initialProps); - surface.attachView(new ReactSurfaceView(context, surface)); - return surface; - } - - /** - * @param context The Android Context to use to render the ReactSurfaceView - * @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 ReactSurfaceImpl(Context context, String moduleName, @Nullable Bundle initialProps) { - this(new SurfaceHandlerBinding(moduleName), context); - - NativeMap nativeProps = - initialProps != null ? (NativeMap) Arguments.fromBundle(initialProps) : null; - mSurfaceHandler.setProps(nativeProps); - - DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); - mSurfaceHandler.setLayoutConstraints( - MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, MeasureSpec.AT_MOST), - MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, MeasureSpec.AT_MOST), - 0, - 0, - doRTLSwap(context), - isRTL(context), - getPixelDensity(context)); - } - - @VisibleForTesting - ReactSurfaceImpl(SurfaceHandlerBinding surfaceHandler, Context context) { - mSurfaceHandler = surfaceHandler; - mContext = context; - } - - /** - * Attach a ReactHost to the ReactSurface. - * - * @param host The ReactHost to attach. - */ - public void attach(ReactHost host) { - if (host instanceof ReactHostImpl && !mReactHost.compareAndSet(null, (ReactHostImpl) host)) { - throw new IllegalStateException("This surface is already attached to a host!"); - } else if (!(host instanceof ReactHostImpl)) { - throw new IllegalArgumentException( - "ReactSurfaceImpl.attach can only attach to ReactHostImpl."); - } - } - - /** - * Attaches a view to the surface. View can be attached only once and should not be detached - * after. - */ - public void attachView(ReactSurfaceView view) { - if (!mSurfaceView.compareAndSet(null, view)) { - throw new IllegalStateException( - "Trying to call ReactSurface.attachView(), but the view is already attached."); - } - // re: thread safety. Context can be changed only once, during view init, so view atomic above - // already guards the context update. - mContext = view.getContext(); - } - - public void updateInitProps(Bundle newProps) { - mSurfaceHandler.setProps((NativeMap) Arguments.fromBundle(newProps)); - } - - @VisibleForTesting - ReactHostImpl getReactHost() { - // NULLSAFE_FIXME[Return Not Nullable] - return mReactHost.get(); - } - - /** Detach the ReactSurface from its ReactHost. */ - @Override - public void detach() { - mReactHost.set(null); - } - - /** package */ - SurfaceHandlerBinding getSurfaceHandler() { - return mSurfaceHandler; - } - - @Override - public @Nullable ViewGroup getView() { - return mSurfaceView.get(); - } - - @Override - public TaskInterface prerender() { - ReactHostImpl host = mReactHost.get(); - if (host == null) { - return Task.forError( - new IllegalStateException( - "Trying to call ReactSurface.prerender(), but no ReactHost is attached.")); - } - return host.prerenderSurface(this); - } - - @Override - public TaskInterface start() { - if (mSurfaceView.get() == null) { - return Task.forError( - new IllegalStateException( - "Trying to call ReactSurface.start(), but view is not created.")); - } - - ReactHostImpl host = mReactHost.get(); - if (host == null) { - return Task.forError( - new IllegalStateException( - "Trying to call ReactSurface.start(), but no ReactHost is attached.")); - } - return host.startSurface(this); - } - - @Override - public TaskInterface stop() { - ReactHostImpl host = mReactHost.get(); - if (host == null) { - return Task.forError( - new IllegalStateException( - "Trying to call ReactSurface.stop(), but no ReactHost is attached.")); - } - return host.stopSurface(this); - } - - @Override - public int getSurfaceID() { - return mSurfaceHandler.getSurfaceId(); - } - - @Override - public String getModuleName() { - return mSurfaceHandler.getModuleName(); - } - - @Override - public void clear() { - UiThreadUtil.runOnUiThread( - () -> { - ReactSurfaceView view = (ReactSurfaceView) getView(); - if (view != null) { - view.removeAllViews(); - view.setId(View.NO_ID); - } - }); - } - - @UiThread - /* package */ synchronized void updateLayoutSpecs( - int widthMeasureSpec, int heightMeasureSpec, int offsetX, int offsetY) { - mSurfaceHandler.setLayoutConstraints( - widthMeasureSpec, - heightMeasureSpec, - offsetX, - offsetY, - doRTLSwap(mContext), - isRTL(mContext), - getPixelDensity(mContext)); - } - - /* package */ @Nullable - EventDispatcher getEventDispatcher() { - ReactHostImpl host = mReactHost.get(); - if (host == null) { - return null; - } - return host.getEventDispatcher(); - } - - @VisibleForTesting - boolean isAttached() { - return mReactHost.get() != null; - } - - @Override - public boolean isRunning() { - return mSurfaceHandler.isRunning(); - } - - @Override - public Context getContext() { - return mContext; - } - - private static float getPixelDensity(Context context) { - return context.getResources().getDisplayMetrics().density; - } - - private static boolean isRTL(Context context) { - return I18nUtil.getInstance().isRTL(context); - } - - private static boolean doRTLSwap(Context context) { - return I18nUtil.getInstance().doLeftAndRightSwapInRTL(context); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceImpl.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceImpl.kt new file mode 100644 index 00000000000..369605c3600 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceImpl.kt @@ -0,0 +1,209 @@ +/* + * 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.runtime + +import android.content.Context +import android.os.Bundle +import android.view.View +import android.view.View.MeasureSpec +import android.view.ViewGroup +import androidx.annotation.UiThread +import com.facebook.infer.annotation.ThreadSafe +import com.facebook.react.ReactHost +import com.facebook.react.bridge.Arguments +import com.facebook.react.bridge.NativeMap +import com.facebook.react.bridge.UiThreadUtil +import com.facebook.react.common.annotations.VisibleForTesting +import com.facebook.react.fabric.SurfaceHandlerBinding +import com.facebook.react.interfaces.TaskInterface +import com.facebook.react.interfaces.fabric.ReactSurface +import com.facebook.react.modules.i18nmanager.I18nUtil +import com.facebook.react.runtime.internal.bolts.Task +import com.facebook.react.uimanager.events.EventDispatcher +import java.util.concurrent.atomic.AtomicReference + +/** A class responsible for creating and rendering a full-screen React surface. */ +@ThreadSafe +public class ReactSurfaceImpl +@VisibleForTesting +internal constructor( + // Prevent mangling of surfaceHandler name for JVM + // Remove when ReactInstance is Kotlin + @get:JvmName("getSurfaceHandler") internal val surfaceHandler: SurfaceHandlerBinding, + context: Context, +) : ReactSurface { + + /** + * Surface can hold two types of context: one used to init the surface without view (e.g. during + * prerendering), and themed view context later, whenever it is available. The assumption is that + * custom theming is applied to both the same way, so changing them SHOULD NOT change the visual + * output. + */ + override var context: Context = context + private set + + private val surfaceViewRef = AtomicReference(null) + + private val reactHostRef = AtomicReference(null) + + internal val reactHost: ReactHostImpl? + get() = reactHostRef.get() + + /** + * @param context The Android Context to use to render the ReactSurfaceView + * @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 constructor( + context: Context, + moduleName: String, + initialProps: Bundle? + ) : this(SurfaceHandlerBinding(moduleName), context) { + val nativeProps = initialProps?.let { Arguments.fromBundle(it) as NativeMap } + surfaceHandler.setProps(nativeProps) + + val displayMetrics = context.resources.displayMetrics + surfaceHandler.setLayoutConstraints( + MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, MeasureSpec.AT_MOST), + MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, MeasureSpec.AT_MOST), + 0, + 0, + doRTLSwap(context), + isRTL(context), + displayMetrics.density) + } + + /** + * Attach a ReactHost to the ReactSurface. + * + * @param host The ReactHost to attach. + */ + public fun attach(host: ReactHost) { + require(host is ReactHostImpl) { "ReactSurfaceImpl.attach can only attach to ReactHostImpl." } + check(reactHostRef.compareAndSet(null, host)) { "This surface is already attached to a host!" } + } + + /** + * Attaches a view to the surface. View can be attached only once and should not be detached + * after. + */ + public fun attachView(view: ReactSurfaceView) { + check(surfaceViewRef.compareAndSet(null, view)) { + "Trying to call ReactSurface.attachView(), but the view is already attached." + } + // re: thread safety. Context can be changed only once, during view init, so view atomic above + // already guards the context update. + context = view.context + } + + public fun updateInitProps(newProps: Bundle) { + surfaceHandler.setProps(Arguments.fromBundle(newProps) as NativeMap) + } + + /** Detach the ReactSurface from its ReactHost. */ + override fun detach() { + reactHostRef.set(null) + } + + public override val view: ViewGroup? + get() = surfaceViewRef.get() + + override fun prerender(): TaskInterface { + val host = + reactHost + ?: return Task.forError( + IllegalStateException( + "Trying to call ReactSurface.prerender(), but no ReactHost is attached.")) + return host.prerenderSurface(this) + } + + override fun start(): TaskInterface { + if (surfaceViewRef.get() == null) { + return Task.forError( + IllegalStateException("Trying to call ReactSurface.start(), but view is not created.")) + } + + val host = + reactHost + ?: return Task.forError( + IllegalStateException( + "Trying to call ReactSurface.start(), but no ReactHost is attached.")) + return host.startSurface(this) + } + + override fun stop(): TaskInterface { + val host = + reactHost + ?: return Task.forError( + IllegalStateException( + "Trying to call ReactSurface.stop(), but no ReactHost is attached.")) + return host.stopSurface(this) + } + + public override val surfaceID: Int + get() = surfaceHandler.surfaceId + + public override val moduleName: String + get() = surfaceHandler.moduleName + + override fun clear() { + UiThreadUtil.runOnUiThread { + val view = view + if (view != null) { + view.removeAllViews() + view.id = View.NO_ID + } + } + } + + @UiThread + @Synchronized + internal fun updateLayoutSpecs( + widthMeasureSpec: Int, + heightMeasureSpec: Int, + offsetX: Int, + offsetY: Int + ) { + surfaceHandler.setLayoutConstraints( + widthMeasureSpec, + heightMeasureSpec, + offsetX, + offsetY, + doRTLSwap(context), + isRTL(context), + context.resources.displayMetrics.density) + } + + internal val eventDispatcher: EventDispatcher? + get() = reactHost?.eventDispatcher + + @get:VisibleForTesting + internal val isAttached: Boolean + get() = reactHost != null + + public override val isRunning: Boolean + get() = surfaceHandler.isRunning + + internal companion object { + @JvmStatic + public fun createWithView( + context: Context, + moduleName: String, + initialProps: Bundle? + ): ReactSurfaceImpl { + val surface = ReactSurfaceImpl(context, moduleName, initialProps) + surface.attachView(ReactSurfaceView(context, surface)) + return surface + } + + private fun isRTL(context: Context): Boolean = I18nUtil.instance.isRTL(context) + + private fun doRTLSwap(context: Context): Boolean = + I18nUtil.instance.doLeftAndRightSwapInRTL(context) + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceView.kt index 185e349ada8..2dc04f8c466 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactSurfaceView.kt @@ -120,10 +120,8 @@ public class ReactSurfaceView(context: Context?, private val surface: ReactSurfa } override fun handleException(t: Throwable) { - val reactHost = surface.reactHost - val errorMessage = Objects.toString(t.message, "") - val e: Exception = IllegalViewOperationException(errorMessage, this, t) - reactHost.handleHostException(e) + val e = IllegalViewOperationException(Objects.toString(t.message, ""), this, t) + (surface.reactHost ?: throw e).handleHostException(e) } override fun setIsFabric(isFabric: Boolean) { @@ -140,7 +138,7 @@ public class ReactSurfaceView(context: Context?, private val surface: ReactSurfa val eventDispatcher = surface.eventDispatcher if (eventDispatcher != null) { jsTouchDispatcher.handleTouchEvent( - event, eventDispatcher, surface.reactHost.currentReactContext) + event, eventDispatcher, surface.reactHost?.currentReactContext) } else { FLog.w( TAG, "Unable to dispatch touch events to JS as the React instance has not been attached") @@ -166,14 +164,14 @@ public class ReactSurfaceView(context: Context?, private val surface: ReactSurfa } override fun hasActiveReactContext(): Boolean = - surface.isAttached && surface.reactHost.currentReactContext != null + surface.isAttached && surface.reactHost?.currentReactContext != null override fun hasActiveReactInstance(): Boolean = - surface.isAttached && surface.reactHost.isInstanceInitialized + surface.isAttached && surface.reactHost?.isInstanceInitialized == true override fun getCurrentReactContext(): ReactContext? = if (surface.isAttached) { - surface.reactHost.currentReactContext + surface.reactHost?.currentReactContext } else null override fun isViewAttachedToReactInstance(): Boolean = surface.isAttached