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/android/templates/CraftBridge.kt.templatemodified+105-10
Changes to packages/android/templates/CraftBridge.kt.template
@@ -2,10 +2,13 @@ package {⁠{PACKAGE_NAME}}
22
33import android.Manifest
44import android.app.Activity
5import android.content.BroadcastReceiver
56import android.content.ClipData
67import android.content.ClipboardManager
78import android.content.Context
89import android.content.Intent
10import android.content.IntentFilter
11import android.content.IntentSender
912import android.content.pm.PackageManager
1013import android.graphics.Bitmap
1114import android.hardware.camera2.CameraAccessException
@@ -365,9 +368,14 @@ class CraftBridge(
365368 CraftAndroid.stopListening();
366369 },
367370
368 // Share
371 // Share. Resolves true when the person picks an app, false
372 // when they dismiss the menu, and rejects when there is
373 // nothing to share or the menu cannot open. No timeout: the
374 // menu waits on a person, not on the device.
369375 share: function(text, title) {
370 CraftAndroid.share(text, title || '');
376 return window.__craftPromise('share', '_craftShareResolve', '_craftShareReject', function() {
377 CraftAndroid.share(text == null ? '' : String(text), title == null ? '' : String(title));
378 });
371379 },
372380
373381 // Camera
@@ -1610,18 +1618,95 @@ class CraftBridge(
16101618
16111619 // ==================== Share ====================
16121620
1621 // Whether the person picked an app from the share menu that is open now.
1622 //
1623 // The chooser says so through the IntentSender below and says nothing at
1624 // all when it is dismissed, while its activity result comes back
1625 // RESULT_CANCELED either way. Neither signal answers on its own, so the
1626 // pick marks this and the result, which always arrives, settles the page's
1627 // promise with it. Everything that touches it runs on the main thread: the
1628 // launch is posted there, and receivers and results are delivered there.
1629 private var shareChosen = false
1630 private var shareReceiverRegistered = false
1631 private val shareChosenAction by lazy { "${activity.packageName}.CRAFT_SHARE_CHOSEN" }
1632 private val shareChosenReceiver = object : BroadcastReceiver() {
1633 override fun onReceive(context: Context, intent: Intent) {
1634 shareChosen = true
1635 }
1636 }
1637
16131638 @JavascriptInterface
16141639 fun share(text: String, title: String) {
1615 if (CraftNative.share(activity, text, title)) return
1640 // The same line iOS draws: an empty string is nothing to share, and a
1641 // chooser opened over nothing would offer apps a blank message.
1642 if (text.isEmpty()) {
1643 rejectShare("Nothing to share")
1644 return
1645 }
1646
1647 activity.runOnUiThread {
1648 if (closed) return@runOnUiThread
1649 shareChosen = false
1650
1651 val chosen = try {
1652 shareChosenSender()
1653 } catch (error: Exception) {
1654 rejectShare(error.message ?: "Unable to open the share menu")
1655 return@runOnUiThread
1656 }
1657
1658 // Zig launches the same chooser for the same result; the answer
1659 // still comes back through onActivityResult below.
1660 if (CraftNative.share(activity, text, title, chosen, REQUEST_SHARE)) return@runOnUiThread
16161661
1617 val intent = Intent(Intent.ACTION_SEND).apply {
1618 type = "text/plain"
1619 putExtra(Intent.EXTRA_TEXT, text)
1620 if (title.isNotEmpty()) {
1621 putExtra(Intent.EXTRA_SUBJECT, title)
1662 val intent = Intent(Intent.ACTION_SEND).apply {
1663 type = "text/plain"
1664 putExtra(Intent.EXTRA_TEXT, text)
1665 if (title.isNotEmpty()) {
1666 putExtra(Intent.EXTRA_SUBJECT, title)
1667 }
1668 }
1669 try {
1670 activity.startActivityForResult(Intent.createChooser(intent, "Share", chosen), REQUEST_SHARE)
1671 } catch (error: Exception) {
1672 rejectShare(error.message ?: "Unable to open the share menu")
16221673 }
16231674 }
1624 activity.startActivity(Intent.createChooser(intent, "Share"))
1675 }
1676
1677 // The IntentSender the chooser fires on a pick.
1678 //
1679 // Immutable: the chooser adds the chosen component to the intent it
1680 // fires, and this only needs to know that it fired. Explicit, by package,
1681 // so the broadcast reaches this app and nothing else, and so the
1682 // not-exported receiver accepts it.
1683 private fun shareChosenSender(): IntentSender {
1684 if (!shareReceiverRegistered) {
1685 ContextCompat.registerReceiver(
1686 activity,
1687 shareChosenReceiver,
1688 IntentFilter(shareChosenAction),
1689 ContextCompat.RECEIVER_NOT_EXPORTED
1690 )
1691 shareReceiverRegistered = true
1692 }
1693 val pick = Intent(shareChosenAction).setPackage(activity.packageName)
1694 return PendingIntent.getBroadcast(
1695 activity,
1696 REQUEST_SHARE,
1697 pick,
1698 PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
1699 ).intentSender
1700 }
1701
1702 private fun handleShareResult() {
1703 val chosen = shareChosen
1704 shareChosen = false
1705 evaluatePromiseJavascript("window._craftShareResolve && window._craftShareResolve($chosen)")
1706 }
1707
1708 private fun rejectShare(message: String) {
1709 evaluatePromiseJavascript("window._craftShareReject && window._craftShareReject(${jsQuote(message)})")
16251710 }
16261711
16271712 // ==================== Camera & Gallery ====================
@@ -3698,7 +3783,7 @@ class CraftBridge(
36983783 fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
36993784 if (closed) return requestCode == REQUEST_CAMERA || requestCode == REQUEST_GALLERY ||
37003785 requestCode == REQUEST_FILE_PICKER || requestCode == REQUEST_VIDEO ||
3701 requestCode == REQUEST_PICK_CONTACT
3786 requestCode == REQUEST_PICK_CONTACT || requestCode == REQUEST_SHARE
37023787
37033788 return when (requestCode) {
37043789 REQUEST_CAMERA, REQUEST_GALLERY -> {
@@ -3717,6 +3802,10 @@ class CraftBridge(
37173802 handleContactPickerResult(resultCode, data)
37183803 true
37193804 }
3805 REQUEST_SHARE -> {
3806 handleShareResult()
3807 true
3808 }
37203809 else -> healthConnect.onActivityResult(requestCode, resultCode, data)
37213810 }
37223811 }
@@ -3736,6 +3825,11 @@ class CraftBridge(
37363825 CraftNative.close(activity)
37373826 healthConnect.close()
37383827
3828 if (shareReceiverRegistered) {
3829 runCatching { activity.unregisterReceiver(shareChosenReceiver) }
3830 shareReceiverRegistered = false
3831 }
3832
37393833 runCatching { speechRecognizer?.cancel() }
37403834 runCatching { speechRecognizer?.destroy() }
37413835 speechRecognizer = null
@@ -4670,6 +4764,7 @@ class CraftBridge(
46704764 const val REQUEST_VIDEO = 1008
46714765 const val REQUEST_BLUETOOTH = 1009
46724766 const val REQUEST_PICK_CONTACT = 1010
4767 const val REQUEST_SHARE = 1011
46734768 private const val PERMISSION_REQUEST_START = 2000
46744769 private const val PERMISSION_REQUEST_END = 2999
46754770 private const val LOCATION_REQUEST_TIMEOUT_MS = 15_000L