Modern Apps git

← Modern-Apps.git

music: migrate wear sync to the Watch conduit client

Commit
d2f9e5b3fd133b42c0b3077518511466a029edde
Author
Vayun Mathur <[email protected]>
When
2026-12-04 19:45 UTC (1 day ago)
Parents
d41295a
The music phone<->watch sync owned a direct RFCOMM WearLinkNode on
both sides, so the app held the Bluetooth path itself and the watch
entry requested the Bluetooth permission through WearShellActivity.
The clock and calendar pairs already moved to the Watch conduit, and
music now follows the same shape.

Both sides construct WearLinkClientNode with the conduit appId
derived by ConduitProtocol.routeKey from the music paths (music),
not the RFCOMM handshake capability. The phone side serves pages,
streams playback and executes ops exactly as before, and republishes
the latest playback state when the conduit reports Connected. The
watch side lands pages and playback in WearMusicStore, sends page,
search and op messages, and resends pending ops on Connected, so the
watch UI still only binds phone-reported state and sends ops. With
the Watch app absent the nodes report NotInstalled/Unavailable and
the sync stays idle. WearMainActivity no longer goes through
WearShellActivity, so nothing on this app's path requests Bluetooth;
its route state and store warm-up now live in the activity itself,
matching the calendar watch entry.

The wire protocol in library.wearlink.music and its golden-JSON
tests are unchanged, and music/AGENTS.md describes the conduit
client in the present tense.

Verified in the worktree: :music:lint and :music:lintVitalRelease
pass, and the full :music:testDevUnitTest suite passes (281 tests
in 54 classes, including the WearMusicProtocol golden-JSON,
WearPendingOps and WearAlbumOrder tests).

Diff

music/AGENTS.md
diff --git a/music/AGENTS.md b/music/AGENTS.mdindex 547f86bdb3..57aab407d7 100644--- a/music/AGENTS.md+++ b/music/AGENTS.md@@ -36,21 +36,21 @@ phones and watches alike. `platform/FormFactor.kt` reads `PackageManager.FEATURE_WATCH` at runtime: `MainActivity` (the sole launcher entry) forwards watches to `ui/WearMainActivity` before any phone state loads; phones get the tab UI. The phone-only entry points-gate on the same signal: `sync/WearMusicSync.start` (phone WearLink-node) is a no-op on a watch, and `service/PlaybackService` creates no+gate on the same signal: `sync/WearMusicSync.start` (phone conduit+client node) is a no-op on a watch, and `service/PlaybackService` creates no player or session on a watch. -## Wear sync (phone <-> watch)+## Wear sync (Watch conduit client) **Protocol:** shared wire types live in `:library:wearlink` at `com.vayunmathur.library.wearlink.music.WearMusicProtocol`; the golden-JSON tests in this module pin the shared code's wire format.-- Page snapshots: WearLink data item (`/music/page`, phone -> watch), requested by message (`/music/page-request`, watch -> phone)-- Now-playing: WearLink data item (`/music/playback`, phone -> watch)-- Watch state: WearLink data item (`/music/watch-state`, watch -> phone)-- Playback/library ops: WearLink message (`/music/op`, watch -> phone)-- Search: WearLink message (`/music/search-request`, watch -> phone)-- Transport: :library:wearlink (RFCOMM, no Play services); app-lifecycle-bound. Phone side is `sync/WearMusicSync.kt`, watch side is `sync/WearLinkBridge.kt`; `FormFactor` picks the side at runtime.+- Page snapshots: conduit data item (`/music/page`, phone -> watch), requested by message (`/music/page-request`, watch -> phone)+- Now-playing: conduit data item (`/music/playback`, phone -> watch)+- Watch state: conduit data item (`/music/watch-state`, watch -> phone)+- 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.  ## Watch experience (in this module)-- `ui/WearMainActivity` (a `WearShellActivity`) 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.+- `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. - The phone owns the catalog and is the only audio player: the watch stores no music files and no whole catalog, and every playback action is an op executed by the phone's playback service; the watch UI reflects the phone-reported state and never claims an unconfirmed result. - Watch listing screenshots are the `group = "wear"` previews in `src/screenshotTest/.../ui/WearMetadataPreviews.kt`, collected by `:music:metadata` into `metadata_data/photos/music/wear/`; phone shots use the `phone` bucket. @@ -81,11 +81,11 @@ player or session on a watch. - com/vayunmathur/music/service/MusicLibraryTree.kt - com/vayunmathur/music/service/CastingPlayer.kt - com/vayunmathur/music/service/CastQueue.kt-### sync/ — wear companion sync (both sides)-- com/vayunmathur/music/sync/WearMusicSync.kt — phone WearLink node; page publish, playback stream, op execution-- com/vayunmathur/music/sync/WearLinkBridge.kt — watch WearLink node; page/playback receive, op send, watch-state publish+### sync/ — wear companion sync (both sides, one APK)+- com/vayunmathur/music/sync/WearMusicSync.kt — phone conduit client node; page publish, playback stream, op execution+- com/vayunmathur/music/sync/WearLinkBridge.kt — watch conduit client node; page/playback receive, op send, watch-state publish ### Watch experience (ui/ + data/)-- com/vayunmathur/music/ui/WearMainActivity.kt — watch entry (WearShellActivity)+- com/vayunmathur/music/ui/WearMainActivity.kt — watch entry - com/vayunmathur/music/ui/WearRoute.kt, WearMusicRoot.kt, WearHomeScreen.kt, WearPageLoader.kt, WearNowPlayingScreen.kt, WearTrackListScreen.kt, WearAlbumListScreen.kt, WearArtistListScreen.kt, WearPlaylistListScreen.kt, WearQueueScreen.kt, WearSearchScreen.kt, WearAddToPlaylistScreen.kt, WearSettingsScreen.kt - com/vayunmathur/music/data/WearMusicStore.kt — visited page snapshots + latest playback state (`wear-music-pages.json`) ### intents/ — assistant playback IPC
music/src/main/java/com/vayunmathur/music/sync/WearLinkBridge.kt
diff --git a/music/src/main/java/com/vayunmathur/music/sync/WearLinkBridge.kt b/music/src/main/java/com/vayunmathur/music/sync/WearLinkBridge.ktindex 47d2fc7f35..bd7d265f1a 100644--- a/music/src/main/java/com/vayunmathur/music/sync/WearLinkBridge.kt+++ b/music/src/main/java/com/vayunmathur/music/sync/WearLinkBridge.kt@@ -3,8 +3,9 @@ package com.vayunmathur.music.sync import android.content.Context import com.vayunmathur.library.log.Log import com.vayunmathur.library.wearlink.WearLinkListener-import com.vayunmathur.library.wearlink.WearLinkNode import com.vayunmathur.library.wearlink.WearLinkState+import com.vayunmathur.library.wearlink.conduit.ConduitProtocol+import com.vayunmathur.library.wearlink.conduit.WearLinkClientNode import com.vayunmathur.music.data.WearMusicStore import com.vayunmathur.library.wearlink.music.WearMusicOp import com.vayunmathur.library.wearlink.music.WearMusicProtocol@@ -20,25 +21,29 @@ import kotlinx.coroutines.launch import kotlinx.serialization.encodeToString  /**- * Watch side of WearLink for Music: page snapshots + playback state land in- * [WearMusicStore]; page/search requests and playback ops are messages to- * the phone. Delivery is bound to this process being alive; pending ops are- * resent whenever a session connects.+ * Watch side of the Watch conduit client for Music: page snapshots ++ * playback state land in [WearMusicStore]; page/search requests and+ * playback ops are messages to the phone. Transport is+ * `WearLinkClientNode` (Messenger to the Watch app's conduit service);+ * this app owns no RFCOMM link and never owns playback — the UI binds+ * the phone-reported state in the store and sends ops. Delivery is+ * bound to this process being alive; pending ops are resent whenever+ * the conduit reports `Connected`.  */ object WearLinkBridge {     private const val TAG = "WearMusicBridge"     private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)      @Volatile-    private var node: WearLinkNode? = null+    private var node: WearLinkClientNode? = null -    /** The process-wide WearLink node for the music companion, started on first use. */-    fun get(context: Context): WearLinkNode {+    /** The process-wide conduit client node for the music companion, started on first use. */+    fun get(context: Context): WearLinkClientNode {         val appContext = context.applicationContext         return node ?: synchronized(this) { node ?: create(appContext).also { node = it } }     } -    private fun create(context: Context): WearLinkNode {+    private fun create(context: Context): WearLinkClientNode {         val listener = object : WearLinkListener {             override fun onData(path: String, body: ByteArray) {                 val text = body.decodeToString()@@ -54,7 +59,13 @@ object WearLinkBridge {                 }             }         }-        val created = WearLinkNode(context, WearMusicProtocol.CAPABILITY, listener)+        // Conduit appId = first path segment of the music paths (the+        // ConduitProtocol route key), not the RFCOMM handshake capability.+        val created = WearLinkClientNode(+            context,+            ConduitProtocol.routeKey(WearMusicProtocol.PAGE_PATH),+            listener,+        )         scope.launch {             var wasConnected = false             created.state.collect { linkState ->
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 d8e4753109..b9c6a0628c 100644--- a/music/src/main/java/com/vayunmathur/music/sync/WearMusicSync.kt+++ b/music/src/main/java/com/vayunmathur/music/sync/WearMusicSync.kt@@ -2,7 +2,9 @@ package com.vayunmathur.music.sync  import android.content.Context import com.vayunmathur.library.wearlink.WearLinkListener-import com.vayunmathur.library.wearlink.WearLinkNode+import com.vayunmathur.library.wearlink.WearLinkState+import com.vayunmathur.library.wearlink.conduit.ConduitProtocol+import com.vayunmathur.library.wearlink.conduit.WearLinkClientNode import com.vayunmathur.music.data.MusicRepository import com.vayunmathur.library.wearlink.music.WearAlbum import com.vayunmathur.library.wearlink.music.WearArtist@@ -29,18 +31,26 @@ import kotlinx.coroutines.launch import kotlinx.serialization.encodeToString  /**- * Phone side of the Music WearLink companion: serves bounded page snapshots- * from the phone library, streams now-playing state, and executes watch ops- * in the phone playback service. The catalog and all audio stay here.+ * Phone side of the Music wear sync over the Watch conduit client+ * (:library:wearlink `WearLinkClientNode`: a Messenger binding to the+ * Watch app's conduit service, not an RFCOMM link this app owns): serves+ * bounded page snapshots from the phone library, streams now-playing+ * state, and executes watch ops in the phone playback service. The+ * catalog and all audio stay here. With the Watch app absent the node+ * reports `NotInstalled`/`Unavailable` and the sync stays idle; playback+ * state republishes on conduit `Connected`.  */ object WearMusicSync {     private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)      @Volatile-    private var node: WearLinkNode? = null+    private var node: WearLinkClientNode? = null     private var generation = 0L -    /** Start phone-side WearLink (call from the app's main activity once). No-op on a watch: there the watch-side bridge ([com.vayunmathur.music.sync.WearLinkBridge]) runs instead, and this phone node (catalog, playback) must never start. */+    @Volatile+    private var lastPlayback: WearPlaybackState? = null++    /** Start phone-side conduit sync (call from the app's main activity once). No-op on a watch: there the watch-side bridge ([com.vayunmathur.music.sync.WearLinkBridge]) runs instead, and this phone node (catalog, playback) must never start. */     fun start(context: Context) {         if (FormFactor.isWatch(context)) return         val appContext = context.applicationContext@@ -70,7 +80,23 @@ object WearMusicSync {                     }                 }             }-            node = WearLinkNode(appContext, WearMusicProtocol.CAPABILITY, listener).also { it.start() }+            // Conduit appId = first path segment of the music paths (the+            // ConduitProtocol route key), not the RFCOMM handshake capability.+            val created = WearLinkClientNode(+                appContext,+                ConduitProtocol.routeKey(WearMusicProtocol.PAGE_PATH),+                listener,+            )+            node = created+            scope.launch {+                var wasConnected = false+                created.state.collect { linkState ->+                    val connected = linkState is WearLinkState.Connected+                    if (connected && !wasConnected) republishPlayback()+                    wasConnected = connected+                }+            }+            created.start()             observePlayback(appContext)         }     }@@ -92,6 +118,7 @@ object WearMusicSync {                     repeatMode = repeat,                 )             }.collect { state ->+                lastPlayback = state                 node?.putData(                     WearMusicProtocol.PLAYBACK_PATH,                     WearMusicProtocol.json.encodeToString(state).encodeToByteArray(),@@ -100,6 +127,15 @@ object WearMusicSync {         }     } +    /** Republish the latest playback state when the conduit link (re)connects. */+    private suspend fun republishPlayback() {+        val state = lastPlayback ?: return+        node?.putData(+            WearMusicProtocol.PLAYBACK_PATH,+            WearMusicProtocol.json.encodeToString(state).encodeToByteArray(),+        )+    }+     private suspend fun publishPage(context: Context, request: WearPageRequest) {         val snapshot = buildPage(context, request)         node?.putData(
music/src/main/java/com/vayunmathur/music/ui/WearMainActivity.kt
diff --git a/music/src/main/java/com/vayunmathur/music/ui/WearMainActivity.kt b/music/src/main/java/com/vayunmathur/music/ui/WearMainActivity.ktindex e11a3a97a1..54b3900902 100644--- a/music/src/main/java/com/vayunmathur/music/ui/WearMainActivity.kt+++ b/music/src/main/java/com/vayunmathur/music/ui/WearMainActivity.kt@@ -1,7 +1,13 @@ package com.vayunmathur.music.ui -import androidx.compose.runtime.Composable-import com.vayunmathur.library.wearlink.WearShellActivity+import android.os.Bundle+import androidx.activity.ComponentActivity+import androidx.activity.compose.setContent+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.wear.compose.material.MaterialTheme import com.vayunmathur.music.data.WearMusicStore import com.vayunmathur.music.sync.WearLinkBridge @@ -11,15 +17,22 @@ import com.vayunmathur.music.sync.WearLinkBridge  * playback state, so visited pages work offline. Playback itself always  * happens on the phone; this UI only sends ops and reflects state.  */-class WearMainActivity : WearShellActivity<WearRoute, WearMusicStore>(WearRoute.Home) {+class WearMainActivity : ComponentActivity() { -    override fun createStore() = WearMusicStore.get(this).also {-        WearLinkBridge.get(this)-        it.publishWatchState()-    }--    @Composable-    override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {-        WearMusicRoot(store = store, route = route, onRoute = navigate)+    override fun onCreate(savedInstanceState: Bundle?) {+        super.onCreate(savedInstanceState)+        // No Bluetooth prompt here: the Watch app owns the conduit link+        // and its permission. With the Watch app absent the bridge stays+        // NotInstalled/Unavailable and the local store keeps working.+        val store = WearMusicStore.get(this).also {+            WearLinkBridge.get(this)+            it.publishWatchState()+        }+        setContent {+            MaterialTheme {+                var route by remember { mutableStateOf<WearRoute>(WearRoute.Home) }+                WearMusicRoot(store = store, route = route, onRoute = { route = it })+            }+        }     } }