| 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.Manifest; |
| 20 | import android.annotation.IntDef; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.annotation.RequiresPermission; |
| 24 | import android.annotation.SdkConstant; |
| 25 | import android.annotation.SdkConstant.SdkConstantType; |
| 26 | import android.annotation.SystemApi; |
| 27 | import android.annotation.SystemService; |
| 28 | import android.annotation.TestApi; |
| 29 | import android.compat.annotation.ChangeId; |
| 30 | import android.compat.annotation.Disabled; |
| 31 | import android.compat.annotation.EnabledSince; |
| 32 | import android.compat.annotation.UnsupportedAppUsage; |
| 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
| 35 | import android.os.Build; |
| 36 | import android.os.Handler; |
| 37 | import android.os.HandlerExecutor; |
| 38 | import android.os.Parcel; |
| 39 | import android.os.Parcelable; |
| 40 | import android.os.RemoteException; |
| 41 | import android.os.WorkSource; |
| 42 | import android.text.TextUtils; |
| 43 | import android.util.Log; |
| 44 | import android.util.proto.ProtoOutputStream; |
| 45 | |
| 46 | import com.android.i18n.timezone.ZoneInfoDb; |
| 47 | |
| 48 | import java.lang.annotation.Retention; |
| 49 | import java.lang.annotation.RetentionPolicy; |
| 50 | import java.lang.ref.WeakReference; |
| 51 | import java.util.Objects; |
| 52 | import java.util.WeakHashMap; |
| 53 | import java.util.concurrent.Executor; |
| 54 | |
| 55 | /** |
| 56 | * This class provides access to the system alarm services. These allow you |
| 57 | * to schedule your application to be run at some point in the future. When |
| 58 | * an alarm goes off, the {@link Intent} that had been registered for it |
| 59 | * is broadcast by the system, automatically starting the target application |
| 60 | * if it is not already running. Registered alarms are retained while the |
| 61 | * device is asleep (and can optionally wake the device up if they go off |
| 62 | * during that time), but will be cleared if it is turned off and rebooted. |
| 63 | * |
| 64 | * <p>The Alarm Manager holds a CPU wake lock as long as the alarm receiver's |
| 65 | * onReceive() method is executing. This guarantees that the phone will not sleep |
| 66 | * until you have finished handling the broadcast. Once onReceive() returns, the |
| 67 | * Alarm Manager releases this wake lock. This means that the phone will in some |
| 68 | * cases sleep as soon as your onReceive() method completes. If your alarm receiver |
| 69 | * called {@link android.content.Context#startService Context.startService()}, it |
| 70 | * is possible that the phone will sleep before the requested service is launched. |
| 71 | * To prevent this, your BroadcastReceiver and Service will need to implement a |
| 72 | * separate wake lock policy to ensure that the phone continues running until the |
| 73 | * service becomes available. |
| 74 | * |
| 75 | * <p><b>Note: The Alarm Manager is intended for cases where you want to have |
| 76 | * your application code run at a specific time, even if your application is |
| 77 | * not currently running. For normal timing operations (ticks, timeouts, |
| 78 | * etc) it is easier and much more efficient to use |
| 79 | * {@link android.os.Handler}.</b> |
| 80 | * |
| 81 | * <p class="caution"><strong>Note:</strong> Beginning with API 19 |
| 82 | * ({@link android.os.Build.VERSION_CODES#KITKAT}) alarm delivery is inexact: |
| 83 | * the OS will shift alarms in order to minimize wakeups and battery use. There are |
| 84 | * new APIs to support applications which need strict delivery guarantees; see |
| 85 | * {@link #setWindow(int, long, long, PendingIntent)} and |
| 86 | * {@link #setExact(int, long, PendingIntent)}. Applications whose {@code targetSdkVersion} |
| 87 | * is earlier than API 19 will continue to see the previous behavior in which all |
| 88 | * alarms are delivered exactly when requested. |
| 89 | */ |
| 90 | @SystemService(Context.ALARM_SERVICE) |
| 91 | public class AlarmManager { |
| 92 | private static final String TAG = "AlarmManager"; |
| 93 | |
| 94 | /** @hide */ |
| 95 | @IntDef(prefix = { "RTC", "ELAPSED" }, value = { |
| 96 | RTC_WAKEUP, |
| 97 | RTC, |
| 98 | ELAPSED_REALTIME_WAKEUP, |
| 99 | ELAPSED_REALTIME, |
| 100 | }) |
| 101 | @Retention(RetentionPolicy.SOURCE) |
| 102 | public @interface AlarmType {} |
| 103 | |
| 104 | /** |
| 105 | * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()} |
| 106 | * (wall clock time in UTC), which will wake up the device when |
| 107 | * it goes off. |
| 108 | */ |
| 109 | public static final int RTC_WAKEUP = 0; |
| 110 | /** |
| 111 | * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()} |
| 112 | * (wall clock time in UTC). This alarm does not wake the |
| 113 | * device up; if it goes off while the device is asleep, it will not be |
| 114 | * delivered until the next time the device wakes up. |
| 115 | */ |
| 116 | public static final int RTC = 1; |
| 117 | /** |
| 118 | * Alarm time in {@link android.os.SystemClock#elapsedRealtime |
| 119 | * SystemClock.elapsedRealtime()} (time since boot, including sleep), |
| 120 | * which will wake up the device when it goes off. |
| 121 | */ |
| 122 | public static final int ELAPSED_REALTIME_WAKEUP = 2; |
| 123 | /** |
| 124 | * Alarm time in {@link android.os.SystemClock#elapsedRealtime |
| 125 | * SystemClock.elapsedRealtime()} (time since boot, including sleep). |
| 126 | * This alarm does not wake the device up; if it goes off while the device |
| 127 | * is asleep, it will not be delivered until the next time the device |
| 128 | * wakes up. |
| 129 | */ |
| 130 | public static final int ELAPSED_REALTIME = 3; |
| 131 | |
| 132 | /** |
| 133 | * Broadcast Action: Sent after the value returned by |
| 134 | * {@link #getNextAlarmClock()} has changed. |
| 135 | * |
| 136 | * <p class="note">This is a protected intent that can only be sent by the system. |
| 137 | * It is only sent to registered receivers.</p> |
| 138 | */ |
| 139 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 140 | public static final String ACTION_NEXT_ALARM_CLOCK_CHANGED = |
| 141 | "android.app.action.NEXT_ALARM_CLOCK_CHANGED"; |
| 142 | |
| 143 | /** |
| 144 | * Broadcast Action: An app is granted the |
| 145 | * {@link android.Manifest.permission#SCHEDULE_EXACT_ALARM} permission. |
| 146 | * |
| 147 | * <p>When the user revokes the {@link android.Manifest.permission#SCHEDULE_EXACT_ALARM} |
| 148 | * permission, all alarms scheduled with |
| 149 | * {@link #setExact}, {@link #setExactAndAllowWhileIdle} and |
| 150 | * {@link #setAlarmClock(AlarmClockInfo, PendingIntent)} will be deleted. |
| 151 | * |
| 152 | * <p>When the user grants the {@link android.Manifest.permission#SCHEDULE_EXACT_ALARM}, |
| 153 | * this broadcast will be sent. Applications can reschedule all the necessary alarms when |
| 154 | * receiving it. |
| 155 | * |
| 156 | * <p>This broadcast will <em>not</em> be sent when the user revokes the permission. |
| 157 | * |
| 158 | * <p><em>Note:</em> |
| 159 | * Applications are still required to check {@link #canScheduleExactAlarms()} |
| 160 | * before using the above APIs after receiving this broadcast, |
| 161 | * because it's possible that the permission is already revoked again by the time |
| 162 | * applications receive this broadcast. |
| 163 | * |
| 164 | * <p>This broadcast will be sent to both runtime receivers and manifest receivers. |
| 165 | * |
| 166 | * <p>This broadcast is sent as a foreground broadcast. |
| 167 | * See {@link android.content.Intent#FLAG_RECEIVER_FOREGROUND}. |
| 168 | * |
| 169 | * <p>When an application receives this broadcast, it's allowed to start a foreground service. |
| 170 | */ |
| 171 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 172 | public static final String ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED = |
| 173 | "android.app.action.SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED"; |
| 174 | |
| 175 | /** @hide */ |
| 176 | @UnsupportedAppUsage |
| 177 | public static final long WINDOW_EXACT = 0; |
| 178 | /** @hide */ |
| 179 | @UnsupportedAppUsage |
| 180 | public static final long WINDOW_HEURISTIC = -1; |
| 181 | |
| 182 | /** |
| 183 | * Flag for alarms: this is to be a stand-alone alarm, that should not be batched with |
| 184 | * other alarms. |
| 185 | * @hide |
| 186 | */ |
| 187 | @UnsupportedAppUsage |
| 188 | public static final int FLAG_STANDALONE = 1<<0; |
| 189 | |
| 190 | /** |
| 191 | * Flag for alarms: this alarm would like to wake the device even if it is idle. This |
| 192 | * is, for example, an alarm for an alarm clock. |
| 193 | * @hide |
| 194 | */ |
| 195 | @UnsupportedAppUsage |
| 196 | public static final int FLAG_WAKE_FROM_IDLE = 1<<1; |
| 197 | |
| 198 | /** |
| 199 | * Flag for alarms: this alarm would like to still execute even if the device is |
| 200 | * idle. This won't bring the device out of idle, just allow this specific alarm to |
| 201 | * run. Note that this means the actual time this alarm goes off can be inconsistent |
| 202 | * with the time of non-allow-while-idle alarms (it could go earlier than the time |
| 203 | * requested by another alarm). |
| 204 | * |
| 205 | * @hide |
| 206 | */ |
| 207 | public static final int FLAG_ALLOW_WHILE_IDLE = 1<<2; |
| 208 | |
| 209 | /** |
| 210 | * Flag for alarms: same as {@link #FLAG_ALLOW_WHILE_IDLE}, but doesn't have restrictions |
| 211 | * on how frequently it can be scheduled. Only available (and automatically applied) to |
| 212 | * system alarms. |
| 213 | * |
| 214 | * <p>Note that alarms set with a {@link WorkSource} <b>do not</b> get this flag. |
| 215 | * |
| 216 | * @hide |
| 217 | */ |
| 218 | @UnsupportedAppUsage |
| 219 | public static final int FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED = 1<<3; |
| 220 | |
| 221 | /** |
| 222 | * Flag for alarms: this alarm marks the point where we would like to come out of idle |
| 223 | * mode. It may be moved by the alarm manager to match the first wake-from-idle alarm. |
| 224 | * Scheduling an alarm with this flag puts the alarm manager in to idle mode, where it |
| 225 | * avoids scheduling any further alarms until the marker alarm is executed. |
| 226 | * @hide |
| 227 | */ |
| 228 | @UnsupportedAppUsage |
| 229 | public static final int FLAG_IDLE_UNTIL = 1<<4; |
| 230 | |
| 231 | /** |
| 232 | * Flag for alarms: Used to provide backwards compatibility for apps with targetSdkVersion less |
| 233 | * than {@link Build.VERSION_CODES#S} |
| 234 | * @hide |
| 235 | */ |
| 236 | public static final int FLAG_ALLOW_WHILE_IDLE_COMPAT = 1 << 5; |
| 237 | |
| 238 | /** |
| 239 | * Flag for alarms: Used to mark prioritized alarms. These alarms will get to execute while idle |
| 240 | * and can be sent separately from other alarms that may be already due at the time. |
| 241 | * These alarms can be set via |
| 242 | * {@link #setPrioritized(int, long, long, String, Executor, OnAlarmListener)} |
| 243 | * @hide |
| 244 | */ |
| 245 | public static final int FLAG_PRIORITIZE = 1 << 6; |
| 246 | |
| 247 | /** |
| 248 | * For apps targeting {@link Build.VERSION_CODES#S} or above, any APIs setting exact alarms, |
| 249 | * e.g. {@link #setExact(int, long, PendingIntent)}, |
| 250 | * {@link #setAlarmClock(AlarmClockInfo, PendingIntent)} and others will require holding a new |
| 251 | * permission {@link Manifest.permission#SCHEDULE_EXACT_ALARM} |
| 252 | * |
| 253 | * @hide |
| 254 | */ |
| 255 | @ChangeId |
| 256 | @EnabledSince(targetSdkVersion = Build.VERSION_CODES.S) |
| 257 | public static final long REQUIRE_EXACT_ALARM_PERMISSION = 171306433L; |
| 258 | |
| 259 | /** |
| 260 | * For apps targeting {@link Build.VERSION_CODES#S} or above, all inexact alarms will require |
| 261 | * to have a minimum window size, expected to be on the order of a few minutes. |
| 262 | * |
| 263 | * Practically, any alarms requiring smaller windows are the same as exact alarms and should use |
| 264 | * the corresponding APIs provided, like {@link #setExact(int, long, PendingIntent)}, et al. |
| 265 | * |
| 266 | * Inexact alarm with shorter windows specified will have their windows elongated by the system. |
| 267 | * |
| 268 | * @hide |
| 269 | */ |
| 270 | @ChangeId |
| 271 | @EnabledSince(targetSdkVersion = Build.VERSION_CODES.S) |
| 272 | public static final long ENFORCE_MINIMUM_WINDOW_ON_INEXACT_ALARMS = 185199076L; |
| 273 | |
| 274 | /** |
| 275 | * For apps targeting {@link Build.VERSION_CODES#TIRAMISU} or above, certain kinds of apps can |
| 276 | * use {@link Manifest.permission#USE_EXACT_ALARM} to schedule exact alarms. |
| 277 | * |
| 278 | * @hide |
| 279 | */ |
| 280 | @ChangeId |
| 281 | @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 282 | public static final long ENABLE_USE_EXACT_ALARM = 218533173L; |
| 283 | |
| 284 | /** |
| 285 | * The permission {@link Manifest.permission#SCHEDULE_EXACT_ALARM} will be denied, unless the |
| 286 | * user explicitly allows it from Settings. |
| 287 | * |
| 288 | * TODO (b/226439802): Either enable it in the next SDK or replace it with a better alternative. |
| 289 | * @hide |
| 290 | */ |
| 291 | @ChangeId |
| 292 | @Disabled |
| 293 | public static final long SCHEDULE_EXACT_ALARM_DENIED_BY_DEFAULT = 226439802L; |
| 294 | |
| 295 | @UnsupportedAppUsage |
| 296 | private final IAlarmManager mService; |
| 297 | private final Context mContext; |
| 298 | private final String mPackageName; |
| 299 | private final boolean mAlwaysExact; |
| 300 | private final int mTargetSdkVersion; |
| 301 | private final Handler mMainThreadHandler; |
| 302 | |
| 303 | /** |
| 304 | * Direct-notification alarms: the requester must be running continuously from the |
| 305 | * time the alarm is set to the time it is delivered, or delivery will fail. Only |
| 306 | * one-shot alarms can be set using this mechanism, not repeating alarms. |
| 307 | */ |
| 308 | public interface OnAlarmListener { |
| 309 | /** |
| 310 | * Callback method that is invoked by the system when the alarm time is reached. |
| 311 | */ |
| 312 | public void onAlarm(); |
| 313 | } |
| 314 | |
| 315 | final class ListenerWrapper extends IAlarmListener.Stub implements Runnable { |
| 316 | final OnAlarmListener mListener; |
| 317 | Executor mExecutor; |
| 318 | IAlarmCompleteListener mCompletion; |
| 319 | |
| 320 | public ListenerWrapper(OnAlarmListener listener) { |
| 321 | mListener = listener; |
| 322 | } |
| 323 | |
| 324 | void setExecutor(Executor e) { |
| 325 | mExecutor = e; |
| 326 | } |
| 327 | |
| 328 | public void cancel() { |
| 329 | try { |
| 330 | mService.remove(null, this); |
| 331 | } catch (RemoteException ex) { |
| 332 | throw ex.rethrowFromSystemServer(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public void doAlarm(IAlarmCompleteListener alarmManager) { |
| 338 | mCompletion = alarmManager; |
| 339 | |
| 340 | mExecutor.execute(this); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void run() { |
| 345 | // Now deliver it to the app |
| 346 | try { |
| 347 | mListener.onAlarm(); |
| 348 | } finally { |
| 349 | // No catch -- make sure to report completion to the system process, |
| 350 | // but continue to allow the exception to crash the app. |
| 351 | |
| 352 | try { |
| 353 | mCompletion.alarmComplete(this); |
| 354 | } catch (Exception e) { |
| 355 | Log.e(TAG, "Unable to report completion to Alarm Manager!", e); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Tracking of the OnAlarmListener -> ListenerWrapper mapping, for cancel() support. |
| 363 | * An entry is guaranteed to stay in this map as long as its ListenerWrapper is held by the |
| 364 | * server. |
| 365 | * |
| 366 | * <p>Access is synchronized on the AlarmManager class object. |
| 367 | */ |
| 368 | private static WeakHashMap<OnAlarmListener, WeakReference<ListenerWrapper>> sWrappers; |
| 369 | |
| 370 | /** |
| 371 | * package private on purpose |
| 372 | */ |
| 373 | AlarmManager(IAlarmManager service, Context ctx) { |
| 374 | mService = service; |
| 375 | |
| 376 | mContext = ctx; |
| 377 | mPackageName = ctx.getPackageName(); |
| 378 | mTargetSdkVersion = ctx.getApplicationInfo().targetSdkVersion; |
| 379 | mAlwaysExact = (mTargetSdkVersion < Build.VERSION_CODES.KITKAT); |
| 380 | mMainThreadHandler = new Handler(ctx.getMainLooper()); |
| 381 | } |
| 382 | |
| 383 | private long legacyExactLength() { |
| 384 | return (mAlwaysExact ? WINDOW_EXACT : WINDOW_HEURISTIC); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * <p>Schedule an alarm. <b>Note: for timing operations (ticks, timeouts, |
| 389 | * etc) it is easier and much more efficient to use {@link android.os.Handler}.</b> |
| 390 | * If there is already an alarm scheduled for the same IntentSender, that previous |
| 391 | * alarm will first be canceled. |
| 392 | * |
| 393 | * <p>If the stated trigger time is in the past, the alarm will be triggered |
| 394 | * immediately. If there is already an alarm for this Intent |
| 395 | * scheduled (with the equality of two intents being defined by |
| 396 | * {@link Intent#filterEquals}), then it will be removed and replaced by |
| 397 | * this one. |
| 398 | * |
| 399 | * <p> |
| 400 | * The alarm is an Intent broadcast that goes to a broadcast receiver that |
| 401 | * you registered with {@link android.content.Context#registerReceiver} |
| 402 | * or through the <receiver> tag in an AndroidManifest.xml file. |
| 403 | * |
| 404 | * <p> |
| 405 | * Alarm intents are delivered with a data extra of type int called |
| 406 | * {@link Intent#EXTRA_ALARM_COUNT Intent.EXTRA_ALARM_COUNT} that indicates |
| 407 | * how many past alarm events have been accumulated into this intent |
| 408 | * broadcast. Recurring alarms that have gone undelivered because the |
| 409 | * phone was asleep may have a count greater than one when delivered. |
| 410 | * |
| 411 | * <div class="note"> |
| 412 | * <p> |
| 413 | * <b>Note:</b> Beginning in API 19, the trigger time passed to this method |
| 414 | * is treated as inexact: the alarm will not be delivered before this time, but |
| 415 | * may be deferred and delivered some time later. The OS will use |
| 416 | * this policy in order to "batch" alarms together across the entire system, |
| 417 | * minimizing the number of times the device needs to "wake up" and minimizing |
| 418 | * battery use. In general, alarms scheduled in the near future will not |
| 419 | * be deferred as long as alarms scheduled far in the future. |
| 420 | * |
| 421 | * <p> |
| 422 | * With the new batching policy, delivery ordering guarantees are not as |
| 423 | * strong as they were previously. If the application sets multiple alarms, |
| 424 | * it is possible that these alarms' <em>actual</em> delivery ordering may not match |
| 425 | * the order of their <em>requested</em> delivery times. If your application has |
| 426 | * strong ordering requirements there are other APIs that you can use to get |
| 427 | * the necessary behavior; see {@link #setWindow(int, long, long, PendingIntent)} |
| 428 | * and {@link #setExact(int, long, PendingIntent)}. |
| 429 | * |
| 430 | * <p> |
| 431 | * Applications whose {@code targetSdkVersion} is before API 19 will |
| 432 | * continue to get the previous alarm behavior: all of their scheduled alarms |
| 433 | * will be treated as exact. |
| 434 | * </div> |
| 435 | * |
| 436 | * @param type type of alarm. |
| 437 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 438 | * off, using the appropriate clock (depending on the alarm type). |
| 439 | * @param operation Action to perform when the alarm goes off; |
| 440 | * typically comes from {@link PendingIntent#getBroadcast |
| 441 | * IntentSender.getBroadcast()}. |
| 442 | * |
| 443 | * @see android.os.Handler |
| 444 | * @see #setExact |
| 445 | * @see #setRepeating |
| 446 | * @see #setWindow |
| 447 | * @see #cancel |
| 448 | * @see android.content.Context#sendBroadcast |
| 449 | * @see android.content.Context#registerReceiver |
| 450 | * @see android.content.Intent#filterEquals |
| 451 | * @see #ELAPSED_REALTIME |
| 452 | * @see #ELAPSED_REALTIME_WAKEUP |
| 453 | * @see #RTC |
| 454 | * @see #RTC_WAKEUP |
| 455 | */ |
| 456 | public void set(@AlarmType int type, long triggerAtMillis, PendingIntent operation) { |
| 457 | setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, operation, null, null, |
| 458 | (Handler) null, null, null); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Direct callback version of {@link #set(int, long, PendingIntent)}. Rather than |
| 463 | * supplying a PendingIntent to be sent when the alarm time is reached, this variant |
| 464 | * supplies an {@link OnAlarmListener} instance that will be invoked at that time. |
| 465 | * <p> |
| 466 | * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 467 | * invoked via the specified target Handler, or on the application's main looper |
| 468 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 469 | * |
| 470 | * @param type type of alarm. |
| 471 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 472 | * off, using the appropriate clock (depending on the alarm type). |
| 473 | * @param tag string describing the alarm, used for logging and battery-use |
| 474 | * attribution |
| 475 | * @param listener {@link OnAlarmListener} instance whose |
| 476 | * {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 477 | * called when the alarm time is reached. A given OnAlarmListener instance can |
| 478 | * only be the target of a single pending alarm, just as a given PendingIntent |
| 479 | * can only be used with one alarm at a time. |
| 480 | * @param targetHandler {@link Handler} on which to execute the listener's onAlarm() |
| 481 | * callback, or {@code null} to run that callback on the main looper. |
| 482 | */ |
| 483 | public void set(@AlarmType int type, long triggerAtMillis, String tag, OnAlarmListener listener, |
| 484 | Handler targetHandler) { |
| 485 | setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, null, listener, tag, |
| 486 | targetHandler, null, null); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Schedule a repeating alarm. <b>Note: for timing operations (ticks, |
| 491 | * timeouts, etc) it is easier and much more efficient to use |
| 492 | * {@link android.os.Handler}.</b> If there is already an alarm scheduled |
| 493 | * for the same IntentSender, it will first be canceled. |
| 494 | * |
| 495 | * <p>Like {@link #set}, except you can also supply a period at which |
| 496 | * the alarm will automatically repeat. This alarm continues |
| 497 | * repeating until explicitly removed with {@link #cancel}. If the stated |
| 498 | * trigger time is in the past, the alarm will be triggered immediately, with an |
| 499 | * alarm count depending on how far in the past the trigger time is relative |
| 500 | * to the repeat interval. |
| 501 | * |
| 502 | * <p>If an alarm is delayed (by system sleep, for example, for non |
| 503 | * _WAKEUP alarm types), a skipped repeat will be delivered as soon as |
| 504 | * possible. After that, future alarms will be delivered according to the |
| 505 | * original schedule; they do not drift over time. For example, if you have |
| 506 | * set a recurring alarm for the top of every hour but the phone was asleep |
| 507 | * from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens, |
| 508 | * then the next alarm will be sent at 9:00. |
| 509 | * |
| 510 | * <p>If your application wants to allow the delivery times to drift in |
| 511 | * order to guarantee that at least a certain time interval always elapses |
| 512 | * between alarms, then the approach to take is to use one-time alarms, |
| 513 | * scheduling the next one yourself when handling each alarm delivery. |
| 514 | * |
| 515 | * <p class="note"> |
| 516 | * <b>Note:</b> as of API 19, all repeating alarms are inexact. If your |
| 517 | * application needs precise delivery times then it must use one-time |
| 518 | * exact alarms, rescheduling each time as described above. Legacy applications |
| 519 | * whose {@code targetSdkVersion} is earlier than API 19 will continue to have all |
| 520 | * of their alarms, including repeating alarms, treated as exact. |
| 521 | * <p>Apps targeting {@link Build.VERSION_CODES#S} will need to set the flag |
| 522 | * {@link PendingIntent#FLAG_MUTABLE} on the {@link PendingIntent} being used to set this alarm, |
| 523 | * if they want the alarm count to be supplied with the key {@link Intent#EXTRA_ALARM_COUNT}. |
| 524 | * |
| 525 | * @param type type of alarm. |
| 526 | * @param triggerAtMillis time in milliseconds that the alarm should first |
| 527 | * go off, using the appropriate clock (depending on the alarm type). |
| 528 | * @param intervalMillis interval in milliseconds between subsequent repeats |
| 529 | * of the alarm. |
| 530 | * @param operation Action to perform when the alarm goes off; |
| 531 | * typically comes from {@link PendingIntent#getBroadcast |
| 532 | * IntentSender.getBroadcast()}. |
| 533 | * |
| 534 | * @see android.os.Handler |
| 535 | * @see #set |
| 536 | * @see #setExact |
| 537 | * @see #setWindow |
| 538 | * @see #cancel |
| 539 | * @see android.content.Context#sendBroadcast |
| 540 | * @see android.content.Context#registerReceiver |
| 541 | * @see android.content.Intent#filterEquals |
| 542 | * @see #ELAPSED_REALTIME |
| 543 | * @see #ELAPSED_REALTIME_WAKEUP |
| 544 | * @see #RTC |
| 545 | * @see #RTC_WAKEUP |
| 546 | * @see Intent#EXTRA_ALARM_COUNT |
| 547 | */ |
| 548 | public void setRepeating(@AlarmType int type, long triggerAtMillis, |
| 549 | long intervalMillis, PendingIntent operation) { |
| 550 | setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, 0, operation, |
| 551 | null, null, (Handler) null, null, null); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Schedule an alarm to be delivered within a given window of time. This method |
| 556 | * is similar to {@link #set(int, long, PendingIntent)}, but allows the |
| 557 | * application to precisely control the degree to which its delivery might be |
| 558 | * adjusted by the OS. This method allows an application to take advantage of the |
| 559 | * battery optimizations that arise from delivery batching even when it has |
| 560 | * modest timeliness requirements for its alarms. |
| 561 | * |
| 562 | * <p> |
| 563 | * Note: Starting with API {@link Build.VERSION_CODES#S}, apps should not pass in a window of |
| 564 | * less than 10 minutes. The system will try its best to accommodate smaller windows if the |
| 565 | * alarm is supposed to fire in the near future, but there are no guarantees and the app should |
| 566 | * expect any window smaller than 10 minutes to get elongated to 10 minutes. |
| 567 | * |
| 568 | * <p> |
| 569 | * This method can also be used to achieve strict ordering guarantees among |
| 570 | * multiple alarms by ensuring that the windows requested for each alarm do |
| 571 | * not intersect. |
| 572 | * |
| 573 | * <p> |
| 574 | * When precise delivery is not required, applications should use the standard |
| 575 | * {@link #set(int, long, PendingIntent)} method. This will give the OS the most |
| 576 | * flexibility to minimize wakeups and battery use. For alarms that must be delivered |
| 577 | * at precisely-specified times with no acceptable variation, applications can use |
| 578 | * {@link #setExact(int, long, PendingIntent)}. |
| 579 | * |
| 580 | * @param type type of alarm. |
| 581 | * @param windowStartMillis The earliest time, in milliseconds, that the alarm should |
| 582 | * be delivered, expressed in the appropriate clock's units (depending on the alarm |
| 583 | * type). |
| 584 | * @param windowLengthMillis The length of the requested delivery window, |
| 585 | * in milliseconds. The alarm will be delivered no later than this many |
| 586 | * milliseconds after {@code windowStartMillis}. Note that this parameter |
| 587 | * is a <i>duration,</i> not the timestamp of the end of the window. |
| 588 | * @param operation Action to perform when the alarm goes off; |
| 589 | * typically comes from {@link PendingIntent#getBroadcast |
| 590 | * IntentSender.getBroadcast()}. |
| 591 | * |
| 592 | * @see #set |
| 593 | * @see #setExact |
| 594 | * @see #setRepeating |
| 595 | * @see #cancel |
| 596 | * @see android.content.Context#sendBroadcast |
| 597 | * @see android.content.Context#registerReceiver |
| 598 | * @see android.content.Intent#filterEquals |
| 599 | * @see #ELAPSED_REALTIME |
| 600 | * @see #ELAPSED_REALTIME_WAKEUP |
| 601 | * @see #RTC |
| 602 | * @see #RTC_WAKEUP |
| 603 | */ |
| 604 | public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis, |
| 605 | PendingIntent operation) { |
| 606 | setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, operation, |
| 607 | null, null, (Handler) null, null, null); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Direct callback version of {@link #setWindow(int, long, long, PendingIntent)}. Rather |
| 612 | * than supplying a PendingIntent to be sent when the alarm time is reached, this variant |
| 613 | * supplies an {@link OnAlarmListener} instance that will be invoked at that time. |
| 614 | * <p> |
| 615 | * The OnAlarmListener {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 616 | * invoked via the specified target Handler, or on the application's main looper |
| 617 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 618 | * |
| 619 | * <p> |
| 620 | * Note: Starting with API {@link Build.VERSION_CODES#S}, apps should not pass in a window of |
| 621 | * less than 10 minutes. The system will try its best to accommodate smaller windows if the |
| 622 | * alarm is supposed to fire in the near future, but there are no guarantees and the app should |
| 623 | * expect any window smaller than 10 minutes to get elongated to 10 minutes. |
| 624 | * |
| 625 | * @see #setWindow(int, long, long, PendingIntent) |
| 626 | */ |
| 627 | public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis, |
| 628 | String tag, OnAlarmListener listener, Handler targetHandler) { |
| 629 | setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, null, listener, tag, |
| 630 | targetHandler, null, null); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Schedule an alarm that is prioritized by the system while the device is in power saving modes |
| 635 | * such as battery saver and device idle (doze). |
| 636 | * |
| 637 | * <p> |
| 638 | * Apps that use this are not guaranteed to get all alarms as requested during power saving |
| 639 | * modes, i.e. the system may still impose restrictions on how frequently these alarms will go |
| 640 | * off for a particular application, like requiring a certain minimum duration be elapsed |
| 641 | * between consecutive alarms. This duration will be normally be in the order of a few minutes. |
| 642 | * |
| 643 | * <p> |
| 644 | * When the system wakes up to deliver these alarms, it may not deliver any of the other pending |
| 645 | * alarms set earlier by the calling app, even the special ones set via |
| 646 | * {@link #setAndAllowWhileIdle(int, long, PendingIntent)} or |
| 647 | * {@link #setExactAndAllowWhileIdle(int, long, PendingIntent)}. So the caller should not |
| 648 | * expect these to arrive in any relative order to its other alarms. |
| 649 | * |
| 650 | * @param type type of alarm |
| 651 | * @param windowStartMillis The earliest time, in milliseconds, that the alarm should |
| 652 | * be delivered, expressed in the appropriate clock's units (depending on the alarm |
| 653 | * type). |
| 654 | * @param windowLengthMillis The length of the requested delivery window, |
| 655 | * in milliseconds. The alarm will be delivered no later than this many |
| 656 | * milliseconds after {@code windowStartMillis}. Note that this parameter |
| 657 | * is a <i>duration,</i> not the timestamp of the end of the window. |
| 658 | * @param tag Optional. A string describing the alarm, used for logging and battery-use |
| 659 | * attribution. |
| 660 | * @param listener {@link OnAlarmListener} instance whose |
| 661 | * {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 662 | * called when the alarm time is reached. A given OnAlarmListener instance can |
| 663 | * only be the target of a single pending alarm, just as a given PendingIntent |
| 664 | * can only be used with one alarm at a time. |
| 665 | * @param executor {@link Executor} on which to execute the listener's onAlarm() |
| 666 | * callback. |
| 667 | * @hide |
| 668 | */ |
| 669 | @SystemApi |
| 670 | @RequiresPermission(Manifest.permission.SCHEDULE_PRIORITIZED_ALARM) |
| 671 | public void setPrioritized(@AlarmType int type, long windowStartMillis, long windowLengthMillis, |
| 672 | @Nullable String tag, @NonNull Executor executor, @NonNull OnAlarmListener listener) { |
| 673 | Objects.requireNonNull(executor); |
| 674 | Objects.requireNonNull(listener); |
| 675 | setImpl(type, windowStartMillis, windowLengthMillis, 0, FLAG_PRIORITIZE, null, listener, |
| 676 | tag, executor, null, null); |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Schedule an alarm to be delivered precisely at the stated time. |
| 681 | * |
| 682 | * <p> |
| 683 | * This method is like {@link #set(int, long, PendingIntent)}, but does not permit |
| 684 | * the OS to adjust the delivery time. The alarm will be delivered as nearly as |
| 685 | * possible to the requested trigger time. |
| 686 | * |
| 687 | * <p> |
| 688 | * <b>Note:</b> only alarms for which there is a strong demand for exact-time |
| 689 | * delivery (such as an alarm clock ringing at the requested time) should be |
| 690 | * scheduled as exact. Applications are strongly discouraged from using exact |
| 691 | * alarms unnecessarily as they reduce the OS's ability to minimize battery use. |
| 692 | * |
| 693 | * <p class="note"><strong>Note:</strong> |
| 694 | * Starting with {@link Build.VERSION_CODES#S}, apps targeting SDK level 31 or higher |
| 695 | * need to request the |
| 696 | * {@link Manifest.permission#SCHEDULE_EXACT_ALARM SCHEDULE_EXACT_ALARM} permission to use this |
| 697 | * API, unless the app is exempt from battery restrictions. |
| 698 | * The user and the system can revoke this permission via the special app access screen in |
| 699 | * Settings. |
| 700 | * |
| 701 | * <p class="note"><strong>Note:</strong> |
| 702 | * Exact alarms should only be used for user-facing features. |
| 703 | * For more details, see <a |
| 704 | * href="{@docRoot}about/versions/12/behavior-changes-12#exact-alarm-permission"> |
| 705 | * Exact alarm permission</a>. |
| 706 | * |
| 707 | * @param type type of alarm. |
| 708 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 709 | * off, using the appropriate clock (depending on the alarm type). |
| 710 | * @param operation Action to perform when the alarm goes off; |
| 711 | * typically comes from {@link PendingIntent#getBroadcast |
| 712 | * IntentSender.getBroadcast()}. |
| 713 | * |
| 714 | * @see #set |
| 715 | * @see #setRepeating |
| 716 | * @see #setWindow |
| 717 | * @see #cancel |
| 718 | * @see android.content.Context#sendBroadcast |
| 719 | * @see android.content.Context#registerReceiver |
| 720 | * @see android.content.Intent#filterEquals |
| 721 | * @see #ELAPSED_REALTIME |
| 722 | * @see #ELAPSED_REALTIME_WAKEUP |
| 723 | * @see #RTC |
| 724 | * @see #RTC_WAKEUP |
| 725 | * @see Manifest.permission#SCHEDULE_EXACT_ALARM SCHEDULE_EXACT_ALARM |
| 726 | */ |
| 727 | @RequiresPermission(value = Manifest.permission.SCHEDULE_EXACT_ALARM, conditional = true) |
| 728 | public void setExact(@AlarmType int type, long triggerAtMillis, PendingIntent operation) { |
| 729 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, operation, null, null, (Handler) null, |
| 730 | null, null); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Direct callback version of {@link #setExact(int, long, PendingIntent)}. Rather |
| 735 | * than supplying a PendingIntent to be sent when the alarm time is reached, this variant |
| 736 | * supplies an {@link OnAlarmListener} instance that will be invoked at that time. |
| 737 | * <p> |
| 738 | * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 739 | * invoked via the specified target Handler, or on the application's main looper |
| 740 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 741 | * |
| 742 | * <p class="note"><strong>Note:</strong> |
| 743 | * Starting with {@link Build.VERSION_CODES#S}, apps targeting SDK level 31 or higher |
| 744 | * need to request the |
| 745 | * {@link Manifest.permission#SCHEDULE_EXACT_ALARM SCHEDULE_EXACT_ALARM} permission to use this |
| 746 | * API, unless the app is exempt from battery restrictions. |
| 747 | * The user and the system can revoke this permission via the special app access screen in |
| 748 | * Settings. |
| 749 | * |
| 750 | * <p class="note"><strong>Note:</strong> |
| 751 | * Exact alarms should only be used for user-facing features. |
| 752 | * For more details, see <a |
| 753 | * href="{@docRoot}about/versions/12/behavior-changes-12#exact-alarm-permission"> |
| 754 | * Exact alarm permission</a>. |
| 755 | * |
| 756 | * @see Manifest.permission#SCHEDULE_EXACT_ALARM SCHEDULE_EXACT_ALARM |
| 757 | */ |
| 758 | @RequiresPermission(value = Manifest.permission.SCHEDULE_EXACT_ALARM, conditional = true) |
| 759 | public void setExact(@AlarmType int type, long triggerAtMillis, String tag, |
| 760 | OnAlarmListener listener, Handler targetHandler) { |
| 761 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, null, listener, tag, |
| 762 | targetHandler, null, null); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Schedule an idle-until alarm, which will keep the alarm manager idle until |
| 767 | * the given time. |
| 768 | * @hide |
| 769 | */ |
| 770 | public void setIdleUntil(@AlarmType int type, long triggerAtMillis, String tag, |
| 771 | OnAlarmListener listener, Handler targetHandler) { |
| 772 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_IDLE_UNTIL, null, |
| 773 | listener, tag, targetHandler, null, null); |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Schedule an alarm that represents an alarm clock, which will be used to notify the user |
| 778 | * when it goes off. The expectation is that when this alarm triggers, the application will |
| 779 | * further wake up the device to tell the user about the alarm -- turning on the screen, |
| 780 | * playing a sound, vibrating, etc. As such, the system will typically also use the |
| 781 | * information supplied here to tell the user about this upcoming alarm if appropriate. |
| 782 | * |
| 783 | * <p>Due to the nature of this kind of alarm, similar to {@link #setExactAndAllowWhileIdle}, |
| 784 | * these alarms will be allowed to trigger even if the system is in a low-power idle |
| 785 | * (a.k.a. doze) mode. The system may also do some prep-work when it sees that such an |
| 786 | * alarm coming up, to reduce the amount of background work that could happen if this |
| 787 | * causes the device to fully wake up -- this is to avoid situations such as a large number |
| 788 | * of devices having an alarm set at the same time in the morning, all waking up at that |
| 789 | * time and suddenly swamping the network with pending background work. As such, these |
| 790 | * types of alarms can be extremely expensive on battery use and should only be used for |
| 791 | * their intended purpose.</p> |
| 792 | * |
| 793 | * <p> |
| 794 | * This method is like {@link #setExact(int, long, PendingIntent)}, but implies |
| 795 | * {@link #RTC_WAKEUP}. |
| 796 | * |
| 797 | * <p class="note"><strong>Note:</strong> |
| 798 | * Starting with {@link Build.VERSION_CODES#S}, apps targeting SDK level 31 or higher |
| 799 | * need to request the |
| 800 | * {@link Manifest.permission#SCHEDULE_EXACT_ALARM SCHEDULE_EXACT_ALARM} permission to use this |
| 801 | * API. |
| 802 | * The user and the system can revoke this permission via the special app access screen in |
| 803 | * Settings. |
| 804 | * |
| 805 | * <p class="note"><strong>Note:</strong> |
| 806 | * Exact alarms should only be used for user-facing features. |
| 807 | * For more details, see <a |
| 808 | * href="{@docRoot}about/versions/12/behavior-changes-12#exact-alarm-permission"> |
| 809 | * Exact alarm permission</a>. |
| 810 | * |
| 811 | * <p>Alarms scheduled via this API |
| 812 | * will be allowed to start a foreground service even if the app is in the background. |
| 813 | * |
| 814 | * @param info |
| 815 | * @param operation Action to perform when the alarm goes off; |
| 816 | * typically comes from {@link PendingIntent#getBroadcast |
| 817 | * IntentSender.getBroadcast()}. |
| 818 | * |
| 819 | * @see #set |
| 820 | * @see #setRepeating |
| 821 | * @see #setWindow |
| 822 | * @see #setExact |
| 823 | * @see #cancel |
| 824 | * @see #getNextAlarmClock() |
| 825 | * @see android.content.Context#sendBroadcast |
| 826 | * @see android.content.Context#registerReceiver |
| 827 | * @see android.content.Intent#filterEquals |
| 828 | * @see Manifest.permission#SCHEDULE_EXACT_ALARM SCHEDULE_EXACT_ALARM |
| 829 | */ |
| 830 | @RequiresPermission(Manifest.permission.SCHEDULE_EXACT_ALARM) |
| 831 | public void setAlarmClock(AlarmClockInfo info, PendingIntent operation) { |
| 832 | setImpl(RTC_WAKEUP, info.getTriggerTime(), WINDOW_EXACT, 0, 0, operation, |
| 833 | null, null, (Handler) null, null, info); |
| 834 | } |
| 835 | |
| 836 | /** @hide */ |
| 837 | @SystemApi |
| 838 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 839 | public void set(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 840 | long intervalMillis, PendingIntent operation, WorkSource workSource) { |
| 841 | setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, operation, null, null, |
| 842 | (Handler) null, workSource, null); |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}. |
| 847 | * Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener. |
| 848 | * <p> |
| 849 | * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 850 | * invoked via the specified target Handler, or on the application's main looper |
| 851 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 852 | * |
| 853 | * @hide |
| 854 | */ |
| 855 | @UnsupportedAppUsage |
| 856 | public void set(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 857 | long intervalMillis, String tag, OnAlarmListener listener, Handler targetHandler, |
| 858 | WorkSource workSource) { |
| 859 | setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, tag, |
| 860 | targetHandler, workSource, null); |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}. |
| 865 | * Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener. |
| 866 | * <p> |
| 867 | * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be |
| 868 | * invoked via the specified target Handler, or on the application's main looper |
| 869 | * if {@code null} is passed as the {@code targetHandler} parameter. |
| 870 | * |
| 871 | * @hide |
| 872 | */ |
| 873 | @SystemApi |
| 874 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 875 | public void set(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 876 | long intervalMillis, OnAlarmListener listener, Handler targetHandler, |
| 877 | WorkSource workSource) { |
| 878 | setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, null, |
| 879 | targetHandler, workSource, null); |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * Exact version of {@link #set(int, long, long, long, OnAlarmListener, Handler, WorkSource)}. |
| 884 | * This equivalent to calling the aforementioned API with {@code windowMillis} and |
| 885 | * {@code intervalMillis} set to 0. |
| 886 | * One subtle difference is that this API requires {@code workSource} to be non-null. If you |
| 887 | * don't want to attribute this alarm to another app for battery consumption, you should use |
| 888 | * {@link #setExact(int, long, String, OnAlarmListener, Handler)} instead. |
| 889 | * |
| 890 | * <p> |
| 891 | * Note that using this API requires you to hold |
| 892 | * {@link Manifest.permission#SCHEDULE_EXACT_ALARM}, unless you are on the system's power |
| 893 | * allowlist. This can be set, for example, by marking the app as {@code <allow-in-power-save>} |
| 894 | * within the system config. |
| 895 | * |
| 896 | * @param type type of alarm |
| 897 | * @param triggerAtMillis The exact time in milliseconds, that the alarm should be delivered, |
| 898 | * expressed in the appropriate clock's units (depending on the alarm |
| 899 | * type). |
| 900 | * @param listener {@link OnAlarmListener} instance whose |
| 901 | * {@link OnAlarmListener#onAlarm() onAlarm()} method will be called when |
| 902 | * the alarm time is reached. |
| 903 | * @param executor The {@link Executor} on which to execute the listener's onAlarm() |
| 904 | * callback. |
| 905 | * @param tag Optional. A string tag used to identify this alarm in logs and |
| 906 | * battery-attribution. |
| 907 | * @param workSource A {@link WorkSource} object to attribute this alarm to the app that |
| 908 | * requested this work. |
| 909 | * @hide |
| 910 | */ |
| 911 | @SystemApi |
| 912 | @RequiresPermission(allOf = { |
| 913 | Manifest.permission.UPDATE_DEVICE_STATS, |
| 914 | Manifest.permission.SCHEDULE_EXACT_ALARM}, conditional = true) |
| 915 | public void setExact(@AlarmType int type, long triggerAtMillis, @Nullable String tag, |
| 916 | @NonNull Executor executor, @NonNull WorkSource workSource, |
| 917 | @NonNull OnAlarmListener listener) { |
| 918 | Objects.requireNonNull(executor); |
| 919 | Objects.requireNonNull(workSource); |
| 920 | Objects.requireNonNull(listener); |
| 921 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, null, listener, tag, executor, |
| 922 | workSource, null); |
| 923 | } |
| 924 | |
| 925 | |
| 926 | private void setImpl(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 927 | long intervalMillis, int flags, PendingIntent operation, final OnAlarmListener listener, |
| 928 | String listenerTag, Handler targetHandler, WorkSource workSource, |
| 929 | AlarmClockInfo alarmClock) { |
| 930 | final Handler handlerToUse = (targetHandler != null) ? targetHandler : mMainThreadHandler; |
| 931 | setImpl(type, triggerAtMillis, windowMillis, intervalMillis, flags, operation, listener, |
| 932 | listenerTag, new HandlerExecutor(handlerToUse), workSource, alarmClock); |
| 933 | } |
| 934 | |
| 935 | private void setImpl(@AlarmType int type, long triggerAtMillis, long windowMillis, |
| 936 | long intervalMillis, int flags, PendingIntent operation, final OnAlarmListener listener, |
| 937 | String listenerTag, Executor targetExecutor, WorkSource workSource, |
| 938 | AlarmClockInfo alarmClock) { |
| 939 | if (triggerAtMillis < 0) { |
| 940 | /* NOTYET |
| 941 | if (mAlwaysExact) { |
| 942 | // Fatal error for KLP+ apps to use negative trigger times |
| 943 | throw new IllegalArgumentException("Invalid alarm trigger time " |
| 944 | + triggerAtMillis); |
| 945 | } |
| 946 | */ |
| 947 | triggerAtMillis = 0; |
| 948 | } |
| 949 | |
| 950 | ListenerWrapper recipientWrapper = null; |
| 951 | if (listener != null) { |
| 952 | synchronized (AlarmManager.class) { |
| 953 | if (sWrappers == null) { |
| 954 | sWrappers = new WeakHashMap<>(); |
| 955 | } |
| 956 | |
| 957 | final WeakReference<ListenerWrapper> weakRef = sWrappers.get(listener); |
| 958 | if (weakRef != null) { |
| 959 | recipientWrapper = weakRef.get(); |
| 960 | } |
| 961 | // no existing wrapper => build a new one |
| 962 | if (recipientWrapper == null) { |
| 963 | recipientWrapper = new ListenerWrapper(listener); |
| 964 | sWrappers.put(listener, new WeakReference<>(recipientWrapper)); |
| 965 | } |
| 966 | } |
| 967 | recipientWrapper.setExecutor(targetExecutor); |
| 968 | } |
| 969 | |
| 970 | try { |
| 971 | mService.set(mPackageName, type, triggerAtMillis, windowMillis, intervalMillis, flags, |
| 972 | operation, recipientWrapper, listenerTag, workSource, alarmClock); |
| 973 | } catch (RemoteException ex) { |
| 974 | throw ex.rethrowFromSystemServer(); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * Available inexact recurrence interval recognized by |
| 980 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 981 | * when running on Android prior to API 19. |
| 982 | */ |
| 983 | public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000; |
| 984 | |
| 985 | /** |
| 986 | * Available inexact recurrence interval recognized by |
| 987 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 988 | * when running on Android prior to API 19. |
| 989 | */ |
| 990 | public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES; |
| 991 | |
| 992 | /** |
| 993 | * Available inexact recurrence interval recognized by |
| 994 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 995 | * when running on Android prior to API 19. |
| 996 | */ |
| 997 | public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR; |
| 998 | |
| 999 | /** |
| 1000 | * Available inexact recurrence interval recognized by |
| 1001 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 1002 | * when running on Android prior to API 19. |
| 1003 | */ |
| 1004 | public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR; |
| 1005 | |
| 1006 | /** |
| 1007 | * Available inexact recurrence interval recognized by |
| 1008 | * {@link #setInexactRepeating(int, long, long, PendingIntent)} |
| 1009 | * when running on Android prior to API 19. |
| 1010 | */ |
| 1011 | public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY; |
| 1012 | |
| 1013 | /** |
| 1014 | * Schedule a repeating alarm that has inexact trigger time requirements; |
| 1015 | * for example, an alarm that repeats every hour, but not necessarily at |
| 1016 | * the top of every hour. These alarms are more power-efficient than |
| 1017 | * the strict recurrences traditionally supplied by {@link #setRepeating}, since the |
| 1018 | * system can adjust alarms' delivery times to cause them to fire simultaneously, |
| 1019 | * avoiding waking the device from sleep more than necessary. |
| 1020 | * |
| 1021 | * <p>Your alarm's first trigger will not be before the requested time, |
| 1022 | * but it might not occur for almost a full interval after that time. In |
| 1023 | * addition, while the overall period of the repeating alarm will be as |
| 1024 | * requested, the time between any two successive firings of the alarm |
| 1025 | * may vary. If your application demands very low jitter, use |
| 1026 | * one-shot alarms with an appropriate window instead; see {@link |
| 1027 | * #setWindow(int, long, long, PendingIntent)} and |
| 1028 | * {@link #setExact(int, long, PendingIntent)}. |
| 1029 | * |
| 1030 | * <p class="note"> |
| 1031 | * As of API 19, all repeating alarms are inexact. Because this method has |
| 1032 | * been available since API 3, your application can safely call it and be |
| 1033 | * assured that it will get similar behavior on both current and older versions |
| 1034 | * of Android. |
| 1035 | * <p>Apps targeting {@link Build.VERSION_CODES#S} will need to set the flag |
| 1036 | * {@link PendingIntent#FLAG_MUTABLE} on the {@link PendingIntent} being used to set this alarm, |
| 1037 | * if they want the alarm count to be supplied with the key {@link Intent#EXTRA_ALARM_COUNT}. |
| 1038 | * |
| 1039 | * @param type type of alarm. |
| 1040 | * @param triggerAtMillis time in milliseconds that the alarm should first |
| 1041 | * go off, using the appropriate clock (depending on the alarm type). This |
| 1042 | * is inexact: the alarm will not fire before this time, but there may be a |
| 1043 | * delay of almost an entire alarm interval before the first invocation of |
| 1044 | * the alarm. |
| 1045 | * @param intervalMillis interval in milliseconds between subsequent repeats |
| 1046 | * of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES, |
| 1047 | * INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY |
| 1048 | * then the alarm will be phase-aligned with other alarms to reduce the |
| 1049 | * number of wakeups. Otherwise, the alarm will be set as though the |
| 1050 | * application had called {@link #setRepeating}. As of API 19, all repeating |
| 1051 | * alarms will be inexact and subject to batching with other alarms regardless |
| 1052 | * of their stated repeat interval. |
| 1053 | * @param operation Action to perform when the alarm goes off; |
| 1054 | * typically comes from {@link PendingIntent#getBroadcast |
| 1055 | * IntentSender.getBroadcast()}. |
| 1056 | * |
| 1057 | * @see android.os.Handler |
| 1058 | * @see #set |
| 1059 | * @see #cancel |
| 1060 | * @see android.content.Context#sendBroadcast |
| 1061 | * @see android.content.Context#registerReceiver |
| 1062 | * @see android.content.Intent#filterEquals |
| 1063 | * @see #ELAPSED_REALTIME |
| 1064 | * @see #ELAPSED_REALTIME_WAKEUP |
| 1065 | * @see #RTC |
| 1066 | * @see #RTC_WAKEUP |
| 1067 | * @see #INTERVAL_FIFTEEN_MINUTES |
| 1068 | * @see #INTERVAL_HALF_HOUR |
| 1069 | * @see #INTERVAL_HOUR |
| 1070 | * @see #INTERVAL_HALF_DAY |
| 1071 | * @see #INTERVAL_DAY |
| 1072 | * @see Intent#EXTRA_ALARM_COUNT |
| 1073 | */ |
| 1074 | public void setInexactRepeating(@AlarmType int type, long triggerAtMillis, |
| 1075 | long intervalMillis, PendingIntent operation) { |
| 1076 | setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, intervalMillis, 0, operation, null, |
| 1077 | null, (Handler) null, null, null); |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * Like {@link #set(int, long, PendingIntent)}, but this alarm will be allowed to execute |
| 1082 | * even when the system is in low-power idle (a.k.a. doze) modes. This type of alarm must |
| 1083 | * <b>only</b> be used for situations where it is actually required that the alarm go off while |
| 1084 | * in idle -- a reasonable example would be for a calendar notification that should make a |
| 1085 | * sound so the user is aware of it. When the alarm is dispatched, the app will also be |
| 1086 | * added to the system's temporary power exemption list for approximately 10 seconds to allow |
| 1087 | * that application to acquire further wake locks in which to complete its work.</p> |
| 1088 | * |
| 1089 | * <p>These alarms can significantly impact the power use |
| 1090 | * of the device when idle (and thus cause significant battery blame to the app scheduling |
| 1091 | * them), so they should be used with care. To reduce abuse, there are restrictions on how |
| 1092 | * frequently these alarms will go off for a particular application. |
| 1093 | * Under normal system operation, it will not dispatch these |
| 1094 | * alarms more than about every minute (at which point every such pending alarm is |
| 1095 | * dispatched); when in low-power idle modes this duration may be significantly longer, |
| 1096 | * such as 15 minutes.</p> |
| 1097 | * |
| 1098 | * <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen |
| 1099 | * out of order with any other alarms, even those from the same app. This will clearly happen |
| 1100 | * when the device is idle (since this alarm can go off while idle, when any other alarms |
| 1101 | * from the app will be held until later), but may also happen even when not idle.</p> |
| 1102 | * |
| 1103 | * <p>Regardless of the app's target SDK version, this call always allows batching of the |
| 1104 | * alarm.</p> |
| 1105 | * |
| 1106 | * @param type type of alarm. |
| 1107 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 1108 | * off, using the appropriate clock (depending on the alarm type). |
| 1109 | * @param operation Action to perform when the alarm goes off; |
| 1110 | * typically comes from {@link PendingIntent#getBroadcast |
| 1111 | * IntentSender.getBroadcast()}. |
| 1112 | * |
| 1113 | * @see #set(int, long, PendingIntent) |
| 1114 | * @see #setExactAndAllowWhileIdle |
| 1115 | * @see #cancel |
| 1116 | * @see android.content.Context#sendBroadcast |
| 1117 | * @see android.content.Context#registerReceiver |
| 1118 | * @see android.content.Intent#filterEquals |
| 1119 | * @see #ELAPSED_REALTIME |
| 1120 | * @see #ELAPSED_REALTIME_WAKEUP |
| 1121 | * @see #RTC |
| 1122 | * @see #RTC_WAKEUP |
| 1123 | */ |
| 1124 | public void setAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis, |
| 1125 | PendingIntent operation) { |
| 1126 | setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, 0, FLAG_ALLOW_WHILE_IDLE, |
| 1127 | operation, null, null, (Handler) null, null, null); |
| 1128 | } |
| 1129 | |
| 1130 | /** |
| 1131 | * Like {@link #setExact(int, long, PendingIntent)}, but this alarm will be allowed to execute |
| 1132 | * even when the system is in low-power idle modes. If you don't need exact scheduling of |
| 1133 | * the alarm but still need to execute while idle, consider using |
| 1134 | * {@link #setAndAllowWhileIdle}. This type of alarm must <b>only</b> |
| 1135 | * be used for situations where it is actually required that the alarm go off while in |
| 1136 | * idle -- a reasonable example would be for a calendar notification that should make a |
| 1137 | * sound so the user is aware of it. When the alarm is dispatched, the app will also be |
| 1138 | * added to the system's temporary power exemption list for approximately 10 seconds to allow |
| 1139 | * that application to acquire further wake locks in which to complete its work.</p> |
| 1140 | * |
| 1141 | * <p>These alarms can significantly impact the power use |
| 1142 | * of the device when idle (and thus cause significant battery blame to the app scheduling |
| 1143 | * them), so they should be used with care. To reduce abuse, there are restrictions on how |
| 1144 | * frequently these alarms will go off for a particular application. |
| 1145 | * Under normal system operation, it will not dispatch these |
| 1146 | * alarms more than about every minute (at which point every such pending alarm is |
| 1147 | * dispatched); when in low-power idle modes this duration may be significantly longer, |
| 1148 | * such as 15 minutes.</p> |
| 1149 | * |
| 1150 | * <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen |
| 1151 | * out of order with any other alarms, even those from the same app. This will clearly happen |
| 1152 | * when the device is idle (since this alarm can go off while idle, when any other alarms |
| 1153 | * from the app will be held until later), but may also happen even when not idle. |
| 1154 | * Note that the OS will allow itself more flexibility for scheduling these alarms than |
| 1155 | * regular exact alarms, since the application has opted into this behavior. When the |
| 1156 | * device is idle it may take even more liberties with scheduling in order to optimize |
| 1157 | * for battery life.</p> |
| 1158 | * |
| 1159 | * <p class="note"><strong>Note:</strong> |
| 1160 | * Starting with {@link Build.VERSION_CODES#S}, apps targeting SDK level 31 or higher |
| 1161 | * need to request the |
| 1162 | * {@link Manifest.permission#SCHEDULE_EXACT_ALARM SCHEDULE_EXACT_ALARM} permission to use this |
| 1163 | * API, unless the app is exempt from battery restrictions. |
| 1164 | * The user and the system can revoke this permission via the special app access screen in |
| 1165 | * Settings. |
| 1166 | * |
| 1167 | * <p class="note"><strong>Note:</strong> |
| 1168 | * Exact alarms should only be used for user-facing features. |
| 1169 | * For more details, see <a |
| 1170 | * href="{@docRoot}about/versions/12/behavior-changes-12#exact-alarm-permission"> |
| 1171 | * Exact alarm permission</a>. |
| 1172 | * |
| 1173 | * <p>Alarms scheduled via this API |
| 1174 | * will be allowed to start a foreground service even if the app is in the background. |
| 1175 | * |
| 1176 | * @param type type of alarm. |
| 1177 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 1178 | * off, using the appropriate clock (depending on the alarm type). |
| 1179 | * @param operation Action to perform when the alarm goes off; |
| 1180 | * typically comes from {@link PendingIntent#getBroadcast |
| 1181 | * IntentSender.getBroadcast()}. |
| 1182 | * |
| 1183 | * @see #set |
| 1184 | * @see #setRepeating |
| 1185 | * @see #setWindow |
| 1186 | * @see #cancel |
| 1187 | * @see android.content.Context#sendBroadcast |
| 1188 | * @see android.content.Context#registerReceiver |
| 1189 | * @see android.content.Intent#filterEquals |
| 1190 | * @see #ELAPSED_REALTIME |
| 1191 | * @see #ELAPSED_REALTIME_WAKEUP |
| 1192 | * @see #RTC |
| 1193 | * @see #RTC_WAKEUP |
| 1194 | * @see Manifest.permission#SCHEDULE_EXACT_ALARM SCHEDULE_EXACT_ALARM |
| 1195 | */ |
| 1196 | @RequiresPermission(value = Manifest.permission.SCHEDULE_EXACT_ALARM, conditional = true) |
| 1197 | public void setExactAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis, |
| 1198 | PendingIntent operation) { |
| 1199 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_ALLOW_WHILE_IDLE, operation, |
| 1200 | null, null, (Handler) null, null, null); |
| 1201 | } |
| 1202 | |
| 1203 | /** |
| 1204 | * Remove any alarms with a matching {@link Intent}. |
| 1205 | * Any alarm, of any type, whose Intent matches this one (as defined by |
| 1206 | * {@link Intent#filterEquals}), will be canceled. |
| 1207 | * |
| 1208 | * @param operation IntentSender which matches a previously added |
| 1209 | * IntentSender. This parameter must not be {@code null}. |
| 1210 | * |
| 1211 | * @see #set |
| 1212 | */ |
| 1213 | public void cancel(PendingIntent operation) { |
| 1214 | if (operation == null) { |
| 1215 | final String msg = "cancel() called with a null PendingIntent"; |
| 1216 | if (mTargetSdkVersion >= Build.VERSION_CODES.N) { |
| 1217 | throw new NullPointerException(msg); |
| 1218 | } else { |
| 1219 | Log.e(TAG, msg); |
| 1220 | return; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | try { |
| 1225 | mService.remove(operation, null); |
| 1226 | } catch (RemoteException ex) { |
| 1227 | throw ex.rethrowFromSystemServer(); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * Remove any alarm scheduled to be delivered to the given {@link OnAlarmListener}. |
| 1233 | * |
| 1234 | * @param listener OnAlarmListener instance that is the target of a currently-set alarm. |
| 1235 | */ |
| 1236 | public void cancel(OnAlarmListener listener) { |
| 1237 | if (listener == null) { |
| 1238 | throw new NullPointerException("cancel() called with a null OnAlarmListener"); |
| 1239 | } |
| 1240 | |
| 1241 | ListenerWrapper wrapper = null; |
| 1242 | synchronized (AlarmManager.class) { |
| 1243 | if (sWrappers != null) { |
| 1244 | final WeakReference<ListenerWrapper> weakRef = sWrappers.get(listener); |
| 1245 | if (weakRef != null) { |
| 1246 | wrapper = weakRef.get(); |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | if (wrapper == null) { |
| 1252 | Log.w(TAG, "Unrecognized alarm listener " + listener); |
| 1253 | return; |
| 1254 | } |
| 1255 | |
| 1256 | wrapper.cancel(); |
| 1257 | } |
| 1258 | |
| 1259 | /** |
| 1260 | * Set the system wall clock time. |
| 1261 | * Requires the permission android.permission.SET_TIME. |
| 1262 | * |
| 1263 | * @param millis time in milliseconds since the Epoch |
| 1264 | */ |
| 1265 | @RequiresPermission(android.Manifest.permission.SET_TIME) |
| 1266 | public void setTime(long millis) { |
| 1267 | try { |
| 1268 | mService.setTime(millis); |
| 1269 | } catch (RemoteException ex) { |
| 1270 | throw ex.rethrowFromSystemServer(); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | /** |
| 1275 | * Sets the system's persistent default time zone. This is the time zone for all apps, even |
| 1276 | * after a reboot. Use {@link java.util.TimeZone#setDefault} if you just want to change the |
| 1277 | * time zone within your app, and even then prefer to pass an explicit |
| 1278 | * {@link java.util.TimeZone} to APIs that require it rather than changing the time zone for |
| 1279 | * all threads. |
| 1280 | * |
| 1281 | * <p> On android M and above, it is an error to pass in a non-Olson timezone to this |
| 1282 | * function. Note that this is a bad idea on all Android releases because POSIX and |
| 1283 | * the {@code TimeZone} class have opposite interpretations of {@code '+'} and {@code '-'} |
| 1284 | * in the same non-Olson ID. |
| 1285 | * |
| 1286 | * @param timeZone one of the Olson ids from the list returned by |
| 1287 | * {@link java.util.TimeZone#getAvailableIDs} |
| 1288 | */ |
| 1289 | @RequiresPermission(android.Manifest.permission.SET_TIME_ZONE) |
| 1290 | public void setTimeZone(String timeZone) { |
| 1291 | if (TextUtils.isEmpty(timeZone)) { |
| 1292 | return; |
| 1293 | } |
| 1294 | |
| 1295 | // Reject this timezone if it isn't an Olson zone we recognize. |
| 1296 | if (mTargetSdkVersion >= Build.VERSION_CODES.M) { |
| 1297 | boolean hasTimeZone = ZoneInfoDb.getInstance().hasTimeZone(timeZone); |
| 1298 | if (!hasTimeZone) { |
| 1299 | throw new IllegalArgumentException("Timezone: " + timeZone + " is not an Olson ID"); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | try { |
| 1304 | mService.setTimeZone(timeZone); |
| 1305 | } catch (RemoteException ex) { |
| 1306 | throw ex.rethrowFromSystemServer(); |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | /** @hide */ |
| 1311 | public long getNextWakeFromIdleTime() { |
| 1312 | try { |
| 1313 | return mService.getNextWakeFromIdleTime(); |
| 1314 | } catch (RemoteException ex) { |
| 1315 | throw ex.rethrowFromSystemServer(); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * Called to check if the caller can schedule exact alarms. |
| 1321 | * Your app schedules exact alarms when it calls any of the {@code setExact...} or |
| 1322 | * {@link #setAlarmClock(AlarmClockInfo, PendingIntent) setAlarmClock} API methods. |
| 1323 | * <p> |
| 1324 | * Apps targeting {@link Build.VERSION_CODES#S} or higher can schedule exact alarms only if they |
| 1325 | * have the {@link Manifest.permission#SCHEDULE_EXACT_ALARM} permission or they are on the |
| 1326 | * device's power-save exemption list. |
| 1327 | * These apps can also |
| 1328 | * start {@link android.provider.Settings#ACTION_REQUEST_SCHEDULE_EXACT_ALARM} to |
| 1329 | * request this permission from the user. |
| 1330 | * <p> |
| 1331 | * Apps targeting lower sdk versions, can always schedule exact alarms. |
| 1332 | * |
| 1333 | * @return {@code true} if the caller can schedule exact alarms, {@code false} otherwise. |
| 1334 | * @see android.provider.Settings#ACTION_REQUEST_SCHEDULE_EXACT_ALARM |
| 1335 | * @see #setExact(int, long, PendingIntent) |
| 1336 | * @see #setExactAndAllowWhileIdle(int, long, PendingIntent) |
| 1337 | * @see #setAlarmClock(AlarmClockInfo, PendingIntent) |
| 1338 | * @see android.os.PowerManager#isIgnoringBatteryOptimizations(String) |
| 1339 | */ |
| 1340 | public boolean canScheduleExactAlarms() { |
| 1341 | try { |
| 1342 | return mService.canScheduleExactAlarms(mContext.getOpPackageName()); |
| 1343 | } catch (RemoteException re) { |
| 1344 | throw re.rethrowFromSystemServer(); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * Called to check if the given package in the given user has the permission |
| 1350 | * {@link Manifest.permission#SCHEDULE_EXACT_ALARM}. |
| 1351 | * |
| 1352 | * <p><em>Note: This is only for use by system components.</em> |
| 1353 | * |
| 1354 | * @hide |
| 1355 | */ |
| 1356 | @TestApi |
| 1357 | public boolean hasScheduleExactAlarm(@NonNull String packageName, int userId) { |
| 1358 | try { |
| 1359 | return mService.hasScheduleExactAlarm(packageName, userId); |
| 1360 | } catch (RemoteException re) { |
| 1361 | throw re.rethrowFromSystemServer(); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * Gets information about the next alarm clock currently scheduled. |
| 1367 | * |
| 1368 | * The alarm clocks considered are those scheduled by any application |
| 1369 | * using the {@link #setAlarmClock} method. |
| 1370 | * |
| 1371 | * @return An {@link AlarmClockInfo} object describing the next upcoming alarm |
| 1372 | * clock event that will occur. If there are no alarm clock events currently |
| 1373 | * scheduled, this method will return {@code null}. |
| 1374 | * |
| 1375 | * @see #setAlarmClock |
| 1376 | * @see AlarmClockInfo |
| 1377 | * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED |
| 1378 | */ |
| 1379 | public AlarmClockInfo getNextAlarmClock() { |
| 1380 | return getNextAlarmClock(mContext.getUserId()); |
| 1381 | } |
| 1382 | |
| 1383 | /** |
| 1384 | * Gets information about the next alarm clock currently scheduled. |
| 1385 | * |
| 1386 | * The alarm clocks considered are those scheduled by any application |
| 1387 | * using the {@link #setAlarmClock} method within the given user. |
| 1388 | * |
| 1389 | * @return An {@link AlarmClockInfo} object describing the next upcoming alarm |
| 1390 | * clock event that will occur within the given user. If there are no alarm clock |
| 1391 | * events currently scheduled in that user, this method will return {@code null}. |
| 1392 | * |
| 1393 | * @see #setAlarmClock |
| 1394 | * @see AlarmClockInfo |
| 1395 | * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED |
| 1396 | * |
| 1397 | * @hide |
| 1398 | */ |
| 1399 | public AlarmClockInfo getNextAlarmClock(int userId) { |
| 1400 | try { |
| 1401 | return mService.getNextAlarmClock(userId); |
| 1402 | } catch (RemoteException ex) { |
| 1403 | throw ex.rethrowFromSystemServer(); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * An immutable description of a scheduled "alarm clock" event. |
| 1409 | * |
| 1410 | * @see AlarmManager#setAlarmClock |
| 1411 | * @see AlarmManager#getNextAlarmClock |
| 1412 | */ |
| 1413 | public static final class AlarmClockInfo implements Parcelable { |
| 1414 | |
| 1415 | private final long mTriggerTime; |
| 1416 | private final PendingIntent mShowIntent; |
| 1417 | |
| 1418 | /** |
| 1419 | * Creates a new alarm clock description. |
| 1420 | * |
| 1421 | * @param triggerTime time at which the underlying alarm is triggered in wall time |
| 1422 | * milliseconds since the epoch |
| 1423 | * @param showIntent an intent that can be used to show or edit details of |
| 1424 | * the alarm clock. |
| 1425 | */ |
| 1426 | public AlarmClockInfo(long triggerTime, PendingIntent showIntent) { |
| 1427 | mTriggerTime = triggerTime; |
| 1428 | mShowIntent = showIntent; |
| 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * Use the {@link #CREATOR} |
| 1433 | * @hide |
| 1434 | */ |
| 1435 | @SuppressWarnings("UnsafeParcelApi") |
| 1436 | AlarmClockInfo(Parcel in) { |
| 1437 | mTriggerTime = in.readLong(); |
| 1438 | mShowIntent = in.readParcelable(PendingIntent.class.getClassLoader()); |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * Returns the time at which the alarm is going to trigger. |
| 1443 | * |
| 1444 | * This value is UTC wall clock time in milliseconds, as returned by |
| 1445 | * {@link System#currentTimeMillis()} for example. |
| 1446 | */ |
| 1447 | public long getTriggerTime() { |
| 1448 | return mTriggerTime; |
| 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * Returns an intent that can be used to show or edit details of the alarm clock in |
| 1453 | * the application that scheduled it. |
| 1454 | * |
| 1455 | * <p class="note">Beware that any application can retrieve and send this intent, |
| 1456 | * potentially with additional fields filled in. See |
| 1457 | * {@link PendingIntent#send(android.content.Context, int, android.content.Intent) |
| 1458 | * PendingIntent.send()} and {@link android.content.Intent#fillIn Intent.fillIn()} |
| 1459 | * for details. |
| 1460 | */ |
| 1461 | public PendingIntent getShowIntent() { |
| 1462 | return mShowIntent; |
| 1463 | } |
| 1464 | |
| 1465 | @Override |
| 1466 | public int describeContents() { |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | @Override |
| 1471 | public void writeToParcel(Parcel dest, int flags) { |
| 1472 | dest.writeLong(mTriggerTime); |
| 1473 | dest.writeParcelable(mShowIntent, flags); |
| 1474 | } |
| 1475 | |
| 1476 | public static final @android.annotation.NonNull Creator<AlarmClockInfo> CREATOR = new Creator<AlarmClockInfo>() { |
| 1477 | @Override |
| 1478 | public AlarmClockInfo createFromParcel(Parcel in) { |
| 1479 | return new AlarmClockInfo(in); |
| 1480 | } |
| 1481 | |
| 1482 | @Override |
| 1483 | public AlarmClockInfo[] newArray(int size) { |
| 1484 | return new AlarmClockInfo[size]; |
| 1485 | } |
| 1486 | }; |
| 1487 | |
| 1488 | /** @hide */ |
| 1489 | public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 1490 | final long token = proto.start(fieldId); |
| 1491 | proto.write(AlarmClockInfoProto.TRIGGER_TIME_MS, mTriggerTime); |
| 1492 | if (mShowIntent != null) { |
| 1493 | mShowIntent.dumpDebug(proto, AlarmClockInfoProto.SHOW_INTENT); |
| 1494 | } |
| 1495 | proto.end(token); |
| 1496 | } |
| 1497 | } |
| 1498 | } |