The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 com.android.server; |
| 18 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 20 | import android.app.ActivityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.app.ActivityManagerNative; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 22 | import android.app.AlarmManager; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 23 | import android.app.AppOpsManager; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 24 | import android.app.BroadcastOptions; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 25 | import android.app.IAlarmCompleteListener; |
| 26 | import android.app.IAlarmListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.app.IAlarmManager; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 28 | import android.app.IUidObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.app.PendingIntent; |
| 30 | import android.content.BroadcastReceiver; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 31 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.Context; |
| 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
| 35 | import android.content.pm.PackageManager; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 36 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.net.Uri; |
| 38 | import android.os.Binder; |
| 39 | import android.os.Bundle; |
| 40 | import android.os.Handler; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 41 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.os.Message; |
| 43 | import android.os.PowerManager; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 44 | import android.os.Process; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 45 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.SystemClock; |
| 47 | import android.os.SystemProperties; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 48 | import android.os.UserHandle; |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 49 | import android.os.WorkSource; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 50 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.text.TextUtils; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 52 | import android.text.format.DateFormat; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 53 | import android.util.ArrayMap; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 54 | import android.util.KeyValueListParser; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 55 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 56 | import android.util.Slog; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 57 | import android.util.SparseArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 58 | import android.util.SparseBooleanArray; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 59 | import android.util.SparseLongArray; |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 60 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 62 | import java.io.ByteArrayOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import java.io.FileDescriptor; |
| 64 | import java.io.PrintWriter; |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 65 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import java.util.ArrayList; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 67 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import java.util.Calendar; |
| 69 | import java.util.Collections; |
| 70 | import java.util.Comparator; |
Mike Lockwood | 1f7b413 | 2009-11-20 15:12:51 -0500 | [diff] [blame] | 71 | import java.util.Date; |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 72 | import java.util.HashMap; |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 73 | import java.util.LinkedList; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 74 | import java.util.Locale; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 75 | import java.util.Random; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import java.util.TimeZone; |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 77 | import java.util.TreeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 79 | import static android.app.AlarmManager.RTC_WAKEUP; |
| 80 | import static android.app.AlarmManager.RTC; |
| 81 | import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP; |
| 82 | import static android.app.AlarmManager.ELAPSED_REALTIME; |
| 83 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 84 | import com.android.internal.util.LocalLog; |
| 85 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 86 | class AlarmManagerService extends SystemService { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 87 | private static final int RTC_WAKEUP_MASK = 1 << RTC_WAKEUP; |
| 88 | private static final int RTC_MASK = 1 << RTC; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 89 | private static final int ELAPSED_REALTIME_WAKEUP_MASK = 1 << ELAPSED_REALTIME_WAKEUP; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 90 | private static final int ELAPSED_REALTIME_MASK = 1 << ELAPSED_REALTIME; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 91 | static final int TIME_CHANGED_MASK = 1 << 16; |
| 92 | static final int IS_WAKEUP_MASK = RTC_WAKEUP_MASK|ELAPSED_REALTIME_WAKEUP_MASK; |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 93 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 94 | // Mask for testing whether a given alarm type is wakeup vs non-wakeup |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 95 | static final int TYPE_NONWAKEUP_MASK = 0x1; // low bit => non-wakeup |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 96 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 97 | static final String TAG = "AlarmManager"; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 98 | static final boolean localLOGV = false; |
| 99 | static final boolean DEBUG_BATCH = localLOGV || false; |
| 100 | static final boolean DEBUG_VALIDATE = localLOGV || false; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 101 | static final boolean DEBUG_ALARM_CLOCK = localLOGV || false; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 102 | static final boolean DEBUG_LISTENER_CALLBACK = localLOGV || false; |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 103 | static final boolean RECORD_ALARMS_IN_HISTORY = true; |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 104 | static final boolean RECORD_DEVICE_IDLE_ALARMS = false; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 105 | static final int ALARM_EVENT = 1; |
| 106 | static final String TIMEZONE_PROPERTY = "persist.sys.timezone"; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 107 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 108 | private final Intent mBackgroundIntent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | = new Intent().addFlags(Intent.FLAG_FROM_BACKGROUND); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 110 | static final IncreasingTimeOrder sIncreasingTimeOrder = new IncreasingTimeOrder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 112 | static final boolean WAKEUP_STATS = false; |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 113 | |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 114 | private static final Intent NEXT_ALARM_CLOCK_CHANGED_INTENT = new Intent( |
| 115 | AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED); |
| 116 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 117 | final LocalLog mLog = new LocalLog(TAG); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 118 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 119 | AppOpsManager mAppOps; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 120 | DeviceIdleController.LocalService mLocalDeviceIdleController; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 122 | final Object mLock = new Object(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 123 | |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 124 | long mNativeData; |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 125 | private long mNextWakeup; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 126 | private long mNextNonWakeup; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 127 | int mBroadcastRefCount = 0; |
| 128 | PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 129 | boolean mLastWakeLockUnimportantForLogging; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 130 | ArrayList<Alarm> mPendingNonWakeupAlarms = new ArrayList<>(); |
| 131 | ArrayList<InFlight> mInFlight = new ArrayList<>(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 132 | final AlarmHandler mHandler = new AlarmHandler(); |
| 133 | ClockReceiver mClockReceiver; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 134 | InteractiveStateReceiver mInteractiveStateReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | private UninstallReceiver mUninstallReceiver; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 136 | final DeliveryTracker mDeliveryTracker = new DeliveryTracker(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 137 | PendingIntent mTimeTickSender; |
| 138 | PendingIntent mDateChangeSender; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 139 | Random mRandom; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 140 | boolean mInteractive = true; |
| 141 | long mNonInteractiveStartTime; |
| 142 | long mNonInteractiveTime; |
| 143 | long mLastAlarmDeliveryTime; |
| 144 | long mStartCurrentDelayTime; |
| 145 | long mNextNonWakeupDeliveryTime; |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 146 | long mLastTimeChangeClockTime; |
| 147 | long mLastTimeChangeRealtime; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 148 | long mAllowWhileIdleMinTime; |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 149 | int mNumTimeChanged; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 150 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 151 | /** |
| 152 | * For each uid, this is the last time we dispatched an "allow while idle" alarm, |
| 153 | * used to determine the earliest we can dispatch the next such alarm. |
| 154 | */ |
| 155 | final SparseLongArray mLastAllowWhileIdleDispatch = new SparseLongArray(); |
| 156 | |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 157 | final static class IdleDispatchEntry { |
| 158 | int uid; |
| 159 | String pkg; |
| 160 | String tag; |
| 161 | String op; |
| 162 | long elapsedRealtime; |
| 163 | long argRealtime; |
| 164 | } |
| 165 | final ArrayList<IdleDispatchEntry> mAllowWhileIdleDispatches = new ArrayList(); |
| 166 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Broadcast options to use for FLAG_ALLOW_WHILE_IDLE. |
| 169 | */ |
| 170 | Bundle mIdleOptions; |
| 171 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 172 | private final SparseArray<AlarmManager.AlarmClockInfo> mNextAlarmClockForUser = |
| 173 | new SparseArray<>(); |
| 174 | private final SparseArray<AlarmManager.AlarmClockInfo> mTmpSparseAlarmClockArray = |
| 175 | new SparseArray<>(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 176 | private final SparseBooleanArray mPendingSendNextAlarmClockChangedForUser = |
| 177 | new SparseBooleanArray(); |
| 178 | private boolean mNextAlarmClockMayChange; |
| 179 | |
| 180 | // May only use on mHandler's thread, locking not required. |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 181 | private final SparseArray<AlarmManager.AlarmClockInfo> mHandlerSparseAlarmClockArray = |
| 182 | new SparseArray<>(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 183 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 184 | /** |
| 185 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 186 | * global Settings. Any access to this class or its fields should be done while |
| 187 | * holding the AlarmManagerService.mLock lock. |
| 188 | */ |
| 189 | private final class Constants extends ContentObserver { |
| 190 | // Key names stored in the settings value. |
| 191 | private static final String KEY_MIN_FUTURITY = "min_futurity"; |
| 192 | private static final String KEY_MIN_INTERVAL = "min_interval"; |
| 193 | private static final String KEY_ALLOW_WHILE_IDLE_SHORT_TIME = "allow_while_idle_short_time"; |
| 194 | private static final String KEY_ALLOW_WHILE_IDLE_LONG_TIME = "allow_while_idle_long_time"; |
| 195 | private static final String KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION |
| 196 | = "allow_while_idle_whitelist_duration"; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 197 | private static final String KEY_LISTENER_TIMEOUT = "listener_timeout"; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 198 | |
| 199 | private static final long DEFAULT_MIN_FUTURITY = 5 * 1000; |
| 200 | private static final long DEFAULT_MIN_INTERVAL = 60 * 1000; |
Christopher Tate | aa244da | 2015-08-27 15:57:57 -0700 | [diff] [blame] | 201 | private static final long DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME = DEFAULT_MIN_FUTURITY; |
Dianne Hackborn | 7682b26 | 2015-10-26 17:51:57 -0700 | [diff] [blame] | 202 | private static final long DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME = 9*60*1000; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 203 | private static final long DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION = 10*1000; |
| 204 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 205 | private static final long DEFAULT_LISTENER_TIMEOUT = 5 * 1000; |
| 206 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 207 | // Minimum futurity of a new alarm |
| 208 | public long MIN_FUTURITY = DEFAULT_MIN_FUTURITY; |
| 209 | |
| 210 | // Minimum alarm recurrence interval |
| 211 | public long MIN_INTERVAL = DEFAULT_MIN_INTERVAL; |
| 212 | |
| 213 | // Minimum time between ALLOW_WHILE_IDLE alarms when system is not idle. |
| 214 | public long ALLOW_WHILE_IDLE_SHORT_TIME = DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME; |
| 215 | |
| 216 | // Minimum time between ALLOW_WHILE_IDLE alarms when system is idling. |
| 217 | public long ALLOW_WHILE_IDLE_LONG_TIME = DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME; |
| 218 | |
| 219 | // BroadcastOptions.setTemporaryAppWhitelistDuration() to use for FLAG_ALLOW_WHILE_IDLE. |
| 220 | public long ALLOW_WHILE_IDLE_WHITELIST_DURATION |
| 221 | = DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION; |
| 222 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 223 | // Direct alarm listener callback timeout |
| 224 | public long LISTENER_TIMEOUT = DEFAULT_LISTENER_TIMEOUT; |
| 225 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 226 | private ContentResolver mResolver; |
| 227 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 228 | private long mLastAllowWhileIdleWhitelistDuration = -1; |
| 229 | |
| 230 | public Constants(Handler handler) { |
| 231 | super(handler); |
| 232 | updateAllowWhileIdleMinTimeLocked(); |
| 233 | updateAllowWhileIdleWhitelistDurationLocked(); |
| 234 | } |
| 235 | |
| 236 | public void start(ContentResolver resolver) { |
| 237 | mResolver = resolver; |
| 238 | mResolver.registerContentObserver(Settings.Global.getUriFor( |
| 239 | Settings.Global.ALARM_MANAGER_CONSTANTS), false, this); |
| 240 | updateConstants(); |
| 241 | } |
| 242 | |
| 243 | public void updateAllowWhileIdleMinTimeLocked() { |
| 244 | mAllowWhileIdleMinTime = mPendingIdleUntil != null |
| 245 | ? ALLOW_WHILE_IDLE_LONG_TIME : ALLOW_WHILE_IDLE_SHORT_TIME; |
| 246 | } |
| 247 | |
| 248 | public void updateAllowWhileIdleWhitelistDurationLocked() { |
| 249 | if (mLastAllowWhileIdleWhitelistDuration != ALLOW_WHILE_IDLE_WHITELIST_DURATION) { |
| 250 | mLastAllowWhileIdleWhitelistDuration = ALLOW_WHILE_IDLE_WHITELIST_DURATION; |
| 251 | BroadcastOptions opts = BroadcastOptions.makeBasic(); |
| 252 | opts.setTemporaryAppWhitelistDuration(ALLOW_WHILE_IDLE_WHITELIST_DURATION); |
| 253 | mIdleOptions = opts.toBundle(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void onChange(boolean selfChange, Uri uri) { |
| 259 | updateConstants(); |
| 260 | } |
| 261 | |
| 262 | private void updateConstants() { |
| 263 | synchronized (mLock) { |
| 264 | try { |
| 265 | mParser.setString(Settings.Global.getString(mResolver, |
| 266 | Settings.Global.ALARM_MANAGER_CONSTANTS)); |
| 267 | } catch (IllegalArgumentException e) { |
| 268 | // Failed to parse the settings string, log this and move on |
| 269 | // with defaults. |
| 270 | Slog.e(TAG, "Bad device idle settings", e); |
| 271 | } |
| 272 | |
| 273 | MIN_FUTURITY = mParser.getLong(KEY_MIN_FUTURITY, DEFAULT_MIN_FUTURITY); |
| 274 | MIN_INTERVAL = mParser.getLong(KEY_MIN_INTERVAL, DEFAULT_MIN_INTERVAL); |
| 275 | ALLOW_WHILE_IDLE_SHORT_TIME = mParser.getLong(KEY_ALLOW_WHILE_IDLE_SHORT_TIME, |
| 276 | DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME); |
| 277 | ALLOW_WHILE_IDLE_LONG_TIME = mParser.getLong(KEY_ALLOW_WHILE_IDLE_LONG_TIME, |
| 278 | DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME); |
| 279 | ALLOW_WHILE_IDLE_WHITELIST_DURATION = mParser.getLong( |
| 280 | KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION, |
| 281 | DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 282 | LISTENER_TIMEOUT = mParser.getLong(KEY_LISTENER_TIMEOUT, |
| 283 | DEFAULT_LISTENER_TIMEOUT); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 284 | |
| 285 | updateAllowWhileIdleMinTimeLocked(); |
| 286 | updateAllowWhileIdleWhitelistDurationLocked(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void dump(PrintWriter pw) { |
| 291 | pw.println(" Settings:"); |
| 292 | |
| 293 | pw.print(" "); pw.print(KEY_MIN_FUTURITY); pw.print("="); |
| 294 | TimeUtils.formatDuration(MIN_FUTURITY, pw); |
| 295 | pw.println(); |
| 296 | |
| 297 | pw.print(" "); pw.print(KEY_MIN_INTERVAL); pw.print("="); |
| 298 | TimeUtils.formatDuration(MIN_INTERVAL, pw); |
| 299 | pw.println(); |
| 300 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 301 | pw.print(" "); pw.print(KEY_LISTENER_TIMEOUT); pw.print("="); |
| 302 | TimeUtils.formatDuration(LISTENER_TIMEOUT, pw); |
| 303 | pw.println(); |
| 304 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 305 | pw.print(" "); pw.print(KEY_ALLOW_WHILE_IDLE_SHORT_TIME); pw.print("="); |
| 306 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_SHORT_TIME, pw); |
| 307 | pw.println(); |
| 308 | |
| 309 | pw.print(" "); pw.print(KEY_ALLOW_WHILE_IDLE_LONG_TIME); pw.print("="); |
| 310 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_LONG_TIME, pw); |
| 311 | pw.println(); |
| 312 | |
| 313 | pw.print(" "); pw.print(KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION); pw.print("="); |
| 314 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_WHITELIST_DURATION, pw); |
| 315 | pw.println(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | final Constants mConstants; |
| 320 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 321 | // Alarm delivery ordering bookkeeping |
| 322 | static final int PRIO_TICK = 0; |
| 323 | static final int PRIO_WAKEUP = 1; |
| 324 | static final int PRIO_NORMAL = 2; |
| 325 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 326 | final class PriorityClass { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 327 | int seq; |
| 328 | int priority; |
| 329 | |
| 330 | PriorityClass() { |
| 331 | seq = mCurrentSeq - 1; |
| 332 | priority = PRIO_NORMAL; |
| 333 | } |
| 334 | } |
| 335 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 336 | final HashMap<String, PriorityClass> mPriorities = new HashMap<>(); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 337 | int mCurrentSeq = 0; |
| 338 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 339 | static final class WakeupEvent { |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 340 | public long when; |
| 341 | public int uid; |
| 342 | public String action; |
| 343 | |
| 344 | public WakeupEvent(long theTime, int theUid, String theAction) { |
| 345 | when = theTime; |
| 346 | uid = theUid; |
| 347 | action = theAction; |
| 348 | } |
| 349 | } |
| 350 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 351 | final LinkedList<WakeupEvent> mRecentWakeups = new LinkedList<WakeupEvent>(); |
| 352 | final long RECENT_WAKEUP_PERIOD = 1000L * 60 * 60 * 24; // one day |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 353 | |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 354 | final class Batch { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 355 | long start; // These endpoints are always in ELAPSED |
| 356 | long end; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 357 | int flags; // Flags for alarms, such as FLAG_STANDALONE. |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 358 | |
| 359 | final ArrayList<Alarm> alarms = new ArrayList<Alarm>(); |
| 360 | |
| 361 | Batch() { |
| 362 | start = 0; |
| 363 | end = Long.MAX_VALUE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 364 | flags = 0; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | Batch(Alarm seed) { |
| 368 | start = seed.whenElapsed; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 369 | end = seed.maxWhenElapsed; |
| 370 | flags = seed.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 371 | alarms.add(seed); |
| 372 | } |
| 373 | |
| 374 | int size() { |
| 375 | return alarms.size(); |
| 376 | } |
| 377 | |
| 378 | Alarm get(int index) { |
| 379 | return alarms.get(index); |
| 380 | } |
| 381 | |
| 382 | boolean canHold(long whenElapsed, long maxWhen) { |
| 383 | return (end >= whenElapsed) && (start <= maxWhen); |
| 384 | } |
| 385 | |
| 386 | boolean add(Alarm alarm) { |
| 387 | boolean newStart = false; |
| 388 | // narrows the batch if necessary; presumes that canHold(alarm) is true |
| 389 | int index = Collections.binarySearch(alarms, alarm, sIncreasingTimeOrder); |
| 390 | if (index < 0) { |
| 391 | index = 0 - index - 1; |
| 392 | } |
| 393 | alarms.add(index, alarm); |
| 394 | if (DEBUG_BATCH) { |
| 395 | Slog.v(TAG, "Adding " + alarm + " to " + this); |
| 396 | } |
| 397 | if (alarm.whenElapsed > start) { |
| 398 | start = alarm.whenElapsed; |
| 399 | newStart = true; |
| 400 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 401 | if (alarm.maxWhenElapsed < end) { |
| 402 | end = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 403 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 404 | flags |= alarm.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 405 | |
| 406 | if (DEBUG_BATCH) { |
| 407 | Slog.v(TAG, " => now " + this); |
| 408 | } |
| 409 | return newStart; |
| 410 | } |
| 411 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 412 | boolean remove(final PendingIntent operation, final IAlarmListener listener) { |
| 413 | if (operation == null && listener == null) { |
| 414 | if (localLOGV) { |
| 415 | Slog.w(TAG, "requested remove() of null operation", |
| 416 | new RuntimeException("here")); |
| 417 | } |
| 418 | return false; |
| 419 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 420 | boolean didRemove = false; |
| 421 | long newStart = 0; // recalculate endpoints as we go |
| 422 | long newEnd = Long.MAX_VALUE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 423 | int newFlags = 0; |
Christopher Tate | ae269d5 | 2013-09-26 13:11:55 -0700 | [diff] [blame] | 424 | for (int i = 0; i < alarms.size(); ) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 425 | Alarm alarm = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 426 | if (alarm.matches(operation, listener)) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 427 | alarms.remove(i); |
| 428 | didRemove = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 429 | if (alarm.alarmClock != null) { |
| 430 | mNextAlarmClockMayChange = true; |
| 431 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 432 | } else { |
| 433 | if (alarm.whenElapsed > newStart) { |
| 434 | newStart = alarm.whenElapsed; |
| 435 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 436 | if (alarm.maxWhenElapsed < newEnd) { |
| 437 | newEnd = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 438 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 439 | newFlags |= alarm.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 440 | i++; |
| 441 | } |
| 442 | } |
| 443 | if (didRemove) { |
| 444 | // commit the new batch bounds |
| 445 | start = newStart; |
| 446 | end = newEnd; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 447 | flags = newFlags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 448 | } |
| 449 | return didRemove; |
| 450 | } |
| 451 | |
| 452 | boolean remove(final String packageName) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 453 | if (packageName == null) { |
| 454 | if (localLOGV) { |
| 455 | Slog.w(TAG, "requested remove() of null packageName", |
| 456 | new RuntimeException("here")); |
| 457 | } |
| 458 | return false; |
| 459 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 460 | boolean didRemove = false; |
| 461 | long newStart = 0; // recalculate endpoints as we go |
| 462 | long newEnd = Long.MAX_VALUE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 463 | int newFlags = 0; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 464 | for (int i = alarms.size()-1; i >= 0; i--) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 465 | Alarm alarm = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 466 | if (alarm.matches(packageName)) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 467 | alarms.remove(i); |
| 468 | didRemove = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 469 | if (alarm.alarmClock != null) { |
| 470 | mNextAlarmClockMayChange = true; |
| 471 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 472 | } else { |
| 473 | if (alarm.whenElapsed > newStart) { |
| 474 | newStart = alarm.whenElapsed; |
| 475 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 476 | if (alarm.maxWhenElapsed < newEnd) { |
| 477 | newEnd = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 478 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 479 | newFlags |= alarm.flags; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | if (didRemove) { |
| 483 | // commit the new batch bounds |
| 484 | start = newStart; |
| 485 | end = newEnd; |
| 486 | flags = newFlags; |
| 487 | } |
| 488 | return didRemove; |
| 489 | } |
| 490 | |
| 491 | boolean removeForStopped(final int uid) { |
| 492 | boolean didRemove = false; |
| 493 | long newStart = 0; // recalculate endpoints as we go |
| 494 | long newEnd = Long.MAX_VALUE; |
| 495 | int newFlags = 0; |
| 496 | for (int i = alarms.size()-1; i >= 0; i--) { |
| 497 | Alarm alarm = alarms.get(i); |
| 498 | try { |
| 499 | if (alarm.uid == uid && ActivityManagerNative.getDefault().getAppStartMode( |
| 500 | uid, alarm.packageName) == ActivityManager.APP_START_MODE_DISABLED) { |
| 501 | alarms.remove(i); |
| 502 | didRemove = true; |
| 503 | if (alarm.alarmClock != null) { |
| 504 | mNextAlarmClockMayChange = true; |
| 505 | } |
| 506 | } else { |
| 507 | if (alarm.whenElapsed > newStart) { |
| 508 | newStart = alarm.whenElapsed; |
| 509 | } |
| 510 | if (alarm.maxWhenElapsed < newEnd) { |
| 511 | newEnd = alarm.maxWhenElapsed; |
| 512 | } |
| 513 | newFlags |= alarm.flags; |
| 514 | } |
| 515 | } catch (RemoteException e) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | if (didRemove) { |
| 519 | // commit the new batch bounds |
| 520 | start = newStart; |
| 521 | end = newEnd; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 522 | flags = newFlags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 523 | } |
| 524 | return didRemove; |
| 525 | } |
| 526 | |
| 527 | boolean remove(final int userHandle) { |
| 528 | boolean didRemove = false; |
| 529 | long newStart = 0; // recalculate endpoints as we go |
| 530 | long newEnd = Long.MAX_VALUE; |
Christopher Tate | ae269d5 | 2013-09-26 13:11:55 -0700 | [diff] [blame] | 531 | for (int i = 0; i < alarms.size(); ) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 532 | Alarm alarm = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 533 | if (UserHandle.getUserId(alarm.creatorUid) == userHandle) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 534 | alarms.remove(i); |
| 535 | didRemove = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 536 | if (alarm.alarmClock != null) { |
| 537 | mNextAlarmClockMayChange = true; |
| 538 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 539 | } else { |
| 540 | if (alarm.whenElapsed > newStart) { |
| 541 | newStart = alarm.whenElapsed; |
| 542 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 543 | if (alarm.maxWhenElapsed < newEnd) { |
| 544 | newEnd = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 545 | } |
| 546 | i++; |
| 547 | } |
| 548 | } |
| 549 | if (didRemove) { |
| 550 | // commit the new batch bounds |
| 551 | start = newStart; |
| 552 | end = newEnd; |
| 553 | } |
| 554 | return didRemove; |
| 555 | } |
| 556 | |
| 557 | boolean hasPackage(final String packageName) { |
| 558 | final int N = alarms.size(); |
| 559 | for (int i = 0; i < N; i++) { |
| 560 | Alarm a = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 561 | if (a.matches(packageName)) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 562 | return true; |
| 563 | } |
| 564 | } |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | boolean hasWakeups() { |
| 569 | final int N = alarms.size(); |
| 570 | for (int i = 0; i < N; i++) { |
| 571 | Alarm a = alarms.get(i); |
| 572 | // non-wakeup alarms are types 1 and 3, i.e. have the low bit set |
| 573 | if ((a.type & TYPE_NONWAKEUP_MASK) == 0) { |
| 574 | return true; |
| 575 | } |
| 576 | } |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public String toString() { |
| 582 | StringBuilder b = new StringBuilder(40); |
| 583 | b.append("Batch{"); b.append(Integer.toHexString(this.hashCode())); |
| 584 | b.append(" num="); b.append(size()); |
| 585 | b.append(" start="); b.append(start); |
| 586 | b.append(" end="); b.append(end); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 587 | if (flags != 0) { |
| 588 | b.append(" flgs=0x"); |
| 589 | b.append(Integer.toHexString(flags)); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 590 | } |
| 591 | b.append('}'); |
| 592 | return b.toString(); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | static class BatchTimeOrder implements Comparator<Batch> { |
| 597 | public int compare(Batch b1, Batch b2) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 598 | long when1 = b1.start; |
| 599 | long when2 = b2.start; |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 600 | if (when1 > when2) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 601 | return 1; |
| 602 | } |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 603 | if (when1 < when2) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 604 | return -1; |
| 605 | } |
| 606 | return 0; |
| 607 | } |
| 608 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 609 | |
| 610 | final Comparator<Alarm> mAlarmDispatchComparator = new Comparator<Alarm>() { |
| 611 | @Override |
| 612 | public int compare(Alarm lhs, Alarm rhs) { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 613 | // priority class trumps everything. TICK < WAKEUP < NORMAL |
| 614 | if (lhs.priorityClass.priority < rhs.priorityClass.priority) { |
| 615 | return -1; |
| 616 | } else if (lhs.priorityClass.priority > rhs.priorityClass.priority) { |
| 617 | return 1; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 618 | } |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 619 | |
| 620 | // within each class, sort by nominal delivery time |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 621 | if (lhs.whenElapsed < rhs.whenElapsed) { |
| 622 | return -1; |
| 623 | } else if (lhs.whenElapsed > rhs.whenElapsed) { |
| 624 | return 1; |
| 625 | } |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 626 | |
| 627 | // same priority class + same target delivery time |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | }; |
| 631 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 632 | void calculateDeliveryPriorities(ArrayList<Alarm> alarms) { |
| 633 | final int N = alarms.size(); |
| 634 | for (int i = 0; i < N; i++) { |
| 635 | Alarm a = alarms.get(i); |
| 636 | |
| 637 | final int alarmPrio; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 638 | if (a.operation != null |
| 639 | && Intent.ACTION_TIME_TICK.equals(a.operation.getIntent().getAction())) { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 640 | alarmPrio = PRIO_TICK; |
| 641 | } else if (a.wakeup) { |
| 642 | alarmPrio = PRIO_WAKEUP; |
| 643 | } else { |
| 644 | alarmPrio = PRIO_NORMAL; |
| 645 | } |
| 646 | |
| 647 | PriorityClass packagePrio = a.priorityClass; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 648 | String alarmPackage = (a.operation != null) |
| 649 | ? a.operation.getCreatorPackage() |
| 650 | : a.packageName; |
| 651 | if (packagePrio == null) packagePrio = mPriorities.get(alarmPackage); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 652 | if (packagePrio == null) { |
| 653 | packagePrio = a.priorityClass = new PriorityClass(); // lowest prio & stale sequence |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 654 | mPriorities.put(alarmPackage, packagePrio); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 655 | } |
| 656 | a.priorityClass = packagePrio; |
| 657 | |
| 658 | if (packagePrio.seq != mCurrentSeq) { |
| 659 | // first alarm we've seen in the current delivery generation from this package |
| 660 | packagePrio.priority = alarmPrio; |
| 661 | packagePrio.seq = mCurrentSeq; |
| 662 | } else { |
| 663 | // Multiple alarms from this package being delivered in this generation; |
| 664 | // bump the package's delivery class if it's warranted. |
| 665 | // TICK < WAKEUP < NORMAL |
| 666 | if (alarmPrio < packagePrio.priority) { |
| 667 | packagePrio.priority = alarmPrio; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 673 | // minimum recurrence period or alarm futurity for us to be able to fuzz it |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 674 | static final long MIN_FUZZABLE_INTERVAL = 10000; |
| 675 | static final BatchTimeOrder sBatchOrder = new BatchTimeOrder(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 676 | final ArrayList<Batch> mAlarmBatches = new ArrayList<>(); |
| 677 | |
| 678 | // set to null if in idle mode; while in this mode, any alarms we don't want |
| 679 | // to run during this time are placed in mPendingWhileIdleAlarms |
| 680 | Alarm mPendingIdleUntil = null; |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 681 | Alarm mNextWakeFromIdle = null; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 682 | ArrayList<Alarm> mPendingWhileIdleAlarms = new ArrayList<>(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 683 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 684 | public AlarmManagerService(Context context) { |
| 685 | super(context); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 686 | mConstants = new Constants(mHandler); |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 689 | static long convertToElapsed(long when, int type) { |
| 690 | final boolean isRtc = (type == RTC || type == RTC_WAKEUP); |
| 691 | if (isRtc) { |
| 692 | when -= System.currentTimeMillis() - SystemClock.elapsedRealtime(); |
| 693 | } |
| 694 | return when; |
| 695 | } |
| 696 | |
| 697 | // Apply a heuristic to { recurrence interval, futurity of the trigger time } to |
| 698 | // calculate the end of our nominal delivery window for the alarm. |
| 699 | static long maxTriggerTime(long now, long triggerAtTime, long interval) { |
| 700 | // Current heuristic: batchable window is 75% of either the recurrence interval |
| 701 | // [for a periodic alarm] or of the time from now to the desired delivery time, |
| 702 | // with a minimum delay/interval of 10 seconds, under which we will simply not |
| 703 | // defer the alarm. |
| 704 | long futurity = (interval == 0) |
| 705 | ? (triggerAtTime - now) |
| 706 | : interval; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 707 | if (futurity < MIN_FUZZABLE_INTERVAL) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 708 | futurity = 0; |
| 709 | } |
| 710 | return triggerAtTime + (long)(.75 * futurity); |
| 711 | } |
| 712 | |
| 713 | // returns true if the batch was added at the head |
| 714 | static boolean addBatchLocked(ArrayList<Batch> list, Batch newBatch) { |
| 715 | int index = Collections.binarySearch(list, newBatch, sBatchOrder); |
| 716 | if (index < 0) { |
| 717 | index = 0 - index - 1; |
| 718 | } |
| 719 | list.add(index, newBatch); |
| 720 | return (index == 0); |
| 721 | } |
| 722 | |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 723 | // Return the index of the matching batch, or -1 if none found. |
| 724 | int attemptCoalesceLocked(long whenElapsed, long maxWhen) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 725 | final int N = mAlarmBatches.size(); |
| 726 | for (int i = 0; i < N; i++) { |
| 727 | Batch b = mAlarmBatches.get(i); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 728 | if ((b.flags&AlarmManager.FLAG_STANDALONE) == 0 && b.canHold(whenElapsed, maxWhen)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 729 | return i; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 732 | return -1; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | // The RTC clock has moved arbitrarily, so we need to recalculate all the batching |
| 736 | void rebatchAllAlarms() { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 737 | synchronized (mLock) { |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 738 | rebatchAllAlarmsLocked(true); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void rebatchAllAlarmsLocked(boolean doValidate) { |
| 743 | ArrayList<Batch> oldSet = (ArrayList<Batch>) mAlarmBatches.clone(); |
| 744 | mAlarmBatches.clear(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 745 | Alarm oldPendingIdleUntil = mPendingIdleUntil; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 746 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 747 | final int oldBatches = oldSet.size(); |
| 748 | for (int batchNum = 0; batchNum < oldBatches; batchNum++) { |
| 749 | Batch batch = oldSet.get(batchNum); |
| 750 | final int N = batch.size(); |
| 751 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 752 | reAddAlarmLocked(batch.get(i), nowElapsed, doValidate); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 753 | } |
| 754 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 755 | if (oldPendingIdleUntil != null && oldPendingIdleUntil != mPendingIdleUntil) { |
| 756 | Slog.wtf(TAG, "Rebatching: idle until changed from " + oldPendingIdleUntil |
| 757 | + " to " + mPendingIdleUntil); |
| 758 | if (mPendingIdleUntil == null) { |
| 759 | // Somehow we lost this... we need to restore all of the pending alarms. |
| 760 | restorePendingWhileIdleAlarmsLocked(); |
| 761 | } |
| 762 | } |
| 763 | rescheduleKernelAlarmsLocked(); |
| 764 | updateNextAlarmClockLocked(); |
| 765 | } |
| 766 | |
| 767 | void reAddAlarmLocked(Alarm a, long nowElapsed, boolean doValidate) { |
| 768 | a.when = a.origWhen; |
| 769 | long whenElapsed = convertToElapsed(a.when, a.type); |
| 770 | final long maxElapsed; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 771 | if (a.windowLength == AlarmManager.WINDOW_EXACT) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 772 | // Exact |
| 773 | maxElapsed = whenElapsed; |
| 774 | } else { |
| 775 | // Not exact. Preserve any explicit window, otherwise recalculate |
| 776 | // the window based on the alarm's new futurity. Note that this |
| 777 | // reflects a policy of preferring timely to deferred delivery. |
| 778 | maxElapsed = (a.windowLength > 0) |
| 779 | ? (whenElapsed + a.windowLength) |
| 780 | : maxTriggerTime(nowElapsed, whenElapsed, a.repeatInterval); |
| 781 | } |
| 782 | a.whenElapsed = whenElapsed; |
| 783 | a.maxWhenElapsed = maxElapsed; |
| 784 | setImplLocked(a, true, doValidate); |
| 785 | } |
| 786 | |
| 787 | void restorePendingWhileIdleAlarmsLocked() { |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 788 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 789 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 790 | ent.uid = 0; |
| 791 | ent.pkg = "FINISH IDLE"; |
| 792 | ent.elapsedRealtime = SystemClock.elapsedRealtime(); |
| 793 | mAllowWhileIdleDispatches.add(ent); |
| 794 | } |
| 795 | |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 796 | // Bring pending alarms back into the main list. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 797 | if (mPendingWhileIdleAlarms.size() > 0) { |
| 798 | ArrayList<Alarm> alarms = mPendingWhileIdleAlarms; |
| 799 | mPendingWhileIdleAlarms = new ArrayList<>(); |
| 800 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 801 | for (int i=alarms.size() - 1; i >= 0; i--) { |
| 802 | Alarm a = alarms.get(i); |
| 803 | reAddAlarmLocked(a, nowElapsed, false); |
| 804 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 805 | } |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 806 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 807 | // Make sure we are using the correct ALLOW_WHILE_IDLE min time. |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 808 | mConstants.updateAllowWhileIdleMinTimeLocked(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 809 | |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 810 | // Reschedule everything. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 811 | rescheduleKernelAlarmsLocked(); |
| 812 | updateNextAlarmClockLocked(); |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 813 | |
| 814 | // And send a TIME_TICK right now, since it is important to get the UI updated. |
| 815 | try { |
| 816 | mTimeTickSender.send(); |
| 817 | } catch (PendingIntent.CanceledException e) { |
| 818 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 821 | static final class InFlight { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 822 | final PendingIntent mPendingIntent; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 823 | final IBinder mListener; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 824 | final WorkSource mWorkSource; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 825 | final int mUid; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 826 | final String mTag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 827 | final BroadcastStats mBroadcastStats; |
| 828 | final FilterStats mFilterStats; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 829 | final int mAlarmType; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 830 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 831 | InFlight(AlarmManagerService service, PendingIntent pendingIntent, IAlarmListener listener, |
| 832 | WorkSource workSource, int uid, String alarmPkg, int alarmType, String tag, |
| 833 | long nowELAPSED) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 834 | mPendingIntent = pendingIntent; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 835 | mListener = listener != null ? listener.asBinder() : null; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 836 | mWorkSource = workSource; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 837 | mUid = uid; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 838 | mTag = tag; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 839 | mBroadcastStats = (pendingIntent != null) |
| 840 | ? service.getStatsLocked(pendingIntent) |
| 841 | : service.getStatsLocked(uid, alarmPkg); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 842 | FilterStats fs = mBroadcastStats.filterStats.get(mTag); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 843 | if (fs == null) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 844 | fs = new FilterStats(mBroadcastStats, mTag); |
| 845 | mBroadcastStats.filterStats.put(mTag, fs); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 846 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 847 | fs.lastTime = nowELAPSED; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 848 | mFilterStats = fs; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 849 | mAlarmType = alarmType; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 850 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 852 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 853 | static final class FilterStats { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 854 | final BroadcastStats mBroadcastStats; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 855 | final String mTag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 856 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 857 | long lastTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | long aggregateTime; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 859 | int count; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | int numWakeup; |
| 861 | long startTime; |
| 862 | int nesting; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 863 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 864 | FilterStats(BroadcastStats broadcastStats, String tag) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 865 | mBroadcastStats = broadcastStats; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 866 | mTag = tag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 867 | } |
| 868 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 869 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 870 | static final class BroadcastStats { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 871 | final int mUid; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 872 | final String mPackageName; |
| 873 | |
| 874 | long aggregateTime; |
| 875 | int count; |
| 876 | int numWakeup; |
| 877 | long startTime; |
| 878 | int nesting; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 879 | final ArrayMap<String, FilterStats> filterStats = new ArrayMap<String, FilterStats>(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 880 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 881 | BroadcastStats(int uid, String packageName) { |
| 882 | mUid = uid; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 883 | mPackageName = packageName; |
| 884 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 886 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 887 | final SparseArray<ArrayMap<String, BroadcastStats>> mBroadcastStats |
| 888 | = new SparseArray<ArrayMap<String, BroadcastStats>>(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 889 | |
| 890 | int mNumDelayedAlarms = 0; |
| 891 | long mTotalDelayTime = 0; |
| 892 | long mMaxDelayTime = 0; |
| 893 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 894 | @Override |
| 895 | public void onStart() { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 896 | mNativeData = init(); |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 897 | mNextWakeup = mNextNonWakeup = 0; |
Robert CH Chou | 64ba8e4 | 2009-11-04 21:38:49 +0800 | [diff] [blame] | 898 | |
| 899 | // We have to set current TimeZone info to kernel |
| 900 | // because kernel doesn't keep this after reboot |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 901 | setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY)); |
Robert CH Chou | 64ba8e4 | 2009-11-04 21:38:49 +0800 | [diff] [blame] | 902 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 903 | PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 904 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*alarm*"); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 905 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 906 | mTimeTickSender = PendingIntent.getBroadcastAsUser(getContext(), 0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | new Intent(Intent.ACTION_TIME_TICK).addFlags( |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 908 | Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 909 | | Intent.FLAG_RECEIVER_FOREGROUND), 0, |
Dianne Hackborn | db5aca9 | 2012-10-26 13:39:41 -0700 | [diff] [blame] | 910 | UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 911 | Intent intent = new Intent(Intent.ACTION_DATE_CHANGED); |
| 912 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 913 | mDateChangeSender = PendingIntent.getBroadcastAsUser(getContext(), 0, intent, |
Dianne Hackborn | db5aca9 | 2012-10-26 13:39:41 -0700 | [diff] [blame] | 914 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | |
| 916 | // now that we have initied the driver schedule the alarm |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 917 | mClockReceiver = new ClockReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | mClockReceiver.scheduleTimeTickEvent(); |
| 919 | mClockReceiver.scheduleDateChangedEvent(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 920 | mInteractiveStateReceiver = new InteractiveStateReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | mUninstallReceiver = new UninstallReceiver(); |
| 922 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 923 | if (mNativeData != 0) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 924 | AlarmThread waitThread = new AlarmThread(); |
| 925 | waitThread.start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 927 | Slog.w(TAG, "Failed to open alarm driver. Falling back to a handler."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 929 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 930 | try { |
| 931 | ActivityManagerNative.getDefault().registerUidObserver(new UidObserver(), |
| 932 | ActivityManager.UID_OBSERVER_IDLE); |
| 933 | } catch (RemoteException e) { |
| 934 | // ignored; both services live in system_server |
| 935 | } |
| 936 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 937 | publishBinderService(Context.ALARM_SERVICE, mService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 939 | |
| 940 | @Override |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 941 | public void onBootPhase(int phase) { |
| 942 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
| 943 | mConstants.start(getContext().getContentResolver()); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 944 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 945 | mLocalDeviceIdleController |
| 946 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | |
| 950 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | protected void finalize() throws Throwable { |
| 952 | try { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 953 | close(mNativeData); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } finally { |
| 955 | super.finalize(); |
| 956 | } |
| 957 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 958 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 959 | void setTimeZoneImpl(String tz) { |
| 960 | if (TextUtils.isEmpty(tz)) { |
| 961 | return; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 964 | TimeZone zone = TimeZone.getTimeZone(tz); |
| 965 | // Prevent reentrant calls from stepping on each other when writing |
| 966 | // the time zone property |
| 967 | boolean timeZoneWasChanged = false; |
| 968 | synchronized (this) { |
| 969 | String current = SystemProperties.get(TIMEZONE_PROPERTY); |
| 970 | if (current == null || !current.equals(zone.getID())) { |
| 971 | if (localLOGV) { |
| 972 | Slog.v(TAG, "timezone changed: " + current + ", new=" + zone.getID()); |
| 973 | } |
| 974 | timeZoneWasChanged = true; |
| 975 | SystemProperties.set(TIMEZONE_PROPERTY, zone.getID()); |
| 976 | } |
| 977 | |
| 978 | // Update the kernel timezone information |
| 979 | // Kernel tracks time offsets as 'minutes west of GMT' |
| 980 | int gmtOffset = zone.getOffset(System.currentTimeMillis()); |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 981 | setKernelTimezone(mNativeData, -(gmtOffset / 60000)); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | TimeZone.setDefault(null); |
| 985 | |
| 986 | if (timeZoneWasChanged) { |
| 987 | Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED); |
| 988 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 989 | intent.putExtra("time-zone", zone.getID()); |
| 990 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 991 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 993 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 994 | void removeImpl(PendingIntent operation) { |
| 995 | if (operation == null) { |
| 996 | return; |
| 997 | } |
| 998 | synchronized (mLock) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 999 | removeLocked(operation, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | void setImpl(int type, long triggerAtTime, long windowLength, long interval, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1004 | PendingIntent operation, IAlarmListener directReceiver, String listenerTag, |
| 1005 | int flags, WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock, |
| 1006 | int callingUid, String callingPackage) { |
| 1007 | // must be *either* PendingIntent or AlarmReceiver, but not both |
| 1008 | if ((operation == null && directReceiver == null) |
| 1009 | || (operation != null && directReceiver != null)) { |
| 1010 | Slog.w(TAG, "Alarms must either supply a PendingIntent or an AlarmReceiver"); |
| 1011 | // NB: previous releases failed silently here, so we are continuing to do the same |
| 1012 | // rather than throw an IllegalArgumentException. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | return; |
| 1014 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1015 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1016 | // Sanity check the window length. This will catch people mistakenly |
| 1017 | // trying to pass an end-of-window timestamp rather than a duration. |
| 1018 | if (windowLength > AlarmManager.INTERVAL_HALF_DAY) { |
| 1019 | Slog.w(TAG, "Window length " + windowLength |
| 1020 | + "ms suspiciously long; limiting to 1 hour"); |
| 1021 | windowLength = AlarmManager.INTERVAL_HOUR; |
| 1022 | } |
| 1023 | |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1024 | // Sanity check the recurrence interval. This will catch people who supply |
| 1025 | // seconds when the API expects milliseconds. |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1026 | final long minInterval = mConstants.MIN_INTERVAL; |
| 1027 | if (interval > 0 && interval < minInterval) { |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1028 | Slog.w(TAG, "Suspiciously short interval " + interval |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1029 | + " millis; expanding to " + (minInterval/1000) |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1030 | + " seconds"); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1031 | interval = minInterval; |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1034 | if (type < RTC_WAKEUP || type > ELAPSED_REALTIME) { |
| 1035 | throw new IllegalArgumentException("Invalid alarm type " + type); |
| 1036 | } |
| 1037 | |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1038 | if (triggerAtTime < 0) { |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1039 | final long what = Binder.getCallingPid(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1040 | Slog.w(TAG, "Invalid alarm trigger time! " + triggerAtTime + " from uid=" + callingUid |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1041 | + " pid=" + what); |
| 1042 | triggerAtTime = 0; |
| 1043 | } |
| 1044 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1045 | final long nowElapsed = SystemClock.elapsedRealtime(); |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1046 | final long nominalTrigger = convertToElapsed(triggerAtTime, type); |
| 1047 | // Try to prevent spamming by making sure we aren't firing alarms in the immediate future |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1048 | final long minTrigger = nowElapsed + mConstants.MIN_FUTURITY; |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1049 | final long triggerElapsed = (nominalTrigger > minTrigger) ? nominalTrigger : minTrigger; |
| 1050 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1051 | final long maxElapsed; |
| 1052 | if (windowLength == AlarmManager.WINDOW_EXACT) { |
| 1053 | maxElapsed = triggerElapsed; |
| 1054 | } else if (windowLength < 0) { |
| 1055 | maxElapsed = maxTriggerTime(nowElapsed, triggerElapsed, interval); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1056 | // Fix this window in place, so that as time approaches we don't collapse it. |
| 1057 | windowLength = maxElapsed - triggerElapsed; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1058 | } else { |
| 1059 | maxElapsed = triggerElapsed + windowLength; |
| 1060 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1061 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | synchronized (mLock) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1063 | if (DEBUG_BATCH) { |
| 1064 | Slog.v(TAG, "set(" + operation + ") : type=" + type |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1065 | + " triggerAtTime=" + triggerAtTime + " win=" + windowLength |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1066 | + " tElapsed=" + triggerElapsed + " maxElapsed=" + maxElapsed |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1067 | + " interval=" + interval + " flags=0x" + Integer.toHexString(flags)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | } |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1069 | setImplLocked(type, triggerAtTime, triggerElapsed, windowLength, maxElapsed, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1070 | interval, operation, directReceiver, listenerTag, flags, true, workSource, |
| 1071 | alarmClock, callingUid, callingPackage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | } |
| 1073 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1075 | private void setImplLocked(int type, long when, long whenElapsed, long windowLength, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1076 | long maxWhen, long interval, PendingIntent operation, IAlarmListener directReceiver, |
| 1077 | String listenerTag, int flags, boolean doValidate, WorkSource workSource, |
| 1078 | AlarmManager.AlarmClockInfo alarmClock, int callingUid, String callingPackage) { |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1079 | Alarm a = new Alarm(type, when, whenElapsed, windowLength, maxWhen, interval, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1080 | operation, directReceiver, listenerTag, workSource, flags, alarmClock, |
| 1081 | callingUid, callingPackage); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1082 | try { |
| 1083 | if (ActivityManagerNative.getDefault().getAppStartMode(callingUid, callingPackage) |
| 1084 | == ActivityManager.APP_START_MODE_DISABLED) { |
| 1085 | Slog.w(TAG, "Not setting alarm from " + callingUid + ":" + a |
| 1086 | + " -- package not allowed to start"); |
| 1087 | return; |
| 1088 | } |
| 1089 | } catch (RemoteException e) { |
| 1090 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1091 | removeLocked(operation, directReceiver); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1092 | setImplLocked(a, false, doValidate); |
| 1093 | } |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 1094 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1095 | private void setImplLocked(Alarm a, boolean rebatching, boolean doValidate) { |
| 1096 | if ((a.flags&AlarmManager.FLAG_IDLE_UNTIL) != 0) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1097 | // This is a special alarm that will put the system into idle until it goes off. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1098 | // The caller has given the time they want this to happen at, however we need |
| 1099 | // to pull that earlier if there are existing alarms that have requested to |
Dianne Hackborn | f383129 | 2015-10-15 14:51:06 -0700 | [diff] [blame] | 1100 | // bring us out of idle at an earlier time. |
| 1101 | if (mNextWakeFromIdle != null && a.whenElapsed > mNextWakeFromIdle.whenElapsed) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1102 | a.when = a.whenElapsed = a.maxWhenElapsed = mNextWakeFromIdle.whenElapsed; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1103 | } |
| 1104 | // Add fuzz to make the alarm go off some time before the actual desired time. |
| 1105 | final long nowElapsed = SystemClock.elapsedRealtime(); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1106 | final int fuzz = fuzzForDuration(a.whenElapsed-nowElapsed); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1107 | if (fuzz > 0) { |
| 1108 | if (mRandom == null) { |
| 1109 | mRandom = new Random(); |
| 1110 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1111 | final int delta = mRandom.nextInt(fuzz); |
| 1112 | a.whenElapsed -= delta; |
| 1113 | if (false) { |
| 1114 | Slog.d(TAG, "Alarm when: " + a.whenElapsed); |
| 1115 | Slog.d(TAG, "Delta until alarm: " + (a.whenElapsed-nowElapsed)); |
| 1116 | Slog.d(TAG, "Applied fuzz: " + fuzz); |
| 1117 | Slog.d(TAG, "Final delta: " + delta); |
| 1118 | Slog.d(TAG, "Final when: " + a.whenElapsed); |
| 1119 | } |
| 1120 | a.when = a.maxWhenElapsed = a.whenElapsed; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | } else if (mPendingIdleUntil != null) { |
| 1124 | // We currently have an idle until alarm scheduled; if the new alarm has |
| 1125 | // not explicitly stated it wants to run while idle, then put it on hold. |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1126 | if ((a.flags&(AlarmManager.FLAG_ALLOW_WHILE_IDLE |
| 1127 | | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED |
| 1128 | | AlarmManager.FLAG_WAKE_FROM_IDLE)) |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1129 | == 0) { |
| 1130 | mPendingWhileIdleAlarms.add(a); |
| 1131 | return; |
| 1132 | } |
| 1133 | } |
| 1134 | |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1135 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1136 | if ((a.flags & AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0) { |
| 1137 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 1138 | ent.uid = a.uid; |
| 1139 | ent.pkg = a.operation.getCreatorPackage(); |
| 1140 | ent.tag = a.operation.getTag(""); |
| 1141 | ent.op = "SET"; |
| 1142 | ent.elapsedRealtime = SystemClock.elapsedRealtime(); |
| 1143 | ent.argRealtime = a.whenElapsed; |
| 1144 | mAllowWhileIdleDispatches.add(ent); |
| 1145 | } |
| 1146 | } |
| 1147 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1148 | int whichBatch = ((a.flags&AlarmManager.FLAG_STANDALONE) != 0) |
| 1149 | ? -1 : attemptCoalesceLocked(a.whenElapsed, a.maxWhenElapsed); |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1150 | if (whichBatch < 0) { |
| 1151 | Batch batch = new Batch(a); |
Christopher Tate | 7d57ed8 | 2013-10-25 20:18:03 -0700 | [diff] [blame] | 1152 | addBatchLocked(mAlarmBatches, batch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | } else { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1154 | Batch batch = mAlarmBatches.get(whichBatch); |
Christopher Tate | 7d57ed8 | 2013-10-25 20:18:03 -0700 | [diff] [blame] | 1155 | if (batch.add(a)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1156 | // The start time of this batch advanced, so batch ordering may |
| 1157 | // have just been broken. Move it to where it now belongs. |
| 1158 | mAlarmBatches.remove(whichBatch); |
| 1159 | addBatchLocked(mAlarmBatches, batch); |
| 1160 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1163 | if (a.alarmClock != null) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1164 | mNextAlarmClockMayChange = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1167 | boolean needRebatch = false; |
| 1168 | |
| 1169 | if ((a.flags&AlarmManager.FLAG_IDLE_UNTIL) != 0) { |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1170 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1171 | if (mPendingIdleUntil == null) { |
| 1172 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 1173 | ent.uid = 0; |
| 1174 | ent.pkg = "START IDLE"; |
| 1175 | ent.elapsedRealtime = SystemClock.elapsedRealtime(); |
| 1176 | mAllowWhileIdleDispatches.add(ent); |
| 1177 | } |
| 1178 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1179 | mPendingIdleUntil = a; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1180 | mConstants.updateAllowWhileIdleMinTimeLocked(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1181 | needRebatch = true; |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1182 | } else if ((a.flags&AlarmManager.FLAG_WAKE_FROM_IDLE) != 0) { |
| 1183 | if (mNextWakeFromIdle == null || mNextWakeFromIdle.whenElapsed > a.whenElapsed) { |
| 1184 | mNextWakeFromIdle = a; |
| 1185 | // If this wake from idle is earlier than whatever was previously scheduled, |
| 1186 | // and we are currently idling, then we need to rebatch alarms in case the idle |
| 1187 | // until time needs to be updated. |
| 1188 | if (mPendingIdleUntil != null) { |
| 1189 | needRebatch = true; |
| 1190 | } |
| 1191 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | if (!rebatching) { |
| 1195 | if (DEBUG_VALIDATE) { |
| 1196 | if (doValidate && !validateConsistencyLocked()) { |
| 1197 | Slog.v(TAG, "Tipping-point operation: type=" + a.type + " when=" + a.when |
| 1198 | + " when(hex)=" + Long.toHexString(a.when) |
| 1199 | + " whenElapsed=" + a.whenElapsed |
| 1200 | + " maxWhenElapsed=" + a.maxWhenElapsed |
| 1201 | + " interval=" + a.repeatInterval + " op=" + a.operation |
| 1202 | + " flags=0x" + Integer.toHexString(a.flags)); |
| 1203 | rebatchAllAlarmsLocked(false); |
| 1204 | needRebatch = false; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | if (needRebatch) { |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1209 | rebatchAllAlarmsLocked(false); |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1210 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1211 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1212 | rescheduleKernelAlarmsLocked(); |
| 1213 | updateNextAlarmClockLocked(); |
| 1214 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1217 | private final IBinder mService = new IAlarmManager.Stub() { |
| 1218 | @Override |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1219 | public void set(String callingPackage, |
| 1220 | int type, long triggerAtTime, long windowLength, long interval, int flags, |
| 1221 | PendingIntent operation, IAlarmListener directReceiver, String listenerTag, |
| 1222 | WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1223 | final int callingUid = Binder.getCallingUid(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1224 | |
| 1225 | // make sure the caller is not lying about which package should be blamed for |
| 1226 | // wakelock time spent in alarm delivery |
| 1227 | mAppOps.checkPackage(callingUid, callingPackage); |
| 1228 | |
| 1229 | // Repeating alarms must use PendingIntent, not direct listener |
| 1230 | if (interval != 0) { |
| 1231 | if (directReceiver != null) { |
| 1232 | throw new IllegalArgumentException("Repeating alarms cannot use AlarmReceivers"); |
| 1233 | } |
| 1234 | } |
| 1235 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1236 | if (workSource != null) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1237 | getContext().enforcePermission( |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1238 | android.Manifest.permission.UPDATE_DEVICE_STATS, |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1239 | Binder.getCallingPid(), callingUid, "AlarmManager.set"); |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1242 | // No incoming callers can request either WAKE_FROM_IDLE or |
| 1243 | // ALLOW_WHILE_IDLE_UNRESTRICTED -- we will apply those later as appropriate. |
| 1244 | flags &= ~(AlarmManager.FLAG_WAKE_FROM_IDLE |
| 1245 | | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED); |
| 1246 | |
| 1247 | // Only the system can use FLAG_IDLE_UNTIL -- this is used to tell the alarm |
| 1248 | // manager when to come out of idle mode, which is only for DeviceIdleController. |
| 1249 | if (callingUid != Process.SYSTEM_UID) { |
| 1250 | flags &= ~AlarmManager.FLAG_IDLE_UNTIL; |
| 1251 | } |
| 1252 | |
| 1253 | // If the caller is a core system component, and not calling to do work on behalf |
| 1254 | // of someone else, then always set ALLOW_WHILE_IDLE_UNRESTRICTED. This means we |
| 1255 | // will allow these alarms to go off as normal even while idle, with no timing |
| 1256 | // restrictions. |
| 1257 | if (callingUid < Process.FIRST_APPLICATION_UID && workSource == null) { |
| 1258 | flags |= AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED; |
| 1259 | } |
| 1260 | |
| 1261 | // If this is an exact time alarm, then it can't be batched with other alarms. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1262 | if (windowLength == AlarmManager.WINDOW_EXACT) { |
| 1263 | flags |= AlarmManager.FLAG_STANDALONE; |
| 1264 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1265 | |
| 1266 | // If this alarm is for an alarm clock, then it must be standalone and we will |
| 1267 | // use it to wake early from idle if needed. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1268 | if (alarmClock != null) { |
| 1269 | flags |= AlarmManager.FLAG_WAKE_FROM_IDLE | AlarmManager.FLAG_STANDALONE; |
| 1270 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1271 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1272 | setImpl(type, triggerAtTime, windowLength, interval, operation, directReceiver, |
| 1273 | listenerTag, flags, workSource, alarmClock, callingUid, callingPackage); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1274 | } |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 1275 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1276 | @Override |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 1277 | public boolean setTime(long millis) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1278 | getContext().enforceCallingOrSelfPermission( |
| 1279 | "android.permission.SET_TIME", |
| 1280 | "setTime"); |
| 1281 | |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 1282 | if (mNativeData == 0) { |
| 1283 | Slog.w(TAG, "Not setting time since no alarm driver is available."); |
| 1284 | return false; |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 1285 | } |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 1286 | |
| 1287 | synchronized (mLock) { |
| 1288 | return setKernelTime(mNativeData, millis) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1291 | |
| 1292 | @Override |
| 1293 | public void setTimeZone(String tz) { |
| 1294 | getContext().enforceCallingOrSelfPermission( |
| 1295 | "android.permission.SET_TIME_ZONE", |
| 1296 | "setTimeZone"); |
| 1297 | |
| 1298 | final long oldId = Binder.clearCallingIdentity(); |
| 1299 | try { |
| 1300 | setTimeZoneImpl(tz); |
| 1301 | } finally { |
| 1302 | Binder.restoreCallingIdentity(oldId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | } |
| 1304 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1305 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1306 | @Override |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1307 | public void remove(PendingIntent operation, IAlarmListener listener) { |
| 1308 | if (operation == null && listener == null) { |
| 1309 | Slog.w(TAG, "remove() with no intent or listener"); |
| 1310 | return; |
| 1311 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1312 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1313 | synchronized (mLock) { |
| 1314 | removeLocked(operation, listener); |
| 1315 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1316 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1317 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1318 | @Override |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1319 | public long getNextWakeFromIdleTime() { |
| 1320 | return getNextWakeFromIdleTimeImpl(); |
| 1321 | } |
| 1322 | |
| 1323 | @Override |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1324 | public AlarmManager.AlarmClockInfo getNextAlarmClock(int userId) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1325 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1326 | Binder.getCallingUid(), userId, false /* allowAll */, false /* requireFull */, |
| 1327 | "getNextAlarmClock", null); |
| 1328 | |
| 1329 | return getNextAlarmClockImpl(userId); |
| 1330 | } |
| 1331 | |
| 1332 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1333 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1334 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1335 | != PackageManager.PERMISSION_GRANTED) { |
| 1336 | pw.println("Permission Denial: can't dump AlarmManager from from pid=" |
| 1337 | + Binder.getCallingPid() |
| 1338 | + ", uid=" + Binder.getCallingUid()); |
| 1339 | return; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1340 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1341 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1342 | dumpImpl(pw); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1343 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1344 | }; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1345 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1346 | void dumpImpl(PrintWriter pw) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1347 | synchronized (mLock) { |
| 1348 | pw.println("Current Alarm Manager state:"); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1349 | mConstants.dump(pw); |
| 1350 | pw.println(); |
| 1351 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1352 | final long nowRTC = System.currentTimeMillis(); |
| 1353 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 1354 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 1355 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1356 | pw.print(" nowRTC="); pw.print(nowRTC); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1357 | pw.print("="); pw.print(sdf.format(new Date(nowRTC))); |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 1358 | pw.print(" nowELAPSED="); pw.print(nowELAPSED); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1359 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1360 | pw.print(" mLastTimeChangeClockTime="); pw.print(mLastTimeChangeClockTime); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 1361 | pw.print("="); pw.println(sdf.format(new Date(mLastTimeChangeClockTime))); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1362 | pw.print(" mLastTimeChangeRealtime="); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 1363 | TimeUtils.formatDuration(mLastTimeChangeRealtime, pw); |
| 1364 | pw.println(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1365 | if (!mInteractive) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1366 | pw.print(" Time since non-interactive: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1367 | TimeUtils.formatDuration(nowELAPSED - mNonInteractiveStartTime, pw); |
| 1368 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1369 | pw.print(" Max wakeup delay: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1370 | TimeUtils.formatDuration(currentNonWakeupFuzzLocked(nowELAPSED), pw); |
| 1371 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1372 | pw.print(" Time since last dispatch: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1373 | TimeUtils.formatDuration(nowELAPSED - mLastAlarmDeliveryTime, pw); |
| 1374 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1375 | pw.print(" Next non-wakeup delivery time: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1376 | TimeUtils.formatDuration(nowELAPSED - mNextNonWakeupDeliveryTime, pw); |
| 1377 | pw.println(); |
| 1378 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1379 | |
| 1380 | long nextWakeupRTC = mNextWakeup + (nowRTC - nowELAPSED); |
| 1381 | long nextNonWakeupRTC = mNextNonWakeup + (nowRTC - nowELAPSED); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1382 | pw.print(" Next non-wakeup alarm: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1383 | TimeUtils.formatDuration(mNextNonWakeup, nowELAPSED, pw); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1384 | pw.print(" = "); pw.println(sdf.format(new Date(nextNonWakeupRTC))); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1385 | pw.print(" Next wakeup: "); TimeUtils.formatDuration(mNextWakeup, nowELAPSED, pw); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1386 | pw.print(" = "); pw.println(sdf.format(new Date(nextWakeupRTC))); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1387 | pw.print(" Num time change events: "); pw.println(mNumTimeChanged); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1388 | |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 1389 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1390 | pw.println(" Next alarm clock information: "); |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 1391 | final TreeSet<Integer> users = new TreeSet<>(); |
| 1392 | for (int i = 0; i < mNextAlarmClockForUser.size(); i++) { |
| 1393 | users.add(mNextAlarmClockForUser.keyAt(i)); |
| 1394 | } |
| 1395 | for (int i = 0; i < mPendingSendNextAlarmClockChangedForUser.size(); i++) { |
| 1396 | users.add(mPendingSendNextAlarmClockChangedForUser.keyAt(i)); |
| 1397 | } |
| 1398 | for (int user : users) { |
| 1399 | final AlarmManager.AlarmClockInfo next = mNextAlarmClockForUser.get(user); |
| 1400 | final long time = next != null ? next.getTriggerTime() : 0; |
| 1401 | final boolean pendingSend = mPendingSendNextAlarmClockChangedForUser.get(user); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1402 | pw.print(" user:"); pw.print(user); |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 1403 | pw.print(" pendingSend:"); pw.print(pendingSend); |
| 1404 | pw.print(" time:"); pw.print(time); |
| 1405 | if (time > 0) { |
| 1406 | pw.print(" = "); pw.print(sdf.format(new Date(time))); |
| 1407 | pw.print(" = "); TimeUtils.formatDuration(time, nowRTC, pw); |
| 1408 | } |
| 1409 | pw.println(); |
| 1410 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1411 | if (mAlarmBatches.size() > 0) { |
| 1412 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1413 | pw.print(" Pending alarm batches: "); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1414 | pw.println(mAlarmBatches.size()); |
| 1415 | for (Batch b : mAlarmBatches) { |
| 1416 | pw.print(b); pw.println(':'); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1417 | dumpAlarmList(pw, b.alarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1418 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1419 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1420 | if (mPendingIdleUntil != null || mPendingWhileIdleAlarms.size() > 0) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1421 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1422 | pw.println(" Idle mode state:"); |
| 1423 | pw.print(" Idling until: "); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1424 | if (mPendingIdleUntil != null) { |
| 1425 | pw.println(mPendingIdleUntil); |
Dianne Hackborn | f383129 | 2015-10-15 14:51:06 -0700 | [diff] [blame] | 1426 | mPendingIdleUntil.dump(pw, " ", nowRTC, nowELAPSED, sdf); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1427 | } else { |
| 1428 | pw.println("null"); |
| 1429 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1430 | pw.println(" Pending alarms:"); |
| 1431 | dumpAlarmList(pw, mPendingWhileIdleAlarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1432 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1433 | if (mNextWakeFromIdle != null) { |
| 1434 | pw.println(); |
| 1435 | pw.print(" Next wake from idle: "); pw.println(mNextWakeFromIdle); |
| 1436 | mNextWakeFromIdle.dump(pw, " ", nowRTC, nowELAPSED, sdf); |
| 1437 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1438 | |
| 1439 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1440 | pw.print(" Past-due non-wakeup alarms: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1441 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 1442 | pw.println(mPendingNonWakeupAlarms.size()); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1443 | dumpAlarmList(pw, mPendingNonWakeupAlarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1444 | } else { |
| 1445 | pw.println("(none)"); |
| 1446 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1447 | pw.print(" Number of delayed alarms: "); pw.print(mNumDelayedAlarms); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1448 | pw.print(", total delay time: "); TimeUtils.formatDuration(mTotalDelayTime, pw); |
| 1449 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1450 | pw.print(" Max delay time: "); TimeUtils.formatDuration(mMaxDelayTime, pw); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1451 | pw.print(", max non-interactive time: "); |
| 1452 | TimeUtils.formatDuration(mNonInteractiveTime, pw); |
| 1453 | pw.println(); |
| 1454 | |
| 1455 | pw.println(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1456 | pw.print(" Broadcast ref count: "); pw.println(mBroadcastRefCount); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1457 | pw.println(); |
| 1458 | |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 1459 | if (mInFlight.size() > 0) { |
| 1460 | pw.println("Outstanding deliveries:"); |
| 1461 | for (int i = 0; i < mInFlight.size(); i++) { |
| 1462 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 1463 | pw.println(mInFlight.get(i)); |
| 1464 | } |
| 1465 | pw.println(); |
| 1466 | } |
| 1467 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1468 | pw.print(" mAllowWhileIdleMinTime="); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1469 | TimeUtils.formatDuration(mAllowWhileIdleMinTime, pw); |
| 1470 | pw.println(); |
| 1471 | if (mLastAllowWhileIdleDispatch.size() > 0) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1472 | pw.println(" Last allow while idle dispatch times:"); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1473 | for (int i=0; i<mLastAllowWhileIdleDispatch.size(); i++) { |
| 1474 | pw.print(" UID "); |
| 1475 | UserHandle.formatUid(pw, mLastAllowWhileIdleDispatch.keyAt(i)); |
| 1476 | pw.print(": "); |
| 1477 | TimeUtils.formatDuration(mLastAllowWhileIdleDispatch.valueAt(i), |
| 1478 | nowELAPSED, pw); |
| 1479 | pw.println(); |
| 1480 | } |
| 1481 | } |
| 1482 | pw.println(); |
| 1483 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1484 | if (mLog.dump(pw, " Recent problems", " ")) { |
| 1485 | pw.println(); |
| 1486 | } |
| 1487 | |
| 1488 | final FilterStats[] topFilters = new FilterStats[10]; |
| 1489 | final Comparator<FilterStats> comparator = new Comparator<FilterStats>() { |
| 1490 | @Override |
| 1491 | public int compare(FilterStats lhs, FilterStats rhs) { |
| 1492 | if (lhs.aggregateTime < rhs.aggregateTime) { |
| 1493 | return 1; |
| 1494 | } else if (lhs.aggregateTime > rhs.aggregateTime) { |
| 1495 | return -1; |
| 1496 | } |
| 1497 | return 0; |
| 1498 | } |
| 1499 | }; |
| 1500 | int len = 0; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1501 | for (int iu=0; iu<mBroadcastStats.size(); iu++) { |
| 1502 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 1503 | for (int ip=0; ip<uidStats.size(); ip++) { |
| 1504 | BroadcastStats bs = uidStats.valueAt(ip); |
| 1505 | for (int is=0; is<bs.filterStats.size(); is++) { |
| 1506 | FilterStats fs = bs.filterStats.valueAt(is); |
| 1507 | int pos = len > 0 |
| 1508 | ? Arrays.binarySearch(topFilters, 0, len, fs, comparator) : 0; |
| 1509 | if (pos < 0) { |
| 1510 | pos = -pos - 1; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1511 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1512 | if (pos < topFilters.length) { |
| 1513 | int copylen = topFilters.length - pos - 1; |
| 1514 | if (copylen > 0) { |
| 1515 | System.arraycopy(topFilters, pos, topFilters, pos+1, copylen); |
| 1516 | } |
| 1517 | topFilters[pos] = fs; |
| 1518 | if (len < topFilters.length) { |
| 1519 | len++; |
| 1520 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | if (len > 0) { |
| 1526 | pw.println(" Top Alarms:"); |
| 1527 | for (int i=0; i<len; i++) { |
| 1528 | FilterStats fs = topFilters[i]; |
| 1529 | pw.print(" "); |
| 1530 | if (fs.nesting > 0) pw.print("*ACTIVE* "); |
| 1531 | TimeUtils.formatDuration(fs.aggregateTime, pw); |
| 1532 | pw.print(" running, "); pw.print(fs.numWakeup); |
| 1533 | pw.print(" wakeups, "); pw.print(fs.count); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1534 | pw.print(" alarms: "); UserHandle.formatUid(pw, fs.mBroadcastStats.mUid); |
| 1535 | pw.print(":"); pw.print(fs.mBroadcastStats.mPackageName); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1536 | pw.println(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1537 | pw.print(" "); pw.print(fs.mTag); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1538 | pw.println(); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | pw.println(" "); |
| 1543 | pw.println(" Alarm Stats:"); |
| 1544 | final ArrayList<FilterStats> tmpFilters = new ArrayList<FilterStats>(); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1545 | for (int iu=0; iu<mBroadcastStats.size(); iu++) { |
| 1546 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 1547 | for (int ip=0; ip<uidStats.size(); ip++) { |
| 1548 | BroadcastStats bs = uidStats.valueAt(ip); |
| 1549 | pw.print(" "); |
| 1550 | if (bs.nesting > 0) pw.print("*ACTIVE* "); |
| 1551 | UserHandle.formatUid(pw, bs.mUid); |
| 1552 | pw.print(":"); |
| 1553 | pw.print(bs.mPackageName); |
| 1554 | pw.print(" "); TimeUtils.formatDuration(bs.aggregateTime, pw); |
| 1555 | pw.print(" running, "); pw.print(bs.numWakeup); |
| 1556 | pw.println(" wakeups:"); |
| 1557 | tmpFilters.clear(); |
| 1558 | for (int is=0; is<bs.filterStats.size(); is++) { |
| 1559 | tmpFilters.add(bs.filterStats.valueAt(is)); |
| 1560 | } |
| 1561 | Collections.sort(tmpFilters, comparator); |
| 1562 | for (int i=0; i<tmpFilters.size(); i++) { |
| 1563 | FilterStats fs = tmpFilters.get(i); |
| 1564 | pw.print(" "); |
| 1565 | if (fs.nesting > 0) pw.print("*ACTIVE* "); |
| 1566 | TimeUtils.formatDuration(fs.aggregateTime, pw); |
| 1567 | pw.print(" "); pw.print(fs.numWakeup); |
| 1568 | pw.print(" wakes " ); pw.print(fs.count); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1569 | pw.print(" alarms, last "); |
| 1570 | TimeUtils.formatDuration(fs.lastTime, nowELAPSED, pw); |
| 1571 | pw.println(":"); |
| 1572 | pw.print(" "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1573 | pw.print(fs.mTag); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1574 | pw.println(); |
| 1575 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1576 | } |
| 1577 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1578 | |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1579 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1580 | pw.println(); |
| 1581 | pw.println(" Allow while idle dispatches:"); |
| 1582 | for (int i = 0; i < mAllowWhileIdleDispatches.size(); i++) { |
| 1583 | IdleDispatchEntry ent = mAllowWhileIdleDispatches.get(i); |
| 1584 | pw.print(" "); |
| 1585 | TimeUtils.formatDuration(ent.elapsedRealtime, nowELAPSED, pw); |
| 1586 | pw.print(": "); |
| 1587 | UserHandle.formatUid(pw, ent.uid); |
| 1588 | pw.print(":"); |
| 1589 | pw.println(ent.pkg); |
| 1590 | if (ent.op != null) { |
| 1591 | pw.print(" "); |
| 1592 | pw.print(ent.op); |
| 1593 | pw.print(" / "); |
| 1594 | pw.print(ent.tag); |
| 1595 | if (ent.argRealtime != 0) { |
| 1596 | pw.print(" ("); |
| 1597 | TimeUtils.formatDuration(ent.argRealtime, nowELAPSED, pw); |
| 1598 | pw.print(")"); |
| 1599 | } |
| 1600 | pw.println(); |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1605 | if (WAKEUP_STATS) { |
| 1606 | pw.println(); |
| 1607 | pw.println(" Recent Wakeup History:"); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1608 | long last = -1; |
| 1609 | for (WakeupEvent event : mRecentWakeups) { |
| 1610 | pw.print(" "); pw.print(sdf.format(new Date(event.when))); |
| 1611 | pw.print('|'); |
| 1612 | if (last < 0) { |
| 1613 | pw.print('0'); |
| 1614 | } else { |
| 1615 | pw.print(event.when - last); |
| 1616 | } |
| 1617 | last = event.when; |
| 1618 | pw.print('|'); pw.print(event.uid); |
| 1619 | pw.print('|'); pw.print(event.action); |
| 1620 | pw.println(); |
| 1621 | } |
| 1622 | pw.println(); |
| 1623 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1627 | private void logBatchesLocked(SimpleDateFormat sdf) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1628 | ByteArrayOutputStream bs = new ByteArrayOutputStream(2048); |
| 1629 | PrintWriter pw = new PrintWriter(bs); |
| 1630 | final long nowRTC = System.currentTimeMillis(); |
| 1631 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 1632 | final int NZ = mAlarmBatches.size(); |
| 1633 | for (int iz = 0; iz < NZ; iz++) { |
| 1634 | Batch bz = mAlarmBatches.get(iz); |
| 1635 | pw.append("Batch "); pw.print(iz); pw.append(": "); pw.println(bz); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1636 | dumpAlarmList(pw, bz.alarms, " ", nowELAPSED, nowRTC, sdf); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1637 | pw.flush(); |
| 1638 | Slog.v(TAG, bs.toString()); |
| 1639 | bs.reset(); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | private boolean validateConsistencyLocked() { |
| 1644 | if (DEBUG_VALIDATE) { |
| 1645 | long lastTime = Long.MIN_VALUE; |
| 1646 | final int N = mAlarmBatches.size(); |
| 1647 | for (int i = 0; i < N; i++) { |
| 1648 | Batch b = mAlarmBatches.get(i); |
| 1649 | if (b.start >= lastTime) { |
| 1650 | // duplicate start times are okay because of standalone batches |
| 1651 | lastTime = b.start; |
| 1652 | } else { |
| 1653 | Slog.e(TAG, "CONSISTENCY FAILURE: Batch " + i + " is out of order"); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1654 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 1655 | logBatchesLocked(sdf); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1656 | return false; |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 1663 | private Batch findFirstWakeupBatchLocked() { |
| 1664 | final int N = mAlarmBatches.size(); |
| 1665 | for (int i = 0; i < N; i++) { |
| 1666 | Batch b = mAlarmBatches.get(i); |
| 1667 | if (b.hasWakeups()) { |
| 1668 | return b; |
| 1669 | } |
| 1670 | } |
| 1671 | return null; |
| 1672 | } |
| 1673 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1674 | long getNextWakeFromIdleTimeImpl() { |
| 1675 | synchronized (mLock) { |
| 1676 | return mNextWakeFromIdle != null ? mNextWakeFromIdle.whenElapsed : Long.MAX_VALUE; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | AlarmManager.AlarmClockInfo getNextAlarmClockImpl(int userId) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1681 | synchronized (mLock) { |
| 1682 | return mNextAlarmClockForUser.get(userId); |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | /** |
| 1687 | * Recomputes the next alarm clock for all users. |
| 1688 | */ |
| 1689 | private void updateNextAlarmClockLocked() { |
| 1690 | if (!mNextAlarmClockMayChange) { |
| 1691 | return; |
| 1692 | } |
| 1693 | mNextAlarmClockMayChange = false; |
| 1694 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1695 | SparseArray<AlarmManager.AlarmClockInfo> nextForUser = mTmpSparseAlarmClockArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1696 | nextForUser.clear(); |
| 1697 | |
| 1698 | final int N = mAlarmBatches.size(); |
| 1699 | for (int i = 0; i < N; i++) { |
| 1700 | ArrayList<Alarm> alarms = mAlarmBatches.get(i).alarms; |
| 1701 | final int M = alarms.size(); |
| 1702 | |
| 1703 | for (int j = 0; j < M; j++) { |
| 1704 | Alarm a = alarms.get(j); |
| 1705 | if (a.alarmClock != null) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1706 | final int userId = UserHandle.getUserId(a.uid); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1707 | |
| 1708 | if (DEBUG_ALARM_CLOCK) { |
| 1709 | Log.v(TAG, "Found AlarmClockInfo at " + |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 1710 | formatNextAlarm(getContext(), a.alarmClock, userId) + |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1711 | " for user " + userId); |
| 1712 | } |
| 1713 | |
| 1714 | // Alarms and batches are sorted by time, no need to compare times here. |
| 1715 | if (nextForUser.get(userId) == null) { |
| 1716 | nextForUser.put(userId, a.alarmClock); |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | // Update mNextAlarmForUser with new values. |
| 1723 | final int NN = nextForUser.size(); |
| 1724 | for (int i = 0; i < NN; i++) { |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1725 | AlarmManager.AlarmClockInfo newAlarm = nextForUser.valueAt(i); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1726 | int userId = nextForUser.keyAt(i); |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1727 | AlarmManager.AlarmClockInfo currentAlarm = mNextAlarmClockForUser.get(userId); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1728 | if (!newAlarm.equals(currentAlarm)) { |
| 1729 | updateNextAlarmInfoForUserLocked(userId, newAlarm); |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | // Remove users without any alarm clocks scheduled. |
| 1734 | final int NNN = mNextAlarmClockForUser.size(); |
| 1735 | for (int i = NNN - 1; i >= 0; i--) { |
| 1736 | int userId = mNextAlarmClockForUser.keyAt(i); |
| 1737 | if (nextForUser.get(userId) == null) { |
| 1738 | updateNextAlarmInfoForUserLocked(userId, null); |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1743 | private void updateNextAlarmInfoForUserLocked(int userId, |
| 1744 | AlarmManager.AlarmClockInfo alarmClock) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1745 | if (alarmClock != null) { |
| 1746 | if (DEBUG_ALARM_CLOCK) { |
| 1747 | Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): " + |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 1748 | formatNextAlarm(getContext(), alarmClock, userId)); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1749 | } |
| 1750 | mNextAlarmClockForUser.put(userId, alarmClock); |
| 1751 | } else { |
| 1752 | if (DEBUG_ALARM_CLOCK) { |
| 1753 | Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): None"); |
| 1754 | } |
| 1755 | mNextAlarmClockForUser.remove(userId); |
| 1756 | } |
| 1757 | |
| 1758 | mPendingSendNextAlarmClockChangedForUser.put(userId, true); |
| 1759 | mHandler.removeMessages(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED); |
| 1760 | mHandler.sendEmptyMessage(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED); |
| 1761 | } |
| 1762 | |
| 1763 | /** |
| 1764 | * Updates NEXT_ALARM_FORMATTED and sends NEXT_ALARM_CLOCK_CHANGED_INTENT for all users |
| 1765 | * for which alarm clocks have changed since the last call to this. |
| 1766 | * |
| 1767 | * Do not call with a lock held. Only call from mHandler's thread. |
| 1768 | * |
| 1769 | * @see AlarmHandler#SEND_NEXT_ALARM_CLOCK_CHANGED |
| 1770 | */ |
| 1771 | private void sendNextAlarmClockChanged() { |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1772 | SparseArray<AlarmManager.AlarmClockInfo> pendingUsers = mHandlerSparseAlarmClockArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1773 | pendingUsers.clear(); |
| 1774 | |
| 1775 | synchronized (mLock) { |
| 1776 | final int N = mPendingSendNextAlarmClockChangedForUser.size(); |
| 1777 | for (int i = 0; i < N; i++) { |
| 1778 | int userId = mPendingSendNextAlarmClockChangedForUser.keyAt(i); |
| 1779 | pendingUsers.append(userId, mNextAlarmClockForUser.get(userId)); |
| 1780 | } |
| 1781 | mPendingSendNextAlarmClockChangedForUser.clear(); |
| 1782 | } |
| 1783 | |
| 1784 | final int N = pendingUsers.size(); |
| 1785 | for (int i = 0; i < N; i++) { |
| 1786 | int userId = pendingUsers.keyAt(i); |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1787 | AlarmManager.AlarmClockInfo alarmClock = pendingUsers.valueAt(i); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1788 | Settings.System.putStringForUser(getContext().getContentResolver(), |
| 1789 | Settings.System.NEXT_ALARM_FORMATTED, |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 1790 | formatNextAlarm(getContext(), alarmClock, userId), |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1791 | userId); |
| 1792 | |
| 1793 | getContext().sendBroadcastAsUser(NEXT_ALARM_CLOCK_CHANGED_INTENT, |
| 1794 | new UserHandle(userId)); |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | /** |
| 1799 | * Formats an alarm like platform/packages/apps/DeskClock used to. |
| 1800 | */ |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 1801 | private static String formatNextAlarm(final Context context, AlarmManager.AlarmClockInfo info, |
| 1802 | int userId) { |
| 1803 | String skeleton = DateFormat.is24HourFormat(context, userId) ? "EHm" : "Ehma"; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1804 | String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton); |
| 1805 | return (info == null) ? "" : |
| 1806 | DateFormat.format(pattern, info.getTriggerTime()).toString(); |
| 1807 | } |
| 1808 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1809 | void rescheduleKernelAlarmsLocked() { |
| 1810 | // Schedule the next upcoming wakeup alarm. If there is a deliverable batch |
| 1811 | // prior to that which contains no wakeups, we schedule that as well. |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1812 | long nextNonWakeup = 0; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1813 | if (mAlarmBatches.size() > 0) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 1814 | final Batch firstWakeup = findFirstWakeupBatchLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1815 | final Batch firstBatch = mAlarmBatches.get(0); |
Prashant Malani | 753e9e0 | 2015-06-10 17:43:49 -0700 | [diff] [blame] | 1816 | if (firstWakeup != null && mNextWakeup != firstWakeup.start) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 1817 | mNextWakeup = firstWakeup.start; |
| 1818 | setLocked(ELAPSED_REALTIME_WAKEUP, firstWakeup.start); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1819 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1820 | if (firstBatch != firstWakeup) { |
| 1821 | nextNonWakeup = firstBatch.start; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1822 | } |
| 1823 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1824 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 1825 | if (nextNonWakeup == 0 || mNextNonWakeupDeliveryTime < nextNonWakeup) { |
| 1826 | nextNonWakeup = mNextNonWakeupDeliveryTime; |
| 1827 | } |
| 1828 | } |
Prashant Malani | 753e9e0 | 2015-06-10 17:43:49 -0700 | [diff] [blame] | 1829 | if (nextNonWakeup != 0 && mNextNonWakeup != nextNonWakeup) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1830 | mNextNonWakeup = nextNonWakeup; |
| 1831 | setLocked(ELAPSED_REALTIME, nextNonWakeup); |
| 1832 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1833 | } |
| 1834 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1835 | private void removeLocked(PendingIntent operation, IAlarmListener directReceiver) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1836 | boolean didRemove = false; |
| 1837 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 1838 | Batch b = mAlarmBatches.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1839 | didRemove |= b.remove(operation, directReceiver); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1840 | if (b.size() == 0) { |
| 1841 | mAlarmBatches.remove(i); |
| 1842 | } |
| 1843 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1844 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1845 | if (mPendingWhileIdleAlarms.get(i).matches(operation, directReceiver)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1846 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 1847 | mPendingWhileIdleAlarms.remove(i); |
| 1848 | } |
| 1849 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1850 | |
| 1851 | if (didRemove) { |
| 1852 | if (DEBUG_BATCH) { |
| 1853 | Slog.v(TAG, "remove(operation) changed bounds; rebatching"); |
| 1854 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1855 | boolean restorePending = false; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1856 | if (mPendingIdleUntil != null && mPendingIdleUntil.matches(operation, directReceiver)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1857 | mPendingIdleUntil = null; |
| 1858 | restorePending = true; |
| 1859 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1860 | if (mNextWakeFromIdle != null && mNextWakeFromIdle.matches(operation, directReceiver)) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1861 | mNextWakeFromIdle = null; |
| 1862 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1863 | rebatchAllAlarmsLocked(true); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1864 | if (restorePending) { |
| 1865 | restorePendingWhileIdleAlarmsLocked(); |
| 1866 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1867 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | void removeLocked(String packageName) { |
| 1872 | boolean didRemove = false; |
| 1873 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 1874 | Batch b = mAlarmBatches.get(i); |
| 1875 | didRemove |= b.remove(packageName); |
| 1876 | if (b.size() == 0) { |
| 1877 | mAlarmBatches.remove(i); |
| 1878 | } |
| 1879 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1880 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1881 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
| 1882 | if (a.matches(packageName)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1883 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 1884 | mPendingWhileIdleAlarms.remove(i); |
| 1885 | } |
| 1886 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1887 | |
| 1888 | if (didRemove) { |
| 1889 | if (DEBUG_BATCH) { |
| 1890 | Slog.v(TAG, "remove(package) changed bounds; rebatching"); |
| 1891 | } |
| 1892 | rebatchAllAlarmsLocked(true); |
| 1893 | rescheduleKernelAlarmsLocked(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1894 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1898 | void removeForStoppedLocked(int uid) { |
| 1899 | boolean didRemove = false; |
| 1900 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 1901 | Batch b = mAlarmBatches.get(i); |
| 1902 | didRemove |= b.removeForStopped(uid); |
| 1903 | if (b.size() == 0) { |
| 1904 | mAlarmBatches.remove(i); |
| 1905 | } |
| 1906 | } |
| 1907 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
| 1908 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
| 1909 | try { |
| 1910 | if (a.uid == uid && ActivityManagerNative.getDefault().getAppStartMode( |
| 1911 | uid, a.packageName) == ActivityManager.APP_START_MODE_DISABLED) { |
| 1912 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 1913 | mPendingWhileIdleAlarms.remove(i); |
| 1914 | } |
| 1915 | } catch (RemoteException e) { |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | if (didRemove) { |
| 1920 | if (DEBUG_BATCH) { |
| 1921 | Slog.v(TAG, "remove(package) changed bounds; rebatching"); |
| 1922 | } |
| 1923 | rebatchAllAlarmsLocked(true); |
| 1924 | rescheduleKernelAlarmsLocked(); |
| 1925 | updateNextAlarmClockLocked(); |
| 1926 | } |
| 1927 | } |
| 1928 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1929 | void removeUserLocked(int userHandle) { |
| 1930 | boolean didRemove = false; |
| 1931 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 1932 | Batch b = mAlarmBatches.get(i); |
| 1933 | didRemove |= b.remove(userHandle); |
| 1934 | if (b.size() == 0) { |
| 1935 | mAlarmBatches.remove(i); |
| 1936 | } |
| 1937 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1938 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1939 | if (UserHandle.getUserId(mPendingWhileIdleAlarms.get(i).creatorUid) |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1940 | == userHandle) { |
| 1941 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 1942 | mPendingWhileIdleAlarms.remove(i); |
| 1943 | } |
| 1944 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1945 | for (int i = mLastAllowWhileIdleDispatch.size() - 1; i >= 0; i--) { |
| 1946 | if (UserHandle.getUserId(mLastAllowWhileIdleDispatch.keyAt(i)) == userHandle) { |
| 1947 | mLastAllowWhileIdleDispatch.removeAt(i); |
| 1948 | } |
| 1949 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1950 | |
| 1951 | if (didRemove) { |
| 1952 | if (DEBUG_BATCH) { |
| 1953 | Slog.v(TAG, "remove(user) changed bounds; rebatching"); |
| 1954 | } |
| 1955 | rebatchAllAlarmsLocked(true); |
| 1956 | rescheduleKernelAlarmsLocked(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1957 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1961 | void interactiveStateChangedLocked(boolean interactive) { |
| 1962 | if (mInteractive != interactive) { |
| 1963 | mInteractive = interactive; |
| 1964 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 1965 | if (interactive) { |
| 1966 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 1967 | final long thisDelayTime = nowELAPSED - mStartCurrentDelayTime; |
| 1968 | mTotalDelayTime += thisDelayTime; |
| 1969 | if (mMaxDelayTime < thisDelayTime) { |
| 1970 | mMaxDelayTime = thisDelayTime; |
| 1971 | } |
| 1972 | deliverAlarmsLocked(mPendingNonWakeupAlarms, nowELAPSED); |
| 1973 | mPendingNonWakeupAlarms.clear(); |
| 1974 | } |
| 1975 | if (mNonInteractiveStartTime > 0) { |
| 1976 | long dur = nowELAPSED - mNonInteractiveStartTime; |
| 1977 | if (dur > mNonInteractiveTime) { |
| 1978 | mNonInteractiveTime = dur; |
| 1979 | } |
| 1980 | } |
| 1981 | } else { |
| 1982 | mNonInteractiveStartTime = nowELAPSED; |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1987 | boolean lookForPackageLocked(String packageName) { |
| 1988 | for (int i = 0; i < mAlarmBatches.size(); i++) { |
| 1989 | Batch b = mAlarmBatches.get(i); |
| 1990 | if (b.hasPackage(packageName)) { |
| 1991 | return true; |
| 1992 | } |
| 1993 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1994 | for (int i = 0; i < mPendingWhileIdleAlarms.size(); i++) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1995 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
| 1996 | if (a.matches(packageName)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1997 | return true; |
| 1998 | } |
| 1999 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2000 | return false; |
| 2001 | } |
| 2002 | |
| 2003 | private void setLocked(int type, long when) { |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 2004 | if (mNativeData != 0) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2005 | // The kernel never triggers alarms with negative wakeup times |
| 2006 | // so we ensure they are positive. |
| 2007 | long alarmSeconds, alarmNanoseconds; |
| 2008 | if (when < 0) { |
| 2009 | alarmSeconds = 0; |
| 2010 | alarmNanoseconds = 0; |
| 2011 | } else { |
| 2012 | alarmSeconds = when / 1000; |
| 2013 | alarmNanoseconds = (when % 1000) * 1000 * 1000; |
| 2014 | } |
| 2015 | |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 2016 | set(mNativeData, type, alarmSeconds, alarmNanoseconds); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2017 | } else { |
| 2018 | Message msg = Message.obtain(); |
| 2019 | msg.what = ALARM_EVENT; |
| 2020 | |
| 2021 | mHandler.removeMessages(ALARM_EVENT); |
| 2022 | mHandler.sendMessageAtTime(msg, when); |
| 2023 | } |
| 2024 | } |
| 2025 | |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 2026 | private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2027 | String prefix, String label, long nowRTC, long nowELAPSED, SimpleDateFormat sdf) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2028 | for (int i=list.size()-1; i>=0; i--) { |
| 2029 | Alarm a = list.get(i); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2030 | pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i); |
| 2031 | pw.print(": "); pw.println(a); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2032 | a.dump(pw, prefix + " ", nowRTC, nowELAPSED, sdf); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2033 | } |
| 2034 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2035 | |
| 2036 | private static final String labelForType(int type) { |
| 2037 | switch (type) { |
| 2038 | case RTC: return "RTC"; |
| 2039 | case RTC_WAKEUP : return "RTC_WAKEUP"; |
| 2040 | case ELAPSED_REALTIME : return "ELAPSED"; |
| 2041 | case ELAPSED_REALTIME_WAKEUP: return "ELAPSED_WAKEUP"; |
| 2042 | default: |
| 2043 | break; |
| 2044 | } |
| 2045 | return "--unknown--"; |
| 2046 | } |
| 2047 | |
| 2048 | private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2049 | String prefix, long nowELAPSED, long nowRTC, SimpleDateFormat sdf) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2050 | for (int i=list.size()-1; i>=0; i--) { |
| 2051 | Alarm a = list.get(i); |
| 2052 | final String label = labelForType(a.type); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2053 | pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i); |
| 2054 | pw.print(": "); pw.println(a); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2055 | a.dump(pw, prefix + " ", nowRTC, nowELAPSED, sdf); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 2059 | private native long init(); |
| 2060 | private native void close(long nativeData); |
| 2061 | private native void set(long nativeData, int type, long seconds, long nanoseconds); |
| 2062 | private native int waitForAlarm(long nativeData); |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 2063 | private native int setKernelTime(long nativeData, long millis); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 2064 | private native int setKernelTimezone(long nativeData, int minuteswest); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2065 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2066 | boolean triggerAlarmsLocked(ArrayList<Alarm> triggerList, final long nowELAPSED, |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2067 | final long nowRTC) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2068 | boolean hasWakeup = false; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2069 | // batches are temporally sorted, so we need only pull from the |
| 2070 | // start of the list until we either empty it or hit a batch |
| 2071 | // that is not yet deliverable |
Christopher Tate | 6578ad1 | 2013-09-24 17:12:46 -0700 | [diff] [blame] | 2072 | while (mAlarmBatches.size() > 0) { |
| 2073 | Batch batch = mAlarmBatches.get(0); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2074 | if (batch.start > nowELAPSED) { |
| 2075 | // Everything else is scheduled for the future |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2076 | break; |
| 2077 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2078 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2079 | // We will (re)schedule some alarms now; don't let that interfere |
| 2080 | // with delivery of this current batch |
| 2081 | mAlarmBatches.remove(0); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2082 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2083 | final int N = batch.size(); |
| 2084 | for (int i = 0; i < N; i++) { |
| 2085 | Alarm alarm = batch.get(i); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2086 | |
| 2087 | if ((alarm.flags&AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0) { |
| 2088 | // If this is an ALLOW_WHILE_IDLE alarm, we constrain how frequently the app can |
| 2089 | // schedule such alarms. |
| 2090 | long lastTime = mLastAllowWhileIdleDispatch.get(alarm.uid, 0); |
| 2091 | long minTime = lastTime + mAllowWhileIdleMinTime; |
| 2092 | if (nowELAPSED < minTime) { |
| 2093 | // Whoops, it hasn't been long enough since the last ALLOW_WHILE_IDLE |
| 2094 | // alarm went off for this app. Reschedule the alarm to be in the |
| 2095 | // correct time period. |
| 2096 | alarm.whenElapsed = minTime; |
| 2097 | if (alarm.maxWhenElapsed < minTime) { |
| 2098 | alarm.maxWhenElapsed = minTime; |
| 2099 | } |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 2100 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 2101 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 2102 | ent.uid = alarm.uid; |
| 2103 | ent.pkg = alarm.operation.getCreatorPackage(); |
| 2104 | ent.tag = alarm.operation.getTag(""); |
| 2105 | ent.op = "RESCHEDULE"; |
| 2106 | ent.elapsedRealtime = nowELAPSED; |
| 2107 | ent.argRealtime = lastTime; |
| 2108 | mAllowWhileIdleDispatches.add(ent); |
| 2109 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2110 | setImplLocked(alarm, true, false); |
| 2111 | continue; |
| 2112 | } |
| 2113 | } |
| 2114 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2115 | alarm.count = 1; |
| 2116 | triggerList.add(alarm); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2117 | if ((alarm.flags&AlarmManager.FLAG_WAKE_FROM_IDLE) != 0) { |
| 2118 | EventLogTags.writeDeviceIdleWakeFromIdle(mPendingIdleUntil != null ? 1 : 0, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2119 | alarm.statsTag); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2120 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2121 | if (mPendingIdleUntil == alarm) { |
| 2122 | mPendingIdleUntil = null; |
| 2123 | rebatchAllAlarmsLocked(false); |
| 2124 | restorePendingWhileIdleAlarmsLocked(); |
| 2125 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2126 | if (mNextWakeFromIdle == alarm) { |
| 2127 | mNextWakeFromIdle = null; |
| 2128 | rebatchAllAlarmsLocked(false); |
| 2129 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2130 | |
| 2131 | // Recurring alarms may have passed several alarm intervals while the |
| 2132 | // phone was asleep or off, so pass a trigger count when sending them. |
| 2133 | if (alarm.repeatInterval > 0) { |
| 2134 | // this adjustment will be zero if we're late by |
| 2135 | // less than one full repeat interval |
| 2136 | alarm.count += (nowELAPSED - alarm.whenElapsed) / alarm.repeatInterval; |
| 2137 | |
| 2138 | // Also schedule its next recurrence |
| 2139 | final long delta = alarm.count * alarm.repeatInterval; |
| 2140 | final long nextElapsed = alarm.whenElapsed + delta; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 2141 | setImplLocked(alarm.type, alarm.when + delta, nextElapsed, alarm.windowLength, |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2142 | maxTriggerTime(nowELAPSED, nextElapsed, alarm.repeatInterval), |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2143 | alarm.repeatInterval, alarm.operation, null, null, alarm.flags, true, |
| 2144 | alarm.workSource, alarm.alarmClock, alarm.uid, alarm.packageName); |
Christopher Tate | 864d42e | 2014-12-02 11:48:53 -0800 | [diff] [blame] | 2145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | |
Christopher Tate | 864d42e | 2014-12-02 11:48:53 -0800 | [diff] [blame] | 2147 | if (alarm.wakeup) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2148 | hasWakeup = true; |
| 2149 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2150 | |
| 2151 | // We removed an alarm clock. Let the caller recompute the next alarm clock. |
| 2152 | if (alarm.alarmClock != null) { |
| 2153 | mNextAlarmClockMayChange = true; |
| 2154 | } |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2155 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2156 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2157 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 2158 | // This is a new alarm delivery set; bump the sequence number to indicate that |
| 2159 | // all apps' alarm delivery classes should be recalculated. |
| 2160 | mCurrentSeq++; |
| 2161 | calculateDeliveryPriorities(triggerList); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2162 | Collections.sort(triggerList, mAlarmDispatchComparator); |
| 2163 | |
| 2164 | if (localLOGV) { |
| 2165 | for (int i=0; i<triggerList.size(); i++) { |
| 2166 | Slog.v(TAG, "Triggering alarm #" + i + ": " + triggerList.get(i)); |
| 2167 | } |
| 2168 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2169 | |
| 2170 | return hasWakeup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2171 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2173 | /** |
| 2174 | * This Comparator sorts Alarms into increasing time order. |
| 2175 | */ |
| 2176 | public static class IncreasingTimeOrder implements Comparator<Alarm> { |
| 2177 | public int compare(Alarm a1, Alarm a2) { |
jinho.park | 1acd32a | 2015-05-27 14:44:18 +0900 | [diff] [blame] | 2178 | long when1 = a1.whenElapsed; |
| 2179 | long when2 = a2.whenElapsed; |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 2180 | if (when1 > when2) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2181 | return 1; |
| 2182 | } |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 2183 | if (when1 < when2) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2184 | return -1; |
| 2185 | } |
| 2186 | return 0; |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | private static class Alarm { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2191 | public final int type; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2192 | public final long origWhen; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2193 | public final boolean wakeup; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2194 | public final PendingIntent operation; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2195 | public final IAlarmListener listener; |
| 2196 | public final String listenerTag; |
| 2197 | public final String statsTag; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2198 | public final WorkSource workSource; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2199 | public final int flags; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2200 | public final AlarmManager.AlarmClockInfo alarmClock; |
| 2201 | public final int uid; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2202 | public final int creatorUid; |
| 2203 | public final String packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2204 | public int count; |
| 2205 | public long when; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 2206 | public long windowLength; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2207 | public long whenElapsed; // 'when' in the elapsed time base |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2208 | public long maxWhenElapsed; // also in the elapsed time base |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2209 | public long repeatInterval; |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 2210 | public PriorityClass priorityClass; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2211 | |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 2212 | public Alarm(int _type, long _when, long _whenElapsed, long _windowLength, long _maxWhen, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2213 | long _interval, PendingIntent _op, IAlarmListener _rec, String _listenerTag, |
| 2214 | WorkSource _ws, int _flags, AlarmManager.AlarmClockInfo _info, |
| 2215 | int _uid, String _pkgName) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2216 | type = _type; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2217 | origWhen = _when; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2218 | wakeup = _type == AlarmManager.ELAPSED_REALTIME_WAKEUP |
| 2219 | || _type == AlarmManager.RTC_WAKEUP; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2220 | when = _when; |
| 2221 | whenElapsed = _whenElapsed; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 2222 | windowLength = _windowLength; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2223 | maxWhenElapsed = _maxWhen; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2224 | repeatInterval = _interval; |
| 2225 | operation = _op; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2226 | listener = _rec; |
| 2227 | listenerTag = _listenerTag; |
| 2228 | statsTag = makeTag(_op, _listenerTag, _type); |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2229 | workSource = _ws; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2230 | flags = _flags; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2231 | alarmClock = _info; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2232 | uid = _uid; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2233 | packageName = _pkgName; |
| 2234 | |
| 2235 | creatorUid = (operation != null) ? operation.getCreatorUid() : uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2236 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2237 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2238 | public static String makeTag(PendingIntent pi, String tag, int type) { |
| 2239 | final String alarmString = type == ELAPSED_REALTIME_WAKEUP || type == RTC_WAKEUP |
| 2240 | ? "*walarm*:" : "*alarm*:"; |
| 2241 | return (pi != null) ? pi.getTag(alarmString) : (alarmString + tag); |
| 2242 | } |
| 2243 | |
| 2244 | public WakeupEvent makeWakeupEvent(long nowRTC) { |
| 2245 | return new WakeupEvent(nowRTC, creatorUid, |
| 2246 | (operation != null) |
| 2247 | ? operation.getIntent().getAction() |
| 2248 | : ("<listener>:" + listenerTag)); |
| 2249 | } |
| 2250 | |
| 2251 | // Returns true if either matches |
| 2252 | public boolean matches(PendingIntent pi, IAlarmListener rec) { |
| 2253 | return (operation != null) |
| 2254 | ? operation.equals(pi) |
Lorenzo Colitti | 9b43ce0 | 2015-11-22 22:00:08 +0900 | [diff] [blame] | 2255 | : rec != null && listener.asBinder().equals(rec.asBinder()); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | public boolean matches(String packageName) { |
| 2259 | return (operation != null) |
| 2260 | ? packageName.equals(operation.getTargetPackage()) |
| 2261 | : packageName.equals(this.packageName); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2264 | @Override |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2265 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2266 | StringBuilder sb = new StringBuilder(128); |
| 2267 | sb.append("Alarm{"); |
| 2268 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 2269 | sb.append(" type "); |
| 2270 | sb.append(type); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2271 | sb.append(" when "); |
| 2272 | sb.append(when); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2273 | sb.append(" "); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2274 | if (operation != null) { |
| 2275 | sb.append(operation.getTargetPackage()); |
| 2276 | } else { |
| 2277 | sb.append(listener.asBinder().toString()); |
| 2278 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2279 | sb.append('}'); |
| 2280 | return sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2283 | public void dump(PrintWriter pw, String prefix, long nowRTC, long nowELAPSED, |
| 2284 | SimpleDateFormat sdf) { |
| 2285 | final boolean isRtc = (type == RTC || type == RTC_WAKEUP); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2286 | pw.print(prefix); pw.print("tag="); pw.println(statsTag); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2287 | pw.print(prefix); pw.print("type="); pw.print(type); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2288 | pw.print(" whenElapsed="); TimeUtils.formatDuration(whenElapsed, |
| 2289 | nowELAPSED, pw); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2290 | pw.print(" when="); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2291 | if (isRtc) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2292 | pw.print(sdf.format(new Date(when))); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2293 | } else { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2294 | TimeUtils.formatDuration(when, nowELAPSED, pw); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2295 | } |
| 2296 | pw.println(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2297 | pw.print(prefix); pw.print("window="); TimeUtils.formatDuration(windowLength, pw); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2298 | pw.print(" repeatInterval="); pw.print(repeatInterval); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2299 | pw.print(" count="); pw.print(count); |
| 2300 | pw.print(" flags=0x"); pw.println(Integer.toHexString(flags)); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2301 | if (alarmClock != null) { |
| 2302 | pw.print(prefix); pw.println("Alarm clock:"); |
| 2303 | pw.print(prefix); pw.print(" triggerTime="); |
| 2304 | pw.println(sdf.format(new Date(alarmClock.getTriggerTime()))); |
| 2305 | pw.print(prefix); pw.print(" showIntent="); pw.println(alarmClock.getShowIntent()); |
| 2306 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2307 | pw.print(prefix); pw.print("operation="); pw.println(operation); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2308 | if (listener != null) { |
| 2309 | pw.print(prefix); pw.print("listener="); pw.println(listener.asBinder()); |
| 2310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | } |
| 2312 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2313 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2314 | void recordWakeupAlarms(ArrayList<Batch> batches, long nowELAPSED, long nowRTC) { |
| 2315 | final int numBatches = batches.size(); |
Christopher Tate | e982faf | 2013-07-19 14:51:44 -0700 | [diff] [blame] | 2316 | for (int nextBatch = 0; nextBatch < numBatches; nextBatch++) { |
| 2317 | Batch b = batches.get(nextBatch); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2318 | if (b.start > nowELAPSED) { |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2319 | break; |
| 2320 | } |
| 2321 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2322 | final int numAlarms = b.alarms.size(); |
Christopher Tate | e982faf | 2013-07-19 14:51:44 -0700 | [diff] [blame] | 2323 | for (int nextAlarm = 0; nextAlarm < numAlarms; nextAlarm++) { |
| 2324 | Alarm a = b.alarms.get(nextAlarm); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2325 | mRecentWakeups.add(a.makeWakeupEvent(nowRTC)); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2326 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2327 | } |
| 2328 | } |
| 2329 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2330 | long currentNonWakeupFuzzLocked(long nowELAPSED) { |
| 2331 | long timeSinceOn = nowELAPSED - mNonInteractiveStartTime; |
| 2332 | if (timeSinceOn < 5*60*1000) { |
| 2333 | // If the screen has been off for 5 minutes, only delay by at most two minutes. |
| 2334 | return 2*60*1000; |
| 2335 | } else if (timeSinceOn < 30*60*1000) { |
| 2336 | // If the screen has been off for 30 minutes, only delay by at most 15 minutes. |
| 2337 | return 15*60*1000; |
| 2338 | } else { |
| 2339 | // Otherwise, we will delay by at most an hour. |
| 2340 | return 60*60*1000; |
| 2341 | } |
| 2342 | } |
| 2343 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2344 | static int fuzzForDuration(long duration) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2345 | if (duration < 15*60*1000) { |
| 2346 | // If the duration until the time is less than 15 minutes, the maximum fuzz |
| 2347 | // is the duration. |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2348 | return (int)duration; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2349 | } else if (duration < 90*60*1000) { |
| 2350 | // If duration is less than 1 1/2 hours, the maximum fuzz is 15 minutes, |
| 2351 | return 15*60*1000; |
| 2352 | } else { |
| 2353 | // Otherwise, we will fuzz by at most half an hour. |
| 2354 | return 30*60*1000; |
| 2355 | } |
| 2356 | } |
| 2357 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2358 | boolean checkAllowNonWakeupDelayLocked(long nowELAPSED) { |
| 2359 | if (mInteractive) { |
| 2360 | return false; |
| 2361 | } |
| 2362 | if (mLastAlarmDeliveryTime <= 0) { |
| 2363 | return false; |
| 2364 | } |
minho.choo | 649acab | 2014-12-12 16:13:55 +0900 | [diff] [blame] | 2365 | if (mPendingNonWakeupAlarms.size() > 0 && mNextNonWakeupDeliveryTime < nowELAPSED) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2366 | // This is just a little paranoia, if somehow we have pending non-wakeup alarms |
| 2367 | // and the next delivery time is in the past, then just deliver them all. This |
| 2368 | // avoids bugs where we get stuck in a loop trying to poll for alarms. |
| 2369 | return false; |
| 2370 | } |
| 2371 | long timeSinceLast = nowELAPSED - mLastAlarmDeliveryTime; |
| 2372 | return timeSinceLast <= currentNonWakeupFuzzLocked(nowELAPSED); |
| 2373 | } |
| 2374 | |
| 2375 | void deliverAlarmsLocked(ArrayList<Alarm> triggerList, long nowELAPSED) { |
| 2376 | mLastAlarmDeliveryTime = nowELAPSED; |
| 2377 | for (int i=0; i<triggerList.size(); i++) { |
| 2378 | Alarm alarm = triggerList.get(i); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2379 | final boolean allowWhileIdle = (alarm.flags&AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2380 | try { |
Christopher Tate | 2ff5a73 | 2014-09-18 13:47:57 -0700 | [diff] [blame] | 2381 | if (localLOGV) { |
| 2382 | Slog.v(TAG, "sending alarm " + alarm); |
| 2383 | } |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 2384 | if (RECORD_ALARMS_IN_HISTORY) { |
| 2385 | if (alarm.workSource != null && alarm.workSource.size() > 0) { |
| 2386 | for (int wi=0; wi<alarm.workSource.size(); wi++) { |
| 2387 | ActivityManagerNative.noteAlarmStart( |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2388 | alarm.operation, alarm.workSource.get(wi), alarm.statsTag); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 2389 | } |
| 2390 | } else { |
| 2391 | ActivityManagerNative.noteAlarmStart( |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2392 | alarm.operation, alarm.uid, alarm.statsTag); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 2393 | } |
| 2394 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2395 | mDeliveryTracker.deliverLocked(alarm, nowELAPSED, allowWhileIdle); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2396 | } catch (RuntimeException e) { |
| 2397 | Slog.w(TAG, "Failure sending alarm.", e); |
| 2398 | } |
| 2399 | } |
| 2400 | } |
| 2401 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2402 | private class AlarmThread extends Thread |
| 2403 | { |
| 2404 | public AlarmThread() |
| 2405 | { |
| 2406 | super("AlarmManager"); |
| 2407 | } |
| 2408 | |
| 2409 | public void run() |
| 2410 | { |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2411 | ArrayList<Alarm> triggerList = new ArrayList<Alarm>(); |
| 2412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2413 | while (true) |
| 2414 | { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 2415 | int result = waitForAlarm(mNativeData); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2416 | |
| 2417 | triggerList.clear(); |
| 2418 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2419 | final long nowRTC = System.currentTimeMillis(); |
| 2420 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 2421 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2422 | if ((result & TIME_CHANGED_MASK) != 0) { |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2423 | // The kernel can give us spurious time change notifications due to |
| 2424 | // small adjustments it makes internally; we want to filter those out. |
| 2425 | final long lastTimeChangeClockTime; |
| 2426 | final long expectedClockTime; |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 2427 | synchronized (mLock) { |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2428 | lastTimeChangeClockTime = mLastTimeChangeClockTime; |
| 2429 | expectedClockTime = lastTimeChangeClockTime |
| 2430 | + (nowELAPSED - mLastTimeChangeRealtime); |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 2431 | } |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2432 | if (lastTimeChangeClockTime == 0 || nowRTC < (expectedClockTime-500) |
| 2433 | || nowRTC > (expectedClockTime+500)) { |
| 2434 | // The change is by at least +/- 500 ms (or this is the first change), |
| 2435 | // let's do it! |
| 2436 | if (DEBUG_BATCH) { |
| 2437 | Slog.v(TAG, "Time changed notification from kernel; rebatching"); |
| 2438 | } |
| 2439 | removeImpl(mTimeTickSender); |
| 2440 | rebatchAllAlarms(); |
| 2441 | mClockReceiver.scheduleTimeTickEvent(); |
| 2442 | synchronized (mLock) { |
| 2443 | mNumTimeChanged++; |
| 2444 | mLastTimeChangeClockTime = nowRTC; |
| 2445 | mLastTimeChangeRealtime = nowELAPSED; |
| 2446 | } |
| 2447 | Intent intent = new Intent(Intent.ACTION_TIME_CHANGED); |
| 2448 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
| 2449 | | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 2450 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2451 | |
| 2452 | // The world has changed on us, so we need to re-evaluate alarms |
| 2453 | // regardless of whether the kernel has told us one went off. |
| 2454 | result |= IS_WAKEUP_MASK; |
| 2455 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2456 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2457 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2458 | if (result != TIME_CHANGED_MASK) { |
| 2459 | // If this was anything besides just a time change, then figure what if |
| 2460 | // anything to do about alarms. |
| 2461 | synchronized (mLock) { |
| 2462 | if (localLOGV) Slog.v( |
| 2463 | TAG, "Checking for alarms... rtc=" + nowRTC |
| 2464 | + ", elapsed=" + nowELAPSED); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2465 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2466 | if (WAKEUP_STATS) { |
| 2467 | if ((result & IS_WAKEUP_MASK) != 0) { |
| 2468 | long newEarliest = nowRTC - RECENT_WAKEUP_PERIOD; |
| 2469 | int n = 0; |
| 2470 | for (WakeupEvent event : mRecentWakeups) { |
| 2471 | if (event.when > newEarliest) break; |
| 2472 | n++; // number of now-stale entries at the list head |
| 2473 | } |
| 2474 | for (int i = 0; i < n; i++) { |
| 2475 | mRecentWakeups.remove(); |
| 2476 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2477 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2478 | recordWakeupAlarms(mAlarmBatches, nowELAPSED, nowRTC); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2479 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2480 | } |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2481 | |
| 2482 | boolean hasWakeup = triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC); |
| 2483 | if (!hasWakeup && checkAllowNonWakeupDelayLocked(nowELAPSED)) { |
| 2484 | // if there are no wakeup alarms and the screen is off, we can |
| 2485 | // delay what we have so far until the future. |
| 2486 | if (mPendingNonWakeupAlarms.size() == 0) { |
| 2487 | mStartCurrentDelayTime = nowELAPSED; |
| 2488 | mNextNonWakeupDeliveryTime = nowELAPSED |
| 2489 | + ((currentNonWakeupFuzzLocked(nowELAPSED)*3)/2); |
| 2490 | } |
| 2491 | mPendingNonWakeupAlarms.addAll(triggerList); |
| 2492 | mNumDelayedAlarms += triggerList.size(); |
| 2493 | rescheduleKernelAlarmsLocked(); |
| 2494 | updateNextAlarmClockLocked(); |
| 2495 | } else { |
| 2496 | // now deliver the alarm intents; if there are pending non-wakeup |
| 2497 | // alarms, we need to merge them in to the list. note we don't |
| 2498 | // just deliver them first because we generally want non-wakeup |
| 2499 | // alarms delivered after wakeup alarms. |
| 2500 | rescheduleKernelAlarmsLocked(); |
| 2501 | updateNextAlarmClockLocked(); |
| 2502 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 2503 | calculateDeliveryPriorities(mPendingNonWakeupAlarms); |
| 2504 | triggerList.addAll(mPendingNonWakeupAlarms); |
| 2505 | Collections.sort(triggerList, mAlarmDispatchComparator); |
| 2506 | final long thisDelayTime = nowELAPSED - mStartCurrentDelayTime; |
| 2507 | mTotalDelayTime += thisDelayTime; |
| 2508 | if (mMaxDelayTime < thisDelayTime) { |
| 2509 | mMaxDelayTime = thisDelayTime; |
| 2510 | } |
| 2511 | mPendingNonWakeupAlarms.clear(); |
| 2512 | } |
| 2513 | deliverAlarmsLocked(triggerList, nowELAPSED); |
| 2514 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | } |
| 2519 | } |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 2520 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2521 | /** |
| 2522 | * Attribute blame for a WakeLock. |
| 2523 | * @param pi PendingIntent to attribute blame to if ws is null. |
| 2524 | * @param ws WorkSource to attribute blame. |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2525 | * @param knownUid attribution uid; < 0 if we need to derive it from the PendingIntent sender |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2526 | */ |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2527 | void setWakelockWorkSource(PendingIntent pi, WorkSource ws, int type, String tag, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2528 | int knownUid, boolean first) { |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 2529 | try { |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 2530 | final boolean unimportant = pi == mTimeTickSender; |
| 2531 | mWakeLock.setUnimportantForLogging(unimportant); |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 2532 | if (first || mLastWakeLockUnimportantForLogging) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2533 | mWakeLock.setHistoryTag(tag); |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 2534 | } else { |
| 2535 | mWakeLock.setHistoryTag(null); |
| 2536 | } |
| 2537 | mLastWakeLockUnimportantForLogging = unimportant; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2538 | if (ws != null) { |
| 2539 | mWakeLock.setWorkSource(ws); |
| 2540 | return; |
| 2541 | } |
| 2542 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2543 | final int uid = (knownUid >= 0) |
| 2544 | ? knownUid |
| 2545 | : ActivityManagerNative.getDefault().getUidForIntentSender(pi.getTarget()); |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 2546 | if (uid >= 0) { |
| 2547 | mWakeLock.setWorkSource(new WorkSource(uid)); |
| 2548 | return; |
| 2549 | } |
| 2550 | } catch (Exception e) { |
| 2551 | } |
| 2552 | |
| 2553 | // Something went wrong; fall back to attributing the lock to the OS |
| 2554 | mWakeLock.setWorkSource(null); |
| 2555 | } |
| 2556 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2557 | private class AlarmHandler extends Handler { |
| 2558 | public static final int ALARM_EVENT = 1; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2559 | public static final int SEND_NEXT_ALARM_CLOCK_CHANGED = 2; |
| 2560 | public static final int LISTENER_TIMEOUT = 3; |
| 2561 | public static final int REPORT_ALARMS_ACTIVE = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2562 | |
| 2563 | public AlarmHandler() { |
| 2564 | } |
| 2565 | |
| 2566 | public void handleMessage(Message msg) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2567 | switch (msg.what) { |
| 2568 | case ALARM_EVENT: { |
| 2569 | ArrayList<Alarm> triggerList = new ArrayList<Alarm>(); |
| 2570 | synchronized (mLock) { |
| 2571 | final long nowRTC = System.currentTimeMillis(); |
| 2572 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 2573 | triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC); |
| 2574 | updateNextAlarmClockLocked(); |
| 2575 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2576 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2577 | // now trigger the alarms without the lock held |
| 2578 | for (int i=0; i<triggerList.size(); i++) { |
| 2579 | Alarm alarm = triggerList.get(i); |
| 2580 | try { |
| 2581 | alarm.operation.send(); |
| 2582 | } catch (PendingIntent.CanceledException e) { |
| 2583 | if (alarm.repeatInterval > 0) { |
| 2584 | // This IntentSender is no longer valid, but this |
| 2585 | // is a repeating alarm, so toss the hoser. |
| 2586 | removeImpl(alarm.operation); |
| 2587 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2588 | } |
| 2589 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2590 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2591 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2592 | |
| 2593 | case SEND_NEXT_ALARM_CLOCK_CHANGED: |
| 2594 | sendNextAlarmClockChanged(); |
| 2595 | break; |
| 2596 | |
| 2597 | case LISTENER_TIMEOUT: |
| 2598 | mDeliveryTracker.alarmTimedOut((IBinder) msg.obj); |
| 2599 | break; |
| 2600 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2601 | case REPORT_ALARMS_ACTIVE: |
| 2602 | if (mLocalDeviceIdleController != null) { |
| 2603 | mLocalDeviceIdleController.setAlarmsActive(msg.arg1 != 0); |
| 2604 | } |
| 2605 | break; |
| 2606 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2607 | default: |
| 2608 | // nope, just ignore it |
| 2609 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2610 | } |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | class ClockReceiver extends BroadcastReceiver { |
| 2615 | public ClockReceiver() { |
| 2616 | IntentFilter filter = new IntentFilter(); |
| 2617 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 2618 | filter.addAction(Intent.ACTION_DATE_CHANGED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2619 | getContext().registerReceiver(this, filter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | @Override |
| 2623 | public void onReceive(Context context, Intent intent) { |
| 2624 | if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 2625 | if (DEBUG_BATCH) { |
| 2626 | Slog.v(TAG, "Received TIME_TICK alarm; rescheduling"); |
| 2627 | } |
| 2628 | scheduleTimeTickEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2629 | } else if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) { |
| 2630 | // Since the kernel does not keep track of DST, we need to |
| 2631 | // reset the TZ information at the beginning of each day |
| 2632 | // based off of the current Zone gmt offset + userspace tracked |
| 2633 | // daylight savings information. |
| 2634 | TimeZone zone = TimeZone.getTimeZone(SystemProperties.get(TIMEZONE_PROPERTY)); |
Lavettacn Xiao | c84cc4f | 2010-08-30 12:47:23 +0200 | [diff] [blame] | 2635 | int gmtOffset = zone.getOffset(System.currentTimeMillis()); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 2636 | setKernelTimezone(mNativeData, -(gmtOffset / 60000)); |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 2637 | scheduleDateChangedEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | public void scheduleTimeTickEvent() { |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 2642 | final long currentTime = System.currentTimeMillis(); |
Sungmin Choi | 563914a | 2013-01-10 17:28:40 +0900 | [diff] [blame] | 2643 | final long nextTime = 60000 * ((currentTime / 60000) + 1); |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 2644 | |
| 2645 | // Schedule this event for the amount of time that it would take to get to |
| 2646 | // the top of the next minute. |
Sungmin Choi | 563914a | 2013-01-10 17:28:40 +0900 | [diff] [blame] | 2647 | final long tickEventDelay = nextTime - currentTime; |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 2648 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2649 | final WorkSource workSource = null; // Let system take blame for time tick events. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2650 | setImpl(ELAPSED_REALTIME, SystemClock.elapsedRealtime() + tickEventDelay, 0, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2651 | 0, mTimeTickSender, null, null, AlarmManager.FLAG_STANDALONE, workSource, |
| 2652 | null, Process.myUid(), "android"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2653 | } |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 2654 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2655 | public void scheduleDateChangedEvent() { |
| 2656 | Calendar calendar = Calendar.getInstance(); |
| 2657 | calendar.setTimeInMillis(System.currentTimeMillis()); |
| 2658 | calendar.set(Calendar.HOUR, 0); |
| 2659 | calendar.set(Calendar.MINUTE, 0); |
| 2660 | calendar.set(Calendar.SECOND, 0); |
| 2661 | calendar.set(Calendar.MILLISECOND, 0); |
| 2662 | calendar.add(Calendar.DAY_OF_MONTH, 1); |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2663 | |
| 2664 | final WorkSource workSource = null; // Let system take blame for date change events. |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2665 | setImpl(RTC, calendar.getTimeInMillis(), 0, 0, mDateChangeSender, null, null, |
| 2666 | AlarmManager.FLAG_STANDALONE, workSource, null, |
| 2667 | Process.myUid(), "android"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2668 | } |
| 2669 | } |
| 2670 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2671 | class InteractiveStateReceiver extends BroadcastReceiver { |
| 2672 | public InteractiveStateReceiver() { |
| 2673 | IntentFilter filter = new IntentFilter(); |
| 2674 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 2675 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 2676 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 2677 | getContext().registerReceiver(this, filter); |
| 2678 | } |
| 2679 | |
| 2680 | @Override |
| 2681 | public void onReceive(Context context, Intent intent) { |
| 2682 | synchronized (mLock) { |
| 2683 | interactiveStateChangedLocked(Intent.ACTION_SCREEN_ON.equals(intent.getAction())); |
| 2684 | } |
| 2685 | } |
| 2686 | } |
| 2687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2688 | class UninstallReceiver extends BroadcastReceiver { |
| 2689 | public UninstallReceiver() { |
| 2690 | IntentFilter filter = new IntentFilter(); |
| 2691 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 2692 | filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2693 | filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2694 | filter.addDataScheme("package"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2695 | getContext().registerReceiver(this, filter); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2696 | // Register for events related to sdcard installation. |
| 2697 | IntentFilter sdFilter = new IntentFilter(); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 2698 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2699 | sdFilter.addAction(Intent.ACTION_USER_STOPPED); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2700 | sdFilter.addAction(Intent.ACTION_UID_REMOVED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2701 | getContext().registerReceiver(this, sdFilter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | @Override |
| 2705 | public void onReceive(Context context, Intent intent) { |
| 2706 | synchronized (mLock) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2707 | String action = intent.getAction(); |
| 2708 | String pkgList[] = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2709 | if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) { |
| 2710 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 2711 | for (String packageName : pkgList) { |
| 2712 | if (lookForPackageLocked(packageName)) { |
| 2713 | setResultCode(Activity.RESULT_OK); |
| 2714 | return; |
| 2715 | } |
| 2716 | } |
| 2717 | return; |
| 2718 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2719 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2720 | } else if (Intent.ACTION_USER_STOPPED.equals(action)) { |
| 2721 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 2722 | if (userHandle >= 0) { |
| 2723 | removeUserLocked(userHandle); |
| 2724 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2725 | } else if (Intent.ACTION_UID_REMOVED.equals(action)) { |
| 2726 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 2727 | if (uid >= 0) { |
| 2728 | mLastAllowWhileIdleDispatch.delete(uid); |
| 2729 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2730 | } else { |
Dianne Hackborn | 409578f | 2010-03-10 17:23:43 -0800 | [diff] [blame] | 2731 | if (Intent.ACTION_PACKAGE_REMOVED.equals(action) |
| 2732 | && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 2733 | // This package is being updated; don't kill its alarms. |
| 2734 | return; |
| 2735 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2736 | Uri data = intent.getData(); |
| 2737 | if (data != null) { |
| 2738 | String pkg = data.getSchemeSpecificPart(); |
| 2739 | if (pkg != null) { |
| 2740 | pkgList = new String[]{pkg}; |
| 2741 | } |
| 2742 | } |
| 2743 | } |
| 2744 | if (pkgList != null && (pkgList.length > 0)) { |
| 2745 | for (String pkg : pkgList) { |
| 2746 | removeLocked(pkg); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 2747 | mPriorities.remove(pkg); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2748 | for (int i=mBroadcastStats.size()-1; i>=0; i--) { |
| 2749 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(i); |
| 2750 | if (uidStats.remove(pkg) != null) { |
| 2751 | if (uidStats.size() <= 0) { |
| 2752 | mBroadcastStats.removeAt(i); |
| 2753 | } |
| 2754 | } |
| 2755 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2761 | |
| 2762 | final class UidObserver extends IUidObserver.Stub { |
| 2763 | @Override public void onUidStateChanged(int uid, int procState) throws RemoteException { |
| 2764 | } |
| 2765 | |
| 2766 | @Override public void onUidGone(int uid) throws RemoteException { |
| 2767 | } |
| 2768 | |
| 2769 | @Override public void onUidActive(int uid) throws RemoteException { |
| 2770 | } |
| 2771 | |
| 2772 | @Override public void onUidIdle(int uid) throws RemoteException { |
Dianne Hackborn | de9c48b | 2015-11-20 14:49:59 -0800 | [diff] [blame] | 2773 | synchronized (mLock) { |
| 2774 | removeForStoppedLocked(uid); |
| 2775 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2776 | } |
| 2777 | }; |
| 2778 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2779 | private final BroadcastStats getStatsLocked(PendingIntent pi) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2780 | String pkg = pi.getCreatorPackage(); |
| 2781 | int uid = pi.getCreatorUid(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2782 | return getStatsLocked(uid, pkg); |
| 2783 | } |
| 2784 | |
| 2785 | private final BroadcastStats getStatsLocked(int uid, String pkgName) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2786 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.get(uid); |
| 2787 | if (uidStats == null) { |
| 2788 | uidStats = new ArrayMap<String, BroadcastStats>(); |
| 2789 | mBroadcastStats.put(uid, uidStats); |
| 2790 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2791 | BroadcastStats bs = uidStats.get(pkgName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2792 | if (bs == null) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2793 | bs = new BroadcastStats(uid, pkgName); |
| 2794 | uidStats.put(pkgName, bs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2795 | } |
| 2796 | return bs; |
| 2797 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2798 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2799 | class DeliveryTracker extends IAlarmCompleteListener.Stub implements PendingIntent.OnFinished { |
| 2800 | private InFlight removeLocked(PendingIntent pi, Intent intent) { |
| 2801 | for (int i = 0; i < mInFlight.size(); i++) { |
| 2802 | if (mInFlight.get(i).mPendingIntent == pi) { |
| 2803 | return mInFlight.remove(i); |
| 2804 | } |
| 2805 | } |
| 2806 | mLog.w("No in-flight alarm for " + pi + " " + intent); |
| 2807 | return null; |
| 2808 | } |
| 2809 | |
| 2810 | private InFlight removeLocked(IBinder listener) { |
| 2811 | for (int i = 0; i < mInFlight.size(); i++) { |
| 2812 | if (mInFlight.get(i).mListener == listener) { |
| 2813 | return mInFlight.remove(i); |
| 2814 | } |
| 2815 | } |
| 2816 | mLog.w("No in-flight alarm for listener " + listener); |
| 2817 | return null; |
| 2818 | } |
| 2819 | |
| 2820 | private void updateStatsLocked(InFlight inflight) { |
| 2821 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 2822 | BroadcastStats bs = inflight.mBroadcastStats; |
| 2823 | bs.nesting--; |
| 2824 | if (bs.nesting <= 0) { |
| 2825 | bs.nesting = 0; |
| 2826 | bs.aggregateTime += nowELAPSED - bs.startTime; |
| 2827 | } |
| 2828 | FilterStats fs = inflight.mFilterStats; |
| 2829 | fs.nesting--; |
| 2830 | if (fs.nesting <= 0) { |
| 2831 | fs.nesting = 0; |
| 2832 | fs.aggregateTime += nowELAPSED - fs.startTime; |
| 2833 | } |
| 2834 | if (RECORD_ALARMS_IN_HISTORY) { |
| 2835 | if (inflight.mWorkSource != null && inflight.mWorkSource.size() > 0) { |
| 2836 | for (int wi=0; wi<inflight.mWorkSource.size(); wi++) { |
| 2837 | ActivityManagerNative.noteAlarmFinish( |
| 2838 | inflight.mPendingIntent, inflight.mWorkSource.get(wi), inflight.mTag); |
| 2839 | } |
| 2840 | } else { |
| 2841 | ActivityManagerNative.noteAlarmFinish( |
| 2842 | inflight.mPendingIntent, inflight.mUid, inflight.mTag); |
| 2843 | } |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | private void updateTrackingLocked(InFlight inflight) { |
| 2848 | if (inflight != null) { |
| 2849 | updateStatsLocked(inflight); |
| 2850 | } |
| 2851 | mBroadcastRefCount--; |
| 2852 | if (mBroadcastRefCount == 0) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2853 | mHandler.obtainMessage(AlarmHandler.REPORT_ALARMS_ACTIVE, 0).sendToTarget(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2854 | mWakeLock.release(); |
| 2855 | if (mInFlight.size() > 0) { |
| 2856 | mLog.w("Finished all dispatches with " + mInFlight.size() |
| 2857 | + " remaining inflights"); |
| 2858 | for (int i=0; i<mInFlight.size(); i++) { |
| 2859 | mLog.w(" Remaining #" + i + ": " + mInFlight.get(i)); |
| 2860 | } |
| 2861 | mInFlight.clear(); |
| 2862 | } |
| 2863 | } else { |
| 2864 | // the next of our alarms is now in flight. reattribute the wakelock. |
| 2865 | if (mInFlight.size() > 0) { |
| 2866 | InFlight inFlight = mInFlight.get(0); |
| 2867 | setWakelockWorkSource(inFlight.mPendingIntent, inFlight.mWorkSource, |
| 2868 | inFlight.mAlarmType, inFlight.mTag, -1, false); |
| 2869 | } else { |
| 2870 | // should never happen |
| 2871 | mLog.w("Alarm wakelock still held but sent queue empty"); |
| 2872 | mWakeLock.setWorkSource(null); |
| 2873 | } |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | /** |
| 2878 | * Callback that arrives when a direct-call alarm reports that delivery has finished |
| 2879 | */ |
| 2880 | @Override |
| 2881 | public void alarmComplete(IBinder who) { |
| 2882 | if (who == null) { |
| 2883 | Slog.w(TAG, "Invalid alarmComplete: uid=" + Binder.getCallingUid() |
| 2884 | + " pid=" + Binder.getCallingPid()); |
| 2885 | return; |
| 2886 | } |
| 2887 | |
| 2888 | final long ident = Binder.clearCallingIdentity(); |
| 2889 | try { |
| 2890 | synchronized (mLock) { |
| 2891 | mHandler.removeMessages(AlarmHandler.LISTENER_TIMEOUT, who); |
| 2892 | InFlight inflight = removeLocked(who); |
| 2893 | if (inflight != null) { |
| 2894 | if (DEBUG_LISTENER_CALLBACK) { |
| 2895 | Slog.i(TAG, "alarmComplete() from " + who); |
| 2896 | } |
| 2897 | updateTrackingLocked(inflight); |
| 2898 | } else { |
| 2899 | // Delivery timed out, and the timeout handling already took care of |
| 2900 | // updating our tracking here, so we needn't do anything further. |
| 2901 | if (DEBUG_LISTENER_CALLBACK) { |
| 2902 | Slog.i(TAG, "Late alarmComplete() from " + who); |
| 2903 | } |
| 2904 | } |
| 2905 | } |
| 2906 | } finally { |
| 2907 | Binder.restoreCallingIdentity(ident); |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | /** |
| 2912 | * Callback that arrives when a PendingIntent alarm has finished delivery |
| 2913 | */ |
| 2914 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2915 | public void onSendFinished(PendingIntent pi, Intent intent, int resultCode, |
| 2916 | String resultData, Bundle resultExtras) { |
| 2917 | synchronized (mLock) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2918 | updateTrackingLocked(removeLocked(pi, intent)); |
| 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | /** |
| 2923 | * Timeout of a direct-call alarm delivery |
| 2924 | */ |
| 2925 | public void alarmTimedOut(IBinder who) { |
| 2926 | synchronized (mLock) { |
| 2927 | InFlight inflight = removeLocked(who); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2928 | if (inflight != null) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2929 | // TODO: implement ANR policy for the target |
| 2930 | if (DEBUG_LISTENER_CALLBACK) { |
| 2931 | Slog.i(TAG, "Alarm listener " + who + " timed out in delivery"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2932 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2933 | updateTrackingLocked(inflight); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2934 | } else { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2935 | if (DEBUG_LISTENER_CALLBACK) { |
| 2936 | Slog.i(TAG, "Spurious timeout of listener " + who); |
| 2937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2938 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2939 | } |
| 2940 | } |
| 2941 | |
| 2942 | /** |
| 2943 | * Deliver an alarm and set up the post-delivery handling appropriately |
| 2944 | */ |
| 2945 | public void deliverLocked(Alarm alarm, long nowELAPSED, boolean allowWhileIdle) { |
| 2946 | if (alarm.operation != null) { |
| 2947 | // PendingIntent alarm |
| 2948 | try { |
| 2949 | alarm.operation.send(getContext(), 0, |
| 2950 | mBackgroundIntent.putExtra( |
| 2951 | Intent.EXTRA_ALARM_COUNT, alarm.count), |
| 2952 | mDeliveryTracker, mHandler, null, |
| 2953 | allowWhileIdle ? mIdleOptions : null); |
| 2954 | } catch (PendingIntent.CanceledException e) { |
| 2955 | if (alarm.repeatInterval > 0) { |
| 2956 | // This IntentSender is no longer valid, but this |
| 2957 | // is a repeating alarm, so toss it |
| 2958 | removeImpl(alarm.operation); |
| 2959 | } |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 2960 | // No actual delivery was possible, so the delivery tracker's |
| 2961 | // 'finished' callback won't be invoked. We also don't need |
| 2962 | // to do any wakelock or stats tracking, so we have nothing |
| 2963 | // left to do here but go on to the next thing. |
| 2964 | return; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2965 | } |
| 2966 | } else { |
| 2967 | // Direct listener callback alarm |
| 2968 | try { |
| 2969 | if (DEBUG_LISTENER_CALLBACK) { |
| 2970 | Slog.v(TAG, "Alarm to uid=" + alarm.uid |
| 2971 | + " listener=" + alarm.listener.asBinder()); |
| 2972 | } |
| 2973 | alarm.listener.doAlarm(this); |
| 2974 | mHandler.sendMessageDelayed( |
| 2975 | mHandler.obtainMessage(AlarmHandler.LISTENER_TIMEOUT, |
| 2976 | alarm.listener.asBinder()), |
| 2977 | mConstants.LISTENER_TIMEOUT); |
| 2978 | } catch (Exception e) { |
| 2979 | if (DEBUG_LISTENER_CALLBACK) { |
| 2980 | Slog.i(TAG, "Alarm undeliverable to listener " |
| 2981 | + alarm.listener.asBinder(), e); |
| 2982 | } |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 2983 | // As in the PendingIntent.CanceledException case, delivery of the |
| 2984 | // alarm was not possible, so we have no wakelock or timeout or |
| 2985 | // stats management to do. It threw before we posted the delayed |
| 2986 | // timeout message, so we're done here. |
| 2987 | return; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | // The alarm is now in flight; now arrange wakelock and stats tracking |
| 2992 | if (mBroadcastRefCount == 0) { |
| 2993 | setWakelockWorkSource(alarm.operation, alarm.workSource, |
| 2994 | alarm.type, alarm.statsTag, (alarm.operation == null) ? alarm.uid : -1, |
| 2995 | true); |
| 2996 | mWakeLock.acquire(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2997 | mHandler.obtainMessage(AlarmHandler.REPORT_ALARMS_ACTIVE, 1).sendToTarget(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2998 | } |
| 2999 | final InFlight inflight = new InFlight(AlarmManagerService.this, |
| 3000 | alarm.operation, alarm.listener, alarm.workSource, alarm.uid, |
| 3001 | alarm.packageName, alarm.type, alarm.statsTag, nowELAPSED); |
| 3002 | mInFlight.add(inflight); |
| 3003 | mBroadcastRefCount++; |
| 3004 | |
| 3005 | if (allowWhileIdle) { |
| 3006 | // Record the last time this uid handled an ALLOW_WHILE_IDLE alarm. |
| 3007 | mLastAllowWhileIdleDispatch.put(alarm.uid, nowELAPSED); |
| 3008 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 3009 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 3010 | ent.uid = alarm.uid; |
| 3011 | ent.pkg = alarm.packageName; |
| 3012 | ent.tag = alarm.statsTag; |
| 3013 | ent.op = "DELIVER"; |
| 3014 | ent.elapsedRealtime = nowELAPSED; |
| 3015 | mAllowWhileIdleDispatches.add(ent); |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | final BroadcastStats bs = inflight.mBroadcastStats; |
| 3020 | bs.count++; |
| 3021 | if (bs.nesting == 0) { |
| 3022 | bs.nesting = 1; |
| 3023 | bs.startTime = nowELAPSED; |
| 3024 | } else { |
| 3025 | bs.nesting++; |
| 3026 | } |
| 3027 | final FilterStats fs = inflight.mFilterStats; |
| 3028 | fs.count++; |
| 3029 | if (fs.nesting == 0) { |
| 3030 | fs.nesting = 1; |
| 3031 | fs.startTime = nowELAPSED; |
| 3032 | } else { |
| 3033 | fs.nesting++; |
| 3034 | } |
| 3035 | if (alarm.type == ELAPSED_REALTIME_WAKEUP |
| 3036 | || alarm.type == RTC_WAKEUP) { |
| 3037 | bs.numWakeup++; |
| 3038 | fs.numWakeup++; |
| 3039 | if (alarm.workSource != null && alarm.workSource.size() > 0) { |
| 3040 | for (int wi=0; wi<alarm.workSource.size(); wi++) { |
| 3041 | ActivityManagerNative.noteWakeupAlarm( |
| 3042 | alarm.operation, alarm.workSource.get(wi), |
| 3043 | alarm.workSource.getName(wi), alarm.statsTag); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 3044 | } |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 3045 | } else { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3046 | ActivityManagerNative.noteWakeupAlarm( |
| 3047 | alarm.operation, -1, alarm.packageName, alarm.statsTag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | } |