@g9tm/app-diagnostic-lynx 0.2.0 → 0.2.2

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,129 @@
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 =
104
+ JSONObject().put("type", e.type).put("fingers", e.fingers).put("x", (e.x / w).toDouble()).put("y", (e.y / h).toDouble())
105
+ .put("cx", (e.cx / w).toDouble()).put("cy", (e.cy / h).toDouble()).put("excluded", e.excluded)
106
+ e.info?.let { json.put("info", JSONObject(it)) }
107
+ G9AppDiagnosticHost.emit(EVENT, json.toString())
108
+ }
109
+ }
110
+
111
+ private fun sample(event: MotionEvent): TouchCore.Sample =
112
+ TouchCore.Sample(event.actionMasked, List(event.pointerCount) { TouchCore.Pointer(event.getPointerId(it), event.getX(it), event.getY(it)) })
113
+
114
+ /** A visible excluded view under the window point. A first finger is rare enough to walk the tree. */
115
+ private fun isOverExcluded(activity: Activity, x: Float, y: Float): Boolean {
116
+ val root = activity.window?.decorView ?: return false
117
+ fun walk(view: View): Boolean {
118
+ if (!view.isShown) return false
119
+ if (G9AppDiagnosticHost.isExcludedView(view)) {
120
+ val at = IntArray(2)
121
+ view.getLocationInWindow(at)
122
+ return x >= at[0] && x <= at[0] + view.width && y >= at[1] && y <= at[1] + view.height
123
+ }
124
+ if (view is ViewGroup) for (i in 0 until view.childCount) if (walk(view.getChildAt(i))) return true
125
+ return false
126
+ }
127
+ return walk(root)
128
+ }
129
+ }