Migrate to Kotlin - BridgeDevSupportManager (#50518)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/50518

This diff migrates the following file to Kotlin - BridgeDevSupportManager
as part of our ongoing effort of migrating the codebase to Kotlin

Changelog:
[Internal] [Changed] - BridgeDevSupportManager to Kotlin

Reviewed By: arushikesarwani94

Differential Revision: D72558373

fbshipit-source-id: 07a16dc479e967677b1aab8a6d4a4366994fd490
This commit is contained in:
Nicola Corti
2025-04-08 01:25:16 -07:00
committed by Facebook GitHub Bot
parent 7aebb3a1be
commit c3c7f9300f
2 changed files with 119 additions and 138 deletions
@@ -1,138 +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.devsupport;
import android.content.Context;
import androidx.annotation.Nullable;
import com.facebook.debug.holder.PrinterHolder;
import com.facebook.debug.tags.ReactDebugOverlayTags;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMarkerConstants;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.SurfaceDelegateFactory;
import com.facebook.react.common.annotations.internal.LegacyArchitecture;
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel;
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger;
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager;
import com.facebook.react.devsupport.interfaces.DevSplitBundleCallback;
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager;
import com.facebook.react.devsupport.interfaces.RedBoxHandler;
import com.facebook.react.packagerconnection.RequestHandler;
import java.util.Map;
/**
* Interface for accessing and interacting with development features. Following features
* are supported through this manager class:
* 1) Displaying JS errors (aka RedBox)
* 2) Displaying developers menu (Reload JS, Debug JS)
* 3) Communication with developer server in order to download updated JS bundle
* 4) Starting/stopping broadcast receiver for js reload signals
* 5) Starting/stopping motion sensor listener that recognize shake gestures which in turn may
* trigger developers menu.
* 6) Launching developers settings view
*
* This class automatically monitors the state of registered views and activities to which they are
* bound to make sure that we don't display overlay or that we we don't listen for sensor events
* when app is backgrounded.
*
* {@link com.facebook.react.ReactInstanceManager} implementation is responsible for instantiating
* this class as well as for populating with a reference to {@link CatalystInstance} whenever
* instance manager recreates it (through {@link #onNewReactContextCreated). Also, instance manager
* is responsible for enabling/disabling dev support in case when app is backgrounded or when all
* the views has been detached from the instance (through {@link #setDevSupportEnabled} method).
*/
@LegacyArchitecture
public final class BridgeDevSupportManager extends DevSupportManagerBase {
static {
LegacyArchitectureLogger.assertLegacyArchitecture(
"BridgeDevSupportManager", LegacyArchitectureLogLevel.WARNING);
}
private boolean mIsSamplingProfilerEnabled = false;
public BridgeDevSupportManager(
Context applicationContext,
ReactInstanceDevHelper reactInstanceManagerHelper,
@Nullable String packagerPathForJSBundleName,
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers,
@Nullable SurfaceDelegateFactory surfaceDelegateFactory,
@Nullable DevLoadingViewManager devLoadingViewManager,
@Nullable PausedInDebuggerOverlayManager pausedInDebuggerOverlayManager) {
super(
applicationContext,
reactInstanceManagerHelper,
packagerPathForJSBundleName,
enableOnCreate,
redBoxHandler,
devBundleDownloadListener,
minNumShakes,
customPackagerCommandHandlers,
surfaceDelegateFactory,
devLoadingViewManager,
pausedInDebuggerOverlayManager);
}
@Override
protected String getUniqueTag() {
return "Bridge";
}
@Override
public void loadSplitBundleFromServer(
final String bundlePath, final DevSplitBundleCallback callback) {
fetchSplitBundleAndCreateBundleLoader(
bundlePath,
new CallbackWithBundleLoader() {
@Override
public void onSuccess(JSBundleLoader bundleLoader) {
bundleLoader.loadScript(getCurrentReactContext().getCatalystInstance());
getCurrentReactContext()
.getJSModule(HMRClient.class)
.registerBundle(getDevServerHelper().getDevServerSplitBundleURL(bundlePath));
callback.onSuccess();
}
@Override
public void onError(String url, Throwable cause) {
callback.onError(url, cause);
}
});
}
@Override
public void handleReloadJS() {
UiThreadUtil.assertOnUiThread();
ReactMarker.logMarker(
ReactMarkerConstants.RELOAD,
getDevSettings().getPackagerConnectionSettings().getDebugServerHost());
// dismiss redbox if exists
hideRedboxDialog();
PrinterHolder.getPrinter()
.logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: load from Server");
String bundleURL =
getDevServerHelper().getDevServerBundleURL(Assertions.assertNotNull(getJSAppBundleName()));
reloadJSFromServer(
bundleURL,
() ->
UiThreadUtil.runOnUiThread(
() -> getReactInstanceDevHelper().onJSBundleLoadedFromServer()));
}
}
@@ -0,0 +1,119 @@
/*
* 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.devsupport
import android.content.Context
import com.facebook.debug.holder.PrinterHolder.printer
import com.facebook.debug.tags.ReactDebugOverlayTags
import com.facebook.infer.annotation.Assertions
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.bridge.ReactMarker
import com.facebook.react.bridge.ReactMarkerConstants
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.common.SurfaceDelegateFactory
import com.facebook.react.common.annotations.internal.LegacyArchitecture
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger.assertLegacyArchitecture
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager
import com.facebook.react.devsupport.interfaces.DevSplitBundleCallback
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager
import com.facebook.react.devsupport.interfaces.RedBoxHandler
import com.facebook.react.packagerconnection.RequestHandler
/**
* Interface for accessing and interacting with development features. Following features are
* supported through this manager class:
* 1) Displaying JS errors (aka RedBox)
* 2) Displaying developers menu (Reload JS, Debug JS)
* 3) Communication with developer server in order to download updated JS bundle
* 4) Starting/stopping broadcast receiver for js reload signals
* 5) Starting/stopping motion sensor listener that recognize shake gestures which in turn may
* trigger developers menu.
* 6) Launching developers settings view
*
* This class automatically monitors the state of registered views and activities to which they are
* bound to make sure that we don't display overlay or that we we don't listen for sensor events
* when app is backgrounded.
*
* [com.facebook.react.ReactInstanceManager] implementation is responsible for instantiating this
* class as well as for populating with a reference to [com.facebook.react.bridge.CatalystInstance]
* whenever instance manager recreates it (through [onNewReactContextCreated]). Also, instance
* manager is responsible for enabling/disabling dev support in case when app is backgrounded or
* when all the views has been detached from the instance (through `setDevSupportEnabled` method).
*/
@LegacyArchitecture
public class BridgeDevSupportManager(
applicationContext: Context,
reactInstanceManagerHelper: ReactInstanceDevHelper,
packagerPathForJSBundleName: String?,
enableOnCreate: Boolean,
redBoxHandler: RedBoxHandler?,
devBundleDownloadListener: DevBundleDownloadListener?,
minNumShakes: Int,
customPackagerCommandHandlers: Map<String?, RequestHandler?>?,
surfaceDelegateFactory: SurfaceDelegateFactory?,
devLoadingViewManager: DevLoadingViewManager?,
pausedInDebuggerOverlayManager: PausedInDebuggerOverlayManager?
) :
DevSupportManagerBase(
applicationContext,
reactInstanceManagerHelper,
packagerPathForJSBundleName,
enableOnCreate,
redBoxHandler,
devBundleDownloadListener,
minNumShakes,
customPackagerCommandHandlers,
surfaceDelegateFactory,
devLoadingViewManager,
pausedInDebuggerOverlayManager) {
override fun getUniqueTag(): String = "Bridge"
override fun loadSplitBundleFromServer(bundlePath: String, callback: DevSplitBundleCallback) {
fetchSplitBundleAndCreateBundleLoader(
bundlePath,
object : CallbackWithBundleLoader {
override fun onSuccess(bundleLoader: JSBundleLoader) {
val context =
requireNotNull(currentReactContext) {
"Failed to load split bundle from server due to DevSupportManager.currentReactContext being null"
}
bundleLoader.loadScript(context.catalystInstance)
context
.getJSModule(HMRClient::class.java)
.registerBundle(devServerHelper.getDevServerSplitBundleURL(bundlePath))
callback.onSuccess()
}
override fun onError(url: String, cause: Throwable) = callback.onError(url, cause)
})
}
override fun handleReloadJS() {
UiThreadUtil.assertOnUiThread()
ReactMarker.logMarker(
ReactMarkerConstants.RELOAD, devSettings.packagerConnectionSettings.debugServerHost)
// dismiss redbox if exists
hideRedboxDialog()
printer.logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: load from Server")
val bundleURL = devServerHelper.getDevServerBundleURL(Assertions.assertNotNull(jsAppBundleName))
reloadJSFromServer(bundleURL) {
UiThreadUtil.runOnUiThread { reactInstanceDevHelper.onJSBundleLoadedFromServer() }
}
}
private companion object {
init {
assertLegacyArchitecture("BridgeDevSupportManager", LegacyArchitectureLogLevel.WARNING)
}
}
}