1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.jetpackcamera.settings.model 17 18 import android.view.Surface 19 20 enum class DeviceRotation { 21 Natural, 22 Rotated90, 23 Rotated180, 24 Rotated270; 25 26 /** 27 * Returns the rotation of the UI, expressed as a [Surface] rotation constant, needed to 28 * compensate for device rotation. 29 * 30 * These values do not match up with the device rotation angle. When the device is rotated, 31 * the UI must rotate in the opposite direction to compensate, so the angles 90 and 270 will 32 * be swapped in UI rotation compared to device rotation. 33 */ toUiSurfaceRotationnull34 fun toUiSurfaceRotation(): Int { 35 return when (this) { 36 Natural -> Surface.ROTATION_0 37 Rotated90 -> Surface.ROTATION_270 38 Rotated180 -> Surface.ROTATION_180 39 Rotated270 -> Surface.ROTATION_90 40 } 41 } 42 toClockwiseRotationDegreesnull43 fun toClockwiseRotationDegrees(): Int { 44 return when (this) { 45 Natural -> 0 46 Rotated90 -> 90 47 Rotated180 -> 180 48 Rotated270 -> 270 49 } 50 } 51 52 companion object { snapFromnull53 fun snapFrom(degrees: Int): DeviceRotation { 54 check(degrees in 0..359) { 55 "Degrees must be in the range [0, 360)" 56 } 57 58 return when (val snappedDegrees = ((degrees + 45) / 90 * 90) % 360) { 59 0 -> Natural 60 90 -> Rotated90 61 180 -> Rotated180 62 270 -> Rotated270 63 else -> throw IllegalStateException( 64 "Unexpected snapped degrees: $snappedDegrees" + 65 ". Should be one of 0, 90, 180 or 270." 66 ) 67 } 68 } 69 } 70 } 71