camera: remove stuck waiting state from watch viewfinder
- Commit
9a31f000a9c7c59917b940d5a45d89e1fe71517e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-06 02:49 UTC (12 hours ago)
- Parents
23dbc53
The watch routed to the viewfinder whenever the mirrored state said phoneOpen, with no requirement that preview frames ever arrive, and the viewfinder screen then showed 'Waiting for the phone viewfinder…' indefinitely. Three paths could hold that state forever: a stale phoneOpen replayed by the conduit store from a dead phone (whose closed state was never published), a single PREVIEW_START send that was dropped by the phone because it was not open at that moment, and a restarted phone preview stream whose sequence restarts at 1 — every new frame was dropped by the watch store's stale-sequence guard, since clearing on streaming-stop kept the old high sequence. Routing now takes frame liveness: wearWatchScreen shows the viewfinder only while the link is up, the phone is open, and either a frame is held and fresh or the first frame is still due within WEAR_VIEWFINDER_FRAME_TIMEOUT_MS (3s); otherwise the watch shows the single open-on-phone button. WearCameraStore (with an injectable clock for tests) stamps the start of the wait — republications of the same state do not restart it — and each frame's arrival, resets the window on link reconnect, and accepts a lower preview sequence carrying a newer frame timestamp as a restarted stream while still dropping replays and late pre-restart frames. WearMainActivity ticks the time-based decision and retries PREVIEW_START every second while no frame is held, so a recovered stream routes back on its own. The waiting string and text are removed: a frameless viewfinder is only the brief empty moment before the first frame. The open-in-phone screen now uses the shared centred filled WearFilledButton with the phone icon, and the wear listing screenshots were regenerated. Tests: WearWatchPresentationTest covers stale phoneOpen without frames, a stalled/frozen frame timing out, the first-frame window, and the link-reconnect replay window; WearCameraStoreTest covers the awaiting window not restarting on republication, frame arrival and streaming-stop stamps, the restarted-sequence acceptance and replay drops, and the reconnect window. Verified: ./gradlew :camera:lint :camera:lintVitalRelease :camera:testDevUnitTest (all green, --max-workers=128) and ./gradlew :camera:metadata regenerating metadata_data/photos/camera/ wear/; :camera:ktlintCheck passes for the touched main/test/ screenshot source sets (its remaining failure is pre-existing androidTest style violations in files this change does not touch).
Diff
camera/AGENTS.md
diff --git a/camera/AGENTS.md b/camera/AGENTS.mdindex 86c928fdde..42159e05f1 100644--- a/camera/AGENTS.md+++ b/camera/AGENTS.md@@ -53,8 +53,8 @@ for a direct open-on-phone broadcast. - **Transport: Watch conduit client** — phone side `platform/WearCameraSync.kt` and watch side `sync/WearLinkBridge.kt` both use `WearLinkClientNode` (Messenger to the Watch app's `watch_conduit` service, appId `camera`); 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; state + thumbnail republish on conduit `Connected`. ## Watch experience (in this module)-- `ui/WearMainActivity` (a `WearShellActivity`) routes `ui/WearRoute` over exactly two screens, driven by the mirrored `phoneOpen` state via the pure `domain/WearWatchPresentation`: `OpenOnPhone` (one centred filled "Open in phone" button while the phone Camera app is not open) and `Viewfinder` (only the streamed viewfinder plus one small capture button, themed by the phone's mode via `wearCaptureButtonSpec`/`wearCaptureOp`; recording shows the stop theme). The watch camera remote never opens a local camera and offers no other controls. Mirrored truth is in-memory only in `data/WearCameraStore` (no offline copy: nothing is usable without the phone).-- Wire protocol (`WearCameraProtocol`, WCF1 preview frames) lives in `:library:wearlink` at `com.vayunmathur.library.wearlink.camera`; this module's `WearCameraProtocolTest` pins the golden JSON/frame bytes (including the watch store's stale-frame rules). Live viewfinder frames travel as `/camera/preview-frame` messages (never the bounded STREAM primitive); phone drops frames latest-only, watch drops stale sequences and clears the live frame when the link drops or streaming stops — a frozen frame is never shown as live.+- `ui/WearMainActivity` (a `WearShellActivity`) routes `ui/WearRoute` over exactly two screens, driven by the mirrored `phoneOpen` state plus frame liveness via the pure `domain/WearWatchPresentation`: `OpenOnPhone` (one centred filled "Open in phone" button with the phone icon while the phone Camera app is not open) and `Viewfinder` (only the streamed viewfinder plus one small capture button, themed by the phone's mode via `wearCaptureButtonSpec`/`wearCaptureOp`; recording shows the stop theme). The viewfinder requires frames to actually arrive: `data/WearCameraStore` stamps the wait for the first frame and each frame's arrival, the preview request is retried while no frame is held, and `wearWatchScreen` falls back to `OpenOnPhone` when no live frame has arrived within `WEAR_VIEWFINDER_FRAME_TIMEOUT_MS` — there is no resting waiting state. The watch camera remote never opens a local camera and offers no other controls. Mirrored truth is in-memory only in `data/WearCameraStore` (no offline copy: nothing is usable without the phone).+- Wire protocol (`WearCameraProtocol`, WCF1 preview frames) lives in `:library:wearlink` at `com.vayunmathur.library.wearlink.camera`; this module's `WearCameraProtocolTest` pins the golden JSON/frame bytes (including the watch store's stale-frame rules). Live viewfinder frames travel as `/camera/preview-frame` messages (never the bounded STREAM primitive); phone drops frames latest-only, watch drops stale sequences (a lower sequence with a newer frame timestamp is a restarted phone stream and is accepted) and clears the live frame when the link drops or streaming stops — a frozen frame is never shown as live. - Watch listing screenshots are the `group = "wear"` previews in `src/screenshotTest/.../ui/WearMetadataPreviews.kt`, collected by `:camera:metadata` into `metadata_data/photos/camera/wear/`; phone/tablet shots keep their buckets. **Gotchas:** - Capture code lives in `platform/` (the Camera2 driver in `platform/camera2/`); there is no `util/` root in this module
camera/src/main/java/com/vayunmathur/camera/data/WearCameraStore.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/data/WearCameraStore.kt b/camera/src/main/java/com/vayunmathur/camera/data/WearCameraStore.ktindex 39833a1f57..26773b18f8 100644--- a/camera/src/main/java/com/vayunmathur/camera/data/WearCameraStore.kt+++ b/camera/src/main/java/com/vayunmathur/camera/data/WearCameraStore.kt@@ -14,7 +14,7 @@ import kotlinx.coroutines.flow.asStateFlow * offline copy to persist and no fake local state to show. A cold start * honestly shows "no phone nearby" until the link publishes again. */-class WearCameraStore {+class WearCameraStore(private val nowMillis: () -> Long = System::currentTimeMillis) { private val _linkConnected = MutableStateFlow(false) val linkConnected: StateFlow<Boolean> = _linkConnected.asStateFlow()@@ -29,27 +29,58 @@ class WearCameraStore { private val _previewRotation = MutableStateFlow(0) val previewRotation: StateFlow<Int> = _previewRotation.asStateFlow() + private val _lastPreviewFrameAtMillis = MutableStateFlow<Long?>(null)+ /** Clock time of the newest accepted preview frame; null while none is held. */+ val lastPreviewFrameAtMillis: StateFlow<Long?> = _lastPreviewFrameAtMillis.asStateFlow()++ private val _awaitingPreviewSinceMillis = MutableStateFlow<Long?>(null)++ /**+ * Clock time the watch started waiting for the first frame of the+ * current open-phone session; null once a frame is held or the phone+ * is closed/unlinked. Republication of the same state never restarts+ * it, so a stale `phoneOpen` replay times out instead of waiting+ * forever (see `wearWatchScreen`).+ */+ val awaitingPreviewSinceMillis: StateFlow<Long?> = _awaitingPreviewSinceMillis.asStateFlow()+ private val _thumbnailJpeg = MutableStateFlow<ByteArray?>(null) val thumbnailJpeg: StateFlow<ByteArray?> = _thumbnailJpeg.asStateFlow() private var lastPreviewSequence = -1L+ private var lastPreviewTimestampMillis = 0L var droppedStaleFrames: Long = 0L private set fun setLinkConnected(connected: Boolean) {+ val wasConnected = _linkConnected.value _linkConnected.value = connected if (!connected) { // Link down: no fake viewfinder. The last mirrored state stays // for context, but the live frame is gone. clearLivePreview()+ _awaitingPreviewSinceMillis.value = null lastPreviewSequence = -1L+ lastPreviewTimestampMillis = 0L+ } else if (!wasConnected && _state.value.phoneOpen && _previewJpeg.value == null) {+ // (Re)connected with the phone already open (the conduit+ // replays `/camera/state` on registration): a fresh window+ // for the stream — re-requested by the UI — to deliver.+ _awaitingPreviewSinceMillis.value = nowMillis() } } /** Applies a mirrored phone state (state mirroring is replace, never merge). */ fun applyState(state: WearCameraState) { _state.value = state+ if (!state.phoneOpen) {+ _awaitingPreviewSinceMillis.value = null+ } else if (_previewJpeg.value == null && _awaitingPreviewSinceMillis.value == null) {+ // First sight of the open phone with no frame yet: start the+ // wait window. Later republications leave it untouched.+ _awaitingPreviewSinceMillis.value = nowMillis()+ } if (!state.previewStreaming) { // The phone says it is not streaming: the last frame is stale, // not live, so it must not sit in the viewfinder. The sequence@@ -57,24 +88,40 @@ class WearCameraStore { // replay of the frame just cleared) must not resurrect it; // the phone's sequence only moves forward within a session. clearLivePreview()+ if (state.phoneOpen && _awaitingPreviewSinceMillis.value == null) {+ _awaitingPreviewSinceMillis.value = nowMillis()+ } } } private fun clearLivePreview() { _previewJpeg.value = null _previewRotation.value = 0+ _lastPreviewFrameAtMillis.value = null } /** Applies one preview frame body; returns false when malformed or stale. */ fun applyPreviewFrame(body: ByteArray): Boolean { val decoded = PreviewFrame.decode(body) ?: return false- if (decoded.sequence <= lastPreviewSequence) {+ // Forward in the current stream, or a restarted phone stream: a+ // new process/session restarts its sequence low while its frame+ // timestamps keep moving forward, so a lower sequence carrying a+ // newer timestamp is a new stream, not a stale replay. Anything+ // else — a replayed/duplicated frame, or a late frame from before+ // a restart (old timestamp, whatever its sequence) — is dropped.+ val forward = decoded.sequence > lastPreviewSequence && decoded.timestampMillis >= lastPreviewTimestampMillis+ val restarted =+ decoded.sequence < lastPreviewSequence && decoded.timestampMillis > lastPreviewTimestampMillis+ if (!forward && !restarted) { droppedStaleFrames++ return false } lastPreviewSequence = decoded.sequence+ lastPreviewTimestampMillis = decoded.timestampMillis _previewRotation.value = decoded.rotationDegrees _previewJpeg.value = decoded.jpeg+ _lastPreviewFrameAtMillis.value = nowMillis()+ _awaitingPreviewSinceMillis.value = null return true }
camera/src/main/java/com/vayunmathur/camera/domain/WearWatchPresentation.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/domain/WearWatchPresentation.kt b/camera/src/main/java/com/vayunmathur/camera/domain/WearWatchPresentation.ktindex 8174063873..a778d9e529 100644--- a/camera/src/main/java/com/vayunmathur/camera/domain/WearWatchPresentation.kt+++ b/camera/src/main/java/com/vayunmathur/camera/domain/WearWatchPresentation.kt@@ -25,15 +25,50 @@ enum class WearWatchScreen { } /**- * The screen for the mirrored truth: the viewfinder exists only while the- * conduit link is up AND the phone reports its Camera app open. A dropped- * link, a closed phone app, or state from an older phone (which never- * publishes `phoneOpen`) all fall back to the open-on-phone button.+ * How long the watch may sit in the viewfinder without a live frame before+ * it falls back to the open-on-phone button. At the stream's 2–8 fps a+ * frame is due every 125–500 ms and camera/link start-up is well under a+ * second, so three seconds without a frame means the stream is not+ * coming — a replayed `phoneOpen` from a dead phone, a lost preview+ * request, or a restarted phone sequence — not a slow first frame.+ */+const val WEAR_VIEWFINDER_FRAME_TIMEOUT_MS: Long = 3_000L++/**+ * The screen for the mirrored truth plus frame liveness: the viewfinder+ * exists only while the conduit link is up, the phone reports its Camera+ * app open, AND frames are actually arriving (or are still due within+ * [WEAR_VIEWFINDER_FRAME_TIMEOUT_MS]). A dropped link, a closed phone+ * app, state from an older phone (which never publishes `phoneOpen`), a+ * stale/replayed `phoneOpen` whose stream never starts, and a stream+ * that stalls all fall back to the open-on-phone button — the watch has+ * no resting "waiting for the viewfinder" state.+ *+ * @param hasLiveFrame a decoded preview frame is currently held.+ * @param frameAgeMillis age of that frame; ignored without one.+ * @param awaitingAgeMillis how long the watch has been waiting for the+ * first frame of this open-phone session; null when not waiting. */ fun wearWatchScreen( linkConnected: Boolean, state: WearCameraState,-): WearWatchScreen = if (linkConnected && state.phoneOpen) WearWatchScreen.Viewfinder else WearWatchScreen.OpenOnPhone+ hasLiveFrame: Boolean,+ frameAgeMillis: Long?,+ awaitingAgeMillis: Long?,+): WearWatchScreen {+ if (!linkConnected || !state.phoneOpen) return WearWatchScreen.OpenOnPhone+ return when {+ hasLiveFrame ->+ if (frameAgeMillis != null && frameAgeMillis > WEAR_VIEWFINDER_FRAME_TIMEOUT_MS) {+ WearWatchScreen.OpenOnPhone+ } else {+ WearWatchScreen.Viewfinder+ }+ awaitingAgeMillis != null && awaitingAgeMillis <= WEAR_VIEWFINDER_FRAME_TIMEOUT_MS ->+ WearWatchScreen.Viewfinder+ else -> WearWatchScreen.OpenOnPhone+ }+} /** How the capture button looks: decided by the phone's mode, never by the watch. */ enum class WearCaptureTheme {
camera/src/main/java/com/vayunmathur/camera/ui/WearMainActivity.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/WearMainActivity.kt b/camera/src/main/java/com/vayunmathur/camera/ui/WearMainActivity.ktindex 41354e2fc2..f6ac686ff7 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/WearMainActivity.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/WearMainActivity.kt@@ -4,11 +4,16 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableLongStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.platform.LocalContext import com.vayunmathur.camera.data.WearCameraStore import com.vayunmathur.camera.domain.WearWatchScreen import com.vayunmathur.camera.domain.wearWatchScreen import com.vayunmathur.camera.sync.WearLinkBridge import com.vayunmathur.library.wearlink.WearShellActivity+import kotlinx.coroutines.delay /** * Camera watch companion entry: while the phone Camera app is not open@@ -16,7 +21,12 @@ import com.vayunmathur.library.wearlink.WearShellActivity * open it shows only the live viewfinder streamed from the phone camera * plus one mode-themed capture button. The watch has no camera of its * own. Screen changes travel through [WearRoute], driven by the mirrored- * `phoneOpen` state (see `wearWatchScreen`).+ * `phoneOpen` state plus frame liveness (see `wearWatchScreen`): a+ * `phoneOpen` whose frames never arrive — a stale conduit replay, a lost+ * preview request, a stalled stream — falls back to the open-on-phone+ * button after a short window instead of waiting forever, and the+ * preview request is retried while no frame is held so a stream that+ * recovers routes back to the viewfinder on its own. */ class WearMainActivity : WearShellActivity<WearRoute, WearCameraStore>(WearRoute.OpenOnPhone) { // No Bluetooth prompt here: the Watch app owns the conduit link@@ -30,10 +40,46 @@ class WearMainActivity : WearShellActivity<WearRoute, WearCameraStore>(WearRoute route: WearRoute, navigate: (WearRoute) -> Unit, ) {+ val context = LocalContext.current val connected by store.linkConnected.collectAsState() val state by store.state.collectAsState()+ val previewJpeg by store.previewJpeg.collectAsState()+ val lastFrameAt by store.lastPreviewFrameAtMillis.collectAsState()+ val awaitingSince by store.awaitingPreviewSinceMillis.collectAsState()++ // The frame-liveness decision is time-based: tick while it can+ // still change so the timeout fires without any state change.+ var nowMillis by remember { mutableLongStateOf(System.currentTimeMillis()) }+ LaunchedEffect(connected, state.phoneOpen, previewJpeg, awaitingSince) {+ while (connected && state.phoneOpen) {+ nowMillis = System.currentTimeMillis()+ delay(TICK_MS)+ nowMillis = System.currentTimeMillis()+ }+ }++ // Ask for the stream, and keep asking while no frame is held: a+ // preview request lost to a stale state replay or a link edge is+ // retried instead of leaving the watch waiting on one send.+ LaunchedEffect(connected, state.phoneOpen, previewJpeg) {+ if (connected && state.phoneOpen && previewJpeg == null) {+ while (true) {+ WearLinkBridge.startPreview(context)+ delay(PREVIEW_RETRY_MS)+ }+ }+ }+ val target =- when (wearWatchScreen(connected, state)) {+ when (+ wearWatchScreen(+ linkConnected = connected,+ state = state,+ hasLiveFrame = previewJpeg != null,+ frameAgeMillis = lastFrameAt?.let { nowMillis - it },+ awaitingAgeMillis = awaitingSince?.let { nowMillis - it },+ )+ ) { WearWatchScreen.OpenOnPhone -> WearRoute.OpenOnPhone WearWatchScreen.Viewfinder -> WearRoute.Viewfinder }@@ -45,4 +91,9 @@ class WearMainActivity : WearShellActivity<WearRoute, WearCameraStore>(WearRoute WearRoute.Viewfinder -> WearViewfinderScreen(store = store) } }++ private companion object {+ const val TICK_MS = 250L+ const val PREVIEW_RETRY_MS = 1_000L+ } }
camera/src/main/java/com/vayunmathur/camera/ui/WearOpenOnPhoneScreen.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/WearOpenOnPhoneScreen.kt b/camera/src/main/java/com/vayunmathur/camera/ui/WearOpenOnPhoneScreen.ktindex 626786f2a8..4e211026b8 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/WearOpenOnPhoneScreen.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/WearOpenOnPhoneScreen.kt@@ -9,17 +9,19 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp-import androidx.wear.compose.material.Button-import androidx.wear.compose.material.Text import com.vayunmathur.camera.R import com.vayunmathur.camera.sync.WearLinkBridge+import com.vayunmathur.library.ui.IconMobileArrowRight+import com.vayunmathur.library.wearlink.WearFilledButton /** * The watch's only screen while the phone Camera app is not open: one- * centred filled button (watch design rule: filled buttons only, on the- * Wear Material themed background the shell provides). Tapping it sends- * the conduit open-on-phone handoff; the phone launches Camera, reports- * `phoneOpen`, and the watch routes to the viewfinder.+ * centred filled button with its phone icon (watch design rule: filled+ * buttons only, on the Wear Material themed background the shell+ * provides). Tapping it sends the conduit open-on-phone handoff; the+ * phone launches Camera, reports `phoneOpen`, frames arrive, and the+ * watch routes to the viewfinder. This is also the honest fallback when+ * an open phone's stream does not deliver frames. */ @Composable fun WearOpenOnPhoneScreen() {@@ -31,8 +33,10 @@ fun WearOpenOnPhoneScreen() { @Composable internal fun WearOpenOnPhoneContent(onOpenOnPhone: () -> Unit) { Box(modifier = Modifier.fillMaxSize().padding(horizontal = 24.dp), contentAlignment = Alignment.Center) {- Button(onClick = onOpenOnPhone) {- Text(stringResource(R.string.wear_open_in_phone))- }+ WearFilledButton(+ label = stringResource(R.string.wear_open_in_phone),+ onClick = onOpenOnPhone,+ icon = { IconMobileArrowRight() },+ ) } }
camera/src/main/java/com/vayunmathur/camera/ui/WearViewfinderScreen.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/WearViewfinderScreen.kt b/camera/src/main/java/com/vayunmathur/camera/ui/WearViewfinderScreen.ktindex a4b2fdc2a2..7e8700fa4a 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/WearViewfinderScreen.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/WearViewfinderScreen.kt@@ -17,14 +17,10 @@ import androidx.compose.ui.draw.rotate import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext-import androidx.compose.ui.res.stringResource-import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.wear.compose.material.Button import androidx.wear.compose.material.ButtonDefaults import androidx.wear.compose.material.MaterialTheme-import androidx.wear.compose.material.Text-import com.vayunmathur.camera.R import com.vayunmathur.camera.data.WearCameraStore import com.vayunmathur.camera.domain.WearCaptureTheme import com.vayunmathur.camera.domain.wearCaptureButtonSpec@@ -69,9 +65,12 @@ fun WearViewfinderScreen(store: WearCameraStore) { /** * Stateless viewfinder behind [WearViewfinderScreen]: the mirrored phone- * state and the decoded frame (null while waiting) are parameters, so- * previews render the waiting state with no phone. Only the frame and- * the one capture button are composed — nothing else.+ * state and the decoded frame are parameters, so previews render with no+ * phone. Only the frame and the one capture button are composed —+ * nothing else. A null frame is the brief moment before the first frame+ * of a live stream (routing falls back to the open-on-phone screen when+ * frames do not arrive, see `wearWatchScreen`), so it renders as an+ * empty viewfinder, never as a waiting message. */ @Composable internal fun WearViewfinderContent(@@ -89,13 +88,6 @@ internal fun WearViewfinderContent( contentScale = ContentScale.Crop, modifier = Modifier.fillMaxSize().rotate(rotation.toFloat()), )- } else {- Text(- stringResource(R.string.wear_viewfinder_waiting),- style = MaterialTheme.typography.body2,- textAlign = TextAlign.Center,- modifier = Modifier.align(Alignment.Center).padding(horizontal = 16.dp),- ) } // Small capture button, lifted 24dp so the round bezel never
camera/src/main/res/values/wear_strings.xml
diff --git a/camera/src/main/res/values/wear_strings.xml b/camera/src/main/res/values/wear_strings.xmlindex 29a7047894..332c596632 100644--- a/camera/src/main/res/values/wear_strings.xml+++ b/camera/src/main/res/values/wear_strings.xml@@ -1,5 +1,4 @@ <?xml version="1.0" encoding="utf-8"?> <resources> <string name="wear_open_in_phone">Open in phone</string>- <string name="wear_viewfinder_waiting">Waiting for the phone viewfinder…</string> </resources>
camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/WearMetadataPreviews.kt
diff --git a/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/WearMetadataPreviews.kt b/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/WearMetadataPreviews.ktindex 8c099e9dd4..b3c46c9cf5 100644--- a/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/WearMetadataPreviews.kt+++ b/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/WearMetadataPreviews.kt@@ -36,7 +36,7 @@ private val fixtureState = WearCameraState( * * Exception (honest, not faked): the viewfinder's live frame is a JPEG * streamed from the phone camera; Layoutlib has no phone camera, so the- * viewfinder capture renders its real waiting state with the capture+ * viewfinder capture renders the empty viewfinder with only the capture * button visible — no synthetic camera image is invented. * `./gradlew :camera:metadata` copies renders into `metadata_data/photos/camera/wear/`. */
camera/src/test/java/com/vayunmathur/camera/data/WearCameraStoreTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/data/WearCameraStoreTest.kt b/camera/src/test/java/com/vayunmathur/camera/data/WearCameraStoreTest.ktnew file mode 100644index 0000000000..5120739276--- /dev/null+++ b/camera/src/test/java/com/vayunmathur/camera/data/WearCameraStoreTest.kt@@ -0,0 +1,91 @@+package com.vayunmathur.camera.data++import com.vayunmathur.library.wearlink.camera.PreviewFrame+import com.vayunmathur.library.wearlink.camera.WearCameraState+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNull+import kotlin.test.assertTrue++class WearCameraStoreTest {++ @Test+ fun awaitingWindowStartsOnceAndRepublicationDoesNotRestartIt() {+ var now = 1_000L+ val store = WearCameraStore { now }+ store.setLinkConnected(true)+ store.applyState(WearCameraState(phoneOpen = true, previewStreaming = true))+ assertEquals(1_000L, store.awaitingPreviewSinceMillis.value)++ // The phone republishes the same state every 250 ms; the wait+ // window must keep running from the first sight, not restart.+ now = 2_500L+ store.applyState(WearCameraState(phoneOpen = true, previewStreaming = true))+ assertEquals(1_000L, store.awaitingPreviewSinceMillis.value)++ // Phone closes: no longer waiting, whatever arrives later.+ now = 3_000L+ store.applyState(WearCameraState(phoneOpen = false))+ assertNull(store.awaitingPreviewSinceMillis.value)+ }++ @Test+ fun frameArrivalEndsAwaitingAndStampsFrameTime() {+ var now = 1_000L+ val store = WearCameraStore { now }+ store.setLinkConnected(true)+ store.applyState(WearCameraState(phoneOpen = true, previewStreaming = true))+ now = 1_400L+ assertTrue(store.applyPreviewFrame(PreviewFrame.encode(1L, 10L, 320, 240, 0, byteArrayOf(1))))+ assertNull(store.awaitingPreviewSinceMillis.value)+ assertEquals(1_400L, store.lastPreviewFrameAtMillis.value)++ // Streaming stops: the held frame leaves and a fresh short grace+ // window (not an indefinite wait) starts for the stream to resume.+ now = 2_000L+ store.applyState(WearCameraState(phoneOpen = true, previewStreaming = false))+ assertNull(store.previewJpeg.value)+ assertNull(store.lastPreviewFrameAtMillis.value)+ assertEquals(2_000L, store.awaitingPreviewSinceMillis.value)+ }++ @Test+ fun restartedPhoneSequenceWithNewerTimestampIsAccepted() {+ val store = WearCameraStore { 5_000L }+ store.setLinkConnected(true)+ store.applyState(WearCameraState(phoneOpen = true, previewStreaming = true))+ // A long-lived phone stream reaches a high sequence.+ assertTrue(store.applyPreviewFrame(PreviewFrame.encode(400L, 900L, 320, 240, 0, byteArrayOf(4))))+ // Phone process dies and restarts: its sequence starts low again+ // while frame timestamps move forward. Those frames must land —+ // dropping them as stale is what froze the watch on no frame.+ assertTrue(store.applyPreviewFrame(PreviewFrame.encode(1L, 950L, 320, 240, 0, byteArrayOf(1))))+ assertTrue(store.applyPreviewFrame(PreviewFrame.encode(2L, 960L, 320, 240, 0, byteArrayOf(2))))+ assertTrue(byteArrayOf(2).contentEquals(store.previewJpeg.value!!))+ // A replay of the pre-restart stream keeps its old timestamp and+ // is still stale, as is a duplicate of the newest frame.+ assertFalse(store.applyPreviewFrame(PreviewFrame.encode(400L, 900L, 320, 240, 0, byteArrayOf(4))))+ assertFalse(store.applyPreviewFrame(PreviewFrame.encode(2L, 960L, 320, 240, 0, byteArrayOf(2))))+ assertEquals(2L, store.droppedStaleFrames)+ }++ @Test+ fun linkReconnectReplayRestartsAwaitingWindow() {+ var now = 1_000L+ val store = WearCameraStore { now }+ // Registration replay delivers the cached state before the link+ // status: the wait window starts, then the link drop clears it.+ store.applyState(WearCameraState(phoneOpen = true, previewStreaming = true))+ assertEquals(1_000L, store.awaitingPreviewSinceMillis.value)+ store.setLinkConnected(false)+ assertNull(store.awaitingPreviewSinceMillis.value)+ assertNull(store.previewJpeg.value)++ // Reconnect with the phone still reported open earns a fresh+ // window for the re-requested stream to deliver a frame.+ now = 9_000L+ store.setLinkConnected(true)+ assertEquals(9_000L, store.awaitingPreviewSinceMillis.value)+ }+}
camera/src/test/java/com/vayunmathur/camera/domain/WearWatchPresentationTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/domain/WearWatchPresentationTest.kt b/camera/src/test/java/com/vayunmathur/camera/domain/WearWatchPresentationTest.ktindex e572496ee1..ebed7d88cd 100644--- a/camera/src/test/java/com/vayunmathur/camera/domain/WearWatchPresentationTest.kt+++ b/camera/src/test/java/com/vayunmathur/camera/domain/WearWatchPresentationTest.kt@@ -15,11 +15,23 @@ class WearWatchPresentationTest { // also what an older phone publishes): only the button. assertEquals( WearWatchScreen.OpenOnPhone,- wearWatchScreen(linkConnected = true, state = WearCameraState(phoneOpen = false)),+ wearWatchScreen(+ linkConnected = true,+ state = WearCameraState(phoneOpen = false),+ hasLiveFrame = false,+ frameAgeMillis = null,+ awaitingAgeMillis = null,+ ), ) assertEquals( WearWatchScreen.OpenOnPhone,- wearWatchScreen(linkConnected = true, state = WearCameraState()),+ wearWatchScreen(+ linkConnected = true,+ state = WearCameraState(),+ hasLiveFrame = false,+ frameAgeMillis = null,+ awaitingAgeMillis = null,+ ), ) } @@ -27,7 +39,13 @@ class WearWatchPresentationTest { fun phoneOpenShowsViewfinderAndCaptureOnly() { assertEquals( WearWatchScreen.Viewfinder,- wearWatchScreen(linkConnected = true, state = WearCameraState(phoneOpen = true)),+ wearWatchScreen(+ linkConnected = true,+ state = WearCameraState(phoneOpen = true),+ hasLiveFrame = true,+ frameAgeMillis = 100L,+ awaitingAgeMillis = null,+ ), ) } @@ -37,7 +55,123 @@ class WearWatchPresentationTest { // single button: a stale phoneOpen can never hold the viewfinder. assertEquals( WearWatchScreen.OpenOnPhone,- wearWatchScreen(linkConnected = false, state = WearCameraState(phoneOpen = true)),+ wearWatchScreen(+ linkConnected = false,+ state = WearCameraState(phoneOpen = true),+ hasLiveFrame = true,+ frameAgeMillis = 0L,+ awaitingAgeMillis = null,+ ),+ )+ }+++ @Test+ fun firstFrameStillDueShowsViewfinderWithoutWaitingState() {+ // Phone just opened and the stream was just requested: the brief+ // window before the first frame is the viewfinder, not a wait.+ assertEquals(+ WearWatchScreen.Viewfinder,+ wearWatchScreen(+ linkConnected = true,+ state = WearCameraState(phoneOpen = true, previewStreaming = true),+ hasLiveFrame = false,+ frameAgeMillis = null,+ awaitingAgeMillis = WEAR_VIEWFINDER_FRAME_TIMEOUT_MS,+ ),+ )+ }++ @Test+ fun stalePhoneOpenWithoutFramesFallsBackToOpenOnPhone() {+ // A replayed phoneOpen (dead phone, lost preview request) with+ // mode/recording state present but no frame past the window must+ // not hold the viewfinder: the user gets the open button again.+ val stale = WearCameraState(phoneOpen = true, mode = "VIDEO", recording = true, previewStreaming = true)+ assertEquals(+ WearWatchScreen.OpenOnPhone,+ wearWatchScreen(+ linkConnected = true,+ state = stale,+ hasLiveFrame = false,+ frameAgeMillis = null,+ awaitingAgeMillis = WEAR_VIEWFINDER_FRAME_TIMEOUT_MS + 1L,+ ),+ )+ // Never started waiting (no session window at all) is also the button.+ assertEquals(+ WearWatchScreen.OpenOnPhone,+ wearWatchScreen(+ linkConnected = true,+ state = stale,+ hasLiveFrame = false,+ frameAgeMillis = null,+ awaitingAgeMillis = null,+ ),+ )+ }++ @Test+ fun stalledStreamWithFrozenFrameTimingFallsBack() {+ // A held frame older than the window is no longer live.+ assertEquals(+ WearWatchScreen.OpenOnPhone,+ wearWatchScreen(+ linkConnected = true,+ state = WearCameraState(phoneOpen = true, previewStreaming = true),+ hasLiveFrame = true,+ frameAgeMillis = WEAR_VIEWFINDER_FRAME_TIMEOUT_MS + 1L,+ awaitingAgeMillis = null,+ ),+ )+ assertEquals(+ WearWatchScreen.Viewfinder,+ wearWatchScreen(+ linkConnected = true,+ state = WearCameraState(phoneOpen = true, previewStreaming = true),+ hasLiveFrame = true,+ frameAgeMillis = WEAR_VIEWFINDER_FRAME_TIMEOUT_MS,+ awaitingAgeMillis = null,+ ),+ )+ }++ @Test+ fun linkReconnectReplayGetsFreshWindowThenTimesOut() {+ // Conduit replay on registration: cached phoneOpen arrives with+ // the link. It earns one short window for frames; if none come+ // (the replay was from a dead phone) it falls back, and a later+ // real frame would route straight back to the viewfinder.+ val replayed = WearCameraState(phoneOpen = true)+ assertEquals(+ WearWatchScreen.OpenOnPhone,+ wearWatchScreen(+ linkConnected = false,+ state = replayed,+ hasLiveFrame = false,+ frameAgeMillis = null,+ awaitingAgeMillis = 0L,+ ),+ )+ assertEquals(+ WearWatchScreen.Viewfinder,+ wearWatchScreen(+ linkConnected = true,+ state = replayed,+ hasLiveFrame = false,+ frameAgeMillis = null,+ awaitingAgeMillis = 0L,+ ),+ )+ assertEquals(+ WearWatchScreen.OpenOnPhone,+ wearWatchScreen(+ linkConnected = true,+ state = replayed,+ hasLiveFrame = false,+ frameAgeMillis = null,+ awaitingAgeMillis = WEAR_VIEWFINDER_FRAME_TIMEOUT_MS + 1L,+ ), ) }
metadata_data/photos/camera/wear/1.png
diff --git a/metadata_data/photos/camera/wear/1.png b/metadata_data/photos/camera/wear/1.pngindex 2be8870afc..732217cc00 100644Binary files a/metadata_data/photos/camera/wear/1.png and b/metadata_data/photos/camera/wear/1.png differ
metadata_data/photos/camera/wear/2.png
diff --git a/metadata_data/photos/camera/wear/2.png b/metadata_data/photos/camera/wear/2.pngindex 9a979d733b..61e35b8e13 100644Binary files a/metadata_data/photos/camera/wear/2.png and b/metadata_data/photos/camera/wear/2.png differ
metadata_data/photos/camera/wear/3.png
diff --git a/metadata_data/photos/camera/wear/3.png b/metadata_data/photos/camera/wear/3.pngindex 1b0bb2ef40..b3a57c87e3 100644Binary files a/metadata_data/photos/camera/wear/3.png and b/metadata_data/photos/camera/wear/3.png differ