ReviewOS

also looking at this

craft-native/craft

fix(android): resolve share() with whether the person picked an app

#210
Merged glennmichael123 wants to merge fix/android-share-outcome into main
12 files +399 -56
packages/zig/src/bridge_android_intents.zigmodified+84-20
Changes to packages/zig/src/bridge_android_intents.zig
@@ -5,18 +5,23 @@
55//! module — they share every JNI call between them, and the only thing that
66//! differs is which extras go on the intent.
77//!
8//! ## Neither reports whether anything happened
8//! ## What each one can find out
99//!
1010//! `startActivity` throws `ActivityNotFoundException` when nothing on the
1111//! device handles the intent, and the Kotlin's `openURL` catches it and answers
12//! `false`. That is the *only* failure either action can see. Once another app
13//! is launched, whether the user did anything with it is unobservable from
14//! here — `share` in particular returns before the chooser is even drawn.
12//! `false`. That is the only failure `openURL` can see: once another app is
13//! launched, whether the user did anything with it is unobservable from here.
1514//!
16//! So `true` means "the intent was accepted", never "the user shared
17//! something", and a page that treats it as the latter is wrong on both
18//! platforms. Kept as-is rather than improved: the shim's answer is the
19//! contract while the shim still exists.
15//! `share` can find out one thing more, and has to, because the page's promise
16//! resolves with it (#203). The chooser reports a pick through the
17//! `IntentSender` passed to `createChooser`, and reports a dismissal not at
18//! all. Its activity result arrives in both cases, and is `RESULT_CANCELED` in
19//! both. So neither signal answers alone, and the Kotlin combines them: the
20//! sender marks a pick, and the result, which always comes, settles the promise
21//! with whatever was marked. This module launches the chooser with both halves
22//! wired. It does not own either, which is why the sender and the request code
23//! arrive as arguments rather than being built here: `CraftBridge` receives the
24//! result, so `CraftBridge` chooses the code it routes on.
2025//!
2126//! ## `share` has one branch, and it is the empty title
2227//!
@@ -89,11 +94,21 @@ pub fn openUrl(j: Jni, allocator: std.mem.Allocator, activity: jobject, url: []c
8994 try startActivity(j, activity, intent);
9095}
9196
92/// `startActivity(Intent.createChooser(Intent(ACTION_SEND)…, "Share"))`.
97/// `startActivityForResult(Intent.createChooser(Intent(ACTION_SEND)…, "Share", chosen), request_code)`.
9398///
9499/// An empty `title` skips `EXTRA_SUBJECT` entirely rather than setting it to
95/// the empty string — see the module comment.
96pub fn share(j: Jni, allocator: std.mem.Allocator, activity: jobject, text: []const u8, title: []const u8) !void {
100/// the empty string — see the module comment. `chosen` is the `IntentSender`
101/// the chooser fires when the person picks an app, and `request_code` is the
102/// one `CraftBridge.onActivityResult` settles the page's promise on.
103pub fn share(
104 j: Jni,
105 allocator: std.mem.Allocator,
106 activity: jobject,
107 text: []const u8,
108 title: []const u8,
109 chosen: jobject,
110 request_code: i32,
111) !void {
97112 try j.pushLocalFrame(24);
98113 defer _ = j.popLocalFrame(null);
99114
@@ -139,12 +154,20 @@ pub fn share(j: Jni, allocator: std.mem.Allocator, activity: jobject, text: []co
139154 try j.staticMethodId(
140155 intent_cls,
141156 "createChooser",
142 "(Landroid/content/Intent;Ljava/lang/CharSequence;)Landroid/content/Intent;",
157 "(Landroid/content/Intent;Ljava/lang/CharSequence;Landroid/content/IntentSender;)Landroid/content/Intent;",
143158 ),
144 &.{ .{ .l = intent }, .{ .l = chooser_label } },
159 &.{ .{ .l = intent }, .{ .l = chooser_label }, .{ .l = chosen } },
145160 );
146161
147 try startActivity(j, activity, chooser);
162 // For a result, not a plain start: the result is the only signal that
163 // arrives when the person dismisses the chooser, and without it the page's
164 // promise would wait for a pick that is never coming.
165 const activity_cls = try j.objectClass(activity);
166 try j.callVoidMethodA(
167 activity,
168 try j.methodId(activity_cls, "startActivityForResult", "(Landroid/content/Intent;I)V"),
169 &.{ .{ .l = chooser }, .{ .i = request_code } },
170 );
148171}
149172
150173// =============================================================================
@@ -163,6 +186,9 @@ var fake_field_names: std.ArrayListUnmanaged([]const u8) = .empty;
163186var fake_strings: std.ArrayListUnmanaged([]const u8) = .empty;
164187var fake_throw_on_start = false;
165188var fake_pending: jobject = null;
189/// The last arguments `createChooser` and `startActivityForResult` received.
190var fake_chooser_sender: jobject = null;
191var fake_request_code: ?jni.jint = null;
166192
167193fn obj(tag: usize) jobject {
168194 return @ptrCast(&fake_storage[tag]);
@@ -196,8 +222,9 @@ fn fNewStringUTF(_: jni.JNIEnv, text: [*:0]const u8) callconv(.c) jni.jstring {
196222 fake_strings.append(testing.allocator, copy) catch testing.allocator.free(copy);
197223 return obj(3);
198224}
199fn fCallStaticObjectMethodA(_: jni.JNIEnv, _: jni.jclass, id: jni.jmethodID, _: [*]const jni.jvalue) callconv(.c) jobject {
225fn fCallStaticObjectMethodA(_: jni.JNIEnv, _: jni.jclass, id: jni.jmethodID, args: [*]const jni.jvalue) callconv(.c) jobject {
200226 fake_calls.append(testing.allocator, nameOf(id)) catch {};
227 if (std.mem.eql(u8, nameOf(id), "createChooser")) fake_chooser_sender = args[2].l;
201228 return obj(4);
202229}
203230fn fCallObjectMethodA(_: jni.JNIEnv, _: jobject, id: jni.jmethodID, _: [*]const jni.jvalue) callconv(.c) jobject {
@@ -208,8 +235,9 @@ fn fNewObjectA(_: jni.JNIEnv, _: jni.jclass, id: jni.jmethodID, _: [*]const jni.
208235 fake_calls.append(testing.allocator, nameOf(id)) catch {};
209236 return obj(6);
210237}
211fn fCallVoidMethodA(_: jni.JNIEnv, _: jobject, id: jni.jmethodID, _: [*]const jni.jvalue) callconv(.c) void {
238fn fCallVoidMethodA(_: jni.JNIEnv, _: jobject, id: jni.jmethodID, args: [*]const jni.jvalue) callconv(.c) void {
212239 fake_calls.append(testing.allocator, nameOf(id)) catch {};
240 if (std.mem.eql(u8, nameOf(id), "startActivityForResult")) fake_request_code = args[1].i;
213241 if (fake_throw_on_start) fake_pending = obj(7);
214242}
215243fn fExceptionOccurred(_: jni.JNIEnv) callconv(.c) jobject {
@@ -255,6 +283,8 @@ fn resetFakes() void {
255283 fake_strings.clearRetainingCapacity();
256284 fake_throw_on_start = false;
257285 fake_pending = null;
286 fake_chooser_sender = null;
287 fake_request_code = null;
258288}
259289
260290fn freeFakes() void {
@@ -321,7 +351,7 @@ test "share sets the text extra and the chooser" {
321351 fakeEnv(&table);
322352 const ptr: *const jni.JNINativeInterface = &table;
323353
324 try share(Jni.init(&ptr), testing.allocator, obj(8), "hello", "Subject");
354 try share(Jni.init(&ptr), testing.allocator, obj(8), "hello", "Subject", obj(9), 1011);
325355
326356 try testing.expect(sawField("ACTION_SEND"));
327357 try testing.expect(sawField("EXTRA_TEXT"));
@@ -329,11 +359,45 @@ test "share sets the text extra and the chooser" {
329359 try testing.expect(sawCall("setType"));
330360 try testing.expect(sawCall("putExtra"));
331361 try testing.expect(sawCall("createChooser"));
332 try testing.expect(sawCall("startActivity"));
333362 try testing.expect(sawString("text/plain"));
334363 try testing.expect(sawString("Share"));
335364}
336365
366test "share launches for a result, with the sender and code it was given" {
367 // Both halves of how the page learns what happened. A plain startActivity
368 // gets no result, so a dismissed chooser would leave the promise waiting
369 // for ever; a chooser without the sender reports no pick, so every share
370 // would resolve false.
371 resetFakes();
372 defer freeFakes();
373 var table: jni.JNINativeInterface = undefined;
374 fakeEnv(&table);
375 const ptr: *const jni.JNINativeInterface = &table;
376
377 try share(Jni.init(&ptr), testing.allocator, obj(8), "hello", "", obj(9), 1011);
378
379 try testing.expect(sawCall("startActivityForResult"));
380 try testing.expect(!sawCall("startActivity"));
381 try testing.expectEqual(obj(9), fake_chooser_sender);
382 try testing.expectEqual(@as(?jni.jint, 1011), fake_request_code);
383}
384
385test "a chooser that cannot start reports the exception rather than success" {
386 // CraftBridge rejects the page's promise on this; swallowing it would
387 // leave the promise waiting on a result for an activity that never began.
388 resetFakes();
389 defer freeFakes();
390 var table: jni.JNINativeInterface = undefined;
391 fakeEnv(&table);
392 const ptr: *const jni.JNINativeInterface = &table;
393
394 fake_throw_on_start = true;
395 try testing.expectError(
396 jni.JniError.JavaException,
397 share(Jni.init(&ptr), testing.allocator, obj(8), "hello", "", obj(9), 1011),
398 );
399}
400
337401test "an empty title omits the subject rather than setting it empty" {
338402 // The one branch in either action. An empty EXTRA_SUBJECT produces a mail
339403 // draft with a blank subject line rather than none, and some clients then
@@ -344,13 +408,13 @@ test "an empty title omits the subject rather than setting it empty" {
344408 fakeEnv(&table);
345409 const ptr: *const jni.JNINativeInterface = &table;
346410
347 try share(Jni.init(&ptr), testing.allocator, obj(8), "hello", "");
411 try share(Jni.init(&ptr), testing.allocator, obj(8), "hello", "", obj(9), 1011);
348412
349413 try testing.expect(sawField("EXTRA_TEXT"));
350414 try testing.expect(!sawField("EXTRA_SUBJECT"));
351415 // And the rest of the intent is still built.
352416 try testing.expect(sawCall("createChooser"));
353 try testing.expect(sawCall("startActivity"));
417 try testing.expect(sawCall("startActivityForResult"));
354418}
355419
356420test "the action names match the Kotlin methods exactly" {