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