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