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