2010-11-23 16:23:58 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2010-11-23 16:23:58 -08:00
|
|
|
|
|
|
|
|
import android.os.Handler;
|
2024-02-14 12:32:59 -08:00
|
|
|
import android.os.Looper;
|
2016-08-27 15:33:16 -07:00
|
|
|
import android.os.SystemClock;
|
2010-11-23 16:23:58 -08:00
|
|
|
|
2024-07-15 14:19:01 -04:00
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
|
|
|
|
2010-11-23 16:23:58 -08:00
|
|
|
public class Alarm implements Runnable{
|
|
|
|
|
// if we reach this time and the alarm hasn't been cancelled, call the listener
|
|
|
|
|
private long mAlarmTriggerTime;
|
|
|
|
|
|
|
|
|
|
// if we've scheduled a call to run() (ie called mHandler.postDelayed), this variable is true.
|
|
|
|
|
// We use this to avoid having multiple pending callbacks
|
|
|
|
|
private boolean mWaitingForCallback;
|
|
|
|
|
|
|
|
|
|
private Handler mHandler;
|
|
|
|
|
private OnAlarmListener mAlarmListener;
|
2011-07-29 14:07:04 -07:00
|
|
|
private boolean mAlarmPending = false;
|
2022-06-22 11:29:20 -07:00
|
|
|
private long mLastSetTimeout;
|
2010-11-23 16:23:58 -08:00
|
|
|
|
|
|
|
|
public Alarm() {
|
2024-02-14 12:32:59 -08:00
|
|
|
this(Looper.myLooper());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Alarm(Looper looper) {
|
|
|
|
|
mHandler = new Handler(looper);
|
2010-11-23 16:23:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setOnAlarmListener(OnAlarmListener alarmListener) {
|
|
|
|
|
mAlarmListener = alarmListener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sets the alarm to go off in a certain number of milliseconds. If the alarm is already set,
|
|
|
|
|
// it's overwritten and only the new alarm setting is used
|
|
|
|
|
public void setAlarm(long millisecondsInFuture) {
|
2016-08-27 15:33:16 -07:00
|
|
|
long currentTime = SystemClock.uptimeMillis();
|
2011-07-29 14:07:04 -07:00
|
|
|
mAlarmPending = true;
|
2016-08-27 15:33:16 -07:00
|
|
|
long oldTriggerTime = mAlarmTriggerTime;
|
2010-11-23 16:23:58 -08:00
|
|
|
mAlarmTriggerTime = currentTime + millisecondsInFuture;
|
2022-06-22 11:29:20 -07:00
|
|
|
mLastSetTimeout = millisecondsInFuture;
|
2016-08-27 15:33:16 -07:00
|
|
|
|
|
|
|
|
// If the previous alarm was set for a longer duration, cancel it.
|
|
|
|
|
if (mWaitingForCallback && oldTriggerTime > mAlarmTriggerTime) {
|
|
|
|
|
mHandler.removeCallbacks(this);
|
|
|
|
|
mWaitingForCallback = false;
|
|
|
|
|
}
|
2010-11-23 16:23:58 -08:00
|
|
|
if (!mWaitingForCallback) {
|
|
|
|
|
mHandler.postDelayed(this, mAlarmTriggerTime - currentTime);
|
|
|
|
|
mWaitingForCallback = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cancelAlarm() {
|
2011-07-29 14:07:04 -07:00
|
|
|
mAlarmPending = false;
|
2010-11-23 16:23:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this is called when our timer runs out
|
|
|
|
|
public void run() {
|
|
|
|
|
mWaitingForCallback = false;
|
2016-08-27 15:33:16 -07:00
|
|
|
if (mAlarmPending) {
|
|
|
|
|
long currentTime = SystemClock.uptimeMillis();
|
2010-11-23 16:23:58 -08:00
|
|
|
if (mAlarmTriggerTime > currentTime) {
|
|
|
|
|
// We still need to wait some time to trigger spring loaded mode--
|
|
|
|
|
// post a new callback
|
|
|
|
|
mHandler.postDelayed(this, Math.max(0, mAlarmTriggerTime - currentTime));
|
|
|
|
|
mWaitingForCallback = true;
|
|
|
|
|
} else {
|
2011-07-29 14:07:04 -07:00
|
|
|
mAlarmPending = false;
|
2010-11-23 16:23:58 -08:00
|
|
|
if (mAlarmListener != null) {
|
|
|
|
|
mAlarmListener.onAlarm(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-29 14:07:04 -07:00
|
|
|
|
|
|
|
|
public boolean alarmPending() {
|
|
|
|
|
return mAlarmPending;
|
|
|
|
|
}
|
2022-06-22 11:29:20 -07:00
|
|
|
|
|
|
|
|
/** Returns the last value passed to {@link #setAlarm(long)} */
|
|
|
|
|
public long getLastSetTimeout() {
|
|
|
|
|
return mLastSetTimeout;
|
|
|
|
|
}
|
2024-07-15 14:19:01 -04:00
|
|
|
|
|
|
|
|
/** Simulates the alarm firing for tests. */
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
public void finishAlarm() {
|
|
|
|
|
if (!mAlarmPending) return;
|
|
|
|
|
mAlarmPending = false;
|
|
|
|
|
mHandler.removeCallbacks(this);
|
|
|
|
|
mAlarmListener.onAlarm(this);
|
|
|
|
|
}
|
2010-11-23 16:23:58 -08:00
|
|
|
}
|