1 /*
2  * Copyright (C) 2021 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.android.systemui.statusbar.phone
17 
18 import android.content.Context
19 import android.hardware.devicestate.DeviceState
20 import android.hardware.devicestate.DeviceStateManager.DeviceStateCallback
21 import com.android.internal.R
22 
23 /**
24  * Listens for fold state changes and reports the new folded state together with other properties
25  * associated with that state.
26  */
27 internal class FoldStateListener(
28     context: Context,
29     private val listener: OnFoldStateChangeListener
30 ) : DeviceStateCallback {
31 
32     internal interface OnFoldStateChangeListener {
33         /**
34          * Reports that the device either became folded or unfolded.
35          *
36          * @param folded whether the device is folded.
37          * @param willGoToSleep whether the device will go to sleep and keep the screen off.
38          */
onFoldStateChangednull39         fun onFoldStateChanged(folded: Boolean, willGoToSleep: Boolean)
40     }
41 
42     private val foldedDeviceStates: IntArray =
43         context.resources.getIntArray(R.array.config_foldedDeviceStates)
44     private val goToSleepDeviceStates: IntArray =
45         context.resources.getIntArray(R.array.config_deviceStatesOnWhichToSleep)
46 
47     private var wasFolded: Boolean? = null
48 
49     override fun onDeviceStateChanged(state: DeviceState) {
50         val isFolded = foldedDeviceStates.contains(state.identifier)
51         if (wasFolded == isFolded) {
52             return
53         }
54         wasFolded = isFolded
55         val willGoToSleep = goToSleepDeviceStates.contains(state.identifier)
56         listener.onFoldStateChanged(isFolded, willGoToSleep)
57     }
58 }
59