-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExternalKeyboardViewManager.java
121 lines (96 loc) · 4.43 KB
/
ExternalKeyboardViewManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.externalkeyboard.views.ExternalKeyboardView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import com.externalkeyboard.events.FocusChangeEvent;
import com.externalkeyboard.events.KeyPressDownEvent;
import com.externalkeyboard.events.KeyPressUpEvent;
import com.externalkeyboard.services.KeyboardKeyPressHandler;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.views.view.ReactViewGroup;
import java.util.Map;
@ReactModule(name = ExternalKeyboardViewManager.NAME)
public class ExternalKeyboardViewManager extends com.externalkeyboard.ExternalKeyboardViewManagerSpec<ExternalKeyboardView> {
public static final String NAME = "ExternalKeyboardView";
private KeyboardKeyPressHandler keyboardKeyPressHandler;
@Override
public String getName() {
return NAME;
}
@Override
public ExternalKeyboardView createViewInstance(ThemedReactContext context) {
this.keyboardKeyPressHandler = new KeyboardKeyPressHandler();
return new ExternalKeyboardView(context);
}
private void onKeyPressHandler(ReactViewGroup reactViewGroup, int keyCode, KeyEvent keyEvent, ThemedReactContext reactContext) {
if(!(reactViewGroup instanceof ExternalKeyboardView)) return;
ExternalKeyboardView viewGroup = (ExternalKeyboardView)reactViewGroup;
if (!viewGroup.hasKeyUpListener && !viewGroup.hasKeyDownListener) {
return;
}
KeyboardKeyPressHandler.PressInfo pressInfo = keyboardKeyPressHandler.getEventsFromKeyPress(keyCode, keyEvent);
if (pressInfo.firePressDownEvent && viewGroup.hasKeyDownListener) {
KeyPressDownEvent keyPressDownEvent = new KeyPressDownEvent(viewGroup.getId(), keyCode, keyEvent);
UIManagerHelper.getEventDispatcherForReactTag(reactContext, viewGroup.getId()).dispatchEvent(keyPressDownEvent);
}
if (pressInfo.firePressUpEvent && viewGroup.hasKeyUpListener) {
KeyPressUpEvent keyPressUpEvent = new KeyPressUpEvent(viewGroup.getId(), keyCode, keyEvent, pressInfo.isLongPress);
UIManagerHelper.getEventDispatcherForReactTag(reactContext, viewGroup.getId()).dispatchEvent(keyPressUpEvent);
}
}
@Override
protected void addEventEmitters(final ThemedReactContext reactContext, ReactViewGroup viewGroup) {
viewGroup.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(View parent, View child) {
child.setOnFocusChangeListener(
(v, hasFocus) -> {
FocusChangeEvent event = new FocusChangeEvent(viewGroup.getId(), hasFocus);
UIManagerHelper.getEventDispatcherForReactTag(reactContext, v.getId()).dispatchEvent(event);
});
child.setOnKeyListener((View v, int keyCode, KeyEvent keyEvent) -> {
onKeyPressHandler(viewGroup, keyCode, keyEvent, reactContext);
return false;
});
}
@Override
public void onChildViewRemoved(View parent, View child) {
}
});
}
@Nullable
@Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
Map<String, Object> export = MapBuilder.<String, Object>builder().build();
if (export == null) {
export = MapBuilder.newHashMap();
}
export.put(FocusChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onFocusChange"));
export.put(KeyPressUpEvent.EVENT_NAME, MapBuilder.of("registrationName", "onKeyUpPress"));
export.put(KeyPressDownEvent.EVENT_NAME, MapBuilder.of("registrationName", "onKeyDownPress"));
return export;
}
@Override
@ReactProp(name = "canBeFocused", defaultBoolean = true)
public void setCanBeFocused(ExternalKeyboardView wrapper, boolean canBeFocused) {
int descendantFocusability = canBeFocused ?
ViewGroup.FOCUS_AFTER_DESCENDANTS
: ViewGroup.FOCUS_BLOCK_DESCENDANTS;
wrapper.setDescendantFocusability(descendantFocusability);
}
@Override
@ReactProp(name = "hasKeyDownPress")
public void setHasKeyDownPress(ExternalKeyboardView view, boolean value) {
view.hasKeyDownListener = value;
}
@Override
@ReactProp(name = "hasKeyUpPress")
public void setHasKeyUpPress(ExternalKeyboardView view, boolean value) {
view.hasKeyUpListener = value;
}
}