Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / camera / AGENTS.md

AGENTS.md15.4 KiBRenderedCodeRaw
1# AGENTS.md — camera/ (':camera')
2
3> Scoped supplement. Global rules in root `AGENTS.md` apply. Stay inside `camera/` + allowed shared modules. Do not root-scan.
4
5_Install: ./install camera (dev by default)._
6- Gradle: :camera / dir: camera/
7- Package roots present: ui, domain, platform, data, sync
8- Entry files: MainActivity.kt (sole launcher; routes watches to WearMainActivity), Route.kt (`Route.Camera` / `Route.Settings`), ui/WearMainActivity.kt
9- Deps: :library:ml, :library:wearlink, :library:log, Wear Compose (material + foundation)
10- Metadata: metadata_data/camera.md (present)
11- Rust: yes / screenshotTest: yes (phone + large-tablet preview-rendered over a screenshot-only fake viewfinder scene)
12
13## Overview
14**Purpose:** The most featureful open-source camera app — usable as the system camera and from the lockscreen. 100% offline.
15**Key features:**
16- Photo + video capture on a direct Camera2 implementation (`platform/camera2/` driver, sessions, extension and high-speed sessions) with per-mode sessions: single, portrait, night, slo-mo, video
17- Panorama stitching + night-burst alignment in the native Rust `camera_stitch` engine
18- Photo-sphere capture: guide waypoints spaced from the active lens's measured FOV at 40% overlap (rows reach the poles), heading/pitch from the fused rotation-vector sensor (gyro dead reckoning, then accelerometer+magnetometer, as fallbacks), stitch seeded with the HAL focal length, and output rendered onto a full 360×180 equirect canvas whose GPano XMP describes that sphere (Google Photos renders it as a sphere)
19- Portrait mode with on-device segmentation (MediaPipe selfie + MODNet matting on a Vulkan compute runtime, `.maml` assets)
20- QR/barcode scanning (zxing) and optional GPS geotagging written into EXIF
21**Architecture / data-flow:** Single-screen `ui/CameraScreen` + `platform/CameraViewModel` over per-mode `platform/` session objects on the `platform/camera2/` driver; `domain/` holds pure lens-selection logic, `platform/` capability queries; heavy work (stitch, night align, bokeh) goes through Rust JNI (`platform/StitchNative`) and lands in MediaStore via `data/MediaStoreSaver`. `onCreateBeforeContent` only honors the `ACTION_IMAGE_CAPTURE` capture-for-result intent; `Content()` sits behind a camera+mic permission gate.
22**Lens selection:** `platform/LensSelector.enumerateLenses` feeds `domain/LensDiscovery` with every top-level camera id plus each logical camera's `getPhysicalCameraIds()` sub-cameras (validated against their stream-configuration maps); the Camera2 preview always opens the facing's logical camera, so lens stops (sub-1x / tele included) are reached through the single continuous `ui/ZoomBar` row and pinch zoom, which apply the ratio to that session rather than opening a sub-camera id; live scrub/pinch moves go through `setZoomRatioLive` (no persistence, no lens sync), and only the committed final ratio (chip tap, scrub/pinch release, lens/Wear selection, via `setZoomRatio`) landing on a lens's nominal ratio syncs `selectedLens` to that lens (startup persistence + night-detection reset, via `syncSelectedLensToZoomStop`), while a ratio between stops leaves the selection alone.
23**Entry points + reading order:** `MainActivity.kt` → `Route.kt` (`Route.Camera`/`Settings`) → `ui/CameraScreen.kt` → `platform/CameraViewModel.kt` → `platform/CameraSessions.kt` → `platform/PanoramaEngine.kt` / `NightCaptureEngine.kt`.
24
25## One APK, two form factors (phone + watch)
26This module is the single Camera APK for phones and watches: same
27`applicationId = "com.vayunmathur.camera"` and signing on both. The manifest deliberately declares no
28`android.hardware.type.watch` feature (required=true would block
29phones; required=false is disallowed for this feature), declares the
30camera feature not-required (a watch has no camera; the watch
31experience is a remote viewfinder streamed from the phone), and
32carries `com.google.android.wearable.standalone=false`; distribution
33is sideload/F-Droid, not Play's watch channel, so the APK installs on
34phones and watches alike. `platform/FormFactor.kt` reads
35`PackageManager.FEATURE_WATCH` at runtime: `MainActivity` (the sole
36launcher entry) forwards watches to `ui/WearMainActivity` before any
37phone state (CameraViewModel, phone wear sync, permission gate) is
38touched; phones get the camera UI. The module declares no broadcast
39receivers, so `MainActivity`'s forward is the only form-factor gate:
40the phone-side `platform/WearCameraSync` starts only on the phone
41path, and the watch-side `sync/WearLinkBridge` is created only by
42`ui/WearMainActivity` (`exported=false`, no launcher filter).
43
44## Wear sync (Watch conduit client)
45**Protocol:** shared wire types live in `:library:wearlink` at `com.vayunmathur.library.wearlink.camera.WearCameraProtocol`; the golden-JSON tests in this module pin the shared code's wire format.
46- Mirrored state + thumbnail: conduit data items (`/camera/state`, `/camera/thumbnail`, phone -> watch)
47- Control ops + live viewfinder: conduit messages (`/camera/op` watch -> phone, `/camera/preview-frame` phone -> watch)
48- **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`.
49
50## Watch experience (in this module)
51- `ui/WearMainActivity` (a `WearShellActivity`) routes `ui/WearRoute` over viewfinder / controls / last capture; the watch camera remote never opens a local camera — with no phone nearby it says so, honestly. Mirrored truth is in-memory only in `data/WearCameraStore` (no offline copy: nothing is usable without the phone).
52- 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.
53- 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.
54**Gotchas:**
55- Capture code lives in `platform/` (the Camera2 driver in `platform/camera2/`); there is no `util/` root in this module
56- Camera2 session reopen after a surface destroy (background/resume, size change) is serialized in `platform/camera2/`: a session parks a reopen until the previous device's real `onClosed` (device `close()` only initiates HAL teardown), and `PreviewSurfaceBridge` forgets its pinned stream size on destroy so a new `SurfaceTexture` is re-pinned before configure
57- Listing screenshots are preview-rendered (`:camera:metadata`): Layoutlib can't open a camera, so `src/screenshotTest` draws the deterministic `FakeCameraPreview` scene behind the real top bar/grid/shutter/mode controls. `Route.Settings` is captured from the stateless `CameraSettingsContent` slice in `ui/SettingsPage.kt` with literal fixtures. The `screenshotFakePreview` parameter on `CameraScreen`/`CameraPreviewSurface` is the same seam in `main` — production-default null, never passed by `MainActivity`, so no fake frame can appear on device
58- Volume keys act as the shutter, gated on the camera route being foreground; Esc/space covers desktop keyboards
59- `.maml` model assets ship uncompressed (`noCompress`) — never compress them
60- Capture rotation is one decision in `domain/CaptureRotation.kt` (sensor orientation from characteristics + physical display rotation + facing), consumed by stills (`StillCapturePlanner`), analysis frames, and video/slo-mo/Motion-Photo muxer hints. The activity is portrait-locked, so the window display rotation is always 0 and must never orient captures: the physical quadrant comes from the `OrientationEventListener` (fed to sessions via `CameraViewModel.setCaptureDisplayRotationDegrees`); preview sizing/metering keep the window rotation. The live preview's *display* rotates in its view layer with that same physical quadrant (`ui/DeviceRotation.previewRotationForOrientation` + `previewCoverScale`, applied in `CameraPreviewSurface`), as do the ZoomBar numbers and the config-row icons — glyph/display rotation only, never a second capture rotation. Metadata convention: JPEG/JPEG_R keep sensor pixels + EXIF orientation (flip-carrying for mirrored raw front pixels), AVIF bakes rotation/mirror into pixels with EXIF Normal and no `irot`, MP4 carries only the muxer orientation hint, night/pano RGB outputs bake pixels upright. No DNG writer is wired (`DngMetadata` only assembles requirements)
61
62## Key files
63### Root — entry + navigation
64- com/vayunmathur/camera/MainActivity.kt
65- com/vayunmathur/camera/Route.kt
66### domain/ — pure lens-selection logic
67- com/vayunmathur/camera/domain/LensModel.kt
68- com/vayunmathur/camera/domain/LensDiscovery.kt — offered-lens computation over pure `LensCandidate`s: every session-configurable camera the HAL exposes (top-level ids plus logical cameras' physical sub-cameras) is offered; a physical ultra-wide is never dropped for overlapping the logical zoom range; on logical-only devices the logical camera's zoom-ratio range synthesizes the ultra-wide (sub-1x) and telephoto (2x) stops no camera id exposes, anchored to the logical id (`PhysicalLens.synthesizedFromZoomRange`)
69- com/vayunmathur/camera/domain/LensSelectionLogic.kt
70- com/vayunmathur/camera/domain/ArgbToI420Converter.kt — bulk ARGB→YUV 4:2:0 for the Motion Photo clip
71- com/vayunmathur/camera/domain/MotionSaveQueue.kt — bound on Motion Photos still saving after the shutter
72- com/vayunmathur/camera/domain/StillCaptureModePolicy.kt — ZSL vs MINIMIZE_LATENCY choice
73- com/vayunmathur/camera/domain/CaptureRotation.kt — the one capture-rotation decision (sensor + physical display + facing) and EXIF convention
74- com/vayunmathur/camera/domain/AnalysisRotation.kt — analysis-frame rotation (same formula as CaptureRotation)
75- com/vayunmathur/camera/domain/PanoXmp.kt
76- com/vayunmathur/camera/domain/SphereWaypoints.kt — FOV-derived photo-sphere waypoint layout + fused-orientation yaw/pitch math
77- com/vayunmathur/camera/domain/MotionPhotoWriter.kt
78### platform/ — capability queries
79- com/vayunmathur/camera/platform/CameraCapabilities.kt
80- com/vayunmathur/camera/platform/WearCameraSync.kt — phone conduit client node; mirrored state/thumbnail publish, op handling, preview streaming; wire protocol `com.vayunmathur.library.wearlink.camera.WearCameraProtocol` (WCF1 frames) in `:library:wearlink`; this module's `WearCameraProtocolTest` pins the golden JSON/frame bytes
81- com/vayunmathur/camera/platform/LensSelector.kt
82### ui/ — single screen + settings + panes
83- com/vayunmathur/camera/ui/CameraScreen.kt
84- com/vayunmathur/camera/ui/CameraScreenState.kt
85- com/vayunmathur/camera/ui/CameraScreenKit.kt
86- com/vayunmathur/camera/ui/CameraScreenEffects.kt
87- com/vayunmathur/camera/ui/CameraPreviewBox.kt
88- com/vayunmathur/camera/ui/CameraPreviewOverlays.kt
89- com/vayunmathur/camera/ui/CaptureFlashOverlay.kt
90- com/vayunmathur/camera/ui/CameraOverlays.kt
91- com/vayunmathur/camera/ui/CameraPanoramaOverlay.kt
92- com/vayunmathur/camera/ui/CameraTopBar.kt
93- com/vayunmathur/camera/ui/CameraBottomControls.kt
94- com/vayunmathur/camera/ui/CameraShutterPane.kt
95- com/vayunmathur/camera/ui/CameraShutterRow.kt
96- com/vayunmathur/camera/ui/CameraModeBars.kt
97- com/vayunmathur/camera/ui/CameraModesPane.kt
98- com/vayunmathur/camera/ui/CameraSettingsBars.kt
99- com/vayunmathur/camera/ui/CameraSettingsColumn.kt
100- com/vayunmathur/camera/ui/CameraStatusIndicators.kt
101- com/vayunmathur/camera/ui/CameraWideLayout.kt
102- com/vayunmathur/camera/ui/ZoomBar.kt — the single zoom row (shown by default in `CameraSettingsColumn` when no setting pane is selected, hidden in zoom-locked panorama/photosphere; there is no zoom entry in the config row below it): continuous relative scrub (live via `setZoomRatioLive`, committed on release) + lens-stop chips, numbers rotating with device orientation. Scrub mapping is the pure `domain/ZoomScrub.kt` (log-scale, slop-compensated, monotonic across lens stops, chip set frozen mid-scrub)
103- com/vayunmathur/camera/ui/SettingsPage.kt
104- com/vayunmathur/camera/ui/CameraPostProcessingSettings.kt
105### platform/ — ViewModel, sessions, engines
106- com/vayunmathur/camera/platform/CameraViewModel.kt
107- com/vayunmathur/camera/platform/CameraViewModelActions.kt
108- com/vayunmathur/camera/platform/CameraViewModelTypes.kt
109- com/vayunmathur/camera/platform/CameraStartup.kt
110- com/vayunmathur/camera/platform/CameraSessions.kt
111- com/vayunmathur/camera/platform/CaptureOrientation.kt — pushes the physical display rotation to live sessions
112- com/vayunmathur/camera/platform/CameraSingleCapture.kt
113- com/vayunmathur/camera/platform/CameraPhotoCapture.kt
114- com/vayunmathur/camera/platform/CameraCaptureProcessing.kt
115- com/vayunmathur/camera/platform/CameraVideoRecording.kt
116- com/vayunmathur/camera/platform/Camera2VideoRecordingEngine.kt
117- com/vayunmathur/camera/platform/Camera2SloMoGlue.kt
118- com/vayunmathur/camera/platform/CameraManualControls.kt
119- com/vayunmathur/camera/platform/CameraControls.kt
120- com/vayunmathur/camera/platform/CameraNightMode.kt
121- com/vayunmathur/camera/platform/CameraNightCapture.kt
122- com/vayunmathur/camera/platform/NightCaptureEngine.kt
123- com/vayunmathur/camera/platform/PanoramaEngine.kt
124- com/vayunmathur/camera/platform/GpuStitcher.kt
125- com/vayunmathur/camera/platform/StitchNative.kt — JNI bridge into `camera_stitch`
126- com/vayunmathur/camera/platform/PortraitBokeh.kt
127- com/vayunmathur/camera/platform/BokehAnalyzer.kt
128- com/vayunmathur/camera/platform/PhotoAnalyzer.kt
129- com/vayunmathur/camera/platform/MotionPhotoEncoder.kt
130- com/vayunmathur/camera/platform/VideoProcessor.kt
131- com/vayunmathur/camera/platform/CodecSupport.kt
132### platform/camera2/ — Camera2 driver
133- com/vayunmathur/camera/platform/camera2/Camera2Driver.kt
134- com/vayunmathur/camera/platform/camera2/Camera2Session.kt
135- com/vayunmathur/camera/platform/camera2/Camera2SessionRingCapture.kt
136- com/vayunmathur/camera/platform/camera2/Camera2ExtensionSession.kt
137- com/vayunmathur/camera/platform/camera2/Camera2HighSpeedSession.kt
138- com/vayunmathur/camera/platform/camera2/StillCapture.kt
139- com/vayunmathur/camera/platform/camera2/StreamSelection.kt
140### Watch experience (ui/ + data/ + sync/)
141- com/vayunmathur/camera/ui/WearMainActivity.kt — watch entry (WearShellActivity)
142- com/vayunmathur/camera/ui/WearRoute.kt, WearViewfinderScreen.kt, WearControlsScreen.kt, WearLastCaptureScreen.kt
143- com/vayunmathur/camera/platform/FormFactor.kt — watch detection (`FEATURE_WATCH`)
144- com/vayunmathur/camera/data/WearCameraStore.kt — mirrored phone state + newest preview frame/thumbnail (in-memory only)
145- com/vayunmathur/camera/sync/WearLinkBridge.kt — watch conduit client node; mirrored state/thumbnail receive, preview frames, op sends
146### data/ — settings + saving
147- com/vayunmathur/camera/data/MediaStoreSaver.kt
148- com/vayunmathur/camera/data/CameraSettings.kt
149- com/vayunmathur/camera/data/CameraSaveLocation.kt
150- com/vayunmathur/camera/data/CameraSaveRouting.kt
151
152## Verify (this module only)
153```
154./gradlew :camera:compileDevKotlin
155./gradlew :camera:lint
156./gradlew :camera:checkMetadata
157```