blob: d17f2f621ec82e6c3a2e015f75ba51bf7a9a2d81 [file] [log] [blame]
Adrian Roos369907f2017-07-14 14:53:39 +02001/*
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
17package com.android.systemui.keyguard;
18
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070019import android.annotation.IntDef;
Adrian Roos51465ea2017-07-27 13:57:35 +020020import android.os.Trace;
21
Adrian Roos369907f2017-07-14 14:53:39 +020022import com.android.systemui.Dumpable;
23
24import java.io.FileDescriptor;
25import java.io.PrintWriter;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070026import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
Adrian Roos369907f2017-07-14 14:53:39 +020028
Jason Monk196d6392018-12-20 13:25:34 -050029import javax.inject.Inject;
30import javax.inject.Singleton;
31
Adrian Roos369907f2017-07-14 14:53:39 +020032/**
33 * Tracks the wakefulness lifecycle.
34 */
Jason Monk196d6392018-12-20 13:25:34 -050035@Singleton
Adrian Roos369907f2017-07-14 14:53:39 +020036public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observer> implements
37 Dumpable {
38
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070039 @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
Adrian Roos369907f2017-07-14 14:53:39 +020048 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
Jason Monk196d6392018-12-20 13:25:34 -050055 @Inject
56 public WakefulnessLifecycle() {
57 }
58
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070059 public @Wakefulness int getWakefulness() {
Adrian Roos369907f2017-07-14 14:53:39 +020060 return mWakefulness;
61 }
62
63 public void dispatchStartedWakingUp() {
Lucas Dupin16cfe452018-02-08 13:14:50 -080064 if (getWakefulness() == WAKEFULNESS_WAKING) {
65 return;
66 }
Adrian Roos51465ea2017-07-27 13:57:35 +020067 setWakefulness(WAKEFULNESS_WAKING);
Adrian Roos369907f2017-07-14 14:53:39 +020068 dispatch(Observer::onStartedWakingUp);
69 }
70
71 public void dispatchFinishedWakingUp() {
Lucas Dupin16cfe452018-02-08 13:14:50 -080072 if (getWakefulness() == WAKEFULNESS_AWAKE) {
73 return;
74 }
Adrian Roos51465ea2017-07-27 13:57:35 +020075 setWakefulness(WAKEFULNESS_AWAKE);
Adrian Roos369907f2017-07-14 14:53:39 +020076 dispatch(Observer::onFinishedWakingUp);
77 }
78
79 public void dispatchStartedGoingToSleep() {
Lucas Dupin16cfe452018-02-08 13:14:50 -080080 if (getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP) {
81 return;
82 }
Adrian Roos51465ea2017-07-27 13:57:35 +020083 setWakefulness(WAKEFULNESS_GOING_TO_SLEEP);
Adrian Roos369907f2017-07-14 14:53:39 +020084 dispatch(Observer::onStartedGoingToSleep);
85 }
86
87 public void dispatchFinishedGoingToSleep() {
Lucas Dupin16cfe452018-02-08 13:14:50 -080088 if (getWakefulness() == WAKEFULNESS_ASLEEP) {
89 return;
90 }
Adrian Roos51465ea2017-07-27 13:57:35 +020091 setWakefulness(WAKEFULNESS_ASLEEP);
Adrian Roos369907f2017-07-14 14:53:39 +020092 dispatch(Observer::onFinishedGoingToSleep);
93 }
94
95 @Override
96 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
97 pw.println("WakefulnessLifecycle:");
98 pw.println(" mWakefulness=" + mWakefulness);
99 }
100
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -0700101 private void setWakefulness(@Wakefulness int wakefulness) {
Adrian Roos51465ea2017-07-27 13:57:35 +0200102 mWakefulness = wakefulness;
103 Trace.traceCounter(Trace.TRACE_TAG_APP, "wakefulness", wakefulness);
104 }
105
Adrian Roos369907f2017-07-14 14:53:39 +0200106 public interface Observer {
107 default void onStartedWakingUp() {}
108 default void onFinishedWakingUp() {}
109 default void onStartedGoingToSleep() {}
110 default void onFinishedGoingToSleep() {}
111 }
112}