Home | History | Annotate | Download | only in keyguard
      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 /**
     27  * Tracks the wakefulness lifecycle.
     28  */
     29 public 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         if (getWakefulness() == WAKEFULNESS_WAKING) {
     45             return;
     46         }
     47         setWakefulness(WAKEFULNESS_WAKING);
     48         dispatch(Observer::onStartedWakingUp);
     49     }
     50 
     51     public void dispatchFinishedWakingUp() {
     52         if (getWakefulness() == WAKEFULNESS_AWAKE) {
     53             return;
     54         }
     55         setWakefulness(WAKEFULNESS_AWAKE);
     56         dispatch(Observer::onFinishedWakingUp);
     57     }
     58 
     59     public void dispatchStartedGoingToSleep() {
     60         if (getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP) {
     61             return;
     62         }
     63         setWakefulness(WAKEFULNESS_GOING_TO_SLEEP);
     64         dispatch(Observer::onStartedGoingToSleep);
     65     }
     66 
     67     public void dispatchFinishedGoingToSleep() {
     68         if (getWakefulness() == WAKEFULNESS_ASLEEP) {
     69             return;
     70         }
     71         setWakefulness(WAKEFULNESS_ASLEEP);
     72         dispatch(Observer::onFinishedGoingToSleep);
     73     }
     74 
     75     @Override
     76     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
     77         pw.println("WakefulnessLifecycle:");
     78         pw.println("  mWakefulness=" + mWakefulness);
     79     }
     80 
     81     private void setWakefulness(int wakefulness) {
     82         mWakefulness = wakefulness;
     83         Trace.traceCounter(Trace.TRACE_TAG_APP, "wakefulness", wakefulness);
     84     }
     85 
     86     public interface Observer {
     87         default void onStartedWakingUp() {}
     88         default void onFinishedWakingUp() {}
     89         default void onStartedGoingToSleep() {}
     90         default void onFinishedGoingToSleep() {}
     91     }
     92 }
     93