blob: 43fe6ab383158ca23f8436ec26c131bdaa9e37a2 [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;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.pm.PackageManager;
29import android.net.Uri;
30import android.os.Binder;
31import android.os.Bundle;
32import android.os.Handler;
33import android.os.Message;
34import android.os.PowerManager;
35import android.os.SystemClock;
36import android.os.SystemProperties;
37import android.text.TextUtils;
38import android.text.format.Time;
39import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080040import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42import java.io.FileDescriptor;
43import java.io.PrintWriter;
44import java.util.ArrayList;
45import java.util.Calendar;
46import java.util.Collections;
47import java.util.Comparator;
Mike Lockwood1f7b4132009-11-20 15:12:51 -050048import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.util.HashMap;
50import java.util.Iterator;
51import java.util.Map;
52import java.util.TimeZone;
53
54class AlarmManagerService extends IAlarmManager.Stub {
55 // The threshold for how long an alarm can be late before we print a
56 // warning message. The time duration is in milliseconds.
57 private static final long LATE_ALARM_THRESHOLD = 10 * 1000;
58
59 private static final int RTC_WAKEUP_MASK = 1 << AlarmManager.RTC_WAKEUP;
60 private static final int RTC_MASK = 1 << AlarmManager.RTC;
61 private static final int ELAPSED_REALTIME_WAKEUP_MASK = 1 << AlarmManager.ELAPSED_REALTIME_WAKEUP;
62 private static final int ELAPSED_REALTIME_MASK = 1 << AlarmManager.ELAPSED_REALTIME;
63 private static final int TIME_CHANGED_MASK = 1 << 16;
64
65 private static final String TAG = "AlarmManager";
66 private static final String ClockReceiver_TAG = "ClockReceiver";
67 private static final boolean localLOGV = false;
68 private static final int ALARM_EVENT = 1;
69 private static final String TIMEZONE_PROPERTY = "persist.sys.timezone";
70
71 private static final Intent mBackgroundIntent
72 = new Intent().addFlags(Intent.FLAG_FROM_BACKGROUND);
73
74 private final Context mContext;
75
76 private Object mLock = new Object();
77
78 private final ArrayList<Alarm> mRtcWakeupAlarms = new ArrayList<Alarm>();
79 private final ArrayList<Alarm> mRtcAlarms = new ArrayList<Alarm>();
80 private final ArrayList<Alarm> mElapsedRealtimeWakeupAlarms = new ArrayList<Alarm>();
81 private final ArrayList<Alarm> mElapsedRealtimeAlarms = new ArrayList<Alarm>();
82 private final IncreasingTimeOrder mIncreasingTimeOrder = new IncreasingTimeOrder();
83
84 // slots corresponding with the inexact-repeat interval buckets,
85 // ordered from shortest to longest
86 private static final long sInexactSlotIntervals[] = {
87 AlarmManager.INTERVAL_FIFTEEN_MINUTES,
88 AlarmManager.INTERVAL_HALF_HOUR,
89 AlarmManager.INTERVAL_HOUR,
90 AlarmManager.INTERVAL_HALF_DAY,
91 AlarmManager.INTERVAL_DAY
92 };
93 private long mInexactDeliveryTimes[] = { 0, 0, 0, 0, 0};
94
95 private int mDescriptor;
96 private int mBroadcastRefCount = 0;
97 private PowerManager.WakeLock mWakeLock;
98 private final AlarmThread mWaitThread = new AlarmThread();
99 private final AlarmHandler mHandler = new AlarmHandler();
100 private ClockReceiver mClockReceiver;
101 private UninstallReceiver mUninstallReceiver;
102 private final ResultReceiver mResultReceiver = new ResultReceiver();
103 private final PendingIntent mTimeTickSender;
104 private final PendingIntent mDateChangeSender;
105
106 private static final class FilterStats {
107 int count;
108 }
109
110 private static final class BroadcastStats {
111 long aggregateTime;
112 int numWakeup;
113 long startTime;
114 int nesting;
115 HashMap<Intent.FilterComparison, FilterStats> filterStats
116 = new HashMap<Intent.FilterComparison, FilterStats>();
117 }
118
119 private final HashMap<String, BroadcastStats> mBroadcastStats
120 = new HashMap<String, BroadcastStats>();
121
122 public AlarmManagerService(Context context) {
123 mContext = context;
124 mDescriptor = init();
125 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
126 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
127
128 mTimeTickSender = PendingIntent.getBroadcast(context, 0,
129 new Intent(Intent.ACTION_TIME_TICK).addFlags(
130 Intent.FLAG_RECEIVER_REGISTERED_ONLY), 0);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800131 Intent intent = new Intent(Intent.ACTION_DATE_CHANGED);
132 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
133 mDateChangeSender = PendingIntent.getBroadcast(context, 0, intent, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
135 // now that we have initied the driver schedule the alarm
136 mClockReceiver= new ClockReceiver();
137 mClockReceiver.scheduleTimeTickEvent();
138 mClockReceiver.scheduleDateChangedEvent();
139 mUninstallReceiver = new UninstallReceiver();
140
141 if (mDescriptor != -1) {
142 mWaitThread.start();
143 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800144 Slog.w(TAG, "Failed to open alarm driver. Falling back to a handler.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
146 }
147
148 protected void finalize() throws Throwable {
149 try {
150 close(mDescriptor);
151 } finally {
152 super.finalize();
153 }
154 }
155
156 public void set(int type, long triggerAtTime, PendingIntent operation) {
157 setRepeating(type, triggerAtTime, 0, operation);
158 }
159
160 public void setRepeating(int type, long triggerAtTime, long interval,
161 PendingIntent operation) {
162 if (operation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800163 Slog.w(TAG, "set/setRepeating ignored because there is no intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 return;
165 }
166 synchronized (mLock) {
167 Alarm alarm = new Alarm();
168 alarm.type = type;
169 alarm.when = triggerAtTime;
170 alarm.repeatInterval = interval;
171 alarm.operation = operation;
172
173 // Remove this alarm if already scheduled.
174 removeLocked(operation);
175
Joe Onorato8a9b2202010-02-26 18:56:32 -0800176 if (localLOGV) Slog.v(TAG, "set: " + alarm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
178 int index = addAlarmLocked(alarm);
179 if (index == 0) {
180 setLocked(alarm);
181 }
182 }
183 }
184
185 public void setInexactRepeating(int type, long triggerAtTime, long interval,
186 PendingIntent operation) {
187 if (operation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800188 Slog.w(TAG, "setInexactRepeating ignored because there is no intent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 return;
190 }
191
192 // find the slot in the delivery-times array that we will use
193 int intervalSlot;
194 for (intervalSlot = 0; intervalSlot < sInexactSlotIntervals.length; intervalSlot++) {
195 if (sInexactSlotIntervals[intervalSlot] == interval) {
196 break;
197 }
198 }
199
200 // Non-bucket intervals just fall back to the less-efficient
201 // unbucketed recurring alarm implementation
202 if (intervalSlot >= sInexactSlotIntervals.length) {
203 setRepeating(type, triggerAtTime, interval, operation);
204 return;
205 }
206
207 // Align bucketed alarm deliveries by trying to match
208 // the shortest-interval bucket already scheduled
209 long bucketTime = 0;
210 for (int slot = 0; slot < mInexactDeliveryTimes.length; slot++) {
211 if (mInexactDeliveryTimes[slot] > 0) {
212 bucketTime = mInexactDeliveryTimes[slot];
213 break;
214 }
215 }
216
217 if (bucketTime == 0) {
218 // If nothing is scheduled yet, just start at the requested time
219 bucketTime = triggerAtTime;
220 } else {
221 // Align the new alarm with the existing bucketed sequence. To achieve
222 // alignment, we slide the start time around by min{interval, slot interval}
223 long adjustment = (interval <= sInexactSlotIntervals[intervalSlot])
224 ? interval : sInexactSlotIntervals[intervalSlot];
225
226 // The bucket may have started in the past; adjust
227 while (bucketTime < triggerAtTime) {
228 bucketTime += adjustment;
229 }
230
231 // Or the bucket may be set to start more than an interval beyond
232 // our requested trigger time; pull it back to meet our needs
233 while (bucketTime > triggerAtTime + adjustment) {
234 bucketTime -= adjustment;
235 }
236 }
237
238 // Remember where this bucket started (reducing the amount of later
239 // fixup required) and set the alarm with the new, bucketed start time.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800240 if (localLOGV) Slog.v(TAG, "setInexactRepeating: interval=" + interval
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 + " bucketTime=" + bucketTime);
242 mInexactDeliveryTimes[intervalSlot] = bucketTime;
243 setRepeating(type, bucketTime, interval, operation);
244 }
245
Dan Egnor97e44942010-02-04 20:27:47 -0800246 public void setTime(long millis) {
247 mContext.enforceCallingOrSelfPermission(
248 "android.permission.SET_TIME",
249 "setTime");
250
251 SystemClock.setCurrentTimeMillis(millis);
252 }
253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 public void setTimeZone(String tz) {
255 mContext.enforceCallingOrSelfPermission(
256 "android.permission.SET_TIME_ZONE",
257 "setTimeZone");
258
259 if (TextUtils.isEmpty(tz)) return;
260 TimeZone zone = TimeZone.getTimeZone(tz);
261 // Prevent reentrant calls from stepping on each other when writing
262 // the time zone property
263 boolean timeZoneWasChanged = false;
264 synchronized (this) {
265 String current = SystemProperties.get(TIMEZONE_PROPERTY);
266 if (current == null || !current.equals(zone.getID())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800267 if (localLOGV) Slog.v(TAG, "timezone changed: " + current + ", new=" + zone.getID());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 timeZoneWasChanged = true;
269 SystemProperties.set(TIMEZONE_PROPERTY, zone.getID());
270 }
271
272 // Update the kernel timezone information
273 // Kernel tracks time offsets as 'minutes west of GMT'
Mike Lockwood1f7b4132009-11-20 15:12:51 -0500274 int gmtOffset = zone.getRawOffset();
275 if (zone.inDaylightTime(new Date(System.currentTimeMillis()))) {
276 gmtOffset += zone.getDSTSavings();
277 }
278 setKernelTimezone(mDescriptor, -(gmtOffset / 60000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 }
280
281 TimeZone.setDefault(null);
282
283 if (timeZoneWasChanged) {
284 Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800285 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 intent.putExtra("time-zone", zone.getID());
287 mContext.sendBroadcast(intent);
288 }
289 }
290
291 public void remove(PendingIntent operation) {
292 if (operation == null) {
293 return;
294 }
295 synchronized (mLock) {
296 removeLocked(operation);
297 }
298 }
299
300 public void removeLocked(PendingIntent operation) {
301 removeLocked(mRtcWakeupAlarms, operation);
302 removeLocked(mRtcAlarms, operation);
303 removeLocked(mElapsedRealtimeWakeupAlarms, operation);
304 removeLocked(mElapsedRealtimeAlarms, operation);
305 }
306
307 private void removeLocked(ArrayList<Alarm> alarmList,
308 PendingIntent operation) {
309 if (alarmList.size() <= 0) {
310 return;
311 }
312
313 // iterator over the list removing any it where the intent match
314 Iterator<Alarm> it = alarmList.iterator();
315
316 while (it.hasNext()) {
317 Alarm alarm = it.next();
318 if (alarm.operation.equals(operation)) {
319 it.remove();
320 }
321 }
322 }
323
324 public void removeLocked(String packageName) {
325 removeLocked(mRtcWakeupAlarms, packageName);
326 removeLocked(mRtcAlarms, packageName);
327 removeLocked(mElapsedRealtimeWakeupAlarms, packageName);
328 removeLocked(mElapsedRealtimeAlarms, packageName);
329 }
330
331 private void removeLocked(ArrayList<Alarm> alarmList,
332 String packageName) {
333 if (alarmList.size() <= 0) {
334 return;
335 }
336
337 // iterator over the list removing any it where the intent match
338 Iterator<Alarm> it = alarmList.iterator();
339
340 while (it.hasNext()) {
341 Alarm alarm = it.next();
342 if (alarm.operation.getTargetPackage().equals(packageName)) {
343 it.remove();
344 }
345 }
346 }
347
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800348 public boolean lookForPackageLocked(String packageName) {
349 return lookForPackageLocked(mRtcWakeupAlarms, packageName)
350 || lookForPackageLocked(mRtcAlarms, packageName)
351 || lookForPackageLocked(mElapsedRealtimeWakeupAlarms, packageName)
352 || lookForPackageLocked(mElapsedRealtimeAlarms, packageName);
353 }
354
355 private boolean lookForPackageLocked(ArrayList<Alarm> alarmList, String packageName) {
356 for (int i=alarmList.size()-1; i>=0; i--) {
357 if (alarmList.get(i).operation.getTargetPackage().equals(packageName)) {
358 return true;
359 }
360 }
361 return false;
362 }
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 private ArrayList<Alarm> getAlarmList(int type) {
365 switch (type) {
366 case AlarmManager.RTC_WAKEUP: return mRtcWakeupAlarms;
367 case AlarmManager.RTC: return mRtcAlarms;
368 case AlarmManager.ELAPSED_REALTIME_WAKEUP: return mElapsedRealtimeWakeupAlarms;
369 case AlarmManager.ELAPSED_REALTIME: return mElapsedRealtimeAlarms;
370 }
371
372 return null;
373 }
374
375 private int addAlarmLocked(Alarm alarm) {
376 ArrayList<Alarm> alarmList = getAlarmList(alarm.type);
377
378 int index = Collections.binarySearch(alarmList, alarm, mIncreasingTimeOrder);
379 if (index < 0) {
380 index = 0 - index - 1;
381 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800382 if (localLOGV) Slog.v(TAG, "Adding alarm " + alarm + " at " + index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 alarmList.add(index, alarm);
384
385 if (localLOGV) {
386 // Display the list of alarms for this alarm type
Joe Onorato8a9b2202010-02-26 18:56:32 -0800387 Slog.v(TAG, "alarms: " + alarmList.size() + " type: " + alarm.type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 int position = 0;
389 for (Alarm a : alarmList) {
390 Time time = new Time();
391 time.set(a.when);
392 String timeStr = time.format("%b %d %I:%M:%S %p");
Joe Onorato8a9b2202010-02-26 18:56:32 -0800393 Slog.v(TAG, position + ": " + timeStr
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 + " " + a.operation.getTargetPackage());
395 position += 1;
396 }
397 }
398
399 return index;
400 }
401
402 public long timeToNextAlarm() {
403 long nextAlarm = 0xfffffffffffffffl;
404 synchronized (mLock) {
405 for (int i=AlarmManager.RTC_WAKEUP;
406 i<=AlarmManager.ELAPSED_REALTIME; i++) {
407 ArrayList<Alarm> alarmList = getAlarmList(i);
408 if (alarmList.size() > 0) {
409 Alarm a = alarmList.get(0);
410 if (a.when < nextAlarm) {
411 nextAlarm = a.when;
412 }
413 }
414 }
415 }
416 return nextAlarm;
417 }
418
419 private void setLocked(Alarm alarm)
420 {
421 if (mDescriptor != -1)
422 {
423 set(mDescriptor, alarm.type, (alarm.when * 1000 * 1000));
424 }
425 else
426 {
427 Message msg = Message.obtain();
428 msg.what = ALARM_EVENT;
429
430 mHandler.removeMessages(ALARM_EVENT);
431 mHandler.sendMessageAtTime(msg, alarm.when);
432 }
433 }
434
435 @Override
436 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
437 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
438 != PackageManager.PERMISSION_GRANTED) {
439 pw.println("Permission Denial: can't dump AlarmManager from from pid="
440 + Binder.getCallingPid()
441 + ", uid=" + Binder.getCallingUid());
442 return;
443 }
444
445 synchronized (mLock) {
446 pw.println("Current Alarm Manager state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700447 if (mRtcWakeupAlarms.size() > 0 || mRtcAlarms.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700449 pw.print(" Realtime wakeup (now=");
450 pw.print(System.currentTimeMillis()); pw.println("):");
451 if (mRtcWakeupAlarms.size() > 0) {
452 dumpAlarmList(pw, mRtcWakeupAlarms, " ", "RTC_WAKEUP");
453 }
454 if (mRtcAlarms.size() > 0) {
455 dumpAlarmList(pw, mRtcAlarms, " ", "RTC");
456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700458 if (mElapsedRealtimeWakeupAlarms.size() > 0 || mElapsedRealtimeAlarms.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700460 pw.print(" Elapsed realtime wakeup (now=");
461 pw.print(SystemClock.elapsedRealtime()); pw.println("):");
462 if (mElapsedRealtimeWakeupAlarms.size() > 0) {
463 dumpAlarmList(pw, mElapsedRealtimeWakeupAlarms, " ", "ELAPSED_WAKEUP");
464 }
465 if (mElapsedRealtimeAlarms.size() > 0) {
466 dumpAlarmList(pw, mElapsedRealtimeAlarms, " ", "ELAPSED");
467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469
470 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700471 pw.print(" Broadcast ref count: "); pw.println(mBroadcastRefCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472
473 pw.println(" ");
474 pw.println(" Alarm Stats:");
475 for (Map.Entry<String, BroadcastStats> be : mBroadcastStats.entrySet()) {
476 BroadcastStats bs = be.getValue();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700477 pw.print(" "); pw.println(be.getKey());
478 pw.print(" "); pw.print(bs.aggregateTime);
479 pw.print("ms running, "); pw.print(bs.numWakeup);
480 pw.println(" wakeups");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 for (Map.Entry<Intent.FilterComparison, FilterStats> fe
482 : bs.filterStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700483 pw.print(" "); pw.print(fe.getValue().count);
484 pw.print(" alarms: ");
485 pw.println(fe.getKey().getIntent().toShortString(true, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487 }
488 }
489 }
490
491 private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, String prefix, String label) {
492 for (int i=list.size()-1; i>=0; i--) {
493 Alarm a = list.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700494 pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i);
495 pw.print(": "); pw.println(a);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 a.dump(pw, prefix + " ");
497 }
498 }
499
500 private native int init();
501 private native void close(int fd);
502 private native void set(int fd, int type, long nanoseconds);
503 private native int waitForAlarm(int fd);
504 private native int setKernelTimezone(int fd, int minuteswest);
505
506 private void triggerAlarmsLocked(ArrayList<Alarm> alarmList,
507 ArrayList<Alarm> triggerList,
508 long now)
509 {
510 Iterator<Alarm> it = alarmList.iterator();
511 ArrayList<Alarm> repeats = new ArrayList<Alarm>();
512
513 while (it.hasNext())
514 {
515 Alarm alarm = it.next();
516
Joe Onorato8a9b2202010-02-26 18:56:32 -0800517 if (localLOGV) Slog.v(TAG, "Checking active alarm when=" + alarm.when + " " + alarm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
519 if (alarm.when > now) {
520 // don't fire alarms in the future
521 break;
522 }
523
524 // If the alarm is late, then print a warning message.
525 // Note that this can happen if the user creates a new event on
526 // the Calendar app with a reminder that is in the past. In that
527 // case, the reminder alarm will fire immediately.
528 if (localLOGV && now - alarm.when > LATE_ALARM_THRESHOLD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800529 Slog.v(TAG, "alarm is late! alarm time: " + alarm.when
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 + " now: " + now + " delay (in seconds): "
531 + (now - alarm.when) / 1000);
532 }
533
534 // Recurring alarms may have passed several alarm intervals while the
535 // phone was asleep or off, so pass a trigger count when sending them.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 if (localLOGV) Slog.v(TAG, "Alarm triggering: " + alarm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 alarm.count = 1;
538 if (alarm.repeatInterval > 0) {
539 // this adjustment will be zero if we're late by
540 // less than one full repeat interval
541 alarm.count += (now - alarm.when) / alarm.repeatInterval;
542 }
543 triggerList.add(alarm);
544
545 // remove the alarm from the list
546 it.remove();
547
548 // if it repeats queue it up to be read-added to the list
549 if (alarm.repeatInterval > 0) {
550 repeats.add(alarm);
551 }
552 }
553
554 // reset any repeating alarms.
555 it = repeats.iterator();
556 while (it.hasNext()) {
557 Alarm alarm = it.next();
558 alarm.when += alarm.count * alarm.repeatInterval;
559 addAlarmLocked(alarm);
560 }
561
562 if (alarmList.size() > 0) {
563 setLocked(alarmList.get(0));
564 }
565 }
566
567 /**
568 * This Comparator sorts Alarms into increasing time order.
569 */
570 public static class IncreasingTimeOrder implements Comparator<Alarm> {
571 public int compare(Alarm a1, Alarm a2) {
572 long when1 = a1.when;
573 long when2 = a2.when;
574 if (when1 - when2 > 0) {
575 return 1;
576 }
577 if (when1 - when2 < 0) {
578 return -1;
579 }
580 return 0;
581 }
582 }
583
584 private static class Alarm {
585 public int type;
586 public int count;
587 public long when;
588 public long repeatInterval;
589 public PendingIntent operation;
590
591 public Alarm() {
592 when = 0;
593 repeatInterval = 0;
594 operation = null;
595 }
596
597 @Override
598 public String toString()
599 {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700600 StringBuilder sb = new StringBuilder(128);
601 sb.append("Alarm{");
602 sb.append(Integer.toHexString(System.identityHashCode(this)));
603 sb.append(" type ");
604 sb.append(type);
605 sb.append(" ");
606 sb.append(operation.getTargetPackage());
607 sb.append('}');
608 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
610
611 public void dump(PrintWriter pw, String prefix)
612 {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700613 pw.print(prefix); pw.print("type="); pw.print(type);
614 pw.print(" when="); pw.print(when);
615 pw.print(" repeatInterval="); pw.print(repeatInterval);
616 pw.print(" count="); pw.println(count);
617 pw.print(prefix); pw.print("operation="); pw.println(operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
619 }
620
621 private class AlarmThread extends Thread
622 {
623 public AlarmThread()
624 {
625 super("AlarmManager");
626 }
627
628 public void run()
629 {
630 while (true)
631 {
632 int result = waitForAlarm(mDescriptor);
633
634 ArrayList<Alarm> triggerList = new ArrayList<Alarm>();
635
636 if ((result & TIME_CHANGED_MASK) != 0) {
637 remove(mTimeTickSender);
638 mClockReceiver.scheduleTimeTickEvent();
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800639 Intent intent = new Intent(Intent.ACTION_TIME_CHANGED);
640 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
641 mContext.sendBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643
644 synchronized (mLock) {
645 final long nowRTC = System.currentTimeMillis();
646 final long nowELAPSED = SystemClock.elapsedRealtime();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800647 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 TAG, "Checking for alarms... rtc=" + nowRTC
649 + ", elapsed=" + nowELAPSED);
650
651 if ((result & RTC_WAKEUP_MASK) != 0)
652 triggerAlarmsLocked(mRtcWakeupAlarms, triggerList, nowRTC);
653
654 if ((result & RTC_MASK) != 0)
655 triggerAlarmsLocked(mRtcAlarms, triggerList, nowRTC);
656
657 if ((result & ELAPSED_REALTIME_WAKEUP_MASK) != 0)
658 triggerAlarmsLocked(mElapsedRealtimeWakeupAlarms, triggerList, nowELAPSED);
659
660 if ((result & ELAPSED_REALTIME_MASK) != 0)
661 triggerAlarmsLocked(mElapsedRealtimeAlarms, triggerList, nowELAPSED);
662
663 // now trigger the alarms
664 Iterator<Alarm> it = triggerList.iterator();
665 while (it.hasNext()) {
666 Alarm alarm = it.next();
667 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800668 if (localLOGV) Slog.v(TAG, "sending alarm " + alarm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 alarm.operation.send(mContext, 0,
670 mBackgroundIntent.putExtra(
671 Intent.EXTRA_ALARM_COUNT, alarm.count),
672 mResultReceiver, mHandler);
673
674 // we have an active broadcast so stay awake.
675 if (mBroadcastRefCount == 0) {
676 mWakeLock.acquire();
677 }
678 mBroadcastRefCount++;
679
680 BroadcastStats bs = getStatsLocked(alarm.operation);
681 if (bs.nesting == 0) {
682 bs.startTime = nowELAPSED;
683 } else {
684 bs.nesting++;
685 }
686 if (alarm.type == AlarmManager.ELAPSED_REALTIME_WAKEUP
687 || alarm.type == AlarmManager.RTC_WAKEUP) {
688 bs.numWakeup++;
689 ActivityManagerNative.noteWakeupAlarm(
690 alarm.operation);
691 }
692 } catch (PendingIntent.CanceledException e) {
693 if (alarm.repeatInterval > 0) {
694 // This IntentSender is no longer valid, but this
695 // is a repeating alarm, so toss the hoser.
696 remove(alarm.operation);
697 }
698 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800699 Slog.w(TAG, "Failure sending alarm.", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
701 }
702 }
703 }
704 }
705 }
706
707 private class AlarmHandler extends Handler {
708 public static final int ALARM_EVENT = 1;
709 public static final int MINUTE_CHANGE_EVENT = 2;
710 public static final int DATE_CHANGE_EVENT = 3;
711
712 public AlarmHandler() {
713 }
714
715 public void handleMessage(Message msg) {
716 if (msg.what == ALARM_EVENT) {
717 ArrayList<Alarm> triggerList = new ArrayList<Alarm>();
718 synchronized (mLock) {
719 final long nowRTC = System.currentTimeMillis();
720 triggerAlarmsLocked(mRtcWakeupAlarms, triggerList, nowRTC);
721 triggerAlarmsLocked(mRtcAlarms, triggerList, nowRTC);
722 triggerAlarmsLocked(mElapsedRealtimeWakeupAlarms, triggerList, nowRTC);
723 triggerAlarmsLocked(mElapsedRealtimeAlarms, triggerList, nowRTC);
724 }
725
726 // now trigger the alarms without the lock held
727 Iterator<Alarm> it = triggerList.iterator();
728 while (it.hasNext())
729 {
730 Alarm alarm = it.next();
731 try {
732 alarm.operation.send();
733 } catch (PendingIntent.CanceledException e) {
734 if (alarm.repeatInterval > 0) {
735 // This IntentSender is no longer valid, but this
736 // is a repeating alarm, so toss the hoser.
737 remove(alarm.operation);
738 }
739 }
740 }
741 }
742 }
743 }
744
745 class ClockReceiver extends BroadcastReceiver {
746 public ClockReceiver() {
747 IntentFilter filter = new IntentFilter();
748 filter.addAction(Intent.ACTION_TIME_TICK);
749 filter.addAction(Intent.ACTION_DATE_CHANGED);
750 mContext.registerReceiver(this, filter);
751 }
752
753 @Override
754 public void onReceive(Context context, Intent intent) {
755 if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
756 scheduleTimeTickEvent();
757 } else if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) {
758 // Since the kernel does not keep track of DST, we need to
759 // reset the TZ information at the beginning of each day
760 // based off of the current Zone gmt offset + userspace tracked
761 // daylight savings information.
762 TimeZone zone = TimeZone.getTimeZone(SystemProperties.get(TIMEZONE_PROPERTY));
763 int gmtOffset = (zone.getRawOffset() + zone.getDSTSavings()) / 60000;
764
765 setKernelTimezone(mDescriptor, -(gmtOffset));
766 scheduleDateChangedEvent();
767 }
768 }
769
770 public void scheduleTimeTickEvent() {
771 Calendar calendar = Calendar.getInstance();
772 calendar.setTimeInMillis(System.currentTimeMillis());
773 calendar.add(Calendar.MINUTE, 1);
774 calendar.set(Calendar.SECOND, 0);
775 calendar.set(Calendar.MILLISECOND, 0);
776
777 set(AlarmManager.RTC, calendar.getTimeInMillis(), mTimeTickSender);
778 }
779
780 public void scheduleDateChangedEvent() {
781 Calendar calendar = Calendar.getInstance();
782 calendar.setTimeInMillis(System.currentTimeMillis());
783 calendar.set(Calendar.HOUR, 0);
784 calendar.set(Calendar.MINUTE, 0);
785 calendar.set(Calendar.SECOND, 0);
786 calendar.set(Calendar.MILLISECOND, 0);
787 calendar.add(Calendar.DAY_OF_MONTH, 1);
788
789 set(AlarmManager.RTC, calendar.getTimeInMillis(), mDateChangeSender);
790 }
791 }
792
793 class UninstallReceiver extends BroadcastReceiver {
794 public UninstallReceiver() {
795 IntentFilter filter = new IntentFilter();
796 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
797 filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800798 filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 filter.addDataScheme("package");
800 mContext.registerReceiver(this, filter);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800801 // Register for events related to sdcard installation.
802 IntentFilter sdFilter = new IntentFilter();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800803 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800804 mContext.registerReceiver(this, sdFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806
807 @Override
808 public void onReceive(Context context, Intent intent) {
809 synchronized (mLock) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800810 String action = intent.getAction();
811 String pkgList[] = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800812 if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
813 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
814 for (String packageName : pkgList) {
815 if (lookForPackageLocked(packageName)) {
816 setResultCode(Activity.RESULT_OK);
817 return;
818 }
819 }
820 return;
821 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800822 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
823 } else {
824 Uri data = intent.getData();
825 if (data != null) {
826 String pkg = data.getSchemeSpecificPart();
827 if (pkg != null) {
828 pkgList = new String[]{pkg};
829 }
830 }
831 }
832 if (pkgList != null && (pkgList.length > 0)) {
833 for (String pkg : pkgList) {
834 removeLocked(pkg);
835 mBroadcastStats.remove(pkg);
836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838 }
839 }
840 }
841
842 private final BroadcastStats getStatsLocked(PendingIntent pi) {
843 String pkg = pi.getTargetPackage();
844 BroadcastStats bs = mBroadcastStats.get(pkg);
845 if (bs == null) {
846 bs = new BroadcastStats();
847 mBroadcastStats.put(pkg, bs);
848 }
849 return bs;
850 }
851
852 class ResultReceiver implements PendingIntent.OnFinished {
853 public void onSendFinished(PendingIntent pi, Intent intent, int resultCode,
854 String resultData, Bundle resultExtras) {
855 synchronized (mLock) {
856 BroadcastStats bs = getStatsLocked(pi);
857 if (bs != null) {
858 bs.nesting--;
859 if (bs.nesting <= 0) {
860 bs.nesting = 0;
861 bs.aggregateTime += SystemClock.elapsedRealtime()
862 - bs.startTime;
863 Intent.FilterComparison fc = new Intent.FilterComparison(intent);
864 FilterStats fs = bs.filterStats.get(fc);
865 if (fs == null) {
866 fs = new FilterStats();
867 bs.filterStats.put(fc, fs);
868 }
869 fs.count++;
870 }
871 }
872 mBroadcastRefCount--;
873 if (mBroadcastRefCount == 0) {
874 mWakeLock.release();
875 }
876 }
877 }
878 }
879}