diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 999220bf697243..372a9168b18e3e 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3140,13 +3140,18 @@ public abstract interface class com/facebook/react/modules/core/DefaultHardwareB public abstract fun invokeDefaultOnBackPressed ()V } -public class com/facebook/react/modules/core/DeviceEventManagerModule : com/facebook/fbreact/specs/NativeDeviceEventManagerSpec { +public final class com/facebook/react/modules/core/DeviceEventManagerModule : com/facebook/fbreact/specs/NativeDeviceEventManagerSpec { + public static final field Companion Lcom/facebook/react/modules/core/DeviceEventManagerModule$Companion; + public static final field NAME Ljava/lang/String; public fun (Lcom/facebook/react/bridge/ReactApplicationContext;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V - public fun emitHardwareBackPressed ()V - public fun emitNewIntentReceived (Landroid/net/Uri;)V + public final fun emitHardwareBackPressed ()V + public final fun emitNewIntentReceived (Landroid/net/Uri;)V public fun invokeDefaultBackPressHandler ()V } +public final class com/facebook/react/modules/core/DeviceEventManagerModule$Companion { +} + public abstract interface class com/facebook/react/modules/core/DeviceEventManagerModule$RCTDeviceEventEmitter : com/facebook/react/bridge/JavaScriptModule { public abstract fun emit (Ljava/lang/String;Ljava/lang/Object;)V } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.java deleted file mode 100644 index e8ba782cb568c5..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.java +++ /dev/null @@ -1,76 +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.modules.core; - -import android.net.Uri; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import com.facebook.fbreact.specs.NativeDeviceEventManagerSpec; -import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.bridge.Arguments; -import com.facebook.react.bridge.JavaScriptModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.UiThreadUtil; -import com.facebook.react.bridge.WritableMap; -import com.facebook.react.module.annotations.ReactModule; - -/** Native module that handles device hardware events like hardware back presses. */ -@ReactModule(name = NativeDeviceEventManagerSpec.NAME) -public class DeviceEventManagerModule extends NativeDeviceEventManagerSpec { - @DoNotStrip - public interface RCTDeviceEventEmitter extends JavaScriptModule { - void emit(@NonNull String eventName, @Nullable Object data); - } - - private final Runnable mInvokeDefaultBackPressRunnable; - - public DeviceEventManagerModule( - ReactApplicationContext reactContext, final DefaultHardwareBackBtnHandler backBtnHandler) { - super(reactContext); - mInvokeDefaultBackPressRunnable = - new Runnable() { - @Override - public void run() { - UiThreadUtil.assertOnUiThread(); - backBtnHandler.invokeDefaultOnBackPressed(); - } - }; - } - - /** Sends an event to the JS instance that the hardware back has been pressed. */ - public void emitHardwareBackPressed() { - ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn(); - - if (reactApplicationContext != null) { - reactApplicationContext.emitDeviceEvent("hardwareBackPress", null); - } - } - - /** Sends an event to the JS instance that a new intent was received. */ - public void emitNewIntentReceived(Uri uri) { - ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn(); - - if (reactApplicationContext != null) { - WritableMap map = Arguments.createMap(); - map.putString("url", uri.toString()); - reactApplicationContext.emitDeviceEvent("url", map); - } - } - - /** - * Invokes the default back handler for the host of this catalyst instance. This should be invoked - * if JS does not want to handle the back press itself. - */ - @Override - public void invokeDefaultBackPressHandler() { - // There should be no need to check if the catalyst instance is alive. After initialization - // the thread instances cannot be null, and scheduling on a thread after ReactApplicationContext - // teardown is a noop. - getReactApplicationContext().runOnUiQueueThread(mInvokeDefaultBackPressRunnable); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt new file mode 100644 index 00000000000000..4667f0ed537a53 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DeviceEventManagerModule.kt @@ -0,0 +1,64 @@ +/* + * 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.modules.core + +import android.net.Uri +import com.facebook.proguard.annotations.DoNotStrip +import com.facebook.react.bridge.Arguments +import com.facebook.react.bridge.JavaScriptModule +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.UiThreadUtil +import com.facebook.react.module.annotations.ReactModule + +/** Native module that handles device hardware events like hardware back presses. */ +@ReactModule(name = com.facebook.fbreact.specs.NativeDeviceEventManagerSpec.NAME) +public class DeviceEventManagerModule( + reactContext: ReactApplicationContext?, + backBtnHandler: DefaultHardwareBackBtnHandler +) : com.facebook.fbreact.specs.NativeDeviceEventManagerSpec(reactContext) { + @DoNotStrip + public fun interface RCTDeviceEventEmitter : JavaScriptModule { + public fun emit(eventName: String, data: Any?) + } + + private val invokeDefaultBackPressRunnable: Runnable = Runnable { + UiThreadUtil.assertOnUiThread() + backBtnHandler.invokeDefaultOnBackPressed() + } + + /** Sends an event to the JS instance that the hardware back has been pressed. */ + public fun emitHardwareBackPressed() { + val reactApplicationContext: ReactApplicationContext? = + getReactApplicationContextIfActiveOrWarn() + reactApplicationContext?.emitDeviceEvent("hardwareBackPress", null) + } + + /** Sends an event to the JS instance that a new intent was received. */ + public fun emitNewIntentReceived(uri: Uri) { + val reactApplicationContext: ReactApplicationContext? = + getReactApplicationContextIfActiveOrWarn() + val map = Arguments.createMap() + map.putString("url", uri.toString()) + reactApplicationContext?.emitDeviceEvent("url", map) + } + + /** + * Invokes the default back handler for the host of this catalyst instance. This should be invoked + * if JS does not want to handle the back press itself. + */ + override fun invokeDefaultBackPressHandler() { + // There should be no need to check if the catalyst instance is alive. After initialization + // the thread instances cannot be null, and scheduling on a thread after ReactApplicationContext + // teardown is a noop. + getReactApplicationContext().runOnUiQueueThread(invokeDefaultBackPressRunnable) + } + + public companion object { + public const val NAME: String = com.facebook.fbreact.specs.NativeDeviceEventManagerSpec.NAME + } +}