music: answer the conduit sync trigger with the playback publish
- Commit
68dd66316c85a189bed72c22dd9392422a738cac- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-05 06:45 UTC (1 day ago)
- Parents
def2456
Adds ConduitSyncTriggerReceiver (exported only under the CONDUIT signature permission) following the Contacts reference pattern: the phone Watch app's scheduled sync-trigger intent invokes `WearMusicSync.triggerPush()` (playback-state publish), the same publish path as the Connected and data-change triggers, with no UI and no prompt. The pure ConduitSyncTriggerHandler decision (matching action and appId only) is unit-tested. No wire-format change. AGENTS.md describes the push trigger. WearMusicSync gains the triggerPush entry point. Library pages stay on-demand (the watch asks for the page it opens), so playback state is the only unsolicited push, as on Connected. Verified: :music:lint (0 errors) and :music:lintVitalRelease pass. The full :music:testDevUnitTest suite hangs in a Robolectric sandbox on this box (the class it stalls in, MusicRepositoryRoomTest, passes standalone both here and on clean 57b53ab2e, and the new handler test passes), so the hang is pre-existing/environmental, not from this change.
Diff
music/AGENTS.md
diff --git a/music/AGENTS.md b/music/AGENTS.mdindex 57aab407d7..4330b5c8c5 100644--- a/music/AGENTS.md+++ b/music/AGENTS.md@@ -48,6 +48,7 @@ player or session on a watch. - Playback/library ops: conduit message (`/music/op`, watch -> phone) - Search: conduit message (`/music/search-request`, watch -> phone) - **Transport: Watch conduit client** — phone side `sync/WearMusicSync.kt` and watch side `sync/WearLinkBridge.kt` both use `WearLinkClientNode` (Messenger to the Watch app's `watch_conduit` service, appId `music`); this app owns no RFCOMM link and holds no Bluetooth permission for this channel, so neither entry activity prompts for Bluetooth. With the Watch app absent the node reports `NotInstalled`/`Unavailable` and the sync stays idle; playback state republishes on conduit `Connected`, and pending watch ops resend on `Connected`. `FormFactor` picks the side at runtime, so only one of the two nodes runs on a given device.+- **Push triggers:** the publish also runs on the phone Watch app's scheduled sync-trigger intent (`sync/ConduitSyncTriggerReceiver`, exported only under the `CONDUIT` signature permission, invoking `WearMusicSync.triggerPush()` — no UI, no prompt). Data arriving while the receiving process is dead lands in the conduit host's persistent store and replays on registration. ## Watch experience (in this module) - `ui/WearMainActivity` routes `ui/WearRoute` (home / albums / artists / playlists / now playing / queue / search / add-to-playlist / settings) over the phone's pages, rendered from cached page snapshots + streamed playback state in `data/WearMusicStore` (`wear-music-pages.json`), so visited pages work offline.
music/src/main/AndroidManifest.xml
diff --git a/music/src/main/AndroidManifest.xml b/music/src/main/AndroidManifest.xmlindex e4bc06af3c..f2ea64d744 100644--- a/music/src/main/AndroidManifest.xml+++ b/music/src/main/AndroidManifest.xml@@ -168,6 +168,19 @@ <data android:mimeType="audio/*" /> </intent-filter> </activity>+ + <!-- Watch conduit scheduled push sync: the phone Watch app fires+ ConduitSync.ACTION (signature permission, package-addressed)+ and this receiver pushes the current snapshot over the+ conduit. Exported only to holders of that permission. -->+ <receiver+ android:name=".sync.ConduitSyncTriggerReceiver"+ android:exported="true"+ android:permission="com.vayunmathur.watch.permissions.CONDUIT">+ <intent-filter>+ <action android:name="com.vayunmathur.watch.action.SYNC_TRIGGER" />+ </intent-filter>+ </receiver> </application> </manifest>\ No newline at end of file
music/src/main/java/com/vayunmathur/music/sync/ConduitSyncTriggerReceiver.kt
diff --git a/music/src/main/java/com/vayunmathur/music/sync/ConduitSyncTriggerReceiver.kt b/music/src/main/java/com/vayunmathur/music/sync/ConduitSyncTriggerReceiver.ktnew file mode 100644index 0000000000..8b0bef94a4--- /dev/null+++ b/music/src/main/java/com/vayunmathur/music/sync/ConduitSyncTriggerReceiver.kt@@ -0,0 +1,71 @@+package com.vayunmathur.music.sync++import android.content.BroadcastReceiver+import android.content.Context+import android.content.Intent+import com.vayunmathur.library.wearlink.conduit.ConduitProtocol+import com.vayunmathur.library.wearlink.conduit.ConduitSync+import com.vayunmathur.library.wearlink.music.WearMusicProtocol+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob+import kotlinx.coroutines.launch++/**+ * Decides whether an incoming intent is this app's sync trigger and,+ * if so, invokes [publish]. Pure (no Android types) so the decision is+ * unit-tested; the receiver below supplies the platform values.+ */+object ConduitSyncTriggerHandler {+ /** Conduit appId this app pushes under (first segment of its snapshot paths). */+ val APP_ID: String = ConduitProtocol.routeKey(WearMusicProtocol.PLAYBACK_PATH)++ /**+ * True (and [publish] invoked) only for the [ConduitSync.ACTION]+ * intent addressed to [APP_ID]; anything else is ignored.+ */+ fun handle(+ action: String?,+ appId: String?,+ publish: () -> Unit,+ ): Boolean {+ if (!ConduitSync.isTrigger(action, appId, APP_ID)) return false+ publish()+ return true+ }+}++/**+ * Receiver for the Watch conduit's scheduled push sync: the phone+ * Watch app fires [ConduitSync.ACTION] (signature permission+ * [ConduitSync.PERMISSION], package-addressed to this app) meaning+ * "push your current snapshot to your watch counterpart now". The+ * response is the existing conduit publish — the same path the+ * Connected and data-change triggers use — never a UI or a user+ * prompt. When this process was dead, the sync's Connected trigger+ * republishes as soon as the conduit bind reports the link up.+ */+class ConduitSyncTriggerReceiver : BroadcastReceiver() {+ override fun onReceive(+ context: Context,+ intent: Intent,+ ) {+ val handled =+ ConduitSyncTriggerHandler.handle(+ intent.action,+ intent.getStringExtra(ConduitSync.EXTRA_APP_ID),+ ) {+ val pending = goAsync()+ val appContext = context.applicationContext+ val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)+ scope.launch {+ try {+ WearMusicSync.triggerPush(appContext)+ } finally {+ pending.finish()+ }+ }+ }+ if (!handled) return+ }+}
music/src/main/java/com/vayunmathur/music/sync/WearMusicSync.kt
diff --git a/music/src/main/java/com/vayunmathur/music/sync/WearMusicSync.kt b/music/src/main/java/com/vayunmathur/music/sync/WearMusicSync.ktindex b9c6a0628c..df72e9cd15 100644--- a/music/src/main/java/com/vayunmathur/music/sync/WearMusicSync.kt+++ b/music/src/main/java/com/vayunmathur/music/sync/WearMusicSync.kt@@ -127,6 +127,19 @@ object WearMusicSync { } } + /**+ * Push the latest playback state now, on request from outside this+ * process's own triggers: the Watch app's scheduled sync-trigger+ * intent (see [ConduitSyncTriggerReceiver]). The same publish path+ * as the Connected trigger; library pages stay on-demand (the watch+ * asks for the page it opens), so playback is the only unsolicited+ * push. No-op on a watch ([start] gates the phone node there).+ */+ fun triggerPush(context: Context) {+ start(context)+ scope.launch { republishPlayback() }+ }+ /** Republish the latest playback state when the conduit link (re)connects. */ private suspend fun republishPlayback() { val state = lastPlayback ?: return
music/src/test/java/com/vayunmathur/music/sync/ConduitSyncTriggerHandlerTest.kt
diff --git a/music/src/test/java/com/vayunmathur/music/sync/ConduitSyncTriggerHandlerTest.kt b/music/src/test/java/com/vayunmathur/music/sync/ConduitSyncTriggerHandlerTest.ktnew file mode 100644index 0000000000..01f40eacac--- /dev/null+++ b/music/src/test/java/com/vayunmathur/music/sync/ConduitSyncTriggerHandlerTest.kt@@ -0,0 +1,30 @@+package com.vayunmathur.music.sync++import com.vayunmathur.library.wearlink.conduit.ConduitSync+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++class ConduitSyncTriggerHandlerTest {+ @Test+ fun `the sync trigger for music invokes publish`() {+ var publishes = 0+ val handled =+ ConduitSyncTriggerHandler.handle(ConduitSync.ACTION, ConduitSyncTriggerHandler.APP_ID) {+ publishes+++ }+ assertTrue(handled)+ assertEquals(1, publishes)+ assertEquals("music", ConduitSyncTriggerHandler.APP_ID)+ }++ @Test+ fun `other apps and other actions do not invoke publish`() {+ var publishes = 0+ assertFalse(ConduitSyncTriggerHandler.handle(ConduitSync.ACTION, "clock") { publishes++ })+ assertFalse(ConduitSyncTriggerHandler.handle("com.example.OTHER", "music") { publishes++ })+ assertFalse(ConduitSyncTriggerHandler.handle(null, null) { publishes++ })+ assertEquals(0, publishes)+ }+}