The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.app; |
| 18 | |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Christopher Tate | 8d3079f | 2017-06-05 18:13:52 -0700 | [diff] [blame] | 20 | import android.annotation.RequiresPermission; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 21 | import android.annotation.SdkConstant; |
David Christie | c20b795 | 2014-09-04 11:29:01 -0700 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 23 | import android.annotation.SystemService; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 24 | import android.annotation.UnsupportedAppUsage; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 25 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.Intent; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 27 | import android.os.Build; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 32 | import android.os.WorkSource; |
Narayan Kamath | a78240b | 2015-04-24 13:22:03 +0100 | [diff] [blame] | 33 | import android.text.TextUtils; |
Christopher Tate | 09d7d8f | 2016-05-31 15:08:04 -0700 | [diff] [blame] | 34 | import android.util.ArrayMap; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 35 | import android.util.Log; |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 36 | import android.util.proto.ProtoOutputStream; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 37 | |
Neil Fuller | b8383a1 | 2018-11-16 21:46:33 +0000 | [diff] [blame] | 38 | import libcore.timezone.ZoneInfoDB; |
Narayan Kamath | a78240b | 2015-04-24 13:22:03 +0100 | [diff] [blame] | 39 | |
| 40 | import java.io.IOException; |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 41 | import java.lang.annotation.Retention; |
| 42 | import java.lang.annotation.RetentionPolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * This class provides access to the system alarm services. These allow you |
| 46 | * to schedule your application to be run at some point in the future. When |
| 47 | * an alarm goes off, the {@link Intent} that had been registered for it |
| 48 | * is broadcast by the system, automatically starting the target application |
| 49 | * if it is not already running. Registered alarms are retained while the |
| 50 | * device is asleep (and can optionally wake the device up if they go off |
| 51 | * during that time), but will be cleared if it is turned off and rebooted. |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 52 | * |
Chris Tate | a34df8a2 | 2009-04-02 23:15:58 -0700 | [diff] [blame] | 53 | * <p>The Alarm Manager holds a CPU wake lock as long as the alarm receiver's |
| 54 | * onReceive() method is executing. This guarantees that the phone will not sleep |
| 55 | * until you have finished handling the broadcast. Once onReceive() returns, the |
| 56 | * Alarm Manager releases this wake lock. This means that the phone will in some |
| 57 | * cases sleep as soon as your onReceive() method completes. If your alarm receiver |
| 58 | * called {@link android.content.Context#startService Context.startService()}, it |
| 59 | * is possible that the phone will sleep before the requested service is launched. |
| 60 | * To prevent this, your BroadcastReceiver and Service will need to implement a |
| 61 | * separate wake lock policy to ensure that the phone continues running until the |
| 62 | * service becomes available. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | * |
| 64 | * <p><b>Note: The Alarm Manager is intended for cases where you want to have |
| 65 | * your application code run at a specific time, even if your application is |
| 66 | * not currently running. For normal timing operations (ticks, timeouts, |
| 67 | * etc) it is easier and much more efficient to use |
| 68 | * {@link android.os.Handler}.</b> |
| 69 | * |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 70 | * <p class="caution"><strong>Note:</strong> Beginning with API 19 |
| 71 | * ({@link android.os.Build.VERSION_CODES#KITKAT}) alarm delivery is inexact: |
| 72 | * the OS will shift alarms in order to minimize wakeups and battery use. There are |
| 73 | * new APIs to support applications which need strict delivery guarantees; see |
| 74 | * {@link #setWindow(int, long, long, PendingIntent)} and |
| 75 | * {@link #setExact(int, long, PendingIntent)}. Applications whose {@code targetSdkVersion} |
| 76 | * is earlier than API 19 will continue to see the previous behavior in which all |
| 77 | * alarms are delivered exactly when requested. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 79 | @SystemService(Context.ALARM_SERVICE) |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 80 | public class AlarmManager { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 81 | private static final String TAG = "AlarmManager"; |
| 82 | |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 83 | /** @hide */ |
| 84 | @IntDef(prefix = { "RTC", "ELAPSED" }, value = { |
| 85 | RTC_WAKEUP, |
| 86 | RTC, |
| 87 | ELAPSED_REALTIME_WAKEUP, |
| 88 | ELAPSED_REALTIME, |
| 89 | }) |
| 90 | @Retention(RetentionPolicy.SOURCE) |
| 91 | public @interface AlarmType {} |
| 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | /** |
| 94 | * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()} |
| 95 | * (wall clock time in UTC), which will wake up the device when |
| 96 | * it goes off. |
| 97 | */ |
| 98 | public static final int RTC_WAKEUP = 0; |
| 99 | /** |
| 100 | * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()} |
| 101 | * (wall clock time in UTC). This alarm does not wake the |
| 102 | * device up; if it goes off while the device is asleep, it will not be |
| 103 | * delivered until the next time the device wakes up. |
| 104 | */ |
| 105 | public static final int RTC = 1; |
| 106 | /** |
| 107 | * Alarm time in {@link android.os.SystemClock#elapsedRealtime |
| 108 | * SystemClock.elapsedRealtime()} (time since boot, including sleep), |
| 109 | * which will wake up the device when it goes off. |
| 110 | */ |
| 111 | public static final int ELAPSED_REALTIME_WAKEUP = 2; |
| 112 | /** |
| 113 | * Alarm time in {@link android.os.SystemClock#elapsedRealtime |
| 114 | * SystemClock.elapsedRealtime()} (time since boot, including sleep). |
| 115 | * This alarm does not wake the device up; if it goes off while the device |
| 116 | * is asleep, it will not be delivered until the next time the device |
| 117 | * wakes up. |
| 118 | */ |
| 119 | public static final int ELAPSED_REALTIME = 3; |
| 120 | |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 121 | /** |
| 122 | * Broadcast Action: Sent after the value returned by |
| 123 | * {@link #getNextAlarmClock()} has changed. |
| 124 | * |
| 125 | * <p class="note">This is a protected intent that can only be sent by the system. |
| 126 | * It is only sent to registered receivers.</p> |
| 127 | */ |
| 128 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 129 | public static final String ACTION_NEXT_ALARM_CLOCK_CHANGED = |
| 130 | "android.app.action.NEXT_ALARM_CLOCK_CHANGED"; |
| 131 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 132 | /** @hide */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 133 | @UnsupportedAppUsage |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 134 | public static final long WINDOW_EXACT = 0; |
| 135 | /** @hide */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 136 | @UnsupportedAppUsage |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 137 | public static final long WINDOW_HEURISTIC = -1; |
| 138 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Flag for alarms: this is to be a stand-alone alarm, that should not be batched with |
| 141 | * other alarms. |
| 142 | * @hide |
| 143 | */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 144 | @UnsupportedAppUsage |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 145 | public static final int FLAG_STANDALONE = 1<<0; |
| 146 | |
| 147 | /** |
| 148 | * Flag for alarms: this alarm would like to wake the device even if it is idle. This |
| 149 | * is, for example, an alarm for an alarm clock. |
| 150 | * @hide |
| 151 | */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 152 | @UnsupportedAppUsage |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 153 | public static final int FLAG_WAKE_FROM_IDLE = 1<<1; |
| 154 | |
| 155 | /** |
| 156 | * Flag for alarms: this alarm would like to still execute even if the device is |
| 157 | * idle. This won't bring the device out of idle, just allow this specific alarm to |
| 158 | * run. Note that this means the actual time this alarm goes off can be inconsistent |
| 159 | * with the time of non-allow-while-idle alarms (it could go earlier than the time |
| 160 | * requested by another alarm). |
| 161 | * |
| 162 | * @hide |
| 163 | */ |
| 164 | public static final int FLAG_ALLOW_WHILE_IDLE = 1<<2; |
| 165 | |
| 166 | /** |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 167 | * Flag for alarms: same as {@link #FLAG_ALLOW_WHILE_IDLE}, but doesn't have restrictions |
| 168 | * on how frequently it can be scheduled. Only available (and automatically applied) to |
| 169 | * system alarms. |
| 170 | * |
| 171 | * @hide |
| 172 | */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 173 | @UnsupportedAppUsage |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 174 | public static final int FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED = 1<<3; |
| 175 | |
| 176 | /** |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 177 | * Flag for alarms: this alarm marks the point where we would like to come out of idle |
| 178 | * mode. It may be moved by the alarm manager to match the first wake-from-idle alarm. |
| 179 | * Scheduling an alarm with this flag puts the alarm manager in to idle mode, where it |
| 180 | * avoids scheduling any further alarms until the marker alarm is executed. |
| 181 | * @hide |
| 182 | */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 183 | @UnsupportedAppUsage |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 184 | public static final int FLAG_IDLE_UNTIL = 1<<4; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 185 | |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 186 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | private final IAlarmManager mService; |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 188 | private final Context mContext; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 189 | private final String mPackageName; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 190 | private final boolean mAlwaysExact; |
Narayan Kamath | a78240b | 2015-04-24 13:22:03 +0100 | [diff] [blame] | 191 | private final int mTargetSdkVersion; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 192 | private final Handler mMainThreadHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Direct-notification alarms: the requester must be running continuously from the |
| 196 | * time the alarm is set to the time it is delivered, or delivery will fail. Only |
| 197 | * one-shot alarms can be set using this mechanism, not repeating alarms. |
| 198 | */ |
| 199 | public interface OnAlarmListener { |
| 200 | /** |
| 201 | * Callback method that is invoked by the system when the alarm time is reached. |
| 202 | */ |
| 203 | public void onAlarm(); |
| 204 | } |
| 205 | |
| 206 | final class ListenerWrapper extends IAlarmListener.Stub implements Runnable { |
| 207 | final OnAlarmListener mListener; |
| 208 | Handler mHandler; |
| 209 | IAlarmCompleteListener mCompletion; |
| 210 | |
| 211 | public ListenerWrapper(OnAlarmListener listener) { |
| 212 | mListener = listener; |
| 213 | } |
| 214 | |
| 215 | public void setHandler(Handler h) { |
| 216 | mHandler = h; |
| 217 | } |
| 218 | |
| 219 | public void cancel() { |
| 220 | try { |
| 221 | mService.remove(null, this); |
| 222 | } catch (RemoteException ex) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 223 | throw ex.rethrowFromSystemServer(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | synchronized (AlarmManager.class) { |
| 227 | if (sWrappers != null) { |
| 228 | sWrappers.remove(mListener); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void doAlarm(IAlarmCompleteListener alarmManager) { |
| 235 | mCompletion = alarmManager; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 236 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 237 | // Remove this listener from the wrapper cache first; the server side |
| 238 | // already considers it gone |
| 239 | synchronized (AlarmManager.class) { |
| 240 | if (sWrappers != null) { |
| 241 | sWrappers.remove(mListener); |
| 242 | } |
| 243 | } |
| 244 | |
Makoto Onuki | a7d39cf | 2017-05-04 08:38:07 -0700 | [diff] [blame] | 245 | mHandler.post(this); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void run() { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 250 | // Now deliver it to the app |
| 251 | try { |
| 252 | mListener.onAlarm(); |
| 253 | } finally { |
| 254 | // No catch -- make sure to report completion to the system process, |
| 255 | // but continue to allow the exception to crash the app. |
| 256 | |
| 257 | try { |
| 258 | mCompletion.alarmComplete(this); |
| 259 | } catch (Exception e) { |
| 260 | Log.e(TAG, "Unable to report completion to Alarm Manager!", e); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Tracking of the OnAlarmListener -> wrapper mapping, for cancel() support. |
| 267 | // Access is synchronized on the AlarmManager class object. |
Christopher Tate | 09d7d8f | 2016-05-31 15:08:04 -0700 | [diff] [blame] | 268 | private static ArrayMap<OnAlarmListener, ListenerWrapper> sWrappers; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 269 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | /** |
| 271 | * package private on purpose |
| 272 | */ |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 273 | AlarmManager(IAlarmManager service, Context ctx) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | mService = service; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 275 | |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 276 | mContext = ctx; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 277 | mPackageName = ctx.getPackageName(); |
Narayan Kamath | a78240b | 2015-04-24 13:22:03 +0100 | [diff] [blame] | 278 | mTargetSdkVersion = ctx.getApplicationInfo().targetSdkVersion; |
| 279 | mAlwaysExact = (mTargetSdkVersion < Build.VERSION_CODES.KITKAT); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 280 | mMainThreadHandler = new Handler(ctx.getMainLooper()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | } |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 282 | |
| 283 | private long legacyExactLength() { |
| 284 | return (mAlwaysExact ? WINDOW_EXACT : WINDOW_HEURISTIC); |
| 285 | } |
| 286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | /** |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 288 | * <p>Schedule an alarm. <b>Note: for timing operations (ticks, timeouts, |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 289 | * etc) it is easier and much more efficient to use {@link android.os.Handler}.</b> |
| 290 | * If there is already an alarm scheduled for the same IntentSender, that previous |
| 291 | * alarm will first be canceled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 293 | * <p>If the stated trigger time is in the past, the alarm will be triggered |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | * immediately. If there is already an alarm for this Intent |
| 295 | * scheduled (with the equality of two intents being defined by |
| 296 | * {@link Intent#filterEquals}), then it will be removed and replaced by |
| 297 | * this one. |
| 298 | * |
| 299 | * <p> |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 300 | * The alarm is an Intent broadcast that goes to a broadcast receiver that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | * you registered with {@link android.content.Context#registerReceiver} |
| 302 | * or through the <receiver> tag in an AndroidManifest.xml file. |
| 303 | * |
| 304 | * <p> |
| 305 | * Alarm intents are delivered with a data extra of type int called |
| 306 | * {@link Intent#EXTRA_ALARM_COUNT Intent.EXTRA_ALARM_COUNT} that indicates |
| 307 | * how many past alarm events have been accumulated into this intent |
| 308 | * broadcast. Recurring alarms that have gone undelivered because the |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 309 | * phone was asleep may have a count greater than one when delivered. |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 310 | * |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 311 | * <div class="note"> |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 312 | * <p> |
| 313 | * <b>Note:</b> Beginning in API 19, the trigger time passed to this method |
| 314 | * is treated as inexact: the alarm will not be delivered before this time, but |
| 315 | * may be deferred and delivered some time later. The OS will use |
| 316 | * this policy in order to "batch" alarms together across the entire system, |
| 317 | * minimizing the number of times the device needs to "wake up" and minimizing |
| 318 | * battery use. In general, alarms scheduled in the near future will not |
| 319 | * be deferred as long as alarms scheduled far in the future. |
| 320 | * |
| 321 | * <p> |
| 322 | * With the new batching policy, delivery ordering guarantees are not as |
| 323 | * strong as they were previously. If the application sets multiple alarms, |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 324 | * it is possible that these alarms' <em>actual</em> delivery ordering may not match |
| 325 | * the order of their <em>requested</em> delivery times. If your application has |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 326 | * strong ordering requirements there are other APIs that you can use to get |
| 327 | * the necessary behavior; see {@link #setWindow(int, long, long, PendingIntent)} |
| 328 | * and {@link #setExact(int, long, PendingIntent)}. |
| 329 | * |
| 330 | * <p> |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 331 | * Applications whose {@code targetSdkVersion} is before API 19 will |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 332 | * continue to get the previous alarm behavior: all of their scheduled alarms |
| 333 | * will be treated as exact. |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 334 | * </div> |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 335 | * |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 336 | * @param type type of alarm. |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 337 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 338 | * off, using the appropriate clock (depending on the alarm type). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | * @param operation Action to perform when the alarm goes off; |
| 340 | * typically comes from {@link PendingIntent#getBroadcast |
| 341 | * IntentSender.getBroadcast()}. |
| 342 | * |
| 343 | * @see android.os.Handler |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 344 | * @see #setExact |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | * @see #setRepeating |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 346 | * @see #setWindow |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | * @see #cancel |
| 348 | * @see android.content.Context#sendBroadcast |
| 349 | * @see android.content.Context#registerReceiver |
| 350 | * @see android.content.Intent#filterEquals |
| 351 | * @see #ELAPSED_REALTIME |
| 352 | * @see #ELAPSED_REALTIME_WAKEUP |
| 353 | * @see #RTC |
| 354 | * @see #RTC_WAKEUP |
| 355 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 356 | public void set(@AlarmType int type, long triggerAtMillis, PendingIntent operation) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 357 | setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, operation, null, null, |
| 358 | null, null, null); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Direct callback version of {@link #set(int, long, PendingIntent)}. Rather than |
| 363 | * supplying a PendingIntent to be sent when the alarm time is reached, this variant |
| 364 | * supplies an {@link OnAlarmListener} instance that will be invoked at that time. |
| 365 | * <p> |
| 366 | * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 367 | * invoked via the specified target Handler, or on the application's main looper |
| 368 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 369 | * |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 370 | * @param type type of alarm. |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 371 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 372 | * off, using the appropriate clock (depending on the alarm type). |
| 373 | * @param tag string describing the alarm, used for logging and battery-use |
| 374 | * attribution |
| 375 | * @param listener {@link OnAlarmListener} instance whose |
| 376 | * {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 377 | * called when the alarm time is reached. A given OnAlarmListener instance can |
| 378 | * only be the target of a single pending alarm, just as a given PendingIntent |
| 379 | * can only be used with one alarm at a time. |
| 380 | * @param targetHandler {@link Handler} on which to execute the listener's onAlarm() |
| 381 | * callback, or {@code null} to run that callback on the main looper. |
| 382 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 383 | public void set(@AlarmType int type, long triggerAtMillis, String tag, OnAlarmListener listener, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 384 | Handler targetHandler) { |
| 385 | setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, null, listener, tag, |
| 386 | targetHandler, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Schedule a repeating alarm. <b>Note: for timing operations (ticks, |
| 391 | * timeouts, etc) it is easier and much more efficient to use |
| 392 | * {@link android.os.Handler}.</b> If there is already an alarm scheduled |
| 393 | * for the same IntentSender, it will first be canceled. |
| 394 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 395 | * <p>Like {@link #set}, except you can also supply a period at which |
| 396 | * the alarm will automatically repeat. This alarm continues |
| 397 | * repeating until explicitly removed with {@link #cancel}. If the stated |
| 398 | * trigger time is in the past, the alarm will be triggered immediately, with an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | * alarm count depending on how far in the past the trigger time is relative |
| 400 | * to the repeat interval. |
| 401 | * |
| 402 | * <p>If an alarm is delayed (by system sleep, for example, for non |
| 403 | * _WAKEUP alarm types), a skipped repeat will be delivered as soon as |
| 404 | * possible. After that, future alarms will be delivered according to the |
| 405 | * original schedule; they do not drift over time. For example, if you have |
| 406 | * set a recurring alarm for the top of every hour but the phone was asleep |
| 407 | * from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens, |
| 408 | * then the next alarm will be sent at 9:00. |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 409 | * |
| 410 | * <p>If your application wants to allow the delivery times to drift in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | * order to guarantee that at least a certain time interval always elapses |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 412 | * between alarms, then the approach to take is to use one-time alarms, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | * scheduling the next one yourself when handling each alarm delivery. |
| 414 | * |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 415 | * <p class="note"> |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 416 | * <b>Note:</b> as of API 19, all repeating alarms are inexact. If your |
| 417 | * application needs precise delivery times then it must use one-time |
| 418 | * exact alarms, rescheduling each time as described above. Legacy applications |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 419 | * whose {@code targetSdkVersion} is earlier than API 19 will continue to have all |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 420 | * of their alarms, including repeating alarms, treated as exact. |
| 421 | * |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 422 | * @param type type of alarm. |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 423 | * @param triggerAtMillis time in milliseconds that the alarm should first |
| 424 | * go off, using the appropriate clock (depending on the alarm type). |
| 425 | * @param intervalMillis interval in milliseconds between subsequent repeats |
| 426 | * of the alarm. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | * @param operation Action to perform when the alarm goes off; |
| 428 | * typically comes from {@link PendingIntent#getBroadcast |
| 429 | * IntentSender.getBroadcast()}. |
| 430 | * |
| 431 | * @see android.os.Handler |
| 432 | * @see #set |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 433 | * @see #setExact |
| 434 | * @see #setWindow |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | * @see #cancel |
| 436 | * @see android.content.Context#sendBroadcast |
| 437 | * @see android.content.Context#registerReceiver |
| 438 | * @see android.content.Intent#filterEquals |
| 439 | * @see #ELAPSED_REALTIME |
| 440 | * @see #ELAPSED_REALTIME_WAKEUP |
| 441 | * @see #RTC |
| 442 | * @see #RTC_WAKEUP |
| 443 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 444 | public void setRepeating(@AlarmType int type, long triggerAtMillis, |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 445 | long intervalMillis, PendingIntent operation) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 446 | setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, 0, operation, |
| 447 | null, null, null, null, null); |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 451 | * Schedule an alarm to be delivered within a given window of time. This method |
| 452 | * is similar to {@link #set(int, long, PendingIntent)}, but allows the |
| 453 | * application to precisely control the degree to which its delivery might be |
| 454 | * adjusted by the OS. This method allows an application to take advantage of the |
| 455 | * battery optimizations that arise from delivery batching even when it has |
| 456 | * modest timeliness requirements for its alarms. |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 457 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 458 | * <p> |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 459 | * This method can also be used to achieve strict ordering guarantees among |
| 460 | * multiple alarms by ensuring that the windows requested for each alarm do |
| 461 | * not intersect. |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 462 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 463 | * <p> |
| 464 | * When precise delivery is not required, applications should use the standard |
| 465 | * {@link #set(int, long, PendingIntent)} method. This will give the OS the most |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 466 | * flexibility to minimize wakeups and battery use. For alarms that must be delivered |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 467 | * at precisely-specified times with no acceptable variation, applications can use |
| 468 | * {@link #setExact(int, long, PendingIntent)}. |
| 469 | * |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 470 | * @param type type of alarm. |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 471 | * @param windowStartMillis The earliest time, in milliseconds, that the alarm should |
| 472 | * be delivered, expressed in the appropriate clock's units (depending on the alarm |
| 473 | * type). |
| 474 | * @param windowLengthMillis The length of the requested delivery window, |
| 475 | * in milliseconds. The alarm will be delivered no later than this many |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 476 | * milliseconds after {@code windowStartMillis}. Note that this parameter |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 477 | * is a <i>duration,</i> not the timestamp of the end of the window. |
| 478 | * @param operation Action to perform when the alarm goes off; |
| 479 | * typically comes from {@link PendingIntent#getBroadcast |
| 480 | * IntentSender.getBroadcast()}. |
| 481 | * |
| 482 | * @see #set |
| 483 | * @see #setExact |
| 484 | * @see #setRepeating |
| 485 | * @see #cancel |
| 486 | * @see android.content.Context#sendBroadcast |
| 487 | * @see android.content.Context#registerReceiver |
| 488 | * @see android.content.Intent#filterEquals |
| 489 | * @see #ELAPSED_REALTIME |
| 490 | * @see #ELAPSED_REALTIME_WAKEUP |
| 491 | * @see #RTC |
| 492 | * @see #RTC_WAKEUP |
| 493 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 494 | public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis, |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 495 | PendingIntent operation) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 496 | setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, operation, |
| 497 | null, null, null, null, null); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Direct callback version of {@link #setWindow(int, long, long, PendingIntent)}. Rather |
| 502 | * than supplying a PendingIntent to be sent when the alarm time is reached, this variant |
| 503 | * supplies an {@link OnAlarmListener} instance that will be invoked at that time. |
| 504 | * <p> |
| 505 | * The OnAlarmListener {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 506 | * invoked via the specified target Handler, or on the application's main looper |
| 507 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 508 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 509 | public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 510 | String tag, OnAlarmListener listener, Handler targetHandler) { |
| 511 | setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, null, listener, tag, |
| 512 | targetHandler, null, null); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 516 | * Schedule an alarm to be delivered precisely at the stated time. |
| 517 | * |
| 518 | * <p> |
| 519 | * This method is like {@link #set(int, long, PendingIntent)}, but does not permit |
| 520 | * the OS to adjust the delivery time. The alarm will be delivered as nearly as |
| 521 | * possible to the requested trigger time. |
| 522 | * |
| 523 | * <p> |
| 524 | * <b>Note:</b> only alarms for which there is a strong demand for exact-time |
| 525 | * delivery (such as an alarm clock ringing at the requested time) should be |
| 526 | * scheduled as exact. Applications are strongly discouraged from using exact |
| 527 | * alarms unnecessarily as they reduce the OS's ability to minimize battery use. |
| 528 | * |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 529 | * @param type type of alarm. |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame] | 530 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 531 | * off, using the appropriate clock (depending on the alarm type). |
| 532 | * @param operation Action to perform when the alarm goes off; |
| 533 | * typically comes from {@link PendingIntent#getBroadcast |
| 534 | * IntentSender.getBroadcast()}. |
| 535 | * |
| 536 | * @see #set |
| 537 | * @see #setRepeating |
| 538 | * @see #setWindow |
| 539 | * @see #cancel |
| 540 | * @see android.content.Context#sendBroadcast |
| 541 | * @see android.content.Context#registerReceiver |
| 542 | * @see android.content.Intent#filterEquals |
| 543 | * @see #ELAPSED_REALTIME |
| 544 | * @see #ELAPSED_REALTIME_WAKEUP |
| 545 | * @see #RTC |
| 546 | * @see #RTC_WAKEUP |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 547 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 548 | public void setExact(@AlarmType int type, long triggerAtMillis, PendingIntent operation) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 549 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, operation, null, null, null, |
| 550 | null, null); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Direct callback version of {@link #setExact(int, long, PendingIntent)}. Rather |
| 555 | * than supplying a PendingIntent to be sent when the alarm time is reached, this variant |
| 556 | * supplies an {@link OnAlarmListener} instance that will be invoked at that time. |
| 557 | * <p> |
| 558 | * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 559 | * invoked via the specified target Handler, or on the application's main looper |
| 560 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 561 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 562 | public void setExact(@AlarmType int type, long triggerAtMillis, String tag, |
| 563 | OnAlarmListener listener, Handler targetHandler) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 564 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, null, listener, tag, |
| 565 | targetHandler, null, null); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Schedule an idle-until alarm, which will keep the alarm manager idle until |
| 570 | * the given time. |
| 571 | * @hide |
| 572 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 573 | public void setIdleUntil(@AlarmType int type, long triggerAtMillis, String tag, |
| 574 | OnAlarmListener listener, Handler targetHandler) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 575 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_IDLE_UNTIL, null, |
| 576 | listener, tag, targetHandler, null, null); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /** |
Dianne Hackborn | a4eb31d | 2017-12-04 17:42:16 -0800 | [diff] [blame] | 580 | * Schedule an alarm that represents an alarm clock, which will be used to notify the user |
| 581 | * when it goes off. The expectation is that when this alarm triggers, the application will |
| 582 | * further wake up the device to tell the user about the alarm -- turning on the screen, |
| 583 | * playing a sound, vibrating, etc. As such, the system will typically also use the |
| 584 | * information supplied here to tell the user about this upcoming alarm if appropriate. |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 585 | * |
Dianne Hackborn | a4eb31d | 2017-12-04 17:42:16 -0800 | [diff] [blame] | 586 | * <p>Due to the nature of this kind of alarm, similar to {@link #setExactAndAllowWhileIdle}, |
| 587 | * these alarms will be allowed to trigger even if the system is in a low-power idle |
| 588 | * (a.k.a. doze) mode. The system may also do some prep-work when it sees that such an |
| 589 | * alarm coming up, to reduce the amount of background work that could happen if this |
| 590 | * causes the device to fully wake up -- this is to avoid situations such as a large number |
| 591 | * of devices having an alarm set at the same time in the morning, all waking up at that |
| 592 | * time and suddenly swamping the network with pending background work. As such, these |
| 593 | * types of alarms can be extremely expensive on battery use and should only be used for |
| 594 | * their intended purpose.</p> |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 595 | * |
| 596 | * <p> |
| 597 | * This method is like {@link #setExact(int, long, PendingIntent)}, but implies |
| 598 | * {@link #RTC_WAKEUP}. |
| 599 | * |
| 600 | * @param info |
| 601 | * @param operation Action to perform when the alarm goes off; |
| 602 | * typically comes from {@link PendingIntent#getBroadcast |
| 603 | * IntentSender.getBroadcast()}. |
| 604 | * |
| 605 | * @see #set |
| 606 | * @see #setRepeating |
| 607 | * @see #setWindow |
| 608 | * @see #setExact |
| 609 | * @see #cancel |
| 610 | * @see #getNextAlarmClock() |
| 611 | * @see android.content.Context#sendBroadcast |
| 612 | * @see android.content.Context#registerReceiver |
| 613 | * @see android.content.Intent#filterEquals |
| 614 | */ |
| 615 | public void setAlarmClock(AlarmClockInfo info, PendingIntent operation) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 616 | setImpl(RTC_WAKEUP, info.getTriggerTime(), WINDOW_EXACT, 0, 0, operation, |
| 617 | null, null, null, null, info); |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /** @hide */ |
David Christie | c20b795 | 2014-09-04 11:29:01 -0700 | [diff] [blame] | 621 | @SystemApi |
Christopher Tate | 8d3079f | 2017-06-05 18:13:52 -0700 | [diff] [blame] | 622 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 623 | public void set(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 624 | long intervalMillis, PendingIntent operation, WorkSource workSource) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 625 | setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, operation, null, null, |
| 626 | null, workSource, null); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}. |
| 631 | * Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener. |
| 632 | * <p> |
| 633 | * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 634 | * invoked via the specified target Handler, or on the application's main looper |
| 635 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 636 | * |
| 637 | * @hide |
| 638 | */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 639 | @UnsupportedAppUsage |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 640 | public void set(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 641 | long intervalMillis, String tag, OnAlarmListener listener, Handler targetHandler, |
| 642 | WorkSource workSource) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 643 | setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, tag, |
| 644 | targetHandler, workSource, null); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}. |
| 649 | * Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener. |
| 650 | * <p> |
| 651 | * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 652 | * invoked via the specified target Handler, or on the application's main looper |
| 653 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 654 | * |
| 655 | * @hide |
| 656 | */ |
Christopher Tate | f2d753e | 2015-11-04 11:10:48 -0800 | [diff] [blame] | 657 | @SystemApi |
Christopher Tate | 8d3079f | 2017-06-05 18:13:52 -0700 | [diff] [blame] | 658 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 659 | public void set(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 660 | long intervalMillis, OnAlarmListener listener, Handler targetHandler, |
| 661 | WorkSource workSource) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 662 | setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, null, |
| 663 | targetHandler, workSource, null); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 666 | private void setImpl(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 667 | long intervalMillis, int flags, PendingIntent operation, final OnAlarmListener listener, |
| 668 | String listenerTag, Handler targetHandler, WorkSource workSource, |
| 669 | AlarmClockInfo alarmClock) { |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 670 | if (triggerAtMillis < 0) { |
Christopher Tate | 56cfa24 | 2013-07-30 19:09:41 -0700 | [diff] [blame] | 671 | /* NOTYET |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 672 | if (mAlwaysExact) { |
| 673 | // Fatal error for KLP+ apps to use negative trigger times |
| 674 | throw new IllegalArgumentException("Invalid alarm trigger time " |
| 675 | + triggerAtMillis); |
| 676 | } |
Christopher Tate | 56cfa24 | 2013-07-30 19:09:41 -0700 | [diff] [blame] | 677 | */ |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 678 | triggerAtMillis = 0; |
| 679 | } |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 680 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 681 | ListenerWrapper recipientWrapper = null; |
| 682 | if (listener != null) { |
| 683 | synchronized (AlarmManager.class) { |
| 684 | if (sWrappers == null) { |
Christopher Tate | 09d7d8f | 2016-05-31 15:08:04 -0700 | [diff] [blame] | 685 | sWrappers = new ArrayMap<OnAlarmListener, ListenerWrapper>(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Christopher Tate | d0cca79 | 2016-04-21 15:05:34 -0700 | [diff] [blame] | 688 | recipientWrapper = sWrappers.get(listener); |
| 689 | // no existing wrapper => build a new one |
| 690 | if (recipientWrapper == null) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 691 | recipientWrapper = new ListenerWrapper(listener); |
Christopher Tate | d0cca79 | 2016-04-21 15:05:34 -0700 | [diff] [blame] | 692 | sWrappers.put(listener, recipientWrapper); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | final Handler handler = (targetHandler != null) ? targetHandler : mMainThreadHandler; |
| 697 | recipientWrapper.setHandler(handler); |
| 698 | } |
| 699 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | try { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 701 | mService.set(mPackageName, type, triggerAtMillis, windowMillis, intervalMillis, flags, |
| 702 | operation, recipientWrapper, listenerTag, workSource, alarmClock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | } catch (RemoteException ex) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 704 | throw ex.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | /** |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 709 | * Available inexact recurrence interval recognized by |
| 710 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 711 | * when running on Android prior to API 19. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | */ |
| 713 | public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 714 | |
| 715 | /** |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 716 | * Available inexact recurrence interval recognized by |
| 717 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 718 | * when running on Android prior to API 19. |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 719 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 721 | |
| 722 | /** |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 723 | * Available inexact recurrence interval recognized by |
| 724 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 725 | * when running on Android prior to API 19. |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 726 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 728 | |
| 729 | /** |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 730 | * Available inexact recurrence interval recognized by |
| 731 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 732 | * when running on Android prior to API 19. |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 733 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 735 | |
| 736 | /** |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 737 | * Available inexact recurrence interval recognized by |
| 738 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 739 | * when running on Android prior to API 19. |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 740 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | /** |
| 744 | * Schedule a repeating alarm that has inexact trigger time requirements; |
| 745 | * for example, an alarm that repeats every hour, but not necessarily at |
| 746 | * the top of every hour. These alarms are more power-efficient than |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 747 | * the strict recurrences traditionally supplied by {@link #setRepeating}, since the |
| 748 | * system can adjust alarms' delivery times to cause them to fire simultaneously, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | * avoiding waking the device from sleep more than necessary. |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 750 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | * <p>Your alarm's first trigger will not be before the requested time, |
| 752 | * but it might not occur for almost a full interval after that time. In |
| 753 | * addition, while the overall period of the repeating alarm will be as |
| 754 | * requested, the time between any two successive firings of the alarm |
| 755 | * may vary. If your application demands very low jitter, use |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 756 | * one-shot alarms with an appropriate window instead; see {@link |
| 757 | * #setWindow(int, long, long, PendingIntent)} and |
| 758 | * {@link #setExact(int, long, PendingIntent)}. |
| 759 | * |
| 760 | * <p class="note"> |
| 761 | * As of API 19, all repeating alarms are inexact. Because this method has |
| 762 | * been available since API 3, your application can safely call it and be |
| 763 | * assured that it will get similar behavior on both current and older versions |
| 764 | * of Android. |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 765 | * |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 766 | * @param type type of alarm. |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 767 | * @param triggerAtMillis time in milliseconds that the alarm should first |
| 768 | * go off, using the appropriate clock (depending on the alarm type). This |
| 769 | * is inexact: the alarm will not fire before this time, but there may be a |
| 770 | * delay of almost an entire alarm interval before the first invocation of |
| 771 | * the alarm. |
| 772 | * @param intervalMillis interval in milliseconds between subsequent repeats |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 773 | * of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES, |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 774 | * INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY |
| 775 | * then the alarm will be phase-aligned with other alarms to reduce the |
| 776 | * number of wakeups. Otherwise, the alarm will be set as though the |
Christopher Tate | 109e4db | 2013-10-25 16:14:38 -0700 | [diff] [blame] | 777 | * application had called {@link #setRepeating}. As of API 19, all repeating |
| 778 | * alarms will be inexact and subject to batching with other alarms regardless |
| 779 | * of their stated repeat interval. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | * @param operation Action to perform when the alarm goes off; |
| 781 | * typically comes from {@link PendingIntent#getBroadcast |
| 782 | * IntentSender.getBroadcast()}. |
| 783 | * |
| 784 | * @see android.os.Handler |
| 785 | * @see #set |
| 786 | * @see #cancel |
| 787 | * @see android.content.Context#sendBroadcast |
| 788 | * @see android.content.Context#registerReceiver |
| 789 | * @see android.content.Intent#filterEquals |
| 790 | * @see #ELAPSED_REALTIME |
| 791 | * @see #ELAPSED_REALTIME_WAKEUP |
| 792 | * @see #RTC |
| 793 | * @see #RTC_WAKEUP |
| 794 | * @see #INTERVAL_FIFTEEN_MINUTES |
| 795 | * @see #INTERVAL_HALF_HOUR |
| 796 | * @see #INTERVAL_HOUR |
| 797 | * @see #INTERVAL_HALF_DAY |
| 798 | * @see #INTERVAL_DAY |
| 799 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 800 | public void setInexactRepeating(@AlarmType int type, long triggerAtMillis, |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 801 | long intervalMillis, PendingIntent operation) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 802 | setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, intervalMillis, 0, operation, null, |
| 803 | null, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | } |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 805 | |
| 806 | /** |
| 807 | * Like {@link #set(int, long, PendingIntent)}, but this alarm will be allowed to execute |
Dianne Hackborn | a4eb31d | 2017-12-04 17:42:16 -0800 | [diff] [blame] | 808 | * even when the system is in low-power idle (a.k.a. doze) modes. This type of alarm must |
| 809 | * <b>only</b> be used for situations where it is actually required that the alarm go off while |
| 810 | * in idle -- a reasonable example would be for a calendar notification that should make a |
Dianne Hackborn | 14c5ab4 | 2015-07-09 18:17:54 -0700 | [diff] [blame] | 811 | * sound so the user is aware of it. When the alarm is dispatched, the app will also be |
| 812 | * added to the system's temporary whitelist for approximately 10 seconds to allow that |
| 813 | * application to acquire further wake locks in which to complete its work.</p> |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 814 | * |
Dianne Hackborn | 14c5ab4 | 2015-07-09 18:17:54 -0700 | [diff] [blame] | 815 | * <p>These alarms can significantly impact the power use |
| 816 | * of the device when idle (and thus cause significant battery blame to the app scheduling |
| 817 | * them), so they should be used with care. To reduce abuse, there are restrictions on how |
| 818 | * frequently these alarms will go off for a particular application. |
| 819 | * Under normal system operation, it will not dispatch these |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 820 | * alarms more than about every minute (at which point every such pending alarm is |
| 821 | * dispatched); when in low-power idle modes this duration may be significantly longer, |
| 822 | * such as 15 minutes.</p> |
| 823 | * |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 824 | * <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen |
| 825 | * out of order with any other alarms, even those from the same app. This will clearly happen |
| 826 | * when the device is idle (since this alarm can go off while idle, when any other alarms |
| 827 | * from the app will be held until later), but may also happen even when not idle.</p> |
| 828 | * |
| 829 | * <p>Regardless of the app's target SDK version, this call always allows batching of the |
| 830 | * alarm.</p> |
| 831 | * |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 832 | * @param type type of alarm. |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 833 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 834 | * off, using the appropriate clock (depending on the alarm type). |
| 835 | * @param operation Action to perform when the alarm goes off; |
| 836 | * typically comes from {@link PendingIntent#getBroadcast |
| 837 | * IntentSender.getBroadcast()}. |
| 838 | * |
| 839 | * @see #set(int, long, PendingIntent) |
| 840 | * @see #setExactAndAllowWhileIdle |
| 841 | * @see #cancel |
| 842 | * @see android.content.Context#sendBroadcast |
| 843 | * @see android.content.Context#registerReceiver |
| 844 | * @see android.content.Intent#filterEquals |
| 845 | * @see #ELAPSED_REALTIME |
| 846 | * @see #ELAPSED_REALTIME_WAKEUP |
| 847 | * @see #RTC |
| 848 | * @see #RTC_WAKEUP |
| 849 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 850 | public void setAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis, |
| 851 | PendingIntent operation) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 852 | setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, 0, FLAG_ALLOW_WHILE_IDLE, |
| 853 | operation, null, null, null, null, null); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /** |
| 857 | * Like {@link #setExact(int, long, PendingIntent)}, but this alarm will be allowed to execute |
| 858 | * even when the system is in low-power idle modes. If you don't need exact scheduling of |
| 859 | * the alarm but still need to execute while idle, consider using |
| 860 | * {@link #setAndAllowWhileIdle}. This type of alarm must <b>only</b> |
| 861 | * be used for situations where it is actually required that the alarm go off while in |
| 862 | * idle -- a reasonable example would be for a calendar notification that should make a |
Dianne Hackborn | 14c5ab4 | 2015-07-09 18:17:54 -0700 | [diff] [blame] | 863 | * sound so the user is aware of it. When the alarm is dispatched, the app will also be |
| 864 | * added to the system's temporary whitelist for approximately 10 seconds to allow that |
| 865 | * application to acquire further wake locks in which to complete its work.</p> |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 866 | * |
Dianne Hackborn | 14c5ab4 | 2015-07-09 18:17:54 -0700 | [diff] [blame] | 867 | * <p>These alarms can significantly impact the power use |
| 868 | * of the device when idle (and thus cause significant battery blame to the app scheduling |
| 869 | * them), so they should be used with care. To reduce abuse, there are restrictions on how |
| 870 | * frequently these alarms will go off for a particular application. |
| 871 | * Under normal system operation, it will not dispatch these |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 872 | * alarms more than about every minute (at which point every such pending alarm is |
| 873 | * dispatched); when in low-power idle modes this duration may be significantly longer, |
| 874 | * such as 15 minutes.</p> |
| 875 | * |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 876 | * <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen |
| 877 | * out of order with any other alarms, even those from the same app. This will clearly happen |
| 878 | * when the device is idle (since this alarm can go off while idle, when any other alarms |
| 879 | * from the app will be held until later), but may also happen even when not idle. |
| 880 | * Note that the OS will allow itself more flexibility for scheduling these alarms than |
| 881 | * regular exact alarms, since the application has opted into this behavior. When the |
| 882 | * device is idle it may take even more liberties with scheduling in order to optimize |
| 883 | * for battery life.</p> |
| 884 | * |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 885 | * @param type type of alarm. |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 886 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 887 | * off, using the appropriate clock (depending on the alarm type). |
| 888 | * @param operation Action to perform when the alarm goes off; |
| 889 | * typically comes from {@link PendingIntent#getBroadcast |
| 890 | * IntentSender.getBroadcast()}. |
| 891 | * |
| 892 | * @see #set |
| 893 | * @see #setRepeating |
| 894 | * @see #setWindow |
| 895 | * @see #cancel |
| 896 | * @see android.content.Context#sendBroadcast |
| 897 | * @see android.content.Context#registerReceiver |
| 898 | * @see android.content.Intent#filterEquals |
| 899 | * @see #ELAPSED_REALTIME |
| 900 | * @see #ELAPSED_REALTIME_WAKEUP |
| 901 | * @see #RTC |
| 902 | * @see #RTC_WAKEUP |
| 903 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 904 | public void setExactAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis, |
| 905 | PendingIntent operation) { |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 906 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_ALLOW_WHILE_IDLE, operation, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 907 | null, null, null, null, null); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 908 | } |
| 909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | /** |
| 911 | * Remove any alarms with a matching {@link Intent}. |
| 912 | * Any alarm, of any type, whose Intent matches this one (as defined by |
| 913 | * {@link Intent#filterEquals}), will be canceled. |
| 914 | * |
| 915 | * @param operation IntentSender which matches a previously added |
Christopher Tate | bb76a6c | 2015-12-02 10:54:56 -0800 | [diff] [blame] | 916 | * IntentSender. This parameter must not be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | * |
| 918 | * @see #set |
| 919 | */ |
| 920 | public void cancel(PendingIntent operation) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 921 | if (operation == null) { |
Christopher Tate | bb76a6c | 2015-12-02 10:54:56 -0800 | [diff] [blame] | 922 | final String msg = "cancel() called with a null PendingIntent"; |
| 923 | if (mTargetSdkVersion >= Build.VERSION_CODES.N) { |
| 924 | throw new NullPointerException(msg); |
| 925 | } else { |
| 926 | Log.e(TAG, msg); |
| 927 | return; |
| 928 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 929 | } |
| 930 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | try { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 932 | mService.remove(operation, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | } catch (RemoteException ex) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 934 | throw ex.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | } |
| 936 | } |
Dan Egnor | 97e4494 | 2010-02-04 20:27:47 -0800 | [diff] [blame] | 937 | |
| 938 | /** |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 939 | * Remove any alarm scheduled to be delivered to the given {@link OnAlarmListener}. |
| 940 | * |
| 941 | * @param listener OnAlarmListener instance that is the target of a currently-set alarm. |
| 942 | */ |
| 943 | public void cancel(OnAlarmListener listener) { |
| 944 | if (listener == null) { |
Christopher Tate | bb76a6c | 2015-12-02 10:54:56 -0800 | [diff] [blame] | 945 | throw new NullPointerException("cancel() called with a null OnAlarmListener"); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | ListenerWrapper wrapper = null; |
| 949 | synchronized (AlarmManager.class) { |
Joe LaPenna | 33ee4bf | 2016-04-01 13:37:37 -0700 | [diff] [blame] | 950 | if (sWrappers != null) { |
Christopher Tate | d0cca79 | 2016-04-21 15:05:34 -0700 | [diff] [blame] | 951 | wrapper = sWrappers.get(listener); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
| 955 | if (wrapper == null) { |
| 956 | Log.w(TAG, "Unrecognized alarm listener " + listener); |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | wrapper.cancel(); |
| 961 | } |
| 962 | |
| 963 | /** |
Dan Egnor | 97e4494 | 2010-02-04 20:27:47 -0800 | [diff] [blame] | 964 | * Set the system wall clock time. |
| 965 | * Requires the permission android.permission.SET_TIME. |
| 966 | * |
| 967 | * @param millis time in milliseconds since the Epoch |
| 968 | */ |
| 969 | public void setTime(long millis) { |
| 970 | try { |
| 971 | mService.setTime(millis); |
| 972 | } catch (RemoteException ex) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 973 | throw ex.rethrowFromSystemServer(); |
Dan Egnor | 97e4494 | 2010-02-04 20:27:47 -0800 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | /** |
Narayan Kamath | a78240b | 2015-04-24 13:22:03 +0100 | [diff] [blame] | 978 | * Sets the system's persistent default time zone. This is the time zone for all apps, even |
| 979 | * after a reboot. Use {@link java.util.TimeZone#setDefault} if you just want to change the |
| 980 | * time zone within your app, and even then prefer to pass an explicit |
| 981 | * {@link java.util.TimeZone} to APIs that require it rather than changing the time zone for |
| 982 | * all threads. |
Dan Egnor | 97e4494 | 2010-02-04 20:27:47 -0800 | [diff] [blame] | 983 | * |
Narayan Kamath | a78240b | 2015-04-24 13:22:03 +0100 | [diff] [blame] | 984 | * <p> On android M and above, it is an error to pass in a non-Olson timezone to this |
| 985 | * function. Note that this is a bad idea on all Android releases because POSIX and |
| 986 | * the {@code TimeZone} class have opposite interpretations of {@code '+'} and {@code '-'} |
| 987 | * in the same non-Olson ID. |
| 988 | * |
| 989 | * @param timeZone one of the Olson ids from the list returned by |
| 990 | * {@link java.util.TimeZone#getAvailableIDs} |
Dan Egnor | 97e4494 | 2010-02-04 20:27:47 -0800 | [diff] [blame] | 991 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | public void setTimeZone(String timeZone) { |
Narayan Kamath | a78240b | 2015-04-24 13:22:03 +0100 | [diff] [blame] | 993 | if (TextUtils.isEmpty(timeZone)) { |
| 994 | return; |
| 995 | } |
| 996 | |
| 997 | // Reject this timezone if it isn't an Olson zone we recognize. |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 998 | if (mTargetSdkVersion >= Build.VERSION_CODES.M) { |
Narayan Kamath | a78240b | 2015-04-24 13:22:03 +0100 | [diff] [blame] | 999 | boolean hasTimeZone = false; |
| 1000 | try { |
| 1001 | hasTimeZone = ZoneInfoDB.getInstance().hasTimeZone(timeZone); |
| 1002 | } catch (IOException ignored) { |
| 1003 | } |
| 1004 | |
| 1005 | if (!hasTimeZone) { |
| 1006 | throw new IllegalArgumentException("Timezone: " + timeZone + " is not an Olson ID"); |
| 1007 | } |
| 1008 | } |
| 1009 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | try { |
| 1011 | mService.setTimeZone(timeZone); |
| 1012 | } catch (RemoteException ex) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1013 | throw ex.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | } |
| 1015 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1016 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1017 | /** @hide */ |
| 1018 | public long getNextWakeFromIdleTime() { |
| 1019 | try { |
| 1020 | return mService.getNextWakeFromIdleTime(); |
| 1021 | } catch (RemoteException ex) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1022 | throw ex.rethrowFromSystemServer(); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1026 | /** |
| 1027 | * Gets information about the next alarm clock currently scheduled. |
| 1028 | * |
Christopher Tate | 2affae9 | 2016-03-09 17:42:52 -0800 | [diff] [blame] | 1029 | * The alarm clocks considered are those scheduled by any application |
| 1030 | * using the {@link #setAlarmClock} method. |
| 1031 | * |
| 1032 | * @return An {@link AlarmClockInfo} object describing the next upcoming alarm |
| 1033 | * clock event that will occur. If there are no alarm clock events currently |
| 1034 | * scheduled, this method will return {@code null}. |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1035 | * |
| 1036 | * @see #setAlarmClock |
| 1037 | * @see AlarmClockInfo |
Christopher Tate | 2affae9 | 2016-03-09 17:42:52 -0800 | [diff] [blame] | 1038 | * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1039 | */ |
| 1040 | public AlarmClockInfo getNextAlarmClock() { |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 1041 | return getNextAlarmClock(mContext.getUserId()); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * Gets information about the next alarm clock currently scheduled. |
| 1046 | * |
Christopher Tate | 2affae9 | 2016-03-09 17:42:52 -0800 | [diff] [blame] | 1047 | * The alarm clocks considered are those scheduled by any application |
| 1048 | * using the {@link #setAlarmClock} method within the given user. |
| 1049 | * |
| 1050 | * @return An {@link AlarmClockInfo} object describing the next upcoming alarm |
| 1051 | * clock event that will occur within the given user. If there are no alarm clock |
| 1052 | * events currently scheduled in that user, this method will return {@code null}. |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1053 | * |
| 1054 | * @see #setAlarmClock |
| 1055 | * @see AlarmClockInfo |
Christopher Tate | 2affae9 | 2016-03-09 17:42:52 -0800 | [diff] [blame] | 1056 | * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1057 | * |
| 1058 | * @hide |
| 1059 | */ |
| 1060 | public AlarmClockInfo getNextAlarmClock(int userId) { |
| 1061 | try { |
| 1062 | return mService.getNextAlarmClock(userId); |
| 1063 | } catch (RemoteException ex) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1064 | throw ex.rethrowFromSystemServer(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1065 | } |
| 1066 | } |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1067 | |
| 1068 | /** |
Christopher Tate | 2affae9 | 2016-03-09 17:42:52 -0800 | [diff] [blame] | 1069 | * An immutable description of a scheduled "alarm clock" event. |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1070 | * |
| 1071 | * @see AlarmManager#setAlarmClock |
| 1072 | * @see AlarmManager#getNextAlarmClock |
| 1073 | */ |
| 1074 | public static final class AlarmClockInfo implements Parcelable { |
| 1075 | |
| 1076 | private final long mTriggerTime; |
| 1077 | private final PendingIntent mShowIntent; |
| 1078 | |
| 1079 | /** |
| 1080 | * Creates a new alarm clock description. |
| 1081 | * |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 1082 | * @param triggerTime time at which the underlying alarm is triggered in wall time |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1083 | * milliseconds since the epoch |
| 1084 | * @param showIntent an intent that can be used to show or edit details of |
| 1085 | * the alarm clock. |
| 1086 | */ |
| 1087 | public AlarmClockInfo(long triggerTime, PendingIntent showIntent) { |
| 1088 | mTriggerTime = triggerTime; |
| 1089 | mShowIntent = showIntent; |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * Use the {@link #CREATOR} |
| 1094 | * @hide |
| 1095 | */ |
| 1096 | AlarmClockInfo(Parcel in) { |
| 1097 | mTriggerTime = in.readLong(); |
| 1098 | mShowIntent = in.readParcelable(PendingIntent.class.getClassLoader()); |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * Returns the time at which the alarm is going to trigger. |
| 1103 | * |
| 1104 | * This value is UTC wall clock time in milliseconds, as returned by |
| 1105 | * {@link System#currentTimeMillis()} for example. |
| 1106 | */ |
| 1107 | public long getTriggerTime() { |
| 1108 | return mTriggerTime; |
| 1109 | } |
| 1110 | |
| 1111 | /** |
Shuhrat Dehkanov | 66729ff | 2015-05-13 17:16:29 +0900 | [diff] [blame] | 1112 | * Returns an intent that can be used to show or edit details of the alarm clock in |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1113 | * the application that scheduled it. |
| 1114 | * |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 1115 | * <p class="note">Beware that any application can retrieve and send this intent, |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1116 | * potentially with additional fields filled in. See |
| 1117 | * {@link PendingIntent#send(android.content.Context, int, android.content.Intent) |
| 1118 | * PendingIntent.send()} and {@link android.content.Intent#fillIn Intent.fillIn()} |
| 1119 | * for details. |
| 1120 | */ |
| 1121 | public PendingIntent getShowIntent() { |
| 1122 | return mShowIntent; |
| 1123 | } |
| 1124 | |
| 1125 | @Override |
| 1126 | public int describeContents() { |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | @Override |
| 1131 | public void writeToParcel(Parcel dest, int flags) { |
| 1132 | dest.writeLong(mTriggerTime); |
| 1133 | dest.writeParcelable(mShowIntent, flags); |
| 1134 | } |
| 1135 | |
| 1136 | public static final Creator<AlarmClockInfo> CREATOR = new Creator<AlarmClockInfo>() { |
| 1137 | @Override |
| 1138 | public AlarmClockInfo createFromParcel(Parcel in) { |
| 1139 | return new AlarmClockInfo(in); |
| 1140 | } |
| 1141 | |
| 1142 | @Override |
| 1143 | public AlarmClockInfo[] newArray(int size) { |
| 1144 | return new AlarmClockInfo[size]; |
| 1145 | } |
| 1146 | }; |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 1147 | |
| 1148 | /** @hide */ |
| 1149 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 1150 | final long token = proto.start(fieldId); |
| 1151 | proto.write(AlarmClockInfoProto.TRIGGER_TIME_MS, mTriggerTime); |
Kweku Adams | ce8e43f | 2018-09-04 11:54:46 -0700 | [diff] [blame] | 1152 | if (mShowIntent != null) { |
| 1153 | mShowIntent.writeToProto(proto, AlarmClockInfoProto.SHOW_INTENT); |
| 1154 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 1155 | proto.end(token); |
| 1156 | } |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1157 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | } |