• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import android.annotation.IntDef;
20 import android.annotation.RequiresPermission;
21 import android.annotation.SdkConstant;
22 import android.annotation.SystemApi;
23 import android.annotation.SystemService;
24 import android.annotation.UnsupportedAppUsage;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.Build;
28 import android.os.Handler;
29 import android.os.Parcel;
30 import android.os.Parcelable;
31 import android.os.RemoteException;
32 import android.os.WorkSource;
33 import android.text.TextUtils;
34 import android.util.ArrayMap;
35 import android.util.Log;
36 import android.util.proto.ProtoOutputStream;
37 
38 import libcore.timezone.ZoneInfoDB;
39 
40 import java.io.IOException;
41 import java.lang.annotation.Retention;
42 import java.lang.annotation.RetentionPolicy;
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.
52  *
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.
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  *
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.
78  */
79 @SystemService(Context.ALARM_SERVICE)
80 public class AlarmManager {
81     private static final String TAG = "AlarmManager";
82 
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 
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 
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 
132     /** @hide */
133     @UnsupportedAppUsage
134     public static final long WINDOW_EXACT = 0;
135     /** @hide */
136     @UnsupportedAppUsage
137     public static final long WINDOW_HEURISTIC = -1;
138 
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      */
144     @UnsupportedAppUsage
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      */
152     @UnsupportedAppUsage
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     /**
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      */
173     @UnsupportedAppUsage
174     public static final int FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED = 1<<3;
175 
176     /**
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      */
183     @UnsupportedAppUsage
184     public static final int FLAG_IDLE_UNTIL = 1<<4;
185 
186     @UnsupportedAppUsage
187     private final IAlarmManager mService;
188     private final Context mContext;
189     private final String mPackageName;
190     private final boolean mAlwaysExact;
191     private final int mTargetSdkVersion;
192     private final Handler mMainThreadHandler;
193 
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          */
onAlarm()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 
ListenerWrapper(OnAlarmListener listener)211         public ListenerWrapper(OnAlarmListener listener) {
212             mListener = listener;
213         }
214 
setHandler(Handler h)215         public void setHandler(Handler h) {
216            mHandler = h;
217         }
218 
cancel()219         public void cancel() {
220             try {
221                 mService.remove(null, this);
222             } catch (RemoteException ex) {
223                 throw ex.rethrowFromSystemServer();
224             }
225 
226             synchronized (AlarmManager.class) {
227                 if (sWrappers != null) {
228                     sWrappers.remove(mListener);
229                 }
230             }
231         }
232 
233         @Override
doAlarm(IAlarmCompleteListener alarmManager)234         public void doAlarm(IAlarmCompleteListener alarmManager) {
235             mCompletion = alarmManager;
236 
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 
245             mHandler.post(this);
246         }
247 
248         @Override
run()249         public void run() {
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.
268     private static ArrayMap<OnAlarmListener, ListenerWrapper> sWrappers;
269 
270     /**
271      * package private on purpose
272      */
AlarmManager(IAlarmManager service, Context ctx)273     AlarmManager(IAlarmManager service, Context ctx) {
274         mService = service;
275 
276         mContext = ctx;
277         mPackageName = ctx.getPackageName();
278         mTargetSdkVersion = ctx.getApplicationInfo().targetSdkVersion;
279         mAlwaysExact = (mTargetSdkVersion < Build.VERSION_CODES.KITKAT);
280         mMainThreadHandler = new Handler(ctx.getMainLooper());
281     }
282 
legacyExactLength()283     private long legacyExactLength() {
284         return (mAlwaysExact ? WINDOW_EXACT : WINDOW_HEURISTIC);
285     }
286 
287     /**
288      * <p>Schedule an alarm.  <b>Note: for timing operations (ticks, timeouts,
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.
292      *
293      * <p>If the stated trigger time is in the past, the alarm will be triggered
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>
300      * The alarm is an Intent broadcast that goes to a broadcast receiver that
301      * you registered with {@link android.content.Context#registerReceiver}
302      * or through the &lt;receiver&gt; 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
309      * phone was asleep may have a count greater than one when delivered.
310      *
311      * <div class="note">
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,
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
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>
331      * Applications whose {@code targetSdkVersion} is before API 19 will
332      * continue to get the previous alarm behavior: all of their scheduled alarms
333      * will be treated as exact.
334      * </div>
335      *
336      * @param type type of alarm.
337      * @param triggerAtMillis time in milliseconds that the alarm should go
338      * off, using the appropriate clock (depending on the alarm type).
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
344      * @see #setExact
345      * @see #setRepeating
346      * @see #setWindow
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      */
set(@larmType int type, long triggerAtMillis, PendingIntent operation)356     public void set(@AlarmType int type, long triggerAtMillis, PendingIntent operation) {
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      *
370      * @param type type of alarm.
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      */
set(@larmType int type, long triggerAtMillis, String tag, OnAlarmListener listener, Handler targetHandler)383     public void set(@AlarmType int type, long triggerAtMillis, String tag, OnAlarmListener listener,
384             Handler targetHandler) {
385         setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, null, listener, tag,
386                 targetHandler, null, null);
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      *
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
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.
409      *
410      * <p>If your application wants to allow the delivery times to drift in
411      * order to guarantee that at least a certain time interval always elapses
412      * between alarms, then the approach to take is to use one-time alarms,
413      * scheduling the next one yourself when handling each alarm delivery.
414      *
415      * <p class="note">
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
419      * whose {@code targetSdkVersion} is earlier than API 19 will continue to have all
420      * of their alarms, including repeating alarms, treated as exact.
421      *
422      * @param type type of alarm.
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.
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
433      * @see #setExact
434      * @see #setWindow
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      */
setRepeating(@larmType int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)444     public void setRepeating(@AlarmType int type, long triggerAtMillis,
445             long intervalMillis, PendingIntent operation) {
446         setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, 0, operation,
447                 null, null, null, null, null);
448     }
449 
450     /**
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.
457      *
458      * <p>
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.
462      *
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
466      * flexibility to minimize wakeups and battery use.  For alarms that must be delivered
467      * at precisely-specified times with no acceptable variation, applications can use
468      * {@link #setExact(int, long, PendingIntent)}.
469      *
470      * @param type type of alarm.
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
476      *        milliseconds after {@code windowStartMillis}.  Note that this parameter
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      */
setWindow(@larmType int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation)494     public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis,
495             PendingIntent operation) {
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      */
setWindow(@larmType int type, long windowStartMillis, long windowLengthMillis, String tag, OnAlarmListener listener, Handler targetHandler)509     public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis,
510             String tag, OnAlarmListener listener, Handler targetHandler) {
511         setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, null, listener, tag,
512                 targetHandler, null, null);
513     }
514 
515     /**
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      *
529      * @param type type of alarm.
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
547      */
setExact(@larmType int type, long triggerAtMillis, PendingIntent operation)548     public void setExact(@AlarmType int type, long triggerAtMillis, PendingIntent operation) {
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      */
setExact(@larmType int type, long triggerAtMillis, String tag, OnAlarmListener listener, Handler targetHandler)562     public void setExact(@AlarmType int type, long triggerAtMillis, String tag,
563             OnAlarmListener listener, Handler targetHandler) {
564         setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, null, listener, tag,
565                 targetHandler, null, null);
566     }
567 
568     /**
569      * Schedule an idle-until alarm, which will keep the alarm manager idle until
570      * the given time.
571      * @hide
572      */
setIdleUntil(@larmType int type, long triggerAtMillis, String tag, OnAlarmListener listener, Handler targetHandler)573     public void setIdleUntil(@AlarmType int type, long triggerAtMillis, String tag,
574             OnAlarmListener listener, Handler targetHandler) {
575         setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_IDLE_UNTIL, null,
576                 listener, tag, targetHandler, null, null);
577     }
578 
579     /**
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.
585      *
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>
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      */
setAlarmClock(AlarmClockInfo info, PendingIntent operation)615     public void setAlarmClock(AlarmClockInfo info, PendingIntent operation) {
616         setImpl(RTC_WAKEUP, info.getTriggerTime(), WINDOW_EXACT, 0, 0, operation,
617                 null, null, null, null, info);
618     }
619 
620     /** @hide */
621     @SystemApi
622     @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
set(@larmType int type, long triggerAtMillis, long windowMillis, long intervalMillis, PendingIntent operation, WorkSource workSource)623     public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
624             long intervalMillis, PendingIntent operation, WorkSource workSource) {
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      */
639     @UnsupportedAppUsage
set(@larmType int type, long triggerAtMillis, long windowMillis, long intervalMillis, String tag, OnAlarmListener listener, Handler targetHandler, WorkSource workSource)640     public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
641             long intervalMillis, String tag, OnAlarmListener listener, Handler targetHandler,
642             WorkSource workSource) {
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      */
657     @SystemApi
658     @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
set(@larmType int type, long triggerAtMillis, long windowMillis, long intervalMillis, OnAlarmListener listener, Handler targetHandler, WorkSource workSource)659     public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
660             long intervalMillis, OnAlarmListener listener, Handler targetHandler,
661             WorkSource workSource) {
662         setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, null,
663                 targetHandler, workSource, null);
664     }
665 
setImpl(@larmType int type, long triggerAtMillis, long windowMillis, long intervalMillis, int flags, PendingIntent operation, final OnAlarmListener listener, String listenerTag, Handler targetHandler, WorkSource workSource, AlarmClockInfo alarmClock)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) {
670         if (triggerAtMillis < 0) {
671             /* NOTYET
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             }
677             */
678             triggerAtMillis = 0;
679         }
680 
681         ListenerWrapper recipientWrapper = null;
682         if (listener != null) {
683             synchronized (AlarmManager.class) {
684                 if (sWrappers == null) {
685                     sWrappers = new ArrayMap<OnAlarmListener, ListenerWrapper>();
686                 }
687 
688                 recipientWrapper = sWrappers.get(listener);
689                 // no existing wrapper => build a new one
690                 if (recipientWrapper == null) {
691                     recipientWrapper = new ListenerWrapper(listener);
692                     sWrappers.put(listener, recipientWrapper);
693                 }
694             }
695 
696             final Handler handler = (targetHandler != null) ? targetHandler : mMainThreadHandler;
697             recipientWrapper.setHandler(handler);
698         }
699 
700         try {
701             mService.set(mPackageName, type, triggerAtMillis, windowMillis, intervalMillis, flags,
702                     operation, recipientWrapper, listenerTag, workSource, alarmClock);
703         } catch (RemoteException ex) {
704             throw ex.rethrowFromSystemServer();
705         }
706     }
707 
708     /**
709      * Available inexact recurrence interval recognized by
710      * {@link #setInexactRepeating(int, long, long, PendingIntent)}
711      * when running on Android prior to API 19.
712      */
713     public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000;
714 
715     /**
716      * Available inexact recurrence interval recognized by
717      * {@link #setInexactRepeating(int, long, long, PendingIntent)}
718      * when running on Android prior to API 19.
719      */
720     public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES;
721 
722     /**
723      * Available inexact recurrence interval recognized by
724      * {@link #setInexactRepeating(int, long, long, PendingIntent)}
725      * when running on Android prior to API 19.
726      */
727     public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR;
728 
729     /**
730      * Available inexact recurrence interval recognized by
731      * {@link #setInexactRepeating(int, long, long, PendingIntent)}
732      * when running on Android prior to API 19.
733      */
734     public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR;
735 
736     /**
737      * Available inexact recurrence interval recognized by
738      * {@link #setInexactRepeating(int, long, long, PendingIntent)}
739      * when running on Android prior to API 19.
740      */
741     public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY;
742 
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
747      * the strict recurrences traditionally supplied by {@link #setRepeating}, since the
748      * system can adjust alarms' delivery times to cause them to fire simultaneously,
749      * avoiding waking the device from sleep more than necessary.
750      *
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
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.
765      *
766      * @param type type of alarm.
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
773      * of the alarm.  Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES,
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
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.
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      */
setInexactRepeating(@larmType int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)800     public void setInexactRepeating(@AlarmType int type, long triggerAtMillis,
801             long intervalMillis, PendingIntent operation) {
802         setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, intervalMillis, 0, operation, null,
803                 null, null, null, null);
804     }
805 
806     /**
807      * Like {@link #set(int, long, PendingIntent)}, but this alarm will be allowed to execute
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
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>
814      *
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
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      *
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      *
832      * @param type type of alarm.
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      */
setAndAllowWhileIdle(@larmType int type, long triggerAtMillis, PendingIntent operation)850     public void setAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis,
851             PendingIntent operation) {
852         setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, 0, FLAG_ALLOW_WHILE_IDLE,
853                 operation, null, null, null, null, null);
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
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>
866      *
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
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      *
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      *
885      * @param type type of alarm.
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      */
setExactAndAllowWhileIdle(@larmType int type, long triggerAtMillis, PendingIntent operation)904     public void setExactAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis,
905             PendingIntent operation) {
906         setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_ALLOW_WHILE_IDLE, operation,
907                 null, null, null, null, null);
908     }
909 
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
916      * IntentSender. This parameter must not be {@code null}.
917      *
918      * @see #set
919      */
cancel(PendingIntent operation)920     public void cancel(PendingIntent operation) {
921         if (operation == null) {
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             }
929         }
930 
931         try {
932             mService.remove(operation, null);
933         } catch (RemoteException ex) {
934             throw ex.rethrowFromSystemServer();
935         }
936     }
937 
938     /**
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      */
cancel(OnAlarmListener listener)943     public void cancel(OnAlarmListener listener) {
944         if (listener == null) {
945             throw new NullPointerException("cancel() called with a null OnAlarmListener");
946         }
947 
948         ListenerWrapper wrapper = null;
949         synchronized (AlarmManager.class) {
950             if (sWrappers != null) {
951                 wrapper = sWrappers.get(listener);
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     /**
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     @RequiresPermission(android.Manifest.permission.SET_TIME)
setTime(long millis)970     public void setTime(long millis) {
971         try {
972             mService.setTime(millis);
973         } catch (RemoteException ex) {
974             throw ex.rethrowFromSystemServer();
975         }
976     }
977 
978     /**
979      * Sets the system's persistent default time zone. This is the time zone for all apps, even
980      * after a reboot. Use {@link java.util.TimeZone#setDefault} if you just want to change the
981      * time zone within your app, and even then prefer to pass an explicit
982      * {@link java.util.TimeZone} to APIs that require it rather than changing the time zone for
983      * all threads.
984      *
985      * <p> On android M and above, it is an error to pass in a non-Olson timezone to this
986      * function. Note that this is a bad idea on all Android releases because POSIX and
987      * the {@code TimeZone} class have opposite interpretations of {@code '+'} and {@code '-'}
988      * in the same non-Olson ID.
989      *
990      * @param timeZone one of the Olson ids from the list returned by
991      *     {@link java.util.TimeZone#getAvailableIDs}
992      */
993     @RequiresPermission(android.Manifest.permission.SET_TIME_ZONE)
setTimeZone(String timeZone)994     public void setTimeZone(String timeZone) {
995         if (TextUtils.isEmpty(timeZone)) {
996             return;
997         }
998 
999         // Reject this timezone if it isn't an Olson zone we recognize.
1000         if (mTargetSdkVersion >= Build.VERSION_CODES.M) {
1001             boolean hasTimeZone = false;
1002             try {
1003                 hasTimeZone = ZoneInfoDB.getInstance().hasTimeZone(timeZone);
1004             } catch (IOException ignored) {
1005             }
1006 
1007             if (!hasTimeZone) {
1008                 throw new IllegalArgumentException("Timezone: " + timeZone + " is not an Olson ID");
1009             }
1010         }
1011 
1012         try {
1013             mService.setTimeZone(timeZone);
1014         } catch (RemoteException ex) {
1015             throw ex.rethrowFromSystemServer();
1016         }
1017     }
1018 
1019     /** @hide */
getNextWakeFromIdleTime()1020     public long getNextWakeFromIdleTime() {
1021         try {
1022             return mService.getNextWakeFromIdleTime();
1023         } catch (RemoteException ex) {
1024             throw ex.rethrowFromSystemServer();
1025         }
1026     }
1027 
1028     /**
1029      * Gets information about the next alarm clock currently scheduled.
1030      *
1031      * The alarm clocks considered are those scheduled by any application
1032      * using the {@link #setAlarmClock} method.
1033      *
1034      * @return An {@link AlarmClockInfo} object describing the next upcoming alarm
1035      *   clock event that will occur.  If there are no alarm clock events currently
1036      *   scheduled, this method will return {@code null}.
1037      *
1038      * @see #setAlarmClock
1039      * @see AlarmClockInfo
1040      * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED
1041      */
getNextAlarmClock()1042     public AlarmClockInfo getNextAlarmClock() {
1043         return getNextAlarmClock(mContext.getUserId());
1044     }
1045 
1046     /**
1047      * Gets information about the next alarm clock currently scheduled.
1048      *
1049      * The alarm clocks considered are those scheduled by any application
1050      * using the {@link #setAlarmClock} method within the given user.
1051      *
1052      * @return An {@link AlarmClockInfo} object describing the next upcoming alarm
1053      *   clock event that will occur within the given user.  If there are no alarm clock
1054      *   events currently scheduled in that user, this method will return {@code null}.
1055      *
1056      * @see #setAlarmClock
1057      * @see AlarmClockInfo
1058      * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED
1059      *
1060      * @hide
1061      */
getNextAlarmClock(int userId)1062     public AlarmClockInfo getNextAlarmClock(int userId) {
1063         try {
1064             return mService.getNextAlarmClock(userId);
1065         } catch (RemoteException ex) {
1066             throw ex.rethrowFromSystemServer();
1067         }
1068     }
1069 
1070     /**
1071      * An immutable description of a scheduled "alarm clock" event.
1072      *
1073      * @see AlarmManager#setAlarmClock
1074      * @see AlarmManager#getNextAlarmClock
1075      */
1076     public static final class AlarmClockInfo implements Parcelable {
1077 
1078         private final long mTriggerTime;
1079         private final PendingIntent mShowIntent;
1080 
1081         /**
1082          * Creates a new alarm clock description.
1083          *
1084          * @param triggerTime time at which the underlying alarm is triggered in wall time
1085          *                    milliseconds since the epoch
1086          * @param showIntent an intent that can be used to show or edit details of
1087          *                        the alarm clock.
1088          */
AlarmClockInfo(long triggerTime, PendingIntent showIntent)1089         public AlarmClockInfo(long triggerTime, PendingIntent showIntent) {
1090             mTriggerTime = triggerTime;
1091             mShowIntent = showIntent;
1092         }
1093 
1094         /**
1095          * Use the {@link #CREATOR}
1096          * @hide
1097          */
AlarmClockInfo(Parcel in)1098         AlarmClockInfo(Parcel in) {
1099             mTriggerTime = in.readLong();
1100             mShowIntent = in.readParcelable(PendingIntent.class.getClassLoader());
1101         }
1102 
1103         /**
1104          * Returns the time at which the alarm is going to trigger.
1105          *
1106          * This value is UTC wall clock time in milliseconds, as returned by
1107          * {@link System#currentTimeMillis()} for example.
1108          */
getTriggerTime()1109         public long getTriggerTime() {
1110             return mTriggerTime;
1111         }
1112 
1113         /**
1114          * Returns an intent that can be used to show or edit details of the alarm clock in
1115          * the application that scheduled it.
1116          *
1117          * <p class="note">Beware that any application can retrieve and send this intent,
1118          * potentially with additional fields filled in. See
1119          * {@link PendingIntent#send(android.content.Context, int, android.content.Intent)
1120          * PendingIntent.send()} and {@link android.content.Intent#fillIn Intent.fillIn()}
1121          * for details.
1122          */
getShowIntent()1123         public PendingIntent getShowIntent() {
1124             return mShowIntent;
1125         }
1126 
1127         @Override
describeContents()1128         public int describeContents() {
1129             return 0;
1130         }
1131 
1132         @Override
writeToParcel(Parcel dest, int flags)1133         public void writeToParcel(Parcel dest, int flags) {
1134             dest.writeLong(mTriggerTime);
1135             dest.writeParcelable(mShowIntent, flags);
1136         }
1137 
1138         public static final @android.annotation.NonNull Creator<AlarmClockInfo> CREATOR = new Creator<AlarmClockInfo>() {
1139             @Override
1140             public AlarmClockInfo createFromParcel(Parcel in) {
1141                 return new AlarmClockInfo(in);
1142             }
1143 
1144             @Override
1145             public AlarmClockInfo[] newArray(int size) {
1146                 return new AlarmClockInfo[size];
1147             }
1148         };
1149 
1150         /** @hide */
writeToProto(ProtoOutputStream proto, long fieldId)1151         public void writeToProto(ProtoOutputStream proto, long fieldId) {
1152             final long token = proto.start(fieldId);
1153             proto.write(AlarmClockInfoProto.TRIGGER_TIME_MS, mTriggerTime);
1154             if (mShowIntent != null) {
1155                 mShowIntent.writeToProto(proto, AlarmClockInfoProto.SHOW_INTENT);
1156             }
1157             proto.end(token);
1158         }
1159     }
1160 }
1161