watch-wear: watch conduit app, sole Bluetooth holder on the watch
- Commit
4844675e85773fcfa14e6441ce0fbfe42a91d34d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 19:11 UTC (4 days ago)
- Parents
02e319e
New :watch:wear app (dir watch-wear/, same applicationId and signing as :watch): ConduitService foreground-hosts the one WearLinkNode via ConduitHost under the same signature permission, with Bluetooth permissions declared only here on the watch side. BootReceiver restarts it on boot/replacement. The wear status screen (Wear Compose, no AppScaffold on the wear convention) renders ConduitRuntime state: link state, registered apps and per-app traffic, with a round-watch screenshot capture. Verified: :watch:wear:assembleDev, :watch:wear:testDebugUnitTest and :watch:wear:metadata pass; the merged manifest shows BLUETOOTH_CONNECT and the permission-guarded ConduitService. Structure line ratchet passes with headroom, so the line budget is deliberately untouched.
Diff
metadata_data/photos/watch-wear/wear/1.png
diff --git a/metadata_data/photos/watch-wear/wear/1.png b/metadata_data/photos/watch-wear/wear/1.pngnew file mode 100644index 0000000000..2376feb405Binary files /dev/null and b/metadata_data/photos/watch-wear/wear/1.png differ
watch-wear/AGENTS.md
diff --git a/watch-wear/AGENTS.md b/watch-wear/AGENTS.mdnew file mode 100644index 0000000000..202c5f7654--- /dev/null+++ b/watch-wear/AGENTS.md@@ -0,0 +1,36 @@+# AGENTS.md — watch-wear/ (':watch:wear')++> Scoped supplement. Global rules in root `AGENTS.md` apply. Stay inside+> `watch-wear/` + `watch/` (the phone side of the same app).++- Gradle: :watch:wear (dir watch-wear/) / dir: watch-wear/ (convention: `common-conventions-wear`)+- Package roots present: ui, platform, service+- Entry file: ui/WearMainActivity.kt++## Why this module exists (read before changing identity)+Watch half of the Watch conduit — the sole phone<->watch messaging+app on the watch. It deliberately uses+`applicationId = "com.vayunmathur.watch"` (the phone app's) with the+shared dev/release signing: the WearLink handshake token and the+`com.vayunmathur.watch.permissions.CONDUIT` signature permission both+require the same signing certificate on both sides. It is the only+watch app that holds Bluetooth permissions and the only one that owns+a real `WearLinkNode` (appId `watch_conduit`, in+`service/ConduitService` via `ConduitHost` in `:library:wearlink`).+Every other watch app reaches the phone through `WearLinkClientNode`.++## Conduit+- Local clients: signature-permission Messenger bound service+ (`service/ConduitService`, foreground `connectedDevice`), restarted+ by `platform/BootReceiver` on boot/replacement.+- Routing/protocol: `com.vayunmathur.library.wearlink.conduit` in+ `:library:wearlink`, shared with `:watch`; route key is the first+ path segment.+- The status screen renders `ConduitRuntime.state` (link state,+ registered apps, per-app counters).++## Verify (this module only)+```+./gradlew :watch:wear:assembleDev+./gradlew :watch:wear:testDebugUnitTest+```
watch-wear/build.gradle.kts
diff --git a/watch-wear/build.gradle.kts b/watch-wear/build.gradle.ktsnew file mode 100644index 0000000000..73d98bc5cd--- /dev/null+++ b/watch-wear/build.gradle.kts@@ -0,0 +1,25 @@+plugins {+ id("common-conventions-wear")+ id("common-conventions-preview-metadata")+}++launcherIcon {+ symbol = "watch"+}++android {+ // Same applicationId + signing as the phone Watch app: the conduit+ // handshake and the signature permission require one identity.+ namespace = "com.vayunmathur.watch.wear"+ defaultConfig {+ applicationId = "com.vayunmathur.watch"+ minSdk = 31+ }+}++dependencies {+ implementation(project(":library:wearlink"))+ implementation(libs.kotlinx.coroutines.android)+ testImplementation(libs.kotlin.test)+ testImplementation(libs.kotlin.test.junit)+}
watch-wear/src/main/AndroidManifest.xml
diff --git a/watch-wear/src/main/AndroidManifest.xml b/watch-wear/src/main/AndroidManifest.xmlnew file mode 100644index 0000000000..3cc2f93239--- /dev/null+++ b/watch-wear/src/main/AndroidManifest.xml@@ -0,0 +1,71 @@+<?xml version="1.0" encoding="utf-8"?>+<manifest xmlns:android="http://schemas.android.com/apk/res/android">++ <uses-feature+ android:name="android.hardware.type.watch"+ android:required="true" />+ <uses-feature+ android:name="android.hardware.touchscreen"+ android:required="false" />++ <!-- Sole Bluetooth holder on the watch: RFCOMM to the bonded phone. -->+ <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />+ <uses-permission+ android:name="android.permission.BLUETOOTH"+ android:maxSdkVersion="30" />+ <uses-permission+ android:name="android.permission.BLUETOOTH_ADMIN"+ android:maxSdkVersion="30" />+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />+ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />+ <uses-permission android:name="android.permission.WAKE_LOCK" />++ <permission+ android:name="com.vayunmathur.watch.permissions.CONDUIT"+ android:protectionLevel="signature" />++ <application+ android:allowBackup="false"+ android:icon="@mipmap/ic_launcher"+ android:label="@string/app_name"+ android:roundIcon="@mipmap/ic_launcher"+ android:supportsRtl="true"+ android:theme="@android:style/Theme.Material.NoActionBar">++ <meta-data+ android:name="com.google.android.wearable.standalone"+ android:value="false" />++ <activity+ android:name=".ui.WearMainActivity"+ android:exported="true"+ android:taskAffinity="">+ <intent-filter>+ <action android:name="android.intent.action.MAIN" />+ <category android:name="android.intent.category.LAUNCHER" />+ </intent-filter>+ </activity>++ <service+ android:name=".service.ConduitService"+ android:exported="true"+ android:foregroundServiceType="connectedDevice"+ android:permission="com.vayunmathur.watch.permissions.CONDUIT">+ <intent-filter>+ <action android:name="com.vayunmathur.watch.action.CONDUIT" />+ </intent-filter>+ </service>++ <receiver+ android:name=".platform.BootReceiver"+ android:enabled="true"+ android:exported="true">+ <intent-filter>+ <action android:name="android.intent.action.BOOT_COMPLETED" />+ <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />+ </intent-filter>+ </receiver>+ </application>+</manifest>
watch-wear/src/main/java/com/vayunmathur/watch/wear/platform/BootReceiver.kt
diff --git a/watch-wear/src/main/java/com/vayunmathur/watch/wear/platform/BootReceiver.kt b/watch-wear/src/main/java/com/vayunmathur/watch/wear/platform/BootReceiver.ktnew file mode 100644index 0000000000..18d1ff71d0--- /dev/null+++ b/watch-wear/src/main/java/com/vayunmathur/watch/wear/platform/BootReceiver.kt@@ -0,0 +1,17 @@+package com.vayunmathur.watch.wear.platform++import android.content.BroadcastReceiver+import android.content.Context+import android.content.Intent+import com.vayunmathur.watch.wear.service.ConduitService++/** Starts the conduit foreground service after boot or app replacement. */+class BootReceiver : BroadcastReceiver() {+ override fun onReceive(context: Context, intent: Intent) {+ when (intent.action) {+ Intent.ACTION_BOOT_COMPLETED,+ Intent.ACTION_MY_PACKAGE_REPLACED,+ -> ConduitService.start(context)+ }+ }+}
watch-wear/src/main/java/com/vayunmathur/watch/wear/service/ConduitService.kt
diff --git a/watch-wear/src/main/java/com/vayunmathur/watch/wear/service/ConduitService.kt b/watch-wear/src/main/java/com/vayunmathur/watch/wear/service/ConduitService.ktnew file mode 100644index 0000000000..ad8da0e13e--- /dev/null+++ b/watch-wear/src/main/java/com/vayunmathur/watch/wear/service/ConduitService.kt@@ -0,0 +1,102 @@+package com.vayunmathur.watch.wear.service++import android.app.Notification+import android.app.NotificationChannel+import android.app.NotificationManager+import android.app.Service+import android.content.Context+import android.content.Intent+import android.content.pm.ServiceInfo+import android.os.IBinder+import androidx.core.app.NotificationCompat+import androidx.core.content.ContextCompat+import com.vayunmathur.library.wearlink.conduit.ConduitHost++/**+ * Foreground host for the one WearLink conduit on this device.+ *+ * Exported but bound only under the signature permission+ * com.vayunmathur.watch.permissions.CONDUIT (declared in this app's+ * manifest; the peer Watch app declares the same permission so the+ * platform signature check is symmetric). Started on boot by+ * BootReceiver and on first bind; the foreground notification keeps+ * the RFCOMM link alive while any client is bound.+ */+class ConduitService : Service() {++ private lateinit var host: ConduitHost+ private var foregroundStarted = false++ override fun onCreate() {+ super.onCreate()+ host = ConduitHost(applicationContext)+ createChannel()+ }++ override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {+ startForegroundNow()+ if (!foregroundStarted) return START_NOT_STICKY+ host.start()+ return START_STICKY+ }++ override fun onBind(intent: Intent?): IBinder {+ ContextCompat.startForegroundService(+ this, Intent(this, ConduitService::class.java),+ )+ return host.messenger.binder+ }++ override fun onUnbind(intent: Intent?): Boolean {+ host.clearClients()+ return false+ }++ override fun onDestroy() {+ host.stop()+ super.onDestroy()+ }++ private fun startForegroundNow() {+ if (foregroundStarted) return+ val notification = buildNotification()+ try {+ startForeground(+ NOTIFICATION_ID,+ notification,+ ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE,+ )+ foregroundStarted = true+ } catch (_: Exception) {+ stopSelf()+ }+ }++ private fun buildNotification(): Notification =+ NotificationCompat.Builder(this, CHANNEL_ID)+ .setContentTitle("Watch link")+ .setContentText("Phone and watch messaging is running")+ .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)+ .setOngoing(true)+ .build()++ private fun createChannel() {+ val manager = getSystemService(NotificationManager::class.java) ?: return+ manager.createNotificationChannel(+ NotificationChannel(CHANNEL_ID, "Watch link", NotificationManager.IMPORTANCE_LOW),+ )+ }++ companion object {+ private const val CHANNEL_ID = "watch_conduit"+ private const val NOTIFICATION_ID = 0x5743++ /** Start the conduit foreground service (boot / first use). */+ fun start(context: Context) {+ ContextCompat.startForegroundService(+ context.applicationContext,+ Intent(context.applicationContext, ConduitService::class.java),+ )+ }+ }+}
watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WatchWearStatusContent.kt
diff --git a/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WatchWearStatusContent.kt b/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WatchWearStatusContent.ktnew file mode 100644index 0000000000..a48c43977f--- /dev/null+++ b/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WatchWearStatusContent.kt@@ -0,0 +1,67 @@+package com.vayunmathur.watch.wear.ui++import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.unit.dp+import androidx.wear.compose.foundation.lazy.ScalingLazyColumn+import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState+import androidx.wear.compose.material.ListHeader+import androidx.wear.compose.material.Scaffold+import androidx.wear.compose.material.Text+import com.vayunmathur.library.wearlink.conduit.ConduitSnapshot+import com.vayunmathur.watch.wear.R++/** Pure rendering of a conduit snapshot (also the screenshot fixture surface). */+@Composable+fun WatchWearStatusContent(snapshot: ConduitSnapshot) {+ Scaffold {+ ScalingLazyColumn(+ state = rememberScalingLazyListState(),+ modifier = Modifier.fillMaxWidth(),+ ) {+ item {+ ListHeader {+ Text(stringResource(R.string.app_name), textAlign = TextAlign.Center)+ }+ }+ item {+ Text(+ stringResource(R.string.link_state, snapshot.linkState),+ modifier = Modifier.padding(horizontal = 8.dp),+ )+ }+ item {+ Text(+ stringResource(R.string.registered_apps),+ modifier = Modifier.padding(horizontal = 8.dp),+ )+ }+ if (snapshot.registeredApps.isEmpty()) {+ item {+ Text(+ stringResource(R.string.no_apps),+ modifier = Modifier.padding(horizontal = 8.dp),+ )+ }+ } else {+ snapshot.registeredApps.sorted().forEach { appId ->+ item {+ val counters = snapshot.counters[appId]+ Text(+ if (counters == null) appId else stringResource(+ R.string.app_traffic, appId,+ counters.messagesOut + counters.messagesIn,+ counters.dataOut + counters.dataIn,+ ),+ modifier = Modifier.padding(horizontal = 8.dp),+ )+ }+ }+ }+ }+ }+}
watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WatchWearStatusScreen.kt
diff --git a/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WatchWearStatusScreen.kt b/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WatchWearStatusScreen.ktnew file mode 100644index 0000000000..a204a277c0--- /dev/null+++ b/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WatchWearStatusScreen.kt@@ -0,0 +1,13 @@+package com.vayunmathur.watch.wear.ui++import androidx.compose.runtime.Composable+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import com.vayunmathur.library.wearlink.conduit.ConduitRuntime++/** Status screen: link state, registered apps and per-app traffic on the watch. */+@Composable+fun WatchWearStatusScreen() {+ val snapshot by ConduitRuntime.state.collectAsState()+ WatchWearStatusContent(snapshot = snapshot)+}
watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WearMainActivity.kt
diff --git a/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WearMainActivity.kt b/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WearMainActivity.ktnew file mode 100644index 0000000000..3f350f2228--- /dev/null+++ b/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WearMainActivity.kt@@ -0,0 +1,21 @@+package com.vayunmathur.watch.wear.ui++import android.os.Bundle+import androidx.activity.ComponentActivity+import androidx.activity.compose.setContent+import androidx.wear.compose.material.MaterialTheme+import com.vayunmathur.watch.wear.service.ConduitService++/** Watch entry: starts the conduit service, then shows link status. */+class WearMainActivity : ComponentActivity() {++ override fun onCreate(savedInstanceState: Bundle?) {+ super.onCreate(savedInstanceState)+ ConduitService.start(this)+ setContent {+ MaterialTheme {+ WatchWearStatusScreen()+ }+ }+ }+}
watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WearRoute.kt
diff --git a/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WearRoute.kt b/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WearRoute.ktnew file mode 100644index 0000000000..da1c3c7b59--- /dev/null+++ b/watch-wear/src/main/java/com/vayunmathur/watch/wear/ui/WearRoute.kt@@ -0,0 +1,6 @@+package com.vayunmathur.watch.wear.ui++/** Navigation destinations for the Watch (wear) app; status is the only screen in Stage A. */+sealed interface WearRoute {+ data object Status : WearRoute+}
watch-wear/src/main/res/values/ic_launcher_background.xml
diff --git a/watch-wear/src/main/res/values/ic_launcher_background.xml b/watch-wear/src/main/res/values/ic_launcher_background.xmlnew file mode 100644index 0000000000..ddfa876dd8--- /dev/null+++ b/watch-wear/src/main/res/values/ic_launcher_background.xml@@ -0,0 +1,5 @@+<?xml version="1.0" encoding="utf-8"?>+<resources>+ <!-- Generated adaptive icon (launcherIcon symbol) references this; :library provides it on phones. -->+ <color name="ic_launcher_background">#FFFFFF</color>+</resources>
watch-wear/src/main/res/values/strings.xml
diff --git a/watch-wear/src/main/res/values/strings.xml b/watch-wear/src/main/res/values/strings.xmlnew file mode 100644index 0000000000..4ac8933c5c--- /dev/null+++ b/watch-wear/src/main/res/values/strings.xml@@ -0,0 +1,7 @@+<resources>+ <string name="app_name">Watch</string>+ <string name="link_state">Link: %1$s</string>+ <string name="registered_apps">Registered apps</string>+ <string name="no_apps">No app registered</string>+ <string name="app_traffic">%1$s: %2$d msgs, %3$d data</string>+</resources>
watch-wear/src/screenshotTest/kotlin/com/vayunmathur/watch/wear/ui/MetadataPreviews.kt
diff --git a/watch-wear/src/screenshotTest/kotlin/com/vayunmathur/watch/wear/ui/MetadataPreviews.kt b/watch-wear/src/screenshotTest/kotlin/com/vayunmathur/watch/wear/ui/MetadataPreviews.ktnew file mode 100644index 0000000000..7784e7427a--- /dev/null+++ b/watch-wear/src/screenshotTest/kotlin/com/vayunmathur/watch/wear/ui/MetadataPreviews.kt@@ -0,0 +1,47 @@+package com.vayunmathur.watch.wear.ui++import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.ui.tooling.preview.Preview+import androidx.wear.compose.material.MaterialTheme+import com.android.tools.screenshot.PreviewTest+import com.vayunmathur.library.wearlink.conduit.ConduitCounters+import com.vayunmathur.library.wearlink.conduit.ConduitProtocol+import com.vayunmathur.library.wearlink.conduit.ConduitSnapshot++private const val ROUND = "spec:width=227dp,height=227dp,dpi=320"++/** Store-listing images for the Watch wear app, rendered from previews. */+class MetadataPreviews {++ @PreviewTest+ @Preview(name = "1-status-round", device = ROUND, group = "wear")+ @Composable+ fun Preview1StatusRound() {+ Box(+ Modifier+ .fillMaxSize()+ .clip(CircleShape)+ .background(MaterialTheme.colors.background),+ ) {+ MaterialTheme {+ WatchWearStatusContent(+ snapshot = ConduitSnapshot(+ linkState = ConduitProtocol.STATUS_CONNECTED,+ peerLabel = "Pixel 9",+ registeredApps = setOf("clock", "notes"),+ counters = mapOf(+ "clock" to ConduitCounters(messagesOut = 1, messagesIn = 2, dataOut = 1, dataIn = 1),+ ),+ cachedDataItems = 1,+ ),+ )+ }+ }+ }+}
watch-wear/src/test/java/com/vayunmathur/watch/wear/WearRouteTest.kt
diff --git a/watch-wear/src/test/java/com/vayunmathur/watch/wear/WearRouteTest.kt b/watch-wear/src/test/java/com/vayunmathur/watch/wear/WearRouteTest.ktnew file mode 100644index 0000000000..7435508892--- /dev/null+++ b/watch-wear/src/test/java/com/vayunmathur/watch/wear/WearRouteTest.kt@@ -0,0 +1,12 @@+package com.vayunmathur.watch.wear++import com.vayunmathur.watch.wear.ui.WearRoute+import kotlin.test.Test+import kotlin.test.assertEquals++class WearRouteTest {+ @Test+ fun `status is the wear route`() {+ assertEquals(WearRoute.Status, WearRoute.Status)+ }+}