ReviewOS

also looking at this

craft-native/craft

feat(android): setKeepAwake through the trampoline — ratchet 70 → 69

#174
Merged glennmichael123 wants to merge feat/android-keep-awake into main
7 files +140 -13
packages/zig/src/android_dispatch.zigmodified+29-4
Changes to packages/zig/src/android_dispatch.zig
@@ -52,7 +52,7 @@ const shareditem = ;
5252const contacts = ;
5353const widgets = ;
5454const shortcuts = ;
55const orientation = ;
55const screen = ;
5656const main_thread = ;
5757
5858const Jni = jni.Jni;
@@ -342,6 +342,11 @@ const natives = [_]jni.JNINativeMethod{
342342 .signature = "(Landroid/app/Activity;)Z",
343343 .fnPtr = @ptrCast(&nativeUnlockOrientation),
344344 },
345 .{
346 .name = "nativeSetKeepAwake",
347 .signature = "(Landroid/app/Activity;Z)Z",
348 .fnPtr = @ptrCast(&nativeSetKeepAwake),
349 },
345350};
346351
347352/// How binding went. A value rather than a log line, so every path is
@@ -1354,7 +1359,7 @@ fn nativeLockOrientation(
13541359
13551360 const text = j.stringToUtf8(arena.allocator(), name) catch return jni.JNI_FALSE;
13561361
1357 orientation.apply(j, activity, orientation.modeFor(text)) catch |err| {
1362 screen.apply(j, activity, screen.modeFor(text)) catch |err| {
13581363 std.log.warn("craft: lockOrientation fell through to the shim ({s})", .{@errorName(err)});
13591364 return jni.JNI_FALSE;
13601365 };
@@ -1369,13 +1374,33 @@ fn nativeUnlockOrientation(
13691374) callconv(.c) jni.jboolean {
13701375 const j = Jni.init(env);
13711376
1372 orientation.apply(j, activity, .unspecified) catch |err| {
1377 screen.apply(j, activity, .unspecified) catch |err| {
13731378 std.log.warn("craft: unlockOrientation fell through to the shim ({s})", .{@errorName(err)});
13741379 return jni.JNI_FALSE;
13751380 };
13761381 return jni.JNI_TRUE;
13771382}
13781383
1384/// `nativeSetKeepAwake(activity, enabled)`.
1385///
1386/// Safe to serve where the flashlight pair is not: the shim's block ends
1387/// `isKeepingAwake = enabled`, and nothing in `CraftBridge.kt` ever reads that
1388/// field — so the Kotlin body not running leaves nothing stale behind.
1389fn nativeSetKeepAwake(
1390 env: jni.JNIEnv,
1391 _: jni.jobject,
1392 activity: jni.jobject,
1393 enabled: jni.jboolean,
1394) callconv(.c) jni.jboolean {
1395 const j = Jni.init(env);
1396
1397 screen.keepAwake(j, activity, enabled != jni.JNI_FALSE) catch |err| {
1398 std.log.warn("craft: setKeepAwake fell through to the shim ({s})", .{@errorName(err)});
1399 return jni.JNI_FALSE;
1400 };
1401 return jni.JNI_TRUE;
1402}
1403
13791404// =============================================================================
13801405// Tests
13811406// =============================================================================
@@ -1487,7 +1512,7 @@ test "the registered natives name methods the Kotlin actually declares" {
14871512 // A descriptor is checked by the JVM at registration, so a wrong one fails
14881513 // at load rather than at call — but only if the *name* matches something.
14891514 // These two strings are the contract with CraftBridge.kt.
1490 try testing.expectEqual(@as(usize, 34), natives.len);
1515 try testing.expectEqual(@as(usize, 35), natives.len);
14911516 try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name));
14921517
14931518 // And the class they bind to is the fixed one, not the templated bridge.