ReviewOS

also looking at this

craft-native/craft

feat(android): the reply channel, so async actions become reachable

#152
Merged chris wants to merge feat/android-reply-channel into main
5 files +464 -0
packages/android/templates/CraftNative.kt.templatemodified+33-0
Changes to packages/android/templates/CraftNative.kt.template
@@ -58,6 +58,39 @@ object CraftNative {
5858 false
5959 }
6060
61 /**
62 * How Zig reaches the page.
63 *
64 * Zig cannot make a `Runnable`, and `evaluateJavascript` is main-thread
65 * only — JNI can only produce a `Runnable` by registering natives on a
66 * class that already exists in the APK, which means shipping Kotlin to
67 * avoid writing Kotlin. So the hop stays here: Zig owns what to say, this
68 * owns which thread says it.
69 *
70 * Installed by `CraftBridge` once its WebView exists, and `@Volatile`
71 * because Zig calls `deliver` from whatever thread a device callback
72 * arrives on.
73 */
74 @Volatile
75 private var deliverer: ((String) -> Unit)? = null
76
77 fun setDeliverer(deliver: ((String) -> Unit)?) {
78 deliverer = deliver
79 }
80
81 /**
82 * Run `script` in the page. Called from Zig, on any thread.
83 *
84 * Silent when no deliverer is installed — which is every moment before the
85 * WebView exists, and after it is torn down. A throw here would cross back
86 * into Zig as a pending exception on a thread that has nowhere to report
87 * it, and the caller already counts what it could not deliver.
88 */
89 @JvmStatic
90 fun deliver(script: String) {
91 deliverer?.invoke(script)
92 }
93
6194 private external fun nativeGetDeviceInfo(activity: Activity): String?
6295 private external fun nativeGetMemoryUsage(): String?
6396 private external fun nativeLog(message: String): Boolean