also looking at this
feat(android): getNetworkStatus — the action iOS cannot serve
#146
7 files
+407
-3
| @@ -40,6 +40,7 @@ const device = ; | ||
| 40 | 40 | const system = ; |
| 41 | 41 | const clipboard = ; |
| 42 | 42 | const intents = ; |
| 43 | const network = ; | |
| 43 | 44 | |
| 44 | 45 | const Jni = jni.Jni; |
| 45 | 46 | |
| @@ -159,6 +160,11 @@ const natives = [_]jni.JNINativeMethod{ | ||
| 159 | 160 | .signature = "(Landroid/app/Activity;Ljava/lang/String;Ljava/lang/String;)Z", |
| 160 | 161 | .fnPtr = @ptrCast(&nativeShare), |
| 161 | 162 | }, |
| 163 | .{ | |
| 164 | .name = "nativeGetNetworkStatus", | |
| 165 | .signature = "(Landroid/app/Activity;)Ljava/lang/String;", | |
| 166 | .fnPtr = @ptrCast(&nativeGetNetworkStatus), | |
| 167 | }, | |
| 162 | 168 | }; |
| 163 | 169 | |
| 164 | 170 | /// How binding went. A value rather than a log line, so every path is |
| @@ -365,6 +371,23 @@ fn nativeShare( | ||
| 365 | 371 | return jni.JNI_TRUE; |
| 366 | 372 | } |
| 367 | 373 | |
| 374 | fn nativeGetNetworkStatus(env: jni.JNIEnv, _: jni.jobject, activity: jni.jobject) callconv(.c) jni.jstring { | |
| 375 | const j = Jni.init(env); | |
| 376 | var arena = std.heap.ArenaAllocator.init(backing); | |
| 377 | defer arena.deinit(); | |
| 378 | const allocator = arena.allocator(); | |
| 379 | ||
| 380 | const status = network.read(j, activity) catch |err| { | |
| 381 | std.log.warn("craft: getNetworkStatus fell through to the shim ({s})", .{@errorName(err)}); | |
| 382 | return null; | |
| 383 | }; | |
| 384 | ||
| 385 | const json = network.render(allocator, status) catch return null; | |
| 386 | const owned = allocator.allocSentinel(u8, json.len, 0) catch return null; | |
| 387 | @memcpy(owned, json); | |
| 388 | return j.newStringUtf(owned.ptr) catch null; | |
| 389 | } | |
| 390 | ||
| 368 | 391 | // ============================================================================= |
| 369 | 392 | // Tests |
| 370 | 393 | // ============================================================================= |
| @@ -476,7 +499,7 @@ test "the registered natives name methods the Kotlin actually declares" { | ||
| 476 | 499 | // A descriptor is checked by the JVM at registration, so a wrong one fails |
| 477 | 500 | // at load rather than at call — but only if the *name* matches something. |
| 478 | 501 | // These two strings are the contract with CraftBridge.kt. |
| 479 | try testing.expectEqual(@as(usize, 7), natives.len); | |
| 502 | try testing.expectEqual(@as(usize, 8), natives.len); | |
| 480 | 503 | try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name)); |
| 481 | 504 | |
| 482 | 505 | // And the class they bind to is the fixed one, not the templated bridge. |