blob: 2813e8b9707e00daea01edf0c491687ee7a17369 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.app;
18
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060019import android.annotation.IntDef;
Christopher Tate8d3079f2017-06-05 18:13:52 -070020import android.annotation.RequiresPermission;
Adrian Roosc42a1e12014-07-07 23:35:53 +020021import android.annotation.SdkConstant;
David Christiec20b7952014-09-04 11:29:01 -070022import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060023import android.annotation.SystemService;
Christopher Tatee0a22b32013-07-11 14:43:13 -070024import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Intent;
Christopher Tatee0a22b32013-07-11 14:43:13 -070026import android.os.Build;
Christopher Tate14a7bb02015-10-01 10:24:31 -070027import android.os.Handler;
Jose Lima235510e2014-08-13 12:50:01 -070028import android.os.Parcel;
29import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.RemoteException;
Adrian Roosc42a1e12014-07-07 23:35:53 +020031import android.os.UserHandle;
David Christieebe51fc2013-07-26 13:23:29 -070032import android.os.WorkSource;
Narayan Kamatha78240b2015-04-24 13:22:03 +010033import android.text.TextUtils;
Christopher Tate09d7d8f2016-05-31 15:08:04 -070034import android.util.ArrayMap;
Christopher Tate14a7bb02015-10-01 10:24:31 -070035import android.util.Log;
36
Narayan Kamatha78240b2015-04-24 13:22:03 +010037import libcore.util.ZoneInfoDB;
38
39import java.io.IOException;
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060040import java.lang.annotation.Retention;
41import java.lang.annotation.RetentionPolicy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43/**
44 * This class provides access to the system alarm services. These allow you
45 * to schedule your application to be run at some point in the future. When
46 * an alarm goes off, the {@link Intent} that had been registered for it
47 * is broadcast by the system, automatically starting the target application
48 * if it is not already running. Registered alarms are retained while the
49 * device is asleep (and can optionally wake the device up if they go off
50 * during that time), but will be cleared if it is turned off and rebooted.
Chris Tatea34df8a22009-04-02 23:15:58 -070051 *
52 * <p>The Alarm Manager holds a CPU wake lock as long as the alarm receiver's
53 * onReceive() method is executing. This guarantees that the phone will not sleep
54 * until you have finished handling the broadcast. Once onReceive() returns, the
55 * Alarm Manager releases this wake lock. This means that the phone will in some
56 * cases sleep as soon as your onReceive() method completes. If your alarm receiver
57 * called {@link android.content.Context#startService Context.startService()}, it
58 * is possible that the phone will sleep before the requested service is launched.
59 * To prevent this, your BroadcastReceiver and Service will need to implement a
60 * separate wake lock policy to ensure that the phone continues running until the
61 * service becomes available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 *
63 * <p><b>Note: The Alarm Manager is intended for cases where you want to have
64 * your application code run at a specific time, even if your application is
65 * not currently running. For normal timing operations (ticks, timeouts,
66 * etc) it is easier and much more efficient to use
67 * {@link android.os.Handler}.</b>
68 *
Christopher Tate109e4db2013-10-25 16:14:38 -070069 * <p class="caution"><strong>Note:</strong> Beginning with API 19
70 * ({@link android.os.Build.VERSION_CODES#KITKAT}) alarm delivery is inexact:
71 * the OS will shift alarms in order to minimize wakeups and battery use. There are
72 * new APIs to support applications which need strict delivery guarantees; see
73 * {@link #setWindow(int, long, long, PendingIntent)} and
74 * {@link #setExact(int, long, PendingIntent)}. Applications whose {@code targetSdkVersion}
75 * is earlier than API 19 will continue to see the previous behavior in which all
76 * alarms are delivered exactly when requested.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060078@SystemService(Context.ALARM_SERVICE)
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -070079public class AlarmManager {
Christopher Tate14a7bb02015-10-01 10:24:31 -070080 private static final String TAG = "AlarmManager";
81
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060082 /** @hide */
83 @IntDef(prefix = { "RTC", "ELAPSED" }, value = {
84 RTC_WAKEUP,
85 RTC,
86 ELAPSED_REALTIME_WAKEUP,
87 ELAPSED_REALTIME,
88 })
89 @Retention(RetentionPolicy.SOURCE)
90 public @interface AlarmType {}
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
93 * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
94 * (wall clock time in UTC), which will wake up the device when
95 * it goes off.
96 */
97 public static final int RTC_WAKEUP = 0;
98 /**
99 * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
100 * (wall clock time in UTC). This alarm does not wake the
101 * device up; if it goes off while the device is asleep, it will not be
102 * delivered until the next time the device wakes up.
103 */
104 public static final int RTC = 1;
105 /**
106 * Alarm time in {@link android.os.SystemClock#elapsedRealtime
107 * SystemClock.elapsedRealtime()} (time since boot, including sleep),
108 * which will wake up the device when it goes off.
109 */
110 public static final int ELAPSED_REALTIME_WAKEUP = 2;
111 /**
112 * Alarm time in {@link android.os.SystemClock#elapsedRealtime
113 * SystemClock.elapsedRealtime()} (time since boot, including sleep).
114 * This alarm does not wake the device up; if it goes off while the device
115 * is asleep, it will not be delivered until the next time the device
116 * wakes up.
117 */
118 public static final int ELAPSED_REALTIME = 3;
119
Adrian Roosc42a1e12014-07-07 23:35:53 +0200120 /**
121 * Broadcast Action: Sent after the value returned by
122 * {@link #getNextAlarmClock()} has changed.
123 *
124 * <p class="note">This is a protected intent that can only be sent by the system.
125 * It is only sent to registered receivers.</p>
126 */
127 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
128 public static final String ACTION_NEXT_ALARM_CLOCK_CHANGED =
129 "android.app.action.NEXT_ALARM_CLOCK_CHANGED";
130
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700131 /** @hide */
132 public static final long WINDOW_EXACT = 0;
133 /** @hide */
134 public static final long WINDOW_HEURISTIC = -1;
135
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700136 /**
137 * Flag for alarms: this is to be a stand-alone alarm, that should not be batched with
138 * other alarms.
139 * @hide
140 */
141 public static final int FLAG_STANDALONE = 1<<0;
142
143 /**
144 * Flag for alarms: this alarm would like to wake the device even if it is idle. This
145 * is, for example, an alarm for an alarm clock.
146 * @hide
147 */
148 public static final int FLAG_WAKE_FROM_IDLE = 1<<1;
149
150 /**
151 * Flag for alarms: this alarm would like to still execute even if the device is
152 * idle. This won't bring the device out of idle, just allow this specific alarm to
153 * run. Note that this means the actual time this alarm goes off can be inconsistent
154 * with the time of non-allow-while-idle alarms (it could go earlier than the time
155 * requested by another alarm).
156 *
157 * @hide
158 */
159 public static final int FLAG_ALLOW_WHILE_IDLE = 1<<2;
160
161 /**
Dianne Hackborn3d1933c42015-06-10 16:25:57 -0700162 * Flag for alarms: same as {@link #FLAG_ALLOW_WHILE_IDLE}, but doesn't have restrictions
163 * on how frequently it can be scheduled. Only available (and automatically applied) to
164 * system alarms.
165 *
166 * @hide
167 */
168 public static final int FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED = 1<<3;
169
170 /**
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700171 * Flag for alarms: this alarm marks the point where we would like to come out of idle
172 * mode. It may be moved by the alarm manager to match the first wake-from-idle alarm.
173 * Scheduling an alarm with this flag puts the alarm manager in to idle mode, where it
174 * avoids scheduling any further alarms until the marker alarm is executed.
175 * @hide
176 */
Dianne Hackborn3d1933c42015-06-10 16:25:57 -0700177 public static final int FLAG_IDLE_UNTIL = 1<<4;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 private final IAlarmManager mService;
Christopher Tate14a7bb02015-10-01 10:24:31 -0700180 private final String mPackageName;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700181 private final boolean mAlwaysExact;
Narayan Kamatha78240b2015-04-24 13:22:03 +0100182 private final int mTargetSdkVersion;
Christopher Tate14a7bb02015-10-01 10:24:31 -0700183 private final Handler mMainThreadHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184
Christopher Tate14a7bb02015-10-01 10:24:31 -0700185 /**
186 * Direct-notification alarms: the requester must be running continuously from the
187 * time the alarm is set to the time it is delivered, or delivery will fail. Only
188 * one-shot alarms can be set using this mechanism, not repeating alarms.
189 */
190 public interface OnAlarmListener {
191 /**
192 * Callback method that is invoked by the system when the alarm time is reached.
193 */
194 public void onAlarm();
195 }
196
197 final class ListenerWrapper extends IAlarmListener.Stub implements Runnable {
198 final OnAlarmListener mListener;
199 Handler mHandler;
200 IAlarmCompleteListener mCompletion;
201
202 public ListenerWrapper(OnAlarmListener listener) {
203 mListener = listener;
204 }
205
206 public void setHandler(Handler h) {
207 mHandler = h;
208 }
209
210 public void cancel() {
211 try {
212 mService.remove(null, this);
213 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700214 throw ex.rethrowFromSystemServer();
Christopher Tate14a7bb02015-10-01 10:24:31 -0700215 }
216
217 synchronized (AlarmManager.class) {
218 if (sWrappers != null) {
219 sWrappers.remove(mListener);
220 }
221 }
222 }
223
224 @Override
225 public void doAlarm(IAlarmCompleteListener alarmManager) {
226 mCompletion = alarmManager;
Christopher Tate14a7bb02015-10-01 10:24:31 -0700227
Christopher Tate14a7bb02015-10-01 10:24:31 -0700228 // Remove this listener from the wrapper cache first; the server side
229 // already considers it gone
230 synchronized (AlarmManager.class) {
231 if (sWrappers != null) {
232 sWrappers.remove(mListener);
233 }
234 }
235
Makoto Onukia7d39cf2017-05-04 08:38:07 -0700236 mHandler.post(this);
237 }
238
239 @Override
240 public void run() {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700241 // Now deliver it to the app
242 try {
243 mListener.onAlarm();
244 } finally {
245 // No catch -- make sure to report completion to the system process,
246 // but continue to allow the exception to crash the app.
247
248 try {
249 mCompletion.alarmComplete(this);
250 } catch (Exception e) {
251 Log.e(TAG, "Unable to report completion to Alarm Manager!", e);
252 }
253 }
254 }
255 }
256
257 // Tracking of the OnAlarmListener -> wrapper mapping, for cancel() support.
258 // Access is synchronized on the AlarmManager class object.
Christopher Tate09d7d8f2016-05-31 15:08:04 -0700259 private static ArrayMap<OnAlarmListener, ListenerWrapper> sWrappers;
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * package private on purpose
263 */
Christopher Tatee0a22b32013-07-11 14:43:13 -0700264 AlarmManager(IAlarmManager service, Context ctx) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mService = service;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700266
Christopher Tate14a7bb02015-10-01 10:24:31 -0700267 mPackageName = ctx.getPackageName();
Narayan Kamatha78240b2015-04-24 13:22:03 +0100268 mTargetSdkVersion = ctx.getApplicationInfo().targetSdkVersion;
269 mAlwaysExact = (mTargetSdkVersion < Build.VERSION_CODES.KITKAT);
Christopher Tate14a7bb02015-10-01 10:24:31 -0700270 mMainThreadHandler = new Handler(ctx.getMainLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700272
273 private long legacyExactLength() {
274 return (mAlwaysExact ? WINDOW_EXACT : WINDOW_HEURISTIC);
275 }
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700278 * <p>Schedule an alarm. <b>Note: for timing operations (ticks, timeouts,
Christopher Tate062bce72013-10-25 13:59:44 -0700279 * etc) it is easier and much more efficient to use {@link android.os.Handler}.</b>
280 * If there is already an alarm scheduled for the same IntentSender, that previous
281 * alarm will first be canceled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 *
Christopher Tate062bce72013-10-25 13:59:44 -0700283 * <p>If the stated trigger time is in the past, the alarm will be triggered
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * immediately. If there is already an alarm for this Intent
285 * scheduled (with the equality of two intents being defined by
286 * {@link Intent#filterEquals}), then it will be removed and replaced by
287 * this one.
288 *
289 * <p>
Christopher Tate062bce72013-10-25 13:59:44 -0700290 * The alarm is an Intent broadcast that goes to a broadcast receiver that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 * you registered with {@link android.content.Context#registerReceiver}
292 * or through the &lt;receiver&gt; tag in an AndroidManifest.xml file.
293 *
294 * <p>
295 * Alarm intents are delivered with a data extra of type int called
296 * {@link Intent#EXTRA_ALARM_COUNT Intent.EXTRA_ALARM_COUNT} that indicates
297 * how many past alarm events have been accumulated into this intent
298 * broadcast. Recurring alarms that have gone undelivered because the
299 * phone was asleep may have a count greater than one when delivered.
Christopher Tate062bce72013-10-25 13:59:44 -0700300 *
Christopher Tate109e4db2013-10-25 16:14:38 -0700301 * <div class="note">
Christopher Tate062bce72013-10-25 13:59:44 -0700302 * <p>
303 * <b>Note:</b> Beginning in API 19, the trigger time passed to this method
304 * is treated as inexact: the alarm will not be delivered before this time, but
305 * may be deferred and delivered some time later. The OS will use
306 * this policy in order to "batch" alarms together across the entire system,
307 * minimizing the number of times the device needs to "wake up" and minimizing
308 * battery use. In general, alarms scheduled in the near future will not
309 * be deferred as long as alarms scheduled far in the future.
310 *
311 * <p>
312 * With the new batching policy, delivery ordering guarantees are not as
313 * strong as they were previously. If the application sets multiple alarms,
Christopher Tate109e4db2013-10-25 16:14:38 -0700314 * it is possible that these alarms' <em>actual</em> delivery ordering may not match
315 * the order of their <em>requested</em> delivery times. If your application has
Christopher Tate062bce72013-10-25 13:59:44 -0700316 * strong ordering requirements there are other APIs that you can use to get
317 * the necessary behavior; see {@link #setWindow(int, long, long, PendingIntent)}
318 * and {@link #setExact(int, long, PendingIntent)}.
319 *
320 * <p>
Christopher Tate109e4db2013-10-25 16:14:38 -0700321 * Applications whose {@code targetSdkVersion} is before API 19 will
Christopher Tate062bce72013-10-25 13:59:44 -0700322 * continue to get the previous alarm behavior: all of their scheduled alarms
323 * will be treated as exact.
Christopher Tate109e4db2013-10-25 16:14:38 -0700324 * </div>
Christopher Tate062bce72013-10-25 13:59:44 -0700325 *
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600326 * @param type type of alarm.
Jesse Wilson79074cd2011-12-22 22:51:37 -0500327 * @param triggerAtMillis time in milliseconds that the alarm should go
328 * off, using the appropriate clock (depending on the alarm type).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 * @param operation Action to perform when the alarm goes off;
330 * typically comes from {@link PendingIntent#getBroadcast
331 * IntentSender.getBroadcast()}.
332 *
333 * @see android.os.Handler
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700334 * @see #setExact
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 * @see #setRepeating
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700336 * @see #setWindow
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * @see #cancel
338 * @see android.content.Context#sendBroadcast
339 * @see android.content.Context#registerReceiver
340 * @see android.content.Intent#filterEquals
341 * @see #ELAPSED_REALTIME
342 * @see #ELAPSED_REALTIME_WAKEUP
343 * @see #RTC
344 * @see #RTC_WAKEUP
345 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600346 public void set(@AlarmType int type, long triggerAtMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700347 setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, operation, null, null,
348 null, null, null);
349 }
350
351 /**
352 * Direct callback version of {@link #set(int, long, PendingIntent)}. Rather than
353 * supplying a PendingIntent to be sent when the alarm time is reached, this variant
354 * supplies an {@link OnAlarmListener} instance that will be invoked at that time.
355 * <p>
356 * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
357 * invoked via the specified target Handler, or on the application's main looper
358 * if {@code null} is passed as the {@code targetHandler} parameter.
359 *
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600360 * @param type type of alarm.
Christopher Tate14a7bb02015-10-01 10:24:31 -0700361 * @param triggerAtMillis time in milliseconds that the alarm should go
362 * off, using the appropriate clock (depending on the alarm type).
363 * @param tag string describing the alarm, used for logging and battery-use
364 * attribution
365 * @param listener {@link OnAlarmListener} instance whose
366 * {@link OnAlarmListener#onAlarm() onAlarm()} method will be
367 * called when the alarm time is reached. A given OnAlarmListener instance can
368 * only be the target of a single pending alarm, just as a given PendingIntent
369 * can only be used with one alarm at a time.
370 * @param targetHandler {@link Handler} on which to execute the listener's onAlarm()
371 * callback, or {@code null} to run that callback on the main looper.
372 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600373 public void set(@AlarmType int type, long triggerAtMillis, String tag, OnAlarmListener listener,
Christopher Tate14a7bb02015-10-01 10:24:31 -0700374 Handler targetHandler) {
375 setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, null, listener, tag,
376 targetHandler, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378
379 /**
380 * Schedule a repeating alarm. <b>Note: for timing operations (ticks,
381 * timeouts, etc) it is easier and much more efficient to use
382 * {@link android.os.Handler}.</b> If there is already an alarm scheduled
383 * for the same IntentSender, it will first be canceled.
384 *
Christopher Tate062bce72013-10-25 13:59:44 -0700385 * <p>Like {@link #set}, except you can also supply a period at which
386 * the alarm will automatically repeat. This alarm continues
387 * repeating until explicitly removed with {@link #cancel}. If the stated
388 * trigger time is in the past, the alarm will be triggered immediately, with an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * alarm count depending on how far in the past the trigger time is relative
390 * to the repeat interval.
391 *
392 * <p>If an alarm is delayed (by system sleep, for example, for non
393 * _WAKEUP alarm types), a skipped repeat will be delivered as soon as
394 * possible. After that, future alarms will be delivered according to the
395 * original schedule; they do not drift over time. For example, if you have
396 * set a recurring alarm for the top of every hour but the phone was asleep
397 * from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens,
398 * then the next alarm will be sent at 9:00.
399 *
400 * <p>If your application wants to allow the delivery times to drift in
401 * order to guarantee that at least a certain time interval always elapses
402 * between alarms, then the approach to take is to use one-time alarms,
403 * scheduling the next one yourself when handling each alarm delivery.
404 *
Christopher Tate109e4db2013-10-25 16:14:38 -0700405 * <p class="note">
Christopher Tate062bce72013-10-25 13:59:44 -0700406 * <b>Note:</b> as of API 19, all repeating alarms are inexact. If your
407 * application needs precise delivery times then it must use one-time
408 * exact alarms, rescheduling each time as described above. Legacy applications
Christopher Tate109e4db2013-10-25 16:14:38 -0700409 * whose {@code targetSdkVersion} is earlier than API 19 will continue to have all
Christopher Tate062bce72013-10-25 13:59:44 -0700410 * of their alarms, including repeating alarms, treated as exact.
411 *
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600412 * @param type type of alarm.
Jesse Wilson79074cd2011-12-22 22:51:37 -0500413 * @param triggerAtMillis time in milliseconds that the alarm should first
414 * go off, using the appropriate clock (depending on the alarm type).
415 * @param intervalMillis interval in milliseconds between subsequent repeats
416 * of the alarm.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 * @param operation Action to perform when the alarm goes off;
418 * typically comes from {@link PendingIntent#getBroadcast
419 * IntentSender.getBroadcast()}.
420 *
421 * @see android.os.Handler
422 * @see #set
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700423 * @see #setExact
424 * @see #setWindow
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * @see #cancel
426 * @see android.content.Context#sendBroadcast
427 * @see android.content.Context#registerReceiver
428 * @see android.content.Intent#filterEquals
429 * @see #ELAPSED_REALTIME
430 * @see #ELAPSED_REALTIME_WAKEUP
431 * @see #RTC
432 * @see #RTC_WAKEUP
433 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600434 public void setRepeating(@AlarmType int type, long triggerAtMillis,
Jesse Wilson79074cd2011-12-22 22:51:37 -0500435 long intervalMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700436 setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, 0, operation,
437 null, null, null, null, null);
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700438 }
439
440 /**
Christopher Tate062bce72013-10-25 13:59:44 -0700441 * Schedule an alarm to be delivered within a given window of time. This method
442 * is similar to {@link #set(int, long, PendingIntent)}, but allows the
443 * application to precisely control the degree to which its delivery might be
444 * adjusted by the OS. This method allows an application to take advantage of the
445 * battery optimizations that arise from delivery batching even when it has
446 * modest timeliness requirements for its alarms.
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700447 *
Christopher Tate062bce72013-10-25 13:59:44 -0700448 * <p>
Christopher Tate109e4db2013-10-25 16:14:38 -0700449 * This method can also be used to achieve strict ordering guarantees among
450 * multiple alarms by ensuring that the windows requested for each alarm do
451 * not intersect.
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700452 *
Christopher Tate062bce72013-10-25 13:59:44 -0700453 * <p>
454 * When precise delivery is not required, applications should use the standard
455 * {@link #set(int, long, PendingIntent)} method. This will give the OS the most
Christopher Tate109e4db2013-10-25 16:14:38 -0700456 * flexibility to minimize wakeups and battery use. For alarms that must be delivered
Christopher Tate062bce72013-10-25 13:59:44 -0700457 * at precisely-specified times with no acceptable variation, applications can use
458 * {@link #setExact(int, long, PendingIntent)}.
459 *
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600460 * @param type type of alarm.
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700461 * @param windowStartMillis The earliest time, in milliseconds, that the alarm should
462 * be delivered, expressed in the appropriate clock's units (depending on the alarm
463 * type).
464 * @param windowLengthMillis The length of the requested delivery window,
465 * in milliseconds. The alarm will be delivered no later than this many
Christopher Tate062bce72013-10-25 13:59:44 -0700466 * milliseconds after {@code windowStartMillis}. Note that this parameter
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700467 * is a <i>duration,</i> not the timestamp of the end of the window.
468 * @param operation Action to perform when the alarm goes off;
469 * typically comes from {@link PendingIntent#getBroadcast
470 * IntentSender.getBroadcast()}.
471 *
472 * @see #set
473 * @see #setExact
474 * @see #setRepeating
475 * @see #cancel
476 * @see android.content.Context#sendBroadcast
477 * @see android.content.Context#registerReceiver
478 * @see android.content.Intent#filterEquals
479 * @see #ELAPSED_REALTIME
480 * @see #ELAPSED_REALTIME_WAKEUP
481 * @see #RTC
482 * @see #RTC_WAKEUP
483 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600484 public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis,
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700485 PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700486 setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, operation,
487 null, null, null, null, null);
488 }
489
490 /**
491 * Direct callback version of {@link #setWindow(int, long, long, PendingIntent)}. Rather
492 * than supplying a PendingIntent to be sent when the alarm time is reached, this variant
493 * supplies an {@link OnAlarmListener} instance that will be invoked at that time.
494 * <p>
495 * The OnAlarmListener {@link OnAlarmListener#onAlarm() onAlarm()} method will be
496 * invoked via the specified target Handler, or on the application's main looper
497 * if {@code null} is passed as the {@code targetHandler} parameter.
498 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600499 public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis,
Christopher Tate14a7bb02015-10-01 10:24:31 -0700500 String tag, OnAlarmListener listener, Handler targetHandler) {
501 setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, null, listener, tag,
502 targetHandler, null, null);
Christopher Tatee0a22b32013-07-11 14:43:13 -0700503 }
504
505 /**
Christopher Tate062bce72013-10-25 13:59:44 -0700506 * Schedule an alarm to be delivered precisely at the stated time.
507 *
508 * <p>
509 * This method is like {@link #set(int, long, PendingIntent)}, but does not permit
510 * the OS to adjust the delivery time. The alarm will be delivered as nearly as
511 * possible to the requested trigger time.
512 *
513 * <p>
514 * <b>Note:</b> only alarms for which there is a strong demand for exact-time
515 * delivery (such as an alarm clock ringing at the requested time) should be
516 * scheduled as exact. Applications are strongly discouraged from using exact
517 * alarms unnecessarily as they reduce the OS's ability to minimize battery use.
518 *
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600519 * @param type type of alarm.
Christopher Tate062bce72013-10-25 13:59:44 -0700520 * @param triggerAtMillis time in milliseconds that the alarm should go
521 * off, using the appropriate clock (depending on the alarm type).
522 * @param operation Action to perform when the alarm goes off;
523 * typically comes from {@link PendingIntent#getBroadcast
524 * IntentSender.getBroadcast()}.
525 *
526 * @see #set
527 * @see #setRepeating
528 * @see #setWindow
529 * @see #cancel
530 * @see android.content.Context#sendBroadcast
531 * @see android.content.Context#registerReceiver
532 * @see android.content.Intent#filterEquals
533 * @see #ELAPSED_REALTIME
534 * @see #ELAPSED_REALTIME_WAKEUP
535 * @see #RTC
536 * @see #RTC_WAKEUP
Christopher Tatee0a22b32013-07-11 14:43:13 -0700537 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600538 public void setExact(@AlarmType int type, long triggerAtMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700539 setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, operation, null, null, null,
540 null, null);
541 }
542
543 /**
544 * Direct callback version of {@link #setExact(int, long, PendingIntent)}. Rather
545 * than supplying a PendingIntent to be sent when the alarm time is reached, this variant
546 * supplies an {@link OnAlarmListener} instance that will be invoked at that time.
547 * <p>
548 * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
549 * invoked via the specified target Handler, or on the application's main looper
550 * if {@code null} is passed as the {@code targetHandler} parameter.
551 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600552 public void setExact(@AlarmType int type, long triggerAtMillis, String tag,
553 OnAlarmListener listener, Handler targetHandler) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700554 setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, null, listener, tag,
555 targetHandler, null, null);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700556 }
557
558 /**
559 * Schedule an idle-until alarm, which will keep the alarm manager idle until
560 * the given time.
561 * @hide
562 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600563 public void setIdleUntil(@AlarmType int type, long triggerAtMillis, String tag,
564 OnAlarmListener listener, Handler targetHandler) {
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700565 setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_IDLE_UNTIL, null,
566 listener, tag, targetHandler, null, null);
Adrian Roosc42a1e12014-07-07 23:35:53 +0200567 }
568
569 /**
570 * Schedule an alarm that represents an alarm clock.
571 *
572 * The system may choose to display information about this alarm to the user.
573 *
574 * <p>
575 * This method is like {@link #setExact(int, long, PendingIntent)}, but implies
576 * {@link #RTC_WAKEUP}.
577 *
578 * @param info
579 * @param operation Action to perform when the alarm goes off;
580 * typically comes from {@link PendingIntent#getBroadcast
581 * IntentSender.getBroadcast()}.
582 *
583 * @see #set
584 * @see #setRepeating
585 * @see #setWindow
586 * @see #setExact
587 * @see #cancel
588 * @see #getNextAlarmClock()
589 * @see android.content.Context#sendBroadcast
590 * @see android.content.Context#registerReceiver
591 * @see android.content.Intent#filterEquals
592 */
593 public void setAlarmClock(AlarmClockInfo info, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700594 setImpl(RTC_WAKEUP, info.getTriggerTime(), WINDOW_EXACT, 0, 0, operation,
595 null, null, null, null, info);
David Christieebe51fc2013-07-26 13:23:29 -0700596 }
597
598 /** @hide */
David Christiec20b7952014-09-04 11:29:01 -0700599 @SystemApi
Christopher Tate8d3079f2017-06-05 18:13:52 -0700600 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600601 public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
602 long intervalMillis, PendingIntent operation, WorkSource workSource) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700603 setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, operation, null, null,
604 null, workSource, null);
605 }
606
607 /**
608 * Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}.
609 * Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener.
610 * <p>
611 * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
612 * invoked via the specified target Handler, or on the application's main looper
613 * if {@code null} is passed as the {@code targetHandler} parameter.
614 *
615 * @hide
616 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600617 public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
618 long intervalMillis, String tag, OnAlarmListener listener, Handler targetHandler,
619 WorkSource workSource) {
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700620 setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, tag,
621 targetHandler, workSource, null);
622 }
623
624 /**
625 * Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}.
626 * Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener.
627 * <p>
628 * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
629 * invoked via the specified target Handler, or on the application's main looper
630 * if {@code null} is passed as the {@code targetHandler} parameter.
631 *
632 * @hide
633 */
Christopher Tatef2d753e2015-11-04 11:10:48 -0800634 @SystemApi
Christopher Tate8d3079f2017-06-05 18:13:52 -0700635 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600636 public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
637 long intervalMillis, OnAlarmListener listener, Handler targetHandler,
638 WorkSource workSource) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700639 setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, null,
640 targetHandler, workSource, null);
Christopher Tatee0a22b32013-07-11 14:43:13 -0700641 }
642
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600643 private void setImpl(@AlarmType int type, long triggerAtMillis, long windowMillis,
644 long intervalMillis, int flags, PendingIntent operation, final OnAlarmListener listener,
645 String listenerTag, Handler targetHandler, WorkSource workSource,
646 AlarmClockInfo alarmClock) {
Christopher Tate5f221e82013-07-30 17:13:15 -0700647 if (triggerAtMillis < 0) {
Christopher Tate56cfa242013-07-30 19:09:41 -0700648 /* NOTYET
Christopher Tate5f221e82013-07-30 17:13:15 -0700649 if (mAlwaysExact) {
650 // Fatal error for KLP+ apps to use negative trigger times
651 throw new IllegalArgumentException("Invalid alarm trigger time "
652 + triggerAtMillis);
653 }
Christopher Tate56cfa242013-07-30 19:09:41 -0700654 */
Christopher Tate5f221e82013-07-30 17:13:15 -0700655 triggerAtMillis = 0;
656 }
David Christieebe51fc2013-07-26 13:23:29 -0700657
Christopher Tate14a7bb02015-10-01 10:24:31 -0700658 ListenerWrapper recipientWrapper = null;
659 if (listener != null) {
660 synchronized (AlarmManager.class) {
661 if (sWrappers == null) {
Christopher Tate09d7d8f2016-05-31 15:08:04 -0700662 sWrappers = new ArrayMap<OnAlarmListener, ListenerWrapper>();
Christopher Tate14a7bb02015-10-01 10:24:31 -0700663 }
664
Christopher Tated0cca792016-04-21 15:05:34 -0700665 recipientWrapper = sWrappers.get(listener);
666 // no existing wrapper => build a new one
667 if (recipientWrapper == null) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700668 recipientWrapper = new ListenerWrapper(listener);
Christopher Tated0cca792016-04-21 15:05:34 -0700669 sWrappers.put(listener, recipientWrapper);
Christopher Tate14a7bb02015-10-01 10:24:31 -0700670 }
671 }
672
673 final Handler handler = (targetHandler != null) ? targetHandler : mMainThreadHandler;
674 recipientWrapper.setHandler(handler);
675 }
676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 try {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700678 mService.set(mPackageName, type, triggerAtMillis, windowMillis, intervalMillis, flags,
679 operation, recipientWrapper, listenerTag, workSource, alarmClock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700681 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683 }
684
685 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700686 * Available inexact recurrence interval recognized by
687 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
688 * when running on Android prior to API 19.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 */
690 public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700691
692 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700693 * Available inexact recurrence interval recognized by
694 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
695 * when running on Android prior to API 19.
Christopher Tatee0a22b32013-07-11 14:43:13 -0700696 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700698
699 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700700 * Available inexact recurrence interval recognized by
701 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
702 * when running on Android prior to API 19.
Christopher Tatee0a22b32013-07-11 14:43:13 -0700703 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700705
706 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700707 * Available inexact recurrence interval recognized by
708 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
709 * when running on Android prior to API 19.
Christopher Tatee0a22b32013-07-11 14:43:13 -0700710 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700712
713 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700714 * Available inexact recurrence interval recognized by
715 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
716 * when running on Android prior to API 19.
Christopher Tatee0a22b32013-07-11 14:43:13 -0700717 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 /**
721 * Schedule a repeating alarm that has inexact trigger time requirements;
722 * for example, an alarm that repeats every hour, but not necessarily at
723 * the top of every hour. These alarms are more power-efficient than
Christopher Tate109e4db2013-10-25 16:14:38 -0700724 * the strict recurrences traditionally supplied by {@link #setRepeating}, since the
725 * system can adjust alarms' delivery times to cause them to fire simultaneously,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 * avoiding waking the device from sleep more than necessary.
Christopher Tate109e4db2013-10-25 16:14:38 -0700727 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 * <p>Your alarm's first trigger will not be before the requested time,
729 * but it might not occur for almost a full interval after that time. In
730 * addition, while the overall period of the repeating alarm will be as
731 * requested, the time between any two successive firings of the alarm
732 * may vary. If your application demands very low jitter, use
Christopher Tate109e4db2013-10-25 16:14:38 -0700733 * one-shot alarms with an appropriate window instead; see {@link
734 * #setWindow(int, long, long, PendingIntent)} and
735 * {@link #setExact(int, long, PendingIntent)}.
736 *
737 * <p class="note">
738 * As of API 19, all repeating alarms are inexact. Because this method has
739 * been available since API 3, your application can safely call it and be
740 * assured that it will get similar behavior on both current and older versions
741 * of Android.
Jesse Wilson79074cd2011-12-22 22:51:37 -0500742 *
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600743 * @param type type of alarm.
Jesse Wilson79074cd2011-12-22 22:51:37 -0500744 * @param triggerAtMillis time in milliseconds that the alarm should first
745 * go off, using the appropriate clock (depending on the alarm type). This
746 * is inexact: the alarm will not fire before this time, but there may be a
747 * delay of almost an entire alarm interval before the first invocation of
748 * the alarm.
749 * @param intervalMillis interval in milliseconds between subsequent repeats
Christopher Tate109e4db2013-10-25 16:14:38 -0700750 * of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES,
Jesse Wilson79074cd2011-12-22 22:51:37 -0500751 * INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY
752 * then the alarm will be phase-aligned with other alarms to reduce the
753 * number of wakeups. Otherwise, the alarm will be set as though the
Christopher Tate109e4db2013-10-25 16:14:38 -0700754 * application had called {@link #setRepeating}. As of API 19, all repeating
755 * alarms will be inexact and subject to batching with other alarms regardless
756 * of their stated repeat interval.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 * @param operation Action to perform when the alarm goes off;
758 * typically comes from {@link PendingIntent#getBroadcast
759 * IntentSender.getBroadcast()}.
760 *
761 * @see android.os.Handler
762 * @see #set
763 * @see #cancel
764 * @see android.content.Context#sendBroadcast
765 * @see android.content.Context#registerReceiver
766 * @see android.content.Intent#filterEquals
767 * @see #ELAPSED_REALTIME
768 * @see #ELAPSED_REALTIME_WAKEUP
769 * @see #RTC
770 * @see #RTC_WAKEUP
771 * @see #INTERVAL_FIFTEEN_MINUTES
772 * @see #INTERVAL_HALF_HOUR
773 * @see #INTERVAL_HOUR
774 * @see #INTERVAL_HALF_DAY
775 * @see #INTERVAL_DAY
776 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600777 public void setInexactRepeating(@AlarmType int type, long triggerAtMillis,
Jesse Wilson79074cd2011-12-22 22:51:37 -0500778 long intervalMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700779 setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, intervalMillis, 0, operation, null,
780 null, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 }
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700782
783 /**
784 * Like {@link #set(int, long, PendingIntent)}, but this alarm will be allowed to execute
785 * even when the system is in low-power idle modes. This type of alarm must <b>only</b>
786 * be used for situations where it is actually required that the alarm go off while in
787 * idle -- a reasonable example would be for a calendar notification that should make a
Dianne Hackborn14c5ab42015-07-09 18:17:54 -0700788 * sound so the user is aware of it. When the alarm is dispatched, the app will also be
789 * added to the system's temporary whitelist for approximately 10 seconds to allow that
790 * application to acquire further wake locks in which to complete its work.</p>
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700791 *
Dianne Hackborn14c5ab42015-07-09 18:17:54 -0700792 * <p>These alarms can significantly impact the power use
793 * of the device when idle (and thus cause significant battery blame to the app scheduling
794 * them), so they should be used with care. To reduce abuse, there are restrictions on how
795 * frequently these alarms will go off for a particular application.
796 * Under normal system operation, it will not dispatch these
Dianne Hackborn3d1933c42015-06-10 16:25:57 -0700797 * alarms more than about every minute (at which point every such pending alarm is
798 * dispatched); when in low-power idle modes this duration may be significantly longer,
799 * such as 15 minutes.</p>
800 *
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700801 * <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen
802 * out of order with any other alarms, even those from the same app. This will clearly happen
803 * when the device is idle (since this alarm can go off while idle, when any other alarms
804 * from the app will be held until later), but may also happen even when not idle.</p>
805 *
806 * <p>Regardless of the app's target SDK version, this call always allows batching of the
807 * alarm.</p>
808 *
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600809 * @param type type of alarm.
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700810 * @param triggerAtMillis time in milliseconds that the alarm should go
811 * off, using the appropriate clock (depending on the alarm type).
812 * @param operation Action to perform when the alarm goes off;
813 * typically comes from {@link PendingIntent#getBroadcast
814 * IntentSender.getBroadcast()}.
815 *
816 * @see #set(int, long, PendingIntent)
817 * @see #setExactAndAllowWhileIdle
818 * @see #cancel
819 * @see android.content.Context#sendBroadcast
820 * @see android.content.Context#registerReceiver
821 * @see android.content.Intent#filterEquals
822 * @see #ELAPSED_REALTIME
823 * @see #ELAPSED_REALTIME_WAKEUP
824 * @see #RTC
825 * @see #RTC_WAKEUP
826 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600827 public void setAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis,
828 PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700829 setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, 0, FLAG_ALLOW_WHILE_IDLE,
830 operation, null, null, null, null, null);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700831 }
832
833 /**
834 * Like {@link #setExact(int, long, PendingIntent)}, but this alarm will be allowed to execute
835 * even when the system is in low-power idle modes. If you don't need exact scheduling of
836 * the alarm but still need to execute while idle, consider using
837 * {@link #setAndAllowWhileIdle}. This type of alarm must <b>only</b>
838 * be used for situations where it is actually required that the alarm go off while in
839 * idle -- a reasonable example would be for a calendar notification that should make a
Dianne Hackborn14c5ab42015-07-09 18:17:54 -0700840 * sound so the user is aware of it. When the alarm is dispatched, the app will also be
841 * added to the system's temporary whitelist for approximately 10 seconds to allow that
842 * application to acquire further wake locks in which to complete its work.</p>
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700843 *
Dianne Hackborn14c5ab42015-07-09 18:17:54 -0700844 * <p>These alarms can significantly impact the power use
845 * of the device when idle (and thus cause significant battery blame to the app scheduling
846 * them), so they should be used with care. To reduce abuse, there are restrictions on how
847 * frequently these alarms will go off for a particular application.
848 * Under normal system operation, it will not dispatch these
Dianne Hackborn3d1933c42015-06-10 16:25:57 -0700849 * alarms more than about every minute (at which point every such pending alarm is
850 * dispatched); when in low-power idle modes this duration may be significantly longer,
851 * such as 15 minutes.</p>
852 *
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700853 * <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen
854 * out of order with any other alarms, even those from the same app. This will clearly happen
855 * when the device is idle (since this alarm can go off while idle, when any other alarms
856 * from the app will be held until later), but may also happen even when not idle.
857 * Note that the OS will allow itself more flexibility for scheduling these alarms than
858 * regular exact alarms, since the application has opted into this behavior. When the
859 * device is idle it may take even more liberties with scheduling in order to optimize
860 * for battery life.</p>
861 *
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600862 * @param type type of alarm.
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700863 * @param triggerAtMillis time in milliseconds that the alarm should go
864 * off, using the appropriate clock (depending on the alarm type).
865 * @param operation Action to perform when the alarm goes off;
866 * typically comes from {@link PendingIntent#getBroadcast
867 * IntentSender.getBroadcast()}.
868 *
869 * @see #set
870 * @see #setRepeating
871 * @see #setWindow
872 * @see #cancel
873 * @see android.content.Context#sendBroadcast
874 * @see android.content.Context#registerReceiver
875 * @see android.content.Intent#filterEquals
876 * @see #ELAPSED_REALTIME
877 * @see #ELAPSED_REALTIME_WAKEUP
878 * @see #RTC
879 * @see #RTC_WAKEUP
880 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600881 public void setExactAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis,
882 PendingIntent operation) {
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700883 setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_ALLOW_WHILE_IDLE, operation,
Christopher Tate14a7bb02015-10-01 10:24:31 -0700884 null, null, null, null, null);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700885 }
886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 /**
888 * Remove any alarms with a matching {@link Intent}.
889 * Any alarm, of any type, whose Intent matches this one (as defined by
890 * {@link Intent#filterEquals}), will be canceled.
891 *
892 * @param operation IntentSender which matches a previously added
Christopher Tatebb76a6c2015-12-02 10:54:56 -0800893 * IntentSender. This parameter must not be {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 *
895 * @see #set
896 */
897 public void cancel(PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700898 if (operation == null) {
Christopher Tatebb76a6c2015-12-02 10:54:56 -0800899 final String msg = "cancel() called with a null PendingIntent";
900 if (mTargetSdkVersion >= Build.VERSION_CODES.N) {
901 throw new NullPointerException(msg);
902 } else {
903 Log.e(TAG, msg);
904 return;
905 }
Christopher Tate14a7bb02015-10-01 10:24:31 -0700906 }
907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 try {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700909 mService.remove(operation, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700911 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
913 }
Dan Egnor97e44942010-02-04 20:27:47 -0800914
915 /**
Christopher Tate14a7bb02015-10-01 10:24:31 -0700916 * Remove any alarm scheduled to be delivered to the given {@link OnAlarmListener}.
917 *
918 * @param listener OnAlarmListener instance that is the target of a currently-set alarm.
919 */
920 public void cancel(OnAlarmListener listener) {
921 if (listener == null) {
Christopher Tatebb76a6c2015-12-02 10:54:56 -0800922 throw new NullPointerException("cancel() called with a null OnAlarmListener");
Christopher Tate14a7bb02015-10-01 10:24:31 -0700923 }
924
925 ListenerWrapper wrapper = null;
926 synchronized (AlarmManager.class) {
Joe LaPenna33ee4bf2016-04-01 13:37:37 -0700927 if (sWrappers != null) {
Christopher Tated0cca792016-04-21 15:05:34 -0700928 wrapper = sWrappers.get(listener);
Christopher Tate14a7bb02015-10-01 10:24:31 -0700929 }
930 }
931
932 if (wrapper == null) {
933 Log.w(TAG, "Unrecognized alarm listener " + listener);
934 return;
935 }
936
937 wrapper.cancel();
938 }
939
940 /**
Dan Egnor97e44942010-02-04 20:27:47 -0800941 * Set the system wall clock time.
942 * Requires the permission android.permission.SET_TIME.
943 *
944 * @param millis time in milliseconds since the Epoch
945 */
946 public void setTime(long millis) {
947 try {
948 mService.setTime(millis);
949 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700950 throw ex.rethrowFromSystemServer();
Dan Egnor97e44942010-02-04 20:27:47 -0800951 }
952 }
953
954 /**
Narayan Kamatha78240b2015-04-24 13:22:03 +0100955 * Sets the system's persistent default time zone. This is the time zone for all apps, even
956 * after a reboot. Use {@link java.util.TimeZone#setDefault} if you just want to change the
957 * time zone within your app, and even then prefer to pass an explicit
958 * {@link java.util.TimeZone} to APIs that require it rather than changing the time zone for
959 * all threads.
Dan Egnor97e44942010-02-04 20:27:47 -0800960 *
Narayan Kamatha78240b2015-04-24 13:22:03 +0100961 * <p> On android M and above, it is an error to pass in a non-Olson timezone to this
962 * function. Note that this is a bad idea on all Android releases because POSIX and
963 * the {@code TimeZone} class have opposite interpretations of {@code '+'} and {@code '-'}
964 * in the same non-Olson ID.
965 *
966 * @param timeZone one of the Olson ids from the list returned by
967 * {@link java.util.TimeZone#getAvailableIDs}
Dan Egnor97e44942010-02-04 20:27:47 -0800968 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 public void setTimeZone(String timeZone) {
Narayan Kamatha78240b2015-04-24 13:22:03 +0100970 if (TextUtils.isEmpty(timeZone)) {
971 return;
972 }
973
974 // Reject this timezone if it isn't an Olson zone we recognize.
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -0700975 if (mTargetSdkVersion >= Build.VERSION_CODES.M) {
Narayan Kamatha78240b2015-04-24 13:22:03 +0100976 boolean hasTimeZone = false;
977 try {
978 hasTimeZone = ZoneInfoDB.getInstance().hasTimeZone(timeZone);
979 } catch (IOException ignored) {
980 }
981
982 if (!hasTimeZone) {
983 throw new IllegalArgumentException("Timezone: " + timeZone + " is not an Olson ID");
984 }
985 }
986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 try {
988 mService.setTimeZone(timeZone);
989 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700990 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 }
992 }
Adrian Roosc42a1e12014-07-07 23:35:53 +0200993
Dianne Hackbornf70faed2015-04-21 14:11:38 -0700994 /** @hide */
995 public long getNextWakeFromIdleTime() {
996 try {
997 return mService.getNextWakeFromIdleTime();
998 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700999 throw ex.rethrowFromSystemServer();
Dianne Hackbornf70faed2015-04-21 14:11:38 -07001000 }
1001 }
1002
Adrian Roosc42a1e12014-07-07 23:35:53 +02001003 /**
1004 * Gets information about the next alarm clock currently scheduled.
1005 *
Christopher Tate2affae92016-03-09 17:42:52 -08001006 * The alarm clocks considered are those scheduled by any application
1007 * using the {@link #setAlarmClock} method.
1008 *
1009 * @return An {@link AlarmClockInfo} object describing the next upcoming alarm
1010 * clock event that will occur. If there are no alarm clock events currently
1011 * scheduled, this method will return {@code null}.
Adrian Roosc42a1e12014-07-07 23:35:53 +02001012 *
1013 * @see #setAlarmClock
1014 * @see AlarmClockInfo
Christopher Tate2affae92016-03-09 17:42:52 -08001015 * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED
Adrian Roosc42a1e12014-07-07 23:35:53 +02001016 */
1017 public AlarmClockInfo getNextAlarmClock() {
1018 return getNextAlarmClock(UserHandle.myUserId());
1019 }
1020
1021 /**
1022 * Gets information about the next alarm clock currently scheduled.
1023 *
Christopher Tate2affae92016-03-09 17:42:52 -08001024 * The alarm clocks considered are those scheduled by any application
1025 * using the {@link #setAlarmClock} method within the given user.
1026 *
1027 * @return An {@link AlarmClockInfo} object describing the next upcoming alarm
1028 * clock event that will occur within the given user. If there are no alarm clock
1029 * events currently scheduled in that user, this method will return {@code null}.
Adrian Roosc42a1e12014-07-07 23:35:53 +02001030 *
1031 * @see #setAlarmClock
1032 * @see AlarmClockInfo
Christopher Tate2affae92016-03-09 17:42:52 -08001033 * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED
Adrian Roosc42a1e12014-07-07 23:35:53 +02001034 *
1035 * @hide
1036 */
1037 public AlarmClockInfo getNextAlarmClock(int userId) {
1038 try {
1039 return mService.getNextAlarmClock(userId);
1040 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001041 throw ex.rethrowFromSystemServer();
Adrian Roosc42a1e12014-07-07 23:35:53 +02001042 }
1043 }
Jose Lima235510e2014-08-13 12:50:01 -07001044
1045 /**
Christopher Tate2affae92016-03-09 17:42:52 -08001046 * An immutable description of a scheduled "alarm clock" event.
Jose Lima235510e2014-08-13 12:50:01 -07001047 *
1048 * @see AlarmManager#setAlarmClock
1049 * @see AlarmManager#getNextAlarmClock
1050 */
1051 public static final class AlarmClockInfo implements Parcelable {
1052
1053 private final long mTriggerTime;
1054 private final PendingIntent mShowIntent;
1055
1056 /**
1057 * Creates a new alarm clock description.
1058 *
1059 * @param triggerTime time at which the underlying alarm is triggered in wall time
1060 * milliseconds since the epoch
1061 * @param showIntent an intent that can be used to show or edit details of
1062 * the alarm clock.
1063 */
1064 public AlarmClockInfo(long triggerTime, PendingIntent showIntent) {
1065 mTriggerTime = triggerTime;
1066 mShowIntent = showIntent;
1067 }
1068
1069 /**
1070 * Use the {@link #CREATOR}
1071 * @hide
1072 */
1073 AlarmClockInfo(Parcel in) {
1074 mTriggerTime = in.readLong();
1075 mShowIntent = in.readParcelable(PendingIntent.class.getClassLoader());
1076 }
1077
1078 /**
1079 * Returns the time at which the alarm is going to trigger.
1080 *
1081 * This value is UTC wall clock time in milliseconds, as returned by
1082 * {@link System#currentTimeMillis()} for example.
1083 */
1084 public long getTriggerTime() {
1085 return mTriggerTime;
1086 }
1087
1088 /**
Shuhrat Dehkanov66729ff2015-05-13 17:16:29 +09001089 * Returns an intent that can be used to show or edit details of the alarm clock in
Jose Lima235510e2014-08-13 12:50:01 -07001090 * the application that scheduled it.
1091 *
1092 * <p class="note">Beware that any application can retrieve and send this intent,
1093 * potentially with additional fields filled in. See
1094 * {@link PendingIntent#send(android.content.Context, int, android.content.Intent)
1095 * PendingIntent.send()} and {@link android.content.Intent#fillIn Intent.fillIn()}
1096 * for details.
1097 */
1098 public PendingIntent getShowIntent() {
1099 return mShowIntent;
1100 }
1101
1102 @Override
1103 public int describeContents() {
1104 return 0;
1105 }
1106
1107 @Override
1108 public void writeToParcel(Parcel dest, int flags) {
1109 dest.writeLong(mTriggerTime);
1110 dest.writeParcelable(mShowIntent, flags);
1111 }
1112
1113 public static final Creator<AlarmClockInfo> CREATOR = new Creator<AlarmClockInfo>() {
1114 @Override
1115 public AlarmClockInfo createFromParcel(Parcel in) {
1116 return new AlarmClockInfo(in);
1117 }
1118
1119 @Override
1120 public AlarmClockInfo[] newArray(int size) {
1121 return new AlarmClockInfo[size];
1122 }
1123 };
1124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125}