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