-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathOverKeyboardViewGroup.kt
More file actions
219 lines (189 loc) · 6.79 KB
/
OverKeyboardViewGroup.kt
File metadata and controls
219 lines (189 loc) · 6.79 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.reactnativekeyboardcontroller.views.overlay
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.PixelFormat
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableNativeMap
import com.facebook.react.config.ReactFeatureFlags
import com.facebook.react.uimanager.JSTouchDispatcher
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.views.view.ReactViewGroup
import com.reactnativekeyboardcontroller.extensions.dp
import com.reactnativekeyboardcontroller.extensions.getDisplaySize
import com.reactnativekeyboardcontroller.log.Logger
private val TAG = OverKeyboardHostView::class.qualifiedName
@SuppressLint("ViewConstructor")
class OverKeyboardHostView(
private val reactContext: ThemedReactContext,
) : ReactViewGroup(reactContext) {
private val dispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, this.id)
private var windowManager: WindowManager = reactContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
private var hostView: OverKeyboardRootViewGroup = OverKeyboardRootViewGroup(reactContext)
internal var stateWrapper: StateWrapper? = null
init {
hostView.eventDispatcher = dispatcher
}
// region life cycles
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
hide()
}
override fun addView(
child: View?,
index: Int,
) {
UiThreadUtil.assertOnUiThread()
hostView.addView(child, index)
}
override fun getChildCount(): Int = hostView.childCount
override fun getChildAt(index: Int): View? = hostView.getChildAt(index)
override fun removeView(child: View?) {
UiThreadUtil.assertOnUiThread()
if (child != null) {
hostView.removeView(child)
}
}
override fun removeViewAt(index: Int) {
UiThreadUtil.assertOnUiThread()
val child = getChildAt(index)
hostView.removeView(child)
}
override fun onLayout(
changed: Boolean,
l: Int,
t: Int,
r: Int,
b: Int,
) {
// Do nothing as we are laid out by UIManager
}
// endregion
fun show() {
val layoutParams =
WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
// This type ensures it floats over other application windows but under system windows
WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
// Ensures touches outside the view pass through to other windows
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT,
)
stretchTo(fullScreen = true)
windowManager.addView(hostView, layoutParams)
}
fun hide() {
if (hostView.isAttached) {
windowManager.removeView(hostView)
stretchTo(fullScreen = false)
}
}
private fun stretchTo(fullScreen: Boolean) {
val displaySize = reactContext.getDisplaySize()
val newStateData: WritableMap = WritableNativeMap()
newStateData.putDouble("screenWidth", if (fullScreen) displaySize.x.toFloat().dp else 0.0)
newStateData.putDouble("screenHeight", if (fullScreen) displaySize.y.toFloat().dp else 0.0)
stateWrapper?.updateState(newStateData)
}
}
@SuppressLint("ViewConstructor")
class OverKeyboardRootViewGroup(
private val reactContext: ThemedReactContext,
) : ReactViewGroup(reactContext),
RootViewCompat {
private val jsTouchDispatcher: JSTouchDispatcher = JSTouchDispatcher(this)
private var jsPointerDispatcher: JSPointerDispatcherCompat? = null
internal var eventDispatcher: EventDispatcher? = null
internal var isAttached = false
init {
if (ReactFeatureFlags.dispatchPointerEvents) {
jsPointerDispatcher = JSPointerDispatcherCompat(this)
}
}
// region life cycles
override fun onAttachedToWindow() {
super.onAttachedToWindow()
isAttached = true
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
isAttached = false
}
// endregion
// region Touch events handling
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
eventDispatcher?.let { eventDispatcher ->
try {
jsTouchDispatcher.handleTouchEvent(event, eventDispatcher)
jsPointerDispatcher?.handleMotionEventCompat(event, eventDispatcher, true)
} catch (
@Suppress("detekt:TooGenericExceptionCaught") e: RuntimeException,
) {
Logger.w(TAG, "Can not handle touch event", e)
}
}
return super.onInterceptTouchEvent(event)
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
eventDispatcher?.let { eventDispatcher ->
try {
jsTouchDispatcher.handleTouchEvent(event, eventDispatcher)
jsPointerDispatcher?.handleMotionEventCompat(event, eventDispatcher, false)
} catch (
@Suppress("detekt:TooGenericExceptionCaught") e: RuntimeException,
) {
Logger.w(TAG, "Can not handle touch event", e)
}
}
super.onTouchEvent(event)
// In case when there is no children interested in handling touch event, we return true from
// the root view in order to receive subsequent events related to that gesture
return true
}
override fun onInterceptHoverEvent(event: MotionEvent): Boolean {
eventDispatcher?.let {
jsPointerDispatcher?.handleMotionEventCompat(event, it, true)
}
return super.onHoverEvent(event)
}
override fun onHoverEvent(event: MotionEvent): Boolean {
eventDispatcher?.let {
jsPointerDispatcher?.handleMotionEventCompat(event, it, false)
}
return super.onHoverEvent(event)
}
override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
// No-op - override in order to still receive events to onInterceptTouchEvent
// even when some other view disallow that
}
// endregion
// region RootView methods
override fun onChildStartedNativeGesture(
childView: View?,
ev: MotionEvent,
) {
eventDispatcher?.let { eventDispatcher ->
jsTouchDispatcher.onChildStartedNativeGesture(ev, eventDispatcher)
jsPointerDispatcher?.onChildStartedNativeGesture(childView, ev, eventDispatcher)
}
}
override fun onChildEndedNativeGesture(
childView: View,
ev: MotionEvent,
) {
eventDispatcher?.let { jsTouchDispatcher.onChildEndedNativeGesture(ev, it) }
jsPointerDispatcher?.onChildEndedNativeGesture()
}
override fun handleException(t: Throwable) {
reactContext.reactApplicationContext.handleException(RuntimeException(t))
}
// endregion
}