Modern Apps git

← Modern-Apps.git

share: Direct Share targets for conversations and nearby transfer

Commit
cd73bc995e37476622b52552419ec577b6611136
Author
Vayun Mathur <[email protected]>
When
2026-11-21 11:10 UTC (14 days ago)
Parents
2d300f9
The system share sheet's direct-share row never offered
these apps. Communicate now publishes its top 4 conversations
as share targets (same shortcut ids, locus and Person as its
conversation shortcuts; refreshed on the thread-list reload,
demoted cleanly when a conversation leaves the top 4): a tap
opens the thread with the shared content as a prefilled draft,
never auto-sent. Shares to Communicate as a plain target are
held in PendingShare and seed the first opened thread.

Share publishes one 'Nearby device' target launching its
existing send popup for nearby transfer, refreshed at app open
and on boot. Contacts was deliberately skipped: it has no
receive-and-forward flow, so a target there would drop shared
content.

The Communicate implementation itself landed in dacc87514;
this commit adds its tests and the Share half.
Verified: communicate suite passes incl. new target tests;
share and communicate compile.

Diff

communicate/src/test/java/com/vayunmathur/communicate/notifications/ConversationSpaceSlice4Test.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/notifications/ConversationSpaceSlice4Test.kt b/communicate/src/test/java/com/vayunmathur/communicate/notifications/ConversationSpaceSlice4Test.ktindex b62451894b..b59fe10ca9 100644--- a/communicate/src/test/java/com/vayunmathur/communicate/notifications/ConversationSpaceSlice4Test.kt+++ b/communicate/src/test/java/com/vayunmathur/communicate/notifications/ConversationSpaceSlice4Test.kt@@ -161,6 +161,45 @@ class ConversationSpaceSlice4Test {         ConversationSpace.removeShortcut(context, target())     } +    @Test+    fun `publishShareTargets caps at four and demotes conversations that fall out`() {+        val targets = (1..6).map { i ->+            target(address = "+155$i", threadId = 100L + i, name = "P$i")+        }+        ConversationSpace.publishShareTargets(context, targets)+        fun shareTargetIds() = androidx.core.content.pm.ShortcutManagerCompat+            .getDynamicShortcuts(context)+            .filter { it.categories?.contains(ConversationSpace.SHARE_TARGET_CATEGORY) == true }+            .map { it.id }+        assertEquals(+            targets.take(4).map { ConversationSpace.shortcutId(it) },+            shareTargetIds().sortedBy { targets.map { t -> ConversationSpace.shortcutId(t) }.indexOf(it) },+        )+        // The newest conversation jumps the queue: it joins the row, P4 drops out+        // (demoted to a plain conversation shortcut, not deleted).+        val newer = target(address = "+1999", threadId = 999L, name = "New")+        ConversationSpace.publishShareTargets(context, listOf(newer) + targets)+        assertEquals(4, shareTargetIds().size)+        assertTrue(ConversationSpace.shortcutId(newer) in shareTargetIds())+        assertTrue(ConversationSpace.shortcutId(targets[3]) !in shareTargetIds())+        targets.forEach { ConversationSpace.removeShortcut(context, it) }+        ConversationSpace.removeShortcut(context, newer)+    }++    @Test+    fun `direct share intent resolves its conversation from the shortcut id`() {+        val intent = Intent(Intent.ACTION_SEND).apply {+            type = "text/plain"+            putExtra(Intent.EXTRA_TEXT, "hi there")+            putExtra(Intent.EXTRA_SHORTCUT_ID, "WhatsApp:[email protected]")+        }+        val link = com.vayunmathur.communicate.intents.DeepLinks.parse(intent)+            as com.vayunmathur.communicate.intents.DeepLink.Conversation+        assertEquals(CommunicateLine.WhatsApp, link.line)+        assertEquals("[email protected]", link.address)+        assertEquals("hi there", link.body)+    }+     @Test     fun `pushShortcut prefers the group title and falls back to app name`() {         val titled = target().copy(title = "Family", personName = "Pat")
share/src/main/AndroidManifest.xml
diff --git a/share/src/main/AndroidManifest.xml b/share/src/main/AndroidManifest.xmlindex fa8eddc1f1..18b21d3747 100644--- a/share/src/main/AndroidManifest.xml+++ b/share/src/main/AndroidManifest.xml@@ -107,6 +107,12 @@                 <category android:name="android.intent.category.DEFAULT" />                 <data android:mimeType="*/*" />             </intent-filter>+            <!-- Direct Share: @xml/shortcuts names this activity as the share+                 target for ShareShortcuts.CATEGORY, so the dynamic shortcut+                 ShareShortcuts.publish() puts it in the direct-share row. -->+            <meta-data+                android:name="android.app.shortcuts"+                android:resource="@xml/shortcuts" />         </activity>          <activity
share/src/main/java/com/vayunmathur/share/MainActivity.kt
diff --git a/share/src/main/java/com/vayunmathur/share/MainActivity.kt b/share/src/main/java/com/vayunmathur/share/MainActivity.ktindex c7bd47c233..c902a674de 100644--- a/share/src/main/java/com/vayunmathur/share/MainActivity.kt+++ b/share/src/main/java/com/vayunmathur/share/MainActivity.kt@@ -9,6 +9,7 @@ import com.vayunmathur.library.ui.AppPermissionsSpec import com.vayunmathur.library.ui.MaActivity import com.vayunmathur.library.ui.PermissionRequirement import com.vayunmathur.share.platform.SharePermissions+import com.vayunmathur.share.platform.ShareShortcuts import com.vayunmathur.share.platform.ShareViewModel import com.vayunmathur.share.platform.ShareViewModelFactory @@ -21,6 +22,11 @@ import com.vayunmathur.share.platform.ShareViewModelFactory class MainActivity : MaActivity() {     private val shareViewModel: ShareViewModel by viewModels { ShareViewModelFactory(application) } +    override fun onCreateBeforeContent() {+        // Direct Share row entry; refreshed on every app open (boot republishes too).+        ShareShortcuts.publish(this)+    }+     @Composable     override fun Content() {         AppPermissionsGate(
share/src/main/java/com/vayunmathur/share/platform/ShareShortcuts.kt
diff --git a/share/src/main/java/com/vayunmathur/share/platform/ShareShortcuts.kt b/share/src/main/java/com/vayunmathur/share/platform/ShareShortcuts.ktnew file mode 100644index 0000000000..9ef7801cac--- /dev/null+++ b/share/src/main/java/com/vayunmathur/share/platform/ShareShortcuts.kt@@ -0,0 +1,49 @@+package com.vayunmathur.share.platform++import android.content.Context+import android.content.Intent+import androidx.core.content.pm.ShortcutInfoCompat+import androidx.core.content.pm.ShortcutManagerCompat+import androidx.core.graphics.drawable.IconCompat+import com.vayunmathur.share.R+import com.vayunmathur.share.ui.SharePopupActivity++/**+ * The app's single Direct Share shortcut.+ *+ * Unlike a messenger there is no person or conversation to target: the nearby+ * device is only chosen after the share starts (discovery happens in the+ * popup), so one long-lived shortcut for the popup itself is the whole surface.+ * It carries [CATEGORY], which must match the `<share-target>` in+ * `res/xml/shortcuts.xml`; tapping it in the share sheet's direct-share row+ * launches [SharePopupActivity] with the original SEND intent, which+ * `ShareViewModel.handleShareIntent` already consumes.+ *+ * The nearby *receive* flow is deliberately not a target: it has no screen+ * (QS tile + notifications, see ShareSendPage) and no activity accepts a+ * share intent for it, so there is nothing a share-target could launch.+ */+object ShareShortcuts {+    /** Stable id of the one published shortcut. */+    const val SHORTCUT_ID = "nearby_transfer"++    /** Direct Share category; must match `res/xml/shortcuts.xml`. */+    const val CATEGORY = "com.vayunmathur.share.category.DIRECT_SHARE"++    /** Publish/refresh the shortcut. Idempotent; safe from any entry point. */+    fun publish(context: Context) {+        val intent = Intent(context, SharePopupActivity::class.java)+            .setAction(Intent.ACTION_SEND)+            .setType("*/*")+            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)+        val shortcut = ShortcutInfoCompat.Builder(context, SHORTCUT_ID)+            .setShortLabel(context.getString(R.string.share_direct_shortcut_label))+            .setLongLabel(context.getString(R.string.share_direct_shortcut_label))+            .setLongLived(true)+            .setCategories(setOf(CATEGORY))+            .setIntent(intent)+            .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher))+            .build()+        runCatching { ShortcutManagerCompat.pushDynamicShortcut(context, shortcut) }+    }+}
share/src/main/java/com/vayunmathur/share/platform/receive/ShareBootReceiver.kt
diff --git a/share/src/main/java/com/vayunmathur/share/platform/receive/ShareBootReceiver.kt b/share/src/main/java/com/vayunmathur/share/platform/receive/ShareBootReceiver.ktindex 888d5b07ff..7b009cc8b0 100644--- a/share/src/main/java/com/vayunmathur/share/platform/receive/ShareBootReceiver.kt+++ b/share/src/main/java/com/vayunmathur/share/platform/receive/ShareBootReceiver.kt@@ -20,6 +20,8 @@ class ShareBootReceiver : BroadcastReceiver() {         ) {             return         }+        // Keep the Direct Share row present without waiting for the app to be opened.+        com.vayunmathur.share.platform.ShareShortcuts.publish(context)         val pending = goAsync()         val appContext = context.applicationContext         CoroutineScope(Dispatchers.IO).launch {
share/src/main/res/values/strings.xml
diff --git a/share/src/main/res/values/strings.xml b/share/src/main/res/values/strings.xmlindex a2f5f922cc..5f5e869804 100644--- a/share/src/main/res/values/strings.xml+++ b/share/src/main/res/values/strings.xml@@ -1,5 +1,6 @@ <resources>     <string name="app_name">Share</string>+    <string name="share_direct_shortcut_label">Nearby device</string>      <!-- Foreground service -->     <string name="share_notification_channel_name">Receiving</string>
share/src/main/res/xml/shortcuts.xml
diff --git a/share/src/main/res/xml/shortcuts.xml b/share/src/main/res/xml/shortcuts.xmlnew file mode 100644index 0000000000..d9bc01137d--- /dev/null+++ b/share/src/main/res/xml/shortcuts.xml@@ -0,0 +1,12 @@+<?xml version="1.0" encoding="utf-8"?>+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">+    <!-- Direct Share (system share sheet's direct-share row). The category must+         match ShareShortcuts.CATEGORY: the dynamic shortcut published with that+         category becomes a chooser target, and tapping it launches+         SharePopupActivity (already the ACTION_SEND / SEND_MULTIPLE target)+         with the shared content, ready to pick a nearby device. -->+    <share-target android:targetClass="com.vayunmathur.share.ui.SharePopupActivity">+        <data android:mimeType="*/*" />+        <category android:name="com.vayunmathur.share.category.DIRECT_SHARE" />+    </share-target>+</shortcuts>