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