Modern Apps git

← Modern-Apps.git

weather: answer the conduit sync trigger with the snapshot publish

Commit
7ce779614d8b04d2e458ffaf6654b99c7de84d3a
Author
Vayun Mathur <[email protected]>
When
2026-12-05 06:45 UTC (1 day ago)
Parents
fe53dc8
Adds ConduitSyncTriggerReceiver (exported only under the CONDUIT
signature permission) following the Contacts reference pattern: the
phone Watch app's scheduled sync-trigger intent invokes `WearWeatherSync.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: :weather:lint (0 errors) and :weather:lintVitalRelease pass;
:weather:testDevUnitTest passes (268 tests).

Diff

weather/AGENTS.md
diff --git a/weather/AGENTS.md b/weather/AGENTS.mdindex c69063565d..9bd399ccc4 100644--- a/weather/AGENTS.md+++ b/weather/AGENTS.md@@ -39,6 +39,7 @@ receivers are a phone-only surface and early-return on a watch. - Snapshot + watch-state sync: conduit data items (`/weather/snapshot` phone -> watch, `/weather/watch-state` watch -> phone) - Sync request: conduit message (`/weather/request-sync`), watch -> phone only - **Transport: Watch conduit client** — phone side `sync/WearWeatherSync.kt` and watch side `sync/WearLinkBridge.kt` both use `WearLinkClientNode` (Messenger to the Watch app's `watch_conduit` service, appId `weather`); 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 forecast refresh. The watch renders the locally stored snapshot offline and holds no network path of its own. Protocol values stay Celsius / km/h; rendering converts to the watch's system regional preferences (`platform/WearSystemUnits`, formatting in `domain/WearWeatherFormatting`).+- **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 `WearWeatherSync.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.  ## Watch experience (in this module) - `ui/WearMainActivity` (a `WearShellActivity`) routes `ui/WearRoute` over locations (`ui/WearLocationsScreen`) and one location's current/hourly/daily detail (`ui/WearLocationScreen`), with a sync-now action that messages the phone side.
weather/src/main/AndroidManifest.xml
diff --git a/weather/src/main/AndroidManifest.xml b/weather/src/main/AndroidManifest.xmlindex 1bf49f8233..ff350c3b5d 100644--- a/weather/src/main/AndroidManifest.xml+++ b/weather/src/main/AndroidManifest.xml@@ -135,5 +135,18 @@                 android:name="android.appwidget.provider"                 android:resource="@xml/weather_blob_widget_info" />         </receiver>++        <!-- 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>
weather/src/main/java/com/vayunmathur/weather/sync/ConduitSyncTriggerReceiver.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/sync/ConduitSyncTriggerReceiver.kt b/weather/src/main/java/com/vayunmathur/weather/sync/ConduitSyncTriggerReceiver.ktnew file mode 100644index 0000000000..b2f8c45936--- /dev/null+++ b/weather/src/main/java/com/vayunmathur/weather/sync/ConduitSyncTriggerReceiver.kt@@ -0,0 +1,73 @@+package com.vayunmathur.weather.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.weather.WearWeatherProtocol+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(WearWeatherProtocol.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 = WearWeatherSync.get(appContext)+                        sync.start()+                        sync.syncNow()+                    } finally {+                        pending.finish()+                    }+                }+            }+        if (!handled) return+    }+}
weather/src/test/java/com/vayunmathur/weather/sync/ConduitSyncTriggerHandlerTest.kt
diff --git a/weather/src/test/java/com/vayunmathur/weather/sync/ConduitSyncTriggerHandlerTest.kt b/weather/src/test/java/com/vayunmathur/weather/sync/ConduitSyncTriggerHandlerTest.ktnew file mode 100644index 0000000000..0761013ce6--- /dev/null+++ b/weather/src/test/java/com/vayunmathur/weather/sync/ConduitSyncTriggerHandlerTest.kt@@ -0,0 +1,30 @@+package com.vayunmathur.weather.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 weather invokes publish`() {+        var publishes = 0+        val handled =+            ConduitSyncTriggerHandler.handle(ConduitSync.ACTION, ConduitSyncTriggerHandler.APP_ID) {+                publishes+++            }+        assertTrue(handled)+        assertEquals(1, publishes)+        assertEquals("weather", 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", "weather") { publishes++ })+        assertFalse(ConduitSyncTriggerHandler.handle(null, null) { publishes++ })+        assertEquals(0, publishes)+    }+}