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 
     21 /**
     22  * A dozer is a class that fires a trigger after it falls asleep.  You can occasionally poke it to
     23  * wake it up, but it will fall asleep if left untouched.
     24  */
     25 public class DozeTrigger {
     26 
     27     Handler mHandler;
     28 
     29     boolean mIsDozing;
     30     boolean mHasTriggered;
     31     int mDozeDurationSeconds;
     32     Runnable mSleepRunnable;
     33 
     34     // Sleep-runnable
     35     Runnable mDozeRunnable = new Runnable() {
     36         @Override
     37         public void run() {
     38             mSleepRunnable.run();
     39             mIsDozing = false;
     40             mHasTriggered = true;
     41         }
     42     };
     43 
     44     public DozeTrigger(int dozeDurationSeconds, Runnable sleepRunnable) {
     45         mHandler = new Handler();
     46         mDozeDurationSeconds = dozeDurationSeconds;
     47         mSleepRunnable = sleepRunnable;
     48     }
     49 
     50     /** Starts dozing. This also resets the trigger flag. */
     51     public void startDozing() {
     52         forcePoke();
     53         mHasTriggered = false;
     54     }
     55 
     56     /** Stops dozing. */
     57     public void stopDozing() {
     58         mHandler.removeCallbacks(mDozeRunnable);
     59         mIsDozing = false;
     60     }
     61 
     62     /** Poke this dozer to wake it up for a little bit, if it is dozing. */
     63     public void poke() {
     64         if (mIsDozing) {
     65             forcePoke();
     66         }
     67     }
     68 
     69     /** Poke this dozer to wake it up for a little bit. */
     70     void forcePoke() {
     71         mHandler.removeCallbacks(mDozeRunnable);
     72         mHandler.postDelayed(mDozeRunnable, mDozeDurationSeconds * 1000);
     73         mIsDozing = true;
     74     }
     75 
     76     /** Returns whether we are dozing or not. */
     77     public boolean isDozing() {
     78         return mIsDozing;
     79     }
     80 
     81     /** Returns whether the trigger has fired at least once. */
     82     public boolean hasTriggered() {
     83         return mHasTriggered;
     84     }
     85 
     86     /** Resets the doze trigger state. */
     87     public void resetTrigger() {
     88         mHasTriggered = false;
     89     }
     90 }
     91