ReviewOS

also looking at this

craft-native/craft

feat(android): the app-state trio in Zig — ratchet 59 → 56

#184
Merged glennmichael123 wants to merge feat/android-app-state into main
6 files +422 -3
packages/zig/src/android_dispatch.zigmodified+97-1
Changes to packages/zig/src/android_dispatch.zig
@@ -42,6 +42,7 @@ const system = @import("bridge_android_system.zig");
4242const clipboard = @import("bridge_android_clipboard.zig");
4343const intents = @import("bridge_android_intents.zig");
4444const network = @import("bridge_android_network.zig");
45const appstate = @import("bridge_android_appstate.zig");
4546const securestore = @import("bridge_android_securestore.zig");
4647const haptics = @import("bridge_android_haptics.zig");
4748const notifications = @import("bridge_android_notifications.zig");
@@ -407,6 +408,29 @@ const natives = [_]jni.JNINativeMethod{
407408 .signature = "(Landroid/app/Activity;)Z",
408409 .fnPtr = @ptrCast(&nativeStopNetworkMonitoring),
409410 },
411 // Not an action: the other end of the lifecycle observer the holder owns.
412 // It takes no Activity, because everything it does is state and the reply
413 // channel — neither of which needs one.
414 .{
415 .name = "nativeAppStateEvent",
416 .signature = "(Ljava/lang/String;)V",
417 .fnPtr = @ptrCast(&nativeAppStateEvent),
418 },
419 .{
420 .name = "nativeStartAppStateMonitoring",
421 .signature = "(Landroid/app/Activity;)Z",
422 .fnPtr = @ptrCast(&nativeStartAppStateMonitoring),
423 },
424 .{
425 .name = "nativeStopAppStateMonitoring",
426 .signature = "(Landroid/app/Activity;)Z",
427 .fnPtr = @ptrCast(&nativeStopAppStateMonitoring),
428 },
429 .{
430 .name = "nativeGetAppState",
431 .signature = "()Ljava/lang/String;",
432 .fnPtr = @ptrCast(&nativeGetAppState),
433 },
410434};
411435
412436/// How binding went. A value rather than a log line, so every path is
@@ -1776,6 +1800,78 @@ fn callHolderVoid(j: Jni, comptime name: [:0]const u8, activity: jni.jobject) !v
17761800 );
17771801}
17781802
1803/// `nativeAppStateEvent(eventName)` — a lifecycle event, arrived at.
1804///
1805/// Runs on the main looper, where `ProcessLifecycleOwner` dispatches. Silent:
1806/// there is no promise waiting, and a page that never registered a callback is
1807/// the ordinary case rather than an error.
1808fn nativeAppStateEvent(
1809 env: jni.JNIEnv,
1810 _: jni.jobject,
1811 event_name: jni.jstring,
1812) callconv(.c) void {
1813 const j = Jni.init(env);
1814 var arena = std.heap.ArenaAllocator.init(backing);
1815 defer arena.deinit();
1816 const allocator = arena.allocator();
1817
1818 const name = j.stringToUtf8(allocator, event_name) catch return;
1819
1820 // Null covers both "this event carries no state" and "it carries the one
1821 // already held", and the shim announces neither.
1822 const changed = appstate.apply(name) orelse return;
1823 appstate.announce(allocator, changed) catch {};
1824}
1825
1826/// `nativeStartAppStateMonitoring(activity)`.
1827///
1828/// The observer is the holder's — `LifecycleEventObserver` is a Java interface
1829/// — and adding it twice is a no-op, because `LifecycleRegistry` keys its
1830/// observers by identity. The shim has the same property for the same reason,
1831/// so unlike the network watch there is nothing to leak here.
1832fn nativeStartAppStateMonitoring(
1833 env: jni.JNIEnv,
1834 _: jni.jobject,
1835 activity: jni.jobject,
1836) callconv(.c) jni.jboolean {
1837 const j = Jni.init(env);
1838
1839 callHolderVoid(j, "startAppStateWatch", activity) catch |err| {
1840 std.log.warn("craft: startAppStateMonitoring fell through to the shim ({s})", .{@errorName(err)});
1841 return jni.JNI_FALSE;
1842 };
1843 return jni.JNI_TRUE;
1844}
1845
1846/// `nativeStopAppStateMonitoring(activity)`.
1847fn nativeStopAppStateMonitoring(
1848 env: jni.JNIEnv,
1849 _: jni.jobject,
1850 activity: jni.jobject,
1851) callconv(.c) jni.jboolean {
1852 const j = Jni.init(env);
1853
1854 callHolderVoid(j, "stopAppStateWatch", activity) catch |err| {
1855 std.log.warn("craft: stopAppStateMonitoring failed ({s})", .{@errorName(err)});
1856 return jni.JNI_FALSE;
1857 };
1858 return jni.JNI_TRUE;
1859}
1860
1861/// `nativeGetAppState()`.
1862///
1863/// Reads the state Zig now owns. This is why the trio moves together: the
1864/// Kotlin's `currentAppState` is only written by the observer, so serving the
1865/// observer without serving this would leave the field frozen at "active"
1866/// while the real state moved on.
1867fn nativeGetAppState(env: jni.JNIEnv, _: jni.jobject) callconv(.c) jni.jstring {
1868 const j = Jni.init(env);
1869 var arena = std.heap.ArenaAllocator.init(backing);
1870 defer arena.deinit();
1871
1872 return j.newStringUtf8(arena.allocator(), appstate.state().name()) catch null;
1873}
1874
17791875// =============================================================================
17801876// Tests
17811877// =============================================================================
@@ -1887,7 +1983,7 @@ test "the registered natives name methods the Kotlin actually declares" {
18871983 // A descriptor is checked by the JVM at registration, so a wrong one fails
18881984 // at load rather than at call — but only if the *name* matches something.
18891985 // These two strings are the contract with CraftBridge.kt.
1890 try testing.expectEqual(@as(usize, 46), natives.len);
1986 try testing.expectEqual(@as(usize, 50), natives.len);
18911987 try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name));
18921988
18931989 // And the class they bind to is the fixed one, not the templated bridge.