1# D10 — Codex Restore Camera Path — Integration23Section 8 of the remaining-gaps brief. Adds a two-card "choose" surface and a4live camera Aztec-scan path to `CodexRestoreScreen`. The PDF import path is5unchanged — it just moves behind the new choose surface.67Draft files in this folder:8- `CodexRestoreChooseAndCamera.kt` — `CodexRestoreChoose` + `CodexCameraScan` composables (package `com.peachpasswords.ui.codex`).9- `CodexAztecFrame.kt` — per-frame Aztec decode for `ImageProxy` (package `com.peachpasswords.codex`). Already drafted, used unchanged.10- `CodexViewModel.camera.kt` — camera members to merge into the real `CodexViewModel`.1112## 1. Gradle deps — `app/build.gradle`1314CameraX current stable is **1.3.4**. ZXing core (`com.google.zxing:core:3.5.3`)15is already present (line 99) — no ZXing change needed. Add inside `dependencies {`16(near line 99, next to the existing zxing line):1718```gradle19 implementation "androidx.camera:camera-core:1.3.4"20 implementation "androidx.camera:camera-camera2:1.3.4"21 implementation "androidx.camera:camera-lifecycle:1.3.4"22 implementation "androidx.camera:camera-view:1.3.4"23```2425`androidx.lifecycle.compose.LocalLifecycleOwner` (used by the preview) is provided26by the already-present `androidx.lifecycle:lifecycle-runtime-compose:2.8.4`.2728## 2. AndroidManifest — CAMERA permission2930In `app/src/main/AndroidManifest.xml`, above `<application>`:3132```xml33 <uses-permission android:name="android.permission.CAMERA" />34 <uses-feature android:name="android.hardware.camera.any" android:required="false" />35```3637`required="false"` keeps the app installable on camera-less devices (the denied/38unavailable case already shows the settings card).3940## 3. Merge camera members into `CodexViewModel.kt`4142Copy the commented body inside `CodexViewModelCameraMembers` (in43`CodexViewModel.camera.kt`) into the `CodexViewModel` class, and add the44`CameraScanState` sealed interface to that file (top level, beside45`RestoreState`). Drop the `draft_d10` package — it goes in46`com.peachpasswords.ui.codex`.4748Add imports to `CodexViewModel.kt`:49```kotlin50import androidx.camera.core.ImageProxy51import com.peachpasswords.codex.ChunkEnvelope52import com.peachpasswords.codex.CodexAztecFrame53```54(`CodexRestore` is already imported.)5556New public VM surface after merge:57- `val camera: StateFlow<CameraScanState>` (initial `Choosing`)58- `fun chooseRestore()` — show the two cards, reset restore state59- `fun startCameraScan()` — begin/restart scanning60- `fun cameraTimedOut()` — record 10s no-detection61- `fun onCameraFrame(image: ImageProxy): Boolean` — analyze one frame; returns62 true once a payload reconstructs (sets `RestoreState.Decoded`, same entry point63 as the PDF path, so the existing `DecodedBody` password/confirm flow works64 unchanged).6566Verified against reality: `ChunkEnvelope` has `s: Int` (stripe) and `i: Int`67(index) — the dedup key `"${env.s}:${env.i}"` is correct.68`CodexRestore.parseEnvelope(text): ChunkEnvelope?` and69`CodexRestore.reconstructPayload(List<ChunkEnvelope>): CodexPayload` are public70and reused unchanged. `RestoreState.Decoded(payload, codesFound)` matches the71existing constructor.7273## 4. Wire `CodexRestoreScreen.kt`7475The screen currently launches the PDF picker directly from `RestoreState.Idle`.76Replace that branch so `Idle` is gated by the camera state.7778**Add imports:**79```kotlin80import androidx.compose.foundation.layout.Box81```82(plus the new composables are same-package, no import needed).8384**Collect the camera state** — after the existing `state` collection (around line 36):85```kotlin86 val cameraState by vm.camera.collectAsStateWithLifecycle()87```8889**Replace the `RestoreState.Idle` branch.** Current anchor (lines 53-54):90```kotlin91 is RestoreState.Idle ->92 CodexButton("Choose Codex PDF", accent, filled = true) { pickLauncher.launch("application/pdf") }93```94Replace with:95```kotlin96 is RestoreState.Idle -> when (val cs = cameraState) {97 is CameraScanState.Choosing -> CodexRestoreChoose(98 accent = accent,99 onScanCamera = { vm.startCameraScan() },100 onImportPdf = { pickLauncher.launch("application/pdf") },101 )102 is CameraScanState.Scanning -> CodexCameraScan(103 accent = accent,104 timedOut = false,105 codesFound = cs.codesFound,106 onFrame = { vm.onCameraFrame(it) },107 onTimeout = { vm.cameraTimedOut() },108 onRetry = { vm.startCameraScan() },109 )110 is CameraScanState.Timeout -> CodexCameraScan(111 accent = accent,112 timedOut = true,113 codesFound = cs.codesFound,114 onFrame = { vm.onCameraFrame(it) },115 onTimeout = { },116 onRetry = { vm.startCameraScan() },117 )118 }119```120121When `onCameraFrame` reconstructs a payload it sets `RestoreState.Decoded`, so122the `when(state)` flips to the existing `DecodedBody` automatically — the camera123overlay disappears and the password prompt appears. No further wiring needed for124the decode→restore handoff.125126**Reset on entry:** ensure the screen lands on the choose surface each visit. The127VM defaults `camera` to `Choosing` and `restore` to `Idle`, so a fresh VM is128already correct. If the screen can be revisited with a retained VM, call129`vm.chooseRestore()` from a `LaunchedEffect(Unit)` at the top of the screen.130131**Error branch:** the existing `RestoreState.Error` "Choose another file" button132re-launches the PDF picker. Optionally also call `vm.chooseRestore()` there so a133failed camera scan returns to the two cards; the brief doesn't require it.134135## 5. Notes / risks136137- The intro paragraph (lines 46-49) says "Choose a Codex PDF…". Suggest softening138 to mention camera too, e.g. "Scan the printed Aztec grid with your camera, or139 choose the Codex PDF…". Cosmetic; not required.140- `CodexCameraScan` runs its own 10s `LaunchedEffect` timer keyed on `timedOut`;141 it also reports `onTimeout`. Because the VM's `Scanning -> Timeout` transition142 re-composes with `timedOut = true`, the timer self-cancels — no double-fire.143- The analyzer closes every `ImageProxy` in a `finally`; `onCameraFrame` must not144 close it (it doesn't — `CodexAztecFrame.decode` explicitly does not close).145- Reuses `CodexAztecFrame.decode` (scene detector, no PURE_BARCODE) for camera146 frames — distinct from `CodexAztec.decode` which is for cropped PDF cells.147- No `RuneBackground` usage here; the screen already uses `ShellBackground`.148- `FLAG_SECURE`: the restore screen does not currently set it; out of scope for149 D10. Flag if a security pass wants the live camera+decoded preview protected.150```