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.systemui.statusbar.policy
18 
19 import android.content.Context
20 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_IGNORED
21 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_LOCKED
22 import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_UNLOCKED
23 import com.android.internal.R
24 import com.android.systemui.log.LogBuffer
25 import com.android.systemui.log.core.LogLevel.VERBOSE
26 import com.android.systemui.log.dagger.DeviceStateAutoRotationLog
27 import javax.inject.Inject
28 
29 class DeviceStateRotationLockSettingControllerLogger
30 @Inject
31 constructor(@DeviceStateAutoRotationLog private val logBuffer: LogBuffer, context: Context) {
32 
33     private val foldedStates = context.resources.getIntArray(R.array.config_foldedDeviceStates)
34     private val halfFoldedStates =
35         context.resources.getIntArray(R.array.config_halfFoldedDeviceStates)
36     private val unfoldedStates = context.resources.getIntArray(R.array.config_openDeviceStates)
37     private val rearDisplayStates =
38         context.resources.getIntArray(R.array.config_rearDisplayDeviceStates)
39 
logListeningChangenull40     fun logListeningChange(listening: Boolean) {
41         logBuffer.log(TAG, VERBOSE, { bool1 = listening }, { "setListening: $bool1" })
42     }
43 
logRotationLockStateChangednull44     fun logRotationLockStateChanged(
45         state: Int,
46         newRotationLocked: Boolean,
47         currentRotationLocked: Boolean
48     ) {
49         logBuffer.log(
50             TAG,
51             VERBOSE,
52             {
53                 int1 = state
54                 bool1 = newRotationLocked
55                 bool2 = currentRotationLocked
56             },
57             {
58                 "onRotationLockStateChanged: " +
59                     "state=$int1 [${int1.toDevicePostureString()}], " +
60                     "newRotationLocked=$bool1, " +
61                     "currentRotationLocked=$bool2"
62             }
63         )
64     }
65 
logSaveNewRotationLockSettingnull66     fun logSaveNewRotationLockSetting(isRotationLocked: Boolean, state: Int) {
67         logBuffer.log(
68             TAG,
69             VERBOSE,
70             {
71                 bool1 = isRotationLocked
72                 int1 = state
73             },
74             { "saveNewRotationLockSetting: isRotationLocked=$bool1, state=$int1" }
75         )
76     }
77 
logUpdateDeviceStatenull78     fun logUpdateDeviceState(currentState: Int, newState: Int) {
79         logBuffer.log(
80             TAG,
81             VERBOSE,
82             {
83                 int1 = currentState
84                 int2 = newState
85             },
86             {
87                 "updateDeviceState: " +
88                     "current=$int1 [${int1.toDevicePostureString()}], " +
89                     "new=$int2 [${int2.toDevicePostureString()}]"
90             }
91         )
92     }
93 
readPersistedSettingnull94     fun readPersistedSetting(
95         caller: String,
96         state: Int,
97         rotationLockSetting: Int,
98         shouldBeLocked: Boolean,
99         isLocked: Boolean
100     ) {
101         logBuffer.log(
102             TAG,
103             VERBOSE,
104             {
105                 str1 = caller
106                 int1 = state
107                 int2 = rotationLockSetting
108                 bool1 = shouldBeLocked
109                 bool2 = isLocked
110             },
111             {
112                 "readPersistedSetting: " +
113                     "caller=$str1, " +
114                     "state=$int1 [${int1.toDevicePostureString()}], " +
115                     "rotationLockSettingForState: ${int2.toRotationLockSettingString()}, " +
116                     "shouldBeLocked=$bool1, " +
117                     "isLocked=$bool2"
118             }
119         )
120     }
121 
toDevicePostureStringnull122     private fun Int.toDevicePostureString(): String {
123         return when (this) {
124             in foldedStates -> "Folded"
125             in unfoldedStates -> "Unfolded"
126             in halfFoldedStates -> "Half-Folded"
127             in rearDisplayStates -> "Rear display"
128             -1 -> "Uninitialized"
129             else -> "Unknown"
130         }
131     }
132 }
133 
toRotationLockSettingStringnull134 private fun Int.toRotationLockSettingString(): String {
135     return when (this) {
136         DEVICE_STATE_ROTATION_LOCK_IGNORED -> "IGNORED"
137         DEVICE_STATE_ROTATION_LOCK_LOCKED -> "LOCKED"
138         DEVICE_STATE_ROTATION_LOCK_UNLOCKED -> "UNLOCKED"
139         else -> "Unknown"
140     }
141 }
142 
143 private const val TAG = "DSRotateLockSettingCon"
144