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