contacts: migrate wear sync to the Watch conduit client
- Commit
e5ed3a20ed625e4898af64c971c09cff9a7aa1e8- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-04 19:45 UTC (1 day ago)
- Parents
d2f9e5b
Convert both sides of the contacts phone<->watch sync from the direct RFCOMM WearLinkNode to the Watch conduit client (WearLinkClientNode), following the already-migrated clock pair. Phone side (sync/WearContactsSync): the node is now a WearLinkClientNode registered under the conduit appId "contacts" (the ConduitProtocol route key of the snapshot paths, not the RFCOMM handshake capability). Presence stays driven by the conduit-reported Connected state, and the snapshot now also republishes on the rising Connected edge, in addition to book changes and watch requests. With the Watch app absent the node reports NotInstalled/Unavailable and the sync stays idle. Watch side (sync/WearLinkBridge + sync/WearWatchStatePublisher): same node swap; the manifest/chunk reassembler feed, watch-state publish, snapshot requests and handoffs are unchanged. Callers (WearDeviceCapabilitiesProbe.isConnected, WearMainActivity) see the same putData/sendMessage/isConnected API. MainActivity no longer requests the Bluetooth permission on this app's path: the Watch app owns the conduit link and its permission. The wire protocol (library.wearlink.contacts types, golden-JSON tests) is untouched. contacts/AGENTS.md wear sections now describe the conduit-client state in the present tense. Verification (worktree, JDK21, --max-workers=128 -Xmx64g): ./gradlew :contacts:lint :contacts:lintVitalRelease green; :contacts:testDevUnitTest runs 622 tests with only the two pre-existing InsertOrEdit failures (InsertOrEditIntentActivity- Pass2Test.insertOrEditShowsNumber and DeepInteractionsPass2Test. insertOrEditShowsContactsAndCreateNewRoutes), reproduced identically on a clean base worktree without this change.
Diff
contacts/AGENTS.md
diff --git a/contacts/AGENTS.md b/contacts/AGENTS.mdindex 1342d9464f..6f8896dc0b 100644--- a/contacts/AGENTS.md+++ b/contacts/AGENTS.md@@ -21,8 +21,8 @@ _Install: ./install contacts (dev by default)._ ## One APK, two form factors (phone + watch) This module is the single Contacts APK for phones and watches: same-`applicationId = "com.vayunmathur.contacts"` and signing on both, as the-WearLink handshake requires. The manifest deliberately declares no+`applicationId = "com.vayunmathur.contacts"` and signing on both.+The manifest deliberately declares no `android.hardware.type.watch` feature (required=true would block phones; required=false is disallowed for this feature), plus touchscreen not-required and@@ -35,10 +35,10 @@ any phone state loads; phones get the list/detail/edit UI. The watch activity has no launcher entry of its own (`exported=false`). There are no watch receivers in this module. -## Wear sync (WearLink)+## Wear sync (Watch conduit client) **Protocol:** shared wire types live in `:library:wearlink` at `com.vayunmathur.library.wearlink.contacts.WearContactsProtocol`; the golden-JSON test in this module (`domain/WearContactsProtocolTest`) pins the shared code's wire format.-- Snapshot: phone -> watch as chunked data items plus a manifest (`/contacts/snapshot/...`); watch state: watch -> phone (`/contacts/watch-state`); snapshot request and call/message handoff are watch -> phone messages.-- Phone side `sync/WearContactsSync.kt` publishes the snapshot from `MainActivity` whenever the book changes; watch side `sync/WearLinkBridge.kt` feeds `data/WearContactsStore`'s reassembler and `sync/WearWatchStatePublisher.kt` publishes watch state / requests / handoffs. With no watch or no live WearLink session the sync stays idle and never crashes the phone UI.+- Snapshot: phone -> watch as chunked conduit data items plus a manifest (`/contacts/snapshot/...`); watch state: watch -> phone (`/contacts/watch-state`); snapshot request and call/message handoff are watch -> phone conduit messages.+- **Transport: Watch conduit client** — phone side `sync/WearContactsSync.kt` and watch side `sync/WearLinkBridge.kt` (with `sync/WearWatchStatePublisher.kt` publishing watch state / requests / handoffs) use `WearLinkClientNode` (Messenger to the Watch app's `watch_conduit` service, appId `contacts`); 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 and never crashes the phone UI; snapshots republish on conduit `Connected` and on book changes. ## Watch experience (in this module) - `ui/WearMainActivity` (a `WearShellActivity`) routes `ui/WearRoute` over the contact list and a detail carrying the contact id.@@ -120,9 +120,9 @@ are no watch receivers in this module. - com/vayunmathur/contacts/ui/WearMainActivity.kt — watch entry (WearShellActivity, exported=false) - com/vayunmathur/contacts/ui/WearRoute.kt, WearContactsApp.kt, WearContactListScreen.kt, WearContactDetailScreen.kt - com/vayunmathur/contacts/data/WearContactsStore.kt — watch snapshot store + reassembler-- com/vayunmathur/contacts/sync/WearContactsSync.kt — phone snapshot publisher-- com/vayunmathur/contacts/sync/WearLinkBridge.kt — watch snapshot receiver-- com/vayunmathur/contacts/sync/WearWatchStatePublisher.kt — watch state / request / handoff publisher+- com/vayunmathur/contacts/sync/WearContactsSync.kt — phone conduit client node; snapshot publish, watch-state receive, request/handoff handling+- com/vayunmathur/contacts/sync/WearLinkBridge.kt — watch conduit client node; snapshot receive into the store reassembler+- com/vayunmathur/contacts/sync/WearWatchStatePublisher.kt — watch state / request / handoff publisher over the conduit client ### intents/ — assistant IPC - com/vayunmathur/contacts/intents/GetIntent.kt - com/vayunmathur/contacts/intents/InsertIntent.kt
contacts/build.gradle.kts
diff --git a/contacts/build.gradle.kts b/contacts/build.gradle.ktsindex c1016d1563..798e49bc71 100644--- a/contacts/build.gradle.kts+++ b/contacts/build.gradle.kts@@ -28,7 +28,7 @@ dependencies { // VCF export/import now uses java.io BufferedReader/Writer – no okio needed implementation(libs.libphonenumber) // Both sides of the Wear OS sync live in this one APK (same- // applicationId and signing, as the WearLink handshake requires):+ // applicationId and signing on phone and watch): // sync/WearContactsSync publishes the phone snapshot, sync/WearLinkBridge // receives it on a watch. FormFactor picks the side at runtime. implementation(project(":library:wearlink"))
contacts/src/main/java/com/vayunmathur/contacts/MainActivity.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/MainActivity.kt b/contacts/src/main/java/com/vayunmathur/contacts/MainActivity.ktindex 53772c85bc..afb8936074 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/MainActivity.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/MainActivity.kt@@ -107,7 +107,8 @@ class MainActivity : MaActivity() { override fun onCreateBeforeContent() { if (routedToWear) return- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)+ // No Bluetooth prompt here: the Watch app owns the conduit link+ // and its permission (see sync/WearContactsSync). handleIntent(intent) }
contacts/src/main/java/com/vayunmathur/contacts/sync/WearContactsSync.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/sync/WearContactsSync.kt b/contacts/src/main/java/com/vayunmathur/contacts/sync/WearContactsSync.ktindex 4ffd2c1a1b..0738efccbe 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/sync/WearContactsSync.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/sync/WearContactsSync.kt@@ -14,8 +14,9 @@ import com.vayunmathur.library.wearlink.contacts.WearWatchState import com.vayunmathur.library.wearlink.contacts.WearContact 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 kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow@@ -46,16 +47,20 @@ fun loadWearContacts(context: Context): List<WearContact> { } /**- * Phone side of the contacts wear sync over WearLink (RFCOMM,- * :library:wearlink): presence is a completed handshake, the snapshot is a- * chunked data item (generation/manifest discipline retained in the payload- * protocol), watch state is a data item, and request/handoff are messages- * handled here while this process lives.+ * Phone side of the contacts 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):+ * presence is the peer-link Connected state the conduit reports, the+ * snapshot is a chunked data item (generation/manifest discipline retained+ * in the payload protocol), watch state is a data item, and+ * request/handoff are messages handled here while this process lives. *- * "No Bluetooth permission / no watch / watch without Contacts" is a normal- * state ([WearPresence.NO_WATCH]), never a crash. There is no Play-services- * listener service any more: watch-originated requests arriving while the- * phone app is dead wait until it next runs and reconnects.+ * "No Watch app / no watch / watch without Contacts" is a normal state+ * ([WearPresence.NO_WATCH]), never a crash: with the Watch app absent the+ * node reports `NotInstalled`/`Unavailable` and the sync stays idle.+ * There is no Play-services listener service: watch-originated requests+ * arriving while the phone app is dead wait until it next runs, and the+ * snapshot republishes on reconnect and on every book change. */ class WearContactsSync( private val context: Context,@@ -94,7 +99,13 @@ class WearContactsSync( } } - private val node = WearLinkNode(context, WearContactsProtocol.CAPABILITY, listener)+ // Conduit appId = first path segment of the contacts paths (the+ // ConduitProtocol route key), not the RFCOMM handshake capability.+ private val node = WearLinkClientNode(+ context,+ ConduitProtocol.routeKey(WearContactsProtocol.MANIFEST_PATH),+ listener,+ ) private var started = false @@ -103,8 +114,10 @@ class WearContactsSync( started = true scope.launch { node.state.collect { linkState ->+ val wasConnected = watchConnected watchConnected = linkState is WearLinkState.Connected refreshPresence()+ if (watchConnected && !wasConnected) republish() } } node.start()@@ -130,7 +143,7 @@ class WearContactsSync( // Chunks first, manifest last — sent as one data item for the // manifest and one per chunk path, preserving the watch-side // reassembler's "apply only when the manifest's count is- // complete" rule over WearLink's generation-checked documents.+ // complete" rule over the conduit's data items. var allSent = true for (chunk in chunks) { val sent = node.putData(@@ -144,7 +157,7 @@ class WearContactsSync( WearContactsProtocol.json.encodeToString(manifest).encodeToByteArray(), ) if (!allSent || !manifestSent) {- Log.status(TAG, "snapshot publish not fully delivered (no live WearLink session)")+ Log.status(TAG, "snapshot publish not fully delivered (conduit link not connected)") } } }
contacts/src/main/java/com/vayunmathur/contacts/sync/WearLinkBridge.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/sync/WearLinkBridge.kt b/contacts/src/main/java/com/vayunmathur/contacts/sync/WearLinkBridge.ktindex 68cc88a311..b7b5d097bd 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/sync/WearLinkBridge.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/sync/WearLinkBridge.kt@@ -7,24 +7,27 @@ import com.vayunmathur.library.wearlink.contacts.WearContactsManifest import com.vayunmathur.library.wearlink.contacts.WearContactsProtocol import com.vayunmathur.library.log.Log import com.vayunmathur.library.wearlink.WearLinkListener-import com.vayunmathur.library.wearlink.WearLinkNode+import com.vayunmathur.library.wearlink.conduit.ConduitProtocol+import com.vayunmathur.library.wearlink.conduit.WearLinkClientNode import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch /**- * Watch side of WearLink for Contacts: manifest/chunk data items feed- * [WearContactsStore]'s reassembler, and a completed snapshot publishes- * watch state back. Replaces the Play-services listener service; delivery- * is bound to this process being alive.+ * Watch side of the Watch conduit client for Contacts: manifest/chunk+ * data items feed [WearContactsStore]'s reassembler, and a completed+ * snapshot publishes watch state back. Transport is `WearLinkClientNode`+ * (Messenger to the Watch app's conduit service); this app owns no+ * RFCOMM link. Delivery is bound to this process being alive; there is+ * no Play-services listener service. */ object WearLinkBridge { private const val TAG = "WearLinkBridge" private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) @Volatile- private var node: WearLinkNode? = null+ private var node: WearLinkClientNode? = null // One store shared with the UI so reassembly state is not split. @Volatile@@ -37,14 +40,14 @@ object WearLinkBridge { } } - fun get(context: Context): WearLinkNode {+ 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 watchStore = store(context)@@ -63,10 +66,16 @@ object WearLinkBridge { } } }- return WearLinkNode(context, WearContactsProtocol.CAPABILITY, listener).also { it.start() }+ // Conduit appId = first path segment of the contacts paths (the+ // ConduitProtocol route key), not the RFCOMM handshake capability.+ return WearLinkClientNode(+ context,+ ConduitProtocol.routeKey(WearContactsProtocol.MANIFEST_PATH),+ listener,+ ).also { it.start() } } - /** Live-session check for capability probing (no system node query any more). */+ /** Live-session check for capability probing (conduit link state, no node query). */ fun isConnected(context: Context): Boolean = get(context).isConnected private fun decodeManifest(body: ByteArray): WearContactsManifest =
contacts/src/main/java/com/vayunmathur/contacts/sync/WearWatchStatePublisher.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/sync/WearWatchStatePublisher.kt b/contacts/src/main/java/com/vayunmathur/contacts/sync/WearWatchStatePublisher.ktindex fdc2d5c492..89d3491eb2 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/sync/WearWatchStatePublisher.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/sync/WearWatchStatePublisher.kt@@ -8,7 +8,7 @@ import com.vayunmathur.library.wearlink.contacts.WearWatchState import com.vayunmathur.library.log.Log import kotlinx.serialization.encodeToString -/** Watch -> phone transport over WearLink: received-state data item + messages. */+/** Watch -> phone transport over the conduit client: received-state data item + messages. */ object WearWatchStatePublisher { private const val TAG = "WearWatchState"@@ -24,7 +24,7 @@ object WearWatchStatePublisher { WearContactsProtocol.WATCH_STATE_PATH, WearContactsProtocol.json.encodeToString(state).encodeToByteArray(), )- if (!sent) Log.status(TAG, "watch-state publish not delivered (no live session)")+ if (!sent) Log.status(TAG, "watch-state publish not delivered (conduit link not connected)") } /** Ask the phone to republish the snapshot (e.g. first launch with an empty book). */@@ -43,6 +43,6 @@ object WearWatchStatePublisher { private suspend fun sendToPhone(context: Context, path: String, payload: ByteArray) { val sent = WearLinkBridge.get(context).sendMessage(path, payload)- if (!sent) Log.status(TAG, "message $path not delivered (no live session)")+ if (!sent) Log.status(TAG, "message $path not delivered (conduit link not connected)") } }