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