@g9tm/app-diagnostic-lynx 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,126 +1,126 @@
1
- package com.g9tm.appdiagnostic
2
-
3
- import android.app.Activity
4
- import android.view.MotionEvent
5
- import android.view.View
6
- import android.view.ViewConfiguration
7
- import android.view.ViewGroup
8
- import android.view.Window
9
- import com.g9tm.appdiagnostic.core.TouchCore
10
- import org.json.JSONObject
11
-
12
- /**
13
- * The tester gesture on Android: every touch of the activity's window, seen BEFORE any view through a wrapper around the
14
- * window's callback — so a host needs no edit in its activity (MAUI needed `MainActivity.DispatchTouchEvent` to forward).
15
- * The decisions are [TouchCore]'s; this class only converts events and carries the verdicts out: a CANCEL to the views
16
- * when a hold begins, a replayed DOWN + POINTER_DOWNs when a held touch turns out to be a pinch or a pan.
17
- *
18
- * Off (nothing watched) the wrapper returns at its first line: Tester Mode off costs one field read per touch.
19
- *
20
- * TWIN: ios/src/G9AppDiagnosticTouches.m (a recogniser on the key window that cancels the views' touches).
21
- */
22
- internal object TouchWatcher {
23
- const val EVENT = "g9:diagnostic-touch"
24
-
25
- @Volatile private var core: TouchCore? = null
26
-
27
- private fun coreFor(activity: Activity): TouchCore =
28
- core ?: synchronized(this) {
29
- core ?: TouchCore(maxOf(16, ViewConfiguration.get(activity).scaledTouchSlop * 2).toFloat()) { android.os.SystemClock.uptimeMillis() }.also { core = it }
30
- }
31
-
32
- /** Sets what is watched; the window of the activity on screen is wrapped (once per window). */
33
- fun configure(activity: Activity?, taps: Boolean, touches: Boolean, consume: Boolean, excludeSurfaces: Boolean) {
34
- val c = core ?: activity?.let { coreFor(it) } ?: return
35
- c.taps = taps
36
- c.touches = touches
37
- c.consume = consume
38
- c.excludeSurfaces = excludeSurfaces
39
- activity?.let { attach(it) }
40
- }
41
-
42
- /** Wraps [activity]'s window callback (an activity recreated by a rotation gets wrapped again). Main thread. */
43
- fun attach(activity: Activity) {
44
- val window = activity.window ?: return
45
- val callback = window.callback ?: return
46
- if (callback is Watching) return
47
- val c = coreFor(activity)
48
- c.isExcluded = { x, y -> isOverExcluded(activity, x, y) }
49
- window.callback = Watching(callback, activity)
50
- }
51
-
52
- private class Watching(private val inner: Window.Callback, private val activity: Activity) : Window.Callback by inner {
53
- override fun dispatchTouchEvent(event: MotionEvent): Boolean {
54
- val c = core
55
- if (c == null || !c.watching) return inner.dispatchTouchEvent(event)
56
- val result =
57
- try {
58
- c.onEvent(sample(event))
59
- } catch (_: Throwable) {
60
- return inner.dispatchTouchEvent(event) // a watcher never breaks a touch
61
- }
62
- for (e in result.emits) emit(e)
63
- return when (result.verdict) {
64
- TouchCore.Verdict.PASS -> inner.dispatchTouchEvent(event)
65
- TouchCore.Verdict.CONSUME -> true
66
- TouchCore.Verdict.CANCEL_THEN_CONSUME -> {
67
- val cancel = MotionEvent.obtain(event)
68
- cancel.action = MotionEvent.ACTION_CANCEL
69
- inner.dispatchTouchEvent(cancel)
70
- cancel.recycle()
71
- true
72
- }
73
- TouchCore.Verdict.REPLAY -> {
74
- replay(event)
75
- true
76
- }
77
- }
78
- }
79
-
80
- /** A held gesture back to the views: a DOWN for the first finger, a POINTER_DOWN per further one, where they are NOW. */
81
- private fun replay(event: MotionEvent) {
82
- val count = event.pointerCount
83
- val properties = Array(count) { MotionEvent.PointerProperties().also { p -> event.getPointerProperties(it, p) } }
84
- val coords = Array(count) { MotionEvent.PointerCoords().also { p -> event.getPointerCoords(it, p) } }
85
- for (fingers in 1..count) {
86
- val action = if (fingers == 1) MotionEvent.ACTION_DOWN else MotionEvent.ACTION_POINTER_DOWN or ((fingers - 1) shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
87
- val synthetic =
88
- MotionEvent.obtain(
89
- event.downTime, event.eventTime, action, fingers, properties.copyOf(fingers).requireNoNulls(), coords.copyOf(fingers).requireNoNulls(),
90
- event.metaState, event.buttonState, event.xPrecision, event.yPrecision, event.deviceId, event.edgeFlags, event.source, event.flags,
91
- )
92
- inner.dispatchTouchEvent(synthetic)
93
- synthetic.recycle()
94
- }
95
- inner.dispatchTouchEvent(event)
96
- }
97
-
98
- private fun emit(e: TouchCore.Emit) {
99
- val root = activity.window?.decorView ?: return
100
- val w = root.width.toFloat()
101
- val h = root.height.toFloat()
102
- if (w <= 0f || h <= 0f) return
103
- val json = JSONObject().put("type", e.type).put("fingers", e.fingers).put("x", (e.x / w).toDouble()).put("y", (e.y / h).toDouble()).put("excluded", e.excluded)
104
- G9AppDiagnosticHost.emit(EVENT, json.toString())
105
- }
106
- }
107
-
108
- private fun sample(event: MotionEvent): TouchCore.Sample =
109
- TouchCore.Sample(event.actionMasked, List(event.pointerCount) { TouchCore.Pointer(event.getPointerId(it), event.getX(it), event.getY(it)) })
110
-
111
- /** A visible excluded view under the window point. A first finger is rare enough to walk the tree. */
112
- private fun isOverExcluded(activity: Activity, x: Float, y: Float): Boolean {
113
- val root = activity.window?.decorView ?: return false
114
- fun walk(view: View): Boolean {
115
- if (!view.isShown) return false
116
- if (G9AppDiagnosticHost.isExcludedView(view)) {
117
- val at = IntArray(2)
118
- view.getLocationInWindow(at)
119
- return x >= at[0] && x <= at[0] + view.width && y >= at[1] && y <= at[1] + view.height
120
- }
121
- if (view is ViewGroup) for (i in 0 until view.childCount) if (walk(view.getChildAt(i))) return true
122
- return false
123
- }
124
- return walk(root)
125
- }
126
- }
1
+ package com.g9tm.appdiagnostic
2
+
3
+ import android.app.Activity
4
+ import android.view.MotionEvent
5
+ import android.view.View
6
+ import android.view.ViewConfiguration
7
+ import android.view.ViewGroup
8
+ import android.view.Window
9
+ import com.g9tm.appdiagnostic.core.TouchCore
10
+ import org.json.JSONObject
11
+
12
+ /**
13
+ * The tester gesture on Android: every touch of the activity's window, seen BEFORE any view through a wrapper around the
14
+ * window's callback — so a host needs no edit in its activity (MAUI needed `MainActivity.DispatchTouchEvent` to forward).
15
+ * The decisions are [TouchCore]'s; this class only converts events and carries the verdicts out: a CANCEL to the views
16
+ * when a hold begins, a replayed DOWN + POINTER_DOWNs when a held touch turns out to be a pinch or a pan.
17
+ *
18
+ * Off (nothing watched) the wrapper returns at its first line: Tester Mode off costs one field read per touch.
19
+ *
20
+ * TWIN: ios/src/G9AppDiagnosticTouches.m (a recogniser on the key window that cancels the views' touches).
21
+ */
22
+ internal object TouchWatcher {
23
+ const val EVENT = "g9:diagnostic-touch"
24
+
25
+ @Volatile private var core: TouchCore? = null
26
+
27
+ private fun coreFor(activity: Activity): TouchCore =
28
+ core ?: synchronized(this) {
29
+ core ?: TouchCore(maxOf(16, ViewConfiguration.get(activity).scaledTouchSlop * 2).toFloat()) { android.os.SystemClock.uptimeMillis() }.also { core = it }
30
+ }
31
+
32
+ /** Sets what is watched; the window of the activity on screen is wrapped (once per window). */
33
+ fun configure(activity: Activity?, taps: Boolean, touches: Boolean, consume: Boolean, excludeSurfaces: Boolean) {
34
+ val c = core ?: activity?.let { coreFor(it) } ?: return
35
+ c.taps = taps
36
+ c.touches = touches
37
+ c.consume = consume
38
+ c.excludeSurfaces = excludeSurfaces
39
+ activity?.let { attach(it) }
40
+ }
41
+
42
+ /** Wraps [activity]'s window callback (an activity recreated by a rotation gets wrapped again). Main thread. */
43
+ fun attach(activity: Activity) {
44
+ val window = activity.window ?: return
45
+ val callback = window.callback ?: return
46
+ if (callback is Watching) return
47
+ val c = coreFor(activity)
48
+ c.isExcluded = { x, y -> isOverExcluded(activity, x, y) }
49
+ window.callback = Watching(callback, activity)
50
+ }
51
+
52
+ private class Watching(private val inner: Window.Callback, private val activity: Activity) : Window.Callback by inner {
53
+ override fun dispatchTouchEvent(event: MotionEvent): Boolean {
54
+ val c = core
55
+ if (c == null || !c.watching) return inner.dispatchTouchEvent(event)
56
+ val result =
57
+ try {
58
+ c.onEvent(sample(event))
59
+ } catch (_: Throwable) {
60
+ return inner.dispatchTouchEvent(event) // a watcher never breaks a touch
61
+ }
62
+ for (e in result.emits) emit(e)
63
+ return when (result.verdict) {
64
+ TouchCore.Verdict.PASS -> inner.dispatchTouchEvent(event)
65
+ TouchCore.Verdict.CONSUME -> true
66
+ TouchCore.Verdict.CANCEL_THEN_CONSUME -> {
67
+ val cancel = MotionEvent.obtain(event)
68
+ cancel.action = MotionEvent.ACTION_CANCEL
69
+ inner.dispatchTouchEvent(cancel)
70
+ cancel.recycle()
71
+ true
72
+ }
73
+ TouchCore.Verdict.REPLAY -> {
74
+ replay(event)
75
+ true
76
+ }
77
+ }
78
+ }
79
+
80
+ /** A held gesture back to the views: a DOWN for the first finger, a POINTER_DOWN per further one, where they are NOW. */
81
+ private fun replay(event: MotionEvent) {
82
+ val count = event.pointerCount
83
+ val properties = Array(count) { MotionEvent.PointerProperties().also { p -> event.getPointerProperties(it, p) } }
84
+ val coords = Array(count) { MotionEvent.PointerCoords().also { p -> event.getPointerCoords(it, p) } }
85
+ for (fingers in 1..count) {
86
+ val action = if (fingers == 1) MotionEvent.ACTION_DOWN else MotionEvent.ACTION_POINTER_DOWN or ((fingers - 1) shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
87
+ val synthetic =
88
+ MotionEvent.obtain(
89
+ event.downTime, event.eventTime, action, fingers, properties.copyOf(fingers).requireNoNulls(), coords.copyOf(fingers).requireNoNulls(),
90
+ event.metaState, event.buttonState, event.xPrecision, event.yPrecision, event.deviceId, event.edgeFlags, event.source, event.flags,
91
+ )
92
+ inner.dispatchTouchEvent(synthetic)
93
+ synthetic.recycle()
94
+ }
95
+ inner.dispatchTouchEvent(event)
96
+ }
97
+
98
+ private fun emit(e: TouchCore.Emit) {
99
+ val root = activity.window?.decorView ?: return
100
+ val w = root.width.toFloat()
101
+ val h = root.height.toFloat()
102
+ if (w <= 0f || h <= 0f) return
103
+ val json = JSONObject().put("type", e.type).put("fingers", e.fingers).put("x", (e.x / w).toDouble()).put("y", (e.y / h).toDouble()).put("excluded", e.excluded)
104
+ G9AppDiagnosticHost.emit(EVENT, json.toString())
105
+ }
106
+ }
107
+
108
+ private fun sample(event: MotionEvent): TouchCore.Sample =
109
+ TouchCore.Sample(event.actionMasked, List(event.pointerCount) { TouchCore.Pointer(event.getPointerId(it), event.getX(it), event.getY(it)) })
110
+
111
+ /** A visible excluded view under the window point. A first finger is rare enough to walk the tree. */
112
+ private fun isOverExcluded(activity: Activity, x: Float, y: Float): Boolean {
113
+ val root = activity.window?.decorView ?: return false
114
+ fun walk(view: View): Boolean {
115
+ if (!view.isShown) return false
116
+ if (G9AppDiagnosticHost.isExcludedView(view)) {
117
+ val at = IntArray(2)
118
+ view.getLocationInWindow(at)
119
+ return x >= at[0] && x <= at[0] + view.width && y >= at[1] && y <= at[1] + view.height
120
+ }
121
+ if (view is ViewGroup) for (i in 0 until view.childCount) if (walk(view.getChildAt(i))) return true
122
+ return false
123
+ }
124
+ return walk(root)
125
+ }
126
+ }
@@ -1,175 +1,175 @@
1
- package com.g9tm.appdiagnostic.core
2
-
3
- /**
4
- * The tester gesture's decisions, free of Android types so the JVM suite drives them (../../jvm): the TWIN of
5
- * G9AppDiagnostic.Maui TesterGestureService (OnTouch + InterceptDispatch, 2026-10-24) and of the Lynx app's own observer.
6
- *
7
- * OBSERVE. A completed multi-finger tap — all fingers down and up within [MAX_TAP_MS] (350 ms for two, 500 for three), no
8
- * finger moved past the slop — is reported; JS pairs two of them into the gesture (@g9tm/app-diagnostic tester/gesture.ts).
9
- * With [touches], every first finger down is reported too (a recording's timeline). A pinch or a pan moves, so it never
10
- * counts.
11
- *
12
- * CONSUME (E-20). Observed only, the gesture's first finger also pressed whatever was under it (a button, a list row, a
13
- * tree on the map), and the report began over a screen the gesture had just changed (QA, 2026-10-24). With [consume], a
14
- * SECOND finger that lands within [SECOND_FINGER_WINDOW_MS] of the first, unmoved, makes the views get a CANCEL (a pressed
15
- * button un-presses; a click is on UP) and the stream is HELD. Lifted as a tap: the views saw only a cancelled touch. Moved
16
- * past the slop or held longer than a tap: a pinch or a pan — the held stream is REPLAYED (a fresh DOWN and POINTER_DOWNs
17
- * where the fingers are now) and the rest flows through, so pinch-zoom still works, a slop late. A second finger that comes
18
- * late passes untouched. Over an excluded surface (the map, in its mode) nothing is held: that surface stays native.
19
- */
20
- class TouchCore(private val slopPx: Float, private val clock: () -> Long) {
21
- companion object {
22
- const val DOWN = 0
23
- const val UP = 1
24
- const val MOVE = 2
25
- const val CANCEL = 3
26
- const val POINTER_DOWN = 5
27
- const val POINTER_UP = 6
28
-
29
- const val MAX_TAP_MS = 350L
30
- const val MAX_THREE_FINGER_TAP_MS = 500L
31
- const val SECOND_FINGER_WINDOW_MS = 250L
32
-
33
- fun maxTapMsFor(fingers: Int): Long = if (fingers >= 3) MAX_THREE_FINGER_TAP_MS else MAX_TAP_MS
34
- }
35
-
36
- /** One pointer of an event, in window pixels. */
37
- data class Pointer(val id: Int, val x: Float, val y: Float)
38
-
39
- /** One event: its masked action (the MotionEvent constants) and every pointer down in it. */
40
- data class Sample(val action: Int, val pointers: List<Pointer>)
41
-
42
- /** What the watcher tells JS. */
43
- data class Emit(val type: String, val fingers: Int, val x: Float, val y: Float, val excluded: Boolean)
44
-
45
- enum class Verdict {
46
- /** The views get the event as it is. */
47
- PASS,
48
-
49
- /** Held here: the views do not get it. */
50
- CONSUME,
51
-
52
- /** The views get a CANCEL of this event, then nothing (the hold begins). */
53
- CANCEL_THEN_CONSUME,
54
-
55
- /** The views get a fresh DOWN + POINTER_DOWNs at the fingers' current places, then this event (the hold ends). */
56
- REPLAY,
57
- }
58
-
59
- data class Result(val verdict: Verdict, val emits: List<Emit>)
60
-
61
- private enum class Phase { IDLE, HOLDING, RELEASED, PASSING }
62
-
63
- @Volatile var taps = false
64
- @Volatile var touches = false
65
- @Volatile var consume = false
66
- @Volatile var excludeSurfaces = false
67
-
68
- /** Whether a window point is on a surface the host excludes (the map's view). Asked only on a first finger down. */
69
- var isExcluded: (Float, Float) -> Boolean = { _, _ -> false }
70
-
71
- private var downAt = 0L
72
- private var maxPointers = 0
73
- private var moved = false
74
- private var downX = 0f
75
- private var downY = 0f
76
- private var downExcluded = false
77
-
78
- private var phase = Phase.IDLE
79
- private val held = HashMap<Int, Pair<Float, Float>>()
80
-
81
- val watching: Boolean
82
- get() = taps || touches
83
-
84
- /** Every event of the window, in order. Never throws. */
85
- fun onEvent(s: Sample): Result {
86
- if (!watching) {
87
- phase = Phase.IDLE
88
- return Result(Verdict.PASS, emptyList())
89
- }
90
- val emits = ArrayList<Emit>(1)
91
- observe(s, emits)
92
- val verdict = if (taps && consume) intercept(s) else Verdict.PASS
93
- return Result(verdict, emits)
94
- }
95
-
96
- private fun observe(s: Sample, emits: MutableList<Emit>) {
97
- val first = s.pointers.firstOrNull()
98
- when (s.action) {
99
- DOWN -> {
100
- downAt = clock()
101
- maxPointers = 1
102
- moved = false
103
- downX = first?.x ?: 0f
104
- downY = first?.y ?: 0f
105
- downExcluded = runCatching { isExcluded(downX, downY) }.getOrDefault(false)
106
- if (touches) emits += Emit("down", 1, downX, downY, false)
107
- }
108
- POINTER_DOWN -> maxPointers = maxOf(maxPointers, s.pointers.size)
109
- MOVE -> if (!moved && first != null && (Math.abs(first.x - downX) > slopPx || Math.abs(first.y - downY) > slopPx)) moved = true
110
- UP -> {
111
- if (taps && !moved && maxPointers >= 2 && clock() - downAt <= maxTapMsFor(maxPointers)) {
112
- emits += Emit("tap", maxPointers, downX, downY, downExcluded)
113
- }
114
- maxPointers = 0
115
- }
116
- CANCEL -> {
117
- maxPointers = 0
118
- moved = true
119
- }
120
- }
121
- }
122
-
123
- private fun intercept(s: Sample): Verdict =
124
- when (s.action) {
125
- DOWN -> {
126
- phase = Phase.IDLE
127
- held.clear()
128
- Verdict.PASS
129
- }
130
- POINTER_DOWN ->
131
- when {
132
- phase == Phase.HOLDING -> {
133
- remember(s)
134
- Verdict.CONSUME
135
- }
136
- phase != Phase.IDLE -> Verdict.PASS
137
- s.pointers.size == 2 && !moved && clock() - downAt <= SECOND_FINGER_WINDOW_MS && !(excludeSurfaces && downExcluded) -> {
138
- remember(s)
139
- phase = Phase.HOLDING
140
- Verdict.CANCEL_THEN_CONSUME
141
- }
142
- else -> {
143
- phase = Phase.PASSING
144
- Verdict.PASS
145
- }
146
- }
147
- MOVE ->
148
- if (phase != Phase.HOLDING) {
149
- Verdict.PASS
150
- } else if (anyMoved(s) || clock() - downAt > maxTapMsFor(s.pointers.size)) {
151
- phase = Phase.RELEASED
152
- Verdict.REPLAY
153
- } else {
154
- Verdict.CONSUME
155
- }
156
- POINTER_UP -> if (phase == Phase.HOLDING) Verdict.CONSUME else Verdict.PASS
157
- UP, CANCEL -> {
158
- val wasHeld = phase == Phase.HOLDING
159
- phase = Phase.IDLE
160
- held.clear()
161
- if (wasHeld) Verdict.CONSUME else Verdict.PASS
162
- }
163
- else -> if (phase == Phase.HOLDING) Verdict.CONSUME else Verdict.PASS
164
- }
165
-
166
- private fun remember(s: Sample) {
167
- for (p in s.pointers) held.putIfAbsent(p.id, p.x to p.y)
168
- }
169
-
170
- private fun anyMoved(s: Sample): Boolean =
171
- s.pointers.any { p ->
172
- val start = held[p.id] ?: return@any false
173
- Math.abs(p.x - start.first) > slopPx || Math.abs(p.y - start.second) > slopPx
174
- }
175
- }
1
+ package com.g9tm.appdiagnostic.core
2
+
3
+ /**
4
+ * The tester gesture's decisions, free of Android types so the JVM suite drives them (../../jvm): the TWIN of
5
+ * G9AppDiagnostic.Maui TesterGestureService (OnTouch + InterceptDispatch, 2026-10-24) and of the Lynx app's own observer.
6
+ *
7
+ * OBSERVE. A completed multi-finger tap — all fingers down and up within [MAX_TAP_MS] (350 ms for two, 500 for three), no
8
+ * finger moved past the slop — is reported; JS pairs two of them into the gesture (@g9tm/app-diagnostic tester/gesture.ts).
9
+ * With [touches], every first finger down is reported too (a recording's timeline). A pinch or a pan moves, so it never
10
+ * counts.
11
+ *
12
+ * CONSUME (E-20). Observed only, the gesture's first finger also pressed whatever was under it (a button, a list row, a
13
+ * tree on the map), and the report began over a screen the gesture had just changed (QA, 2026-10-24). With [consume], a
14
+ * SECOND finger that lands within [SECOND_FINGER_WINDOW_MS] of the first, unmoved, makes the views get a CANCEL (a pressed
15
+ * button un-presses; a click is on UP) and the stream is HELD. Lifted as a tap: the views saw only a cancelled touch. Moved
16
+ * past the slop or held longer than a tap: a pinch or a pan — the held stream is REPLAYED (a fresh DOWN and POINTER_DOWNs
17
+ * where the fingers are now) and the rest flows through, so pinch-zoom still works, a slop late. A second finger that comes
18
+ * late passes untouched. Over an excluded surface (the map, in its mode) nothing is held: that surface stays native.
19
+ */
20
+ class TouchCore(private val slopPx: Float, private val clock: () -> Long) {
21
+ companion object {
22
+ const val DOWN = 0
23
+ const val UP = 1
24
+ const val MOVE = 2
25
+ const val CANCEL = 3
26
+ const val POINTER_DOWN = 5
27
+ const val POINTER_UP = 6
28
+
29
+ const val MAX_TAP_MS = 350L
30
+ const val MAX_THREE_FINGER_TAP_MS = 500L
31
+ const val SECOND_FINGER_WINDOW_MS = 250L
32
+
33
+ fun maxTapMsFor(fingers: Int): Long = if (fingers >= 3) MAX_THREE_FINGER_TAP_MS else MAX_TAP_MS
34
+ }
35
+
36
+ /** One pointer of an event, in window pixels. */
37
+ data class Pointer(val id: Int, val x: Float, val y: Float)
38
+
39
+ /** One event: its masked action (the MotionEvent constants) and every pointer down in it. */
40
+ data class Sample(val action: Int, val pointers: List<Pointer>)
41
+
42
+ /** What the watcher tells JS. */
43
+ data class Emit(val type: String, val fingers: Int, val x: Float, val y: Float, val excluded: Boolean)
44
+
45
+ enum class Verdict {
46
+ /** The views get the event as it is. */
47
+ PASS,
48
+
49
+ /** Held here: the views do not get it. */
50
+ CONSUME,
51
+
52
+ /** The views get a CANCEL of this event, then nothing (the hold begins). */
53
+ CANCEL_THEN_CONSUME,
54
+
55
+ /** The views get a fresh DOWN + POINTER_DOWNs at the fingers' current places, then this event (the hold ends). */
56
+ REPLAY,
57
+ }
58
+
59
+ data class Result(val verdict: Verdict, val emits: List<Emit>)
60
+
61
+ private enum class Phase { IDLE, HOLDING, RELEASED, PASSING }
62
+
63
+ @Volatile var taps = false
64
+ @Volatile var touches = false
65
+ @Volatile var consume = false
66
+ @Volatile var excludeSurfaces = false
67
+
68
+ /** Whether a window point is on a surface the host excludes (the map's view). Asked only on a first finger down. */
69
+ var isExcluded: (Float, Float) -> Boolean = { _, _ -> false }
70
+
71
+ private var downAt = 0L
72
+ private var maxPointers = 0
73
+ private var moved = false
74
+ private var downX = 0f
75
+ private var downY = 0f
76
+ private var downExcluded = false
77
+
78
+ private var phase = Phase.IDLE
79
+ private val held = HashMap<Int, Pair<Float, Float>>()
80
+
81
+ val watching: Boolean
82
+ get() = taps || touches
83
+
84
+ /** Every event of the window, in order. Never throws. */
85
+ fun onEvent(s: Sample): Result {
86
+ if (!watching) {
87
+ phase = Phase.IDLE
88
+ return Result(Verdict.PASS, emptyList())
89
+ }
90
+ val emits = ArrayList<Emit>(1)
91
+ observe(s, emits)
92
+ val verdict = if (taps && consume) intercept(s) else Verdict.PASS
93
+ return Result(verdict, emits)
94
+ }
95
+
96
+ private fun observe(s: Sample, emits: MutableList<Emit>) {
97
+ val first = s.pointers.firstOrNull()
98
+ when (s.action) {
99
+ DOWN -> {
100
+ downAt = clock()
101
+ maxPointers = 1
102
+ moved = false
103
+ downX = first?.x ?: 0f
104
+ downY = first?.y ?: 0f
105
+ downExcluded = runCatching { isExcluded(downX, downY) }.getOrDefault(false)
106
+ if (touches) emits += Emit("down", 1, downX, downY, false)
107
+ }
108
+ POINTER_DOWN -> maxPointers = maxOf(maxPointers, s.pointers.size)
109
+ MOVE -> if (!moved && first != null && (Math.abs(first.x - downX) > slopPx || Math.abs(first.y - downY) > slopPx)) moved = true
110
+ UP -> {
111
+ if (taps && !moved && maxPointers >= 2 && clock() - downAt <= maxTapMsFor(maxPointers)) {
112
+ emits += Emit("tap", maxPointers, downX, downY, downExcluded)
113
+ }
114
+ maxPointers = 0
115
+ }
116
+ CANCEL -> {
117
+ maxPointers = 0
118
+ moved = true
119
+ }
120
+ }
121
+ }
122
+
123
+ private fun intercept(s: Sample): Verdict =
124
+ when (s.action) {
125
+ DOWN -> {
126
+ phase = Phase.IDLE
127
+ held.clear()
128
+ Verdict.PASS
129
+ }
130
+ POINTER_DOWN ->
131
+ when {
132
+ phase == Phase.HOLDING -> {
133
+ remember(s)
134
+ Verdict.CONSUME
135
+ }
136
+ phase != Phase.IDLE -> Verdict.PASS
137
+ s.pointers.size == 2 && !moved && clock() - downAt <= SECOND_FINGER_WINDOW_MS && !(excludeSurfaces && downExcluded) -> {
138
+ remember(s)
139
+ phase = Phase.HOLDING
140
+ Verdict.CANCEL_THEN_CONSUME
141
+ }
142
+ else -> {
143
+ phase = Phase.PASSING
144
+ Verdict.PASS
145
+ }
146
+ }
147
+ MOVE ->
148
+ if (phase != Phase.HOLDING) {
149
+ Verdict.PASS
150
+ } else if (anyMoved(s) || clock() - downAt > maxTapMsFor(s.pointers.size)) {
151
+ phase = Phase.RELEASED
152
+ Verdict.REPLAY
153
+ } else {
154
+ Verdict.CONSUME
155
+ }
156
+ POINTER_UP -> if (phase == Phase.HOLDING) Verdict.CONSUME else Verdict.PASS
157
+ UP, CANCEL -> {
158
+ val wasHeld = phase == Phase.HOLDING
159
+ phase = Phase.IDLE
160
+ held.clear()
161
+ if (wasHeld) Verdict.CONSUME else Verdict.PASS
162
+ }
163
+ else -> if (phase == Phase.HOLDING) Verdict.CONSUME else Verdict.PASS
164
+ }
165
+
166
+ private fun remember(s: Sample) {
167
+ for (p in s.pointers) held.putIfAbsent(p.id, p.x to p.y)
168
+ }
169
+
170
+ private fun anyMoved(s: Sample): Boolean =
171
+ s.pointers.any { p ->
172
+ val start = held[p.id] ?: return@any false
173
+ Math.abs(p.x - start.first) > slopPx || Math.abs(p.y - start.second) > slopPx
174
+ }
175
+ }