Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@
<profileable android:shell="true"/>
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout|keyboardHidden"
android:exported="true"
android:label="@string/app_name_short"
android:screenOrientation="nosensor"
android:screenOrientation="user"
android:theme="@style/Theme.JetpackCamera">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/google/jetpackcamera/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.os.Bundle
import android.provider.MediaStore
import android.provider.Settings
import android.util.Log
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -103,6 +104,9 @@ class MainActivity : ComponentActivity() {
navigationBarStyle = SystemBarStyle.dark(android.graphics.Color.TRANSPARENT)
)
super.onCreate(savedInstanceState)
window.attributes = window.attributes.apply {
rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS
}
cameraLaunchConfigProvider.setIntent(intent)
var uiState: MainActivityUiState by mutableStateOf(Loading)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import android.util.Log
import android.util.Range
import android.util.Rational
import android.util.Size
import android.view.Surface
import androidx.annotation.MainThread
import androidx.annotation.OptIn
import androidx.camera.camera2.interop.Camera2CameraControl
Expand Down Expand Up @@ -877,6 +878,13 @@ private fun createPreviewUseCase(
stabilizationMode: StabilizationMode,
captureResults: MutableStateFlow<TotalCaptureResult?>? = null
): Preview = Preview.Builder().apply {
// Phone landscape counter-rotates the UI and PreviewDisplay back into a canonical portrait
// (ROTATION_0) coordinate frame. Pin Preview's target rotation to ROTATION_0 so ViewPort crop
// rects and StreamSharing buffers remain aligned with the portrait viewfinder even when bound
// while the OS window is in landscape. Upright large-screen / multi-window layouts will drive
// Preview and ViewPort from a dedicated window/viewfinder rotation distinct from the sensor-
// based deviceRotation used for ImageCapture and VideoCapture EXIF orientation.
setTargetRotation(Surface.ROTATION_0)
Comment thread
davidjiagoogle marked this conversation as resolved.
Resolved
updateCameraStateWithCaptureResults(
targetCameraInfo = cameraInfo,
captureResults = captureResults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,49 @@ enum class DeviceRotation {
}
}

/**
* Returns the clockwise rotation in degrees needed to orient UI elements upright within a
* natural-portrait coordinate frame.
*
* [Rotated180] (reverse portrait) is mapped to `0f` so camera controls do not flip upside-down.
*/
fun toUiRotationDegrees(): Float {
return when (this) {
Natural, Rotated180 -> 0f
Rotated90 -> -90f
Rotated270 -> 90f
}
}

/**
* Returns the clockwise rotation in degrees needed to counter-rotate a root container back to
* the device's natural portrait coordinate frame when the OS window is rotated to `this`
* orientation.
*/
fun toWindowCounterRotationDegrees(): Float {
return when (this) {
Natural -> 0f
Rotated90 -> 90f
Rotated180 -> 180f
Rotated270 -> -90f
}
}

val isLandscape: Boolean
get() = this == Rotated90 || this == Rotated270

companion object {
/** Returns the [DeviceRotation] corresponding to a UI [Surface] rotation constant. */
fun fromUiSurfaceRotation(surfaceRotation: Int): DeviceRotation {
return when (surfaceRotation) {
Surface.ROTATION_0 -> Natural
Surface.ROTATION_90 -> Rotated270
Surface.ROTATION_180 -> Rotated180
Surface.ROTATION_270 -> Rotated90
else -> Natural
}
}

fun snapFrom(degrees: Int): DeviceRotation {
check(degrees in 0..359) {
"Degrees must be in the range [0, 360)"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.jetpackcamera.model

import android.view.Surface
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class DeviceRotationTest {
Comment thread
davidjiagoogle marked this conversation as resolved.
Resolved

@Test
fun toUiRotationDegrees_mapsRotated180ToZeroAndLandscapesToOppositeSign() {
assertThat(DeviceRotation.Natural.toUiRotationDegrees()).isEqualTo(0f)
assertThat(DeviceRotation.Rotated180.toUiRotationDegrees()).isEqualTo(0f)
assertThat(DeviceRotation.Rotated90.toUiRotationDegrees()).isEqualTo(-90f)
assertThat(DeviceRotation.Rotated270.toUiRotationDegrees()).isEqualTo(90f)
}

@Test
fun toWindowCounterRotationDegrees_cancelsOutUiRotationInLandscape() {
assertThat(DeviceRotation.Natural.toWindowCounterRotationDegrees()).isEqualTo(0f)
assertThat(DeviceRotation.Rotated90.toWindowCounterRotationDegrees()).isEqualTo(90f)
assertThat(DeviceRotation.Rotated180.toWindowCounterRotationDegrees()).isEqualTo(180f)
assertThat(DeviceRotation.Rotated270.toWindowCounterRotationDegrees()).isEqualTo(-90f)

assertThat(
DeviceRotation.Rotated90.toWindowCounterRotationDegrees() +
DeviceRotation.Rotated90.toUiRotationDegrees()
).isEqualTo(0f)
assertThat(
DeviceRotation.Rotated270.toWindowCounterRotationDegrees() +
DeviceRotation.Rotated270.toUiRotationDegrees()
).isEqualTo(0f)
}

@Test
fun isLandscape_isTrueOnlyForRotated90AndRotated270() {
assertThat(DeviceRotation.Natural.isLandscape).isFalse()
assertThat(DeviceRotation.Rotated180.isLandscape).isFalse()
assertThat(DeviceRotation.Rotated90.isLandscape).isTrue()
assertThat(DeviceRotation.Rotated270.isLandscape).isTrue()
}

@Test
fun fromUiSurfaceRotation_isInverseOfToUiSurfaceRotation() {
for (rotation in DeviceRotation.entries) {
val surfaceRotation = rotation.toUiSurfaceRotation()
assertThat(DeviceRotation.fromUiSurfaceRotation(surfaceRotation)).isEqualTo(rotation)
}
assertThat(DeviceRotation.fromUiSurfaceRotation(Surface.ROTATION_0))
.isEqualTo(DeviceRotation.Natural)
assertThat(DeviceRotation.fromUiSurfaceRotation(Surface.ROTATION_90))
.isEqualTo(DeviceRotation.Rotated270)
assertThat(DeviceRotation.fromUiSurfaceRotation(Surface.ROTATION_180))
.isEqualTo(DeviceRotation.Rotated180)
assertThat(DeviceRotation.fromUiSurfaceRotation(Surface.ROTATION_270))
.isEqualTo(DeviceRotation.Rotated90)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package com.google.jetpackcamera.feature.preview

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.pm.ActivityInfo
import android.util.Log
import android.util.Range
import androidx.activity.compose.BackHandler
Expand All @@ -38,6 +42,7 @@ import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.rememberBottomSheetScaffoldState
import androidx.compose.material3.rememberStandardBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
Expand Down Expand Up @@ -190,13 +195,6 @@ fun PreviewScreen(
}
}

val context = LocalContext.current
LaunchedEffect(Unit) {
debouncedOrientationFlow(context).collect(
viewModel.cameraController::setDisplayRotation
)
}

ContentScreen(
modifier = modifier,
captureUiStateProvider = readyStateProvider,
Expand Down Expand Up @@ -254,6 +252,33 @@ private fun ContentScreen(
currentCaptureUiStateProvider().videoRecordingState is VideoRecordingState.Active
}
}
val isVideoRecordingInProgress = remember {
derivedStateOf {
currentCaptureUiStateProvider().videoRecordingState !is VideoRecordingState.Inactive
}
}

val context = LocalContext.current
DisposableEffect(context, isVideoRecordingInProgress.value) {
val activity = context.findActivity()
if (activity != null && isVideoRecordingInProgress.value) {
val previousOrientation = activity.requestedOrientation
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED
onDispose {
activity.requestedOrientation = previousOrientation
}
} else {
onDispose {}
}
}

LaunchedEffect(isVideoRecordingInProgress.value, cameraController) {
if (!isVideoRecordingInProgress.value) {
cameraController?.let { controller ->
debouncedOrientationFlow(context).collect(controller::setDisplayRotation)
}
}
}
Comment thread
davidjiagoogle marked this conversation as resolved.
Resolved

val scope = rememberCoroutineScope()
val zoomStateManager = remember(zoomController) {
Expand Down Expand Up @@ -1039,3 +1064,9 @@ private val FAKE_PREVIEW_UI_STATE_LOCKED_RECORDING = FAKE_PREVIEW_UI_STATE_READY
captureButtonUiState = CaptureButtonUiState.Enabled.Recording.LockedRecording,
audioUiState = AudioUiState.Enabled.On(1.0, true)
)

private tailrec fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fun CameraErrorDialog(
) {
Surface(
modifier = Modifier
.rotatedLayout(LocalDeviceRotation.current)
.padding(horizontal = 24.dp)
.widthIn(min = 280.dp, max = 560.dp)
.pointerInput(Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,10 @@ private fun LockSwitchCaptureButtonNucleus(
}
) {
Icon(
modifier = Modifier.size(switchHeight * .75f).align(Alignment.Center),
modifier = Modifier
.size(switchHeight * .75f)
.align(Alignment.Center)
.animateToUpright(),
tint = Color.White,
painter = if (shouldBeLocked()) {
painterResource(R.drawable.ic_lock)
Expand Down
Loading
Loading