1 /*
2  * Copyright (C) 2017 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.keyguard;
18 
19 import android.annotation.IntDef;
20 import android.os.Trace;
21 
22 import com.android.systemui.Dumpable;
23 
24 import java.io.FileDescriptor;
25 import java.io.PrintWriter;
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 import javax.inject.Inject;
30 import javax.inject.Singleton;
31 
32 /**
33  * Tracks the wakefulness lifecycle.
34  */
35 @Singleton
36 public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observer> implements
37         Dumpable {
38 
39     @IntDef(prefix = { "WAKEFULNESS_" }, value = {
40             WAKEFULNESS_ASLEEP,
41             WAKEFULNESS_WAKING,
42             WAKEFULNESS_AWAKE,
43             WAKEFULNESS_GOING_TO_SLEEP,
44     })
45     @Retention(RetentionPolicy.SOURCE)
46     public @interface Wakefulness {}
47 
48     public static final int WAKEFULNESS_ASLEEP = 0;
49     public static final int WAKEFULNESS_WAKING = 1;
50     public static final int WAKEFULNESS_AWAKE = 2;
51     public static final int WAKEFULNESS_GOING_TO_SLEEP = 3;
52 
53     private int mWakefulness = WAKEFULNESS_ASLEEP;
54 
55     @Inject
WakefulnessLifecycle()56     public WakefulnessLifecycle() {
57     }
58 
getWakefulness()59     public @Wakefulness int getWakefulness() {
60         return mWakefulness;
61     }
62 
dispatchStartedWakingUp()63     public void dispatchStartedWakingUp() {
64         if (getWakefulness() == WAKEFULNESS_WAKING) {
65             return;
66         }
67         setWakefulness(WAKEFULNESS_WAKING);
68         dispatch(Observer::onStartedWakingUp);
69     }
70 
dispatchFinishedWakingUp()71     public void dispatchFinishedWakingUp() {
72         if (getWakefulness() == WAKEFULNESS_AWAKE) {
73             return;
74         }
75         setWakefulness(WAKEFULNESS_AWAKE);
76         dispatch(Observer::onFinishedWakingUp);
77     }
78 
dispatchStartedGoingToSleep()79     public void dispatchStartedGoingToSleep() {
80         if (getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP) {
81             return;
82         }
83         setWakefulness(WAKEFULNESS_GOING_TO_SLEEP);
84         dispatch(Observer::onStartedGoingToSleep);
85     }
86 
dispatchFinishedGoingToSleep()87     public void dispatchFinishedGoingToSleep() {
88         if (getWakefulness() == WAKEFULNESS_ASLEEP) {
89             return;
90         }
91         setWakefulness(WAKEFULNESS_ASLEEP);
92         dispatch(Observer::onFinishedGoingToSleep);
93     }
94 
95     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)96     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
97         pw.println("WakefulnessLifecycle:");
98         pw.println("  mWakefulness=" + mWakefulness);
99     }
100 
setWakefulness(@akefulness int wakefulness)101     private void setWakefulness(@Wakefulness int wakefulness) {
102         mWakefulness = wakefulness;
103         Trace.traceCounter(Trace.TRACE_TAG_APP, "wakefulness", wakefulness);
104     }
105 
106     public interface Observer {
onStartedWakingUp()107         default void onStartedWakingUp() {}
onFinishedWakingUp()108         default void onFinishedWakingUp() {}
onStartedGoingToSleep()109         default void onStartedGoingToSleep() {}
onFinishedGoingToSleep()110         default void onFinishedGoingToSleep() {}
111     }
112 }
113