@g9tm/app-diagnostic-lynx 0.3.1 → 0.5.0

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.
@@ -30,4 +30,4 @@ with the command that proved it and the date. Nothing here is claimed from a Nod
30
30
  | What | Why it matters | How it will be proven |
31
31
  | --- | --- | --- |
32
32
  | **N-AD-01b** The same on a phone (the gesture with real fingers, the microphone with a real voice) | An emulator injects touches into the decor view and records silence | A Didban session on a lab phone (P7.7) |
33
- | **N-AD-02** iOS (`ios/src`) | Not written yet in this package: the app's `AGMedia.mm` / `AGFiles.mm` move here with neutral names | A build on the Mac through a Didban session (the Mac is reserved for Didban) |
33
+ | **N-AD-02** iOS (`ios/src`) | Written and moved here on 2026-11-20 (0.5.0) from the first consumer's own iOS media and file code, with neutral names (`G9AppDiagnosticModule`, `G9AppDiagnosticHost`, `G9DiagnosticRecorders`); the same files serve the Capacitor and Tauri plugins. Not built or run yet | A build of the host and of `samples/tauri` on the Mac, then a tester report from an iPhone, through a Didban session (the Mac is reserved for Didban) |
package/README.md CHANGED
@@ -1,42 +1,63 @@
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
+ ## iOS (0.5.0)
31
+
32
+ `pod 'G9AppDiagnosticLynx', :path => '../node_modules/@g9tm/app-diagnostic-lynx/ios'`, then register the module with the
33
+ Lynx config and set the hooks before the first call:
34
+
35
+ ```objc
36
+ #import "G9AppDiagnosticModule.h"
37
+ G9AppDiagnosticRegister(config); // the LynxConfig your host builds
38
+ G9AppDiagnosticHost.allowedRoots = ^{ return @[NSHomeDirectory()]; };
39
+ G9AppDiagnosticHost.isExcludedView = ^BOOL(UIView *v) { return [v isKindOfClass:MapHostView.class]; };
40
+ G9AppDiagnosticHost.urlSession = ^{ return mySession; }; // its trust rules for the range PUT
41
+ ```
42
+
43
+ The same methods as Android; iOS has no public way to inject a finger, so a remote `touch` is a logical tap (a control's
44
+ action or an accessibility activation).
45
+
46
+ ## A default panel
47
+
48
+ `import { TesterPanel } from '@g9tm/app-diagnostic-lynx/ui'` (`@lynx-js/react`, an optional peer): the pill, the report form
49
+ and the report list over the engine. An app with a look of its own keeps drawing its own over the engine's state.
50
+
51
+ ## JS
52
+
53
+ ```ts
54
+ import { createDiagnosticNative, nativeSendRange, onDiagnosticTouch } from '@g9tm/app-diagnostic-lynx';
55
+ const native = createDiagnosticNative();
56
+ await native.watchTouches({ taps: true, touches: false, consume: true, excludeSurfaces: false });
57
+ const off = onDiagnosticTouch(lynx.getJSModule('GlobalEventEmitter'), (e) => pair(e));
58
+ ```
59
+
60
+ ## Checks
61
+
62
+ `npm test` (JS face) · `npm run test:android-jvm` (the gesture's decisions) · `npm run check:android-module` (the module
63
+ 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,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
+ }
@@ -6,10 +6,9 @@ import android.content.Context
6
6
  import android.content.ContextWrapper
7
7
  import android.os.Bundle
8
8
  import android.view.View
9
- import com.lynx.react.bridge.JavaOnlyArray
10
- import com.lynx.tasm.behavior.LynxContext
11
9
  import java.io.File
12
10
  import java.lang.ref.WeakReference
11
+ import java.util.concurrent.CopyOnWriteArrayList
13
12
  import kotlinx.coroutines.Dispatchers
14
13
  import kotlinx.coroutines.delay
15
14
  import kotlinx.coroutines.withContext
@@ -28,7 +27,8 @@ class G9DiagnosticException(val code: String, message: String, cause: Throwable?
28
27
  * - [isExcludedView]: a surface the gesture leaves native in the «except the map» mode (the host's map view). Default: none.
29
28
  *
30
29
  * The current activity is tracked here (the recorders draw it, the gesture watches its window); the module's events go to
31
- * every live Lynx context.
30
+ * every sink a bridge added — Lynx's contexts (LynxBridge.kt), a Capacitor plugin's listeners (0.5.0). This file and the
31
+ * recorders it serves know no bridge: @g9tm/app-diagnostic-capacitor compiles them as they are.
32
32
  */
33
33
  object G9AppDiagnosticHost {
34
34
  @Volatile var httpClient: () -> OkHttpClient = { defaultClient }
@@ -41,17 +41,18 @@ object G9AppDiagnosticHost {
41
41
 
42
42
  @Volatile private var application: Application? = null
43
43
  @Volatile private var current: WeakReference<Activity>? = null
44
- private val contexts = ArrayList<WeakReference<LynxContext>>()
44
+ private val sinks = CopyOnWriteArrayList<(String, String) -> Unit>()
45
45
 
46
- /** Called by every module instance: the application, its activity and its Lynx context. Idempotent. */
47
- internal fun attach(lynx: LynxContext) {
48
- synchronized(contexts) {
49
- contexts.removeAll { it.get() == null || it.get() === lynx }
50
- contexts += WeakReference(lynx)
51
- }
52
- activityOf(lynx)?.let { if (!it.isFinishing) current = WeakReference(it) }
46
+ /** Where the events go (a bridge's own way to the JS side). Returns how to take it away again. */
47
+ fun addSink(sink: (name: String, json: String) -> Unit): () -> Unit {
48
+ sinks += sink
49
+ return { sinks -= sink }
50
+ }
51
+
52
+ /** Called by every bridge's module: the application and the activity it lives in. Idempotent. */
53
+ fun attach(app: Application, activity: Activity?) {
54
+ activity?.let { if (!it.isFinishing) current = WeakReference(it) }
53
55
  if (application != null) return
54
- val app = lynx.applicationContext as? Application ?: return
55
56
  synchronized(this) {
56
57
  if (application != null) return
57
58
  application = app
@@ -102,26 +103,25 @@ object G9AppDiagnosticHost {
102
103
  return file
103
104
  }
104
105
 
105
- /** A global event to every live Lynx context (JSON text, the one argument). */
106
+ /** An event to every sink (JSON text): a sink's own failure never stops the next. */
106
107
  internal fun emit(name: String, json: String) {
107
- val targets = synchronized(contexts) { contexts.mapNotNull { it.get() } }
108
- for (lynx in targets) {
108
+ for (sink in sinks) {
109
109
  try {
110
- lynx.sendGlobalEvent(name, JavaOnlyArray().apply { pushString(json) })
110
+ sink(name, json)
111
111
  } catch (_: Throwable) {
112
- // A context being torn down: the next one still gets it.
112
+ // A bridge being torn down: the next one still gets it.
113
113
  }
114
114
  }
115
115
  }
116
116
 
117
117
  internal suspend fun <T> onMain(work: () -> T): T = withContext(Dispatchers.Main) { work() }
118
118
 
119
- private fun activityOf(context: Context?): Activity? {
119
+ /** The activity a context wraps (a LynxContext, a themed wrapper), or null. */
120
+ fun activityOf(context: Context?): Activity? {
120
121
  var c = context
121
122
  repeat(10) {
122
123
  when (c) {
123
124
  is Activity -> return c as Activity
124
- is LynxContext -> c = (c as LynxContext).baseContext
125
125
  is ContextWrapper -> c = (c as ContextWrapper).baseContext
126
126
  else -> return null
127
127
  }
@@ -43,7 +43,7 @@ class G9AppDiagnosticModule(private val lynx: LynxContext) : G9AppDiagnosticModu
43
43
  private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
44
44
 
45
45
  init {
46
- G9AppDiagnosticHost.attach(lynx)
46
+ LynxBridge.attach(lynx)
47
47
  }
48
48
 
49
49
  private fun respond(callback: Callback?, work: suspend () -> Any?) {
@@ -200,7 +200,7 @@ class G9AppDiagnosticModule(private val lynx: LynxContext) : G9AppDiagnosticModu
200
200
  }
201
201
 
202
202
  companion object {
203
- const val VERSION = "0.1.0"
203
+ const val VERSION = "0.5.0"
204
204
  private val nextKey = AtomicInteger()
205
205
 
206
206
  internal fun toMap(value: Map<*, *>): JavaOnlyMap {
@@ -0,0 +1,41 @@
1
+ package com.g9tm.appdiagnostic
2
+
3
+ import android.app.Application
4
+ import com.lynx.react.bridge.JavaOnlyArray
5
+ import com.lynx.tasm.behavior.LynxContext
6
+ import java.lang.ref.WeakReference
7
+
8
+ /**
9
+ * The Lynx side of G9AppDiagnosticHost (0.5.0 — split out so the recorders compile without Lynx): every module instance
10
+ * attaches its LynxContext here, and the host's events reach every live context as a global event (JSON text, the one
11
+ * argument).
12
+ */
13
+ internal object LynxBridge {
14
+ private val contexts = ArrayList<WeakReference<LynxContext>>()
15
+ @Volatile private var sinkAdded = false
16
+
17
+ fun attach(lynx: LynxContext) {
18
+ synchronized(contexts) {
19
+ contexts.removeAll { it.get() == null || it.get() === lynx }
20
+ contexts += WeakReference(lynx)
21
+ }
22
+ (lynx.applicationContext as? Application)?.let { G9AppDiagnosticHost.attach(it, G9AppDiagnosticHost.activityOf(lynx)) }
23
+ if (sinkAdded) return
24
+ synchronized(this) {
25
+ if (sinkAdded) return
26
+ sinkAdded = true
27
+ G9AppDiagnosticHost.addSink { name, json -> emit(name, json) }
28
+ }
29
+ }
30
+
31
+ private fun emit(name: String, json: String) {
32
+ val targets = synchronized(contexts) { contexts.mapNotNull { it.get() } }
33
+ for (lynx in targets) {
34
+ try {
35
+ lynx.sendGlobalEvent(name, JavaOnlyArray().apply { pushString(json) })
36
+ } catch (_: Throwable) {
37
+ // A context being torn down: the next one still gets it.
38
+ }
39
+ }
40
+ }
41
+ }
@@ -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
+ }