• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UNFOLDED
23 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_UNKNOWN
24 import android.provider.Settings.Secure.DeviceStateRotationLockKey
25 import com.android.internal.R
26 
27 /** Helps to convert between device state and posture. */
28 class PosturesHelper(context: Context) {
29 
30     private val foldedDeviceStates =
31         context.resources.getIntArray(R.array.config_foldedDeviceStates)
32     private val halfFoldedDeviceStates =
33         context.resources.getIntArray(R.array.config_halfFoldedDeviceStates)
34     private val unfoldedDeviceStates =
35         context.resources.getIntArray(R.array.config_openDeviceStates)
36 
37     @DeviceStateRotationLockKey
deviceStateToPosturenull38     fun deviceStateToPosture(deviceState: Int): Int {
39         return when (deviceState) {
40             in foldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_FOLDED
41             in halfFoldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_HALF_FOLDED
42             in unfoldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_UNFOLDED
43             else -> DEVICE_STATE_ROTATION_KEY_UNKNOWN
44         }
45     }
46 
postureToDeviceStatenull47     fun postureToDeviceState(@DeviceStateRotationLockKey posture: Int): Int? {
48         return when (posture) {
49             DEVICE_STATE_ROTATION_KEY_FOLDED -> foldedDeviceStates.firstOrNull()
50             DEVICE_STATE_ROTATION_KEY_HALF_FOLDED -> halfFoldedDeviceStates.firstOrNull()
51             DEVICE_STATE_ROTATION_KEY_UNFOLDED -> unfoldedDeviceStates.firstOrNull()
52             else -> null
53         }
54     }
55 }
56