1 /*
2  * Copyright (C) 2010 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.launcher3;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 import android.os.SystemClock;
22 
23 public class Alarm implements Runnable{
24     // if we reach this time and the alarm hasn't been cancelled, call the listener
25     private long mAlarmTriggerTime;
26 
27     // if we've scheduled a call to run() (ie called mHandler.postDelayed), this variable is true.
28     // We use this to avoid having multiple pending callbacks
29     private boolean mWaitingForCallback;
30 
31     private Handler mHandler;
32     private OnAlarmListener mAlarmListener;
33     private boolean mAlarmPending = false;
34     private long mLastSetTimeout;
35 
Alarm()36     public Alarm() {
37         this(Looper.myLooper());
38     }
39 
Alarm(Looper looper)40     public Alarm(Looper looper) {
41         mHandler = new Handler(looper);
42     }
43 
setOnAlarmListener(OnAlarmListener alarmListener)44     public void setOnAlarmListener(OnAlarmListener alarmListener) {
45         mAlarmListener = alarmListener;
46     }
47 
48     // Sets the alarm to go off in a certain number of milliseconds. If the alarm is already set,
49     // it's overwritten and only the new alarm setting is used
setAlarm(long millisecondsInFuture)50     public void setAlarm(long millisecondsInFuture) {
51         long currentTime = SystemClock.uptimeMillis();
52         mAlarmPending = true;
53         long oldTriggerTime = mAlarmTriggerTime;
54         mAlarmTriggerTime = currentTime + millisecondsInFuture;
55         mLastSetTimeout = millisecondsInFuture;
56 
57         // If the previous alarm was set for a longer duration, cancel it.
58         if (mWaitingForCallback && oldTriggerTime > mAlarmTriggerTime) {
59             mHandler.removeCallbacks(this);
60             mWaitingForCallback = false;
61         }
62         if (!mWaitingForCallback) {
63             mHandler.postDelayed(this, mAlarmTriggerTime - currentTime);
64             mWaitingForCallback = true;
65         }
66     }
67 
cancelAlarm()68     public void cancelAlarm() {
69         mAlarmPending = false;
70     }
71 
72     // this is called when our timer runs out
run()73     public void run() {
74         mWaitingForCallback = false;
75         if (mAlarmPending) {
76             long currentTime = SystemClock.uptimeMillis();
77             if (mAlarmTriggerTime > currentTime) {
78                 // We still need to wait some time to trigger spring loaded mode--
79                 // post a new callback
80                 mHandler.postDelayed(this, Math.max(0, mAlarmTriggerTime - currentTime));
81                 mWaitingForCallback = true;
82             } else {
83                 mAlarmPending = false;
84                 if (mAlarmListener != null) {
85                     mAlarmListener.onAlarm(this);
86                 }
87             }
88         }
89     }
90 
alarmPending()91     public boolean alarmPending() {
92         return mAlarmPending;
93     }
94 
95     /** Returns the last value passed to {@link #setAlarm(long)} */
getLastSetTimeout()96     public long getLastSetTimeout() {
97         return mLastSetTimeout;
98     }
99 }
100