also looking at this
feat(android): the app-state trio in Zig — ratchet 59 → 56
#184
6 files
+422
-3
| @@ -42,6 +42,7 @@ const system = @import("bridge_android_system.zig"); | ||
| 42 | 42 | const clipboard = @import("bridge_android_clipboard.zig"); |
| 43 | 43 | const intents = @import("bridge_android_intents.zig"); |
| 44 | 44 | const network = @import("bridge_android_network.zig"); |
| 45 | const appstate = @import("bridge_android_appstate.zig"); | |
| 45 | 46 | const securestore = @import("bridge_android_securestore.zig"); |
| 46 | 47 | const haptics = @import("bridge_android_haptics.zig"); |
| 47 | 48 | const notifications = @import("bridge_android_notifications.zig"); |
| @@ -407,6 +408,29 @@ const natives = [_]jni.JNINativeMethod{ | ||
| 407 | 408 | .signature = "(Landroid/app/Activity;)Z", |
| 408 | 409 | .fnPtr = @ptrCast(&nativeStopNetworkMonitoring), |
| 409 | 410 | }, |
| 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 | }, | |
| 410 | 434 | }; |
| 411 | 435 | |
| 412 | 436 | /// 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 | ||
| 1776 | 1800 | ); |
| 1777 | 1801 | } |
| 1778 | 1802 | |
| 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. | |
| 1808 | fn 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. | |
| 1832 | fn 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)`. | |
| 1847 | fn 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. | |
| 1867 | fn 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 | ||
| 1779 | 1875 | // ============================================================================= |
| 1780 | 1876 | // Tests |
| 1781 | 1877 | // ============================================================================= |
| @@ -1887,7 +1983,7 @@ test "the registered natives name methods the Kotlin actually declares" { | ||
| 1887 | 1983 | // A descriptor is checked by the JVM at registration, so a wrong one fails |
| 1888 | 1984 | // at load rather than at call — but only if the *name* matches something. |
| 1889 | 1985 | // 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); | |
| 1891 | 1987 | try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name)); |
| 1892 | 1988 | |
| 1893 | 1989 | // And the class they bind to is the fixed one, not the templated bridge. |