contacts: answer the conduit sync trigger with the snapshot publish
- Commit
f3cd845e81b6da67f9122f78ef2f02adda7be7e5- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-05 01:22 UTC (1 day ago)
- Parents
8225851
Adds ConduitSyncTriggerReceiver (exported only under the CONDUIT signature permission) as the reference phone-app receiver: the Watch app's scheduled intent invokes WearContactsSync.triggerPush, the same publish path as the Connected and book-change triggers, with no UI and no prompt. WearContactsSync gains the triggerPush entry point; the pure ConduitSyncTriggerHandler decision (matching action and appId only) is unit-tested. Contacts watch has no user-data mutation, so the watch-to-phone direction rides its existing immediate pushes (watch-state publish, call/message handoffs); both directions now land in the conduit hosts' persistent stores when the receiving app is not running. Verified: :contacts:lint and :contacts:lintVitalRelease pass; :contacts:testDevUnitTest passes except two compose tests that fail identically on clean 94b70ac39 (pre-existing).
Diff
contacts/AGENTS.md
diff --git a/contacts/AGENTS.md b/contacts/AGENTS.mdindex 6f8896dc0b..2fae304077 100644--- a/contacts/AGENTS.md+++ b/contacts/AGENTS.md@@ -39,6 +39,7 @@ are no watch receivers in this module. **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 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.+- **Push triggers:** the snapshot publish runs on (1) the phone Watch app's scheduled sync-trigger intent (`sync/ConduitSyncTriggerReceiver`, exported only under the `CONDUIT` signature permission, invoking `WearContactsSync.triggerPush()` — no UI, no prompt), (2) book changes (immediate publish from `MainActivity`), and (3) conduit `Connected`. The watch side has no user-data mutation: its outbound pushes are the immediate watch-state publish and call/message handoff requests via `WearWatchStatePublisher`. Snapshots arriving while the watch process is dead land in the watch conduit's persistent store and replay on registration; the watch copy itself persists in `data/WearContactsStore`. ## Watch experience (in this module) - `ui/WearMainActivity` (a `WearShellActivity`) routes `ui/WearRoute` over the contact list and a detail carrying the contact id.
contacts/src/main/AndroidManifest.xml
diff --git a/contacts/src/main/AndroidManifest.xml b/contacts/src/main/AndroidManifest.xmlindex d3b732cd55..491aea58f6 100644--- a/contacts/src/main/AndroidManifest.xml+++ b/contacts/src/main/AndroidManifest.xml@@ -215,7 +215,18 @@ android:permission="com.vayunmathur.contacts.permissions.ACCESS_CONTACTS"> </activity> - <!-- WearLink delivery is app-lifecycle-bound; no system listener service. -->+ <!-- 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> <!-- Watch experience: entered only via MainActivity's watch routing (no launcher filter of its own). Inert on a phone: it is reached
contacts/src/main/java/com/vayunmathur/contacts/sync/ConduitSyncTriggerReceiver.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/sync/ConduitSyncTriggerReceiver.kt b/contacts/src/main/java/com/vayunmathur/contacts/sync/ConduitSyncTriggerReceiver.ktnew file mode 100644index 0000000000..feee64c097--- /dev/null+++ b/contacts/src/main/java/com/vayunmathur/contacts/sync/ConduitSyncTriggerReceiver.kt@@ -0,0 +1,74 @@+package com.vayunmathur.contacts.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.contacts.WearContactsProtocol+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(WearContactsProtocol.MANIFEST_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+ }+}++/**+ * Reference 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 contacts snapshot to your watch counterpart+ * now". The response is the existing conduit publish — the same+ * [WearContactsSync.publishWearContacts] path the Connected and+ * book-change triggers use — never a UI or a user prompt. When this+ * process was dead, [WearContactsSync.start]'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 = WearContactsSync(appContext, scope)+ sync.start()+ sync.triggerPush()+ } finally {+ pending.finish()+ }+ }+ }+ if (!handled) return+ }+}
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 0738efccbe..c2b2ab5db5 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/sync/WearContactsSync.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/sync/WearContactsSync.kt@@ -162,6 +162,17 @@ class WearContactsSync( } } + /**+ * Push the current snapshot now, on request from outside this+ * process's own triggers: the Watch app's scheduled sync-trigger+ * intent (see [ConduitSyncTriggerReceiver]) and the watch's own+ * snapshot request both land here, on the same publish path as the+ * Connected/book-change triggers. Safe when idle: an undelivered+ * publish is retried by the Connected trigger once the conduit+ * link is up.+ */+ fun triggerPush() = republish()+ private fun republish() { scope.launch(Dispatchers.IO) { runCatching { publishWearContacts(loadWearContacts(context)) }
contacts/src/test/java/com/vayunmathur/contacts/sync/ConduitSyncTriggerHandlerTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/sync/ConduitSyncTriggerHandlerTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/sync/ConduitSyncTriggerHandlerTest.ktnew file mode 100644index 0000000000..a32f8f2a93--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/sync/ConduitSyncTriggerHandlerTest.kt@@ -0,0 +1,30 @@+package com.vayunmathur.contacts.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 contacts invokes publish`() {+ var publishes = 0+ val handled =+ ConduitSyncTriggerHandler.handle(ConduitSync.ACTION, ConduitSyncTriggerHandler.APP_ID) {+ publishes+++ }+ assertTrue(handled)+ assertEquals(1, publishes)+ assertEquals("contacts", ConduitSyncTriggerHandler.APP_ID)+ }++ @Test+ fun `other apps and other actions do not invoke publish`() {+ var publishes = 0+ assertFalse(ConduitSyncTriggerHandler.handle(ConduitSync.ACTION, "notes") { publishes++ })+ assertFalse(ConduitSyncTriggerHandler.handle("com.example.OTHER", "contacts") { publishes++ })+ assertFalse(ConduitSyncTriggerHandler.handle(null, null) { publishes++ })+ assertEquals(0, publishes)+ }+}