also looking at this
feat(android): the first listener shim, and network monitoring on it — 61 → 59
#183
5 files
+246
-13
| @@ -391,6 +391,22 @@ const natives = [_]jni.JNINativeMethod{ | ||
| 391 | 391 | .signature = "(Landroid/app/Activity;)Ljava/lang/String;", |
| 392 | 392 | .fnPtr = @ptrCast(&nativeResumeLocationRecording), |
| 393 | 393 | }, |
| 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 | }, | |
| 394 | 410 | }; |
| 395 | 411 | |
| 396 | 412 | /// How binding went. A value rather than a log line, so every path is |
| @@ -1676,25 +1692,86 @@ fn nativeResumeLocationRecording( | ||
| 1676 | 1692 | } |
| 1677 | 1693 | |
| 1678 | 1694 | fn 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); | |
| 1688 | 1696 | } |
| 1689 | 1697 | |
| 1690 | 1698 | fn 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. | |
| 1711 | fn 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. | |
| 1731 | fn 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. | |
| 1752 | fn 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. | |
| 1767 | fn callHolderVoid(j: Jni, comptime name: [:0]const u8, activity: jni.jobject) !void { | |
| 1691 | 1768 | try j.pushLocalFrame(8); |
| 1692 | 1769 | defer _ = j.popLocalFrame(null); |
| 1693 | 1770 | |
| 1694 | 1771 | const holder = try j.findClass(holder_class); |
| 1695 | 1772 | try j.callStaticVoidMethodA( |
| 1696 | 1773 | holder, |
| 1697 | try j.staticMethodId(holder, "stopRecordingService", "(Landroid/app/Activity;)V"), | |
| 1774 | try j.staticMethodId(holder, name, "(Landroid/app/Activity;)V"), | |
| 1698 | 1775 | &.{.{ .l = activity }}, |
| 1699 | 1776 | ); |
| 1700 | 1777 | } |
| @@ -1810,7 +1887,7 @@ test "the registered natives name methods the Kotlin actually declares" { | ||
| 1810 | 1887 | // A descriptor is checked by the JVM at registration, so a wrong one fails |
| 1811 | 1888 | // at load rather than at call — but only if the *name* matches something. |
| 1812 | 1889 | // 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); | |
| 1814 | 1891 | try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name)); |
| 1815 | 1892 | |
| 1816 | 1893 | // And the class they bind to is the fixed one, not the templated bridge. |