also looking at this
fix(android): resolve share() with whether the person picked an app
#210
12 files
+399
-56
| @@ -5,18 +5,23 @@ | ||
| 5 | 5 | //! module — they share every JNI call between them, and the only thing that |
| 6 | 6 | //! differs is which extras go on the intent. |
| 7 | 7 | //! |
| 8 | //! ## Neither reports whether anything happened | |
| 8 | //! ## What each one can find out | |
| 9 | 9 | //! |
| 10 | 10 | //! `startActivity` throws `ActivityNotFoundException` when nothing on the |
| 11 | 11 | //! 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. | |
| 15 | 14 | //! |
| 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. | |
| 20 | 25 | //! |
| 21 | 26 | //! ## `share` has one branch, and it is the empty title |
| 22 | 27 | //! |
| @@ -89,11 +94,21 @@ pub fn openUrl(j: Jni, allocator: std.mem.Allocator, activity: jobject, url: []c | ||
| 89 | 94 | try startActivity(j, activity, intent); |
| 90 | 95 | } |
| 91 | 96 | |
| 92 | /// `startActivity(Intent.createChooser(Intent(ACTION_SEND)…, "Share"))`. | |
| 97 | /// `startActivityForResult(Intent.createChooser(Intent(ACTION_SEND)…, "Share", chosen), request_code)`. | |
| 93 | 98 | /// |
| 94 | 99 | /// An empty `title` skips `EXTRA_SUBJECT` entirely rather than setting it to |
| 95 | /// the empty string — see the module comment. | |
| 96 | pub 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. | |
| 103 | pub 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 { | |
| 97 | 112 | try j.pushLocalFrame(24); |
| 98 | 113 | defer _ = j.popLocalFrame(null); |
| 99 | 114 | |
| @@ -139,12 +154,20 @@ pub fn share(j: Jni, allocator: std.mem.Allocator, activity: jobject, text: []co | ||
| 139 | 154 | try j.staticMethodId( |
| 140 | 155 | intent_cls, |
| 141 | 156 | "createChooser", |
| 142 | "(Landroid/content/Intent;Ljava/lang/CharSequence;)Landroid/content/Intent;", | |
| 157 | "(Landroid/content/Intent;Ljava/lang/CharSequence;Landroid/content/IntentSender;)Landroid/content/Intent;", | |
| 143 | 158 | ), |
| 144 | &.{ .{ .l = intent }, .{ .l = chooser_label } }, | |
| 159 | &.{ .{ .l = intent }, .{ .l = chooser_label }, .{ .l = chosen } }, | |
| 145 | 160 | ); |
| 146 | 161 | |
| 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 | ); | |
| 148 | 171 | } |
| 149 | 172 | |
| 150 | 173 | // ============================================================================= |
| @@ -163,6 +186,9 @@ var fake_field_names: std.ArrayListUnmanaged([]const u8) = .empty; | ||
| 163 | 186 | var fake_strings: std.ArrayListUnmanaged([]const u8) = .empty; |
| 164 | 187 | var fake_throw_on_start = false; |
| 165 | 188 | var fake_pending: jobject = null; |
| 189 | /// The last arguments `createChooser` and `startActivityForResult` received. | |
| 190 | var fake_chooser_sender: jobject = null; | |
| 191 | var fake_request_code: ?jni.jint = null; | |
| 166 | 192 | |
| 167 | 193 | fn obj(tag: usize) jobject { |
| 168 | 194 | return @ptrCast(&fake_storage[tag]); |
| @@ -196,8 +222,9 @@ fn fNewStringUTF(_: jni.JNIEnv, text: [*:0]const u8) callconv(.c) jni.jstring { | ||
| 196 | 222 | fake_strings.append(testing.allocator, copy) catch testing.allocator.free(copy); |
| 197 | 223 | return obj(3); |
| 198 | 224 | } |
| 199 | fn fCallStaticObjectMethodA(_: jni.JNIEnv, _: jni.jclass, id: jni.jmethodID, _: [*]const jni.jvalue) callconv(.c) jobject { | |
| 225 | fn fCallStaticObjectMethodA(_: jni.JNIEnv, _: jni.jclass, id: jni.jmethodID, args: [*]const jni.jvalue) callconv(.c) jobject { | |
| 200 | 226 | fake_calls.append(testing.allocator, nameOf(id)) catch {}; |
| 227 | if (std.mem.eql(u8, nameOf(id), "createChooser")) fake_chooser_sender = args[2].l; | |
| 201 | 228 | return obj(4); |
| 202 | 229 | } |
| 203 | 230 | fn 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. | ||
| 208 | 235 | fake_calls.append(testing.allocator, nameOf(id)) catch {}; |
| 209 | 236 | return obj(6); |
| 210 | 237 | } |
| 211 | fn fCallVoidMethodA(_: jni.JNIEnv, _: jobject, id: jni.jmethodID, _: [*]const jni.jvalue) callconv(.c) void { | |
| 238 | fn fCallVoidMethodA(_: jni.JNIEnv, _: jobject, id: jni.jmethodID, args: [*]const jni.jvalue) callconv(.c) void { | |
| 212 | 239 | fake_calls.append(testing.allocator, nameOf(id)) catch {}; |
| 240 | if (std.mem.eql(u8, nameOf(id), "startActivityForResult")) fake_request_code = args[1].i; | |
| 213 | 241 | if (fake_throw_on_start) fake_pending = obj(7); |
| 214 | 242 | } |
| 215 | 243 | fn fExceptionOccurred(_: jni.JNIEnv) callconv(.c) jobject { |
| @@ -255,6 +283,8 @@ fn resetFakes() void { | ||
| 255 | 283 | fake_strings.clearRetainingCapacity(); |
| 256 | 284 | fake_throw_on_start = false; |
| 257 | 285 | fake_pending = null; |
| 286 | fake_chooser_sender = null; | |
| 287 | fake_request_code = null; | |
| 258 | 288 | } |
| 259 | 289 | |
| 260 | 290 | fn freeFakes() void { |
| @@ -321,7 +351,7 @@ test "share sets the text extra and the chooser" { | ||
| 321 | 351 | fakeEnv(&table); |
| 322 | 352 | const ptr: *const jni.JNINativeInterface = &table; |
| 323 | 353 | |
| 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); | |
| 325 | 355 | |
| 326 | 356 | try testing.expect(sawField("ACTION_SEND")); |
| 327 | 357 | try testing.expect(sawField("EXTRA_TEXT")); |
| @@ -329,11 +359,45 @@ test "share sets the text extra and the chooser" { | ||
| 329 | 359 | try testing.expect(sawCall("setType")); |
| 330 | 360 | try testing.expect(sawCall("putExtra")); |
| 331 | 361 | try testing.expect(sawCall("createChooser")); |
| 332 | try testing.expect(sawCall("startActivity")); | |
| 333 | 362 | try testing.expect(sawString("text/plain")); |
| 334 | 363 | try testing.expect(sawString("Share")); |
| 335 | 364 | } |
| 336 | 365 | |
| 366 | test "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 | ||
| 385 | test "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 | ||
| 337 | 401 | test "an empty title omits the subject rather than setting it empty" { |
| 338 | 402 | // The one branch in either action. An empty EXTRA_SUBJECT produces a mail |
| 339 | 403 | // 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" { | ||
| 344 | 408 | fakeEnv(&table); |
| 345 | 409 | const ptr: *const jni.JNINativeInterface = &table; |
| 346 | 410 | |
| 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); | |
| 348 | 412 | |
| 349 | 413 | try testing.expect(sawField("EXTRA_TEXT")); |
| 350 | 414 | try testing.expect(!sawField("EXTRA_SUBJECT")); |
| 351 | 415 | // And the rest of the intent is still built. |
| 352 | 416 | try testing.expect(sawCall("createChooser")); |
| 353 | try testing.expect(sawCall("startActivity")); | |
| 417 | try testing.expect(sawCall("startActivityForResult")); | |
| 354 | 418 | } |
| 355 | 419 | |
| 356 | 420 | test "the action names match the Kotlin methods exactly" { |