ReviewOS

also looking at this

craft-native/craft

feat(android): the first listener shim, and network monitoring on it — 61 → 59

#183
Merged glennmichael123 wants to merge feat/android-network-watch into main
5 files +246 -13
packages/zig/src/android_dispatch.zigmodified+88-11
Changes to packages/zig/src/android_dispatch.zig
@@ -391,6 +391,22 @@ const natives = [_]jni.JNINativeMethod{
391391 .signature = "(Landroid/app/Activity;)Ljava/lang/String;",
392392 .fnPtr = @ptrCast(&nativeResumeLocationRecording),
393393 },
394 // Not an action: the other end of the network callback the holder owns.
395 .{
396 .name = "nativeNetworkChanged",
397 .signature = "(Landroid/app/Activity;)V",
398 .fnPtr = @ptrCast(&nativeNetworkChanged),
399 },
400 .{
401 .name = "nativeStartNetworkMonitoring",
402 .signature = "(Landroid/app/Activity;)Z",
403 .fnPtr = @ptrCast(&nativeStartNetworkMonitoring),
404 },
405 .{
406 .name = "nativeStopNetworkMonitoring",
407 .signature = "(Landroid/app/Activity;)Z",
408 .fnPtr = @ptrCast(&nativeStopNetworkMonitoring),
409 },
394410};
395411
396412/// How binding went. A value rather than a log line, so every path is
@@ -1676,25 +1692,86 @@ fn nativeResumeLocationRecording(
16761692}
16771693
16781694fn startRecordingService(j: Jni, activity: jni.jobject) !void {
1679 try j.pushLocalFrame(8);
1680 defer _ = j.popLocalFrame(null);
1681
1682 const holder = try j.findClass(holder_class);
1683 try j.callStaticVoidMethodA(
1684 holder,
1685 try j.staticMethodId(holder, "startRecordingService", "(Landroid/app/Activity;)V"),
1686 &.{.{ .l = activity }},
1687 );
1695 return callHolderVoid(j, "startRecordingService", activity);
16881696}
16891697
16901698fn stopRecordingService(j: Jni, activity: jni.jobject) !void {
1699 return callHolderVoid(j, "stopRecordingService", activity);
1700}
1701
1702/// `nativeNetworkChanged(activity)` — a network callback, arrived at.
1703///
1704/// Runs on a Binder thread. That is fine and is the reason the reply channel
1705/// exists in the shape it does: `events.settle` attaches whatever thread it is
1706/// called on and the Kotlin deliverer hops to the main looper.
1707///
1708/// Void and silent. There is no promise waiting on this — the page registered
1709/// a callback with `onNetworkChange` and every change calls it — so a failure
1710/// has nowhere to be reported and shows up in `droppedCount` instead.
1711fn nativeNetworkChanged(
1712 env: jni.JNIEnv,
1713 _: jni.jobject,
1714 activity: jni.jobject,
1715) callconv(.c) void {
1716 const j = Jni.init(env);
1717 var arena = std.heap.ArenaAllocator.init(backing);
1718 defer arena.deinit();
1719 const allocator = arena.allocator();
1720
1721 const status = network.read(j, activity) catch return;
1722 const json = network.render(allocator, status) catch return;
1723 events.settle(allocator, network.change_global, json) catch {};
1724}
1725
1726/// `nativeStartNetworkMonitoring(activity)`.
1727///
1728/// The callback object is the holder's, because
1729/// `ConnectivityManager.NetworkCallback` is an abstract Java class and JNI
1730/// cannot subclass one. What Zig owns is what happens when it fires.
1731fn nativeStartNetworkMonitoring(
1732 env: jni.JNIEnv,
1733 _: jni.jobject,
1734 activity: jni.jobject,
1735) callconv(.c) jni.jboolean {
1736 const j = Jni.init(env);
1737
1738 callHolderVoid(j, "startNetworkWatch", activity) catch |err| {
1739 std.log.warn("craft: startNetworkMonitoring fell through to the shim ({s})", .{@errorName(err)});
1740 return jni.JNI_FALSE;
1741 };
1742 return jni.JNI_TRUE;
1743}
1744
1745/// `nativeStopNetworkMonitoring(activity)`.
1746///
1747/// A false here after a successful start would leave the callback registered:
1748/// the shim's own `networkCallback` field is null, so its stop finds nothing
1749/// to unregister. The holder's stop is the only thing that can undo the
1750/// holder's start, which is why this reports the JNI failure rather than
1751/// pretending the shim can finish the job.
1752fn nativeStopNetworkMonitoring(
1753 env: jni.JNIEnv,
1754 _: jni.jobject,
1755 activity: jni.jobject,
1756) callconv(.c) jni.jboolean {
1757 const j = Jni.init(env);
1758
1759 callHolderVoid(j, "stopNetworkWatch", activity) catch |err| {
1760 std.log.warn("craft: stopNetworkMonitoring failed ({s})", .{@errorName(err)});
1761 return jni.JNI_FALSE;
1762 };
1763 return jni.JNI_TRUE;
1764}
1765
1766/// Call a `(Activity) -> void` static on the holder.
1767fn callHolderVoid(j: Jni, comptime name: [:0]const u8, activity: jni.jobject) !void {
16911768 try j.pushLocalFrame(8);
16921769 defer _ = j.popLocalFrame(null);
16931770
16941771 const holder = try j.findClass(holder_class);
16951772 try j.callStaticVoidMethodA(
16961773 holder,
1697 try j.staticMethodId(holder, "stopRecordingService", "(Landroid/app/Activity;)V"),
1774 try j.staticMethodId(holder, name, "(Landroid/app/Activity;)V"),
16981775 &.{.{ .l = activity }},
16991776 );
17001777}
@@ -1810,7 +1887,7 @@ test "the registered natives name methods the Kotlin actually declares" {
18101887 // A descriptor is checked by the JVM at registration, so a wrong one fails
18111888 // at load rather than at call — but only if the *name* matches something.
18121889 // These two strings are the contract with CraftBridge.kt.
1813 try testing.expectEqual(@as(usize, 43), natives.len);
1890 try testing.expectEqual(@as(usize, 46), natives.len);
18141891 try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name));
18151892
18161893 // And the class they bind to is the fixed one, not the templated bridge.