@g9tm/app-diagnostic-lynx 0.3.0 → 0.3.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.
- package/README.md +42 -42
- package/android/build.gradle.kts +45 -45
- package/android/src/main/java/com/g9tm/appdiagnostic/TouchWatcher.kt +129 -129
- package/android/src/main/java/com/g9tm/appdiagnostic/core/TouchCore.kt +270 -270
- package/package.json +1 -1
- package/src/index.ts +175 -175
- package/types/g9-app-diagnostic-module.d.ts +60 -60
package/README.md
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
# @g9tm/app-diagnostic-lynx
|
|
2
|
-
|
|
3
|
-
The Lynx native half of [`@g9tm/app-diagnostic`](../app-diagnostic): what the tester mode and the live channel need from the
|
|
4
|
-
device, with no product name anywhere.
|
|
5
|
-
|
|
6
|
-
| Method | What |
|
|
7
|
-
| --- | --- |
|
|
8
|
-
| `capabilities` / `state` | what this device can record; which recorder runs now (a recorder outlives a script reload) |
|
|
9
|
-
| `startScreen` / `stopScreen` | the app's OWN window as an H.264 MP4 (≤ 960 px, adaptive 4-20 fps, 10 minutes at most, no system consent) |
|
|
10
|
-
| `startVoice` / `stopVoice` | a spoken note as AAC `.m4a`; the microphone permission is asked at the first note |
|
|
11
|
-
| `screenshot` | the window as the user sees it, a map's surface included, as JPEG |
|
|
12
|
-
| `sendRange` | one HTTP request whose body is a byte range of a file, read natively (the resumable report upload) |
|
|
13
|
-
| `watchTouches` | the tester gesture: multi-finger taps (with the fingers' midpoint `cx`, `cy`) and first touches as the global event `g9:diagnostic-touch`; with `touches`, also a `seq` trace of every touch that could have been a tap (why it was or was not one); with `consume`, a two-finger touch is held away from the views and replayed when it is a pinch or a pan (E-20). A finger «moved» only when it left its own landing point |
|
|
14
|
-
|
|
15
|
-
The contract (methods, envelopes, error codes, the event) is `types/g9-app-diagnostic-module.d.ts`; `npm run codegen`-style
|
|
16
|
-
regeneration is `lynx-autolink-codegen` then `node scripts/android-bridge-types.mjs --fix`.
|
|
17
|
-
|
|
18
|
-
## Host hooks (Android)
|
|
19
|
-
|
|
20
|
-
Set before the first call, typically in `Application.onCreate`:
|
|
21
|
-
|
|
22
|
-
```kotlin
|
|
23
|
-
G9AppDiagnosticHost.httpClient = { myOkHttpClient } // its trust and proxy rules for the range PUT
|
|
24
|
-
G9AppDiagnosticHost.allowedRoots = { listOf(it.filesDir, it.cacheDir) }
|
|
25
|
-
G9AppDiagnosticHost.isExcludedView = { it.javaClass.simpleName == "MapHost" } // the «except the map» mode
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
The host's activity needs no edit: the gesture wraps the activity window's callback itself.
|
|
29
|
-
|
|
30
|
-
## JS
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
import { createDiagnosticNative, nativeSendRange, onDiagnosticTouch } from '@g9tm/app-diagnostic-lynx';
|
|
34
|
-
const native = createDiagnosticNative();
|
|
35
|
-
await native.watchTouches({ taps: true, touches: false, consume: true, excludeSurfaces: false });
|
|
36
|
-
const off = onDiagnosticTouch(lynx.getJSModule('GlobalEventEmitter'), (e) => pair(e));
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## Checks
|
|
40
|
-
|
|
41
|
-
`npm test` (JS face) · `npm run test:android-jvm` (the gesture's decisions) · `npm run check:android-module` (the module
|
|
42
|
-
against Lynx 4.1.0 and the bridge's signatures). What is proven and what is not: [Native-Validation-Pending.md](Native-Validation-Pending.md).
|
|
1
|
+
# @g9tm/app-diagnostic-lynx
|
|
2
|
+
|
|
3
|
+
The Lynx native half of [`@g9tm/app-diagnostic`](../app-diagnostic): what the tester mode and the live channel need from the
|
|
4
|
+
device, with no product name anywhere.
|
|
5
|
+
|
|
6
|
+
| Method | What |
|
|
7
|
+
| --- | --- |
|
|
8
|
+
| `capabilities` / `state` | what this device can record; which recorder runs now (a recorder outlives a script reload) |
|
|
9
|
+
| `startScreen` / `stopScreen` | the app's OWN window as an H.264 MP4 (≤ 960 px, adaptive 4-20 fps, 10 minutes at most, no system consent) |
|
|
10
|
+
| `startVoice` / `stopVoice` | a spoken note as AAC `.m4a`; the microphone permission is asked at the first note |
|
|
11
|
+
| `screenshot` | the window as the user sees it, a map's surface included, as JPEG |
|
|
12
|
+
| `sendRange` | one HTTP request whose body is a byte range of a file, read natively (the resumable report upload) |
|
|
13
|
+
| `watchTouches` | the tester gesture: multi-finger taps (with the fingers' midpoint `cx`, `cy`) and first touches as the global event `g9:diagnostic-touch`; with `touches`, also a `seq` trace of every touch that could have been a tap (why it was or was not one); with `consume`, a two-finger touch is held away from the views and replayed when it is a pinch or a pan (E-20). A finger «moved» only when it left its own landing point |
|
|
14
|
+
|
|
15
|
+
The contract (methods, envelopes, error codes, the event) is `types/g9-app-diagnostic-module.d.ts`; `npm run codegen`-style
|
|
16
|
+
regeneration is `lynx-autolink-codegen` then `node scripts/android-bridge-types.mjs --fix`.
|
|
17
|
+
|
|
18
|
+
## Host hooks (Android)
|
|
19
|
+
|
|
20
|
+
Set before the first call, typically in `Application.onCreate`:
|
|
21
|
+
|
|
22
|
+
```kotlin
|
|
23
|
+
G9AppDiagnosticHost.httpClient = { myOkHttpClient } // its trust and proxy rules for the range PUT
|
|
24
|
+
G9AppDiagnosticHost.allowedRoots = { listOf(it.filesDir, it.cacheDir) }
|
|
25
|
+
G9AppDiagnosticHost.isExcludedView = { it.javaClass.simpleName == "MapHost" } // the «except the map» mode
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The host's activity needs no edit: the gesture wraps the activity window's callback itself.
|
|
29
|
+
|
|
30
|
+
## JS
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { createDiagnosticNative, nativeSendRange, onDiagnosticTouch } from '@g9tm/app-diagnostic-lynx';
|
|
34
|
+
const native = createDiagnosticNative();
|
|
35
|
+
await native.watchTouches({ taps: true, touches: false, consume: true, excludeSurfaces: false });
|
|
36
|
+
const off = onDiagnosticTouch(lynx.getJSModule('GlobalEventEmitter'), (e) => pair(e));
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Checks
|
|
40
|
+
|
|
41
|
+
`npm test` (JS face) · `npm run test:android-jvm` (the gesture's decisions) · `npm run check:android-module` (the module
|
|
42
|
+
against Lynx 4.1.0 and the bridge's signatures). What is proven and what is not: [Native-Validation-Pending.md](Native-Validation-Pending.md).
|
package/android/build.gradle.kts
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
// @g9tm/app-diagnostic-lynx for Android. Linked into a host by Lynx Autolink from node_modules; the host's settings supply
|
|
2
|
-
// the plugin versions (AGP, Kotlin), like @g9tm/syncdata-lynx.
|
|
3
|
-
plugins {
|
|
4
|
-
id("com.android.library")
|
|
5
|
-
id("org.jetbrains.kotlin.android")
|
|
6
|
-
id("org.jetbrains.kotlin.kapt")
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
android {
|
|
10
|
-
namespace = "com.g9tm.appdiagnostic"
|
|
11
|
-
compileSdk = 36
|
|
12
|
-
|
|
13
|
-
defaultConfig {
|
|
14
|
-
minSdk = 26
|
|
15
|
-
consumerProguardFiles("consumer-rules.pro")
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
compileOptions {
|
|
19
|
-
sourceCompatibility = JavaVersion.VERSION_17
|
|
20
|
-
targetCompatibility = JavaVersion.VERSION_17
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
testOptions { unitTests.isReturnDefaultValues = true }
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
kotlin { jvmToolchain(17) }
|
|
27
|
-
|
|
28
|
-
val lynx = "4.2.0"
|
|
29
|
-
|
|
30
|
-
dependencies {
|
|
31
|
-
compileOnly("org.lynxsdk.lynx:lynx:$lynx")
|
|
32
|
-
compileOnly("org.lynxsdk.lynx:service-api:$lynx")
|
|
33
|
-
kapt("org.lynxsdk.lynx:lynx-processor:$lynx")
|
|
34
|
-
// The generated spec imports androidx.annotation and lynx-processor's output uses @Keep, but Lynx's POMs declare it
|
|
35
|
-
// at runtime scope only: without this line kapt fails in the host ("cannot access Keep").
|
|
36
|
-
compileOnly("androidx.annotation:annotation:1.9.1")
|
|
37
|
-
// The microphone permission goes through the host activity's ActivityResultRegistry (every ComponentActivity has one).
|
|
38
|
-
compileOnly("androidx.activity:activity:1.10.1")
|
|
39
|
-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
|
|
40
|
-
// The range PUT; a host passes its own client (G9AppDiagnosticHost.httpClient) so its trust and proxy rules apply.
|
|
41
|
-
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
|
42
|
-
|
|
43
|
-
// The JVM suite (../jvm) also runs as Android local unit tests.
|
|
44
|
-
testImplementation("junit:junit:4.13.2")
|
|
45
|
-
}
|
|
1
|
+
// @g9tm/app-diagnostic-lynx for Android. Linked into a host by Lynx Autolink from node_modules; the host's settings supply
|
|
2
|
+
// the plugin versions (AGP, Kotlin), like @g9tm/syncdata-lynx.
|
|
3
|
+
plugins {
|
|
4
|
+
id("com.android.library")
|
|
5
|
+
id("org.jetbrains.kotlin.android")
|
|
6
|
+
id("org.jetbrains.kotlin.kapt")
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
android {
|
|
10
|
+
namespace = "com.g9tm.appdiagnostic"
|
|
11
|
+
compileSdk = 36
|
|
12
|
+
|
|
13
|
+
defaultConfig {
|
|
14
|
+
minSdk = 26
|
|
15
|
+
consumerProguardFiles("consumer-rules.pro")
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
compileOptions {
|
|
19
|
+
sourceCompatibility = JavaVersion.VERSION_17
|
|
20
|
+
targetCompatibility = JavaVersion.VERSION_17
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
testOptions { unitTests.isReturnDefaultValues = true }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
kotlin { jvmToolchain(17) }
|
|
27
|
+
|
|
28
|
+
val lynx = "4.2.0"
|
|
29
|
+
|
|
30
|
+
dependencies {
|
|
31
|
+
compileOnly("org.lynxsdk.lynx:lynx:$lynx")
|
|
32
|
+
compileOnly("org.lynxsdk.lynx:service-api:$lynx")
|
|
33
|
+
kapt("org.lynxsdk.lynx:lynx-processor:$lynx")
|
|
34
|
+
// The generated spec imports androidx.annotation and lynx-processor's output uses @Keep, but Lynx's POMs declare it
|
|
35
|
+
// at runtime scope only: without this line kapt fails in the host ("cannot access Keep").
|
|
36
|
+
compileOnly("androidx.annotation:annotation:1.9.1")
|
|
37
|
+
// The microphone permission goes through the host activity's ActivityResultRegistry (every ComponentActivity has one).
|
|
38
|
+
compileOnly("androidx.activity:activity:1.10.1")
|
|
39
|
+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
|
|
40
|
+
// The range PUT; a host passes its own client (G9AppDiagnosticHost.httpClient) so its trust and proxy rules apply.
|
|
41
|
+
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
|
42
|
+
|
|
43
|
+
// The JVM suite (../jvm) also runs as Android local unit tests.
|
|
44
|
+
testImplementation("junit:junit:4.13.2")
|
|
45
|
+
}
|
|
@@ -1,129 +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 =
|
|
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
|
-
}
|
|
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
|
+
}
|