notes: migrate wear sync to Watch conduit client
- Commit
46d1e0a4e6fa843b9b741ccee9c49286af66817d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-04 19:38 UTC (1 day ago)
- Parents
304025c
The notes phone<->watch sync constructed its own RFCOMM WearLinkNode
on both sides, so the Notes app owned a Bluetooth link and the phone
entry activity requested the Bluetooth permission for it. The clock
pair already runs on the Watch conduit client instead, where the
Watch app owns the link and its permission.
Both sides now use WearLinkClientNode (Messenger to the Watch app's
watch_conduit service) with the conduit appId derived from the notes
paths via ConduitProtocol.routeKey ("notes"), exactly as the clock
pair does: sync/WearNotesSync on the phone and sync/WearLinkBridge on
the watch. WearLinkBridge.get/create now return WearLinkClientNode.
Snapshots republish on conduit Connected and on note changes; with
the Watch app absent the node reports NotInstalled/Unavailable and
the sync stays idle. The explicit WearLinkPermissions request on the
phone path in MainActivity is dropped (prompting is the Watch app's
job); the watch entry rides the shared WearShellActivity as before.
The wire protocol is unchanged: library.wearlink.notes types, paths
and the golden-JSON tests are untouched, and pending-op semantics
(resend on connect, leave the queue only when a snapshot reflects
the op) carry over, since WearLinkClientNode keeps the drop-in
putData/sendMessage/state API. Its requireAck now means the conduit
accepted the message for forwarding rather than a peer ack, which
matches how flushPending already behaves.
notes/AGENTS.md wear sections now describe the conduit-client state
in the present tense, mirroring clock/ and calendar/.
Verified in the worktree with JDK 21 / big-heap owner settings:
./gradlew :notes:lint :notes:lintVitalRelease :notes:testDevUnitTest
-- BUILD SUCCESSFUL (lintVitalRelease's report task is SKIPPED by
the build, as on the base).Diff
notes/AGENTS.md
diff --git a/notes/AGENTS.md b/notes/AGENTS.mdindex 581c8bf534..6af2637966 100644--- a/notes/AGENTS.md+++ b/notes/AGENTS.md@@ -36,11 +36,12 @@ declares no broadcast receivers; the watch activity carries no launcher filter (`exported=false`) and is reachable only through that routing, so every entry point is inert on the wrong form factor. -## Wear sync (phone + watch in this module)-**Protocol:** shared wire types live in `:library:wearlink` at `com.vayunmathur.library.wearlink.notes.WearNotesProtocol`; the golden-JSON tests in this module pin the shared code's wire format. Transport is :library:wearlink (RFCOMM, no Play services), app-lifecycle-bound on both sides.-- Snapshot + watch-state sync: WearLink data items (`/notes/snapshot` phone -> watch, `/notes/watch-state` watch -> phone)-- Watch-originated ops: WearLink message (`/notes/op`, create/edit/delete)-- Sync request: WearLink message (`/notes/request-sync`), watch -> phone only+## Wear sync (Watch conduit client)+**Protocol:** shared wire types live in `:library:wearlink` at `com.vayunmathur.library.wearlink.notes.WearNotesProtocol`; the golden-JSON tests in this module pin the shared code's wire format.+- Snapshot + watch-state sync: conduit data items (`/notes/snapshot` phone -> watch, `/notes/watch-state` watch -> phone)+- Watch-originated ops: conduit message (`/notes/op`, create/edit/delete)+- Sync request: conduit message (`/notes/request-sync`), watch -> phone only+- **Transport: Watch conduit client** — phone side `sync/WearNotesSync.kt` and watch side `sync/WearLinkBridge.kt` both use `WearLinkClientNode` (Messenger to the Watch app's `watch_conduit` service, appId `notes`); 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; snapshots republish on conduit `Connected` and on note changes. - **Sync semantics:** phone -> watch is a bounded snapshot — 50 most recent notes, title ≤120 chars, body ≤4000 chars, ≤90_000 JSON bytes (`truncated` flags loss); the watch persists it and reads fully offline. Watch -> phone create/edit/delete ops apply on the phone only, last-writer-wins per note by `updatedAt` (ties: tombstone, then larger title/body), no text merge. Notes with image/ink blocks (`textOnly = false`) are read-only on the watch except delete; watch creates/edits become single-text-block notes on the phone. An op stays pending on the watch (persisted, resent on reconnect) until a phone snapshot reflects it, so the watch UI never claims "synced" before that. ## Watch experience (in this module)@@ -81,8 +82,8 @@ routing, so every entry point is inert on the wrong form factor. - com/vayunmathur/notes/ui/dialogs/RenameTagDialog.kt - com/vayunmathur/notes/ui/dialogs/DeleteTagDialog.kt ### sync/ — wear sync (both sides)-- com/vayunmathur/notes/sync/WearNotesSync.kt — phone side: snapshot publish, watch-state receive, watch-op apply under LWW-- com/vayunmathur/notes/sync/WearLinkBridge.kt — watch side: snapshot receive into the store, pending-op flush, watch-state publish+- com/vayunmathur/notes/sync/WearNotesSync.kt — phone conduit client node; snapshot publish, watch-state receive, watch-op apply under LWW+- com/vayunmathur/notes/sync/WearLinkBridge.kt — watch conduit client node; snapshot receive into the store, pending-op flush, watch-state publish - com/vayunmathur/notes/sync/RelayNoteProvider.kt ### Watch experience (ui/ + data/ + platform/) - com/vayunmathur/notes/ui/WearMainActivity.kt — watch entry (WearShellActivity)
notes/src/main/java/com/vayunmathur/notes/MainActivity.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt b/notes/src/main/java/com/vayunmathur/notes/MainActivity.ktindex e77529feb4..3f533629cd 100644--- a/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt+++ b/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt@@ -55,8 +55,9 @@ class MainActivity : MaActivity() { override fun onCreateBeforeContent() { if (routedToWear) return- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- // Wear companion sync lives and dies with this process (WearLink has+ // Wear companion sync over the Watch conduit client (no Play+ // services, no Bluetooth prompt here — prompting is the Watch+ // app's job) lives and dies with this process (the conduit has // no system wakeup); it publishes once the repository is ready below. com.vayunmathur.notes.sync.WearNotesSync.get(this) lifecycleScope.launch(Dispatchers.IO) {
notes/src/main/java/com/vayunmathur/notes/sync/WearLinkBridge.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/sync/WearLinkBridge.kt b/notes/src/main/java/com/vayunmathur/notes/sync/WearLinkBridge.ktindex 04393e3217..01a78a0f05 100644--- a/notes/src/main/java/com/vayunmathur/notes/sync/WearLinkBridge.kt+++ b/notes/src/main/java/com/vayunmathur/notes/sync/WearLinkBridge.kt@@ -3,8 +3,9 @@ package com.vayunmathur.notes.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.notes.data.WearNotesStore import com.vayunmathur.library.wearlink.notes.WearNoteOp import com.vayunmathur.library.wearlink.notes.WearNotesProtocol@@ -17,28 +18,31 @@ import kotlinx.coroutines.launch import kotlinx.serialization.encodeToString /**- * Watch side of WearLink for Notes: snapshot data items land in- * [WearNotesStore], watch state goes back as a data item, pending ops and- * request-sync are messages. Replaces any Play-services listener; delivery- * is bound to this process being alive. Pending ops are (re)sent whenever- * a session connects and whenever a new op is queued — they leave the- * queue only when a phone snapshot reflects them, never on send alone.+ * Watch side of the Watch conduit client for Notes: snapshot data items+ * land in [WearNotesStore], watch state goes back as a data item, pending+ * ops and request-sync are messages. 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. Pending ops are (re)sent whenever+ * the conduit reports the link connected and whenever a new op is+ * queued — they leave the queue only when a phone snapshot reflects+ * them, never on send alone. */ 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 - 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) { if (path != WearNotesProtocol.SNAPSHOT_PATH) return@@ -52,7 +56,13 @@ object WearLinkBridge { WearNotesStore.get(context).setSnapshot(snapshot) } }- val created = WearLinkNode(context, WearNotesProtocol.CAPABILITY, listener)+ // Conduit appId = first path segment of the notes paths (the+ // ConduitProtocol route key), not the RFCOMM handshake capability.+ val created = WearLinkClientNode(+ context,+ ConduitProtocol.routeKey(WearNotesProtocol.SNAPSHOT_PATH),+ listener,+ ) scope.launch { var wasConnected = false created.state.collect { linkState ->
notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.kt b/notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.ktindex 0300a8e0d8..2b0c62f2bb 100644--- a/notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.kt+++ b/notes/src/main/java/com/vayunmathur/notes/sync/WearNotesSync.kt@@ -3,8 +3,9 @@ package com.vayunmathur.notes.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.notes.data.Note import com.vayunmathur.notes.data.NoteBlock import com.vayunmathur.notes.data.NoteBody@@ -40,12 +41,18 @@ data class WearNotesSyncState( ) /**- * Phone side of the wear sync over WearLink (RFCOMM, :library:wearlink):- * presence is a completed handshake, the recent-notes snapshot is a data- * item, and watch-originated ops are messages handled here while this- * process lives. There is no Play-services listener: an op sent while the- * phone app is dead is not delivered (WearLink has no store-and-forward);- * the watch keeps it pending and resends on the next live session.+ * Phone side of the 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+ * recent-notes snapshot is a data item, and watch-originated ops are+ * messages handled here while this process lives. There is no+ * Play-services listener: an op sent while the phone app is dead is not+ * delivered (the conduit has no store-and-forward for it); the watch+ * keeps it pending and resends on the next live session. With the Watch+ * app absent the node reports `NotInstalled`/`Unavailable` and the sync+ * stays idle; snapshots republish on conduit `Connected` and on note+ * changes. * * Watch ops apply under the protocol's last-writer-wins policy * ([wearNoteWins]) against the phone row with the same syncId, preserving@@ -110,7 +117,13 @@ class WearNotesSync private constructor(context: Context) { } } - private val node = WearLinkNode(appContext, WearNotesProtocol.CAPABILITY, listener)+ // Conduit appId = first path segment of the notes paths (the+ // ConduitProtocol route key), not the RFCOMM handshake capability.+ private val node = WearLinkClientNode(+ appContext,+ ConduitProtocol.routeKey(WearNotesProtocol.SNAPSHOT_PATH),+ listener,+ ) private var started = false private var pendingPublish: Job? = null @@ -164,7 +177,7 @@ class WearNotesSync private constructor(context: Context) { ) } } else {- Log.status(TAG, "publish snapshot not delivered (no live WearLink session)")+ Log.status(TAG, "publish snapshot not delivered (conduit link not connected)") } }
notes/src/main/java/com/vayunmathur/notes/ui/WearMainActivity.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/ui/WearMainActivity.kt b/notes/src/main/java/com/vayunmathur/notes/ui/WearMainActivity.ktindex 3e0aaff7d8..313d528030 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/WearMainActivity.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/WearMainActivity.kt@@ -9,7 +9,7 @@ import com.vayunmathur.notes.sync.WearLinkBridge * Watch notes entry: recent list, note view and quick-note creation, * rendered only from the locally stored snapshot so it works standalone * (phone absent, cold start). Watch-originated changes queue in the- * store and flow back over WearLink; nothing here claims a phone write.+ * store and flow back over the conduit; nothing here claims a phone write. */ class WearMainActivity : WearShellActivity<WearRoute, WearNotesStore>(WearRoute.NoteList) {