also looking at this
fix(android): resolve share() with whether the person picked an app
#210
12 files
+399
-56
| @@ -2,10 +2,13 @@ package {{PACKAGE_NAME}} | ||
| 2 | 2 | |
| 3 | 3 | import android.Manifest |
| 4 | 4 | import android.app.Activity |
| 5 | import android.content.BroadcastReceiver | |
| 5 | 6 | import android.content.ClipData |
| 6 | 7 | import android.content.ClipboardManager |
| 7 | 8 | import android.content.Context |
| 8 | 9 | import android.content.Intent |
| 10 | import android.content.IntentFilter | |
| 11 | import android.content.IntentSender | |
| 9 | 12 | import android.content.pm.PackageManager |
| 10 | 13 | import android.graphics.Bitmap |
| 11 | 14 | import android.hardware.camera2.CameraAccessException |
| @@ -365,9 +368,14 @@ class CraftBridge( | ||
| 365 | 368 | CraftAndroid.stopListening(); |
| 366 | 369 | }, |
| 367 | 370 | |
| 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. | |
| 369 | 375 | 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 | }); | |
| 371 | 379 | }, |
| 372 | 380 | |
| 373 | 381 | // Camera |
| @@ -1610,18 +1618,95 @@ class CraftBridge( | ||
| 1610 | 1618 | |
| 1611 | 1619 | // ==================== Share ==================== |
| 1612 | 1620 | |
| 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 | ||
| 1613 | 1638 | @JavascriptInterface |
| 1614 | 1639 | 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 | |
| 1616 | 1661 | |
| 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") | |
| 1622 | 1673 | } |
| 1623 | 1674 | } |
| 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)})") | |
| 1625 | 1710 | } |
| 1626 | 1711 | |
| 1627 | 1712 | // ==================== Camera & Gallery ==================== |
| @@ -3698,7 +3783,7 @@ class CraftBridge( | ||
| 3698 | 3783 | fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean { |
| 3699 | 3784 | if (closed) return requestCode == REQUEST_CAMERA || requestCode == REQUEST_GALLERY || |
| 3700 | 3785 | requestCode == REQUEST_FILE_PICKER || requestCode == REQUEST_VIDEO || |
| 3701 | requestCode == REQUEST_PICK_CONTACT | |
| 3786 | requestCode == REQUEST_PICK_CONTACT || requestCode == REQUEST_SHARE | |
| 3702 | 3787 | |
| 3703 | 3788 | return when (requestCode) { |
| 3704 | 3789 | REQUEST_CAMERA, REQUEST_GALLERY -> { |
| @@ -3717,6 +3802,10 @@ class CraftBridge( | ||
| 3717 | 3802 | handleContactPickerResult(resultCode, data) |
| 3718 | 3803 | true |
| 3719 | 3804 | } |
| 3805 | REQUEST_SHARE -> { | |
| 3806 | handleShareResult() | |
| 3807 | true | |
| 3808 | } | |
| 3720 | 3809 | else -> healthConnect.onActivityResult(requestCode, resultCode, data) |
| 3721 | 3810 | } |
| 3722 | 3811 | } |
| @@ -3736,6 +3825,11 @@ class CraftBridge( | ||
| 3736 | 3825 | CraftNative.close(activity) |
| 3737 | 3826 | healthConnect.close() |
| 3738 | 3827 | |
| 3828 | if (shareReceiverRegistered) { | |
| 3829 | runCatching { activity.unregisterReceiver(shareChosenReceiver) } | |
| 3830 | shareReceiverRegistered = false | |
| 3831 | } | |
| 3832 | ||
| 3739 | 3833 | runCatching { speechRecognizer?.cancel() } |
| 3740 | 3834 | runCatching { speechRecognizer?.destroy() } |
| 3741 | 3835 | speechRecognizer = null |
| @@ -4670,6 +4764,7 @@ class CraftBridge( | ||
| 4670 | 4764 | const val REQUEST_VIDEO = 1008 |
| 4671 | 4765 | const val REQUEST_BLUETOOTH = 1009 |
| 4672 | 4766 | const val REQUEST_PICK_CONTACT = 1010 |
| 4767 | const val REQUEST_SHARE = 1011 | |
| 4673 | 4768 | private const val PERMISSION_REQUEST_START = 2000 |
| 4674 | 4769 | private const val PERMISSION_REQUEST_END = 2999 |
| 4675 | 4770 | private const val LOCATION_REQUEST_TIMEOUT_MS = 15_000L |