camera: restore portrait lock - controls stay put, icons rotate in place
- Commit
014da28573ee19f32d4714051da2b8e06cb4cdbc- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-21 16:01 UTC (15 days ago)
- Parents
584b88c
Restores the owner's b84d8760a behaviour, undone by d0ec660e4 (a lint pass dropped the portrait lock, 'the wide layout is adaptive'). Rotating the phone rotated the whole control layout. The activity is portrait-locked again (LockedOrientationActivity suppressed with the reason), the expanded-width side-by-side switch is gone, and the existing OrientationEventListener plumbing rotates glyphs in place (now including the gallery placeholder). Capture orientation is untouched: CameraX auto-rotation still orients stills and video. The focus ring deliberately does not rotate - it's symmetric and must stay on the tapped point. A guard test asserts the manifest orientation so dropping the lock fails tests next time. Verified in the integrated 548-test green run.
Diff
camera/src/main/AndroidManifest.xml
diff --git a/camera/src/main/AndroidManifest.xml b/camera/src/main/AndroidManifest.xmlindex d04c1ae676..529144b0c2 100644--- a/camera/src/main/AndroidManifest.xml+++ b/camera/src/main/AndroidManifest.xml@@ -31,10 +31,16 @@ android:roundIcon="@mipmap/ic_launcher" android:supportsRtl="true" android:theme="@style/Theme.General">+ <!-- Portrait-locked on purpose: the viewfinder layout never rotates, so+ capture controls stay in the same place in portrait and landscape;+ only the control icons rotate in place (OrientationEventListener in+ CameraScreenEffects) and CameraX auto-rotation orients captures. --> <activity android:configChanges="mcc|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|keyboardHidden|layoutDirection|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode" android:name=".MainActivity" android:exported="true"+ android:screenOrientation="portrait"+ tools:ignore="LockedOrientationActivity" android:showWhenLocked="true" android:turnScreenOn="true" android:theme="@style/Theme.General">
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.ktindex d9f9efc0ec..dc2509e26a 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraBottomControls.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraBottomControls.kt@@ -16,13 +16,12 @@ import com.vayunmathur.camera.platform.startBurst import com.vayunmathur.camera.platform.stopBurst import com.vayunmathur.camera.platform.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.+ * Shutter row, mode selector and bottom bar below the preview. Always stacked+ * in that order — rotating the phone never rearranges them (see+ * [captureControlsSideBySide]); only the icons inside rotate in place. */ @Composable internal fun CameraBottomControls(@@ -35,7 +34,6 @@ internal fun CameraBottomControls( ) { 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;@@ -83,8 +81,8 @@ internal fun CameraBottomControls( } } - // Expanded windows place the shutter and mode controls side by side- // below the preview; short windows keep the phone stack.+ // Never side-by-side on rotation: shutter, then modes, stacked below the+ // preview at every device rotation. CameraWideLayout( shutter = { CameraShutterPane(@@ -119,6 +117,6 @@ internal fun CameraBottomControls( videoPickerEnabled = onCaptureResult == null, ) },- sideBySide = expandedSide,+ sideBySide = captureControlsSideBySide(state.deviceRotation), ) }
camera/src/main/java/com/vayunmathur/camera/ui/CameraShutterRow.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraShutterRow.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraShutterRow.ktindex fc4cba5147..379dbffad0 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraShutterRow.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraShutterRow.kt@@ -101,7 +101,7 @@ internal fun ShutterRow( modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(8.dp)) ) } else {- IconPhotoLibrary(Modifier.size(24.dp), Color.White)+ IconPhotoLibrary(Modifier.size(24.dp).rotate(iconRotation), Color.White) } } }
camera/src/main/java/com/vayunmathur/camera/ui/CameraWideLayout.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraWideLayout.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraWideLayout.ktindex 2f1261ac66..0d46bd6290 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraWideLayout.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraWideLayout.kt@@ -8,13 +8,11 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier /**- * Adaptive placement for the viewfinder control rows (shutter, mode selector,- * bottom bar).+ * Placement for the viewfinder control rows (shutter, mode selector, bottom bar). *- * Phones stack them vertically under the preview. On wide windows they sit side- * by side — shutter left, mode + bottom stacked right — so they stop eating the- * preview's vertical space in landscape desktop windows. Slots only: the caller- * passes the real rows, which capture the screen's state directly.+ * Slots only: the caller passes the real rows and decides [sideBySide]. The+ * camera caller always stacks them vertically (see+ * [captureControlsSideBySide]) so rotation never rearranges the controls. */ @Composable fun CameraWideLayout(
camera/src/main/java/com/vayunmathur/camera/ui/DeviceRotation.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/DeviceRotation.kt b/camera/src/main/java/com/vayunmathur/camera/ui/DeviceRotation.ktnew file mode 100644index 0000000000..d5bf706717--- /dev/null+++ b/camera/src/main/java/com/vayunmathur/camera/ui/DeviceRotation.kt@@ -0,0 +1,43 @@+package com.vayunmathur.camera.ui++/**+ * Pure rotation-in-place helpers for the portrait-locked camera.+ *+ * The activity never rotates (see AndroidManifest `screenOrientation="portrait"`),+ * so the window, and therefore every control's placement, is identical in+ * portrait and landscape. What changes with the physical device is only the+ * glyph orientation: [deviceRotationForOrientation] maps the sensor angle from+ * `OrientationEventListener` onto the quadrant the on-screen icons must rotate+ * to (via `Modifier.rotate`), while capture output rotation is handled by+ * CameraX (`SessionConfig.setAutoRotationEnabled(true)` in the session binder).+ */++/**+ * Maps an `OrientationEventListener` angle (0..359, or+ * `ORIENTATION_UNKNOWN` = -1) to the icon rotation in degrees clockwise.+ * Returns null for an unknown angle so the caller keeps the last rotation+ * instead of snapping back to upright.+ */+internal fun deviceRotationForOrientation(orientation: Int): Int? = when {+ orientation < 0 -> null+ orientation in 45..134 -> 270+ orientation in 135..224 -> 180+ orientation in 225..314 -> 90+ else -> 0+}++/**+ * Whether the capture controls (shutter row + mode/bottom rows) rearrange+ * side-by-side for [deviceRotation]. Always false: rotating the phone must+ * never move a control — the shutter stays centred at the bottom, the mode+ * row stays under it, and the bottom bar keeps its slots; only the icons+ * inside those slots rotate. A layout switch keyed on rotation (or on the+ * window widening that a rotating activity would cause) is the regression+ * this guards.+ */+internal fun captureControlsSideBySide(deviceRotation: Int): Boolean {+ require(deviceRotation == 0 || deviceRotation == 90 || deviceRotation == 180 || deviceRotation == 270) {+ "deviceRotation must be a quadrant, was $deviceRotation"+ }+ return false+}
camera/src/main/java/com/vayunmathur/camera/ui/FocusIndicator.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/FocusIndicator.kt b/camera/src/main/java/com/vayunmathur/camera/ui/FocusIndicator.ktindex 77d82ee4d4..ae09134758 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/FocusIndicator.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/FocusIndicator.kt@@ -31,6 +31,10 @@ private const val BRACKET_FRACTION = 0.45f * The composable only draws; when it appears and disappears is the caller's * in-screen state. Motion goes through the library helpers (`animatedDp` / * `animatedFloat` on [Motion] specs), never raw animation APIs.+ *+ * Deliberately NOT rotated with the device: the ring + corner brackets are+ * symmetric under 90° rotations, and [point] is in preview coordinates — the+ * ring must stay on the tapped spot, not swing around it. */ @Composable internal fun FocusIndicator(
camera/src/test/java/com/vayunmathur/camera/ui/OrientationLayoutGuardTest.kt
diff --git a/camera/src/test/java/com/vayunmathur/camera/ui/OrientationLayoutGuardTest.kt b/camera/src/test/java/com/vayunmathur/camera/ui/OrientationLayoutGuardTest.ktnew file mode 100644index 0000000000..b4929874b1--- /dev/null+++ b/camera/src/test/java/com/vayunmathur/camera/ui/OrientationLayoutGuardTest.kt@@ -0,0 +1,73 @@+package com.vayunmathur.camera.ui++import android.content.ComponentName+import android.content.pm.ActivityInfo+import androidx.test.core.app.ApplicationProvider+import androidx.test.ext.junit.runners.AndroidJUnit4+import com.vayunmathur.camera.MainActivity+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNull+import org.junit.runner.RunWith++/**+ * Guards the rotation-in-place behaviour: the camera is portrait-locked, so+ * rotating the phone never rearranges the capture controls — only their icons+ * rotate (via [deviceRotationForOrientation]) and CameraX auto-rotation+ * orients the capture. Reverting any leg fails here: dropping the manifest+ * lock (as the lint sweep in d0ec660e4 did), remapping a quadrant, or+ * reintroducing a side-by-side layout switch on rotation.+ *+ * Host-side limits: Robolectric cannot physically rotate a device or measure+ * real window bounds, so actual on-device placement, the animated icon+ * rotation, and EXIF/video orientation can only be checked on a device; here+ * we pin the manifest contract, the quadrant math, and the placement decision+ * the layout actually calls.+ */+@RunWith(AndroidJUnit4::class)+class OrientationLayoutGuardTest {++ @Test+ fun activityStaysPortraitLockedSoLayoutNeverRotates() {+ val context = ApplicationProvider.getApplicationContext<android.content.Context>()+ val info = context.packageManager.getActivityInfo(+ ComponentName(context, MainActivity::class.java), 0,+ )+ assertEquals(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, info.screenOrientation)+ }++ @Test+ fun sensorAngleMapsToIconQuadrant() {+ // Upright, including the dead zones either side of 0/360.+ assertEquals(0, deviceRotationForOrientation(0))+ assertEquals(0, deviceRotationForOrientation(44))+ assertEquals(0, deviceRotationForOrientation(315))+ assertEquals(0, deviceRotationForOrientation(359))+ // Each landscape / upside-down quadrant.+ assertEquals(270, deviceRotationForOrientation(45))+ assertEquals(270, deviceRotationForOrientation(90))+ assertEquals(270, deviceRotationForOrientation(134))+ assertEquals(180, deviceRotationForOrientation(135))+ assertEquals(180, deviceRotationForOrientation(180))+ assertEquals(180, deviceRotationForOrientation(224))+ assertEquals(90, deviceRotationForOrientation(225))+ assertEquals(90, deviceRotationForOrientation(270))+ assertEquals(90, deviceRotationForOrientation(314))+ }++ @Test+ fun unknownSensorAngleKeepsPreviousRotation() {+ assertNull(deviceRotationForOrientation(-1))+ }++ @Test+ fun controlPlacementIdenticalAcrossOrientations() {+ for (rotation in listOf(0, 90, 180, 270)) {+ assertFalse(+ captureControlsSideBySide(rotation),+ "controls must stack identically at deviceRotation=$rotation",+ )+ }+ }+}