also looking at this
feat(android): haptic and vibrate — ratchet 91 -> 89
#150
7 files
+410
-3
| @@ -42,6 +42,7 @@ const clipboard = @import("bridge_android_clipboard.zig"); | ||
| 42 | 42 | const intents = @import("bridge_android_intents.zig"); |
| 43 | 43 | const network = @import("bridge_android_network.zig"); |
| 44 | 44 | const securestore = @import("bridge_android_securestore.zig"); |
| 45 | const haptics = @import("bridge_android_haptics.zig"); | |
| 45 | 46 | |
| 46 | 47 | const Jni = jni.Jni; |
| 47 | 48 | |
| @@ -190,6 +191,16 @@ const natives = [_]jni.JNINativeMethod{ | ||
| 190 | 191 | .signature = "(Landroid/content/SharedPreferences;)Z", |
| 191 | 192 | .fnPtr = @ptrCast(&nativeSecureClear), |
| 192 | 193 | }, |
| 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 | }, | |
| 193 | 204 | }; |
| 194 | 205 | |
| 195 | 206 | /// 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 | ||
| 490 | 501 | return jni.JNI_TRUE; |
| 491 | 502 | } |
| 492 | 503 | |
| 504 | fn 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 | ||
| 522 | fn 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 | ||
| 493 | 548 | // ============================================================================= |
| 494 | 549 | // Tests |
| 495 | 550 | // ============================================================================= |
| @@ -601,7 +656,7 @@ test "the registered natives name methods the Kotlin actually declares" { | ||
| 601 | 656 | // A descriptor is checked by the JVM at registration, so a wrong one fails |
| 602 | 657 | // at load rather than at call — but only if the *name* matches something. |
| 603 | 658 | // 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); | |
| 605 | 660 | try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name)); |
| 606 | 661 | |
| 607 | 662 | // And the class they bind to is the fixed one, not the templated bridge. |