ReviewOS

also looking at this

craft-native/craft

feat(android): haptic and vibrate — ratchet 91 -> 89

#150
Merged chris wants to merge feat/android-haptics into main
7 files +410 -3
packages/zig/src/android_dispatch.zigmodified+56-1
Changes to packages/zig/src/android_dispatch.zig
@@ -42,6 +42,7 @@ const clipboard = @import("bridge_android_clipboard.zig");
4242const intents = @import("bridge_android_intents.zig");
4343const network = @import("bridge_android_network.zig");
4444const securestore = @import("bridge_android_securestore.zig");
45const haptics = @import("bridge_android_haptics.zig");
4546
4647const Jni = jni.Jni;
4748
@@ -190,6 +191,16 @@ const natives = [_]jni.JNINativeMethod{
190191 .signature = "(Landroid/content/SharedPreferences;)Z",
191192 .fnPtr = @ptrCast(&nativeSecureClear),
192193 },
194 .{
195 .name = "nativeHaptic",
196 .signature = "(Landroid/app/Activity;Ljava/lang/String;)Z",
197 .fnPtr = @ptrCast(&nativeHaptic),
198 },
199 .{
200 .name = "nativeVibrate",
201 .signature = "(Landroid/app/Activity;Ljava/lang/String;)Z",
202 .fnPtr = @ptrCast(&nativeVibrate),
203 },
193204};
194205
195206/// How binding went. A value rather than a log line, so every path is
@@ -490,6 +501,50 @@ fn nativeSecureClear(env: jni.JNIEnv, _: jni.jobject, prefs: jni.jobject) callco
490501 return jni.JNI_TRUE;
491502}
492503
504fn nativeHaptic(
505 env: jni.JNIEnv,
506 _: jni.jobject,
507 activity: jni.jobject,
508 style: jni.jstring,
509) callconv(.c) jni.jboolean {
510 const j = Jni.init(env);
511 var arena = std.heap.ArenaAllocator.init(backing);
512 defer arena.deinit();
513
514 const text = j.stringToUtf8(arena.allocator(), style) catch return jni.JNI_FALSE;
515 haptics.play(j, activity, haptics.effectForStyle(text)) catch |err| {
516 std.log.warn("craft: haptic fell through to the shim ({s})", .{@errorName(err)});
517 return jni.JNI_FALSE;
518 };
519 return jni.JNI_TRUE;
520}
521
522fn nativeVibrate(
523 env: jni.JNIEnv,
524 _: jni.jobject,
525 activity: jni.jobject,
526 pattern_json: jni.jstring,
527) callconv(.c) jni.jboolean {
528 const j = Jni.init(env);
529 var arena = std.heap.ArenaAllocator.init(backing);
530 defer arena.deinit();
531 const allocator = arena.allocator();
532
533 const json = j.stringToUtf8(allocator, pattern_json) catch return jni.JNI_FALSE;
534
535 // Null is a pattern the shim would throw on. Declining lets it throw, log
536 // "Vibration error", and vibrate nothing — the same outcome with the log
537 // kept. See bridge_android_haptics.parsePattern.
538 const timings = haptics.parsePattern(allocator, json) catch return jni.JNI_FALSE;
539 if (timings == null) return jni.JNI_FALSE;
540
541 haptics.play(j, activity, .{ .waveform = timings.? }) catch |err| {
542 std.log.warn("craft: vibrate fell through to the shim ({s})", .{@errorName(err)});
543 return jni.JNI_FALSE;
544 };
545 return jni.JNI_TRUE;
546}
547
493548// =============================================================================
494549// Tests
495550// =============================================================================
@@ -601,7 +656,7 @@ test "the registered natives name methods the Kotlin actually declares" {
601656 // A descriptor is checked by the JVM at registration, so a wrong one fails
602657 // at load rather than at call — but only if the *name* matches something.
603658 // These two strings are the contract with CraftBridge.kt.
604 try testing.expectEqual(@as(usize, 12), natives.len);
659 try testing.expectEqual(@as(usize, 14), natives.len);
605660 try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name));
606661
607662 // And the class they bind to is the fixed one, not the templated bridge.