1 /*
2  * Copyright (C) 2022 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.keyguard.shared.model
17 
18 import com.android.systemui.keyguard.WakefulnessLifecycle
19 import com.android.systemui.keyguard.shared.model.WakeSleepReason.GESTURE
20 import com.android.systemui.keyguard.shared.model.WakeSleepReason.POWER_BUTTON
21 import com.android.systemui.keyguard.shared.model.WakeSleepReason.TAP
22 import com.android.systemui.keyguard.shared.model.WakefulnessState.ASLEEP
23 import com.android.systemui.keyguard.shared.model.WakefulnessState.AWAKE
24 import com.android.systemui.keyguard.shared.model.WakefulnessState.STARTING_TO_SLEEP
25 import com.android.systemui.keyguard.shared.model.WakefulnessState.STARTING_TO_WAKE
26 
27 /** Model device wakefulness states. */
28 data class WakefulnessModel(
29     val state: WakefulnessState,
30     val lastWakeReason: WakeSleepReason,
31     val lastSleepReason: WakeSleepReason,
32 ) {
33     fun isStartingToWake() = state == STARTING_TO_WAKE
34 
35     fun isStartingToSleep() = state == STARTING_TO_SLEEP
36 
37     private fun isAsleep() = state == ASLEEP
38 
39     private fun isAwake() = state == AWAKE
40 
41     fun isStartingToWakeOrAwake() = isStartingToWake() || isAwake()
42 
43     fun isStartingToSleepOrAsleep() = isStartingToSleep() || isAsleep()
44 
45     fun isDeviceInteractive() = !isAsleep()
46 
47     fun isWakingFrom(wakeSleepReason: WakeSleepReason) =
48         isStartingToWake() && lastWakeReason == wakeSleepReason
49 
50     fun isStartingToSleepFrom(wakeSleepReason: WakeSleepReason) =
51         isStartingToSleep() && lastSleepReason == wakeSleepReason
52 
53     fun isTransitioningFromPowerButton() =
54         isStartingToSleepFrom(POWER_BUTTON) || isWakingFrom(POWER_BUTTON)
55 
56     fun isDeviceInteractiveFromTapOrGesture(): Boolean {
57         return isDeviceInteractive() && (lastWakeReason == TAP || lastWakeReason == GESTURE)
58     }
59 
60     companion object {
61         fun fromWakefulnessLifecycle(wakefulnessLifecycle: WakefulnessLifecycle): WakefulnessModel {
62             return WakefulnessModel(
63                 WakefulnessState.fromWakefulnessLifecycleInt(wakefulnessLifecycle.wakefulness),
64                 WakeSleepReason.fromPowerManagerWakeReason(wakefulnessLifecycle.lastWakeReason),
65                 WakeSleepReason.fromPowerManagerSleepReason(wakefulnessLifecycle.lastSleepReason),
66             )
67         }
68     }
69 }
70