Modern Apps git

← Modern-Apps.git

large refactor

Commit
cd6a8ab472ae6974c734adca2c52de1cedf346fc
Author
vayun-mathur <[email protected]>
When
2026-09-13 16:49 UTC (2 months ago)
Parents
73badab

Diff

camera/src/main/java/com/vayunmathur/camera/ui/CameraBottomControls.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraBottomControls.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraBottomControls.ktnew file mode 100644index 0000000000..747a8d04e0--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraBottomControls.kt@@ -0,0 +1,122 @@+package com.vayunmathur.camera.ui++import android.content.ActivityNotFoundException+import android.content.ClipData+import android.content.Intent+import android.graphics.Bitmap+import android.provider.MediaStore+import android.util.Log+import androidx.compose.runtime.Composable+import androidx.compose.ui.platform.LocalContext+import com.vayunmathur.camera.Route+import com.vayunmathur.camera.util.CameraMode+import com.vayunmathur.camera.util.CameraViewModel+import com.vayunmathur.camera.util.captureVideoSnapshot+import com.vayunmathur.camera.util.startBurst+import com.vayunmathur.camera.util.stopBurst+import com.vayunmathur.camera.util.togglePauseRecording+import com.vayunmathur.library.ui.ExternalIntents+import com.vayunmathur.library.ui.isExpandedWidth+import com.vayunmathur.library.util.NavBackStack++/**+ * Shutter row, mode selector and bottom bar below the preview, plus their adaptive+ * side-by-side placement on wide windows. The phone stack and the wide-layout slots+ * share one set of callbacks; behavior identical to the previously inline code.+ */+@Composable+internal fun CameraBottomControls(+    state: CameraScreenState,+    viewModel: CameraViewModel,+    backStack: NavBackStack<Route>,+    onCaptureResult: ((Bitmap?) -> Unit)?,+    onCapture: () -> Unit,+    animatedRotation: Float,+) {+    val context = LocalContext.current+    val cameraMode = state.cameraMode+    val expandedSide = isExpandedWidth()++    // A last-capture URI can be stale (image deleted) or absent (nothing shot+    // this session). Verify it still resolves, else open the gallery collection;+    // wrap the whole launch so a missing image never crashes the app.+    val onGallery: () -> Unit = {+        runCatching {+            val uri = state.lastCaptureUri?.takeIf {+                runCatching { context.contentResolver.getType(it) }.getOrNull() != null+            }+            val baseIntent = if (uri != null) {+                Intent(Intent.ACTION_VIEW).apply {+                    setDataAndType(uri, context.contentResolver.getType(uri) ?: "image/*")+                    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)+                    clipData = ClipData.newRawUri(null, uri)+                }+            } else {+                Intent(Intent.ACTION_MAIN).apply {+                    addCategory(Intent.CATEGORY_APP_GALLERY)+                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+                }+            }+            try {+                if (!ExternalIntents.launch(context, baseIntent)) {+                    throw ActivityNotFoundException("System gallery not found")+                }+            } catch (_: ActivityNotFoundException) {+                val fallback = if (uri != null) {+                    Intent(Intent.ACTION_VIEW).apply {+                        setDataAndType(uri, context.contentResolver.getType(uri) ?: "image/*")+                        addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)+                        clipData = ClipData.newRawUri(null, uri)+                    }+                } else {+                    Intent(Intent.ACTION_VIEW, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)+                }+                ExternalIntents.launch(context, Intent.createChooser(fallback, null))+            }+        }.onFailure { Log.w("CameraScreen", "Could not open image viewer", it) }+    }+    val onPickerChanged: (Boolean) -> Unit = { photo ->+        if (photo) {+            viewModel.switchCameraMode(CameraMode.PHOTO)+        } else {+            viewModel.switchCameraMode(CameraMode.VIDEO)+        }+    }++    // Expanded windows place the shutter and mode controls side by side+    // below the preview; short windows keep the phone stack.+    CameraWideLayout(+        shutter = {+            CameraShutterPane(+                cameraMode = cameraMode,+                isRecording = state.isRecording,+                isCapturing = state.isCapturing,+                recordingPaused = state.recordingPaused,+                videoSnapshotSupported = state.videoSnapshotSupported,+                galleryBitmap = state.galleryBitmap,+                onCaptureResult = onCaptureResult,+                onBurstStart = { viewModel.startBurst() },+                onBurstStop = { viewModel.stopBurst() },+                onCapture = onCapture,+                onPauseResume = { viewModel.togglePauseRecording() },+                onSnapshot = { viewModel.captureVideoSnapshot() },+                onFlipCamera = { viewModel.flipCamera() },+                onGallery = onGallery,+                iconRotation = animatedRotation,+                flipEnabled = !state.isSloMo,+            )+        },+        modes = {+            CameraModesPane(+                cameraMode = cameraMode,+                isPhotoType = state.isPhotoType,+                sloMoSupported = state.sloMoSupported,+                onModeSelected = { viewModel.switchCameraMode(it) },+                iconRotation = animatedRotation,+                onPickerChanged = onPickerChanged,+                onSettingsClick = { backStack.add(Route.Settings) },+            )+        },+        sideBySide = expandedSide,+    )+}
camera/src/main/java/com/vayunmathur/camera/ui/CameraModeBars.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraModeBars.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraModeBars.ktnew file mode 100644index 0000000000..1bc08c5eab--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraModeBars.kt@@ -0,0 +1,130 @@+package com.vayunmathur.camera.ui++import androidx.compose.foundation.background+import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.foundation.shape.RoundedCornerShape+import com.vayunmathur.library.ui.IconCamera+import com.vayunmathur.library.ui.IconVideoCamera+import com.vayunmathur.library.ui.IconSettings+import com.vayunmathur.library.ui.Text+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.rotate+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.sp+import com.vayunmathur.camera.util.CameraMode++@Composable+internal fun ModeSelector(+    cameraMode: CameraMode,+    isPhotoType: Boolean,+    sloMoSupported: Boolean?,+    onModeSelected: (CameraMode) -> Unit+) {+    Row(+        modifier = Modifier.fillMaxWidth(),+        horizontalArrangement = Arrangement.Center,+        verticalAlignment = Alignment.CenterVertically+    ) {+        val modes = if (isPhotoType) {+            listOf(+                CameraMode.PORTRAIT to "Portrait",+                CameraMode.PHOTO to "Photo",+                CameraMode.PANORAMA to "Pano",+                CameraMode.PHOTOSPHERE to "Sphere"+            )+        } else {+            // Only include Slo-Mo when the device's back camera actually supports true HFR.+            buildList {+                if (sloMoSupported != false) {+                    add(CameraMode.SLOW_MO to "Slo-Mo")+                }+                add(CameraMode.VIDEO to "Video")+                add(CameraMode.CINEMATIC to "Cinematic")+                add(CameraMode.TIMELAPSE to "Timelapse")+            }+        }+        modes.forEach { (mode, label) ->+            val isSelected = cameraMode == mode+            Text(+                text = label,+                color = Color.White,+                fontSize = 14.sp,+                fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,+                modifier = Modifier+                    .selectedPill(isSelected, RoundedCornerShape(20.dp))+                    .clickable { if (!isSelected) onModeSelected(mode) }+                    .padding(horizontal = 16.dp, vertical = 6.dp)+            )+        }+    }+}++@Composable+internal fun BottomBar(+    cameraMode: CameraMode,+    isPhotoType: Boolean,+    iconRotation: Float,+    onPickerChanged: (Boolean) -> Unit,+    onSettingsClick: () -> Unit+) {+    Row(+        modifier = Modifier+            .fillMaxWidth()+            .padding(horizontal = 16.dp, vertical = 8.dp),+        horizontalArrangement = Arrangement.SpaceBetween,+        verticalAlignment = Alignment.CenterVertically+    ) {+        Box(+            modifier = Modifier+                .size(44.dp)+                .background(Color(0xFF3C3C3C), CircleShape)+                .clickable(onClick = onSettingsClick),+            contentAlignment = Alignment.Center+        ) {+            IconSettings(+                tint = Color.White,+                modifier = Modifier.size(22.dp).rotate(iconRotation)+            )+        }++        Row(+            modifier = Modifier+                .background(Color(0xFF3C3C3C), RoundedCornerShape(20.dp))+                .padding(4.dp),+            verticalAlignment = Alignment.CenterVertically+        ) {+            Box(+                modifier = Modifier+                    .size(36.dp)+                    .selectedPill(isPhotoType, CircleShape, Color(0xFF5C5C5C))+                    .clickable { if (!isPhotoType) onPickerChanged(true) },+                contentAlignment = Alignment.Center+            ) {+                IconCamera(Modifier.size(20.dp).rotate(iconRotation), Color.White)+            }+            Box(+                modifier = Modifier+                    .size(36.dp)+                    .selectedPill(!isPhotoType, CircleShape, Color(0xFF5C5C5C))+                    .clickable { if (isPhotoType) onPickerChanged(false) },+                contentAlignment = Alignment.Center+            ) {+                IconVideoCamera(Modifier.size(20.dp).rotate(iconRotation), Color.White)+            }+        }++        Spacer(Modifier.size(44.dp))+    }+}
camera/src/main/java/com/vayunmathur/camera/ui/CameraModesPane.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraModesPane.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraModesPane.ktnew file mode 100644index 0000000000..7bf9aa4586--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraModesPane.kt@@ -0,0 +1,30 @@+package com.vayunmathur.camera.ui++import androidx.compose.runtime.Composable+import com.vayunmathur.camera.util.CameraMode++@Composable+internal fun CameraModesPane(+    cameraMode: CameraMode,+    isPhotoType: Boolean,+    sloMoSupported: Boolean?,+    onModeSelected: (CameraMode) -> Unit,+    iconRotation: Float,+    onPickerChanged: (Boolean) -> Unit,+    onSettingsClick: () -> Unit,+) {+    ModeSelector(+        cameraMode = cameraMode,+        isPhotoType = isPhotoType,+        sloMoSupported = sloMoSupported,+        onModeSelected = onModeSelected+    )++    BottomBar(+        cameraMode = cameraMode,+        isPhotoType = isPhotoType,+        iconRotation = iconRotation,+        onPickerChanged = onPickerChanged,+        onSettingsClick = onSettingsClick+    )+}
camera/src/main/java/com/vayunmathur/camera/ui/CameraPanoramaOverlay.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPanoramaOverlay.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPanoramaOverlay.ktnew file mode 100644index 0000000000..fabe275718--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPanoramaOverlay.kt@@ -0,0 +1,132 @@+package com.vayunmathur.camera.ui++import androidx.compose.animation.core.Animatable+import androidx.compose.animation.core.tween+import androidx.compose.foundation.Canvas+import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxSize+import com.vayunmathur.library.ui.CircularProgressIndicator+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.layout.width+import androidx.compose.foundation.shape.RoundedCornerShape+import com.vayunmathur.library.ui.Text+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.remember+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.Path+import androidx.compose.ui.graphics.StrokeCap+import androidx.compose.ui.graphics.drawscope.Stroke+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.sp+import com.vayunmathur.camera.R+import com.vayunmathur.camera.util.GuideDot+import com.vayunmathur.camera.util.GuideDotState++@Composable+internal fun PanoramaOverlay(+    isSweeping: Boolean,+    isStitching: Boolean,+    guideDots: List<GuideDot>,+    currentAngle: Float,+    sweepDirection: Int,+    currentPitch: Float,+    modifier: Modifier = Modifier+) {+    if (isStitching) {+        Box(modifier.fillMaxSize(), contentAlignment = Alignment.Center) {+            Column(horizontalAlignment = Alignment.CenterHorizontally) {+                CircularProgressIndicator(color = Color.White, strokeWidth = 2.dp)+                Spacer(Modifier.height(8.dp))+                Text(text = stringResource(R.string.processing_panorama_u2026), color = Color.White, fontSize = 16.sp)+            }+        }+        return+    }++    if (!isSweeping) return++    val capturedCount = guideDots.count { it.state == GuideDotState.CAPTURED }+    val totalDots = guideDots.size+    val halfFOV = 30f++    val flashAlpha = remember { Animatable(0f) }+    LaunchedEffect(capturedCount) {+        if (capturedCount > 1) {+            flashAlpha.snapTo(0.3f)+            flashAlpha.animateTo(0f, tween(250))+        }+    }++    Box(modifier.fillMaxSize()) {+        if (flashAlpha.value > 0f) {+            Box(Modifier.fillMaxSize().background(Color.White.copy(alpha = flashAlpha.value)))+        }++        Canvas(Modifier.fillMaxSize()) {+            val centerX = size.width / 2+            val centerY = size.height / 2+            val halfVertFOV = 40f++            guideDots.forEach { dot ->+                val angleOffset = dot.targetAngle - currentAngle+                val pitchOffset = dot.targetPitch - currentPitch+                if (Math.abs(angleOffset) <= halfFOV * 1.2f && Math.abs(pitchOffset) <= halfVertFOV * 1.2f) {+                    val dotX = centerX + (angleOffset / halfFOV) * (size.width / 2)+                    val dotY = centerY - (pitchOffset / halfVertFOV) * (size.height / 2)+                    val baseRadius = 14.dp.toPx()++                    when (dot.state) {+                        GuideDotState.PENDING -> {+                            drawCircle(Color.White.copy(alpha = 0.7f), baseRadius, Offset(dotX, dotY), style = Stroke(2.dp.toPx()))+                            drawCircle(Color.White.copy(alpha = 0.3f), 3.dp.toPx(), Offset(dotX, dotY))+                        }+                        GuideDotState.ALIGNING, GuideDotState.CAPTURING -> {+                            drawCircle(Color(0xFF4FC3F7), baseRadius * 1.2f, Offset(dotX, dotY))+                            drawCircle(Color.White, baseRadius * 1.2f, Offset(dotX, dotY), style = Stroke(2.dp.toPx()))+                        }+                        GuideDotState.CAPTURED -> {+                            drawCircle(Color(0xFF4CAF50), baseRadius, Offset(dotX, dotY))+                            val checkPath = Path().apply {+                                moveTo(dotX - baseRadius * 0.35f, dotY)+                                lineTo(dotX - baseRadius * 0.05f, dotY + baseRadius * 0.3f)+                                lineTo(dotX + baseRadius * 0.4f, dotY - baseRadius * 0.3f)+                            }+                            drawPath(checkPath, Color.White, style = Stroke(2.5.dp.toPx(), cap = StrokeCap.Round))+                        }+                    }+                }+            }++            val crossSize = 20.dp.toPx()+            val crossGap = 6.dp.toPx()+            val crossStroke = 2.dp.toPx()+            val crossColor = Color.White+            drawLine(crossColor, Offset(centerX - crossSize, centerY), Offset(centerX - crossGap, centerY), crossStroke)+            drawLine(crossColor, Offset(centerX + crossGap, centerY), Offset(centerX + crossSize, centerY), crossStroke)+            drawLine(crossColor, Offset(centerX, centerY - crossSize), Offset(centerX, centerY - crossGap), crossStroke)+            drawLine(crossColor, Offset(centerX, centerY + crossGap), Offset(centerX, centerY + crossSize), crossStroke)+            drawCircle(crossColor, 2.dp.toPx(), Offset(centerX, centerY))+        }++        Text(+            "$capturedCount / $totalDots",+            color = Color.White,+            fontSize = 16.sp,+            fontWeight = FontWeight.Medium,+            modifier = Modifier.align(Alignment.TopCenter).padding(top = 60.dp)+                .background(Color.Black.copy(alpha = 0.4f), RoundedCornerShape(16.dp))+                .padding(horizontal = 16.dp, vertical = 6.dp)+        )+    }+}
camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.ktnew file mode 100644index 0000000000..f082ac4880--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt@@ -0,0 +1,269 @@+package com.vayunmathur.camera.ui++import android.graphics.BitmapShader+import android.graphics.Matrix+import android.graphics.ColorMatrixColorFilter+import android.graphics.RenderEffect+import android.graphics.RuntimeShader+import android.graphics.Shader+import android.os.Build+import androidx.camera.core.DynamicRange+import androidx.camera.core.FocusMeteringAction+import androidx.camera.core.SurfaceOrientedMeteringPointFactory+import androidx.camera.compose.CameraXViewfinder+import androidx.camera.viewfinder.compose.MutableCoordinateTransformer+import androidx.camera.viewfinder.core.ImplementationMode+import androidx.compose.foundation.background+import androidx.compose.foundation.gestures.detectTapGestures+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.BoxWithConstraints+import androidx.compose.foundation.layout.BoxWithConstraintsScope+import androidx.compose.foundation.layout.aspectRatio+import androidx.compose.foundation.layout.fillMaxHeight+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.runtime.Composable+import androidx.compose.runtime.DisposableEffect+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.remember+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.draw.clipToBounds+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.asComposeRenderEffect+import androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.layout.ContentScale+import androidx.compose.ui.unit.dp+import android.util.Log+import com.vayunmathur.camera.util.BOKEH_SHADER+import com.vayunmathur.camera.util.CameraMode+import com.vayunmathur.camera.util.CameraViewModel+import com.vayunmathur.camera.util.bokehBlurScale+import com.vayunmathur.camera.util.buildColorAdjustmentMatrix+import com.vayunmathur.camera.util.lockFocusAndMetering+import com.vayunmathur.camera.util.onViewfinderZoomRatio+import com.vayunmathur.camera.util.setBlurStrength+import com.vayunmathur.camera.util.setExposureCompensation+import com.vayunmathur.camera.util.setExposureTimeIndex+import com.vayunmathur.camera.util.setManualIsoIndex+import com.vayunmathur.camera.util.setShadows+import com.vayunmathur.camera.util.setWarmth+import com.vayunmathur.camera.util.setZoomRatio+import com.vayunmathur.camera.util.toggleNightModeOverride++/**+ * The letterboxed viewfinder plus its gesture handling, bokeh/color RenderEffects,+ * grid/level/night overlays, settings column and panorama overlay. Extracted verbatim+ * from [CameraScreen]; behavior identical.+ */+@Composable+internal fun BoxWithConstraintsScope.CameraPreviewBox(+    state: CameraScreenState,+    viewModel: CameraViewModel,+    animatedRotation: Float,+) {+    val cameraMode = state.cameraMode+    val surfaceRequest = state.surfaceRequest+    val maskBitmap = state.maskBitmap++    // Fit the preview inside the available area (letterboxed) so a tall ratio never+    // overflows onto the top/bottom bars and steals their touch events.+    val boxAspect = if (maxHeight.value > 0f) maxWidth.value / maxHeight.value else 1f+    val previewSize = if (state.previewAspectRatio > boxAspect) {+        Modifier.fillMaxWidth().aspectRatio(state.previewAspectRatio)+    } else {+        Modifier.fillMaxHeight().aspectRatio(state.previewAspectRatio)+    }+    // RuntimeShader and the runtime-shader RenderEffect are API 33+. On older+    // releases the portrait preview renders without the background blur.+    val bokehShader = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {+        remember { lazy { RuntimeShader(BOKEH_SHADER) } }+    } else {+        null+    }+    val coordinateTransformer = remember { MutableCoordinateTransformer() }+    val previewModifier = previewSize+        // CameraX mirrors the front-camera preview by default; counter it when the+        // user turns selfie mirroring off so the preview matches the saved image (#632).+        .graphicsLayer { scaleX = if (state.mirrorPreview) -1f else 1f }+        .clip(RoundedCornerShape(12.dp))+        .then(+            run {+                val hasColorAdj = state.warmth != 0f || state.shadows != 0f+                // Snapshot mask bitmap at this composition point – don't !! inside graphicsLayer+                // or a race with DisposableEffect onDispose (null + recycle) causes NPE at 567.+                val currentMask = maskBitmap?.takeIf {+                    !it.isRecycled && it.width > 0 && it.height > 0+                }+                val hasBokeh = cameraMode == CameraMode.PORTRAIT && currentMask != null+                if (hasBokeh || hasColorAdj) {+                    Modifier.graphicsLayer {+                        var effect: RenderEffect? = null++                        if (hasBokeh &&+                            Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU+                        ) {+                            val localMask = currentMask+                            val shader = bokehShader?.value+                            if (shader != null && localMask != null &&+                                !localMask.isRecycled &&+                                size.width > 0 && size.height > 0+                            ) {+                                try {+                                    val maskShader = BitmapShader(localMask, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)+                                    val matrix = Matrix()+                                    matrix.setScale(size.width / localMask.width, size.height / localMask.height)+                                    maskShader.setLocalMatrix(matrix)+                                    shader.setInputShader("alphaMask", maskShader)+                                    shader.setFloatUniform("blurScale", bokehBlurScale(state.blurStrength))+                                    effect = RenderEffect.createRuntimeShaderEffect(shader, "cameraInput")+                                } catch (_: Exception) {+                                    // Mask may be recycled between snapshot and render – skip bokeh this frame+                                }+                            }+                        }++                        if (hasColorAdj) {+                            val colorEffect = RenderEffect.createColorFilterEffect(+                                ColorMatrixColorFilter(+                                    buildColorAdjustmentMatrix(state.warmth, state.shadows)+                                )+                            )+                            effect = if (effect != null) {+                                RenderEffect.createChainEffect(colorEffect, effect)+                            } else {+                                colorEffect+                            }+                        }++                        renderEffect = effect?.asComposeRenderEffect()+                    }+                } else {+                    Modifier.graphicsLayer { renderEffect = null }+                }+            }+        )+            // Tap-to-focus and pinch-to-zoom are handled by CameraXViewfinder's+            // built-in gestures (below). Long-press to lock AE/AF isn't built in, so+            // keep just that here.+            .pointerInput(state.activeSetting) {+                fun meteringPoint(tapOffset: Offset) = surfaceRequest?.let { request ->+                    val transformed = with(coordinateTransformer) { tapOffset.transform() }+                    val factory = SurfaceOrientedMeteringPointFactory(+                        request.resolution.width.toFloat(),+                        request.resolution.height.toFloat()+                    )+                    factory.createPoint(transformed.x, transformed.y)+                }+                detectTapGestures(+                    onLongPress = { tapOffset ->+                        val point = meteringPoint(tapOffset) ?: return@detectTapGestures+                        viewModel.lockFocusAndMetering(+                            FocusMeteringAction.Builder(point).disableAutoCancel().build()+                        )+                    }+                )+            }++    surfaceRequest?.let { request ->+        LaunchedEffect(request) {+            Log.d("NightPreview", "CameraXViewfinder COMPOSED with request res=${request.resolution} dynamicRange=${request.dynamicRange} useNightPreview=${state.useNightPreview} sessionKind=${state.sessionKind} zoomRatio=${state.zoomRatio} levels=${state.availableZoomLevels} thread=${Thread.currentThread().name}")+        }+        DisposableEffect(request) {+            Log.d("NightPreview", "CameraXViewfinder DisposableEffect ATTACH request res=${request.resolution} useNightPreview=${state.useNightPreview}")+            onDispose {+                Log.d("NightPreview", "CameraXViewfinder DisposableEffect DETACH request res=${request.resolution} useNightPreview=${state.useNightPreview} – surface will be invalidated, if next request fails to emit we go black")+            }+        }+        CameraXViewfinder(+            surfaceRequest = request,+            modifier = previewModifier,+            // HDR (HLG 10-bit) video previews must render through a SurfaceView+            // (EXTERNAL): the EMBEDDED/TextureView path doesn't tone-map the BT.2020+            // HLG buffer, which shows up as a reddish/pink cast in video mode. SDR+            // (photo) stays EMBEDDED so graphicsLayer RenderEffects (warmth, bokeh)+            // still composite onto the preview.+            implementationMode = if (+                request.dynamicRange.bitDepth != DynamicRange.BIT_DEPTH_8_BIT+            ) {+                ImplementationMode.EXTERNAL+            } else {+                ImplementationMode.EMBEDDED+            },+            coordinateTransformer = coordinateTransformer,+            alignment = Alignment.Center,+            // Crop (fill) so the camera feed fills the ratio-shaped box for the+            // selected aspect ratio (1:1 / 16:9 / 4:3). Fit letterboxed the native+            // 4:3 frame inside the box, making the preview shrink instead of reshape.+            contentScale = ContentScale.Crop,+            // Built-in tap-to-focus and pinch-to-zoom (1.7.0-alpha02). The viewfinder+            // applies focus/zoom on the SurfaceRequest's camera; we just sync the+            // displayed zoom ratio so the zoom bar stays in step.+            isTapToFocusEnabled = true,+            isPinchToZoomEnabled = true,+            onZoomRatioChanged = { ratio -> viewModel.onViewfinderZoomRatio(ratio) },+        )+    }+    if (surfaceRequest == null) {+        // Brief gap between a mode's teardown and the next session's first frame.+        // Show a plain black fill (no debug text) so the switch reads as instant.+        Box(modifier = previewModifier.background(Color.Black))+    }++    CameraPreviewOverlays(+        gridEnabled = state.gridEnabled,+        levelEnabled = state.levelEnabled,+        isPhotoType = state.isPhotoType,+        roll = state.roll,+        previewSize = previewSize,+        timerCountdown = state.timerCountdown,+        longExposureProgress = state.longExposureProgress,+        longExposureRemaining = state.longExposureRemaining,+        lowLightDetected = state.lowLightDetected,+        nightExtAvailable = state.nightExtAvailable,+        nightModeActive = state.nightModeActive,+        onToggleNightOverride = { viewModel.toggleNightModeOverride() },+        iconRotation = animatedRotation,+        extensionStrength = state.extensionStrength,+        photoSessionActive = state.photoSessionActive,+        analysisStreamActive = state.analysisStreamActive,+    )++    CameraSettingsColumn(+        activeSetting = state.activeSetting,+        zoomRatio = state.zoomRatio,+        availableZoomLevels = state.availableZoomLevels,+        onZoomSelected = { viewModel.setZoomRatio(it) },+        exposureComp = state.exposureComp,+        onExposureComp = { viewModel.setExposureCompensation(it) },+        shadows = state.shadows,+        onShadows = { viewModel.setShadows(it) },+        warmth = state.warmth,+        onWarmth = { viewModel.setWarmth(it) },+        exposureTimeIndex = state.exposureTimeIndex,+        onExposureTimeIndex = { viewModel.setExposureTimeIndex(it) },+        blurStrength = state.blurStrength,+        onBlurStrength = { viewModel.setBlurStrength(it) },+        manualIsoIndex = state.manualIsoIndex,+        isoStops = state.isoStops,+        onManualIsoIndex = { viewModel.setManualIsoIndex(it) },+        cameraMode = cameraMode,+        onSelectSetting = { state.activeSetting = it },+        modifier = Modifier.align(Alignment.BottomCenter),+    )++    if ((cameraMode == CameraMode.PANORAMA || cameraMode == CameraMode.PHOTOSPHERE) && (state.panoSweeping || state.panoStitching)) {+        PanoramaOverlay(+            isSweeping = state.panoSweeping,+            isStitching = state.panoStitching,+            guideDots = state.panoDots,+            currentAngle = state.panoCurrentAngle,+            sweepDirection = state.panoDirection,+            currentPitch = state.panoPitch,+            modifier = previewSize+        )+    }+}
camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewOverlays.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewOverlays.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewOverlays.ktnew file mode 100644index 0000000000..5ce468c69e--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewOverlays.kt@@ -0,0 +1,107 @@+package com.vayunmathur.camera.ui++import androidx.compose.foundation.background+import androidx.compose.foundation.layout.BoxScope+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.sp+import com.vayunmathur.camera.R+import com.vayunmathur.library.ui.Text++@Composable+internal fun BoxScope.CameraPreviewOverlays(+    gridEnabled: Boolean,+    levelEnabled: Boolean,+    isPhotoType: Boolean,+    roll: Float,+    previewSize: Modifier,+    timerCountdown: Int,+    longExposureProgress: Float,+    longExposureRemaining: String,+    lowLightDetected: Boolean,+    nightExtAvailable: Boolean,+    nightModeActive: Boolean,+    onToggleNightOverride: () -> Unit,+    iconRotation: Float,+    extensionStrength: Int?,+    photoSessionActive: Boolean,+    analysisStreamActive: Boolean,+) {+    if (gridEnabled) {+        GridOverlay(+            modifier = previewSize+        )+    }++    if (levelEnabled && isPhotoType) {+        LevelOverlay(+            roll = roll,+            modifier = previewSize+        )+    }++    if (timerCountdown > 0) {+        Text(+            text = "$timerCountdown",+            fontSize = 96.sp,+            color = Color.White,+            textAlign = TextAlign.Center+        )+    }++    if (longExposureProgress > 0f) {+        LongExposureOverlay(+            progress = longExposureProgress,+            remainingText = longExposureRemaining+        )+    }++    // Auto night: shown when a dark scene is detected (getNightModeIndicator /+    // luminance) and the extension is usable; tap toggles night off for this scene.+    if (lowLightDetected && nightExtAvailable) {+        NightModeButton(+            active = nightModeActive,+            onClick = onToggleNightOverride,+            iconRotation = iconRotation,+            modifier = Modifier.align(Alignment.TopCenter).padding(top = 12.dp)+        )+    }++    // Live vendor night-processing strength while the NIGHT extension preview is bound.+    extensionStrength?.let { strength ->+        Text(+            text = stringResource(R.string.night_2, strength),+            color = Color.White,+            fontSize = 12.sp,+            modifier = Modifier+                .align(Alignment.TopCenter)+                .padding(top = 56.dp)+                .background(Color(0x66000000), RoundedCornerShape(12.dp))+                .padding(horizontal = 10.dp, vertical = 4.dp)+        )+    }++    // Some vendor NIGHT extensions can't host a concurrent ImageAnalysis stream, so+    // the session binds without one and QR codes stop being read. Say so rather than+    // letting the scanner look broken.+    if (isPhotoType && photoSessionActive && !analysisStreamActive) {+        Text(+            text = stringResource(R.string.qr_scanning_paused),+            color = Color.White,+            fontSize = 12.sp,+            modifier = Modifier+                .align(Alignment.TopCenter)+                .padding(top = 84.dp)+                .background(Color(0x66000000), RoundedCornerShape(12.dp))+                .padding(horizontal = 10.dp, vertical = 4.dp)+        )+    }+}
camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.ktindex 590420d812..a0798b16d9 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt@@ -3,488 +3,67 @@ // animate towards; the shutter and record-button springs are tuned to this viewfinder's own feel. package com.vayunmathur.camera.ui -import android.content.ClipData-import android.content.ClipboardManager-import android.content.Context-import android.content.ActivityNotFoundException-import android.content.Intent-import android.graphics.BitmapShader-import android.graphics.Matrix-import android.graphics.ColorMatrixColorFilter-import android.graphics.RenderEffect-import android.graphics.RuntimeShader-import android.graphics.Shader-import android.os.Build-import android.provider.MediaStore-import android.util.Patterns-import android.util.Log-import android.view.OrientationEventListener-import androidx.camera.compose.CameraXViewfinder-import androidx.camera.core.CameraInfo-import androidx.camera.core.FocusMeteringAction-import androidx.camera.core.CameraSelector-import androidx.camera.core.ImageAnalysis-import androidx.camera.core.SurfaceOrientedMeteringPointFactory-import androidx.camera.core.DynamicRange-import androidx.camera.viewfinder.compose.MutableCoordinateTransformer-import androidx.camera.viewfinder.core.ImplementationMode-import androidx.compose.animation.core.Animatable-import androidx.compose.animation.core.spring-import androidx.compose.animation.core.tween-import androidx.compose.foundation.Canvas-import androidx.compose.foundation.Image-import androidx.compose.foundation.background-import androidx.compose.foundation.border-import androidx.compose.foundation.clickable-import androidx.compose.foundation.combinedClickable-import androidx.compose.foundation.ExperimentalFoundationApi-import androidx.compose.foundation.gestures.detectTapGestures-import androidx.compose.foundation.horizontalScroll-import androidx.compose.foundation.layout.Arrangement-import androidx.compose.foundation.layout.Box+import android.graphics.Bitmap import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.Row-import androidx.compose.foundation.layout.Spacer-import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize-import com.vayunmathur.library.ui.ExternalIntents-import com.vayunmathur.library.util.AppMessages-import com.vayunmathur.library.ui.CircularProgressIndicator import androidx.compose.foundation.layout.fillMaxWidth-import androidx.compose.foundation.layout.fillMaxHeight-import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding-import androidx.compose.foundation.layout.size-import androidx.compose.foundation.layout.width-import androidx.compose.foundation.layout.widthIn-import androidx.compose.foundation.shape.CircleShape-import androidx.compose.foundation.shape.RoundedCornerShape-import com.vayunmathur.library.ui.Button-import com.vayunmathur.library.ui.FilledTonalButton-import com.vayunmathur.library.ui.IconBedtime-import com.vayunmathur.library.ui.IconBlur-import com.vayunmathur.library.ui.IconCamera-import com.vayunmathur.library.ui.IconContrast-import com.vayunmathur.library.ui.IconFlashAuto-import com.vayunmathur.library.ui.IconFlashOff-import com.vayunmathur.library.ui.IconFlashOn-import com.vayunmathur.library.ui.IconFlashlight-import com.vayunmathur.library.ui.IconFlipCamera-import com.vayunmathur.library.ui.IconGrid-import com.vayunmathur.library.ui.IconIso-import com.vayunmathur.library.ui.IconLightbulb-import com.vayunmathur.library.ui.IconMic-import com.vayunmathur.library.ui.IconMicOff-import com.vayunmathur.library.ui.IconPause-import com.vayunmathur.library.ui.IconPhotoLibrary-import com.vayunmathur.library.ui.IconPlay-import com.vayunmathur.library.ui.IconSunny-import com.vayunmathur.library.ui.IconTimer-import com.vayunmathur.library.ui.IconToolsLevel-import com.vayunmathur.library.ui.IconVideoCamera-import com.vayunmathur.library.ui.IconSettings-import com.vayunmathur.library.ui.isExpandedWidth-import com.vayunmathur.library.ui.IconButton-import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.Motion-import com.vayunmathur.library.ui.Scaffold-import com.vayunmathur.library.ui.Slider-import com.vayunmathur.library.ui.SliderDefaults-import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.TextButton-import com.vayunmathur.library.ui.animatedFloat import androidx.compose.runtime.Composable-import androidx.compose.runtime.DisposableEffect-import androidx.compose.runtime.LaunchedEffect-import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableFloatStateOf-import androidx.compose.runtime.mutableIntStateOf-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState-import androidx.compose.runtime.rememberCoroutineScope-import androidx.compose.foundation.rememberScrollState-import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier-import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clipToBounds-import androidx.compose.ui.draw.rotate-import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color-import androidx.compose.ui.graphics.Path-import androidx.compose.ui.graphics.Shape-import androidx.compose.ui.graphics.StrokeCap-import androidx.compose.ui.graphics.drawscope.Stroke-import androidx.compose.ui.graphics.asComposeRenderEffect-import androidx.compose.ui.graphics.asImageBitmap-import androidx.compose.ui.graphics.graphicsLayer-import androidx.compose.ui.input.pointer.pointerInput-import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalView-import androidx.compose.ui.res.stringResource-import androidx.compose.ui.semantics.contentDescription-import androidx.compose.ui.semantics.semantics-import androidx.compose.ui.text.font.FontWeight-import androidx.compose.ui.text.style.TextAlign-import androidx.compose.ui.unit.dp-import androidx.compose.ui.unit.sp-import androidx.core.net.toUri-import androidx.lifecycle.Lifecycle-import androidx.lifecycle.compose.LocalLifecycleOwner-import androidx.lifecycle.repeatOnLifecycle-import com.vayunmathur.camera.R import com.vayunmathur.camera.Route-import com.vayunmathur.camera.util.AspectRatioOption-import com.vayunmathur.camera.util.BokehAnalyzer import com.vayunmathur.camera.util.CameraMode import com.vayunmathur.camera.util.CameraViewModel-import com.vayunmathur.camera.util.GuideDot-import com.vayunmathur.camera.util.GuideDotState import com.vayunmathur.camera.util.FlashMode-import com.vayunmathur.camera.util.BOKEH_SHADER-import com.vayunmathur.camera.util.bokehBlurScale-import com.vayunmathur.camera.util.buildColorAdjustmentMatrix-import com.vayunmathur.camera.util.formatZoomLabel-import com.vayunmathur.camera.util.PhotoAnalyzer import com.vayunmathur.camera.util.TimerDuration+import com.vayunmathur.camera.util.capturePhotoForResult+import com.vayunmathur.camera.util.setFlashMode+import com.vayunmathur.camera.util.setQrResult+import com.vayunmathur.camera.util.setTimerDuration+import com.vayunmathur.camera.util.startPanorama+import com.vayunmathur.camera.util.startPhotosphere+import com.vayunmathur.camera.util.stopPanorama+import com.vayunmathur.camera.util.stopPhotosphere+import com.vayunmathur.camera.util.takePhoto+import com.vayunmathur.camera.util.toggleGrid+import com.vayunmathur.camera.util.toggleHighSpeedRecording+import com.vayunmathur.camera.util.toggleLevel+import com.vayunmathur.camera.util.toggleMicMuted+import com.vayunmathur.camera.util.toggleRecording+import com.vayunmathur.camera.util.toggleTorch+import com.vayunmathur.library.ui.Motion+import com.vayunmathur.library.ui.Scaffold+import com.vayunmathur.library.ui.animatedFloat import com.vayunmathur.library.util.NavBackStack-import kotlinx.coroutines.awaitCancellation-import kotlinx.coroutines.delay-import kotlinx.coroutines.launch-import kotlin.math.roundToInt---private enum class CameraSetting {-    BRIGHTNESS, SHADOWS, WARMTH, EXPOSURE_TIME, PORTRAIT_BLUR, ISO-}--/** Which camera pipeline drives the preview for the current mode. */-private enum class SessionKind { PHOTO, PORTRAIT, PANORAMA, HIGH_SPEED, VIDEO }--private fun Modifier.selectedPill(-    selected: Boolean,-    shape: Shape,-    color: Color = Color(0xFF3C3C3C)-): Modifier = if (selected) background(color, shape) else this  @Composable fun CameraScreen(     backStack: NavBackStack<Route>,     viewModel: CameraViewModel,-    onCaptureResult: ((android.graphics.Bitmap?) -> Unit)? = null+    onCaptureResult: ((Bitmap?) -> Unit)? = null ) {-    val context = LocalContext.current     val view = LocalView.current--    val cameraMode by viewModel.cameraMode.collectAsState()-    val lensFacing by viewModel.lensFacing.collectAsState()-    val flashMode by viewModel.flashMode.collectAsState()-    val torchEnabled by viewModel.torchEnabled.collectAsState()-    val isRecording by viewModel.isRecording.collectAsState()-    val recordingDuration by viewModel.recordingDurationSec.collectAsState()-    val timerCountdown by viewModel.timerCountdown.collectAsState()-    val qrResult by viewModel.qrResult.collectAsState()-    val aspectRatio by viewModel.aspectRatio.collectAsState()-    val zoomRatio by viewModel.zoomRatio.collectAsState()-    val mirrorFront by viewModel.mirrorFront.collectAsState()-    val timerDuration by viewModel.timerDuration.collectAsState()-    val isCapturing by viewModel.isCapturing.collectAsState()-    val burstActive by viewModel.burstActive.collectAsState()-    val burstCount by viewModel.burstCount.collectAsState()-    val focusLocked by viewModel.focusLocked.collectAsState()-    val recordingPaused by viewModel.recordingPaused.collectAsState()-    val micMuted by viewModel.micMuted.collectAsState()-    val videoSnapshotSupported by viewModel.videoSnapshotSupported.collectAsState()-    val lastCaptureUri by viewModel.lastCaptureUri.collectAsState()-    val gridEnabled by viewModel.gridEnabled.collectAsState()-    val levelEnabled by viewModel.levelEnabled.collectAsState()-    val roll by viewModel.roll.collectAsState()-    val blurStrength by viewModel.blurStrength.collectAsState()-    val exposureComp by viewModel.exposureCompensation.collectAsState()-    val warmth by viewModel.warmth.collectAsState()-    val shadows by viewModel.shadows.collectAsState()-    val exposureTimeIndex by viewModel.exposureTimeIndex.collectAsState()-    val manualIsoIndex by viewModel.manualIsoIndex.collectAsState()-    val isoStops by viewModel.isoStops.collectAsState()-    val longExposureProgress by viewModel.longExposureProgress.collectAsState()-    val longExposureRemaining by viewModel.longExposureRemaining.collectAsState()-    val lowLightDetected by viewModel.lowLightDetected.collectAsState()-    val nightModeActive by viewModel.nightModeActive.collectAsState()-    val extensionStrength by viewModel.extensionStrength.collectAsState()--    var activeSetting by remember { mutableStateOf<CameraSetting?>(null) }-    var maskBitmap by remember { mutableStateOf<android.graphics.Bitmap?>(null) }-    val sloMoSupported by viewModel.sloMoSupported.collectAsState()-    val isPhotoType = cameraMode in listOf(CameraMode.PHOTO, CameraMode.PORTRAIT, CameraMode.PANORAMA, CameraMode.PHOTOSPHERE)-    val isSloMo = cameraMode == CameraMode.SLOW_MO-    val isVideoType = cameraMode == CameraMode.VIDEO || cameraMode == CameraMode.TIMELAPSE || cameraMode == CameraMode.CINEMATIC-    val isPanoType = cameraMode == CameraMode.PANORAMA || cameraMode == CameraMode.PHOTOSPHERE-    val isPortrait = cameraMode == CameraMode.PORTRAIT-    val sessionKind = when {-        isSloMo -> SessionKind.HIGH_SPEED-        isVideoType -> SessionKind.VIDEO-        isPanoType -> SessionKind.PANORAMA-        isPortrait -> SessionKind.PORTRAIT-        else -> SessionKind.PHOTO-    }-    val highSpeedActive by viewModel.highSpeedActive.collectAsState()-    val photoSessionActive by viewModel.photoSessionActive.collectAsState()-    val analysisStreamActive by viewModel.analysisStreamActive.collectAsState()-    // Whether to offer the NIGHT extension. Reactive: the ViewModel recomputes it on lens/mode-    // change (off-main; support probe + weekly failure cache) AND flips it false immediately if a-    // night bind fails, so a broken extender (GrapheneOS/Pixel) stops engaging night after one try.-    val nightExtAvailable by viewModel.nightExtensionUsable.collectAsState()-    LaunchedEffect(lensFacing, cameraMode) {-        kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) {-            viewModel.refreshNightExtensionUsable(cameraMode)-        }-    }-    // Auto night: when getNightModeIndicator()/luminance reports a dark scene (nightModeActive) in-    // PHOTO mode and the extension is usable, bind the vendor NIGHT extension preview.-    val useNightPreview = sessionKind == SessionKind.PHOTO && cameraMode == CameraMode.PHOTO &&-        nightModeActive && nightExtAvailable-    LaunchedEffect(useNightPreview) {-        Log.d("NightPreview", "CameraScreen useNightPreview recomputed=$useNightPreview sessionKind=$sessionKind cameraMode=$cameraMode nightModeActive=$nightModeActive nightExtAvailable=$nightExtAvailable lensFacing=$lensFacing")-    }-    val surfaceRequest by viewModel.surfaceRequest.collectAsState()-    LaunchedEffect(surfaceRequest) {-        Log.d("NightPreview", "CameraScreen surfaceRequest changed res=${surfaceRequest?.resolution} frameType=${surfaceRequest?.javaClass?.simpleName} useNightPreview=$useNightPreview sessionKind=$sessionKind thread=${Thread.currentThread().name} lensFacing=${lensFacing}")-        if (surfaceRequest == null && useNightPreview) {-            Log.w("NightPreview", "CameraScreen WARNING: surfaceRequest is NULL while useNightPreview=true – preview will be black until Provider re-emits. This is the black-frame trap you described: teardown -> delay(250) -> setupNightPreviewSession() tears down standard photo session surface before extension emits!")-        }-        if (surfaceRequest == null && !useNightPreview) {-            Log.w("NightPreview", "CameraScreen tracing maybe delayed rebind? surface null while normal photo – expected during 250ms delay window")-        }-    }-    val coordinateTransformer = remember { MutableCoordinateTransformer() }-    val availableZoomLevels by viewModel.availableZoomLevels.collectAsState()-    LaunchedEffect(availableZoomLevels) {-        Log.d("NightPreview", "CameraScreen availableZoomLevels changed=$availableZoomLevels currentZoom=${zoomRatio} useNightPreview=$useNightPreview – vendor NIGHT often reports only [1x], making zoom bar appear to 'disappear' except 1x")-    }-    // RuntimeShader and the runtime-shader RenderEffect are API 33+. On older-    // releases the portrait preview renders without the background blur.-    val bokehShader = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {-        remember { lazy { RuntimeShader(BOKEH_SHADER) } }-    } else {-        null-    }--    val panoSweeping by viewModel.panoramaEngine.isSweeping.collectAsState()-    val panoStitching by viewModel.panoramaEngine.isStitching.collectAsState()-    val panoDots by viewModel.panoramaEngine.guideDots.collectAsState()-    val panoCurrentAngle by viewModel.panoramaEngine.currentAngle.collectAsState()-    val panoDirection by viewModel.panoramaEngine.sweepDirection.collectAsState()-    val panoPitch by viewModel.panoramaEngine.currentPitch.collectAsState()--    // Orientation tracking-    var deviceRotation by remember { mutableIntStateOf(0) }-    val animatedRotation = animatedFloat(-        target = deviceRotation.toFloat(),-        spec = Motion.over(300)-    )--    // Tracks physical orientation purely to rotate the on-screen control icons. Capture output-    // rotation is handled by CameraX via SessionConfig.setAutoRotationEnabled(true).-    DisposableEffect(Unit) {-        val listener = object : OrientationEventListener(context) {-            override fun onOrientationChanged(orientation: Int) {-                if (orientation == ORIENTATION_UNKNOWN) return-                deviceRotation = when {-                    orientation in 45..134 -> 270-                    orientation in 135..224 -> 180-                    orientation in 225..314 -> 90-                    else -> 0-                }-            }-        }-        listener.enable()-        onDispose { listener.disable() }-    }--    // Gallery thumbnail (loaded off the main thread in the ViewModel)-    val galleryBitmap by viewModel.galleryThumbnail.collectAsState()--    LaunchedEffect(Unit) {-        viewModel.updateLocation()-    }--    LaunchedEffect(flashMode) {-        viewModel.applyImageCaptureFlashMode()-    }--    LaunchedEffect(torchEnabled) {-        viewModel.enableTorch(torchEnabled)-    }--    LaunchedEffect(exposureComp) {-        viewModel.applyExposureCompensation(exposureComp)-    }--    // Close mode-specific settings when leaving the mode that offers them.-    LaunchedEffect(cameraMode) {-        val portraitOnly = activeSetting == CameraSetting.PORTRAIT_BLUR && cameraMode != CameraMode.PORTRAIT-        val photoOnly = activeSetting == CameraSetting.ISO && cameraMode != CameraMode.PHOTO-        if (portraitOnly || photoOnly) activeSetting = null-    }--    // Analyzer selection for the photo modes. Keyed on photoSessionActive so the analyzer is-    // re-applied to the freshly-bound ImageAnalysis after every (re)bind (mode switch / flip), and-    // on lensFacing to match the DisposableEffect below, which detaches on a flip.-    LaunchedEffect(cameraMode, photoSessionActive, lensFacing) {-        when {-            cameraMode == CameraMode.SLOW_MO -> {-                maskBitmap = null-                viewModel.setImageAnalyzer(null)-                viewModel.setQrResult(null)-            }-            isPhotoType -> {-                when (cameraMode) {-                    CameraMode.PORTRAIT -> {-                        // Analyzer lifecycle (create/close) managed by DisposableEffect below.-                    }-                    CameraMode.PANORAMA, CameraMode.PHOTOSPHERE -> {-                        maskBitmap = null-                        viewModel.setImageAnalyzer(-                            ImageAnalysis.Analyzer { imageProxy ->-                                viewModel.panoramaEngine.latestFrame = imageProxy.toBitmap()-                                imageProxy.close()-                            }-                        )-                    }-                    else -> {-                        maskBitmap = null-                        viewModel.setPhotoAnalyzer(-                            PhotoAnalyzer(-                                onLuminance = { viewModel.onLuminance(it) },-                                onQrDetected = { viewModel.setQrResult(it) },-                                // Motion-Photo ring buffer only in plain PHOTO mode.-                                onMotionFrame = if (cameraMode == CameraMode.PHOTO) {-                                    { bmp, ts, rot -> viewModel.addMotionFrame(bmp, ts, rot) }-                                } else null-                            )-                        )-                    }-                }-            }-            else -> {-                maskBitmap = null-                viewModel.setQrResult(null)-            }-        }-    }--    // Owns the PORTRAIT bokeh segmenter so it is closed (and its mask recycled) on mode change.-    // Keyed on photoSessionActive (re-attach after a rebind) and lensFacing (front/back changes-    // the frame orientation/mirroring the analyzer must apply).-    // Runs on dedicated bokeh executor so main thread stays free for preview rendering.-    // Final capture ImageCapture is still max-res – only analysis is capped.-    // Mask callback comes from bokeh thread; post recycle+set to main to avoid racing with-    // the RenderEffect reading the previous Bitmap on the UI thread.-    val mainHandler = remember { android.os.Handler(android.os.Looper.getMainLooper()) }-    DisposableEffect(cameraMode, photoSessionActive, lensFacing) {-        val analyzer = if (cameraMode == CameraMode.PORTRAIT) {-            BokehAnalyzer(-                context,-                isFrontFacing = lensFacing == CameraSelector.LENS_FACING_FRONT-            ) { mask ->-                if (mask.isRecycled) return@BokehAnalyzer-                mainHandler.post {-                    // If mask was recycled while message queued, drop it-                    if (mask.isRecycled) return@post-                    val prev = maskBitmap-                    if (prev != null && prev !== mask && !prev.isRecycled) {-                        try { prev.recycle() } catch (_: Exception) {}-                    }-                    maskBitmap = mask-                }-            }.also {-                viewModel.setBokehAnalyzer(it)-            }-        } else null-        onDispose {-            // Only detach what this effect attached. It also keys on lensFacing, which the-            // analyzer-selection effect above does not, so an unconditional clear stripped-            // PhotoAnalyzer off the photo stream on every camera flip.-            if (analyzer != null) {-                viewModel.setImageAnalyzer(null)-                analyzer.close()-            }-            // Post recycle to avoid racing with graphicsLayer reading maskBitmap-            mainHandler.post {-                maskBitmap?.let { bmp ->-                    if (!bmp.isRecycled) {-                        try { bmp.recycle() } catch (_: Exception) {}-                    }-                }-                maskBitmap = null-            }-        }-    }--    // Unified session binding, made lifecycle-aware so the camera is released when the app is-    // backgrounded and rebound on resume. Without this the ManualLifecycleOwner stays RESUMED,-    // the OS reclaims the camera while we're away, and the preview comes back frozen.-    val lifecycleOwner = LocalLifecycleOwner.current-    LaunchedEffect(lensFacing, sessionKind, useNightPreview, lifecycleOwner) {-        Log.d("NightPreview", "CameraScreen session LaunchedEffect START keys lensFacing=$lensFacing sessionKind=$sessionKind useNightPreview=$useNightPreview lifecycle=${lifecycleOwner.lifecycle.currentState} thread=${Thread.currentThread().name}")-        lifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {-            Log.d("NightPreview", "CameraScreen repeatOnLifecycle STARTED – calling teardownSession()")-            viewModel.teardownSession()-            // Brief yield so the previous session's surface detaches before the next binds; short-            // enough that a mode switch reads as instant, long enough to avoid a bind-over-teardown race.-            delay(40)-            val bindStart = System.currentTimeMillis()-            val bindSuccess = try {-                when (sessionKind) {-                    SessionKind.HIGH_SPEED -> viewModel.setupHighSpeedSession()-                    SessionKind.VIDEO -> viewModel.setupVideoSession()-                    SessionKind.PANORAMA -> viewModel.setupPanoramaSession()-                    SessionKind.PORTRAIT -> viewModel.setupPortraitSession()-                    SessionKind.PHOTO ->-                        if (useNightPreview) viewModel.setupNightPreviewSession()-                        else viewModel.setupPhotoSession()-                }-            } catch (e: Exception) {-                Log.e("NightPreview", "CameraScreen session binding THREW (was not logged before) kind=$sessionKind useNightPreview=$useNightPreview nightExtAvailable=$nightExtAvailable", e)-                false-            }-            Log.d("NightPreview", "CameraScreen session bind finished success=$bindSuccess took=${System.currentTimeMillis() - bindStart}ms surface after=${viewModel.surfaceRequest.value?.resolution} zoomLevels=${viewModel.availableZoomLevels.value} zoomRatio=${viewModel.zoomRatio.value}")-            if (!bindSuccess) {-                Log.e("NightPreview", "CameraScreen BIND FAILED – this produces solid black preview and zoom bar showing only 1x (fallback?). Check logcat for NightPreview tag – exception was previously swallowed as warning")-            }-            try {-                awaitCancellation()-            } finally {-                Log.d("NightPreview", "CameraScreen session coroutine cancelled/finished, calling teardownSession()")-                viewModel.teardownSession()-            }-        }-        Log.d("NightPreview", "CameraScreen repeatOnLifecycle block EXIT – lifecycle dropped below STARTED")-    }---    val previewAspectRatio = when (aspectRatio) {-        AspectRatioOption.RATIO_16_9 -> 9f / 16f-        AspectRatioOption.RATIO_4_3 -> 3f / 4f-        AspectRatioOption.RATIO_1_1 -> 1f-    }+    val state = rememberCameraScreenState(viewModel)      // The shutter action, shared by the on-screen button and the volume-key (hardware) shutter.     val performCapture: () -> Unit = {         view.performHapticFeedback(android.view.HapticFeedbackConstants.CLOCK_TICK)-        if (longExposureProgress <= 0f) when {-            cameraMode == CameraMode.PHOTOSPHERE -> {-                if (panoSweeping) viewModel.stopPhotosphere() else viewModel.startPhotosphere()+        if (state.longExposureProgress <= 0f) when {+            state.cameraMode == CameraMode.PHOTOSPHERE -> {+                if (state.panoSweeping) viewModel.stopPhotosphere() else viewModel.startPhotosphere()             }-            cameraMode == CameraMode.PANORAMA -> {-                if (panoSweeping) viewModel.stopPanorama() else viewModel.startPanorama()+            state.cameraMode == CameraMode.PANORAMA -> {+                if (state.panoSweeping) viewModel.stopPanorama() else viewModel.startPanorama()             }-            isSloMo && highSpeedActive -> viewModel.toggleHighSpeedRecording()-            isPhotoType -> {+            state.isSloMo && state.highSpeedActive -> viewModel.toggleHighSpeedRecording()+            state.isPhotoType -> {                 if (onCaptureResult != null) {                     viewModel.capturePhotoForResult(                         onSaved = { thumbnail -> onCaptureResult(thumbnail) },@@ -498,9 +77,14 @@ fun CameraScreen(         }     }     val currentCapture by rememberUpdatedState(performCapture)-    LaunchedEffect(Unit) {-        viewModel.shutterEvents.collect { currentCapture() }-    }++    CameraScreenEffects(state = state, viewModel = viewModel, onCapture = { currentCapture() })++    // Tracks physical orientation purely to rotate the on-screen control icons.+    val animatedRotation = animatedFloat(+        target = state.deviceRotation.toFloat(),+        spec = Motion.over(300)+    )      // RAW SCAFFOLD EXCEPTION: full-bleed viewfinder with a black surface and its own custom     // top/bottom control bars (TopBar/ShutterRow/ModeSelector/BottomBar). It has no Material@@ -516,25 +100,22 @@ fun CameraScreen(             val qrBottomPadding = maxHeight * 0.3f             val indicatorTopPadding = maxHeight * 0.07f             val focusTopPadding = maxHeight * 0.12f-            // Expanded windows place the shutter and mode controls side by side-            // below the preview; short windows keep the phone stack.-            val expandedSide = isExpandedWidth()             Column(modifier = Modifier.fillMaxSize()) {                 CameraTopBar(-                    flashMode = flashMode,-                    torchEnabled = torchEnabled,-                    gridEnabled = gridEnabled,-                    levelEnabled = levelEnabled,-                    aspectRatio = aspectRatio,-                    isPhotoType = isPhotoType,-                    isVideoType = isVideoType,-                    micMuted = micMuted,-                    timerDuration = timerDuration,+                    flashMode = state.flashMode,+                    torchEnabled = state.torchEnabled,+                    gridEnabled = state.gridEnabled,+                    levelEnabled = state.levelEnabled,+                    aspectRatio = state.aspectRatio,+                    isPhotoType = state.isPhotoType,+                    isVideoType = state.isVideoType,+                    micMuted = state.micMuted,+                    timerDuration = state.timerDuration,                     onFlashToggle = {-                        if (torchEnabled) {+                        if (state.torchEnabled) {                             viewModel.toggleTorch()                         } else {-                            val next = when (flashMode) {+                            val next = when (state.flashMode) {                                 FlashMode.OFF -> FlashMode.ON                                 FlashMode.ON -> FlashMode.AUTO                                 FlashMode.AUTO -> FlashMode.OFF@@ -548,7 +129,7 @@ fun CameraScreen(                     onAspectCycle = { viewModel.cycleAspectRatio() },                     onMicToggle = { viewModel.toggleMicMuted() },                     onTimerCycle = {-                        val next = when (timerDuration) {+                        val next = when (state.timerDuration) {                             TimerDuration.NONE -> TimerDuration.THREE                             TimerDuration.THREE -> TimerDuration.FIVE                             TimerDuration.FIVE -> TimerDuration.TEN@@ -566,890 +147,36 @@ fun CameraScreen(                         .clipToBounds(),                     contentAlignment = Alignment.Center                 ) {-                    // Fit the preview inside the available area (letterboxed) so a tall ratio never-                    // overflows onto the top/bottom bars and steals their touch events.-                    val boxAspect = if (maxHeight.value > 0f) maxWidth.value / maxHeight.value else 1f-                    val previewSize = if (previewAspectRatio > boxAspect) {-                        Modifier.fillMaxWidth().aspectRatio(previewAspectRatio)-                    } else {-                        Modifier.fillMaxHeight().aspectRatio(previewAspectRatio)-                    }-                    val mirrorPreview =-                        lensFacing == CameraSelector.LENS_FACING_FRONT && !mirrorFront-                    val previewModifier = previewSize-                        // CameraX mirrors the front-camera preview by default; counter it when the-                        // user turns selfie mirroring off so the preview matches the saved image (#632).-                        .graphicsLayer { scaleX = if (mirrorPreview) -1f else 1f }-                        .clip(RoundedCornerShape(12.dp))-                        .then(-                            run {-                                val hasColorAdj = warmth != 0f || shadows != 0f-                                // Snapshot mask bitmap at this composition point – don't !! inside graphicsLayer-                                // or a race with DisposableEffect onDispose (null + recycle) causes NPE at 567.-                                val currentMask = maskBitmap?.takeIf {-                                    !it.isRecycled && it.width > 0 && it.height > 0-                                }-                                val hasBokeh = cameraMode == CameraMode.PORTRAIT && currentMask != null-                                if (hasBokeh || hasColorAdj) {-                                    Modifier.graphicsLayer {-                                        var effect: RenderEffect? = null--                                        if (hasBokeh &&-                                            Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU-                                        ) {-                                            val localMask = currentMask-                                            val shader = bokehShader?.value-                                            if (shader != null && localMask != null &&-                                                !localMask.isRecycled &&-                                                size.width > 0 && size.height > 0-                                            ) {-                                                try {-                                                    val maskShader = BitmapShader(localMask, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)-                                                    val matrix = Matrix()-                                                    matrix.setScale(size.width / localMask.width, size.height / localMask.height)-                                                    maskShader.setLocalMatrix(matrix)-                                                    shader.setInputShader("alphaMask", maskShader)-                                                    shader.setFloatUniform("blurScale", bokehBlurScale(blurStrength))-                                                    effect = RenderEffect.createRuntimeShaderEffect(shader, "cameraInput")-                                                } catch (_: Exception) {-                                                    // Mask may be recycled between snapshot and render – skip bokeh this frame-                                                }-                                            }-                                        }--                                        if (hasColorAdj) {-                                            val colorEffect = RenderEffect.createColorFilterEffect(-                                                ColorMatrixColorFilter(-                                                    buildColorAdjustmentMatrix(warmth, shadows)-                                                )-                                            )-                                            effect = if (effect != null) {-                                                RenderEffect.createChainEffect(colorEffect, effect)-                                            } else {-                                                colorEffect-                                            }-                                        }--                                        renderEffect = effect?.asComposeRenderEffect()-                                    }-                                } else {-                                    Modifier.graphicsLayer { renderEffect = null }-                                }-                            }-                        )-                            // Tap-to-focus and pinch-to-zoom are handled by CameraXViewfinder's-                            // built-in gestures (below). Long-press to lock AE/AF isn't built in, so-                            // keep just that here.-                            .pointerInput(activeSetting) {-                                fun meteringPoint(tapOffset: Offset) = surfaceRequest?.let { request ->-                                    val transformed = with(coordinateTransformer) { tapOffset.transform() }-                                    val factory = SurfaceOrientedMeteringPointFactory(-                                        request.resolution.width.toFloat(),-                                        request.resolution.height.toFloat()-                                    )-                                    factory.createPoint(transformed.x, transformed.y)-                                }-                                detectTapGestures(-                                    onLongPress = { tapOffset ->-                                        val point = meteringPoint(tapOffset) ?: return@detectTapGestures-                                        viewModel.lockFocusAndMetering(-                                            FocusMeteringAction.Builder(point).disableAutoCancel().build()-                                        )-                                    }-                                )-                            }--                    surfaceRequest?.let { request ->-                        LaunchedEffect(request) {-                            Log.d("NightPreview", "CameraXViewfinder COMPOSED with request res=${request.resolution} dynamicRange=${request.dynamicRange} useNightPreview=$useNightPreview sessionKind=$sessionKind zoomRatio=${zoomRatio} levels=${availableZoomLevels} thread=${Thread.currentThread().name}")-                        }-                        DisposableEffect(request) {-                            Log.d("NightPreview", "CameraXViewfinder DisposableEffect ATTACH request res=${request.resolution} useNightPreview=$useNightPreview")-                            onDispose {-                                Log.d("NightPreview", "CameraXViewfinder DisposableEffect DETACH request res=${request.resolution} useNightPreview=$useNightPreview – surface will be invalidated, if next request fails to emit we go black")-                            }-                        }-                        CameraXViewfinder(-                            surfaceRequest = request,-                            modifier = previewModifier,-                            // HDR (HLG 10-bit) video previews must render through a SurfaceView-                            // (EXTERNAL): the EMBEDDED/TextureView path doesn't tone-map the BT.2020-                            // HLG buffer, which shows up as a reddish/pink cast in video mode. SDR-                            // (photo) stays EMBEDDED so graphicsLayer RenderEffects (warmth, bokeh)-                            // still composite onto the preview.-                            implementationMode = if (-                                request.dynamicRange.bitDepth != DynamicRange.BIT_DEPTH_8_BIT-                            ) {-                                ImplementationMode.EXTERNAL-                            } else {-                                ImplementationMode.EMBEDDED-                            },-                            coordinateTransformer = coordinateTransformer,-                            alignment = Alignment.Center,-                            // Crop (fill) so the camera feed fills the ratio-shaped box for the-                            // selected aspect ratio (1:1 / 16:9 / 4:3). Fit letterboxed the native-                            // 4:3 frame inside the box, making the preview shrink instead of reshape.-                            contentScale = ContentScale.Crop,-                            // Built-in tap-to-focus and pinch-to-zoom (1.7.0-alpha02). The viewfinder-                            // applies focus/zoom on the SurfaceRequest's camera; we just sync the-                            // displayed zoom ratio so the zoom bar stays in step.-                            isTapToFocusEnabled = true,-                            isPinchToZoomEnabled = true,-                            onZoomRatioChanged = { ratio -> viewModel.onViewfinderZoomRatio(ratio) },-                        )-                    }-                    if (surfaceRequest == null) {-                        // Brief gap between a mode's teardown and the next session's first frame.-                        // Show a plain black fill (no debug text) so the switch reads as instant.-                        Box(modifier = previewModifier.background(Color.Black))-                    }--                    if (gridEnabled) {-                        GridOverlay(-                            modifier = previewSize-                        )-                    }--                    if (levelEnabled && isPhotoType) {-                        LevelOverlay(-                            roll = roll,-                            modifier = previewSize-                        )-                    }--                    if (timerCountdown > 0) {-                        Text(-                            text = "$timerCountdown",-                            fontSize = 96.sp,-                            color = Color.White,-                            textAlign = TextAlign.Center-                        )-                    }--                    if (longExposureProgress > 0f) {-                        LongExposureOverlay(-                            progress = longExposureProgress,-                            remainingText = longExposureRemaining-                        )-                    }--                    // Auto night: shown when a dark scene is detected (getNightModeIndicator /-                    // luminance) and the extension is usable; tap toggles night off for this scene.-                    if (lowLightDetected && nightExtAvailable) {-                        NightModeButton(-                            active = nightModeActive,-                            onClick = { viewModel.toggleNightModeOverride() },-                            iconRotation = animatedRotation,-                            modifier = Modifier.align(Alignment.TopCenter).padding(top = 12.dp)-                        )-                    }--                    // Live vendor night-processing strength while the NIGHT extension preview is bound.-                    extensionStrength?.let { strength ->-                        Text(-                            text = stringResource(R.string.night_2, strength),-                            color = Color.White,-                            fontSize = 12.sp,-                            modifier = Modifier-                                .align(Alignment.TopCenter)-                                .padding(top = 56.dp)-                                .background(Color(0x66000000), RoundedCornerShape(12.dp))-                                .padding(horizontal = 10.dp, vertical = 4.dp)-                        )-                    }--                    // Some vendor NIGHT extensions can't host a concurrent ImageAnalysis stream, so-                    // the session binds without one and QR codes stop being read. Say so rather than-                    // letting the scanner look broken.-                    if (isPhotoType && photoSessionActive && !analysisStreamActive) {-                        Text(-                            text = stringResource(R.string.qr_scanning_paused),-                            color = Color.White,-                            fontSize = 12.sp,-                            modifier = Modifier-                                .align(Alignment.TopCenter)-                                .padding(top = 84.dp)-                                .background(Color(0x66000000), RoundedCornerShape(12.dp))-                                .padding(horizontal = 10.dp, vertical = 4.dp)-                        )-                    }--                    Column(-                        modifier = Modifier-                            .align(Alignment.BottomCenter)-                            .fillMaxWidth()-                            .padding(bottom = 8.dp),-                        horizontalAlignment = Alignment.CenterHorizontally,-                        verticalArrangement = Arrangement.spacedBy(8.dp)-                    ) {-                        when (activeSetting) {-                            null -> ZoomBar(-                                currentZoom = zoomRatio,-                                zoomLevels = availableZoomLevels,-                                onZoomSelected = { viewModel.setZoomRatio(it) }-                            )-                            CameraSetting.BRIGHTNESS -> HorizontalSettingSlider(-                                value = exposureComp,-                                onValueChange = { viewModel.setExposureCompensation(it) },-                                icon = { m, c -> IconSunny(m, c) },-                                label = stringResource(com.vayunmathur.camera.R.string.brightness)-                            )-                            CameraSetting.SHADOWS -> HorizontalSettingSlider(-                                value = shadows,-                                onValueChange = { viewModel.setShadows(it) },-                                icon = { m, c -> IconContrast(m, c) },-                                label = stringResource(com.vayunmathur.camera.R.string.shadows)-                            )-                            CameraSetting.WARMTH -> HorizontalSettingSlider(-                                value = warmth,-                                onValueChange = { viewModel.setWarmth(it) },-                                icon = { m, c -> IconLightbulb(m, c) },-                                label = stringResource(com.vayunmathur.camera.R.string.warmth)-                            )-                            CameraSetting.EXPOSURE_TIME -> ExposureTimeBar(-                                selectedIndex = exposureTimeIndex,-                                onIndexChange = { viewModel.setExposureTimeIndex(it) }-                            )-                            CameraSetting.PORTRAIT_BLUR -> HorizontalSettingSlider(-                                value = blurStrength,-                                onValueChange = { viewModel.setBlurStrength(it) },-                                icon = { m, c -> IconBlur(m, c) },-                                label = stringResource(R.string.blur),-                                valueRange = 0f..1f,-                                displayValue = { "%.0f%%".format(it * 100) }-                            )-                            CameraSetting.ISO -> IsoBar(-                                selectedIndex = manualIsoIndex,-                                isoStops = isoStops,-                                onIndexChange = { viewModel.setManualIsoIndex(it) }-                            )-                        }--                        SettingsButtonRow(-                            activeSetting = activeSetting,-                            cameraMode = cameraMode,-                            onSelect = { activeSetting = it }-                        )-                    }--                    if ((cameraMode == CameraMode.PANORAMA || cameraMode == CameraMode.PHOTOSPHERE) && (panoSweeping || panoStitching)) {-                        PanoramaOverlay(-                            isSweeping = panoSweeping,-                            isStitching = panoStitching,-                            guideDots = panoDots,-                            currentAngle = panoCurrentAngle,-                            sweepDirection = panoDirection,-                            currentPitch = panoPitch,-                            modifier = previewSize-                        )-                    }-                }--                @Composable-                fun ShutterPane() {-                ShutterRow(-                    cameraMode = cameraMode,-                    isRecording = isRecording,-                    isCapturing = isCapturing,-                    recordingPaused = recordingPaused,-                    videoSnapshotSupported = videoSnapshotSupported,-                    galleryBitmap = galleryBitmap,-                    burstEnabled = cameraMode == CameraMode.PHOTO && onCaptureResult == null,-                    onBurstStart = { viewModel.startBurst() },-                    onBurstStop = { viewModel.stopBurst() },-                    onCapture = performCapture,-                    onPauseResume = { viewModel.togglePauseRecording() },-                    onSnapshot = { viewModel.captureVideoSnapshot() },-                    onFlipCamera = { viewModel.flipCamera() },-                    onGallery = {-                        // A last-capture URI can be stale (image deleted) or absent (nothing shot-                        // this session). Verify it still resolves, else open the gallery collection;-                        // wrap the whole launch so a missing image never crashes the app.-                        runCatching {-                            val uri = lastCaptureUri?.takeIf {-                                runCatching { context.contentResolver.getType(it) }.getOrNull() != null-                            }-                            val baseIntent = if (uri != null) {-                                Intent(Intent.ACTION_VIEW).apply {-                                    setDataAndType(uri, context.contentResolver.getType(uri) ?: "image/*")-                                    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)-                                    clipData = ClipData.newRawUri(null, uri)-                                }-                            } else {-                                Intent(Intent.ACTION_MAIN).apply {-                                    addCategory(Intent.CATEGORY_APP_GALLERY)-                                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)-                                }-                            }-                            try {-                                if (!ExternalIntents.launch(context, baseIntent)) {-                                    throw ActivityNotFoundException("System gallery not found")-                                }-                            } catch (_: ActivityNotFoundException) {-                                val fallback = if (uri != null) {-                                    Intent(Intent.ACTION_VIEW).apply {-                                        setDataAndType(uri, context.contentResolver.getType(uri) ?: "image/*")-                                        addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)-                                        clipData = ClipData.newRawUri(null, uri)-                                    }-                                } else {-                                    Intent(Intent.ACTION_VIEW, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)-                                }-                                ExternalIntents.launch(context, Intent.createChooser(fallback, null))-                            }-                        }.onFailure { Log.w("CameraScreen", "Could not open image viewer", it) }-                    },-                    iconRotation = animatedRotation,-                    flipEnabled = !isSloMo-                )-                }--                @Composable-                fun ModesPane() {-                ModeSelector(-                    cameraMode = cameraMode,-                    isPhotoType = isPhotoType,-                    sloMoSupported = sloMoSupported,-                    onModeSelected = { viewModel.switchCameraMode(it) }-                )--                BottomBar(-                    cameraMode = cameraMode,-                    isPhotoType = isPhotoType,-                    iconRotation = animatedRotation,-                    onPickerChanged = { photo ->-                        if (photo) {-                            viewModel.switchCameraMode(CameraMode.PHOTO)-                        } else {-                            viewModel.switchCameraMode(CameraMode.VIDEO)-                        }-                    },-                    onSettingsClick = { backStack.add(Route.Settings) }-                )-                }--                CameraWideLayout(-                    shutter = { ShutterPane() },-                    modes = { ModesPane() },-                    sideBySide = expandedSide,-                )-            }--            qrResult?.let { qr ->-                QrResultOverlay(-                    text = qr,-                    onDismiss = { viewModel.setQrResult(null) },-                    context = context,-                    modifier = Modifier-                        .align(Alignment.BottomCenter)-                        .padding(bottom = qrBottomPadding)-                )-            }--            if (isRecording) {-                RecordingIndicator(-                    durationSec = recordingDuration,-                    modifier = Modifier.align(Alignment.TopCenter).padding(top = indicatorTopPadding)-                )-            }--            if (burstActive) {-                Row(-                    modifier = Modifier-                        .align(Alignment.TopCenter)-                        .padding(top = indicatorTopPadding)-                        .background(Color(0xCC000000), RoundedCornerShape(20.dp))-                        .padding(horizontal = 16.dp, vertical = 8.dp),-                    verticalAlignment = Alignment.CenterVertically,-                    horizontalArrangement = Arrangement.spacedBy(8.dp)-                ) {-                    Text(-                        text = stringResource(R.string.burst_count, burstCount),-                        color = Color.White,-                        fontSize = 16.sp,-                        fontWeight = FontWeight.Bold+                    CameraPreviewBox(+                        state = state,+                        viewModel = viewModel,+                        animatedRotation = animatedRotation,                     )                 }-            } -            if (focusLocked && !isRecording) {-                Text(-                    text = stringResource(R.string.ae_af_lock),-                    color = Color(0xFFFFD54F),-                    fontSize = 13.sp,-                    fontWeight = FontWeight.Bold,-                    modifier = Modifier-                        .align(Alignment.TopCenter)-                        .padding(top = focusTopPadding)-                        .background(Color(0xCC000000), RoundedCornerShape(12.dp))-                        .padding(horizontal = 12.dp, vertical = 6.dp)+                CameraBottomControls(+                    state = state,+                    viewModel = viewModel,+                    backStack = backStack,+                    onCaptureResult = onCaptureResult,+                    onCapture = { currentCapture() },+                    animatedRotation = animatedRotation,                 )             }-        }-    }-}--// Top bar lives in CameraTopBar.kt.--// Preview overlays live in CameraOverlays.kt.--// Settings bars live in CameraSettingsBars.kt.--@Composable-internal fun ShutterRow(-    cameraMode: CameraMode,-    isRecording: Boolean,-    isCapturing: Boolean,-    recordingPaused: Boolean,-    videoSnapshotSupported: Boolean,-    galleryBitmap: android.graphics.Bitmap?,-    burstEnabled: Boolean,-    onBurstStart: () -> Unit,-    onBurstStop: () -> Unit,-    onCapture: () -> Unit,-    onPauseResume: () -> Unit,-    onSnapshot: () -> Unit,-    onFlipCamera: () -> Unit,-    onGallery: () -> Unit,-    iconRotation: Float,-    flipEnabled: Boolean = true-) {-    val shutterScale = animatedFloat(-        target = if (isCapturing) 0.8f else 1f,-        spec = spring(dampingRatio = 0.4f, stiffness = 800f)-    )-    val recordingCornerRadius = animatedFloat(-        target = if (isRecording) 8f else 38f,-        spec = spring(dampingRatio = 0.6f, stiffness = 500f)-    )--    Row(-        modifier = Modifier-            .fillMaxWidth()-            .padding(horizontal = 32.dp, vertical = 16.dp),-        horizontalArrangement = Arrangement.SpaceBetween,-        verticalAlignment = Alignment.CenterVertically-    ) {-        // Left slot: pause/resume while recording, otherwise the gallery thumbnail.-        if (isRecording) {-            Box(-                modifier = Modifier-                    .size(48.dp)-                    .background(Color(0xFF3C3C3C), CircleShape)-                    .clickable(onClick = onPauseResume),-                contentAlignment = Alignment.Center-            ) {-                if (recordingPaused) IconPlay(Modifier.size(24.dp).rotate(iconRotation), Color.White)-                else IconPause(Modifier.size(24.dp).rotate(iconRotation), Color.White)-            }-        } else {-            Box(-                modifier = Modifier-                    .size(48.dp)-                    .clip(RoundedCornerShape(8.dp))-                    .background(Color(0xFF3C3C3C))-                    .clickable(onClick = onGallery),-                contentAlignment = Alignment.Center-            ) {-                if (galleryBitmap != null) {-                    Image(-                        bitmap = galleryBitmap.asImageBitmap(),-                        contentDescription = stringResource(R.string.open_gallery),-                        contentScale = ContentScale.Crop,-                        modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(8.dp))-                    )-                } else {-                    IconPhotoLibrary(Modifier.size(24.dp), Color.White)-                }-            }-        }--        val animatedSize = animatedFloat(-            target = if (isRecording) 36f else 66f,-            spec = spring(dampingRatio = 0.6f, stiffness = 500f)-        )-        Box(-            modifier = Modifier-                .size(76.dp)-                .border(3.dp, Color.White, CircleShape),-            contentAlignment = Alignment.Center-        ) {-            Box(-                modifier = Modifier-                    .graphicsLayer {-                        scaleX = shutterScale-                        scaleY = shutterScale-                    }-                    .size(animatedSize.dp)-                    .clip(RoundedCornerShape(recordingCornerRadius.dp))-                    .background(-                        if (cameraMode in listOf(CameraMode.VIDEO, CameraMode.SLOW_MO, CameraMode.TIMELAPSE, CameraMode.CINEMATIC)) Color.Red-                        else Color.White-                    )-                    .then(-                        if (burstEnabled) {-                            Modifier.pointerInput(Unit) {-                                detectTapGestures(-                                    onTap = { onCapture() },-                                    onLongPress = { onBurstStart() },-                                    onPress = {-                                        tryAwaitRelease()-                                        // No-op if a burst was never started (short tap).-                                        onBurstStop()-                                    }-                                )-                            }-                        } else {-                            Modifier.clickable(onClick = onCapture)-                        }-                    )-            )-        }--        // Right slot: video snapshot while recording (if supported), otherwise flip camera.-        // No flip in Slo-Mo — back camera only.-        if (isRecording) {-            if (videoSnapshotSupported) {-                Box(-                    modifier = Modifier-                        .size(48.dp)-                        .background(Color.White, CircleShape)-                        .clickable(onClick = onSnapshot),-                    contentAlignment = Alignment.Center-                ) {-                    IconCamera(Modifier.size(24.dp).rotate(iconRotation), Color.Black)-                }-            } else {-                Spacer(Modifier.size(48.dp))-            }-        } else {-            if (flipEnabled) {-                Box(-                    modifier = Modifier-                        .size(48.dp)-                        .background(Color(0xFF3C3C3C), CircleShape)-                        .clickable(onClick = onFlipCamera),-                    contentAlignment = Alignment.Center-                ) {-                        IconFlipCamera(Modifier.size(24.dp).rotate(iconRotation), Color.White)-                }-            } else {-                Spacer(Modifier.size(48.dp))-            }-        }-    }-} -@Composable-private fun ModeSelector(-    cameraMode: CameraMode,-    isPhotoType: Boolean,-    sloMoSupported: Boolean?,-    onModeSelected: (CameraMode) -> Unit-) {-    Row(-        modifier = Modifier.fillMaxWidth(),-        horizontalArrangement = Arrangement.Center,-        verticalAlignment = Alignment.CenterVertically-    ) {-        val modes = if (isPhotoType) {-            listOf(-                CameraMode.PORTRAIT to "Portrait",-                CameraMode.PHOTO to "Photo",-                CameraMode.PANORAMA to "Pano",-                CameraMode.PHOTOSPHERE to "Sphere"+            CameraStatusIndicators(+                qrResult = state.qrResult,+                onDismissQr = { viewModel.setQrResult(null) },+                context = LocalContext.current,+                qrBottomPadding = qrBottomPadding,+                isRecording = state.isRecording,+                recordingDuration = state.recordingDuration,+                indicatorTopPadding = indicatorTopPadding,+                burstActive = state.burstActive,+                burstCount = state.burstCount,+                focusLocked = state.focusLocked,+                focusTopPadding = focusTopPadding,             )-        } else {-            // Only include Slo-Mo when the device's back camera actually supports true HFR.-            buildList {-                if (sloMoSupported != false) {-                    add(CameraMode.SLOW_MO to "Slo-Mo")-                }-                add(CameraMode.VIDEO to "Video")-                add(CameraMode.CINEMATIC to "Cinematic")-                add(CameraMode.TIMELAPSE to "Timelapse")-            }         }-        modes.forEach { (mode, label) ->-            val isSelected = cameraMode == mode-            Text(-                text = label,-                color = Color.White,-                fontSize = 14.sp,-                fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,-                modifier = Modifier-                    .selectedPill(isSelected, RoundedCornerShape(20.dp))-                    .clickable { if (!isSelected) onModeSelected(mode) }-                    .padding(horizontal = 16.dp, vertical = 6.dp)-            )-        }-    }-}--@Composable-private fun BottomBar(-    cameraMode: CameraMode,-    isPhotoType: Boolean,-    iconRotation: Float,-    onPickerChanged: (Boolean) -> Unit,-    onSettingsClick: () -> Unit-) {-    Row(-        modifier = Modifier-            .fillMaxWidth()-            .padding(horizontal = 16.dp, vertical = 8.dp),-        horizontalArrangement = Arrangement.SpaceBetween,-        verticalAlignment = Alignment.CenterVertically-    ) {-        Box(-            modifier = Modifier-                .size(44.dp)-                .background(Color(0xFF3C3C3C), CircleShape)-                .clickable(onClick = onSettingsClick),-            contentAlignment = Alignment.Center-        ) {-            IconSettings(-                tint = Color.White,-                modifier = Modifier.size(22.dp).rotate(iconRotation)-            )-        }--        Row(-            modifier = Modifier-                .background(Color(0xFF3C3C3C), RoundedCornerShape(20.dp))-                .padding(4.dp),-            verticalAlignment = Alignment.CenterVertically-        ) {-            Box(-                modifier = Modifier-                    .size(36.dp)-                    .selectedPill(isPhotoType, CircleShape, Color(0xFF5C5C5C))-                    .clickable { if (!isPhotoType) onPickerChanged(true) },-                contentAlignment = Alignment.Center-            ) {-                IconCamera(Modifier.size(20.dp).rotate(iconRotation), Color.White)-            }-            Box(-                modifier = Modifier-                    .size(36.dp)-                    .selectedPill(!isPhotoType, CircleShape, Color(0xFF5C5C5C))-                    .clickable { if (isPhotoType) onPickerChanged(false) },-                contentAlignment = Alignment.Center-            ) {-                IconVideoCamera(Modifier.size(20.dp).rotate(iconRotation), Color.White)-            }-        }--        Spacer(Modifier.size(44.dp))-    }-}--// QrResultOverlay lives in CameraOverlays.kt.--// RecordingIndicator lives in CameraOverlays.kt.--// LongExposureOverlay lives in CameraOverlays.kt.--@Composable-internal fun ExposureTimeBar(-    selectedIndex: Int,-    onIndexChange: (Int) -> Unit,-    modifier: Modifier = Modifier-) {-    val stops = CameraViewModel.EXPOSURE_TIME_STOPS-    val isManual = selectedIndex > 0-    Row(-        modifier = modifier-            .fillMaxWidth()-            .padding(horizontal = 16.dp, vertical = 4.dp),-        verticalAlignment = Alignment.CenterVertically-    ) {-        Box(-            modifier = Modifier-                .size(32.dp)-                .background(-                    if (isManual) Color(0xFF3C3C3C) else Color.Transparent,-                    CircleShape-                ),-            contentAlignment = Alignment.Center-        ) {-            IconTimer(Modifier.size(20.dp), Color.White)-        }-        Slider(-            value = selectedIndex.toFloat(),-            onValueChange = { onIndexChange(it.roundToInt()) },-            valueRange = 0f..(stops.size - 1).toFloat(),-            steps = stops.size - 2,-            modifier = Modifier.weight(1f).padding(horizontal = 8.dp),-            colors = SliderDefaults.colors(-                thumbColor = Color.White,-                activeTrackColor = Color.White,-                inactiveTrackColor = Color(0xFF666666)-            )-        )-        Text(-            text = stops[selectedIndex].label,-            color = Color.White,-            fontSize = 13.sp,-            fontWeight = FontWeight.Medium,-            modifier = Modifier.width(64.dp),-            textAlign = TextAlign.End-        )-    }-}--/** Manual ISO: index 0 == Auto, 1..[isoStops].size select a sensitivity stop. */-@Composable-private fun IsoBar(-    selectedIndex: Int,-    isoStops: List<Int>,-    onIndexChange: (Int) -> Unit,-    modifier: Modifier = Modifier-) {-    val isManual = selectedIndex > 0-    Row(-        modifier = modifier-            .fillMaxWidth()-            .padding(horizontal = 16.dp, vertical = 4.dp),-        verticalAlignment = Alignment.CenterVertically-    ) {-        Box(-            modifier = Modifier-                .size(32.dp)-                .background(if (isManual) Color(0xFF3C3C3C) else Color.Transparent, CircleShape)-                .clip(CircleShape)-                .clickable { onIndexChange(0) },-            contentAlignment = Alignment.Center-        ) {-            IconIso(Modifier.size(20.dp), Color.White)-        }-        if (isoStops.isEmpty()) {-            Text(-                text = stringResource(R.string.not_available),-                color = Color.White,-                fontSize = 13.sp,-                modifier = Modifier.weight(1f).padding(horizontal = 8.dp),-                textAlign = TextAlign.Center-            )-        } else {-            Slider(-                value = selectedIndex.toFloat(),-                onValueChange = { onIndexChange(it.roundToInt()) },-                valueRange = 0f..isoStops.size.toFloat(),-                steps = (isoStops.size - 1).coerceAtLeast(0),-                modifier = Modifier.weight(1f).padding(horizontal = 8.dp),-                colors = SliderDefaults.colors(-                    thumbColor = Color.White,-                    activeTrackColor = Color.White,-                    inactiveTrackColor = Color(0xFF666666)-                )-            )-        }-        Text(-            text = if (selectedIndex == 0) stringResource(R.string.auto) else "${isoStops.getOrNull(selectedIndex - 1) ?: ""}",-            color = Color.White,-            fontSize = 13.sp,-            fontWeight = FontWeight.Medium,-            modifier = Modifier.width(56.dp),-            textAlign = TextAlign.End-        )-    }-}--@Composable-private fun PanoramaOverlay(-    isSweeping: Boolean,-    isStitching: Boolean,-    guideDots: List<GuideDot>,-    currentAngle: Float,-    sweepDirection: Int,-    currentPitch: Float,-    modifier: Modifier = Modifier-) {-    if (isStitching) {-        Box(modifier.fillMaxSize(), contentAlignment = Alignment.Center) {-            Column(horizontalAlignment = Alignment.CenterHorizontally) {-                CircularProgressIndicator(color = Color.White, strokeWidth = 2.dp)-                Spacer(Modifier.height(8.dp))-                Text(text = stringResource(R.string.processing_panorama_u2026), color = Color.White, fontSize = 16.sp)-            }-        }-        return-    }--    if (!isSweeping) return--    val capturedCount = guideDots.count { it.state == GuideDotState.CAPTURED }-    val totalDots = guideDots.size-    val halfFOV = 30f--    val flashAlpha = remember { Animatable(0f) }-    LaunchedEffect(capturedCount) {-        if (capturedCount > 1) {-            flashAlpha.snapTo(0.3f)-            flashAlpha.animateTo(0f, tween(250))-        }-    }--    Box(modifier.fillMaxSize()) {-        if (flashAlpha.value > 0f) {-            Box(Modifier.fillMaxSize().background(Color.White.copy(alpha = flashAlpha.value)))-        }--        Canvas(Modifier.fillMaxSize()) {-            val centerX = size.width / 2-            val centerY = size.height / 2-            val halfVertFOV = 40f--            guideDots.forEach { dot ->-                val angleOffset = dot.targetAngle - currentAngle-                val pitchOffset = dot.targetPitch - currentPitch-                if (Math.abs(angleOffset) <= halfFOV * 1.2f && Math.abs(pitchOffset) <= halfVertFOV * 1.2f) {-                    val dotX = centerX + (angleOffset / halfFOV) * (size.width / 2)-                    val dotY = centerY - (pitchOffset / halfVertFOV) * (size.height / 2)-                    val baseRadius = 14.dp.toPx()--                    when (dot.state) {-                        GuideDotState.PENDING -> {-                            drawCircle(Color.White.copy(alpha = 0.7f), baseRadius, Offset(dotX, dotY), style = Stroke(2.dp.toPx()))-                            drawCircle(Color.White.copy(alpha = 0.3f), 3.dp.toPx(), Offset(dotX, dotY))-                        }-                        GuideDotState.ALIGNING, GuideDotState.CAPTURING -> {-                            drawCircle(Color(0xFF4FC3F7), baseRadius * 1.2f, Offset(dotX, dotY))-                            drawCircle(Color.White, baseRadius * 1.2f, Offset(dotX, dotY), style = Stroke(2.dp.toPx()))-                        }-                        GuideDotState.CAPTURED -> {-                            drawCircle(Color(0xFF4CAF50), baseRadius, Offset(dotX, dotY))-                            val checkPath = Path().apply {-                                moveTo(dotX - baseRadius * 0.35f, dotY)-                                lineTo(dotX - baseRadius * 0.05f, dotY + baseRadius * 0.3f)-                                lineTo(dotX + baseRadius * 0.4f, dotY - baseRadius * 0.3f)-                            }-                            drawPath(checkPath, Color.White, style = Stroke(2.5.dp.toPx(), cap = StrokeCap.Round))-                        }-                    }-                }-            }--            val crossSize = 20.dp.toPx()-            val crossGap = 6.dp.toPx()-            val crossStroke = 2.dp.toPx()-            val crossColor = Color.White-            drawLine(crossColor, Offset(centerX - crossSize, centerY), Offset(centerX - crossGap, centerY), crossStroke)-            drawLine(crossColor, Offset(centerX + crossGap, centerY), Offset(centerX + crossSize, centerY), crossStroke)-            drawLine(crossColor, Offset(centerX, centerY - crossSize), Offset(centerX, centerY - crossGap), crossStroke)-            drawLine(crossColor, Offset(centerX, centerY + crossGap), Offset(centerX, centerY + crossSize), crossStroke)-            drawCircle(crossColor, 2.dp.toPx(), Offset(centerX, centerY))-        }--        Text(-            "$capturedCount / $totalDots",-            color = Color.White,-            fontSize = 16.sp,-            fontWeight = FontWeight.Medium,-            modifier = Modifier.align(Alignment.TopCenter).padding(top = 60.dp)-                .background(Color.Black.copy(alpha = 0.4f), RoundedCornerShape(16.dp))-                .padding(horizontal = 16.dp, vertical = 6.dp)-        )     } }
camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.ktnew file mode 100644index 0000000000..5d750805b8--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenEffects.kt@@ -0,0 +1,265 @@+package com.vayunmathur.camera.ui++import android.content.Context+import android.util.Log+import android.view.OrientationEventListener+import androidx.camera.core.CameraSelector+import androidx.camera.core.ImageAnalysis+import androidx.compose.runtime.Composable+import androidx.compose.runtime.DisposableEffect+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState+import androidx.compose.ui.platform.LocalContext+import androidx.lifecycle.Lifecycle+import androidx.lifecycle.compose.LocalLifecycleOwner+import androidx.lifecycle.repeatOnLifecycle+import com.vayunmathur.camera.util.BokehAnalyzer+import com.vayunmathur.camera.util.CameraMode+import com.vayunmathur.camera.util.CameraViewModel+import com.vayunmathur.camera.util.PhotoAnalyzer+import com.vayunmathur.camera.util.addMotionFrame+import com.vayunmathur.camera.util.applyExposureCompensation+import com.vayunmathur.camera.util.applyImageCaptureFlashMode+import com.vayunmathur.camera.util.enableTorch+import com.vayunmathur.camera.util.onLuminance+import com.vayunmathur.camera.util.refreshNightExtensionUsable+import com.vayunmathur.camera.util.setBokehAnalyzer+import com.vayunmathur.camera.util.setImageAnalyzer+import com.vayunmathur.camera.util.setPhotoAnalyzer+import com.vayunmathur.camera.util.setQrResult+import com.vayunmathur.camera.util.setupHighSpeedSession+import com.vayunmathur.camera.util.setupNightPreviewSession+import com.vayunmathur.camera.util.setupPanoramaSession+import com.vayunmathur.camera.util.setupPhotoSession+import com.vayunmathur.camera.util.setupPortraitSession+import com.vayunmathur.camera.util.setupVideoSession+import com.vayunmathur.camera.util.teardownSession+import com.vayunmathur.camera.util.updateLocation+import kotlinx.coroutines.awaitCancellation+import kotlinx.coroutines.delay++/**+ * All side-effects owned by [CameraScreen]: orientation tracking, one-shot VM sync,+ * analyzer selection, the PORTRAIT bokeh segmenter lifecycle, and unified session binding.+ * Behavior is identical to the previously inline effects; only the location moved.+ */+@Composable+internal fun CameraScreenEffects(+    state: CameraScreenState,+    viewModel: CameraViewModel,+    onCapture: () -> Unit,+) {+    val context = LocalContext.current+    val cameraMode = state.cameraMode+    val lensFacing = state.lensFacing+    val sessionKind = state.sessionKind+    val useNightPreview = state.useNightPreview++    // Tracks physical orientation purely to rotate the on-screen control icons. Capture output+    // rotation is handled by CameraX via SessionConfig.setAutoRotationEnabled(true).+    DisposableEffect(Unit) {+        val listener = object : OrientationEventListener(context) {+            override fun onOrientationChanged(orientation: Int) {+                if (orientation == ORIENTATION_UNKNOWN) return+                state.deviceRotation = when {+                    orientation in 45..134 -> 270+                    orientation in 135..224 -> 180+                    orientation in 225..314 -> 90+                    else -> 0+                }+            }+        }+        listener.enable()+        onDispose { listener.disable() }+    }++    LaunchedEffect(Unit) {+        viewModel.updateLocation()+    }++    LaunchedEffect(state.flashMode) {+        viewModel.applyImageCaptureFlashMode()+    }++    LaunchedEffect(state.torchEnabled) {+        viewModel.enableTorch(state.torchEnabled)+    }++    LaunchedEffect(state.exposureComp) {+        viewModel.applyExposureCompensation(state.exposureComp)+    }++    // Close mode-specific settings when leaving the mode that offers them.+    LaunchedEffect(cameraMode) {+        val portraitOnly = state.activeSetting == CameraSetting.PORTRAIT_BLUR && cameraMode != CameraMode.PORTRAIT+        val photoOnly = state.activeSetting == CameraSetting.ISO && cameraMode != CameraMode.PHOTO+        if (portraitOnly || photoOnly) state.activeSetting = null+    }++    // Whether to offer the NIGHT extension. Reactive: the ViewModel recomputes it on lens/mode+    // change (off-main; support probe + weekly failure cache) AND flips it false immediately if a+    // night bind fails, so a broken extender (GrapheneOS/Pixel) stops engaging night after one try.+    LaunchedEffect(lensFacing, cameraMode) {+        kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Default) {+            viewModel.refreshNightExtensionUsable(cameraMode)+        }+    }+    LaunchedEffect(useNightPreview) {+        Log.d("NightPreview", "CameraScreen useNightPreview recomputed=$useNightPreview sessionKind=$sessionKind cameraMode=$cameraMode nightModeActive=${state.nightModeActive} nightExtAvailable=${state.nightExtAvailable} lensFacing=$lensFacing")+    }+    val surfaceRequest = state.surfaceRequest+    LaunchedEffect(surfaceRequest) {+        Log.d("NightPreview", "CameraScreen surfaceRequest changed res=${surfaceRequest?.resolution} frameType=${surfaceRequest?.javaClass?.simpleName} useNightPreview=$useNightPreview sessionKind=$sessionKind thread=${Thread.currentThread().name} lensFacing=${lensFacing}")+        if (surfaceRequest == null && useNightPreview) {+            Log.w("NightPreview", "CameraScreen WARNING: surfaceRequest is NULL while useNightPreview=true – preview will be black until Provider re-emits. This is the black-frame trap you described: teardown -> delay(250) -> setupNightPreviewSession() tears down standard photo session surface before extension emits!")+        }+        if (surfaceRequest == null && !useNightPreview) {+            Log.w("NightPreview", "CameraScreen tracing maybe delayed rebind? surface null while normal photo – expected during 250ms delay window")+        }+    }+    LaunchedEffect(state.availableZoomLevels) {+        Log.d("NightPreview", "CameraScreen availableZoomLevels changed=${state.availableZoomLevels} currentZoom=${state.zoomRatio} useNightPreview=$useNightPreview – vendor NIGHT often reports only [1x], making zoom bar appear to 'disappear' except 1x")+    }++    // Analyzer selection for the photo modes. Keyed on photoSessionActive so the analyzer is+    // re-applied to the freshly-bound ImageAnalysis after every (re)bind (mode switch / flip), and+    // on lensFacing to match the DisposableEffect below, which detaches on a flip.+    LaunchedEffect(cameraMode, state.photoSessionActive, lensFacing) {+        when {+            cameraMode == CameraMode.SLOW_MO -> {+                state.maskBitmap = null+                viewModel.setImageAnalyzer(null)+                viewModel.setQrResult(null)+            }+            state.isPhotoType -> {+                when (cameraMode) {+                    CameraMode.PORTRAIT -> {+                        // Analyzer lifecycle (create/close) managed by DisposableEffect below.+                    }+                    CameraMode.PANORAMA, CameraMode.PHOTOSPHERE -> {+                        state.maskBitmap = null+                        viewModel.setImageAnalyzer(+                            ImageAnalysis.Analyzer { imageProxy ->+                                viewModel.panoramaEngine.latestFrame = imageProxy.toBitmap()+                                imageProxy.close()+                            }+                        )+                    }+                    else -> {+                        state.maskBitmap = null+                        viewModel.setPhotoAnalyzer(+                            PhotoAnalyzer(+                                onLuminance = { viewModel.onLuminance(it) },+                                onQrDetected = { viewModel.setQrResult(it) },+                                // Motion-Photo ring buffer only in plain PHOTO mode.+                                onMotionFrame = if (cameraMode == CameraMode.PHOTO) {+                                    { bmp, ts, rot -> viewModel.addMotionFrame(bmp, ts, rot) }+                                } else null+                            )+                        )+                    }+                }+            }+            else -> {+                state.maskBitmap = null+                viewModel.setQrResult(null)+            }+        }+    }++    // Owns the PORTRAIT bokeh segmenter so it is closed (and its mask recycled) on mode change.+    // Keyed on photoSessionActive (re-attach after a rebind) and lensFacing (front/back changes+    // the frame orientation/mirroring the analyzer must apply).+    // Runs on dedicated bokeh executor so main thread stays free for preview rendering.+    // Final capture ImageCapture is still max-res – only analysis is capped.+    // Mask callback comes from bokeh thread; post recycle+set to main to avoid racing with+    // the RenderEffect reading the previous Bitmap on the UI thread.+    val mainHandler = remember { android.os.Handler(android.os.Looper.getMainLooper()) }+    DisposableEffect(cameraMode, state.photoSessionActive, lensFacing) {+        val analyzer = if (cameraMode == CameraMode.PORTRAIT) {+            BokehAnalyzer(+                context,+                isFrontFacing = lensFacing == CameraSelector.LENS_FACING_FRONT+            ) { mask ->+                if (mask.isRecycled) return@BokehAnalyzer+                mainHandler.post {+                    // If mask was recycled while message queued, drop it+                    if (mask.isRecycled) return@post+                    val prev = state.maskBitmap+                    if (prev != null && prev !== mask && !prev.isRecycled) {+                        try { prev.recycle() } catch (_: Exception) {}+                    }+                    state.maskBitmap = mask+                }+            }.also {+                viewModel.setBokehAnalyzer(it)+            }+        } else null+        onDispose {+            // Only detach what this effect attached. It also keys on lensFacing, which the+            // analyzer-selection effect above does not, so an unconditional clear stripped+            // PhotoAnalyzer off the photo stream on every camera flip.+            if (analyzer != null) {+                viewModel.setImageAnalyzer(null)+                analyzer.close()+            }+            // Post recycle to avoid racing with graphicsLayer reading maskBitmap+            mainHandler.post {+                state.maskBitmap?.let { bmp ->+                    if (!bmp.isRecycled) {+                        try { bmp.recycle() } catch (_: Exception) {}+                    }+                }+                state.maskBitmap = null+            }+        }+    }++    // Unified session binding, made lifecycle-aware so the camera is released when the app is+    // backgrounded and rebound on resume. Without this the ManualLifecycleOwner stays RESUMED,+    // the OS reclaims the camera while we're away, and the preview comes back frozen.+    val lifecycleOwner = LocalLifecycleOwner.current+    LaunchedEffect(lensFacing, sessionKind, useNightPreview, lifecycleOwner) {+        Log.d("NightPreview", "CameraScreen session LaunchedEffect START keys lensFacing=$lensFacing sessionKind=$sessionKind useNightPreview=$useNightPreview lifecycle=${lifecycleOwner.lifecycle.currentState} thread=${Thread.currentThread().name}")+        lifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {+            Log.d("NightPreview", "CameraScreen repeatOnLifecycle STARTED – calling teardownSession()")+            viewModel.teardownSession()+            // Brief yield so the previous session's surface detaches before the next binds; short+            // enough that a mode switch reads as instant, long enough to avoid a bind-over-teardown race.+            delay(40)+            val bindStart = System.currentTimeMillis()+            val bindSuccess = try {+                when (sessionKind) {+                    SessionKind.HIGH_SPEED -> viewModel.setupHighSpeedSession()+                    SessionKind.VIDEO -> viewModel.setupVideoSession()+                    SessionKind.PANORAMA -> viewModel.setupPanoramaSession()+                    SessionKind.PORTRAIT -> viewModel.setupPortraitSession()+                    SessionKind.PHOTO ->+                        if (useNightPreview) viewModel.setupNightPreviewSession()+                        else viewModel.setupPhotoSession()+                }+            } catch (e: Exception) {+                Log.e("NightPreview", "CameraScreen session binding THREW (was not logged before) kind=$sessionKind useNightPreview=$useNightPreview nightExtAvailable=${state.nightExtAvailable}", e)+                false+            }+            Log.d("NightPreview", "CameraScreen session bind finished success=$bindSuccess took=${System.currentTimeMillis() - bindStart}ms surface after=${viewModel.surfaceRequest.value?.resolution} zoomLevels=${viewModel.availableZoomLevels.value} zoomRatio=${viewModel.zoomRatio.value}")+            if (!bindSuccess) {+                Log.e("NightPreview", "CameraScreen BIND FAILED – this produces solid black preview and zoom bar showing only 1x (fallback?). Check logcat for NightPreview tag – exception was previously swallowed as warning")+            }+            try {+                awaitCancellation()+            } finally {+                Log.d("NightPreview", "CameraScreen session coroutine cancelled/finished, calling teardownSession()")+                viewModel.teardownSession()+            }+        }+        Log.d("NightPreview", "CameraScreen repeatOnLifecycle block EXIT – lifecycle dropped below STARTED")+    }++    // Hardware volume-key shutter.+    val currentCapture = rememberUpdatedState(onCapture)+    LaunchedEffect(Unit) {+        viewModel.shutterEvents.collect { currentCapture.value() }+    }+}
camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenState.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenState.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenState.ktnew file mode 100644index 0000000000..00e016a959--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreenState.kt@@ -0,0 +1,225 @@+package com.vayunmathur.camera.ui++import android.graphics.Bitmap+import androidx.camera.core.CameraSelector+import androidx.compose.runtime.Composable+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableIntStateOf+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import com.vayunmathur.camera.util.AspectRatioOption+import com.vayunmathur.camera.util.CameraMode+import com.vayunmathur.camera.util.CameraViewModel+import com.vayunmathur.camera.util.FlashMode+import com.vayunmathur.camera.util.TimerDuration++/**+ * All UI state read by [CameraScreen], collected from [CameraViewModel] flows in one+ * place so the screen itself stays a thin layout. Also owns the two pieces of+ * composition-local state (open settings pane, portrait mask bitmap, orientation).+ *+ * Collection happens in [rememberCameraScreenState] (a @Composable context); this class is a+ * plain snapshot holder so the screen stays a thin layout.+ */+internal class CameraScreenState(+    val cameraMode: CameraMode,+    val lensFacing: Int,+    val flashMode: FlashMode,+    val torchEnabled: Boolean,+    val isRecording: Boolean,+    val recordingDuration: Long,+    val timerCountdown: Int,+    val qrResult: String?,+    val aspectRatio: AspectRatioOption,+    val zoomRatio: Float,+    val mirrorFront: Boolean,+    val timerDuration: TimerDuration,+    val isCapturing: Boolean,+    val burstActive: Boolean,+    val burstCount: Int,+    val focusLocked: Boolean,+    val recordingPaused: Boolean,+    val micMuted: Boolean,+    val videoSnapshotSupported: Boolean,+    val lastCaptureUri: android.net.Uri?,+    val gridEnabled: Boolean,+    val levelEnabled: Boolean,+    val roll: Float,+    val blurStrength: Float,+    val exposureComp: Float,+    val warmth: Float,+    val shadows: Float,+    val exposureTimeIndex: Int,+    val manualIsoIndex: Int,+    val isoStops: List<Int>,+    val longExposureProgress: Float,+    val longExposureRemaining: String,+    val lowLightDetected: Boolean,+    val nightModeActive: Boolean,+    val extensionStrength: Int?,+    val sloMoSupported: Boolean?,+    val highSpeedActive: Boolean,+    val photoSessionActive: Boolean,+    val analysisStreamActive: Boolean,+    val nightExtAvailable: Boolean,+    val surfaceRequest: androidx.camera.core.SurfaceRequest?,+    val availableZoomLevels: List<Pair<String, Float>>,+    val galleryBitmap: Bitmap?,+    val panoSweeping: Boolean,+    val panoStitching: Boolean,+    val panoDots: List<com.vayunmathur.camera.util.GuideDot>,+    val panoCurrentAngle: Float,+    val panoDirection: Int,+    val panoPitch: Float,+) {+    var activeSetting by mutableStateOf<CameraSetting?>(null)+    var maskBitmap by mutableStateOf<Bitmap?>(null)+    var deviceRotation by mutableIntStateOf(0)++    val isPhotoType get() = cameraMode in listOf(CameraMode.PHOTO, CameraMode.PORTRAIT, CameraMode.PANORAMA, CameraMode.PHOTOSPHERE)+    val isSloMo get() = cameraMode == CameraMode.SLOW_MO+    val isVideoType get() = cameraMode == CameraMode.VIDEO || cameraMode == CameraMode.TIMELAPSE || cameraMode == CameraMode.CINEMATIC+    val isPanoType get() = cameraMode == CameraMode.PANORAMA || cameraMode == CameraMode.PHOTOSPHERE+    val isPortrait get() = cameraMode == CameraMode.PORTRAIT+    val sessionKind get() = when {+        isSloMo -> SessionKind.HIGH_SPEED+        isVideoType -> SessionKind.VIDEO+        isPanoType -> SessionKind.PANORAMA+        isPortrait -> SessionKind.PORTRAIT+        else -> SessionKind.PHOTO+    }++    // Auto night: when getNightModeIndicator()/luminance reports a dark scene (nightModeActive) in+    // PHOTO mode and the extension is usable, bind the vendor NIGHT extension preview.+    val useNightPreview get() = sessionKind == SessionKind.PHOTO && cameraMode == CameraMode.PHOTO &&+        nightModeActive && nightExtAvailable++    val previewAspectRatio get() = when (aspectRatio) {+        AspectRatioOption.RATIO_16_9 -> 9f / 16f+        AspectRatioOption.RATIO_4_3 -> 3f / 4f+        AspectRatioOption.RATIO_1_1 -> 1f+    }++    val mirrorPreview get() = lensFacing == CameraSelector.LENS_FACING_FRONT && !mirrorFront+}++@Composable+internal fun rememberCameraScreenState(viewModel: CameraViewModel): CameraScreenState {+    val cameraMode by viewModel.cameraMode.collectAsState()+    val lensFacing by viewModel.lensFacing.collectAsState()+    val flashMode by viewModel.flashMode.collectAsState()+    val torchEnabled by viewModel.torchEnabled.collectAsState()+    val isRecording by viewModel.isRecording.collectAsState()+    val recordingDuration by viewModel.recordingDurationSec.collectAsState()+    val timerCountdown by viewModel.timerCountdown.collectAsState()+    val qrResult by viewModel.qrResult.collectAsState()+    val aspectRatio by viewModel.aspectRatio.collectAsState()+    val zoomRatio by viewModel.zoomRatio.collectAsState()+    val mirrorFront by viewModel.mirrorFront.collectAsState()+    val timerDuration by viewModel.timerDuration.collectAsState()+    val isCapturing by viewModel.isCapturing.collectAsState()+    val burstActive by viewModel.burstActive.collectAsState()+    val burstCount by viewModel.burstCount.collectAsState()+    val focusLocked by viewModel.focusLocked.collectAsState()+    val recordingPaused by viewModel.recordingPaused.collectAsState()+    val micMuted by viewModel.micMuted.collectAsState()+    val videoSnapshotSupported by viewModel.videoSnapshotSupported.collectAsState()+    val lastCaptureUri by viewModel.lastCaptureUri.collectAsState()+    val gridEnabled by viewModel.gridEnabled.collectAsState()+    val levelEnabled by viewModel.levelEnabled.collectAsState()+    val roll by viewModel.roll.collectAsState()+    val blurStrength by viewModel.blurStrength.collectAsState()+    val exposureComp by viewModel.exposureCompensation.collectAsState()+    val warmth by viewModel.warmth.collectAsState()+    val shadows by viewModel.shadows.collectAsState()+    val exposureTimeIndex by viewModel.exposureTimeIndex.collectAsState()+    val manualIsoIndex by viewModel.manualIsoIndex.collectAsState()+   

Diff truncated.