notes: answer the conduit sync trigger with the snapshot publish
- Commit
ea07eb1488c939bec849e1c3b4dd42e1cd2c5481- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-05 06:45 UTC (1 day ago)
- Parents
3d4b8a8
Adds ConduitSyncTriggerReceiver (exported only under the CONDUIT signature permission) following the Contacts reference pattern: the phone Watch app's scheduled sync-trigger intent invokes `WearNotesSync.syncNow()` (snapshot 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. Verified: :notes:lint (0 errors) and :notes:lintVitalRelease pass; :notes:testDevUnitTest passes (261 tests).
Diff
notes/AGENTS.md
diff --git a/notes/AGENTS.md b/notes/AGENTS.mdindex 1062db2baa..4cfd615d2f 100644--- a/notes/AGENTS.md+++ b/notes/AGENTS.md@@ -32,7 +32,9 @@ 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 list/detail UI. This module-declares no broadcast receivers; the watch activity carries no+declares one broadcast receiver, the conduit sync-trigger receiver+(`sync/ConduitSyncTriggerReceiver`, phone-side snapshot publish only);+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. @@ -42,6 +44,7 @@ routing, so every entry point is inert on the wrong form factor. - 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.+- **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 `WearNotesSync.syncNow()` — no UI, no prompt). Data arriving while the receiving process is dead lands in the conduit host's persistent store and replays on registration. - **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)
notes/src/main/AndroidManifest.xml
diff --git a/notes/src/main/AndroidManifest.xml b/notes/src/main/AndroidManifest.xmlindex cb3a323ada..8d8447f99c 100644--- a/notes/src/main/AndroidManifest.xml+++ b/notes/src/main/AndroidManifest.xml@@ -134,6 +134,19 @@ android:exported="true" android:readPermission="com.vayunmathur.notes.permission.RELAY" android:writePermission="com.vayunmathur.notes.permission.RELAY" />+ + <!-- 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
notes/src/main/java/com/vayunmathur/notes/sync/ConduitSyncTriggerReceiver.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/sync/ConduitSyncTriggerReceiver.kt b/notes/src/main/java/com/vayunmathur/notes/sync/ConduitSyncTriggerReceiver.ktnew file mode 100644index 0000000000..416503ec9c--- /dev/null+++ b/notes/src/main/java/com/vayunmathur/notes/sync/ConduitSyncTriggerReceiver.kt@@ -0,0 +1,73 @@+package com.vayunmathur.notes.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.notes.WearNotesProtocol+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(WearNotesProtocol.SNAPSHOT_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 {+ val sync = WearNotesSync.get(appContext)+ sync.start()+ sync.syncNow()+ } finally {+ pending.finish()+ }+ }+ }+ if (!handled) return+ }+}
notes/src/test/java/com/vayunmathur/notes/sync/ConduitSyncTriggerHandlerTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/sync/ConduitSyncTriggerHandlerTest.kt b/notes/src/test/java/com/vayunmathur/notes/sync/ConduitSyncTriggerHandlerTest.ktnew file mode 100644index 0000000000..25fb74cf45--- /dev/null+++ b/notes/src/test/java/com/vayunmathur/notes/sync/ConduitSyncTriggerHandlerTest.kt@@ -0,0 +1,30 @@+package com.vayunmathur.notes.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 notes invokes publish`() {+ var publishes = 0+ val handled =+ ConduitSyncTriggerHandler.handle(ConduitSync.ACTION, ConduitSyncTriggerHandler.APP_ID) {+ publishes+++ }+ assertTrue(handled)+ assertEquals(1, publishes)+ assertEquals("notes", 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", "notes") { publishes++ })+ assertFalse(ConduitSyncTriggerHandler.handle(null, null) { publishes++ })+ assertEquals(0, publishes)+ }+}