1 /*
2  * Copyright (C) 2023 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 
17 package com.android.settingslib.devicestate
18 
19 import android.content.Context
20 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_FOLDED
21 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_HALF_FOLDED
22 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY
23 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_UNFOLDED
24 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_UNKNOWN
25 import android.provider.Settings.Secure.DeviceStateRotationLockKey
26 import com.android.internal.R
27 
28 /** Helps to convert between device state and posture. */
29 class PosturesHelper(context: Context) {
30 
31     private val foldedDeviceStates =
32         context.resources.getIntArray(R.array.config_foldedDeviceStates)
33     private val halfFoldedDeviceStates =
34         context.resources.getIntArray(R.array.config_halfFoldedDeviceStates)
35     private val unfoldedDeviceStates =
36         context.resources.getIntArray(R.array.config_openDeviceStates)
37     private val rearDisplayDeviceStates =
38         context.resources.getIntArray(R.array.config_rearDisplayDeviceStates)
39 
40     @DeviceStateRotationLockKey
deviceStateToPosturenull41     fun deviceStateToPosture(deviceState: Int): Int {
42         return when (deviceState) {
43             in foldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_FOLDED
44             in halfFoldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_HALF_FOLDED
45             in unfoldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_UNFOLDED
46             in rearDisplayDeviceStates -> DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY
47             else -> DEVICE_STATE_ROTATION_KEY_UNKNOWN
48         }
49     }
50 
postureToDeviceStatenull51     fun postureToDeviceState(@DeviceStateRotationLockKey posture: Int): Int? {
52         return when (posture) {
53             DEVICE_STATE_ROTATION_KEY_FOLDED -> foldedDeviceStates.firstOrNull()
54             DEVICE_STATE_ROTATION_KEY_HALF_FOLDED -> halfFoldedDeviceStates.firstOrNull()
55             DEVICE_STATE_ROTATION_KEY_UNFOLDED -> unfoldedDeviceStates.firstOrNull()
56             DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY -> rearDisplayDeviceStates.firstOrNull()
57             else -> null
58         }
59     }
60 }
61