appstore: wear sync over the watch conduit client
- Commit
d5b9b036657709bbe445e1c4448ad0dad50eae1c- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-04 19:47 UTC (1 day ago)
- Parents
e5ed3a2
Move both sides of the App Store phone<->watch sync from a direct RFCOMM WearLinkNode to WearLinkClientNode, exactly following the clock pair: registration with conduit appId appstore (the ConduitProtocol route key of the appstore paths), presence from the conduit-reported peer-link Connected state, catalog and watch state as conduit data items, offers/install requests/results as conduit messages, and APK bytes as a conduit stream. The Watch app absent case degrades as NotInstalled/Unavailable: sends return false, per-app states keep honest, and nothing crashes. The phone republishes its retained catalog on Connected and the watch republishes its watch state on Connected, in addition to the existing publish triggers. Drop the Bluetooth permission request from MainActivity; the Watch app owns the conduit link and its permission. No direct WearLinkNode is constructed anywhere in this app anymore. Wire protocol is untouched: library.wearlink.appstore types, golden-JSON tests, the offer/stream size+SHA-256 contract and all signature/trust verification are byte-identical. appstore/AGENTS.md wear sections updated to the conduit-client state, present tense. Verified: ./gradlew :appstore:lint :appstore:lintVitalRelease green in the worktree (release lint re-run after the combined invocation). An earlier combined run of :appstore:testDevUnitTest showed 526/527 passing with one pre-existing TvMainActivityTest Compose timeout failure unrelated to this change; per owner instruction no further tests were run, so the failure was not re-proven on clean base.
Diff
appstore/AGENTS.md
diff --git a/appstore/AGENTS.md b/appstore/AGENTS.mdindex d6bd8b839f..2b8ef75db3 100644--- a/appstore/AGENTS.md+++ b/appstore/AGENTS.md@@ -24,7 +24,7 @@ _Install: ./install appstore (dev by default)._ ## One APK, three form factors (phone + TV + watch) This module is the single App Store APK for phones, TVs and watches: same `applicationId = "com.vayunmathur.appstore"` and signing on all three, as the-WearLink handshake requires. The manifest deliberately declares no+conduit signature check requires. The manifest deliberately declares no `android.hardware.type.watch` feature (required=true would block phones; required=false is disallowed for this feature), plus leanback/touchscreen not-required and@@ -39,12 +39,11 @@ before any phone state loads; phones/TVs get the store UI (TV via `platform/InstallResultReceiver` (watch) each early-return on the wrong form factor. -## Wear sync (Watch conduit / WearLink)+## Wear sync (Watch conduit client) **Protocol:** shared wire types live in `:library:wearlink` at `com.vayunmathur.library.wearlink.appstore.WearSyncProtocol`; the golden-JSON test in `domain/WearSyncProtocolTest` pins the shared code's wire format.-- Catalog + watch-state sync: WearLink data items (`/appstore/catalog`, `/appstore/watch-state`)-- Offers / install requests / results: WearLink messages; APK bytes: WearLink stream (`/appstore/apk/<package>`), verified size+SHA-256 against the preceding offer before PackageInstaller ever sees them-- Transport: :library:wearlink (RFCOMM, no Play services); delivery is app-lifecycle-bound — no system listener wakes a dead process-- Phone side `sync/WearSyncManager` (capability discovery, catalog publish, APK streaming) and watch side `sync/WearLinkBridge` both live in this APK; `FormFactor` picks the side at runtime.+- Catalog + watch-state sync: conduit data items (`/appstore/catalog`, `/appstore/watch-state`)+- Offers / install requests / results: conduit messages; APK bytes: conduit stream (`/appstore/apk/<package>`), verified size+SHA-256 against the preceding offer before PackageInstaller ever sees them+- **Transport: Watch conduit client** — phone side `sync/WearSyncManager` and watch side `sync/WearLinkBridge` both use `WearLinkClientNode` (Messenger to the Watch app's `watch_conduit` service, appId `appstore`); 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; the catalog and watch state republish on conduit `Connected`. Delivery is app-lifecycle-bound — no system listener wakes a dead process. `FormFactor` picks the side at runtime. ## Watch experience (in this module) - `ui/WearMainActivity` (a `WearShellActivity`, `exported=false`, entered only via `MainActivity`'s watch routing) is the whole watch store: one scrolling catalog/install list reading only the watch's persisted state, so it works standalone with whatever has already been downloaded.
appstore/src/main/java/com/vayunmathur/appstore/MainActivity.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/MainActivity.kt b/appstore/src/main/java/com/vayunmathur/appstore/MainActivity.ktindex 57b0796d71..1843d079b6 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/MainActivity.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/MainActivity.kt@@ -74,7 +74,8 @@ class MainActivity : MaActivity() { if (routedToWear) return // STANDARD includes GTS R1-R4 (needed for Play Store -> play.google.com / android.clients.google.com) // + ISRG X1/X2 (F-Droid) + DigiCert G2/G3 (GitHub). Covers all appstore hosts without full system ~140 roots.- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)+ // Wear sync runs over the Watch conduit client: no Bluetooth+ // prompt here — prompting is the Watch app's job. NetworkClient.init(this, TrustBundle.STANDARD) UpdateCheckWorker.schedule(this) // Listing artwork (iconUrl) loads over HTTP(S) through the shared image
appstore/src/main/java/com/vayunmathur/appstore/sync/WearLinkBridge.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/sync/WearLinkBridge.kt b/appstore/src/main/java/com/vayunmathur/appstore/sync/WearLinkBridge.ktindex 843c3c7cc0..d19e6aceef 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/sync/WearLinkBridge.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/sync/WearLinkBridge.kt@@ -13,7 +13,9 @@ import com.vayunmathur.library.log.Log import com.vayunmathur.library.wearlink.StreamSink import com.vayunmathur.library.wearlink.StreamHeader 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 java.io.File import java.io.FileInputStream import java.io.FileOutputStream@@ -26,13 +28,15 @@ import kotlinx.coroutines.launch import kotlinx.serialization.encodeToString /**- * Watch side of WearLink: catalog data items, APK offers, APK streams and- * install requests from the phone. Replaces the old Play-services listener- * service — delivery is now bound to this process being alive (the activity- * starts the node; there is no system wakeup for a dead process).+ * Watch side of the Watch conduit client for the App Store: catalog data+ * items, APK offers, APK streams and install requests from the phone.+ * 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 (the activity starts the node; there is no system wakeup for+ * a dead process), and watch state republishes on conduit Connected. * * APK bytes only land in app-private storage after the stream verifies- * against its [ApkOffer] (size, then SHA-256, enforced by the WearLink+ * against its [ApkOffer] (size, then SHA-256, enforced by the conduit * stream layer against the header the phone sent from the same file). */ object WearLinkBridge {@@ -41,17 +45,17 @@ object WearLinkBridge { private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) @Volatile- private var node: WearLinkNode? = null+ private var node: WearLinkClientNode? = null /** The live node, created (and started) on first use. */- 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 != WearSyncProtocol.CATALOG_PATH) return@@ -103,7 +107,22 @@ object WearLinkBridge { return ApkStreamSink(context, store, packageName, offer) } }- return WearLinkNode(context, WearSyncProtocol.CAPABILITY, listener).also { it.start() }+ // Conduit appId = first path segment of the appstore paths (the+ // ConduitProtocol route key), not the RFCOMM handshake capability.+ val client = WearLinkClientNode(+ context,+ ConduitProtocol.routeKey(WearSyncProtocol.CATALOG_PATH),+ listener,+ )+ scope.launch {+ var wasConnected = false+ client.state.collect { linkState ->+ val connected = linkState is WearLinkState.Connected+ if (connected && !wasConnected) publishWatchState(context)+ wasConnected = connected+ }+ }+ return client.also { it.start() } } /** Publish this watch's state data item through the live node, if any. */
appstore/src/main/java/com/vayunmathur/appstore/sync/WearSyncManager.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/sync/WearSyncManager.kt b/appstore/src/main/java/com/vayunmathur/appstore/sync/WearSyncManager.ktindex b348cee549..bef013a720 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/sync/WearSyncManager.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/sync/WearSyncManager.kt@@ -17,8 +17,9 @@ import com.vayunmathur.library.log.Log import com.vayunmathur.library.network.NetworkClient import com.vayunmathur.library.wearlink.FileStreamSource 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 java.io.File import java.io.IOException import java.net.HttpURLConnection@@ -35,14 +36,19 @@ import kotlinx.coroutines.withContext import kotlinx.serialization.encodeToString /**- * Phone side of the wear sync over WearLink (RFCOMM, :library:wearlink):- * presence is a completed cert-bound handshake, catalog/watch-state are- * data items, offers/results are messages and APK bytes are a stream.+ * 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,+ * catalog/watch-state are data items, offers/results are messages and+ * APK bytes are a conduit stream. *- * "No Bluetooth permission / Bluetooth off / no watch / watch without the- * wear store" is a normal state ([watchConnected] = false), never a crash.- * Delivery is app-lifecycle-bound: unlike the old Play-services listener,- * nothing wakes this process when the watch speaks first.+ * "Watch app absent / conduit unavailable / no watch / watch without the+ * wear store" is a normal state ([watchConnected] = false), never a crash:+ * with the Watch app absent the node reports `NotInstalled`/`Unavailable`+ * and sends simply return false. Delivery is app-lifecycle-bound: nothing+ * wakes this process when the watch speaks first, and the catalog+ * republishes on conduit Connected and on every catalog change. */ class WearSyncManager( private val context: Context,@@ -74,13 +80,23 @@ class WearSyncManager( } } - private val node = WearLinkNode(context, WearSyncProtocol.CAPABILITY, listener)+ // Conduit appId = first path segment of the appstore paths (the+ // ConduitProtocol route key), not the RFCOMM handshake capability.+ private val node = WearLinkClientNode(+ context,+ ConduitProtocol.routeKey(WearSyncProtocol.CATALOG_PATH),+ listener,+ ) private val _watchConnected = MutableStateFlow(false) - /** True only when a WearLink handshake with the wear store completed. */+ /** True only when the conduit reports the peer link Connected. */ val watchConnected: StateFlow<Boolean> = _watchConnected.asStateFlow() + /** Last catalog offered, republished when the conduit link connects. */+ @Volatile+ private var lastCatalogApps: List<UnifiedApp> = emptyList()+ private val _states = MutableStateFlow<Map<String, PhoneWearState>>(emptyMap()) val states: StateFlow<Map<String, PhoneWearState>> = _states.asStateFlow() @@ -95,7 +111,13 @@ class WearSyncManager( scope.launch { node.state.collect { state -> val connected = state is WearLinkState.Connected+ val wasConnected = _watchConnected.value _watchConnected.value = connected+ if (connected && !wasConnected) {+ // Republish on Connected so the watch never sits on a+ // stale catalog from before the link dropped.+ if (lastCatalogApps.isNotEmpty()) syncCatalog(lastCatalogApps)+ } if (!connected) { _states.update { current -> current.mapValues { (_, s) ->@@ -110,6 +132,7 @@ class WearSyncManager( /** Publish the watch-eligible catalog snapshot so the watch can list it standalone. */ fun syncCatalog(apps: List<UnifiedApp>) {+ lastCatalogApps = apps noteCatalogVersions(apps) val catalog = WearCatalog( apps.filter { watchSendEligibility(it.apkUrl, it.splitFiles.size, it.sizeBytes) == null }
appstore/src/main/java/com/vayunmathur/appstore/ui/WearMainActivity.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/ui/WearMainActivity.kt b/appstore/src/main/java/com/vayunmathur/appstore/ui/WearMainActivity.ktindex be386ffdaa..ec14f0281e 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/ui/WearMainActivity.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/ui/WearMainActivity.kt@@ -32,6 +32,10 @@ import com.vayunmathur.library.wearlink.WearShellActivity */ class WearMainActivity : WearShellActivity<Unit, WearStore>(Unit) { + // No Bluetooth prompt on this app's path: the Watch app owns the+ // conduit link and its permission. With the Watch app absent the+ // bridge stays NotInstalled/Unavailable and the local store keeps+ // working. override fun createStore() = WearStore.get(this).also { com.vayunmathur.appstore.sync.WearLinkBridge.get(this) it.publishWatchState()