Skip to content

Commit 8ae4bbc

Browse files
mateoguzmanafacebook-github-bot
authored andcommitted
Migrate ReactAndroidHWInputDeviceHelper to Kotlin (facebook#50046)
Summary: Migrate com.facebook.react.ReactAndroidHWInputDeviceHelper to Kotlin. ## Changelog: [INTERNAL] - Migrate com.facebook.react.ReactAndroidHWInputDeviceHelper to Kotlin Pull Request resolved: facebook#50046 Test Plan: ```bash yarn test-android yarn android ``` Reviewed By: cortinico Differential Revision: D71307536 Pulled By: javache fbshipit-source-id: 60c458127b7a7e99d9c8bbb4715cab04452984da
1 parent 850760a commit 8ae4bbc

File tree

2 files changed

+88
-100
lines changed

2 files changed

+88
-100
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactAndroidHWInputDeviceHelper.java

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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
9+
10+
import android.view.KeyEvent
11+
import android.view.View
12+
import com.facebook.react.bridge.WritableMap
13+
import com.facebook.react.bridge.WritableNativeMap
14+
15+
/** Responsible for dispatching events specific for hardware inputs. */
16+
internal class ReactAndroidHWInputDeviceHelper(private val reactRootView: ReactRootView) {
17+
/**
18+
* We keep a reference to the last focused view id so that we can send it as a target for key
19+
* events and be able to send a blur event when focus changes.
20+
*/
21+
private var lastFocusedViewId = View.NO_ID
22+
23+
/** Called from [ReactRootView]. This is the main place the key events are handled. */
24+
fun handleKeyEvent(ev: KeyEvent) {
25+
val eventKeyCode = ev.keyCode
26+
val eventKeyAction = ev.action
27+
if ((eventKeyAction == KeyEvent.ACTION_UP || eventKeyAction == KeyEvent.ACTION_DOWN) &&
28+
KEY_EVENTS_ACTIONS.containsKey(eventKeyCode)) {
29+
dispatchEvent(KEY_EVENTS_ACTIONS[eventKeyCode], lastFocusedViewId, eventKeyAction)
30+
}
31+
}
32+
33+
/** Called from [ReactRootView] when focused view changes. */
34+
fun onFocusChanged(newFocusedView: View) {
35+
if (lastFocusedViewId == newFocusedView.id) {
36+
return
37+
}
38+
if (lastFocusedViewId != View.NO_ID) {
39+
dispatchEvent("blur", lastFocusedViewId)
40+
}
41+
lastFocusedViewId = newFocusedView.id
42+
dispatchEvent("focus", newFocusedView.id)
43+
}
44+
45+
/** Called from [ReactRootView] when the whole view hierarchy looses focus. */
46+
fun clearFocus() {
47+
if (lastFocusedViewId != View.NO_ID) {
48+
dispatchEvent("blur", lastFocusedViewId)
49+
}
50+
lastFocusedViewId = View.NO_ID
51+
}
52+
53+
private fun dispatchEvent(eventType: String?, targetViewId: Int, eventKeyAction: Int = -1) {
54+
val event: WritableMap =
55+
WritableNativeMap().apply {
56+
putString("eventType", eventType)
57+
putInt("eventKeyAction", eventKeyAction)
58+
if (targetViewId != View.NO_ID) {
59+
putInt("tag", targetViewId)
60+
}
61+
}
62+
reactRootView.sendEvent("onHWKeyEvent", event)
63+
}
64+
65+
private companion object {
66+
/**
67+
* Contains a mapping between handled KeyEvents and the corresponding navigation event that
68+
* should be fired when the KeyEvent is received.
69+
*/
70+
private val KEY_EVENTS_ACTIONS: Map<Int, String> =
71+
mapOf(
72+
KeyEvent.KEYCODE_DPAD_CENTER to "select",
73+
KeyEvent.KEYCODE_ENTER to "select",
74+
KeyEvent.KEYCODE_SPACE to "select",
75+
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE to "playPause",
76+
KeyEvent.KEYCODE_MEDIA_REWIND to "rewind",
77+
KeyEvent.KEYCODE_MEDIA_FAST_FORWARD to "fastForward",
78+
KeyEvent.KEYCODE_MEDIA_STOP to "stop",
79+
KeyEvent.KEYCODE_MEDIA_NEXT to "next",
80+
KeyEvent.KEYCODE_MEDIA_PREVIOUS to "previous",
81+
KeyEvent.KEYCODE_DPAD_UP to "up",
82+
KeyEvent.KEYCODE_DPAD_RIGHT to "right",
83+
KeyEvent.KEYCODE_DPAD_DOWN to "down",
84+
KeyEvent.KEYCODE_DPAD_LEFT to "left",
85+
KeyEvent.KEYCODE_INFO to "info",
86+
KeyEvent.KEYCODE_MENU to "menu")
87+
}
88+
}

0 commit comments

Comments
 (0)