blob: cbd00f3e9ac7556ba27bfee91daaf60d4ae970c3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import android.app.Activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.app.ActivityManagerNative;
21import android.app.AlarmManager;
22import android.app.IAlarmManager;
23import android.app.PendingIntent;
24import android.content.BroadcastReceiver;
Dianne Hackborn81038902012-11-26 17:04:09 -080025import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.pm.PackageManager;
30import android.net.Uri;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Message;
35import android.os.PowerManager;
36import android.os.SystemClock;
37import android.os.SystemProperties;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070038import android.os.UserHandle;
Christopher Tatec4a07d12012-04-06 14:19:13 -070039import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.text.TextUtils;
41import android.text.format.Time;
Dianne Hackborn81038902012-11-26 17:04:09 -080042import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080043import android.util.Slog;
Dianne Hackborn043fcd92010-10-06 14:27:34 -070044import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46import java.io.FileDescriptor;
47import java.io.PrintWriter;
Dianne Hackborn043fcd92010-10-06 14:27:34 -070048import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080050import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import java.util.Calendar;
52import java.util.Collections;
53import java.util.Comparator;
Mike Lockwood1f7b4132009-11-20 15:12:51 -050054import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import java.util.HashMap;
56import java.util.Iterator;
57import java.util.Map;
58import java.util.TimeZone;
59
Dianne Hackborn81038902012-11-26 17:04:09 -080060import com.android.internal.util.LocalLog;
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062class AlarmManagerService extends IAlarmManager.Stub {
63 // The threshold for how long an alarm can be late before we print a
64 // warning message. The time duration is in milliseconds.
65 private static final long LATE_ALARM_THRESHOLD = 10 * 1000;
66
67 private static final int RTC_WAKEUP_MASK = 1 << AlarmManager.RTC_WAKEUP;
68 private static final int RTC_MASK = 1 << AlarmManager.RTC;
69 private static final int ELAPSED_REALTIME_WAKEUP_MASK = 1 << AlarmManager.ELAPSED_REALTIME_WAKEUP;
70 private static final int ELAPSED_REALTIME_MASK = 1 << AlarmManager.ELAPSED_REALTIME;
71 private static final int TIME_CHANGED_MASK = 1 << 16;
Christopher Tateb8849c12011-02-08 13:39:01 -080072
73 // Alignment quantum for inexact repeating alarms
74 private static final long QUANTUM = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 private static final String TAG = "AlarmManager";
77 private static final String ClockReceiver_TAG = "ClockReceiver";
78 private static final boolean localLOGV = false;
79 private static final int ALARM_EVENT = 1;
80 private static final String TIMEZONE_PROPERTY = "persist.sys.timezone";
81
82 private static final Intent mBackgroundIntent
83 = new Intent().addFlags(Intent.FLAG_FROM_BACKGROUND);
84
85 private final Context mContext;
Dianne Hackborn81038902012-11-26 17:04:09 -080086
87 private final LocalLog mLog = new LocalLog(TAG);
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private Object mLock = new Object();
90
91 private final ArrayList<Alarm> mRtcWakeupAlarms = new ArrayList<Alarm>();
92 private final ArrayList<Alarm> mRtcAlarms = new ArrayList<Alarm>();
93 private final ArrayList<Alarm> mElapsedRealtimeWakeupAlarms = new ArrayList<Alarm>();
94 private final ArrayList<Alarm> mElapsedRealtimeAlarms = new ArrayList<Alarm>();
95 private final IncreasingTimeOrder mIncreasingTimeOrder = new IncreasingTimeOrder();
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private int mDescriptor;
98 private int mBroadcastRefCount = 0;
99 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn81038902012-11-26 17:04:09 -0800100 private ArrayList<InFlight> mInFlight = new ArrayList<InFlight>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private final AlarmThread mWaitThread = new AlarmThread();
102 private final AlarmHandler mHandler = new AlarmHandler();
103 private ClockReceiver mClockReceiver;
104 private UninstallReceiver mUninstallReceiver;
105 private final ResultReceiver mResultReceiver = new ResultReceiver();
106 private final PendingIntent mTimeTickSender;
107 private final PendingIntent mDateChangeSender;
Dianne Hackborn81038902012-11-26 17:04:09 -0800108
109 private static final class InFlight extends Intent {
110 final PendingIntent mPendingIntent;
111 final Pair<String, ComponentName> mTarget;
112 final BroadcastStats mBroadcastStats;
113 final FilterStats mFilterStats;
114
115 InFlight(AlarmManagerService service, PendingIntent pendingIntent) {
116 mPendingIntent = pendingIntent;
117 Intent intent = pendingIntent.getIntent();
118 mTarget = intent != null
119 ? new Pair<String, ComponentName>(intent.getAction(), intent.getComponent())
120 : null;
121 mBroadcastStats = service.getStatsLocked(pendingIntent);
122 FilterStats fs = mBroadcastStats.filterStats.get(mTarget);
123 if (fs == null) {
124 fs = new FilterStats(mBroadcastStats, mTarget);
125 mBroadcastStats.filterStats.put(mTarget, fs);
126 }
127 mFilterStats = fs;
128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 }
Dianne Hackborn81038902012-11-26 17:04:09 -0800130
131 private static final class FilterStats {
132 final BroadcastStats mBroadcastStats;
133 final Pair<String, ComponentName> mTarget;
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 long aggregateTime;
Dianne Hackborn81038902012-11-26 17:04:09 -0800136 int count;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 int numWakeup;
138 long startTime;
139 int nesting;
Dianne Hackborn81038902012-11-26 17:04:09 -0800140
141 FilterStats(BroadcastStats broadcastStats, Pair<String, ComponentName> target) {
142 mBroadcastStats = broadcastStats;
143 mTarget = target;
144 }
145 }
146
147 private static final class BroadcastStats {
148 final String mPackageName;
149
150 long aggregateTime;
151 int count;
152 int numWakeup;
153 long startTime;
154 int nesting;
155 final HashMap<Pair<String, ComponentName>, FilterStats> filterStats
156 = new HashMap<Pair<String, ComponentName>, FilterStats>();
157
158 BroadcastStats(String packageName) {
159 mPackageName = packageName;
160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 }
162
163 private final HashMap<String, BroadcastStats> mBroadcastStats
164 = new HashMap<String, BroadcastStats>();
165
166 public AlarmManagerService(Context context) {
167 mContext = context;
168 mDescriptor = init();
Robert CH Chou64ba8e42009-11-04 21:38:49 +0800169
170 // We have to set current TimeZone info to kernel
171 // because kernel doesn't keep this after reboot
172 String tz = SystemProperties.get(TIMEZONE_PROPERTY);
173 if (tz != null) {
174 setTimeZone(tz);
175 }
176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
178 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
179
Dianne Hackborndb5aca92012-10-26 13:39:41 -0700180 mTimeTickSender = PendingIntent.getBroadcastAsUser(context, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 new Intent(Intent.ACTION_TIME_TICK).addFlags(
Dianne Hackborndb5aca92012-10-26 13:39:41 -0700182 Intent.FLAG_RECEIVER_REGISTERED_ONLY), 0,
183 UserHandle.ALL);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800184 Intent intent = new Intent(Intent.ACTION_DATE_CHANGED);
185 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Dianne Hackborndb5aca92012-10-26 13:39:41 -0700186 mDateChangeSender = PendingIntent.getBroadcastAsUser(context, 0, intent,
187 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
189 // now that we have initied the driver schedule the alarm
190 mClockReceiver= new ClockReceiver();
191 mClockReceiver.scheduleTimeTickEvent();
192 mClockReceiver.scheduleDateChangedEvent();
193 mUninstallReceiver = new UninstallReceiver();
194
195 if (mDescriptor != -1) {
196 mWaitThread.start();
197 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800198 Slog.w(TAG, "Failed to open alarm driver. Falling back to a handler.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
200 }
201
202 protected void finalize() throws Throwable {
203 try {
204 close(mDescriptor);
205 } finally {
206 super.finalize();
207 }
208 }
209
210 public void set(int type, long triggerAtTime, PendingIntent operation) {
211 setRepeating(type, triggerAtTime, 0, operation);
212 }
213
214 public void setRepeating(int type, long triggerAtTime, long interval,
215 PendingIntent operation) {
216 if (operation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800217 Slog.w(TAG, "set/setRepeating ignored because there is no intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 return;
219 }
220 synchronized (mLock) {
221 Alarm alarm = new Alarm();
222 alarm.type = type;
223 alarm.when = triggerAtTime;
224 alarm.repeatInterval = interval;
225 alarm.operation = operation;
226
227 // Remove this alarm if already scheduled.
228 removeLocked(operation);
229
Joe Onorato8a9b2202010-02-26 18:56:32 -0800230 if (localLOGV) Slog.v(TAG, "set: " + alarm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
232 int index = addAlarmLocked(alarm);
233 if (index == 0) {
234 setLocked(alarm);
235 }
236 }
237 }
238
239 public void setInexactRepeating(int type, long triggerAtTime, long interval,
240 PendingIntent operation) {
241 if (operation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800242 Slog.w(TAG, "setInexactRepeating ignored because there is no intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 return;
244 }
245
Christopher Tateb8849c12011-02-08 13:39:01 -0800246 if (interval <= 0) {
247 Slog.w(TAG, "setInexactRepeating ignored because interval " + interval
248 + " is invalid");
249 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 }
Christopher Tateb8849c12011-02-08 13:39:01 -0800251
252 // If the requested interval isn't a multiple of 15 minutes, just treat it as exact
253 if (interval % QUANTUM != 0) {
254 if (localLOGV) Slog.v(TAG, "Interval " + interval + " not a quantum multiple");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 setRepeating(type, triggerAtTime, interval, operation);
256 return;
257 }
258
Christopher Tateb8849c12011-02-08 13:39:01 -0800259 // Translate times into the ELAPSED timebase for alignment purposes so that
260 // alignment never tries to match against wall clock times.
261 final boolean isRtc = (type == AlarmManager.RTC || type == AlarmManager.RTC_WAKEUP);
262 final long skew = (isRtc)
263 ? System.currentTimeMillis() - SystemClock.elapsedRealtime()
264 : 0;
265
266 // Slip forward to the next ELAPSED-timebase quantum after the stated time. If
267 // we're *at* a quantum point, leave it alone.
268 final long adjustedTriggerTime;
269 long offset = (triggerAtTime - skew) % QUANTUM;
270 if (offset != 0) {
271 adjustedTriggerTime = triggerAtTime - offset + QUANTUM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 } else {
Christopher Tateb8849c12011-02-08 13:39:01 -0800273 adjustedTriggerTime = triggerAtTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
Christopher Tateb8849c12011-02-08 13:39:01 -0800276 // Set the alarm based on the quantum-aligned start time
277 if (localLOGV) Slog.v(TAG, "setInexactRepeating: type=" + type + " interval=" + interval
278 + " trigger=" + adjustedTriggerTime + " orig=" + triggerAtTime);
279 setRepeating(type, adjustedTriggerTime, interval, operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281
Dan Egnor97e44942010-02-04 20:27:47 -0800282 public void setTime(long millis) {
283 mContext.enforceCallingOrSelfPermission(
284 "android.permission.SET_TIME",
285 "setTime");
286
287 SystemClock.setCurrentTimeMillis(millis);
288 }
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 public void setTimeZone(String tz) {
291 mContext.enforceCallingOrSelfPermission(
292 "android.permission.SET_TIME_ZONE",
293 "setTimeZone");
294
Christopher Tate89779822012-08-31 14:40:03 -0700295 long oldId = Binder.clearCallingIdentity();
296 try {
297 if (TextUtils.isEmpty(tz)) return;
298 TimeZone zone = TimeZone.getTimeZone(tz);
299 // Prevent reentrant calls from stepping on each other when writing
300 // the time zone property
301 boolean timeZoneWasChanged = false;
302 synchronized (this) {
303 String current = SystemProperties.get(TIMEZONE_PROPERTY);
304 if (current == null || !current.equals(zone.getID())) {
305 if (localLOGV) {
306 Slog.v(TAG, "timezone changed: " + current + ", new=" + zone.getID());
307 }
308 timeZoneWasChanged = true;
309 SystemProperties.set(TIMEZONE_PROPERTY, zone.getID());
310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311
Christopher Tate89779822012-08-31 14:40:03 -0700312 // Update the kernel timezone information
313 // Kernel tracks time offsets as 'minutes west of GMT'
314 int gmtOffset = zone.getOffset(System.currentTimeMillis());
315 setKernelTimezone(mDescriptor, -(gmtOffset / 60000));
316 }
317
318 TimeZone.setDefault(null);
319
320 if (timeZoneWasChanged) {
321 Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED);
322 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
323 intent.putExtra("time-zone", zone.getID());
324 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
325 }
326 } finally {
327 Binder.restoreCallingIdentity(oldId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 }
329 }
330
331 public void remove(PendingIntent operation) {
332 if (operation == null) {
333 return;
334 }
335 synchronized (mLock) {
336 removeLocked(operation);
337 }
338 }
339
340 public void removeLocked(PendingIntent operation) {
341 removeLocked(mRtcWakeupAlarms, operation);
342 removeLocked(mRtcAlarms, operation);
343 removeLocked(mElapsedRealtimeWakeupAlarms, operation);
344 removeLocked(mElapsedRealtimeAlarms, operation);
345 }
346
347 private void removeLocked(ArrayList<Alarm> alarmList,
348 PendingIntent operation) {
349 if (alarmList.size() <= 0) {
350 return;
351 }
352
353 // iterator over the list removing any it where the intent match
354 Iterator<Alarm> it = alarmList.iterator();
355
356 while (it.hasNext()) {
357 Alarm alarm = it.next();
358 if (alarm.operation.equals(operation)) {
359 it.remove();
360 }
361 }
362 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 public void removeLocked(String packageName) {
365 removeLocked(mRtcWakeupAlarms, packageName);
366 removeLocked(mRtcAlarms, packageName);
367 removeLocked(mElapsedRealtimeWakeupAlarms, packageName);
368 removeLocked(mElapsedRealtimeAlarms, packageName);
369 }
370
371 private void removeLocked(ArrayList<Alarm> alarmList,
372 String packageName) {
373 if (alarmList.size() <= 0) {
374 return;
375 }
376
377 // iterator over the list removing any it where the intent match
378 Iterator<Alarm> it = alarmList.iterator();
379
380 while (it.hasNext()) {
381 Alarm alarm = it.next();
382 if (alarm.operation.getTargetPackage().equals(packageName)) {
383 it.remove();
384 }
385 }
386 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700387
388 public void removeUserLocked(int userHandle) {
389 removeUserLocked(mRtcWakeupAlarms, userHandle);
390 removeUserLocked(mRtcAlarms, userHandle);
391 removeUserLocked(mElapsedRealtimeWakeupAlarms, userHandle);
392 removeUserLocked(mElapsedRealtimeAlarms, userHandle);
393 }
394
395 private void removeUserLocked(ArrayList<Alarm> alarmList, int userHandle) {
396 if (alarmList.size() <= 0) {
397 return;
398 }
399
400 // iterator over the list removing any it where the intent match
401 Iterator<Alarm> it = alarmList.iterator();
402
403 while (it.hasNext()) {
404 Alarm alarm = it.next();
Dianne Hackborn8832c182012-09-17 17:20:24 -0700405 if (UserHandle.getUserId(alarm.operation.getCreatorUid()) == userHandle) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700406 it.remove();
407 }
408 }
409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800411 public boolean lookForPackageLocked(String packageName) {
412 return lookForPackageLocked(mRtcWakeupAlarms, packageName)
413 || lookForPackageLocked(mRtcAlarms, packageName)
414 || lookForPackageLocked(mElapsedRealtimeWakeupAlarms, packageName)
415 || lookForPackageLocked(mElapsedRealtimeAlarms, packageName);
416 }
417
418 private boolean lookForPackageLocked(ArrayList<Alarm> alarmList, String packageName) {
419 for (int i=alarmList.size()-1; i>=0; i--) {
420 if (alarmList.get(i).operation.getTargetPackage().equals(packageName)) {
421 return true;
422 }
423 }
424 return false;
425 }
426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 private ArrayList<Alarm> getAlarmList(int type) {
428 switch (type) {
429 case AlarmManager.RTC_WAKEUP: return mRtcWakeupAlarms;
430 case AlarmManager.RTC: return mRtcAlarms;
431 case AlarmManager.ELAPSED_REALTIME_WAKEUP: return mElapsedRealtimeWakeupAlarms;
432 case AlarmManager.ELAPSED_REALTIME: return mElapsedRealtimeAlarms;
433 }
434
435 return null;
436 }
437
438 private int addAlarmLocked(Alarm alarm) {
439 ArrayList<Alarm> alarmList = getAlarmList(alarm.type);
440
441 int index = Collections.binarySearch(alarmList, alarm, mIncreasingTimeOrder);
442 if (index < 0) {
443 index = 0 - index - 1;
444 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800445 if (localLOGV) Slog.v(TAG, "Adding alarm " + alarm + " at " + index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 alarmList.add(index, alarm);
447
448 if (localLOGV) {
449 // Display the list of alarms for this alarm type
Joe Onorato8a9b2202010-02-26 18:56:32 -0800450 Slog.v(TAG, "alarms: " + alarmList.size() + " type: " + alarm.type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 int position = 0;
452 for (Alarm a : alarmList) {
453 Time time = new Time();
454 time.set(a.when);
455 String timeStr = time.format("%b %d %I:%M:%S %p");
Joe Onorato8a9b2202010-02-26 18:56:32 -0800456 Slog.v(TAG, position + ": " + timeStr
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 + " " + a.operation.getTargetPackage());
458 position += 1;
459 }
460 }
461
462 return index;
463 }
464
465 public long timeToNextAlarm() {
Jeff Brown11c5f1a2010-03-31 15:29:40 -0700466 long nextAlarm = Long.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 synchronized (mLock) {
468 for (int i=AlarmManager.RTC_WAKEUP;
469 i<=AlarmManager.ELAPSED_REALTIME; i++) {
470 ArrayList<Alarm> alarmList = getAlarmList(i);
471 if (alarmList.size() > 0) {
472 Alarm a = alarmList.get(0);
473 if (a.when < nextAlarm) {
474 nextAlarm = a.when;
475 }
476 }
477 }
478 }
479 return nextAlarm;
480 }
481
482 private void setLocked(Alarm alarm)
483 {
484 if (mDescriptor != -1)
485 {
Jeff Brown11c5f1a2010-03-31 15:29:40 -0700486 // The kernel never triggers alarms with negative wakeup times
487 // so we ensure they are positive.
488 long alarmSeconds, alarmNanoseconds;
489 if (alarm.when < 0) {
490 alarmSeconds = 0;
491 alarmNanoseconds = 0;
492 } else {
493 alarmSeconds = alarm.when / 1000;
494 alarmNanoseconds = (alarm.when % 1000) * 1000 * 1000;
495 }
496
497 set(mDescriptor, alarm.type, alarmSeconds, alarmNanoseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
499 else
500 {
501 Message msg = Message.obtain();
502 msg.what = ALARM_EVENT;
503
504 mHandler.removeMessages(ALARM_EVENT);
505 mHandler.sendMessageAtTime(msg, alarm.when);
506 }
507 }
508
509 @Override
510 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
511 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
512 != PackageManager.PERMISSION_GRANTED) {
513 pw.println("Permission Denial: can't dump AlarmManager from from pid="
514 + Binder.getCallingPid()
515 + ", uid=" + Binder.getCallingUid());
516 return;
517 }
518
519 synchronized (mLock) {
520 pw.println("Current Alarm Manager state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700521 if (mRtcWakeupAlarms.size() > 0 || mRtcAlarms.size() > 0) {
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700522 final long now = System.currentTimeMillis();
523 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700525 pw.print(" Realtime wakeup (now=");
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700526 pw.print(sdf.format(new Date(now))); pw.println("):");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700527 if (mRtcWakeupAlarms.size() > 0) {
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700528 dumpAlarmList(pw, mRtcWakeupAlarms, " ", "RTC_WAKEUP", now);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700529 }
530 if (mRtcAlarms.size() > 0) {
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700531 dumpAlarmList(pw, mRtcAlarms, " ", "RTC", now);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700534 if (mElapsedRealtimeWakeupAlarms.size() > 0 || mElapsedRealtimeAlarms.size() > 0) {
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700535 final long now = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700537 pw.print(" Elapsed realtime wakeup (now=");
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700538 TimeUtils.formatDuration(now, pw); pw.println("):");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700539 if (mElapsedRealtimeWakeupAlarms.size() > 0) {
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700540 dumpAlarmList(pw, mElapsedRealtimeWakeupAlarms, " ", "ELAPSED_WAKEUP", now);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700541 }
542 if (mElapsedRealtimeAlarms.size() > 0) {
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700543 dumpAlarmList(pw, mElapsedRealtimeAlarms, " ", "ELAPSED", now);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
Dianne Hackborn81038902012-11-26 17:04:09 -0800546
547 pw.println();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700548 pw.print(" Broadcast ref count: "); pw.println(mBroadcastRefCount);
Dianne Hackborn81038902012-11-26 17:04:09 -0800549 pw.println();
550
551 if (mLog.dump(pw, " Recent problems", " ")) {
552 pw.println();
553 }
554
555 final FilterStats[] topFilters = new FilterStats[10];
556 final Comparator<FilterStats> comparator = new Comparator<FilterStats>() {
557 @Override
558 public int compare(FilterStats lhs, FilterStats rhs) {
559 if (lhs.aggregateTime < rhs.aggregateTime) {
560 return 1;
561 } else if (lhs.aggregateTime > rhs.aggregateTime) {
562 return -1;
563 }
564 return 0;
565 }
566 };
567 int len = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 for (Map.Entry<String, BroadcastStats> be : mBroadcastStats.entrySet()) {
569 BroadcastStats bs = be.getValue();
Dianne Hackborn81038902012-11-26 17:04:09 -0800570 for (Map.Entry<Pair<String, ComponentName>, FilterStats> fe
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 : bs.filterStats.entrySet()) {
Dianne Hackborn81038902012-11-26 17:04:09 -0800572 FilterStats fs = fe.getValue();
573 int pos = len > 0
574 ? Arrays.binarySearch(topFilters, 0, len, fs, comparator) : 0;
575 if (pos < 0) {
576 pos = -pos - 1;
577 }
578 if (pos < topFilters.length) {
579 int copylen = topFilters.length - pos - 1;
580 if (copylen > 0) {
581 System.arraycopy(topFilters, pos, topFilters, pos+1, copylen);
582 }
583 topFilters[pos] = fs;
584 if (len < topFilters.length) {
585 len++;
586 }
587 }
588 }
589 }
590 if (len > 0) {
591 pw.println(" Top Alarms:");
592 for (int i=0; i<len; i++) {
593 FilterStats fs = topFilters[i];
594 pw.print(" ");
595 if (fs.nesting > 0) pw.print("*ACTIVE* ");
596 TimeUtils.formatDuration(fs.aggregateTime, pw);
597 pw.print(" running, "); pw.print(fs.numWakeup);
598 pw.print(" wakeups, "); pw.print(fs.count);
599 pw.print(" alarms: "); pw.print(fs.mBroadcastStats.mPackageName);
600 pw.println();
601 pw.print(" ");
602 if (fs.mTarget.first != null) {
603 pw.print(" act="); pw.print(fs.mTarget.first);
604 }
605 if (fs.mTarget.second != null) {
606 pw.print(" cmp="); pw.print(fs.mTarget.second.toShortString());
607 }
608 pw.println();
609 }
610 }
611
612 pw.println(" ");
613 pw.println(" Alarm Stats:");
614 final ArrayList<FilterStats> tmpFilters = new ArrayList<FilterStats>();
615 for (Map.Entry<String, BroadcastStats> be : mBroadcastStats.entrySet()) {
616 BroadcastStats bs = be.getValue();
617 pw.print(" ");
618 if (bs.nesting > 0) pw.print("*ACTIVE* ");
619 pw.print(be.getKey());
620 pw.print(" "); TimeUtils.formatDuration(bs.aggregateTime, pw);
621 pw.print(" running, "); pw.print(bs.numWakeup);
622 pw.println(" wakeups:");
623 tmpFilters.clear();
624 for (Map.Entry<Pair<String, ComponentName>, FilterStats> fe
625 : bs.filterStats.entrySet()) {
626 tmpFilters.add(fe.getValue());
627 }
628 Collections.sort(tmpFilters, comparator);
629 for (int i=0; i<tmpFilters.size(); i++) {
630 FilterStats fs = tmpFilters.get(i);
631 pw.print(" ");
632 if (fs.nesting > 0) pw.print("*ACTIVE* ");
633 TimeUtils.formatDuration(fs.aggregateTime, pw);
634 pw.print(" "); pw.print(fs.numWakeup);
635 pw.print(" wakes " ); pw.print(fs.count);
636 pw.print(" alarms:");
637 if (fs.mTarget.first != null) {
638 pw.print(" act="); pw.print(fs.mTarget.first);
639 }
640 if (fs.mTarget.second != null) {
641 pw.print(" cmp="); pw.print(fs.mTarget.second.toShortString());
642 }
643 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
645 }
646 }
647 }
648
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700649 private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list,
650 String prefix, String label, long now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 for (int i=list.size()-1; i>=0; i--) {
652 Alarm a = list.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700653 pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i);
654 pw.print(": "); pw.println(a);
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700655 a.dump(pw, prefix + " ", now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
657 }
658
659 private native int init();
660 private native void close(int fd);
Jeff Brown11c5f1a2010-03-31 15:29:40 -0700661 private native void set(int fd, int type, long seconds, long nanoseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 private native int waitForAlarm(int fd);
663 private native int setKernelTimezone(int fd, int minuteswest);
664
665 private void triggerAlarmsLocked(ArrayList<Alarm> alarmList,
666 ArrayList<Alarm> triggerList,
667 long now)
668 {
669 Iterator<Alarm> it = alarmList.iterator();
670 ArrayList<Alarm> repeats = new ArrayList<Alarm>();
671
672 while (it.hasNext())
673 {
674 Alarm alarm = it.next();
675
Joe Onorato8a9b2202010-02-26 18:56:32 -0800676 if (localLOGV) Slog.v(TAG, "Checking active alarm when=" + alarm.when + " " + alarm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
678 if (alarm.when > now) {
679 // don't fire alarms in the future
680 break;
681 }
682
683 // If the alarm is late, then print a warning message.
684 // Note that this can happen if the user creates a new event on
685 // the Calendar app with a reminder that is in the past. In that
686 // case, the reminder alarm will fire immediately.
687 if (localLOGV && now - alarm.when > LATE_ALARM_THRESHOLD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800688 Slog.v(TAG, "alarm is late! alarm time: " + alarm.when
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 + " now: " + now + " delay (in seconds): "
690 + (now - alarm.when) / 1000);
691 }
692
693 // Recurring alarms may have passed several alarm intervals while the
694 // phone was asleep or off, so pass a trigger count when sending them.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800695 if (localLOGV) Slog.v(TAG, "Alarm triggering: " + alarm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 alarm.count = 1;
697 if (alarm.repeatInterval > 0) {
698 // this adjustment will be zero if we're late by
699 // less than one full repeat interval
700 alarm.count += (now - alarm.when) / alarm.repeatInterval;
701 }
702 triggerList.add(alarm);
703
704 // remove the alarm from the list
705 it.remove();
706
707 // if it repeats queue it up to be read-added to the list
708 if (alarm.repeatInterval > 0) {
709 repeats.add(alarm);
710 }
711 }
712
713 // reset any repeating alarms.
714 it = repeats.iterator();
715 while (it.hasNext()) {
716 Alarm alarm = it.next();
717 alarm.when += alarm.count * alarm.repeatInterval;
718 addAlarmLocked(alarm);
719 }
720
721 if (alarmList.size() > 0) {
722 setLocked(alarmList.get(0));
723 }
724 }
725
726 /**
727 * This Comparator sorts Alarms into increasing time order.
728 */
729 public static class IncreasingTimeOrder implements Comparator<Alarm> {
730 public int compare(Alarm a1, Alarm a2) {
731 long when1 = a1.when;
732 long when2 = a2.when;
733 if (when1 - when2 > 0) {
734 return 1;
735 }
736 if (when1 - when2 < 0) {
737 return -1;
738 }
739 return 0;
740 }
741 }
742
743 private static class Alarm {
744 public int type;
745 public int count;
746 public long when;
747 public long repeatInterval;
748 public PendingIntent operation;
749
750 public Alarm() {
751 when = 0;
752 repeatInterval = 0;
753 operation = null;
754 }
755
756 @Override
757 public String toString()
758 {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700759 StringBuilder sb = new StringBuilder(128);
760 sb.append("Alarm{");
761 sb.append(Integer.toHexString(System.identityHashCode(this)));
762 sb.append(" type ");
763 sb.append(type);
764 sb.append(" ");
765 sb.append(operation.getTargetPackage());
766 sb.append('}');
767 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700770 public void dump(PrintWriter pw, String prefix, long now) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700771 pw.print(prefix); pw.print("type="); pw.print(type);
Dianne Hackborn043fcd92010-10-06 14:27:34 -0700772 pw.print(" when="); TimeUtils.formatDuration(when, now, pw);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700773 pw.print(" repeatInterval="); pw.print(repeatInterval);
774 pw.print(" count="); pw.println(count);
775 pw.print(prefix); pw.print("operation="); pw.println(operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 }
777 }
778
779 private class AlarmThread extends Thread
780 {
781 public AlarmThread()
782 {
783 super("AlarmManager");
784 }
785
786 public void run()
787 {
788 while (true)
789 {
790 int result = waitForAlarm(mDescriptor);
791
792 ArrayList<Alarm> triggerList = new ArrayList<Alarm>();
793
794 if ((result & TIME_CHANGED_MASK) != 0) {
795 remove(mTimeTickSender);
796 mClockReceiver.scheduleTimeTickEvent();
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800797 Intent intent = new Intent(Intent.ACTION_TIME_CHANGED);
Dianne Hackborn89ba6752011-01-23 16:51:16 -0800798 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
799 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700800 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802
803 synchronized (mLock) {
804 final long nowRTC = System.currentTimeMillis();
805 final long nowELAPSED = SystemClock.elapsedRealtime();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800806 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 TAG, "Checking for alarms... rtc=" + nowRTC
808 + ", elapsed=" + nowELAPSED);
809
810 if ((result & RTC_WAKEUP_MASK) != 0)
811 triggerAlarmsLocked(mRtcWakeupAlarms, triggerList, nowRTC);
812
813 if ((result & RTC_MASK) != 0)
814 triggerAlarmsLocked(mRtcAlarms, triggerList, nowRTC);
815
816 if ((result & ELAPSED_REALTIME_WAKEUP_MASK) != 0)
817 triggerAlarmsLocked(mElapsedRealtimeWakeupAlarms, triggerList, nowELAPSED);
818
819 if ((result & ELAPSED_REALTIME_MASK) != 0)
820 triggerAlarmsLocked(mElapsedRealtimeAlarms, triggerList, nowELAPSED);
821
822 // now trigger the alarms
823 Iterator<Alarm> it = triggerList.iterator();
824 while (it.hasNext()) {
825 Alarm alarm = it.next();
826 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800827 if (localLOGV) Slog.v(TAG, "sending alarm " + alarm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 alarm.operation.send(mContext, 0,
829 mBackgroundIntent.putExtra(
830 Intent.EXTRA_ALARM_COUNT, alarm.count),
831 mResultReceiver, mHandler);
832
Christopher Tatec4a07d12012-04-06 14:19:13 -0700833 // we have an active broadcast so stay awake.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 if (mBroadcastRefCount == 0) {
Christopher Tatec4a07d12012-04-06 14:19:13 -0700835 setWakelockWorkSource(alarm.operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 mWakeLock.acquire();
837 }
Dianne Hackborn81038902012-11-26 17:04:09 -0800838 final InFlight inflight = new InFlight(AlarmManagerService.this,
839 alarm.operation);
840 mInFlight.add(inflight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 mBroadcastRefCount++;
Dianne Hackborn81038902012-11-26 17:04:09 -0800842
843 final BroadcastStats bs = inflight.mBroadcastStats;
844 bs.count++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 if (bs.nesting == 0) {
Dianne Hackborn81038902012-11-26 17:04:09 -0800846 bs.nesting = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 bs.startTime = nowELAPSED;
848 } else {
849 bs.nesting++;
850 }
Dianne Hackborn81038902012-11-26 17:04:09 -0800851 final FilterStats fs = inflight.mFilterStats;
852 fs.count++;
853 if (fs.nesting == 0) {
854 fs.nesting = 1;
855 fs.startTime = nowELAPSED;
856 } else {
857 fs.nesting++;
858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 if (alarm.type == AlarmManager.ELAPSED_REALTIME_WAKEUP
860 || alarm.type == AlarmManager.RTC_WAKEUP) {
861 bs.numWakeup++;
Dianne Hackborn81038902012-11-26 17:04:09 -0800862 fs.numWakeup++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 ActivityManagerNative.noteWakeupAlarm(
864 alarm.operation);
865 }
866 } catch (PendingIntent.CanceledException e) {
867 if (alarm.repeatInterval > 0) {
868 // This IntentSender is no longer valid, but this
869 // is a repeating alarm, so toss the hoser.
870 remove(alarm.operation);
871 }
872 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800873 Slog.w(TAG, "Failure sending alarm.", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 }
875 }
876 }
877 }
878 }
879 }
Christopher Tatec4a07d12012-04-06 14:19:13 -0700880
881 void setWakelockWorkSource(PendingIntent pi) {
882 try {
883 final int uid = ActivityManagerNative.getDefault()
884 .getUidForIntentSender(pi.getTarget());
885 if (uid >= 0) {
886 mWakeLock.setWorkSource(new WorkSource(uid));
887 return;
888 }
889 } catch (Exception e) {
890 }
891
892 // Something went wrong; fall back to attributing the lock to the OS
893 mWakeLock.setWorkSource(null);
894 }
895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 private class AlarmHandler extends Handler {
897 public static final int ALARM_EVENT = 1;
898 public static final int MINUTE_CHANGE_EVENT = 2;
899 public static final int DATE_CHANGE_EVENT = 3;
900
901 public AlarmHandler() {
902 }
903
904 public void handleMessage(Message msg) {
905 if (msg.what == ALARM_EVENT) {
906 ArrayList<Alarm> triggerList = new ArrayList<Alarm>();
907 synchronized (mLock) {
908 final long nowRTC = System.currentTimeMillis();
909 triggerAlarmsLocked(mRtcWakeupAlarms, triggerList, nowRTC);
910 triggerAlarmsLocked(mRtcAlarms, triggerList, nowRTC);
911 triggerAlarmsLocked(mElapsedRealtimeWakeupAlarms, triggerList, nowRTC);
912 triggerAlarmsLocked(mElapsedRealtimeAlarms, triggerList, nowRTC);
913 }
914
915 // now trigger the alarms without the lock held
916 Iterator<Alarm> it = triggerList.iterator();
917 while (it.hasNext())
918 {
919 Alarm alarm = it.next();
920 try {
921 alarm.operation.send();
922 } catch (PendingIntent.CanceledException e) {
923 if (alarm.repeatInterval > 0) {
924 // This IntentSender is no longer valid, but this
925 // is a repeating alarm, so toss the hoser.
926 remove(alarm.operation);
927 }
928 }
929 }
930 }
931 }
932 }
933
934 class ClockReceiver extends BroadcastReceiver {
935 public ClockReceiver() {
936 IntentFilter filter = new IntentFilter();
937 filter.addAction(Intent.ACTION_TIME_TICK);
938 filter.addAction(Intent.ACTION_DATE_CHANGED);
939 mContext.registerReceiver(this, filter);
940 }
941
942 @Override
943 public void onReceive(Context context, Intent intent) {
944 if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
945 scheduleTimeTickEvent();
946 } else if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) {
947 // Since the kernel does not keep track of DST, we need to
948 // reset the TZ information at the beginning of each day
949 // based off of the current Zone gmt offset + userspace tracked
950 // daylight savings information.
951 TimeZone zone = TimeZone.getTimeZone(SystemProperties.get(TIMEZONE_PROPERTY));
Lavettacn Xiaoc84cc4f2010-08-30 12:47:23 +0200952 int gmtOffset = zone.getOffset(System.currentTimeMillis());
953 setKernelTimezone(mDescriptor, -(gmtOffset / 60000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 scheduleDateChangedEvent();
955 }
956 }
957
958 public void scheduleTimeTickEvent() {
959 Calendar calendar = Calendar.getInstance();
Paul Westbrook51608a52011-08-25 13:18:54 -0700960 final long currentTime = System.currentTimeMillis();
961 calendar.setTimeInMillis(currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 calendar.add(Calendar.MINUTE, 1);
963 calendar.set(Calendar.SECOND, 0);
964 calendar.set(Calendar.MILLISECOND, 0);
Paul Westbrook51608a52011-08-25 13:18:54 -0700965
966 // Schedule this event for the amount of time that it would take to get to
967 // the top of the next minute.
968 final long tickEventDelay = calendar.getTimeInMillis() - currentTime;
969
970 set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + tickEventDelay,
971 mTimeTickSender);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 }
973
974 public void scheduleDateChangedEvent() {
975 Calendar calendar = Calendar.getInstance();
976 calendar.setTimeInMillis(System.currentTimeMillis());
977 calendar.set(Calendar.HOUR, 0);
978 calendar.set(Calendar.MINUTE, 0);
979 calendar.set(Calendar.SECOND, 0);
980 calendar.set(Calendar.MILLISECOND, 0);
981 calendar.add(Calendar.DAY_OF_MONTH, 1);
982
983 set(AlarmManager.RTC, calendar.getTimeInMillis(), mDateChangeSender);
984 }
985 }
986
987 class UninstallReceiver extends BroadcastReceiver {
988 public UninstallReceiver() {
989 IntentFilter filter = new IntentFilter();
990 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
991 filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800992 filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 filter.addDataScheme("package");
994 mContext.registerReceiver(this, filter);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800995 // Register for events related to sdcard installation.
996 IntentFilter sdFilter = new IntentFilter();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800997 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700998 sdFilter.addAction(Intent.ACTION_USER_STOPPED);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800999 mContext.registerReceiver(this, sdFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
1002 @Override
1003 public void onReceive(Context context, Intent intent) {
1004 synchronized (mLock) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001005 String action = intent.getAction();
1006 String pkgList[] = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001007 if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
1008 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
1009 for (String packageName : pkgList) {
1010 if (lookForPackageLocked(packageName)) {
1011 setResultCode(Activity.RESULT_OK);
1012 return;
1013 }
1014 }
1015 return;
1016 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001017 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001018 } else if (Intent.ACTION_USER_STOPPED.equals(action)) {
1019 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1020 if (userHandle >= 0) {
1021 removeUserLocked(userHandle);
1022 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001023 } else {
Dianne Hackborn409578f2010-03-10 17:23:43 -08001024 if (Intent.ACTION_PACKAGE_REMOVED.equals(action)
1025 && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
1026 // This package is being updated; don't kill its alarms.
1027 return;
1028 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001029 Uri data = intent.getData();
1030 if (data != null) {
1031 String pkg = data.getSchemeSpecificPart();
1032 if (pkg != null) {
1033 pkgList = new String[]{pkg};
1034 }
1035 }
1036 }
1037 if (pkgList != null && (pkgList.length > 0)) {
1038 for (String pkg : pkgList) {
1039 removeLocked(pkg);
1040 mBroadcastStats.remove(pkg);
1041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043 }
1044 }
1045 }
1046
1047 private final BroadcastStats getStatsLocked(PendingIntent pi) {
1048 String pkg = pi.getTargetPackage();
1049 BroadcastStats bs = mBroadcastStats.get(pkg);
1050 if (bs == null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001051 bs = new BroadcastStats(pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 mBroadcastStats.put(pkg, bs);
1053 }
1054 return bs;
1055 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 class ResultReceiver implements PendingIntent.OnFinished {
1058 public void onSendFinished(PendingIntent pi, Intent intent, int resultCode,
1059 String resultData, Bundle resultExtras) {
1060 synchronized (mLock) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001061 InFlight inflight = null;
1062 for (int i=0; i<mInFlight.size(); i++) {
1063 if (mInFlight.get(i).mPendingIntent == pi) {
1064 inflight = mInFlight.remove(i);
1065 break;
1066 }
1067 }
1068 if (inflight != null) {
1069 final long nowELAPSED = SystemClock.elapsedRealtime();
1070 BroadcastStats bs = inflight.mBroadcastStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 bs.nesting--;
1072 if (bs.nesting <= 0) {
1073 bs.nesting = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001074 bs.aggregateTime += nowELAPSED - bs.startTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001076 FilterStats fs = inflight.mFilterStats;
1077 fs.nesting--;
1078 if (fs.nesting <= 0) {
1079 fs.nesting = 0;
1080 fs.aggregateTime += nowELAPSED - fs.startTime;
1081 }
1082 } else {
1083 mLog.w("No in-flight alarm for " + pi + " " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
1085 mBroadcastRefCount--;
1086 if (mBroadcastRefCount == 0) {
1087 mWakeLock.release();
Dianne Hackborn81038902012-11-26 17:04:09 -08001088 if (mInFlight.size() > 0) {
1089 mLog.w("Finished all broadcasts with " + mInFlight.size()
1090 + " remaining inflights");
1091 for (int i=0; i<mInFlight.size(); i++) {
1092 mLog.w(" Remaining #" + i + ": " + mInFlight.get(i));
1093 }
1094 mInFlight.clear();
1095 }
Christopher Tatec4a07d12012-04-06 14:19:13 -07001096 } else {
1097 // the next of our alarms is now in flight. reattribute the wakelock.
Dianne Hackborn81038902012-11-26 17:04:09 -08001098 if (mInFlight.size() > 0) {
1099 setWakelockWorkSource(mInFlight.get(0).mPendingIntent);
Christopher Tatec4a07d12012-04-06 14:19:13 -07001100 } else {
1101 // should never happen
Dianne Hackborn81038902012-11-26 17:04:09 -08001102 mLog.w("Alarm wakelock still held but sent queue empty");
Christopher Tatec4a07d12012-04-06 14:19:13 -07001103 mWakeLock.setWorkSource(null);
1104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106 }
1107 }
1108 }
1109}