blob: d5b434c6472ea14dc9a37b300fd14a2172d6d27b [file] [log] [blame]
Michael Jurkad3ef3062010-11-23 16:23:58 -08001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Michael Jurkad3ef3062010-11-23 16:23:58 -080018
19import android.os.Handler;
Sunny Goyala2125e12016-08-27 15:33:16 -070020import android.os.SystemClock;
Michael Jurkad3ef3062010-11-23 16:23:58 -080021
22public class Alarm implements Runnable{
23 // if we reach this time and the alarm hasn't been cancelled, call the listener
24 private long mAlarmTriggerTime;
25
26 // if we've scheduled a call to run() (ie called mHandler.postDelayed), this variable is true.
27 // We use this to avoid having multiple pending callbacks
28 private boolean mWaitingForCallback;
29
30 private Handler mHandler;
31 private OnAlarmListener mAlarmListener;
Adam Cohen67bd9cc2011-07-29 14:07:04 -070032 private boolean mAlarmPending = false;
Michael Jurkad3ef3062010-11-23 16:23:58 -080033
34 public Alarm() {
35 mHandler = new Handler();
36 }
37
38 public void setOnAlarmListener(OnAlarmListener alarmListener) {
39 mAlarmListener = alarmListener;
40 }
41
42 // Sets the alarm to go off in a certain number of milliseconds. If the alarm is already set,
43 // it's overwritten and only the new alarm setting is used
44 public void setAlarm(long millisecondsInFuture) {
Sunny Goyala2125e12016-08-27 15:33:16 -070045 long currentTime = SystemClock.uptimeMillis();
Adam Cohen67bd9cc2011-07-29 14:07:04 -070046 mAlarmPending = true;
Sunny Goyala2125e12016-08-27 15:33:16 -070047 long oldTriggerTime = mAlarmTriggerTime;
Michael Jurkad3ef3062010-11-23 16:23:58 -080048 mAlarmTriggerTime = currentTime + millisecondsInFuture;
Sunny Goyala2125e12016-08-27 15:33:16 -070049
50 // If the previous alarm was set for a longer duration, cancel it.
51 if (mWaitingForCallback && oldTriggerTime > mAlarmTriggerTime) {
52 mHandler.removeCallbacks(this);
53 mWaitingForCallback = false;
54 }
Michael Jurkad3ef3062010-11-23 16:23:58 -080055 if (!mWaitingForCallback) {
56 mHandler.postDelayed(this, mAlarmTriggerTime - currentTime);
57 mWaitingForCallback = true;
58 }
59 }
60
61 public void cancelAlarm() {
Adam Cohen67bd9cc2011-07-29 14:07:04 -070062 mAlarmPending = false;
Michael Jurkad3ef3062010-11-23 16:23:58 -080063 }
64
65 // this is called when our timer runs out
66 public void run() {
67 mWaitingForCallback = false;
Sunny Goyala2125e12016-08-27 15:33:16 -070068 if (mAlarmPending) {
69 long currentTime = SystemClock.uptimeMillis();
Michael Jurkad3ef3062010-11-23 16:23:58 -080070 if (mAlarmTriggerTime > currentTime) {
71 // We still need to wait some time to trigger spring loaded mode--
72 // post a new callback
73 mHandler.postDelayed(this, Math.max(0, mAlarmTriggerTime - currentTime));
74 mWaitingForCallback = true;
75 } else {
Adam Cohen67bd9cc2011-07-29 14:07:04 -070076 mAlarmPending = false;
Michael Jurkad3ef3062010-11-23 16:23:58 -080077 if (mAlarmListener != null) {
78 mAlarmListener.onAlarm(this);
79 }
80 }
81 }
82 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -070083
84 public boolean alarmPending() {
85 return mAlarmPending;
86 }
Michael Jurkad3ef3062010-11-23 16:23:58 -080087}