blob: fb8088c13b921937275868a370853484856ce022 [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 Goyal72a74662024-02-14 12:32:59 -080020import android.os.Looper;
Sunny Goyala2125e12016-08-27 15:33:16 -070021import android.os.SystemClock;
Michael Jurkad3ef3062010-11-23 16:23:58 -080022
23public 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;
Adam Cohen67bd9cc2011-07-29 14:07:04 -070033 private boolean mAlarmPending = false;
Tony Wickhamc2143352022-06-22 11:29:20 -070034 private long mLastSetTimeout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080035
36 public Alarm() {
Sunny Goyal72a74662024-02-14 12:32:59 -080037 this(Looper.myLooper());
38 }
39
40 public Alarm(Looper looper) {
41 mHandler = new Handler(looper);
Michael Jurkad3ef3062010-11-23 16:23:58 -080042 }
43
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
50 public void setAlarm(long millisecondsInFuture) {
Sunny Goyala2125e12016-08-27 15:33:16 -070051 long currentTime = SystemClock.uptimeMillis();
Adam Cohen67bd9cc2011-07-29 14:07:04 -070052 mAlarmPending = true;
Sunny Goyala2125e12016-08-27 15:33:16 -070053 long oldTriggerTime = mAlarmTriggerTime;
Michael Jurkad3ef3062010-11-23 16:23:58 -080054 mAlarmTriggerTime = currentTime + millisecondsInFuture;
Tony Wickhamc2143352022-06-22 11:29:20 -070055 mLastSetTimeout = millisecondsInFuture;
Sunny Goyala2125e12016-08-27 15:33:16 -070056
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 }
Michael Jurkad3ef3062010-11-23 16:23:58 -080062 if (!mWaitingForCallback) {
63 mHandler.postDelayed(this, mAlarmTriggerTime - currentTime);
64 mWaitingForCallback = true;
65 }
66 }
67
68 public void cancelAlarm() {
Adam Cohen67bd9cc2011-07-29 14:07:04 -070069 mAlarmPending = false;
Michael Jurkad3ef3062010-11-23 16:23:58 -080070 }
71
72 // this is called when our timer runs out
73 public void run() {
74 mWaitingForCallback = false;
Sunny Goyala2125e12016-08-27 15:33:16 -070075 if (mAlarmPending) {
76 long currentTime = SystemClock.uptimeMillis();
Michael Jurkad3ef3062010-11-23 16:23:58 -080077 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 {
Adam Cohen67bd9cc2011-07-29 14:07:04 -070083 mAlarmPending = false;
Michael Jurkad3ef3062010-11-23 16:23:58 -080084 if (mAlarmListener != null) {
85 mAlarmListener.onAlarm(this);
86 }
87 }
88 }
89 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -070090
91 public boolean alarmPending() {
92 return mAlarmPending;
93 }
Tony Wickhamc2143352022-06-22 11:29:20 -070094
95 /** Returns the last value passed to {@link #setAlarm(long)} */
96 public long getLastSetTimeout() {
97 return mLastSetTimeout;
98 }
Michael Jurkad3ef3062010-11-23 16:23:58 -080099}