1 package com.android.systemui.power.shared.model
2 
3 import com.android.systemui.keyguard.KeyguardService
4 
5 /**
6  * Models whether the device is awake or asleep, along with information about why we're in that
7  * state.
8  */
9 data class WakefulnessModel(
10     /**
11      * Internal-only wakefulness state, which we receive via [KeyguardService]. This is a more
12      * granular state that tells us whether we've started or finished waking up or going to sleep.
13      *
14      * This distinction has historically been confusing - the display is on once we've "finished"
15      * waking up, but we're still playing screen-on animations. Similarly, the screen off animation
16      * is still playing even once we've "finished" going to sleep.
17      *
18      * Avoid using this whenever possible - [isAwake] and [isAsleep] should be sufficient for nearly
19      * all use cases. If you need more granular information about a waking/sleeping transition, use
20      * the [KeyguardTransitionInteractor].
21      */
22     internal val internalWakefulnessState: WakefulnessState = WakefulnessState.AWAKE,
23     val lastWakeReason: WakeSleepReason = WakeSleepReason.OTHER,
24     val lastSleepReason: WakeSleepReason = WakeSleepReason.OTHER,
25 
26     /**
27      * Whether the power button double tap gesture was triggered since the last time went to sleep.
28      * If this value is true while [isAsleep]=true, it means we'll be waking back up shortly. If it
29      * is true while [isAwake]=true, it means we're awake because of the button gesture.
30      *
31      * This value remains true until the next time [isAsleep]=true, since it would otherwise be
32      * totally arbitrary at what point we decide the gesture was no longer "triggered". Since a
33      * sleep event is guaranteed to arrive prior to the next power button gesture (as the first tap
34      * of the double tap always begins a sleep transition), this will always be reset to false prior
35      * to a subsequent power gesture.
36      */
37     val powerButtonLaunchGestureTriggered: Boolean = false,
38 ) {
isAwakenull39     fun isAwake() =
40         internalWakefulnessState == WakefulnessState.AWAKE ||
41             internalWakefulnessState == WakefulnessState.STARTING_TO_WAKE
42 
43     fun isAsleep() = !isAwake()
44 
45     fun isAwakeFrom(wakeSleepReason: WakeSleepReason) =
46         isAwake() && lastWakeReason == wakeSleepReason
47 
48     fun isAwakeFromTouch(): Boolean {
49         return isAwake() && lastWakeReason.isTouch
50     }
51 
isAsleepFromnull52     fun isAsleepFrom(wakeSleepReason: WakeSleepReason) =
53         isAsleep() && lastSleepReason == wakeSleepReason
54 
55     fun isAwakeOrAsleepFrom(reason: WakeSleepReason) = isAsleepFrom(reason) || isAwakeFrom(reason)
56 
57     fun isAwakeFromTapOrGesture(): Boolean {
58         return isAwake() &&
59             (lastWakeReason == WakeSleepReason.TAP || lastWakeReason == WakeSleepReason.GESTURE)
60     }
61 }
62