Home | History | Annotate | Download | only in misc
      1 /*
      2  * Copyright (C) 2014 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.recents.misc;
     18 
     19 import android.os.Handler;
     20 import android.view.ViewDebug;
     21 
     22 /**
     23  * A dozer is a class that fires a trigger after it falls asleep.
     24  * You can occasionally poke the trigger to wake it up, but it will fall asleep if left untouched.
     25  */
     26 public class DozeTrigger {
     27 
     28     Handler mHandler;
     29 
     30     @ViewDebug.ExportedProperty(category="recents")
     31     boolean mIsDozing;
     32     @ViewDebug.ExportedProperty(category="recents")
     33     boolean mIsAsleep;
     34     @ViewDebug.ExportedProperty(category="recents")
     35     int mDozeDurationMilliseconds;
     36     Runnable mOnSleepRunnable;
     37 
     38     // Sleep-runnable
     39     Runnable mDozeRunnable = new Runnable() {
     40         @Override
     41         public void run() {
     42             mIsDozing = false;
     43             mIsAsleep = true;
     44             mOnSleepRunnable.run();
     45         }
     46     };
     47 
     48     public DozeTrigger(int dozeDurationMilliseconds, Runnable onSleepRunnable) {
     49         mHandler = new Handler();
     50         mDozeDurationMilliseconds = dozeDurationMilliseconds;
     51         mOnSleepRunnable = onSleepRunnable;
     52     }
     53 
     54     /**
     55      * Starts dozing and queues the onSleepRunnable to be called. This also resets the trigger flag.
     56      */
     57     public void startDozing() {
     58         forcePoke();
     59         mIsAsleep = false;
     60     }
     61 
     62     /**
     63      * Stops dozing and prevents the onSleepRunnable from being called.
     64      */
     65     public void stopDozing() {
     66         mHandler.removeCallbacks(mDozeRunnable);
     67         mIsDozing = false;
     68         mIsAsleep = false;
     69     }
     70 
     71     /**
     72      * Updates the duration that we have to wait until dozing triggers.
     73      */
     74     public void setDozeDuration(int duration) {
     75         mDozeDurationMilliseconds = duration;
     76     }
     77 
     78     /**
     79      * Poke this dozer to wake it up if it is dozing, delaying the onSleepRunnable from being
     80      * called for a for the doze duration.
     81      */
     82     public void poke() {
     83         if (mIsDozing) {
     84             forcePoke();
     85         }
     86     }
     87 
     88     /**
     89      * Poke this dozer to wake it up even if it is not currently dozing.
     90      */
     91     void forcePoke() {
     92         mHandler.removeCallbacks(mDozeRunnable);
     93         mHandler.postDelayed(mDozeRunnable, mDozeDurationMilliseconds);
     94         mIsDozing = true;
     95     }
     96 
     97     /** Returns whether we are dozing or not. */
     98     public boolean isDozing() {
     99         return mIsDozing;
    100     }
    101 
    102     /** Returns whether the trigger has fired at least once. */
    103     public boolean isAsleep() {
    104         return mIsAsleep;
    105     }
    106 }
    107