also looking at this
feat(android): deleteCalendarEvent — the first action to answer through the channel
#155
7 files
+288
-3
| @@ -45,6 +45,7 @@ const securestore = @import("bridge_android_securestore.zig"); | ||
| 45 | 45 | const haptics = @import("bridge_android_haptics.zig"); |
| 46 | 46 | const notifcancel = @import("bridge_android_notifcancel.zig"); |
| 47 | 47 | const events = @import("android_events.zig"); |
| 48 | const calendar = @import("bridge_android_calendar.zig"); | |
| 48 | 49 | |
| 49 | 50 | const Jni = jni.Jni; |
| 50 | 51 | |
| @@ -217,6 +218,14 @@ const natives = [_]jni.JNINativeMethod{ | ||
| 217 | 218 | .signature = "(Landroid/app/Activity;)Z", |
| 218 | 219 | .fnPtr = @ptrCast(&nativeCancelAllNotifications), |
| 219 | 220 | }, |
| 221 | // The first action that answers through the reply channel rather than by | |
| 222 | // returning. The boolean still means "Zig served it" — the *result* goes | |
| 223 | // to the page separately, which is exactly the shape iOS's seam has. | |
| 224 | .{ | |
| 225 | .name = "nativeDeleteCalendarEvent", | |
| 226 | .signature = "(Landroid/app/Activity;Ljava/lang/String;)Z", | |
| 227 | .fnPtr = @ptrCast(&nativeDeleteCalendarEvent), | |
| 228 | }, | |
| 220 | 229 | }; |
| 221 | 230 | |
| 222 | 231 | /// How binding went. A value rather than a log line, so every path is |
| @@ -587,6 +596,52 @@ fn nativeCancelAllNotifications(env: jni.JNIEnv, _: jni.jobject, activity: jni.j | ||
| 587 | 596 | return jni.JNI_TRUE; |
| 588 | 597 | } |
| 589 | 598 | |
| 599 | /// `nativeDeleteCalendarEvent(activity, eventId)`. | |
| 600 | /// | |
| 601 | /// Returns whether Zig took the action, not whether the delete succeeded. The | |
| 602 | /// outcome reaches the page through the reply channel, so a `true` here means | |
| 603 | /// "do not also run the Kotlin" and nothing more — the same distinction | |
| 604 | /// `ios_dispatch` draws between claiming an action and answering it. | |
| 605 | fn nativeDeleteCalendarEvent( | |
| 606 | env: jni.JNIEnv, | |
| 607 | _: jni.jobject, | |
| 608 | activity: jni.jobject, | |
| 609 | event_id: jni.jstring, | |
| 610 | ) callconv(.c) jni.jboolean { | |
| 611 | const j = Jni.init(env); | |
| 612 | var arena = std.heap.ArenaAllocator.init(backing); | |
| 613 | defer arena.deinit(); | |
| 614 | const allocator = arena.allocator(); | |
| 615 | ||
| 616 | const text = j.stringToUtf8(allocator, event_id) catch return jni.JNI_FALSE; | |
| 617 | ||
| 618 | const id = calendar.parseEventId(text) orelse { | |
| 619 | // What the shim's NumberFormatException produces, with the message it | |
| 620 | // produces — but escaped, so an id containing a quote rejects instead | |
| 621 | // of hanging the promise. See #154. | |
| 622 | var message: std.ArrayListUnmanaged(u8) = .empty; | |
| 623 | defer message.deinit(allocator); | |
| 624 | message.appendSlice(allocator, "For input string: \"") catch return jni.JNI_FALSE; | |
| 625 | message.appendSlice(allocator, text) catch return jni.JNI_FALSE; | |
| 626 | message.append(allocator, '"') catch return jni.JNI_FALSE; | |
| 627 | ||
| 628 | calendar.rejectWith(allocator, message.items) catch return jni.JNI_FALSE; | |
| 629 | return jni.JNI_TRUE; | |
| 630 | }; | |
| 631 | ||
| 632 | calendar.deleteEvent(j, activity, id) catch |err| { | |
| 633 | // A SecurityException for a missing WRITE_CALENDAR permission lands | |
| 634 | // here, as it lands in the shim's catch. The error name rather than | |
| 635 | // the Java message: the throwable was already described to logcat and | |
| 636 | // cleared by `Jni.check`, so its text is gone by now. | |
| 637 | calendar.rejectWith(allocator, @errorName(err)) catch {}; | |
| 638 | return jni.JNI_TRUE; | |
| 639 | }; | |
| 640 | ||
| 641 | events.settle(allocator, calendar.resolve_global, "true") catch return jni.JNI_FALSE; | |
| 642 | return jni.JNI_TRUE; | |
| 643 | } | |
| 644 | ||
| 590 | 645 | // ============================================================================= |
| 591 | 646 | // Tests |
| 592 | 647 | // ============================================================================= |
| @@ -698,7 +753,7 @@ test "the registered natives name methods the Kotlin actually declares" { | ||
| 698 | 753 | // A descriptor is checked by the JVM at registration, so a wrong one fails |
| 699 | 754 | // at load rather than at call — but only if the *name* matches something. |
| 700 | 755 | // These two strings are the contract with CraftBridge.kt. |
| 701 | try testing.expectEqual(@as(usize, 16), natives.len); | |
| 756 | try testing.expectEqual(@as(usize, 17), natives.len); | |
| 702 | 757 | try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name)); |
| 703 | 758 | |
| 704 | 759 | // And the class they bind to is the fixed one, not the templated bridge. |