Skip to content

Commit e17e3e3

Browse files
joevilchesfacebook-github-bot
authored andcommitted
Decouple ReactAndroidHWInputDeviceHelper from ReactRootView (#52891)
Summary: Pull Request resolved: #52891 Right now these are tightly coupled for no reason. The device helper just asks the root view to send the event out, which it uses ReactContext for. We can just have that be passed in and accomplish the same task. Changelog: [Internal] Reviewed By: Abbondanzo, rozele Differential Revision: D79007328 fbshipit-source-id: ef0a5ac4ec0acb52fc7c2a26010811767e3c1e67
1 parent b4a10f0 commit e17e3e3

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,54 @@ package com.facebook.react
99

1010
import android.view.KeyEvent
1111
import android.view.View
12+
import com.facebook.react.bridge.ReactContext
1213
import com.facebook.react.bridge.WritableMap
1314
import com.facebook.react.bridge.WritableNativeMap
1415

1516
/** Responsible for dispatching events specific for hardware inputs. */
16-
internal class ReactAndroidHWInputDeviceHelper(private val reactRootView: ReactRootView) {
17+
internal class ReactAndroidHWInputDeviceHelper() {
1718
/**
1819
* We keep a reference to the last focused view id so that we can send it as a target for key
1920
* events and be able to send a blur event when focus changes.
2021
*/
2122
private var lastFocusedViewId = View.NO_ID
2223

2324
/** Called from [ReactRootView]. This is the main place the key events are handled. */
24-
fun handleKeyEvent(ev: KeyEvent) {
25+
fun handleKeyEvent(ev: KeyEvent, context: ReactContext) {
2526
val eventKeyCode = ev.keyCode
2627
val eventKeyAction = ev.action
2728
if ((eventKeyAction == KeyEvent.ACTION_UP || eventKeyAction == KeyEvent.ACTION_DOWN) &&
2829
KEY_EVENTS_ACTIONS.containsKey(eventKeyCode)) {
29-
dispatchEvent(KEY_EVENTS_ACTIONS[eventKeyCode], lastFocusedViewId, eventKeyAction)
30+
dispatchEvent(context, KEY_EVENTS_ACTIONS[eventKeyCode], lastFocusedViewId, eventKeyAction)
3031
}
3132
}
3233

3334
/** Called from [ReactRootView] when focused view changes. */
34-
fun onFocusChanged(newFocusedView: View) {
35+
fun onFocusChanged(newFocusedView: View, context: ReactContext) {
3536
if (lastFocusedViewId == newFocusedView.id) {
3637
return
3738
}
3839
if (lastFocusedViewId != View.NO_ID) {
39-
dispatchEvent("blur", lastFocusedViewId)
40+
dispatchEvent(context, "blur", lastFocusedViewId)
4041
}
4142
lastFocusedViewId = newFocusedView.id
42-
dispatchEvent("focus", newFocusedView.id)
43+
dispatchEvent(context, "focus", newFocusedView.id)
4344
}
4445

4546
/** Called from [ReactRootView] when the whole view hierarchy looses focus. */
46-
fun clearFocus() {
47+
fun clearFocus(context: ReactContext) {
4748
if (lastFocusedViewId != View.NO_ID) {
48-
dispatchEvent("blur", lastFocusedViewId)
49+
dispatchEvent(context, "blur", lastFocusedViewId)
4950
}
5051
lastFocusedViewId = View.NO_ID
5152
}
5253

53-
private fun dispatchEvent(eventType: String?, targetViewId: Int, eventKeyAction: Int = -1) {
54+
private fun dispatchEvent(
55+
context: ReactContext,
56+
eventType: String?,
57+
targetViewId: Int,
58+
eventKeyAction: Int = -1,
59+
) {
5460
val event: WritableMap =
5561
WritableNativeMap().apply {
5662
putString("eventType", eventType)
@@ -59,7 +65,7 @@ internal class ReactAndroidHWInputDeviceHelper(private val reactRootView: ReactR
5965
putInt("tag", targetViewId)
6066
}
6167
}
62-
reactRootView.sendEvent("onHWKeyEvent", event)
68+
context.emitDeviceEvent("onHWKeyEvent", event)
6369
}
6470

6571
private companion object {

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public interface ReactRootViewEventListener {
106106
private @Nullable JSTouchDispatcher mJSTouchDispatcher;
107107
private @Nullable JSPointerDispatcher mJSPointerDispatcher;
108108
private final ReactAndroidHWInputDeviceHelper mAndroidHWInputDeviceHelper =
109-
new ReactAndroidHWInputDeviceHelper(this);
109+
new ReactAndroidHWInputDeviceHelper();
110110
private boolean mWasMeasured = false;
111111
private int mWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
112112
private int mHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
@@ -328,7 +328,10 @@ public boolean dispatchKeyEvent(KeyEvent ev) {
328328
FLog.w(TAG, "Unable to handle key event as the catalyst instance has not been attached");
329329
return super.dispatchKeyEvent(ev);
330330
}
331-
mAndroidHWInputDeviceHelper.handleKeyEvent(ev);
331+
ReactContext context = getCurrentReactContext();
332+
if (context != null) {
333+
mAndroidHWInputDeviceHelper.handleKeyEvent(ev, context);
334+
}
332335
return super.dispatchKeyEvent(ev);
333336
}
334337

@@ -341,7 +344,10 @@ protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyF
341344
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
342345
return;
343346
}
344-
mAndroidHWInputDeviceHelper.clearFocus();
347+
ReactContext context = getCurrentReactContext();
348+
if (context != null) {
349+
mAndroidHWInputDeviceHelper.clearFocus(context);
350+
}
345351
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
346352
}
347353

@@ -355,7 +361,10 @@ public void requestChildFocus(View child, View focused) {
355361
super.requestChildFocus(child, focused);
356362
return;
357363
}
358-
mAndroidHWInputDeviceHelper.onFocusChanged(focused);
364+
ReactContext context = getCurrentReactContext();
365+
if (context != null) {
366+
mAndroidHWInputDeviceHelper.onFocusChanged(focused, context);
367+
}
359368
super.requestChildFocus(child, focused);
360369
}
361370

0 commit comments

Comments
 (0)