|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.modules.core |
| 9 | + |
| 10 | +import android.net.Uri |
| 11 | +import com.facebook.proguard.annotations.DoNotStrip |
| 12 | +import com.facebook.react.bridge.Arguments |
| 13 | +import com.facebook.react.bridge.JavaScriptModule |
| 14 | +import com.facebook.react.bridge.ReactApplicationContext |
| 15 | +import com.facebook.react.bridge.UiThreadUtil |
| 16 | +import com.facebook.react.module.annotations.ReactModule |
| 17 | + |
| 18 | +/** Native module that handles device hardware events like hardware back presses. */ |
| 19 | +@ReactModule(name = com.facebook.fbreact.specs.NativeDeviceEventManagerSpec.NAME) |
| 20 | +public class DeviceEventManagerModule( |
| 21 | + reactContext: ReactApplicationContext?, |
| 22 | + backBtnHandler: DefaultHardwareBackBtnHandler |
| 23 | +) : com.facebook.fbreact.specs.NativeDeviceEventManagerSpec(reactContext) { |
| 24 | + @DoNotStrip |
| 25 | + public fun interface RCTDeviceEventEmitter : JavaScriptModule { |
| 26 | + public fun emit(eventName: String, data: Any?) |
| 27 | + } |
| 28 | + |
| 29 | + private val invokeDefaultBackPressRunnable: Runnable = Runnable { |
| 30 | + UiThreadUtil.assertOnUiThread() |
| 31 | + backBtnHandler.invokeDefaultOnBackPressed() |
| 32 | + } |
| 33 | + |
| 34 | + /** Sends an event to the JS instance that the hardware back has been pressed. */ |
| 35 | + public fun emitHardwareBackPressed() { |
| 36 | + val reactApplicationContext: ReactApplicationContext? = |
| 37 | + getReactApplicationContextIfActiveOrWarn() |
| 38 | + reactApplicationContext?.emitDeviceEvent("hardwareBackPress", null) |
| 39 | + } |
| 40 | + |
| 41 | + /** Sends an event to the JS instance that a new intent was received. */ |
| 42 | + public fun emitNewIntentReceived(uri: Uri) { |
| 43 | + val reactApplicationContext: ReactApplicationContext? = |
| 44 | + getReactApplicationContextIfActiveOrWarn() |
| 45 | + val map = Arguments.createMap() |
| 46 | + map.putString("url", uri.toString()) |
| 47 | + reactApplicationContext?.emitDeviceEvent("url", map) |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Invokes the default back handler for the host of this catalyst instance. This should be invoked |
| 52 | + * if JS does not want to handle the back press itself. |
| 53 | + */ |
| 54 | + override fun invokeDefaultBackPressHandler() { |
| 55 | + // There should be no need to check if the catalyst instance is alive. After initialization |
| 56 | + // the thread instances cannot be null, and scheduling on a thread after ReactApplicationContext |
| 57 | + // teardown is a noop. |
| 58 | + getReactApplicationContext().runOnUiQueueThread(invokeDefaultBackPressRunnable) |
| 59 | + } |
| 60 | + |
| 61 | + public companion object { |
| 62 | + public const val NAME: String = com.facebook.fbreact.specs.NativeDeviceEventManagerSpec.NAME |
| 63 | + } |
| 64 | +} |
0 commit comments