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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.app.ActivityManagerNative; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 21 | import android.app.AlarmManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.app.IAlarmManager; |
| 23 | import android.app.PendingIntent; |
| 24 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 25 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.net.Uri; |
| 31 | import android.os.Binder; |
| 32 | import android.os.Bundle; |
| 33 | import android.os.Handler; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 34 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.Message; |
| 36 | import android.os.PowerManager; |
| 37 | import android.os.SystemClock; |
| 38 | import android.os.SystemProperties; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 40 | import android.os.WorkSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 42 | import android.util.ArrayMap; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 43 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 44 | import android.util.Slog; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 45 | import android.util.SparseArray; |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 46 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 48 | import java.io.ByteArrayOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import java.io.FileDescriptor; |
| 50 | import java.io.PrintWriter; |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 51 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import java.util.ArrayList; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 53 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import java.util.Calendar; |
| 55 | import java.util.Collections; |
| 56 | import java.util.Comparator; |
Mike Lockwood | 1f7b413 | 2009-11-20 15:12:51 -0500 | [diff] [blame] | 57 | import java.util.Date; |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 58 | import java.util.LinkedList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import java.util.TimeZone; |
| 60 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 61 | import static android.app.AlarmManager.RTC_WAKEUP; |
| 62 | import static android.app.AlarmManager.RTC; |
| 63 | import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP; |
| 64 | import static android.app.AlarmManager.ELAPSED_REALTIME; |
| 65 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 66 | import com.android.internal.util.LocalLog; |
| 67 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 68 | class AlarmManagerService extends SystemService { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | // The threshold for how long an alarm can be late before we print a |
| 70 | // warning message. The time duration is in milliseconds. |
| 71 | private static final long LATE_ALARM_THRESHOLD = 10 * 1000; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 72 | |
| 73 | private static final int RTC_WAKEUP_MASK = 1 << RTC_WAKEUP; |
| 74 | private static final int RTC_MASK = 1 << RTC; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 75 | private static final int ELAPSED_REALTIME_WAKEUP_MASK = 1 << ELAPSED_REALTIME_WAKEUP; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 76 | private static final int ELAPSED_REALTIME_MASK = 1 << ELAPSED_REALTIME; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 77 | static final int TIME_CHANGED_MASK = 1 << 16; |
| 78 | 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] | 79 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 80 | // 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] | 81 | static final int TYPE_NONWAKEUP_MASK = 0x1; // low bit => non-wakeup |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 82 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 83 | static final String TAG = "AlarmManager"; |
| 84 | static final String ClockReceiver_TAG = "ClockReceiver"; |
| 85 | static final boolean localLOGV = false; |
| 86 | static final boolean DEBUG_BATCH = localLOGV || false; |
| 87 | static final boolean DEBUG_VALIDATE = localLOGV || false; |
| 88 | static final int ALARM_EVENT = 1; |
| 89 | static final String TIMEZONE_PROPERTY = "persist.sys.timezone"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 91 | static final Intent mBackgroundIntent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | = new Intent().addFlags(Intent.FLAG_FROM_BACKGROUND); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 93 | static final IncreasingTimeOrder sIncreasingTimeOrder = new IncreasingTimeOrder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 95 | static final boolean WAKEUP_STATS = false; |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 97 | final LocalLog mLog = new LocalLog(TAG); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 98 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 99 | final Object mLock = new Object(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 100 | |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 101 | long mNativeData; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 102 | private long mNextWakeup; |
| 103 | private long mNextNonWakeup; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 104 | int mBroadcastRefCount = 0; |
| 105 | PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 106 | boolean mLastWakeLockUnimportantForLogging; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 107 | ArrayList<InFlight> mInFlight = new ArrayList<InFlight>(); |
| 108 | final AlarmHandler mHandler = new AlarmHandler(); |
| 109 | ClockReceiver mClockReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | private UninstallReceiver mUninstallReceiver; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 111 | final ResultReceiver mResultReceiver = new ResultReceiver(); |
| 112 | PendingIntent mTimeTickSender; |
| 113 | PendingIntent mDateChangeSender; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 114 | |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 115 | class WakeupEvent { |
| 116 | public long when; |
| 117 | public int uid; |
| 118 | public String action; |
| 119 | |
| 120 | public WakeupEvent(long theTime, int theUid, String theAction) { |
| 121 | when = theTime; |
| 122 | uid = theUid; |
| 123 | action = theAction; |
| 124 | } |
| 125 | } |
| 126 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 127 | final LinkedList<WakeupEvent> mRecentWakeups = new LinkedList<WakeupEvent>(); |
| 128 | final long RECENT_WAKEUP_PERIOD = 1000L * 60 * 60 * 24; // one day |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 129 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 130 | static final class Batch { |
| 131 | long start; // These endpoints are always in ELAPSED |
| 132 | long end; |
| 133 | boolean standalone; // certain "batches" don't participate in coalescing |
| 134 | |
| 135 | final ArrayList<Alarm> alarms = new ArrayList<Alarm>(); |
| 136 | |
| 137 | Batch() { |
| 138 | start = 0; |
| 139 | end = Long.MAX_VALUE; |
| 140 | } |
| 141 | |
| 142 | Batch(Alarm seed) { |
| 143 | start = seed.whenElapsed; |
| 144 | end = seed.maxWhen; |
| 145 | alarms.add(seed); |
| 146 | } |
| 147 | |
| 148 | int size() { |
| 149 | return alarms.size(); |
| 150 | } |
| 151 | |
| 152 | Alarm get(int index) { |
| 153 | return alarms.get(index); |
| 154 | } |
| 155 | |
| 156 | boolean canHold(long whenElapsed, long maxWhen) { |
| 157 | return (end >= whenElapsed) && (start <= maxWhen); |
| 158 | } |
| 159 | |
| 160 | boolean add(Alarm alarm) { |
| 161 | boolean newStart = false; |
| 162 | // narrows the batch if necessary; presumes that canHold(alarm) is true |
| 163 | int index = Collections.binarySearch(alarms, alarm, sIncreasingTimeOrder); |
| 164 | if (index < 0) { |
| 165 | index = 0 - index - 1; |
| 166 | } |
| 167 | alarms.add(index, alarm); |
| 168 | if (DEBUG_BATCH) { |
| 169 | Slog.v(TAG, "Adding " + alarm + " to " + this); |
| 170 | } |
| 171 | if (alarm.whenElapsed > start) { |
| 172 | start = alarm.whenElapsed; |
| 173 | newStart = true; |
| 174 | } |
| 175 | if (alarm.maxWhen < end) { |
| 176 | end = alarm.maxWhen; |
| 177 | } |
| 178 | |
| 179 | if (DEBUG_BATCH) { |
| 180 | Slog.v(TAG, " => now " + this); |
| 181 | } |
| 182 | return newStart; |
| 183 | } |
| 184 | |
| 185 | boolean remove(final PendingIntent operation) { |
| 186 | boolean didRemove = false; |
| 187 | long newStart = 0; // recalculate endpoints as we go |
| 188 | long newEnd = Long.MAX_VALUE; |
Christopher Tate | ae269d5 | 2013-09-26 13:11:55 -0700 | [diff] [blame] | 189 | for (int i = 0; i < alarms.size(); ) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 190 | Alarm alarm = alarms.get(i); |
| 191 | if (alarm.operation.equals(operation)) { |
| 192 | alarms.remove(i); |
| 193 | didRemove = true; |
| 194 | } else { |
| 195 | if (alarm.whenElapsed > newStart) { |
| 196 | newStart = alarm.whenElapsed; |
| 197 | } |
| 198 | if (alarm.maxWhen < newEnd) { |
| 199 | newEnd = alarm.maxWhen; |
| 200 | } |
| 201 | i++; |
| 202 | } |
| 203 | } |
| 204 | if (didRemove) { |
| 205 | // commit the new batch bounds |
| 206 | start = newStart; |
| 207 | end = newEnd; |
| 208 | } |
| 209 | return didRemove; |
| 210 | } |
| 211 | |
| 212 | boolean remove(final String packageName) { |
| 213 | boolean didRemove = false; |
| 214 | long newStart = 0; // recalculate endpoints as we go |
| 215 | long newEnd = Long.MAX_VALUE; |
Christopher Tate | ae269d5 | 2013-09-26 13:11:55 -0700 | [diff] [blame] | 216 | for (int i = 0; i < alarms.size(); ) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 217 | Alarm alarm = alarms.get(i); |
| 218 | if (alarm.operation.getTargetPackage().equals(packageName)) { |
| 219 | alarms.remove(i); |
| 220 | didRemove = true; |
| 221 | } else { |
| 222 | if (alarm.whenElapsed > newStart) { |
| 223 | newStart = alarm.whenElapsed; |
| 224 | } |
| 225 | if (alarm.maxWhen < newEnd) { |
| 226 | newEnd = alarm.maxWhen; |
| 227 | } |
| 228 | i++; |
| 229 | } |
| 230 | } |
| 231 | if (didRemove) { |
| 232 | // commit the new batch bounds |
| 233 | start = newStart; |
| 234 | end = newEnd; |
| 235 | } |
| 236 | return didRemove; |
| 237 | } |
| 238 | |
| 239 | boolean remove(final int userHandle) { |
| 240 | boolean didRemove = false; |
| 241 | long newStart = 0; // recalculate endpoints as we go |
| 242 | long newEnd = Long.MAX_VALUE; |
Christopher Tate | ae269d5 | 2013-09-26 13:11:55 -0700 | [diff] [blame] | 243 | for (int i = 0; i < alarms.size(); ) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 244 | Alarm alarm = alarms.get(i); |
| 245 | if (UserHandle.getUserId(alarm.operation.getCreatorUid()) == userHandle) { |
| 246 | alarms.remove(i); |
| 247 | didRemove = true; |
| 248 | } else { |
| 249 | if (alarm.whenElapsed > newStart) { |
| 250 | newStart = alarm.whenElapsed; |
| 251 | } |
| 252 | if (alarm.maxWhen < newEnd) { |
| 253 | newEnd = alarm.maxWhen; |
| 254 | } |
| 255 | i++; |
| 256 | } |
| 257 | } |
| 258 | if (didRemove) { |
| 259 | // commit the new batch bounds |
| 260 | start = newStart; |
| 261 | end = newEnd; |
| 262 | } |
| 263 | return didRemove; |
| 264 | } |
| 265 | |
| 266 | boolean hasPackage(final String packageName) { |
| 267 | final int N = alarms.size(); |
| 268 | for (int i = 0; i < N; i++) { |
| 269 | Alarm a = alarms.get(i); |
| 270 | if (a.operation.getTargetPackage().equals(packageName)) { |
| 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | boolean hasWakeups() { |
| 278 | final int N = alarms.size(); |
| 279 | for (int i = 0; i < N; i++) { |
| 280 | Alarm a = alarms.get(i); |
| 281 | // non-wakeup alarms are types 1 and 3, i.e. have the low bit set |
| 282 | if ((a.type & TYPE_NONWAKEUP_MASK) == 0) { |
| 283 | return true; |
| 284 | } |
| 285 | } |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public String toString() { |
| 291 | StringBuilder b = new StringBuilder(40); |
| 292 | b.append("Batch{"); b.append(Integer.toHexString(this.hashCode())); |
| 293 | b.append(" num="); b.append(size()); |
| 294 | b.append(" start="); b.append(start); |
| 295 | b.append(" end="); b.append(end); |
| 296 | if (standalone) { |
| 297 | b.append(" STANDALONE"); |
| 298 | } |
| 299 | b.append('}'); |
| 300 | return b.toString(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | static class BatchTimeOrder implements Comparator<Batch> { |
| 305 | public int compare(Batch b1, Batch b2) { |
| 306 | long when1 = b1.start; |
| 307 | long when2 = b2.start; |
| 308 | if (when1 - when2 > 0) { |
| 309 | return 1; |
| 310 | } |
| 311 | if (when1 - when2 < 0) { |
| 312 | return -1; |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 317 | |
| 318 | final Comparator<Alarm> mAlarmDispatchComparator = new Comparator<Alarm>() { |
| 319 | @Override |
| 320 | public int compare(Alarm lhs, Alarm rhs) { |
| 321 | if (lhs.wakeup != rhs.wakeup) { |
| 322 | return lhs.wakeup ? -1 : 1; |
| 323 | } |
| 324 | if (lhs.whenElapsed < rhs.whenElapsed) { |
| 325 | return -1; |
| 326 | } else if (lhs.whenElapsed > rhs.whenElapsed) { |
| 327 | return 1; |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | }; |
| 332 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 333 | // 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] | 334 | static final long MIN_FUZZABLE_INTERVAL = 10000; |
| 335 | static final BatchTimeOrder sBatchOrder = new BatchTimeOrder(); |
| 336 | final ArrayList<Batch> mAlarmBatches = new ArrayList<Batch>(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 337 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 338 | public AlarmManagerService(Context context) { |
| 339 | super(context); |
| 340 | } |
| 341 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 342 | static long convertToElapsed(long when, int type) { |
| 343 | final boolean isRtc = (type == RTC || type == RTC_WAKEUP); |
| 344 | if (isRtc) { |
| 345 | when -= System.currentTimeMillis() - SystemClock.elapsedRealtime(); |
| 346 | } |
| 347 | return when; |
| 348 | } |
| 349 | |
| 350 | // Apply a heuristic to { recurrence interval, futurity of the trigger time } to |
| 351 | // calculate the end of our nominal delivery window for the alarm. |
| 352 | static long maxTriggerTime(long now, long triggerAtTime, long interval) { |
| 353 | // Current heuristic: batchable window is 75% of either the recurrence interval |
| 354 | // [for a periodic alarm] or of the time from now to the desired delivery time, |
| 355 | // with a minimum delay/interval of 10 seconds, under which we will simply not |
| 356 | // defer the alarm. |
| 357 | long futurity = (interval == 0) |
| 358 | ? (triggerAtTime - now) |
| 359 | : interval; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 360 | if (futurity < MIN_FUZZABLE_INTERVAL) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 361 | futurity = 0; |
| 362 | } |
| 363 | return triggerAtTime + (long)(.75 * futurity); |
| 364 | } |
| 365 | |
| 366 | // returns true if the batch was added at the head |
| 367 | static boolean addBatchLocked(ArrayList<Batch> list, Batch newBatch) { |
| 368 | int index = Collections.binarySearch(list, newBatch, sBatchOrder); |
| 369 | if (index < 0) { |
| 370 | index = 0 - index - 1; |
| 371 | } |
| 372 | list.add(index, newBatch); |
| 373 | return (index == 0); |
| 374 | } |
| 375 | |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 376 | // Return the index of the matching batch, or -1 if none found. |
| 377 | int attemptCoalesceLocked(long whenElapsed, long maxWhen) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 378 | final int N = mAlarmBatches.size(); |
| 379 | for (int i = 0; i < N; i++) { |
| 380 | Batch b = mAlarmBatches.get(i); |
| 381 | if (!b.standalone && b.canHold(whenElapsed, maxWhen)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 382 | return i; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 385 | return -1; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // The RTC clock has moved arbitrarily, so we need to recalculate all the batching |
| 389 | void rebatchAllAlarms() { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 390 | synchronized (mLock) { |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 391 | rebatchAllAlarmsLocked(true); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void rebatchAllAlarmsLocked(boolean doValidate) { |
| 396 | ArrayList<Batch> oldSet = (ArrayList<Batch>) mAlarmBatches.clone(); |
| 397 | mAlarmBatches.clear(); |
| 398 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 399 | final int oldBatches = oldSet.size(); |
| 400 | for (int batchNum = 0; batchNum < oldBatches; batchNum++) { |
| 401 | Batch batch = oldSet.get(batchNum); |
| 402 | final int N = batch.size(); |
| 403 | for (int i = 0; i < N; i++) { |
| 404 | Alarm a = batch.get(i); |
| 405 | long whenElapsed = convertToElapsed(a.when, a.type); |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 406 | final long maxElapsed; |
| 407 | if (a.whenElapsed == a.maxWhen) { |
| 408 | // Exact |
| 409 | maxElapsed = whenElapsed; |
| 410 | } else { |
| 411 | // Not exact. Preserve any explicit window, otherwise recalculate |
| 412 | // the window based on the alarm's new futurity. Note that this |
| 413 | // reflects a policy of preferring timely to deferred delivery. |
| 414 | maxElapsed = (a.windowLength > 0) |
| 415 | ? (whenElapsed + a.windowLength) |
| 416 | : maxTriggerTime(nowElapsed, whenElapsed, a.repeatInterval); |
| 417 | } |
| 418 | setImplLocked(a.type, a.when, whenElapsed, a.windowLength, maxElapsed, |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 419 | a.repeatInterval, a.operation, batch.standalone, doValidate, a.workSource); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 424 | static final class InFlight extends Intent { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 425 | final PendingIntent mPendingIntent; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 426 | final WorkSource mWorkSource; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 427 | final Pair<String, ComponentName> mTarget; |
| 428 | final BroadcastStats mBroadcastStats; |
| 429 | final FilterStats mFilterStats; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 430 | final int mAlarmType; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 431 | |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 432 | InFlight(AlarmManagerService service, PendingIntent pendingIntent, WorkSource workSource, |
| 433 | int alarmType) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 434 | mPendingIntent = pendingIntent; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 435 | mWorkSource = workSource; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 436 | Intent intent = pendingIntent.getIntent(); |
| 437 | mTarget = intent != null |
| 438 | ? new Pair<String, ComponentName>(intent.getAction(), intent.getComponent()) |
| 439 | : null; |
| 440 | mBroadcastStats = service.getStatsLocked(pendingIntent); |
| 441 | FilterStats fs = mBroadcastStats.filterStats.get(mTarget); |
| 442 | if (fs == null) { |
| 443 | fs = new FilterStats(mBroadcastStats, mTarget); |
| 444 | mBroadcastStats.filterStats.put(mTarget, fs); |
| 445 | } |
| 446 | mFilterStats = fs; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 447 | mAlarmType = alarmType; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 448 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 450 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 451 | static final class FilterStats { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 452 | final BroadcastStats mBroadcastStats; |
| 453 | final Pair<String, ComponentName> mTarget; |
| 454 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | long aggregateTime; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 456 | int count; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | int numWakeup; |
| 458 | long startTime; |
| 459 | int nesting; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 460 | |
| 461 | FilterStats(BroadcastStats broadcastStats, Pair<String, ComponentName> target) { |
| 462 | mBroadcastStats = broadcastStats; |
| 463 | mTarget = target; |
| 464 | } |
| 465 | } |
| 466 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 467 | static final class BroadcastStats { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 468 | final int mUid; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 469 | final String mPackageName; |
| 470 | |
| 471 | long aggregateTime; |
| 472 | int count; |
| 473 | int numWakeup; |
| 474 | long startTime; |
| 475 | int nesting; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 476 | final ArrayMap<Pair<String, ComponentName>, FilterStats> filterStats |
| 477 | = new ArrayMap<Pair<String, ComponentName>, FilterStats>(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 478 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 479 | BroadcastStats(int uid, String packageName) { |
| 480 | mUid = uid; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 481 | mPackageName = packageName; |
| 482 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 485 | final SparseArray<ArrayMap<String, BroadcastStats>> mBroadcastStats |
| 486 | = new SparseArray<ArrayMap<String, BroadcastStats>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 488 | @Override |
| 489 | public void onStart() { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 490 | mNativeData = init(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 491 | mNextWakeup = mNextNonWakeup = 0; |
Robert CH Chou | 64ba8e4 | 2009-11-04 21:38:49 +0800 | [diff] [blame] | 492 | |
| 493 | // We have to set current TimeZone info to kernel |
| 494 | // because kernel doesn't keep this after reboot |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 495 | setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY)); |
Robert CH Chou | 64ba8e4 | 2009-11-04 21:38:49 +0800 | [diff] [blame] | 496 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 497 | PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 498 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*alarm*"); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 499 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 500 | mTimeTickSender = PendingIntent.getBroadcastAsUser(getContext(), 0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | new Intent(Intent.ACTION_TIME_TICK).addFlags( |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 502 | Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 503 | | Intent.FLAG_RECEIVER_FOREGROUND), 0, |
Dianne Hackborn | db5aca9 | 2012-10-26 13:39:41 -0700 | [diff] [blame] | 504 | UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 505 | Intent intent = new Intent(Intent.ACTION_DATE_CHANGED); |
| 506 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 507 | mDateChangeSender = PendingIntent.getBroadcastAsUser(getContext(), 0, intent, |
Dianne Hackborn | db5aca9 | 2012-10-26 13:39:41 -0700 | [diff] [blame] | 508 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | |
| 510 | // now that we have initied the driver schedule the alarm |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 511 | mClockReceiver = new ClockReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | mClockReceiver.scheduleTimeTickEvent(); |
| 513 | mClockReceiver.scheduleDateChangedEvent(); |
| 514 | mUninstallReceiver = new UninstallReceiver(); |
| 515 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 516 | if (mNativeData != 0) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 517 | AlarmThread waitThread = new AlarmThread(); |
| 518 | waitThread.start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 520 | 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] | 521 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 522 | |
| 523 | publishBinderService(Context.ALARM_SERVICE, mService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 525 | |
| 526 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | protected void finalize() throws Throwable { |
| 528 | try { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 529 | close(mNativeData); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | } finally { |
| 531 | super.finalize(); |
| 532 | } |
| 533 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 534 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 535 | void setTimeZoneImpl(String tz) { |
| 536 | if (TextUtils.isEmpty(tz)) { |
| 537 | return; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 540 | TimeZone zone = TimeZone.getTimeZone(tz); |
| 541 | // Prevent reentrant calls from stepping on each other when writing |
| 542 | // the time zone property |
| 543 | boolean timeZoneWasChanged = false; |
| 544 | synchronized (this) { |
| 545 | String current = SystemProperties.get(TIMEZONE_PROPERTY); |
| 546 | if (current == null || !current.equals(zone.getID())) { |
| 547 | if (localLOGV) { |
| 548 | Slog.v(TAG, "timezone changed: " + current + ", new=" + zone.getID()); |
| 549 | } |
| 550 | timeZoneWasChanged = true; |
| 551 | SystemProperties.set(TIMEZONE_PROPERTY, zone.getID()); |
| 552 | } |
| 553 | |
| 554 | // Update the kernel timezone information |
| 555 | // Kernel tracks time offsets as 'minutes west of GMT' |
| 556 | int gmtOffset = zone.getOffset(System.currentTimeMillis()); |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 557 | setKernelTimezone(mNativeData, -(gmtOffset / 60000)); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | TimeZone.setDefault(null); |
| 561 | |
| 562 | if (timeZoneWasChanged) { |
| 563 | Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED); |
| 564 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 565 | intent.putExtra("time-zone", zone.getID()); |
| 566 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 567 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 569 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 570 | void removeImpl(PendingIntent operation) { |
| 571 | if (operation == null) { |
| 572 | return; |
| 573 | } |
| 574 | synchronized (mLock) { |
| 575 | removeLocked(operation); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void setImpl(int type, long triggerAtTime, long windowLength, long interval, |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 580 | PendingIntent operation, boolean isStandalone, WorkSource workSource) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | if (operation == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 582 | Slog.w(TAG, "set/setRepeating ignored because there is no intent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | return; |
| 584 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 585 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 586 | // Sanity check the window length. This will catch people mistakenly |
| 587 | // trying to pass an end-of-window timestamp rather than a duration. |
| 588 | if (windowLength > AlarmManager.INTERVAL_HALF_DAY) { |
| 589 | Slog.w(TAG, "Window length " + windowLength |
| 590 | + "ms suspiciously long; limiting to 1 hour"); |
| 591 | windowLength = AlarmManager.INTERVAL_HOUR; |
| 592 | } |
| 593 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 594 | if (type < RTC_WAKEUP || type > ELAPSED_REALTIME) { |
| 595 | throw new IllegalArgumentException("Invalid alarm type " + type); |
| 596 | } |
| 597 | |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 598 | if (triggerAtTime < 0) { |
| 599 | final long who = Binder.getCallingUid(); |
| 600 | final long what = Binder.getCallingPid(); |
| 601 | Slog.w(TAG, "Invalid alarm trigger time! " + triggerAtTime + " from uid=" + who |
| 602 | + " pid=" + what); |
| 603 | triggerAtTime = 0; |
| 604 | } |
| 605 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 606 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 607 | final long triggerElapsed = convertToElapsed(triggerAtTime, type); |
| 608 | final long maxElapsed; |
| 609 | if (windowLength == AlarmManager.WINDOW_EXACT) { |
| 610 | maxElapsed = triggerElapsed; |
| 611 | } else if (windowLength < 0) { |
| 612 | maxElapsed = maxTriggerTime(nowElapsed, triggerElapsed, interval); |
| 613 | } else { |
| 614 | maxElapsed = triggerElapsed + windowLength; |
| 615 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | synchronized (mLock) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 618 | if (DEBUG_BATCH) { |
| 619 | Slog.v(TAG, "set(" + operation + ") : type=" + type |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 620 | + " triggerAtTime=" + triggerAtTime + " win=" + windowLength |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 621 | + " tElapsed=" + triggerElapsed + " maxElapsed=" + maxElapsed |
| 622 | + " interval=" + interval + " standalone=" + isStandalone); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | } |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 624 | setImplLocked(type, triggerAtTime, triggerElapsed, windowLength, maxElapsed, |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 625 | interval, operation, isStandalone, true, workSource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | } |
| 627 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 629 | private void setImplLocked(int type, long when, long whenElapsed, long windowLength, |
| 630 | long maxWhen, long interval, PendingIntent operation, boolean isStandalone, |
| 631 | boolean doValidate, WorkSource workSource) { |
| 632 | Alarm a = new Alarm(type, when, whenElapsed, windowLength, maxWhen, interval, |
| 633 | operation, workSource); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 634 | removeLocked(operation); |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 635 | |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 636 | int whichBatch = (isStandalone) ? -1 : attemptCoalesceLocked(whenElapsed, maxWhen); |
| 637 | if (whichBatch < 0) { |
| 638 | Batch batch = new Batch(a); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 639 | batch.standalone = isStandalone; |
Christopher Tate | 7d57ed8 | 2013-10-25 20:18:03 -0700 | [diff] [blame] | 640 | addBatchLocked(mAlarmBatches, batch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | } else { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 642 | Batch batch = mAlarmBatches.get(whichBatch); |
Christopher Tate | 7d57ed8 | 2013-10-25 20:18:03 -0700 | [diff] [blame] | 643 | if (batch.add(a)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 644 | // The start time of this batch advanced, so batch ordering may |
| 645 | // have just been broken. Move it to where it now belongs. |
| 646 | mAlarmBatches.remove(whichBatch); |
| 647 | addBatchLocked(mAlarmBatches, batch); |
| 648 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 651 | if (DEBUG_VALIDATE) { |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 652 | if (doValidate && !validateConsistencyLocked()) { |
| 653 | Slog.v(TAG, "Tipping-point operation: type=" + type + " when=" + when |
| 654 | + " when(hex)=" + Long.toHexString(when) |
| 655 | + " whenElapsed=" + whenElapsed + " maxWhen=" + maxWhen |
| 656 | + " interval=" + interval + " op=" + operation |
| 657 | + " standalone=" + isStandalone); |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 658 | rebatchAllAlarmsLocked(false); |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Christopher Tate | 7d57ed8 | 2013-10-25 20:18:03 -0700 | [diff] [blame] | 662 | rescheduleKernelAlarmsLocked(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 665 | private final IBinder mService = new IAlarmManager.Stub() { |
| 666 | @Override |
| 667 | public void set(int type, long triggerAtTime, long windowLength, long interval, |
| 668 | PendingIntent operation, WorkSource workSource) { |
| 669 | if (workSource != null) { |
| 670 | getContext().enforceCallingPermission( |
| 671 | android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 672 | "AlarmManager.set"); |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 675 | setImpl(type, triggerAtTime, windowLength, interval, operation, |
| 676 | false, workSource); |
| 677 | } |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 678 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 679 | @Override |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 680 | public boolean setTime(long millis) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 681 | getContext().enforceCallingOrSelfPermission( |
| 682 | "android.permission.SET_TIME", |
| 683 | "setTime"); |
| 684 | |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 685 | if (mNativeData == 0) { |
| 686 | Slog.w(TAG, "Not setting time since no alarm driver is available."); |
| 687 | return false; |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 688 | } |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 689 | |
| 690 | synchronized (mLock) { |
| 691 | return setKernelTime(mNativeData, millis) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 694 | |
| 695 | @Override |
| 696 | public void setTimeZone(String tz) { |
| 697 | getContext().enforceCallingOrSelfPermission( |
| 698 | "android.permission.SET_TIME_ZONE", |
| 699 | "setTimeZone"); |
| 700 | |
| 701 | final long oldId = Binder.clearCallingIdentity(); |
| 702 | try { |
| 703 | setTimeZoneImpl(tz); |
| 704 | } finally { |
| 705 | Binder.restoreCallingIdentity(oldId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | } |
| 707 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 708 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 709 | @Override |
| 710 | public void remove(PendingIntent operation) { |
| 711 | removeImpl(operation); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 714 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 715 | @Override |
| 716 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 717 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 718 | != PackageManager.PERMISSION_GRANTED) { |
| 719 | pw.println("Permission Denial: can't dump AlarmManager from from pid=" |
| 720 | + Binder.getCallingPid() |
| 721 | + ", uid=" + Binder.getCallingUid()); |
| 722 | return; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 723 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 724 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 725 | dumpImpl(pw); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 726 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 727 | }; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 728 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 729 | void dumpImpl(PrintWriter pw) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | synchronized (mLock) { |
| 731 | pw.println("Current Alarm Manager state:"); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 732 | final long nowRTC = System.currentTimeMillis(); |
| 733 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 734 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 735 | |
| 736 | pw.print("nowRTC="); pw.print(nowRTC); |
| 737 | pw.print("="); pw.print(sdf.format(new Date(nowRTC))); |
| 738 | pw.print(" nowELAPSED="); pw.println(nowELAPSED); |
| 739 | |
| 740 | long nextWakeupRTC = mNextWakeup + (nowRTC - nowELAPSED); |
| 741 | long nextNonWakeupRTC = mNextNonWakeup + (nowRTC - nowELAPSED); |
| 742 | pw.print("Next alarm: "); pw.print(mNextNonWakeup); |
| 743 | pw.print(" = "); pw.println(sdf.format(new Date(nextNonWakeupRTC))); |
| 744 | pw.print("Next wakeup: "); pw.print(mNextWakeup); |
| 745 | pw.print(" = "); pw.println(sdf.format(new Date(nextWakeupRTC))); |
| 746 | |
| 747 | if (mAlarmBatches.size() > 0) { |
| 748 | pw.println(); |
| 749 | pw.print("Pending alarm batches: "); |
| 750 | pw.println(mAlarmBatches.size()); |
| 751 | for (Batch b : mAlarmBatches) { |
| 752 | pw.print(b); pw.println(':'); |
| 753 | dumpAlarmList(pw, b.alarms, " ", nowELAPSED, nowRTC); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 754 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 756 | |
| 757 | pw.println(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 758 | pw.print(" Broadcast ref count: "); pw.println(mBroadcastRefCount); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 759 | pw.println(); |
| 760 | |
| 761 | if (mLog.dump(pw, " Recent problems", " ")) { |
| 762 | pw.println(); |
| 763 | } |
| 764 | |
| 765 | final FilterStats[] topFilters = new FilterStats[10]; |
| 766 | final Comparator<FilterStats> comparator = new Comparator<FilterStats>() { |
| 767 | @Override |
| 768 | public int compare(FilterStats lhs, FilterStats rhs) { |
| 769 | if (lhs.aggregateTime < rhs.aggregateTime) { |
| 770 | return 1; |
| 771 | } else if (lhs.aggregateTime > rhs.aggregateTime) { |
| 772 | return -1; |
| 773 | } |
| 774 | return 0; |
| 775 | } |
| 776 | }; |
| 777 | int len = 0; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 778 | for (int iu=0; iu<mBroadcastStats.size(); iu++) { |
| 779 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 780 | for (int ip=0; ip<uidStats.size(); ip++) { |
| 781 | BroadcastStats bs = uidStats.valueAt(ip); |
| 782 | for (int is=0; is<bs.filterStats.size(); is++) { |
| 783 | FilterStats fs = bs.filterStats.valueAt(is); |
| 784 | int pos = len > 0 |
| 785 | ? Arrays.binarySearch(topFilters, 0, len, fs, comparator) : 0; |
| 786 | if (pos < 0) { |
| 787 | pos = -pos - 1; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 788 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 789 | if (pos < topFilters.length) { |
| 790 | int copylen = topFilters.length - pos - 1; |
| 791 | if (copylen > 0) { |
| 792 | System.arraycopy(topFilters, pos, topFilters, pos+1, copylen); |
| 793 | } |
| 794 | topFilters[pos] = fs; |
| 795 | if (len < topFilters.length) { |
| 796 | len++; |
| 797 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | if (len > 0) { |
| 803 | pw.println(" Top Alarms:"); |
| 804 | for (int i=0; i<len; i++) { |
| 805 | FilterStats fs = topFilters[i]; |
| 806 | pw.print(" "); |
| 807 | if (fs.nesting > 0) pw.print("*ACTIVE* "); |
| 808 | TimeUtils.formatDuration(fs.aggregateTime, pw); |
| 809 | pw.print(" running, "); pw.print(fs.numWakeup); |
| 810 | pw.print(" wakeups, "); pw.print(fs.count); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 811 | pw.print(" alarms: "); UserHandle.formatUid(pw, fs.mBroadcastStats.mUid); |
| 812 | pw.print(":"); pw.print(fs.mBroadcastStats.mPackageName); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 813 | pw.println(); |
| 814 | pw.print(" "); |
| 815 | if (fs.mTarget.first != null) { |
| 816 | pw.print(" act="); pw.print(fs.mTarget.first); |
| 817 | } |
| 818 | if (fs.mTarget.second != null) { |
| 819 | pw.print(" cmp="); pw.print(fs.mTarget.second.toShortString()); |
| 820 | } |
| 821 | pw.println(); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | pw.println(" "); |
| 826 | pw.println(" Alarm Stats:"); |
| 827 | final ArrayList<FilterStats> tmpFilters = new ArrayList<FilterStats>(); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 828 | for (int iu=0; iu<mBroadcastStats.size(); iu++) { |
| 829 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 830 | for (int ip=0; ip<uidStats.size(); ip++) { |
| 831 | BroadcastStats bs = uidStats.valueAt(ip); |
| 832 | pw.print(" "); |
| 833 | if (bs.nesting > 0) pw.print("*ACTIVE* "); |
| 834 | UserHandle.formatUid(pw, bs.mUid); |
| 835 | pw.print(":"); |
| 836 | pw.print(bs.mPackageName); |
| 837 | pw.print(" "); TimeUtils.formatDuration(bs.aggregateTime, pw); |
| 838 | pw.print(" running, "); pw.print(bs.numWakeup); |
| 839 | pw.println(" wakeups:"); |
| 840 | tmpFilters.clear(); |
| 841 | for (int is=0; is<bs.filterStats.size(); is++) { |
| 842 | tmpFilters.add(bs.filterStats.valueAt(is)); |
| 843 | } |
| 844 | Collections.sort(tmpFilters, comparator); |
| 845 | for (int i=0; i<tmpFilters.size(); i++) { |
| 846 | FilterStats fs = tmpFilters.get(i); |
| 847 | pw.print(" "); |
| 848 | if (fs.nesting > 0) pw.print("*ACTIVE* "); |
| 849 | TimeUtils.formatDuration(fs.aggregateTime, pw); |
| 850 | pw.print(" "); pw.print(fs.numWakeup); |
| 851 | pw.print(" wakes " ); pw.print(fs.count); |
| 852 | pw.print(" alarms:"); |
| 853 | if (fs.mTarget.first != null) { |
| 854 | pw.print(" act="); pw.print(fs.mTarget.first); |
| 855 | } |
| 856 | if (fs.mTarget.second != null) { |
| 857 | pw.print(" cmp="); pw.print(fs.mTarget.second.toShortString()); |
| 858 | } |
| 859 | pw.println(); |
| 860 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | } |
| 862 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 863 | |
| 864 | if (WAKEUP_STATS) { |
| 865 | pw.println(); |
| 866 | pw.println(" Recent Wakeup History:"); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 867 | long last = -1; |
| 868 | for (WakeupEvent event : mRecentWakeups) { |
| 869 | pw.print(" "); pw.print(sdf.format(new Date(event.when))); |
| 870 | pw.print('|'); |
| 871 | if (last < 0) { |
| 872 | pw.print('0'); |
| 873 | } else { |
| 874 | pw.print(event.when - last); |
| 875 | } |
| 876 | last = event.when; |
| 877 | pw.print('|'); pw.print(event.uid); |
| 878 | pw.print('|'); pw.print(event.action); |
| 879 | pw.println(); |
| 880 | } |
| 881 | pw.println(); |
| 882 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 886 | private void logBatchesLocked() { |
| 887 | ByteArrayOutputStream bs = new ByteArrayOutputStream(2048); |
| 888 | PrintWriter pw = new PrintWriter(bs); |
| 889 | final long nowRTC = System.currentTimeMillis(); |
| 890 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 891 | final int NZ = mAlarmBatches.size(); |
| 892 | for (int iz = 0; iz < NZ; iz++) { |
| 893 | Batch bz = mAlarmBatches.get(iz); |
| 894 | pw.append("Batch "); pw.print(iz); pw.append(": "); pw.println(bz); |
| 895 | dumpAlarmList(pw, bz.alarms, " ", nowELAPSED, nowRTC); |
| 896 | pw.flush(); |
| 897 | Slog.v(TAG, bs.toString()); |
| 898 | bs.reset(); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | private boolean validateConsistencyLocked() { |
| 903 | if (DEBUG_VALIDATE) { |
| 904 | long lastTime = Long.MIN_VALUE; |
| 905 | final int N = mAlarmBatches.size(); |
| 906 | for (int i = 0; i < N; i++) { |
| 907 | Batch b = mAlarmBatches.get(i); |
| 908 | if (b.start >= lastTime) { |
| 909 | // duplicate start times are okay because of standalone batches |
| 910 | lastTime = b.start; |
| 911 | } else { |
| 912 | Slog.e(TAG, "CONSISTENCY FAILURE: Batch " + i + " is out of order"); |
| 913 | logBatchesLocked(); |
| 914 | return false; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | return true; |
| 919 | } |
| 920 | |
| 921 | private Batch findFirstWakeupBatchLocked() { |
| 922 | final int N = mAlarmBatches.size(); |
| 923 | for (int i = 0; i < N; i++) { |
| 924 | Batch b = mAlarmBatches.get(i); |
| 925 | if (b.hasWakeups()) { |
| 926 | return b; |
| 927 | } |
| 928 | } |
| 929 | return null; |
| 930 | } |
| 931 | |
| 932 | void rescheduleKernelAlarmsLocked() { |
| 933 | // Schedule the next upcoming wakeup alarm. If there is a deliverable batch |
| 934 | // prior to that which contains no wakeups, we schedule that as well. |
| 935 | if (mAlarmBatches.size() > 0) { |
| 936 | final Batch firstWakeup = findFirstWakeupBatchLocked(); |
| 937 | final Batch firstBatch = mAlarmBatches.get(0); |
| 938 | if (firstWakeup != null && mNextWakeup != firstWakeup.start) { |
| 939 | mNextWakeup = firstWakeup.start; |
| 940 | setLocked(ELAPSED_REALTIME_WAKEUP, firstWakeup.start); |
| 941 | } |
| 942 | if (firstBatch != firstWakeup && mNextNonWakeup != firstBatch.start) { |
| 943 | mNextNonWakeup = firstBatch.start; |
| 944 | setLocked(ELAPSED_REALTIME, firstBatch.start); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | private void removeLocked(PendingIntent operation) { |
| 950 | boolean didRemove = false; |
| 951 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 952 | Batch b = mAlarmBatches.get(i); |
| 953 | didRemove |= b.remove(operation); |
| 954 | if (b.size() == 0) { |
| 955 | mAlarmBatches.remove(i); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (didRemove) { |
| 960 | if (DEBUG_BATCH) { |
| 961 | Slog.v(TAG, "remove(operation) changed bounds; rebatching"); |
| 962 | } |
| 963 | rebatchAllAlarmsLocked(true); |
| 964 | rescheduleKernelAlarmsLocked(); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | void removeLocked(String packageName) { |
| 969 | boolean didRemove = false; |
| 970 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 971 | Batch b = mAlarmBatches.get(i); |
| 972 | didRemove |= b.remove(packageName); |
| 973 | if (b.size() == 0) { |
| 974 | mAlarmBatches.remove(i); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | if (didRemove) { |
| 979 | if (DEBUG_BATCH) { |
| 980 | Slog.v(TAG, "remove(package) changed bounds; rebatching"); |
| 981 | } |
| 982 | rebatchAllAlarmsLocked(true); |
| 983 | rescheduleKernelAlarmsLocked(); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | void removeUserLocked(int userHandle) { |
| 988 | boolean didRemove = false; |
| 989 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 990 | Batch b = mAlarmBatches.get(i); |
| 991 | didRemove |= b.remove(userHandle); |
| 992 | if (b.size() == 0) { |
| 993 | mAlarmBatches.remove(i); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | if (didRemove) { |
| 998 | if (DEBUG_BATCH) { |
| 999 | Slog.v(TAG, "remove(user) changed bounds; rebatching"); |
| 1000 | } |
| 1001 | rebatchAllAlarmsLocked(true); |
| 1002 | rescheduleKernelAlarmsLocked(); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | boolean lookForPackageLocked(String packageName) { |
| 1007 | for (int i = 0; i < mAlarmBatches.size(); i++) { |
| 1008 | Batch b = mAlarmBatches.get(i); |
| 1009 | if (b.hasPackage(packageName)) { |
| 1010 | return true; |
| 1011 | } |
| 1012 | } |
| 1013 | return false; |
| 1014 | } |
| 1015 | |
| 1016 | private void setLocked(int type, long when) { |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 1017 | if (mNativeData != 0) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1018 | // The kernel never triggers alarms with negative wakeup times |
| 1019 | // so we ensure they are positive. |
| 1020 | long alarmSeconds, alarmNanoseconds; |
| 1021 | if (when < 0) { |
| 1022 | alarmSeconds = 0; |
| 1023 | alarmNanoseconds = 0; |
| 1024 | } else { |
| 1025 | alarmSeconds = when / 1000; |
| 1026 | alarmNanoseconds = (when % 1000) * 1000 * 1000; |
| 1027 | } |
| 1028 | |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 1029 | set(mNativeData, type, alarmSeconds, alarmNanoseconds); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1030 | } else { |
| 1031 | Message msg = Message.obtain(); |
| 1032 | msg.what = ALARM_EVENT; |
| 1033 | |
| 1034 | mHandler.removeMessages(ALARM_EVENT); |
| 1035 | mHandler.sendMessageAtTime(msg, when); |
| 1036 | } |
| 1037 | } |
| 1038 | |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 1039 | private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, |
| 1040 | String prefix, String label, long now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | for (int i=list.size()-1; i>=0; i--) { |
| 1042 | Alarm a = list.get(i); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1043 | pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i); |
| 1044 | pw.print(": "); pw.println(a); |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 1045 | a.dump(pw, prefix + " ", now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | } |
| 1047 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1048 | |
| 1049 | private static final String labelForType(int type) { |
| 1050 | switch (type) { |
| 1051 | case RTC: return "RTC"; |
| 1052 | case RTC_WAKEUP : return "RTC_WAKEUP"; |
| 1053 | case ELAPSED_REALTIME : return "ELAPSED"; |
| 1054 | case ELAPSED_REALTIME_WAKEUP: return "ELAPSED_WAKEUP"; |
| 1055 | default: |
| 1056 | break; |
| 1057 | } |
| 1058 | return "--unknown--"; |
| 1059 | } |
| 1060 | |
| 1061 | private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, |
| 1062 | String prefix, long nowELAPSED, long nowRTC) { |
| 1063 | for (int i=list.size()-1; i>=0; i--) { |
| 1064 | Alarm a = list.get(i); |
| 1065 | final String label = labelForType(a.type); |
| 1066 | long now = (a.type <= RTC) ? nowRTC : nowELAPSED; |
| 1067 | pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i); |
| 1068 | pw.print(": "); pw.println(a); |
| 1069 | a.dump(pw, prefix + " ", now); |
| 1070 | } |
| 1071 | } |
| 1072 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 1073 | private native long init(); |
| 1074 | private native void close(long nativeData); |
| 1075 | private native void set(long nativeData, int type, long seconds, long nanoseconds); |
| 1076 | private native int waitForAlarm(long nativeData); |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 1077 | private native int setKernelTime(long nativeData, long millis); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 1078 | private native int setKernelTimezone(long nativeData, int minuteswest); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1080 | void triggerAlarmsLocked(ArrayList<Alarm> triggerList, final long nowELAPSED, |
| 1081 | final long nowRTC) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1082 | // batches are temporally sorted, so we need only pull from the |
| 1083 | // start of the list until we either empty it or hit a batch |
| 1084 | // that is not yet deliverable |
Christopher Tate | 6578ad1 | 2013-09-24 17:12:46 -0700 | [diff] [blame] | 1085 | while (mAlarmBatches.size() > 0) { |
| 1086 | Batch batch = mAlarmBatches.get(0); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1087 | if (batch.start > nowELAPSED) { |
| 1088 | // Everything else is scheduled for the future |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | break; |
| 1090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1092 | // We will (re)schedule some alarms now; don't let that interfere |
| 1093 | // with delivery of this current batch |
| 1094 | mAlarmBatches.remove(0); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1095 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1096 | final int N = batch.size(); |
| 1097 | for (int i = 0; i < N; i++) { |
| 1098 | Alarm alarm = batch.get(i); |
| 1099 | alarm.count = 1; |
| 1100 | triggerList.add(alarm); |
| 1101 | |
| 1102 | // Recurring alarms may have passed several alarm intervals while the |
| 1103 | // phone was asleep or off, so pass a trigger count when sending them. |
| 1104 | if (alarm.repeatInterval > 0) { |
| 1105 | // this adjustment will be zero if we're late by |
| 1106 | // less than one full repeat interval |
| 1107 | alarm.count += (nowELAPSED - alarm.whenElapsed) / alarm.repeatInterval; |
| 1108 | |
| 1109 | // Also schedule its next recurrence |
| 1110 | final long delta = alarm.count * alarm.repeatInterval; |
| 1111 | final long nextElapsed = alarm.whenElapsed + delta; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1112 | setImplLocked(alarm.type, alarm.when + delta, nextElapsed, alarm.windowLength, |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1113 | maxTriggerTime(nowELAPSED, nextElapsed, alarm.repeatInterval), |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1114 | alarm.repeatInterval, alarm.operation, batch.standalone, true, |
| 1115 | alarm.workSource); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1118 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1120 | |
| 1121 | Collections.sort(triggerList, mAlarmDispatchComparator); |
| 1122 | |
| 1123 | if (localLOGV) { |
| 1124 | for (int i=0; i<triggerList.size(); i++) { |
| 1125 | Slog.v(TAG, "Triggering alarm #" + i + ": " + triggerList.get(i)); |
| 1126 | } |
| 1127 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | /** |
| 1131 | * This Comparator sorts Alarms into increasing time order. |
| 1132 | */ |
| 1133 | public static class IncreasingTimeOrder implements Comparator<Alarm> { |
| 1134 | public int compare(Alarm a1, Alarm a2) { |
| 1135 | long when1 = a1.when; |
| 1136 | long when2 = a2.when; |
| 1137 | if (when1 - when2 > 0) { |
| 1138 | return 1; |
| 1139 | } |
| 1140 | if (when1 - when2 < 0) { |
| 1141 | return -1; |
| 1142 | } |
| 1143 | return 0; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | private static class Alarm { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1148 | public final int type; |
| 1149 | public final boolean wakeup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | public int count; |
| 1151 | public long when; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1152 | public long windowLength; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1153 | public long whenElapsed; // 'when' in the elapsed time base |
| 1154 | public long maxWhen; // also in the elapsed time base |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | public long repeatInterval; |
| 1156 | public PendingIntent operation; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1157 | public WorkSource workSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1159 | public Alarm(int _type, long _when, long _whenElapsed, long _windowLength, long _maxWhen, |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1160 | long _interval, PendingIntent _op, WorkSource _ws) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1161 | type = _type; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1162 | wakeup = _type == AlarmManager.ELAPSED_REALTIME_WAKEUP |
| 1163 | || _type == AlarmManager.RTC_WAKEUP; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1164 | when = _when; |
| 1165 | whenElapsed = _whenElapsed; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1166 | windowLength = _windowLength; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1167 | maxWhen = _maxWhen; |
| 1168 | repeatInterval = _interval; |
| 1169 | operation = _op; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1170 | workSource = _ws; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | @Override |
| 1174 | public String toString() |
| 1175 | { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1176 | StringBuilder sb = new StringBuilder(128); |
| 1177 | sb.append("Alarm{"); |
| 1178 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 1179 | sb.append(" type "); |
| 1180 | sb.append(type); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1181 | sb.append(" when "); |
| 1182 | sb.append(when); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1183 | sb.append(" "); |
| 1184 | sb.append(operation.getTargetPackage()); |
| 1185 | sb.append('}'); |
| 1186 | return sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 1189 | public void dump(PrintWriter pw, String prefix, long now) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1190 | pw.print(prefix); pw.print("type="); pw.print(type); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1191 | pw.print(" whenElapsed="); pw.print(whenElapsed); |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 1192 | pw.print(" when="); TimeUtils.formatDuration(when, now, pw); |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1193 | pw.print(" window="); pw.print(windowLength); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1194 | pw.print(" repeatInterval="); pw.print(repeatInterval); |
| 1195 | pw.print(" count="); pw.println(count); |
| 1196 | pw.print(prefix); pw.print("operation="); pw.println(operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | } |
| 1198 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1199 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1200 | void recordWakeupAlarms(ArrayList<Batch> batches, long nowELAPSED, long nowRTC) { |
| 1201 | final int numBatches = batches.size(); |
Christopher Tate | e982faf | 2013-07-19 14:51:44 -0700 | [diff] [blame] | 1202 | for (int nextBatch = 0; nextBatch < numBatches; nextBatch++) { |
| 1203 | Batch b = batches.get(nextBatch); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1204 | if (b.start > nowELAPSED) { |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1205 | break; |
| 1206 | } |
| 1207 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1208 | final int numAlarms = b.alarms.size(); |
Christopher Tate | e982faf | 2013-07-19 14:51:44 -0700 | [diff] [blame] | 1209 | for (int nextAlarm = 0; nextAlarm < numAlarms; nextAlarm++) { |
| 1210 | Alarm a = b.alarms.get(nextAlarm); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1211 | WakeupEvent e = new WakeupEvent(nowRTC, |
| 1212 | a.operation.getCreatorUid(), |
| 1213 | a.operation.getIntent().getAction()); |
| 1214 | mRecentWakeups.add(e); |
| 1215 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | private class AlarmThread extends Thread |
| 1220 | { |
| 1221 | public AlarmThread() |
| 1222 | { |
| 1223 | super("AlarmManager"); |
| 1224 | } |
| 1225 | |
| 1226 | public void run() |
| 1227 | { |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1228 | ArrayList<Alarm> triggerList = new ArrayList<Alarm>(); |
| 1229 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | while (true) |
| 1231 | { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 1232 | int result = waitForAlarm(mNativeData); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1233 | |
| 1234 | triggerList.clear(); |
| 1235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | if ((result & TIME_CHANGED_MASK) != 0) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1237 | if (DEBUG_BATCH) { |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1238 | Slog.v(TAG, "Time changed notification from kernel; rebatching"); |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1239 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1240 | removeImpl(mTimeTickSender); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1241 | rebatchAllAlarms(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | mClockReceiver.scheduleTimeTickEvent(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1243 | Intent intent = new Intent(Intent.ACTION_TIME_CHANGED); |
Dianne Hackborn | 89ba675 | 2011-01-23 16:51:16 -0800 | [diff] [blame] | 1244 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
| 1245 | | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1246 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | synchronized (mLock) { |
| 1250 | final long nowRTC = System.currentTimeMillis(); |
| 1251 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1252 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | TAG, "Checking for alarms... rtc=" + nowRTC |
| 1254 | + ", elapsed=" + nowELAPSED); |
| 1255 | |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1256 | if (WAKEUP_STATS) { |
| 1257 | if ((result & IS_WAKEUP_MASK) != 0) { |
| 1258 | long newEarliest = nowRTC - RECENT_WAKEUP_PERIOD; |
| 1259 | int n = 0; |
| 1260 | for (WakeupEvent event : mRecentWakeups) { |
| 1261 | if (event.when > newEarliest) break; |
| 1262 | n++; // number of now-stale entries at the list head |
| 1263 | } |
| 1264 | for (int i = 0; i < n; i++) { |
| 1265 | mRecentWakeups.remove(); |
| 1266 | } |
| 1267 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1268 | recordWakeupAlarms(mAlarmBatches, nowELAPSED, nowRTC); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1272 | triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC); |
| 1273 | rescheduleKernelAlarmsLocked(); |
| 1274 | |
| 1275 | // now deliver the alarm intents |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1276 | for (int i=0; i<triggerList.size(); i++) { |
| 1277 | Alarm alarm = triggerList.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1279 | if (localLOGV) Slog.v(TAG, "sending alarm " + alarm); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1280 | alarm.operation.send(getContext(), 0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1281 | mBackgroundIntent.putExtra( |
| 1282 | Intent.EXTRA_ALARM_COUNT, alarm.count), |
| 1283 | mResultReceiver, mHandler); |
| 1284 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 1285 | // we have an active broadcast so stay awake. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | if (mBroadcastRefCount == 0) { |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1287 | setWakelockWorkSource(alarm.operation, alarm.workSource, |
| 1288 | alarm.type, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | mWakeLock.acquire(); |
| 1290 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1291 | final InFlight inflight = new InFlight(AlarmManagerService.this, |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1292 | alarm.operation, alarm.workSource, alarm.type); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1293 | mInFlight.add(inflight); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | mBroadcastRefCount++; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1295 | |
| 1296 | final BroadcastStats bs = inflight.mBroadcastStats; |
| 1297 | bs.count++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | if (bs.nesting == 0) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1299 | bs.nesting = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | bs.startTime = nowELAPSED; |
| 1301 | } else { |
| 1302 | bs.nesting++; |
| 1303 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1304 | final FilterStats fs = inflight.mFilterStats; |
| 1305 | fs.count++; |
| 1306 | if (fs.nesting == 0) { |
| 1307 | fs.nesting = 1; |
| 1308 | fs.startTime = nowELAPSED; |
| 1309 | } else { |
| 1310 | fs.nesting++; |
| 1311 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1312 | if (alarm.type == ELAPSED_REALTIME_WAKEUP |
| 1313 | || alarm.type == RTC_WAKEUP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | bs.numWakeup++; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1315 | fs.numWakeup++; |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 1316 | if (alarm.workSource != null && alarm.workSource.size() > 0) { |
| 1317 | for (int wi=0; wi<alarm.workSource.size(); wi++) { |
| 1318 | ActivityManagerNative.noteWakeupAlarm( |
| 1319 | alarm.operation, alarm.workSource.get(wi), |
| 1320 | alarm.workSource.getName(wi)); |
| 1321 | } |
| 1322 | } else { |
| 1323 | ActivityManagerNative.noteWakeupAlarm( |
| 1324 | alarm.operation, -1, null); |
| 1325 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | } |
| 1327 | } catch (PendingIntent.CanceledException e) { |
| 1328 | if (alarm.repeatInterval > 0) { |
| 1329 | // This IntentSender is no longer valid, but this |
| 1330 | // is a repeating alarm, so toss the hoser. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1331 | removeImpl(alarm.operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | } |
| 1333 | } catch (RuntimeException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1334 | Slog.w(TAG, "Failure sending alarm.", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | } |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 1341 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1342 | /** |
| 1343 | * Attribute blame for a WakeLock. |
| 1344 | * @param pi PendingIntent to attribute blame to if ws is null. |
| 1345 | * @param ws WorkSource to attribute blame. |
| 1346 | */ |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1347 | void setWakelockWorkSource(PendingIntent pi, WorkSource ws, int type, boolean first) { |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 1348 | try { |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 1349 | final boolean unimportant = pi == mTimeTickSender; |
| 1350 | mWakeLock.setUnimportantForLogging(unimportant); |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 1351 | if (first || mLastWakeLockUnimportantForLogging) { |
| 1352 | mWakeLock.setHistoryTag(pi.getTag( |
| 1353 | type == ELAPSED_REALTIME_WAKEUP || type == RTC_WAKEUP |
| 1354 | ? "*walarm*:" : "*alarm*:")); |
| 1355 | } else { |
| 1356 | mWakeLock.setHistoryTag(null); |
| 1357 | } |
| 1358 | mLastWakeLockUnimportantForLogging = unimportant; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1359 | if (ws != null) { |
| 1360 | mWakeLock.setWorkSource(ws); |
| 1361 | return; |
| 1362 | } |
| 1363 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 1364 | final int uid = ActivityManagerNative.getDefault() |
| 1365 | .getUidForIntentSender(pi.getTarget()); |
| 1366 | if (uid >= 0) { |
| 1367 | mWakeLock.setWorkSource(new WorkSource(uid)); |
| 1368 | return; |
| 1369 | } |
| 1370 | } catch (Exception e) { |
| 1371 | } |
| 1372 | |
| 1373 | // Something went wrong; fall back to attributing the lock to the OS |
| 1374 | mWakeLock.setWorkSource(null); |
| 1375 | } |
| 1376 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | private class AlarmHandler extends Handler { |
| 1378 | public static final int ALARM_EVENT = 1; |
| 1379 | public static final int MINUTE_CHANGE_EVENT = 2; |
| 1380 | public static final int DATE_CHANGE_EVENT = 3; |
| 1381 | |
| 1382 | public AlarmHandler() { |
| 1383 | } |
| 1384 | |
| 1385 | public void handleMessage(Message msg) { |
| 1386 | if (msg.what == ALARM_EVENT) { |
| 1387 | ArrayList<Alarm> triggerList = new ArrayList<Alarm>(); |
| 1388 | synchronized (mLock) { |
| 1389 | final long nowRTC = System.currentTimeMillis(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1390 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 1391 | triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | // now trigger the alarms without the lock held |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1395 | for (int i=0; i<triggerList.size(); i++) { |
| 1396 | Alarm alarm = triggerList.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | try { |
| 1398 | alarm.operation.send(); |
| 1399 | } catch (PendingIntent.CanceledException e) { |
| 1400 | if (alarm.repeatInterval > 0) { |
| 1401 | // This IntentSender is no longer valid, but this |
| 1402 | // is a repeating alarm, so toss the hoser. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1403 | removeImpl(alarm.operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | class ClockReceiver extends BroadcastReceiver { |
| 1412 | public ClockReceiver() { |
| 1413 | IntentFilter filter = new IntentFilter(); |
| 1414 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 1415 | filter.addAction(Intent.ACTION_DATE_CHANGED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1416 | getContext().registerReceiver(this, filter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | @Override |
| 1420 | public void onReceive(Context context, Intent intent) { |
| 1421 | if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1422 | if (DEBUG_BATCH) { |
| 1423 | Slog.v(TAG, "Received TIME_TICK alarm; rescheduling"); |
| 1424 | } |
| 1425 | scheduleTimeTickEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1426 | } else if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) { |
| 1427 | // Since the kernel does not keep track of DST, we need to |
| 1428 | // reset the TZ information at the beginning of each day |
| 1429 | // based off of the current Zone gmt offset + userspace tracked |
| 1430 | // daylight savings information. |
| 1431 | TimeZone zone = TimeZone.getTimeZone(SystemProperties.get(TIMEZONE_PROPERTY)); |
Lavettacn Xiao | c84cc4f | 2010-08-30 12:47:23 +0200 | [diff] [blame] | 1432 | int gmtOffset = zone.getOffset(System.currentTimeMillis()); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 1433 | setKernelTimezone(mNativeData, -(gmtOffset / 60000)); |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1434 | scheduleDateChangedEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | public void scheduleTimeTickEvent() { |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 1439 | final long currentTime = System.currentTimeMillis(); |
Sungmin Choi | 563914a | 2013-01-10 17:28:40 +0900 | [diff] [blame] | 1440 | final long nextTime = 60000 * ((currentTime / 60000) + 1); |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 1441 | |
| 1442 | // Schedule this event for the amount of time that it would take to get to |
| 1443 | // the top of the next minute. |
Sungmin Choi | 563914a | 2013-01-10 17:28:40 +0900 | [diff] [blame] | 1444 | final long tickEventDelay = nextTime - currentTime; |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 1445 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1446 | final WorkSource workSource = null; // Let system take blame for time tick events. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1447 | setImpl(ELAPSED_REALTIME, SystemClock.elapsedRealtime() + tickEventDelay, 0, |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1448 | 0, mTimeTickSender, true, workSource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1449 | } |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | public void scheduleDateChangedEvent() { |
| 1452 | Calendar calendar = Calendar.getInstance(); |
| 1453 | calendar.setTimeInMillis(System.currentTimeMillis()); |
| 1454 | calendar.set(Calendar.HOUR, 0); |
| 1455 | calendar.set(Calendar.MINUTE, 0); |
| 1456 | calendar.set(Calendar.SECOND, 0); |
| 1457 | calendar.set(Calendar.MILLISECOND, 0); |
| 1458 | calendar.add(Calendar.DAY_OF_MONTH, 1); |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1459 | |
| 1460 | final WorkSource workSource = null; // Let system take blame for date change events. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1461 | setImpl(RTC, calendar.getTimeInMillis(), 0, 0, mDateChangeSender, true, workSource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | class UninstallReceiver extends BroadcastReceiver { |
| 1466 | public UninstallReceiver() { |
| 1467 | IntentFilter filter = new IntentFilter(); |
| 1468 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1469 | filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1470 | filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | filter.addDataScheme("package"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1472 | getContext().registerReceiver(this, filter); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1473 | // Register for events related to sdcard installation. |
| 1474 | IntentFilter sdFilter = new IntentFilter(); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1475 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1476 | sdFilter.addAction(Intent.ACTION_USER_STOPPED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1477 | getContext().registerReceiver(this, sdFilter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | @Override |
| 1481 | public void onReceive(Context context, Intent intent) { |
| 1482 | synchronized (mLock) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1483 | String action = intent.getAction(); |
| 1484 | String pkgList[] = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1485 | if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) { |
| 1486 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 1487 | for (String packageName : pkgList) { |
| 1488 | if (lookForPackageLocked(packageName)) { |
| 1489 | setResultCode(Activity.RESULT_OK); |
| 1490 | return; |
| 1491 | } |
| 1492 | } |
| 1493 | return; |
| 1494 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1495 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1496 | } else if (Intent.ACTION_USER_STOPPED.equals(action)) { |
| 1497 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1498 | if (userHandle >= 0) { |
| 1499 | removeUserLocked(userHandle); |
| 1500 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1501 | } else { |
Dianne Hackborn | 409578f | 2010-03-10 17:23:43 -0800 | [diff] [blame] | 1502 | if (Intent.ACTION_PACKAGE_REMOVED.equals(action) |
| 1503 | && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 1504 | // This package is being updated; don't kill its alarms. |
| 1505 | return; |
| 1506 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1507 | Uri data = intent.getData(); |
| 1508 | if (data != null) { |
| 1509 | String pkg = data.getSchemeSpecificPart(); |
| 1510 | if (pkg != null) { |
| 1511 | pkgList = new String[]{pkg}; |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | if (pkgList != null && (pkgList.length > 0)) { |
| 1516 | for (String pkg : pkgList) { |
| 1517 | removeLocked(pkg); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1518 | for (int i=mBroadcastStats.size()-1; i>=0; i--) { |
| 1519 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(i); |
| 1520 | if (uidStats.remove(pkg) != null) { |
| 1521 | if (uidStats.size() <= 0) { |
| 1522 | mBroadcastStats.removeAt(i); |
| 1523 | } |
| 1524 | } |
| 1525 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | private final BroadcastStats getStatsLocked(PendingIntent pi) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1533 | String pkg = pi.getCreatorPackage(); |
| 1534 | int uid = pi.getCreatorUid(); |
| 1535 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.get(uid); |
| 1536 | if (uidStats == null) { |
| 1537 | uidStats = new ArrayMap<String, BroadcastStats>(); |
| 1538 | mBroadcastStats.put(uid, uidStats); |
| 1539 | } |
| 1540 | BroadcastStats bs = uidStats.get(pkg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | if (bs == null) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1542 | bs = new BroadcastStats(uid, pkg); |
| 1543 | uidStats.put(pkg, bs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1544 | } |
| 1545 | return bs; |
| 1546 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1547 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | class ResultReceiver implements PendingIntent.OnFinished { |
| 1549 | public void onSendFinished(PendingIntent pi, Intent intent, int resultCode, |
| 1550 | String resultData, Bundle resultExtras) { |
| 1551 | synchronized (mLock) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1552 | InFlight inflight = null; |
| 1553 | for (int i=0; i<mInFlight.size(); i++) { |
| 1554 | if (mInFlight.get(i).mPendingIntent == pi) { |
| 1555 | inflight = mInFlight.remove(i); |
| 1556 | break; |
| 1557 | } |
| 1558 | } |
| 1559 | if (inflight != null) { |
| 1560 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 1561 | BroadcastStats bs = inflight.mBroadcastStats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1562 | bs.nesting--; |
| 1563 | if (bs.nesting <= 0) { |
| 1564 | bs.nesting = 0; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1565 | bs.aggregateTime += nowELAPSED - bs.startTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1566 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1567 | FilterStats fs = inflight.mFilterStats; |
| 1568 | fs.nesting--; |
| 1569 | if (fs.nesting <= 0) { |
| 1570 | fs.nesting = 0; |
| 1571 | fs.aggregateTime += nowELAPSED - fs.startTime; |
| 1572 | } |
| 1573 | } else { |
| 1574 | mLog.w("No in-flight alarm for " + pi + " " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | } |
| 1576 | mBroadcastRefCount--; |
| 1577 | if (mBroadcastRefCount == 0) { |
| 1578 | mWakeLock.release(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1579 | if (mInFlight.size() > 0) { |
| 1580 | mLog.w("Finished all broadcasts with " + mInFlight.size() |
| 1581 | + " remaining inflights"); |
| 1582 | for (int i=0; i<mInFlight.size(); i++) { |
| 1583 | mLog.w(" Remaining #" + i + ": " + mInFlight.get(i)); |
| 1584 | } |
| 1585 | mInFlight.clear(); |
| 1586 | } |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 1587 | } else { |
| 1588 | // the next of our alarms is now in flight. reattribute the wakelock. |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1589 | if (mInFlight.size() > 0) { |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1590 | InFlight inFlight = mInFlight.get(0); |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1591 | setWakelockWorkSource(inFlight.mPendingIntent, inFlight.mWorkSource, |
| 1592 | inFlight.mAlarmType, false); |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 1593 | } else { |
| 1594 | // should never happen |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1595 | mLog.w("Alarm wakelock still held but sent queue empty"); |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 1596 | mWakeLock.setWorkSource(null); |
| 1597 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | } |