Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 18 | |
| 19 | import android.os.Handler; |
Sunny Goyal | a2125e1 | 2016-08-27 15:33:16 -0700 | [diff] [blame] | 20 | import android.os.SystemClock; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 21 | |
| 22 | public 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 Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 32 | private boolean mAlarmPending = false; |
Tony Wickham | c214335 | 2022-06-22 11:29:20 -0700 | [diff] [blame] | 33 | private long mLastSetTimeout; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 34 | |
| 35 | public Alarm() { |
| 36 | mHandler = new Handler(); |
| 37 | } |
| 38 | |
| 39 | public void setOnAlarmListener(OnAlarmListener alarmListener) { |
| 40 | mAlarmListener = alarmListener; |
| 41 | } |
| 42 | |
| 43 | // Sets the alarm to go off in a certain number of milliseconds. If the alarm is already set, |
| 44 | // it's overwritten and only the new alarm setting is used |
| 45 | public void setAlarm(long millisecondsInFuture) { |
Sunny Goyal | a2125e1 | 2016-08-27 15:33:16 -0700 | [diff] [blame] | 46 | long currentTime = SystemClock.uptimeMillis(); |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 47 | mAlarmPending = true; |
Sunny Goyal | a2125e1 | 2016-08-27 15:33:16 -0700 | [diff] [blame] | 48 | long oldTriggerTime = mAlarmTriggerTime; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 49 | mAlarmTriggerTime = currentTime + millisecondsInFuture; |
Tony Wickham | c214335 | 2022-06-22 11:29:20 -0700 | [diff] [blame] | 50 | mLastSetTimeout = millisecondsInFuture; |
Sunny Goyal | a2125e1 | 2016-08-27 15:33:16 -0700 | [diff] [blame] | 51 | |
| 52 | // If the previous alarm was set for a longer duration, cancel it. |
| 53 | if (mWaitingForCallback && oldTriggerTime > mAlarmTriggerTime) { |
| 54 | mHandler.removeCallbacks(this); |
| 55 | mWaitingForCallback = false; |
| 56 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 57 | if (!mWaitingForCallback) { |
| 58 | mHandler.postDelayed(this, mAlarmTriggerTime - currentTime); |
| 59 | mWaitingForCallback = true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public void cancelAlarm() { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 64 | mAlarmPending = false; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // this is called when our timer runs out |
| 68 | public void run() { |
| 69 | mWaitingForCallback = false; |
Sunny Goyal | a2125e1 | 2016-08-27 15:33:16 -0700 | [diff] [blame] | 70 | if (mAlarmPending) { |
| 71 | long currentTime = SystemClock.uptimeMillis(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 72 | if (mAlarmTriggerTime > currentTime) { |
| 73 | // We still need to wait some time to trigger spring loaded mode-- |
| 74 | // post a new callback |
| 75 | mHandler.postDelayed(this, Math.max(0, mAlarmTriggerTime - currentTime)); |
| 76 | mWaitingForCallback = true; |
| 77 | } else { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 78 | mAlarmPending = false; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 79 | if (mAlarmListener != null) { |
| 80 | mAlarmListener.onAlarm(this); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 85 | |
| 86 | public boolean alarmPending() { |
| 87 | return mAlarmPending; |
| 88 | } |
Tony Wickham | c214335 | 2022-06-22 11:29:20 -0700 | [diff] [blame] | 89 | |
| 90 | /** Returns the last value passed to {@link #setAlarm(long)} */ |
| 91 | public long getLastSetTimeout() { |
| 92 | return mLastSetTimeout; |
| 93 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 94 | } |