WakefulnessLifecycle.java revision 51465ea3b667a4123baf7eebb8f590aeaec2aafe
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
17package com.android.systemui.keyguard;
18
19import android.os.Trace;
20
21import com.android.systemui.Dumpable;
22
23import java.io.FileDescriptor;
24import java.io.PrintWriter;
25
26/**
27 * Tracks the wakefulness lifecycle.
28 */
29public class WakefulnessLifecycle extends Lifecycle<WakefulnessLifecycle.Observer> implements
30        Dumpable {
31
32    public static final int WAKEFULNESS_ASLEEP = 0;
33    public static final int WAKEFULNESS_WAKING = 1;
34    public static final int WAKEFULNESS_AWAKE = 2;
35    public static final int WAKEFULNESS_GOING_TO_SLEEP = 3;
36
37    private int mWakefulness = WAKEFULNESS_ASLEEP;
38
39    public int getWakefulness() {
40        return mWakefulness;
41    }
42
43    public void dispatchStartedWakingUp() {
44        setWakefulness(WAKEFULNESS_WAKING);
45        dispatch(Observer::onStartedWakingUp);
46    }
47
48    public void dispatchFinishedWakingUp() {
49        setWakefulness(WAKEFULNESS_AWAKE);
50        dispatch(Observer::onFinishedWakingUp);
51    }
52
53    public void dispatchStartedGoingToSleep() {
54        setWakefulness(WAKEFULNESS_GOING_TO_SLEEP);
55        dispatch(Observer::onStartedGoingToSleep);
56    }
57
58    public void dispatchFinishedGoingToSleep() {
59        setWakefulness(WAKEFULNESS_ASLEEP);
60        dispatch(Observer::onFinishedGoingToSleep);
61    }
62
63    @Override
64    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
65        pw.println("WakefulnessLifecycle:");
66        pw.println("  mWakefulness=" + mWakefulness);
67    }
68
69    private void setWakefulness(int wakefulness) {
70        mWakefulness = wakefulness;
71        Trace.traceCounter(Trace.TRACE_TAG_APP, "wakefulness", wakefulness);
72    }
73
74    public interface Observer {
75        default void onStartedWakingUp() {}
76        default void onFinishedWakingUp() {}
77        default void onStartedGoingToSleep() {}
78        default void onFinishedGoingToSleep() {}
79    }
80}
81