@g9tm/app-diagnostic-lynx 0.0.0-stage → 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.
- package/Native-Validation-Pending.md +33 -0
- package/README.md +42 -3
- package/android/build.gradle.kts +45 -0
- package/android/consumer-rules.pro +2 -0
- package/android/src/main/AndroidManifest.xml +5 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/G9AppDiagnosticHost.kt +131 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/G9AppDiagnosticModule.kt +224 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/RemoteTouch.kt +67 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/ScreenRecorder.kt +250 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/TouchWatcher.kt +126 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/VoiceRecorder.kt +82 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/WindowCapture.kt +92 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/core/TouchCore.kt +175 -0
- package/android/src/main/java/com/g9tm/appdiagnostic/generated/G9AppDiagnosticModuleSpec.java +45 -0
- package/generated/G9AppDiagnosticModule.ts +70 -0
- package/lynx.lib.json +12 -0
- package/package.json +39 -6
- package/src/index.ts +157 -0
- package/types/g9-app-diagnostic-module.d.ts +57 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Native validation — what is proven, what is pending
|
|
2
|
+
|
|
3
|
+
The ledger of `@g9tm/app-diagnostic-lynx`'s native code (the `G9AppDiagnosticModule` module). A row moves to **proven** only
|
|
4
|
+
with the command that proved it and the date. Nothing here is claimed from a Node test.
|
|
5
|
+
|
|
6
|
+
## Proven (2026-11-02, Windows 11, Node 24.12, JDK 21 (Android Studio jbr), Gradle 8.14.3)
|
|
7
|
+
|
|
8
|
+
| What | How | Result |
|
|
9
|
+
| --- | --- | --- |
|
|
10
|
+
| The JS face: envelopes → values, error codes kept, a missing method is `unavailable`, the range request as JSON, the touch events parsed | `npm test` (`test/native.test.ts`, a fake module of the contract's shape) | 3/3 |
|
|
11
|
+
| The gesture's decisions (`android/…/core/TouchCore.kt`): taps, first touches, the hold at a quick second finger (E-20), the replay of a pinch or pan, a late finger, an excluded surface, a system cancel, a throwing hit test | `npm run test:android-jvm` | 8/8 |
|
|
12
|
+
| The Android module against the Lynx 4.1.0 classes, the Autolink provider | `npm run check:android-module` (kapt + lynx-processor, Robolectric android-all, androidx.activity stubs) | compiles; `LynxLibraryProviderImpl` registers `G9AppDiagnosticModule` |
|
|
13
|
+
| Every `@LynxMethod` signature is one Lynx's Android bridge accepts | `npm run check:android-module` → `LynxBridgeSignatureTest`; `node scripts/android-bridge-types.mjs` | 9/9 methods (`v.X`, `v.TX`, `v.TdX`, `v.TddX`, `v.zzzzX`). It caught overrides without `@LynxMethod` — the module would have reached JS with no methods |
|
|
14
|
+
|
|
15
|
+
## Proven on the emulator (2026-11-02, Pixel 9 Pro XL AVD, Android 16 / API 36, x86_64, Lynx 4.1.0, the first consumer's host)
|
|
16
|
+
|
|
17
|
+
| What | How | Result |
|
|
18
|
+
| --- | --- | --- |
|
|
19
|
+
| **N-AD-01** Autolink: the module in a real host's registry | the host's generated `LynxAutolinkGenerated` | lists `com.g9tm.appdiagnostic.LynxLibraryProviderImpl`; JS sees the module (`capabilities` answers) |
|
|
20
|
+
| The gesture HELD (E-20) | a two-finger double tap injected into the decor view (the input system's entry point) over a tab button | the gesture fired (screenshot attached) and the tab did NOT switch; one finger on the same tab switches it |
|
|
21
|
+
| The excluded surface | the same over the map in the «except the map» mode / in the «any screen» mode | excluded: the map zoomed out natively, no gesture; not excluded: no zoom-out, the gesture fired |
|
|
22
|
+
| Screen recording | 4 s with three taps | an MP4 with `ftyp`, `moov`, `mdat` (finished); the timeline holds the three first-finger downs at the right window fractions |
|
|
23
|
+
| Voice | two notes (permission granted beforehand) | two `.m4a` of ~79 KB |
|
|
24
|
+
| Window screenshot | the gesture's shot | a 363 KB JPEG of the window, the map included |
|
|
25
|
+
| `sendRange` on the host's client | a whole tester report through a local fake gateway over HTTPS (self-signed, trusted only by the host's dev-host trust) | 10 PUTs of 64 KB read natively; a dropped answer → retry → 416 → resumed at the gateway's offset; the hash matched at finalize. The host hook `httpClient` is what made the TLS trust apply |
|
|
26
|
+
|
|
27
|
+
## Pending
|
|
28
|
+
|
|
29
|
+
| What | Why it matters | How it will be proven |
|
|
30
|
+
| --- | --- | --- |
|
|
31
|
+
| **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) |
|
|
32
|
+
| **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-03** The gesture's pinch replay on a real map (MapLibre) | A replay a slop late must still zoom | Emulator with two-finger input, then a phone |
|
package/README.md
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
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 and first touches as the global event `g9:diagnostic-touch`; with `consume`, a two-finger touch is held away from the views and replayed when it is a pinch or a pan (E-20) |
|
|
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).
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
package com.g9tm.appdiagnostic
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.app.Application
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.content.ContextWrapper
|
|
7
|
+
import android.os.Bundle
|
|
8
|
+
import android.view.View
|
|
9
|
+
import com.lynx.react.bridge.JavaOnlyArray
|
|
10
|
+
import com.lynx.tasm.behavior.LynxContext
|
|
11
|
+
import java.io.File
|
|
12
|
+
import java.lang.ref.WeakReference
|
|
13
|
+
import kotlinx.coroutines.Dispatchers
|
|
14
|
+
import kotlinx.coroutines.delay
|
|
15
|
+
import kotlinx.coroutines.withContext
|
|
16
|
+
import okhttp3.OkHttpClient
|
|
17
|
+
|
|
18
|
+
/** A failure with a code from the module's contract (types/g9-app-diagnostic-module.d.ts). */
|
|
19
|
+
class G9DiagnosticException(val code: String, message: String, cause: Throwable? = null) : Exception(message, cause)
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What the package needs from the app that hosts it, and the few hooks a host may set — before the first call, typically in
|
|
23
|
+
* `Application.onCreate`. Nothing here names a product: every default is neutral.
|
|
24
|
+
*
|
|
25
|
+
* - [httpClient]: the client a range request goes out on. The host's own, so its trust (a development certificate) and its
|
|
26
|
+
* proxy rules are the ones every other request has. Default: a plain client.
|
|
27
|
+
* - [allowedRoots]: the folders a recording, a screenshot or a sent file may be in. Default: the app's files and cache.
|
|
28
|
+
* - [isExcludedView]: a surface the gesture leaves native in the «except the map» mode (the host's map view). Default: none.
|
|
29
|
+
*
|
|
30
|
+
* 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.
|
|
32
|
+
*/
|
|
33
|
+
object G9AppDiagnosticHost {
|
|
34
|
+
@Volatile var httpClient: () -> OkHttpClient = { defaultClient }
|
|
35
|
+
|
|
36
|
+
@Volatile var allowedRoots: (Context) -> List<File> = { listOf(it.filesDir, it.cacheDir) }
|
|
37
|
+
|
|
38
|
+
@Volatile var isExcludedView: (View) -> Boolean = { false }
|
|
39
|
+
|
|
40
|
+
private val defaultClient by lazy { OkHttpClient() }
|
|
41
|
+
|
|
42
|
+
@Volatile private var application: Application? = null
|
|
43
|
+
@Volatile private var current: WeakReference<Activity>? = null
|
|
44
|
+
private val contexts = ArrayList<WeakReference<LynxContext>>()
|
|
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) }
|
|
53
|
+
if (application != null) return
|
|
54
|
+
val app = lynx.applicationContext as? Application ?: return
|
|
55
|
+
synchronized(this) {
|
|
56
|
+
if (application != null) return
|
|
57
|
+
application = app
|
|
58
|
+
app.registerActivityLifecycleCallbacks(
|
|
59
|
+
object : Application.ActivityLifecycleCallbacks {
|
|
60
|
+
override fun onActivityResumed(activity: Activity) {
|
|
61
|
+
current = WeakReference(activity)
|
|
62
|
+
TouchWatcher.attach(activity)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
override fun onActivityDestroyed(activity: Activity) {
|
|
66
|
+
if (current?.get() === activity) current = null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
|
|
70
|
+
|
|
71
|
+
override fun onActivityStarted(activity: Activity) {}
|
|
72
|
+
|
|
73
|
+
override fun onActivityPaused(activity: Activity) {}
|
|
74
|
+
|
|
75
|
+
override fun onActivityStopped(activity: Activity) {}
|
|
76
|
+
|
|
77
|
+
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
|
|
78
|
+
},
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
internal val app: Application
|
|
84
|
+
get() = application ?: throw G9DiagnosticException("unavailable", "The module is not attached to an application")
|
|
85
|
+
|
|
86
|
+
internal fun activityNow(): Activity? = current?.get()?.takeIf { !it.isFinishing && !it.isDestroyed }
|
|
87
|
+
|
|
88
|
+
/** The activity on screen, waiting a few seconds for one (an app coming back to the foreground). */
|
|
89
|
+
internal suspend fun awaitActivity(): Activity {
|
|
90
|
+
repeat(50) {
|
|
91
|
+
activityNow()?.let { return it }
|
|
92
|
+
delay(100)
|
|
93
|
+
}
|
|
94
|
+
throw G9DiagnosticException("unavailable", "No activity on screen")
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A folder or file the caller named, if it is inside the folders the host allows. */
|
|
98
|
+
internal fun inside(path: String): File {
|
|
99
|
+
val file = File(path).canonicalFile
|
|
100
|
+
val roots = allowedRoots(app).map { it.canonicalPath.trimEnd('/') }
|
|
101
|
+
if (roots.none { file.path.startsWith("$it/") }) throw G9DiagnosticException("invalid", "Outside the app folders: $path")
|
|
102
|
+
return file
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A global event to every live Lynx context (JSON text, the one argument). */
|
|
106
|
+
internal fun emit(name: String, json: String) {
|
|
107
|
+
val targets = synchronized(contexts) { contexts.mapNotNull { it.get() } }
|
|
108
|
+
for (lynx in targets) {
|
|
109
|
+
try {
|
|
110
|
+
lynx.sendGlobalEvent(name, JavaOnlyArray().apply { pushString(json) })
|
|
111
|
+
} catch (_: Throwable) {
|
|
112
|
+
// A context being torn down: the next one still gets it.
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
internal suspend fun <T> onMain(work: () -> T): T = withContext(Dispatchers.Main) { work() }
|
|
118
|
+
|
|
119
|
+
private fun activityOf(context: Context?): Activity? {
|
|
120
|
+
var c = context
|
|
121
|
+
repeat(10) {
|
|
122
|
+
when (c) {
|
|
123
|
+
is Activity -> return c as Activity
|
|
124
|
+
is LynxContext -> c = (c as LynxContext).baseContext
|
|
125
|
+
is ContextWrapper -> c = (c as ContextWrapper).baseContext
|
|
126
|
+
else -> return null
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return null
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
package com.g9tm.appdiagnostic
|
|
2
|
+
|
|
3
|
+
import android.Manifest
|
|
4
|
+
import android.app.Activity
|
|
5
|
+
import android.content.pm.PackageManager
|
|
6
|
+
import android.graphics.Bitmap
|
|
7
|
+
import androidx.activity.result.ActivityResultLauncher
|
|
8
|
+
import androidx.activity.result.ActivityResultRegistryOwner
|
|
9
|
+
import androidx.activity.result.contract.ActivityResultContracts
|
|
10
|
+
import com.g9tm.appdiagnostic.generated.G9AppDiagnosticModuleSpec
|
|
11
|
+
import com.lynx.jsbridge.LynxMethod
|
|
12
|
+
import com.lynx.jsbridge.LynxNativeModule
|
|
13
|
+
import com.lynx.react.bridge.Callback
|
|
14
|
+
import com.lynx.react.bridge.JavaOnlyArray
|
|
15
|
+
import com.lynx.react.bridge.JavaOnlyMap
|
|
16
|
+
import com.lynx.tasm.behavior.LynxContext
|
|
17
|
+
import java.io.File
|
|
18
|
+
import java.io.FileOutputStream
|
|
19
|
+
import java.io.RandomAccessFile
|
|
20
|
+
import java.util.concurrent.TimeUnit
|
|
21
|
+
import java.util.concurrent.atomic.AtomicInteger
|
|
22
|
+
import kotlin.coroutines.resume
|
|
23
|
+
import kotlinx.coroutines.CoroutineScope
|
|
24
|
+
import kotlinx.coroutines.Dispatchers
|
|
25
|
+
import kotlinx.coroutines.SupervisorJob
|
|
26
|
+
import kotlinx.coroutines.launch
|
|
27
|
+
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
28
|
+
import okhttp3.MediaType.Companion.toMediaType
|
|
29
|
+
import okhttp3.Request
|
|
30
|
+
import okhttp3.RequestBody
|
|
31
|
+
import okio.BufferedSink
|
|
32
|
+
import org.json.JSONObject
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* `G9AppDiagnosticModule` (types/g9-app-diagnostic-module.d.ts): the native half of the tester mode and the live channel —
|
|
36
|
+
* the app's own window as an MP4, a spoken note, the window screenshot, a file range sent natively, and the tester gesture.
|
|
37
|
+
* Every reply is `{ok: true, value}` or `{ok: false, error: {code, message}}`; work runs off the JS thread.
|
|
38
|
+
*
|
|
39
|
+
* TWIN: ios/src/G9AppDiagnosticModule.m.
|
|
40
|
+
*/
|
|
41
|
+
@LynxNativeModule(name = "G9AppDiagnosticModule")
|
|
42
|
+
class G9AppDiagnosticModule(private val lynx: LynxContext) : G9AppDiagnosticModuleSpec(lynx) {
|
|
43
|
+
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
|
44
|
+
|
|
45
|
+
init {
|
|
46
|
+
G9AppDiagnosticHost.attach(lynx)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private fun respond(callback: Callback?, work: suspend () -> Any?) {
|
|
50
|
+
scope.launch {
|
|
51
|
+
val reply =
|
|
52
|
+
try {
|
|
53
|
+
mapOf("ok" to true, "value" to work())
|
|
54
|
+
} catch (e: G9DiagnosticException) {
|
|
55
|
+
mapOf("ok" to false, "error" to mapOf("code" to e.code, "message" to (e.message ?: e.code)))
|
|
56
|
+
} catch (e: Throwable) {
|
|
57
|
+
mapOf("ok" to false, "error" to mapOf("code" to "io", "message" to (e.message ?: e.javaClass.simpleName)))
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
callback?.invoke(toMap(reply))
|
|
61
|
+
} catch (_: Throwable) {
|
|
62
|
+
// the page went away
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@LynxMethod
|
|
68
|
+
override fun capabilities(callback: Callback?) =
|
|
69
|
+
respond(callback) {
|
|
70
|
+
mapOf("version" to VERSION, "platform" to "android", "screen" to ScreenRecorder.supported(), "voice" to VoiceRecorder.supported(), "consume" to true)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@LynxMethod
|
|
74
|
+
override fun state(callback: Callback?) = respond(callback) { mapOf("screen" to ScreenRecorder.isRecording, "voice" to VoiceRecorder.isRecording) }
|
|
75
|
+
|
|
76
|
+
@LynxMethod
|
|
77
|
+
override fun startScreen(dir: String?, maxFps: Double, callback: Callback?) =
|
|
78
|
+
respond(callback) {
|
|
79
|
+
ScreenRecorder.start(G9AppDiagnosticHost.inside(dir.orEmpty()), maxFps.toInt())
|
|
80
|
+
null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@LynxMethod
|
|
84
|
+
override fun stopScreen(callback: Callback?) =
|
|
85
|
+
respond(callback) {
|
|
86
|
+
ScreenRecorder.stop()?.let { mapOf("path" to it.path, "bytes" to it.bytes, "durationMs" to it.durationMs, "width" to it.width, "height" to it.height, "capped" to it.capped) }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@LynxMethod
|
|
90
|
+
override fun startVoice(dir: String?, callback: Callback?) =
|
|
91
|
+
respond(callback) {
|
|
92
|
+
val folder = G9AppDiagnosticHost.inside(dir.orEmpty())
|
|
93
|
+
if (!microphone()) throw G9DiagnosticException("permission", "The microphone permission was refused")
|
|
94
|
+
VoiceRecorder.start(folder)
|
|
95
|
+
null
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
@LynxMethod
|
|
99
|
+
override fun stopVoice(callback: Callback?) = respond(callback) { VoiceRecorder.stop()?.let { mapOf("path" to it.path, "bytes" to it.bytes, "durationMs" to it.durationMs) } }
|
|
100
|
+
|
|
101
|
+
@LynxMethod
|
|
102
|
+
override fun watchTouches(taps: Boolean, touches: Boolean, consume: Boolean, excludeSurfaces: Boolean, callback: Callback?) =
|
|
103
|
+
respond(callback) {
|
|
104
|
+
G9AppDiagnosticHost.onMain { TouchWatcher.configure(G9AppDiagnosticHost.activityNow(), taps, touches, consume, excludeSurfaces) }
|
|
105
|
+
null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@LynxMethod
|
|
109
|
+
override fun screenshot(dir: String?, maxEdge: Double, quality: Double, callback: Callback?) =
|
|
110
|
+
respond(callback) {
|
|
111
|
+
val folder = G9AppDiagnosticHost.inside(dir.orEmpty()).apply { mkdirs() }
|
|
112
|
+
val activity = G9AppDiagnosticHost.awaitActivity()
|
|
113
|
+
val (width, height) = WindowCapture.windowSize(activity)
|
|
114
|
+
if (width <= 0 || height <= 0) throw G9DiagnosticException("unavailable", "The window is not laid out")
|
|
115
|
+
val edge = maxEdge.toInt().coerceIn(64, 4096)
|
|
116
|
+
val scale = minOf(1.0, edge.toDouble() / maxOf(width, height))
|
|
117
|
+
val out = WindowCapture.capture(activity, (width * scale).toInt().coerceAtLeast(1), (height * scale).toInt().coerceAtLeast(1))
|
|
118
|
+
val file = File(folder, "screen-${System.currentTimeMillis()}.jpg")
|
|
119
|
+
FileOutputStream(file).use { out.compress(Bitmap.CompressFormat.JPEG, quality.toInt().coerceIn(10, 100), it) }
|
|
120
|
+
val result = mapOf("path" to file.path, "width" to out.width, "height" to out.height, "bytes" to file.length())
|
|
121
|
+
out.recycle()
|
|
122
|
+
result
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* One HTTP request whose body is a byte range of a file (the tester report's resumable PUT: a chunk of a 100 MB archive
|
|
127
|
+
* never crosses the bridge as base64), on the host's client. Any HTTP status resolves; only a transport failure rejects.
|
|
128
|
+
*/
|
|
129
|
+
@LynxMethod
|
|
130
|
+
override fun sendRange(requestJson: String?, callback: Callback?) =
|
|
131
|
+
respond(callback) {
|
|
132
|
+
val r = JSONObject(requestJson.orEmpty())
|
|
133
|
+
val file = G9AppDiagnosticHost.inside(r.optString("path"))
|
|
134
|
+
if (!file.isFile) throw G9DiagnosticException("not-found", "No such file: ${file.path}")
|
|
135
|
+
val offset = r.optLong("offset", 0L).coerceAtLeast(0L)
|
|
136
|
+
val length = r.optLong("length", file.length() - offset).coerceIn(0L, (file.length() - offset).coerceAtLeast(0L))
|
|
137
|
+
val type = r.optString("contentType").ifEmpty { "application/octet-stream" }.toMediaType()
|
|
138
|
+
val body =
|
|
139
|
+
object : RequestBody() {
|
|
140
|
+
override fun contentType() = type
|
|
141
|
+
|
|
142
|
+
override fun contentLength() = length
|
|
143
|
+
|
|
144
|
+
override fun writeTo(sink: BufferedSink) {
|
|
145
|
+
RandomAccessFile(file, "r").use { raf ->
|
|
146
|
+
raf.seek(offset)
|
|
147
|
+
val buffer = ByteArray(64 * 1024)
|
|
148
|
+
var left = length
|
|
149
|
+
while (left > 0) {
|
|
150
|
+
val n = raf.read(buffer, 0, minOf(buffer.size.toLong(), left).toInt())
|
|
151
|
+
if (n < 0) break
|
|
152
|
+
sink.write(buffer, 0, n)
|
|
153
|
+
left -= n
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
val builder = Request.Builder().url(r.getString("url")).method(r.optString("method", "PUT"), body)
|
|
159
|
+
r.optJSONObject("headers")?.let { h -> for (key in h.keys()) builder.header(key, h.getString(key)) }
|
|
160
|
+
val timeoutMs = r.optLong("timeoutMs", 180_000L).coerceIn(1_000L, 900_000L)
|
|
161
|
+
val client = G9AppDiagnosticHost.httpClient().newBuilder().callTimeout(timeoutMs, TimeUnit.MILLISECONDS).build()
|
|
162
|
+
try {
|
|
163
|
+
client.newCall(builder.build()).execute().use { response ->
|
|
164
|
+
mapOf("status" to response.code, "body" to (response.body?.string() ?: ""), "headers" to response.headers.toMultimap().mapValues { it.value.joinToString(",") })
|
|
165
|
+
}
|
|
166
|
+
} catch (e: java.io.InterruptedIOException) {
|
|
167
|
+
throw G9DiagnosticException("timeout", e.message ?: "timed out", e)
|
|
168
|
+
} catch (e: java.io.IOException) {
|
|
169
|
+
throw G9DiagnosticException("network", e.message ?: "network error", e)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A real touch for the operator's remote control (RemoteTouch, E-13). */
|
|
174
|
+
@LynxMethod
|
|
175
|
+
override fun touch(kind: String?, x: Double, y: Double, toX: Double, toY: Double, durationMs: Double, callback: Callback?) =
|
|
176
|
+
respond(callback) { mapOf("handled" to RemoteTouch.perform(kind.orEmpty(), x, y, toX, toY, durationMs.toLong())) }
|
|
177
|
+
|
|
178
|
+
/** RECORD_AUDIO, asked through the activity's result registry the first time; true when granted. */
|
|
179
|
+
private suspend fun microphone(): Boolean {
|
|
180
|
+
val app = G9AppDiagnosticHost.app
|
|
181
|
+
if (app.checkSelfPermission(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) return true
|
|
182
|
+
val host: Activity = G9AppDiagnosticHost.awaitActivity()
|
|
183
|
+
if (host !is ActivityResultRegistryOwner || host.isFinishing) return false
|
|
184
|
+
return suspendCancellableCoroutine { cont ->
|
|
185
|
+
host.runOnUiThread {
|
|
186
|
+
try {
|
|
187
|
+
var launcher: ActivityResultLauncher<String>? = null
|
|
188
|
+
launcher =
|
|
189
|
+
host.activityResultRegistry.register("g9.diagnostic.mic." + nextKey.incrementAndGet(), ActivityResultContracts.RequestPermission()) { granted ->
|
|
190
|
+
launcher?.unregister()
|
|
191
|
+
if (cont.isActive) cont.resume(granted)
|
|
192
|
+
}
|
|
193
|
+
launcher.launch(Manifest.permission.RECORD_AUDIO)
|
|
194
|
+
} catch (_: Throwable) {
|
|
195
|
+
// No androidx.activity in this host, or the registry refused: say what is true now rather than fail.
|
|
196
|
+
if (cont.isActive) cont.resume(app.checkSelfPermission(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
companion object {
|
|
203
|
+
const val VERSION = "0.1.0"
|
|
204
|
+
private val nextKey = AtomicInteger()
|
|
205
|
+
|
|
206
|
+
internal fun toMap(value: Map<*, *>): JavaOnlyMap {
|
|
207
|
+
val out = JavaOnlyMap()
|
|
208
|
+
for ((k, v) in value) put(out, k as String, v)
|
|
209
|
+
return out
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private fun put(out: JavaOnlyMap, key: String, v: Any?) {
|
|
213
|
+
when (v) {
|
|
214
|
+
null -> out.putNull(key)
|
|
215
|
+
is Boolean -> out.putBoolean(key, v)
|
|
216
|
+
is Number -> out.putDouble(key, v.toDouble())
|
|
217
|
+
is String -> out.putString(key, v)
|
|
218
|
+
is Map<*, *> -> out.putMap(key, toMap(v))
|
|
219
|
+
is List<*> -> out.putArray(key, JavaOnlyArray().apply { v.forEach { item -> if (item is Map<*, *>) pushMap(toMap(item)) else pushString(item?.toString()) } })
|
|
220
|
+
else -> out.putString(key, v.toString())
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
package com.g9tm.appdiagnostic
|
|
2
|
+
|
|
3
|
+
import android.os.SystemClock
|
|
4
|
+
import android.view.InputDevice
|
|
5
|
+
import android.view.MotionEvent
|
|
6
|
+
import android.view.View
|
|
7
|
+
import kotlinx.coroutines.Dispatchers
|
|
8
|
+
import kotlinx.coroutines.delay
|
|
9
|
+
import kotlinx.coroutines.withContext
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The operator's remote control as REAL touches (E-13): `MotionEvent`s with a finger's tool type and the touchscreen
|
|
13
|
+
* source, dispatched into the window's decor view — the input system's own entry point — with real time between DOWN,
|
|
14
|
+
* MOVEs and UP. Whatever is on top (a sheet, the map's own touch handling, a native view) gets it as it gets a finger, in
|
|
15
|
+
* every build; a logical tap (finding a JS handler by position) misses native views and gestures. The decor view hands the
|
|
16
|
+
* events to the window's callback first, so the tester gesture's watcher sees them too.
|
|
17
|
+
*
|
|
18
|
+
* Points are dp from the window's top-left. A separate window on top (a system permission dialog) is not in this tree and
|
|
19
|
+
* is not reached. The caller (the live channel) admits a remote command only with the user's consent.
|
|
20
|
+
*/
|
|
21
|
+
internal object RemoteTouch {
|
|
22
|
+
private const val FRAME_MS = 16L
|
|
23
|
+
|
|
24
|
+
suspend fun perform(kind: String, xDp: Double, yDp: Double, toXDp: Double, toYDp: Double, durationMs: Long): Boolean {
|
|
25
|
+
if (kind !in setOf("tap", "longPress", "drag")) throw G9DiagnosticException("invalid", "touch: kind is tap, longPress or drag (got '$kind')")
|
|
26
|
+
val activity = G9AppDiagnosticHost.awaitActivity()
|
|
27
|
+
return withContext(Dispatchers.Main) {
|
|
28
|
+
val root = activity.window.decorView
|
|
29
|
+
val density = activity.resources.displayMetrics.density
|
|
30
|
+
val x = (xDp * density).toFloat()
|
|
31
|
+
val y = (yDp * density).toFloat()
|
|
32
|
+
if (x < 0 || y < 0 || x >= root.width || y >= root.height) throw G9DiagnosticException("invalid", "touch: ($xDp, $yDp) dp is outside the window")
|
|
33
|
+
val hold = durationMs.coerceIn(1L, 10_000L)
|
|
34
|
+
val downTime = SystemClock.uptimeMillis()
|
|
35
|
+
val handled = dispatch(root, downTime, MotionEvent.ACTION_DOWN, x, y)
|
|
36
|
+
var upX = x
|
|
37
|
+
var upY = y
|
|
38
|
+
if (kind == "drag") {
|
|
39
|
+
val tx = (toXDp * density).toFloat()
|
|
40
|
+
val ty = (toYDp * density).toFloat()
|
|
41
|
+
val steps = (hold / FRAME_MS).coerceAtLeast(1)
|
|
42
|
+
for (i in 1..steps) {
|
|
43
|
+
delay(FRAME_MS)
|
|
44
|
+
val f = i.toFloat() / steps
|
|
45
|
+
dispatch(root, downTime, MotionEvent.ACTION_MOVE, x + (tx - x) * f, y + (ty - y) * f)
|
|
46
|
+
}
|
|
47
|
+
upX = tx
|
|
48
|
+
upY = ty
|
|
49
|
+
} else {
|
|
50
|
+
delay(hold)
|
|
51
|
+
}
|
|
52
|
+
dispatch(root, downTime, MotionEvent.ACTION_UP, upX, upY)
|
|
53
|
+
handled
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private fun dispatch(root: View, downTime: Long, action: Int, x: Float, y: Float): Boolean {
|
|
58
|
+
val props = arrayOf(MotionEvent.PointerProperties().apply { id = 0; toolType = MotionEvent.TOOL_TYPE_FINGER })
|
|
59
|
+
val coords = arrayOf(MotionEvent.PointerCoords().apply { this.x = x; this.y = y; pressure = 1f; size = 1f })
|
|
60
|
+
val event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), action, 1, props, coords, 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0)
|
|
61
|
+
return try {
|
|
62
|
+
root.dispatchTouchEvent(event)
|
|
63
|
+
} finally {
|
|
64
|
+
event.recycle()
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|