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; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 21 | import android.app.AlarmManager; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 22 | import android.app.AppOpsManager; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 23 | import android.app.BroadcastOptions; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 24 | import android.app.IAlarmCompleteListener; |
| 25 | import android.app.IAlarmListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.app.IAlarmManager; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 27 | import android.app.IUidObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.app.PendingIntent; |
| 29 | import android.content.BroadcastReceiver; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 30 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
| 34 | import android.content.pm.PackageManager; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 35 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.net.Uri; |
| 37 | import android.os.Binder; |
| 38 | import android.os.Bundle; |
| 39 | import android.os.Handler; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 40 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.Message; |
| 42 | import android.os.PowerManager; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 43 | import android.os.Process; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 44 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.os.SystemClock; |
| 46 | import android.os.SystemProperties; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 47 | import android.os.UserHandle; |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 48 | import android.os.WorkSource; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 49 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.text.TextUtils; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 51 | import android.text.format.DateFormat; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 52 | import android.util.ArrayMap; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 53 | import android.util.KeyValueListParser; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 54 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 55 | import android.util.Slog; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 56 | import android.util.SparseArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 57 | import android.util.SparseBooleanArray; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 58 | import android.util.SparseLongArray; |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 59 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 61 | import java.io.ByteArrayOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import java.io.FileDescriptor; |
| 63 | import java.io.PrintWriter; |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 64 | import java.text.SimpleDateFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import java.util.ArrayList; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 66 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import java.util.Calendar; |
| 68 | import java.util.Collections; |
| 69 | import java.util.Comparator; |
Mike Lockwood | 1f7b413 | 2009-11-20 15:12:51 -0500 | [diff] [blame] | 70 | import java.util.Date; |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 71 | import java.util.HashMap; |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 72 | import java.util.LinkedList; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 73 | import java.util.Locale; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 74 | import java.util.Random; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import java.util.TimeZone; |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 76 | import java.util.TreeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 78 | import static android.app.AlarmManager.RTC_WAKEUP; |
| 79 | import static android.app.AlarmManager.RTC; |
| 80 | import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP; |
| 81 | import static android.app.AlarmManager.ELAPSED_REALTIME; |
| 82 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 83 | import com.android.internal.util.LocalLog; |
| 84 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 85 | class AlarmManagerService extends SystemService { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 86 | private static final int RTC_WAKEUP_MASK = 1 << RTC_WAKEUP; |
| 87 | private static final int RTC_MASK = 1 << RTC; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 88 | private static final int ELAPSED_REALTIME_WAKEUP_MASK = 1 << ELAPSED_REALTIME_WAKEUP; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 89 | private static final int ELAPSED_REALTIME_MASK = 1 << ELAPSED_REALTIME; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 90 | static final int TIME_CHANGED_MASK = 1 << 16; |
| 91 | static final int IS_WAKEUP_MASK = RTC_WAKEUP_MASK|ELAPSED_REALTIME_WAKEUP_MASK; |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 92 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 93 | // Mask for testing whether a given alarm type is wakeup vs non-wakeup |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 94 | static final int TYPE_NONWAKEUP_MASK = 0x1; // low bit => non-wakeup |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 95 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 96 | static final String TAG = "AlarmManager"; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 97 | static final boolean localLOGV = false; |
| 98 | static final boolean DEBUG_BATCH = localLOGV || false; |
| 99 | static final boolean DEBUG_VALIDATE = localLOGV || false; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 100 | static final boolean DEBUG_ALARM_CLOCK = localLOGV || false; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 101 | static final boolean DEBUG_LISTENER_CALLBACK = localLOGV || false; |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 102 | static final boolean RECORD_ALARMS_IN_HISTORY = true; |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 103 | static final boolean RECORD_DEVICE_IDLE_ALARMS = false; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 104 | static final int ALARM_EVENT = 1; |
| 105 | static final String TIMEZONE_PROPERTY = "persist.sys.timezone"; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 106 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 107 | private final Intent mBackgroundIntent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | = new Intent().addFlags(Intent.FLAG_FROM_BACKGROUND); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 109 | static final IncreasingTimeOrder sIncreasingTimeOrder = new IncreasingTimeOrder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 111 | static final boolean WAKEUP_STATS = false; |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 112 | |
Christopher Tate | 24cd46f | 2016-02-02 14:28:01 -0800 | [diff] [blame] | 113 | private static final Intent NEXT_ALARM_CLOCK_CHANGED_INTENT = |
| 114 | new Intent(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED) |
| 115 | .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 116 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 117 | final LocalLog mLog = new LocalLog(TAG); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 118 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 119 | AppOpsManager mAppOps; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 120 | DeviceIdleController.LocalService mLocalDeviceIdleController; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 122 | final Object mLock = new Object(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 123 | |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 124 | long mNativeData; |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 125 | private long mNextWakeup; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 126 | private long mNextNonWakeup; |
Dianne Hackborn | aa9a59d | 2016-05-23 15:59:18 -0700 | [diff] [blame] | 127 | private long mLastWakeupSet; |
| 128 | private long mLastWakeup; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 129 | int mBroadcastRefCount = 0; |
| 130 | PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 131 | boolean mLastWakeLockUnimportantForLogging; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 132 | ArrayList<Alarm> mPendingNonWakeupAlarms = new ArrayList<>(); |
| 133 | ArrayList<InFlight> mInFlight = new ArrayList<>(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 134 | final AlarmHandler mHandler = new AlarmHandler(); |
| 135 | ClockReceiver mClockReceiver; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 136 | InteractiveStateReceiver mInteractiveStateReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | private UninstallReceiver mUninstallReceiver; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 138 | final DeliveryTracker mDeliveryTracker = new DeliveryTracker(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 139 | PendingIntent mTimeTickSender; |
| 140 | PendingIntent mDateChangeSender; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 141 | Random mRandom; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 142 | boolean mInteractive = true; |
| 143 | long mNonInteractiveStartTime; |
| 144 | long mNonInteractiveTime; |
| 145 | long mLastAlarmDeliveryTime; |
| 146 | long mStartCurrentDelayTime; |
| 147 | long mNextNonWakeupDeliveryTime; |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 148 | long mLastTimeChangeClockTime; |
| 149 | long mLastTimeChangeRealtime; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 150 | long mAllowWhileIdleMinTime; |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 151 | int mNumTimeChanged; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 152 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 153 | /** |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 154 | * The current set of user whitelisted apps for device idle mode, meaning these are allowed |
| 155 | * to freely schedule alarms. |
| 156 | */ |
| 157 | int[] mDeviceIdleUserWhitelist = new int[0]; |
| 158 | |
| 159 | /** |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 160 | * For each uid, this is the last time we dispatched an "allow while idle" alarm, |
| 161 | * used to determine the earliest we can dispatch the next such alarm. |
| 162 | */ |
| 163 | final SparseLongArray mLastAllowWhileIdleDispatch = new SparseLongArray(); |
| 164 | |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 165 | final static class IdleDispatchEntry { |
| 166 | int uid; |
| 167 | String pkg; |
| 168 | String tag; |
| 169 | String op; |
| 170 | long elapsedRealtime; |
| 171 | long argRealtime; |
| 172 | } |
| 173 | final ArrayList<IdleDispatchEntry> mAllowWhileIdleDispatches = new ArrayList(); |
| 174 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Broadcast options to use for FLAG_ALLOW_WHILE_IDLE. |
| 177 | */ |
| 178 | Bundle mIdleOptions; |
| 179 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 180 | private final SparseArray<AlarmManager.AlarmClockInfo> mNextAlarmClockForUser = |
| 181 | new SparseArray<>(); |
| 182 | private final SparseArray<AlarmManager.AlarmClockInfo> mTmpSparseAlarmClockArray = |
| 183 | new SparseArray<>(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 184 | private final SparseBooleanArray mPendingSendNextAlarmClockChangedForUser = |
| 185 | new SparseBooleanArray(); |
| 186 | private boolean mNextAlarmClockMayChange; |
| 187 | |
| 188 | // May only use on mHandler's thread, locking not required. |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 189 | private final SparseArray<AlarmManager.AlarmClockInfo> mHandlerSparseAlarmClockArray = |
| 190 | new SparseArray<>(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 191 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 192 | /** |
| 193 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 194 | * global Settings. Any access to this class or its fields should be done while |
| 195 | * holding the AlarmManagerService.mLock lock. |
| 196 | */ |
| 197 | private final class Constants extends ContentObserver { |
| 198 | // Key names stored in the settings value. |
| 199 | private static final String KEY_MIN_FUTURITY = "min_futurity"; |
| 200 | private static final String KEY_MIN_INTERVAL = "min_interval"; |
| 201 | private static final String KEY_ALLOW_WHILE_IDLE_SHORT_TIME = "allow_while_idle_short_time"; |
| 202 | private static final String KEY_ALLOW_WHILE_IDLE_LONG_TIME = "allow_while_idle_long_time"; |
| 203 | private static final String KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION |
| 204 | = "allow_while_idle_whitelist_duration"; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 205 | private static final String KEY_LISTENER_TIMEOUT = "listener_timeout"; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 206 | |
| 207 | private static final long DEFAULT_MIN_FUTURITY = 5 * 1000; |
| 208 | private static final long DEFAULT_MIN_INTERVAL = 60 * 1000; |
Christopher Tate | aa244da | 2015-08-27 15:57:57 -0700 | [diff] [blame] | 209 | private static final long DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME = DEFAULT_MIN_FUTURITY; |
Dianne Hackborn | 7682b26 | 2015-10-26 17:51:57 -0700 | [diff] [blame] | 210 | private static final long DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME = 9*60*1000; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 211 | private static final long DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION = 10*1000; |
| 212 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 213 | private static final long DEFAULT_LISTENER_TIMEOUT = 5 * 1000; |
| 214 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 215 | // Minimum futurity of a new alarm |
| 216 | public long MIN_FUTURITY = DEFAULT_MIN_FUTURITY; |
| 217 | |
| 218 | // Minimum alarm recurrence interval |
| 219 | public long MIN_INTERVAL = DEFAULT_MIN_INTERVAL; |
| 220 | |
| 221 | // Minimum time between ALLOW_WHILE_IDLE alarms when system is not idle. |
| 222 | public long ALLOW_WHILE_IDLE_SHORT_TIME = DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME; |
| 223 | |
| 224 | // Minimum time between ALLOW_WHILE_IDLE alarms when system is idling. |
| 225 | public long ALLOW_WHILE_IDLE_LONG_TIME = DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME; |
| 226 | |
| 227 | // BroadcastOptions.setTemporaryAppWhitelistDuration() to use for FLAG_ALLOW_WHILE_IDLE. |
| 228 | public long ALLOW_WHILE_IDLE_WHITELIST_DURATION |
| 229 | = DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION; |
| 230 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 231 | // Direct alarm listener callback timeout |
| 232 | public long LISTENER_TIMEOUT = DEFAULT_LISTENER_TIMEOUT; |
| 233 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 234 | private ContentResolver mResolver; |
| 235 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 236 | private long mLastAllowWhileIdleWhitelistDuration = -1; |
| 237 | |
| 238 | public Constants(Handler handler) { |
| 239 | super(handler); |
| 240 | updateAllowWhileIdleMinTimeLocked(); |
| 241 | updateAllowWhileIdleWhitelistDurationLocked(); |
| 242 | } |
| 243 | |
| 244 | public void start(ContentResolver resolver) { |
| 245 | mResolver = resolver; |
| 246 | mResolver.registerContentObserver(Settings.Global.getUriFor( |
| 247 | Settings.Global.ALARM_MANAGER_CONSTANTS), false, this); |
| 248 | updateConstants(); |
| 249 | } |
| 250 | |
| 251 | public void updateAllowWhileIdleMinTimeLocked() { |
| 252 | mAllowWhileIdleMinTime = mPendingIdleUntil != null |
| 253 | ? ALLOW_WHILE_IDLE_LONG_TIME : ALLOW_WHILE_IDLE_SHORT_TIME; |
| 254 | } |
| 255 | |
| 256 | public void updateAllowWhileIdleWhitelistDurationLocked() { |
| 257 | if (mLastAllowWhileIdleWhitelistDuration != ALLOW_WHILE_IDLE_WHITELIST_DURATION) { |
| 258 | mLastAllowWhileIdleWhitelistDuration = ALLOW_WHILE_IDLE_WHITELIST_DURATION; |
| 259 | BroadcastOptions opts = BroadcastOptions.makeBasic(); |
| 260 | opts.setTemporaryAppWhitelistDuration(ALLOW_WHILE_IDLE_WHITELIST_DURATION); |
| 261 | mIdleOptions = opts.toBundle(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void onChange(boolean selfChange, Uri uri) { |
| 267 | updateConstants(); |
| 268 | } |
| 269 | |
| 270 | private void updateConstants() { |
| 271 | synchronized (mLock) { |
| 272 | try { |
| 273 | mParser.setString(Settings.Global.getString(mResolver, |
| 274 | Settings.Global.ALARM_MANAGER_CONSTANTS)); |
| 275 | } catch (IllegalArgumentException e) { |
| 276 | // Failed to parse the settings string, log this and move on |
| 277 | // with defaults. |
Dianne Hackborn | 0ef403e | 2017-01-24 18:22:15 -0800 | [diff] [blame] | 278 | Slog.e(TAG, "Bad alarm manager settings", e); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | MIN_FUTURITY = mParser.getLong(KEY_MIN_FUTURITY, DEFAULT_MIN_FUTURITY); |
| 282 | MIN_INTERVAL = mParser.getLong(KEY_MIN_INTERVAL, DEFAULT_MIN_INTERVAL); |
| 283 | ALLOW_WHILE_IDLE_SHORT_TIME = mParser.getLong(KEY_ALLOW_WHILE_IDLE_SHORT_TIME, |
| 284 | DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME); |
| 285 | ALLOW_WHILE_IDLE_LONG_TIME = mParser.getLong(KEY_ALLOW_WHILE_IDLE_LONG_TIME, |
| 286 | DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME); |
| 287 | ALLOW_WHILE_IDLE_WHITELIST_DURATION = mParser.getLong( |
| 288 | KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION, |
| 289 | DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 290 | LISTENER_TIMEOUT = mParser.getLong(KEY_LISTENER_TIMEOUT, |
| 291 | DEFAULT_LISTENER_TIMEOUT); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 292 | |
| 293 | updateAllowWhileIdleMinTimeLocked(); |
| 294 | updateAllowWhileIdleWhitelistDurationLocked(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void dump(PrintWriter pw) { |
| 299 | pw.println(" Settings:"); |
| 300 | |
| 301 | pw.print(" "); pw.print(KEY_MIN_FUTURITY); pw.print("="); |
| 302 | TimeUtils.formatDuration(MIN_FUTURITY, pw); |
| 303 | pw.println(); |
| 304 | |
| 305 | pw.print(" "); pw.print(KEY_MIN_INTERVAL); pw.print("="); |
| 306 | TimeUtils.formatDuration(MIN_INTERVAL, pw); |
| 307 | pw.println(); |
| 308 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 309 | pw.print(" "); pw.print(KEY_LISTENER_TIMEOUT); pw.print("="); |
| 310 | TimeUtils.formatDuration(LISTENER_TIMEOUT, pw); |
| 311 | pw.println(); |
| 312 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 313 | pw.print(" "); pw.print(KEY_ALLOW_WHILE_IDLE_SHORT_TIME); pw.print("="); |
| 314 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_SHORT_TIME, pw); |
| 315 | pw.println(); |
| 316 | |
| 317 | pw.print(" "); pw.print(KEY_ALLOW_WHILE_IDLE_LONG_TIME); pw.print("="); |
| 318 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_LONG_TIME, pw); |
| 319 | pw.println(); |
| 320 | |
| 321 | pw.print(" "); pw.print(KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION); pw.print("="); |
| 322 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_WHITELIST_DURATION, pw); |
| 323 | pw.println(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | final Constants mConstants; |
| 328 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 329 | // Alarm delivery ordering bookkeeping |
| 330 | static final int PRIO_TICK = 0; |
| 331 | static final int PRIO_WAKEUP = 1; |
| 332 | static final int PRIO_NORMAL = 2; |
| 333 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 334 | final class PriorityClass { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 335 | int seq; |
| 336 | int priority; |
| 337 | |
| 338 | PriorityClass() { |
| 339 | seq = mCurrentSeq - 1; |
| 340 | priority = PRIO_NORMAL; |
| 341 | } |
| 342 | } |
| 343 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 344 | final HashMap<String, PriorityClass> mPriorities = new HashMap<>(); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 345 | int mCurrentSeq = 0; |
| 346 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 347 | static final class WakeupEvent { |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 348 | public long when; |
| 349 | public int uid; |
| 350 | public String action; |
| 351 | |
| 352 | public WakeupEvent(long theTime, int theUid, String theAction) { |
| 353 | when = theTime; |
| 354 | uid = theUid; |
| 355 | action = theAction; |
| 356 | } |
| 357 | } |
| 358 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 359 | final LinkedList<WakeupEvent> mRecentWakeups = new LinkedList<WakeupEvent>(); |
| 360 | final long RECENT_WAKEUP_PERIOD = 1000L * 60 * 60 * 24; // one day |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 361 | |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 362 | final class Batch { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 363 | long start; // These endpoints are always in ELAPSED |
| 364 | long end; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 365 | int flags; // Flags for alarms, such as FLAG_STANDALONE. |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 366 | |
| 367 | final ArrayList<Alarm> alarms = new ArrayList<Alarm>(); |
| 368 | |
| 369 | Batch() { |
| 370 | start = 0; |
| 371 | end = Long.MAX_VALUE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 372 | flags = 0; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | Batch(Alarm seed) { |
| 376 | start = seed.whenElapsed; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 377 | end = seed.maxWhenElapsed; |
| 378 | flags = seed.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 379 | alarms.add(seed); |
| 380 | } |
| 381 | |
| 382 | int size() { |
| 383 | return alarms.size(); |
| 384 | } |
| 385 | |
| 386 | Alarm get(int index) { |
| 387 | return alarms.get(index); |
| 388 | } |
| 389 | |
| 390 | boolean canHold(long whenElapsed, long maxWhen) { |
| 391 | return (end >= whenElapsed) && (start <= maxWhen); |
| 392 | } |
| 393 | |
| 394 | boolean add(Alarm alarm) { |
| 395 | boolean newStart = false; |
| 396 | // narrows the batch if necessary; presumes that canHold(alarm) is true |
| 397 | int index = Collections.binarySearch(alarms, alarm, sIncreasingTimeOrder); |
| 398 | if (index < 0) { |
| 399 | index = 0 - index - 1; |
| 400 | } |
| 401 | alarms.add(index, alarm); |
| 402 | if (DEBUG_BATCH) { |
| 403 | Slog.v(TAG, "Adding " + alarm + " to " + this); |
| 404 | } |
| 405 | if (alarm.whenElapsed > start) { |
| 406 | start = alarm.whenElapsed; |
| 407 | newStart = true; |
| 408 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 409 | if (alarm.maxWhenElapsed < end) { |
| 410 | end = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 411 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 412 | flags |= alarm.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 413 | |
| 414 | if (DEBUG_BATCH) { |
| 415 | Slog.v(TAG, " => now " + this); |
| 416 | } |
| 417 | return newStart; |
| 418 | } |
| 419 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 420 | boolean remove(final PendingIntent operation, final IAlarmListener listener) { |
| 421 | if (operation == null && listener == null) { |
| 422 | if (localLOGV) { |
| 423 | Slog.w(TAG, "requested remove() of null operation", |
| 424 | new RuntimeException("here")); |
| 425 | } |
| 426 | return false; |
| 427 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 428 | boolean didRemove = false; |
| 429 | long newStart = 0; // recalculate endpoints as we go |
| 430 | long newEnd = Long.MAX_VALUE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 431 | int newFlags = 0; |
Christopher Tate | ae269d5 | 2013-09-26 13:11:55 -0700 | [diff] [blame] | 432 | for (int i = 0; i < alarms.size(); ) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 433 | Alarm alarm = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 434 | if (alarm.matches(operation, listener)) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 435 | alarms.remove(i); |
| 436 | didRemove = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 437 | if (alarm.alarmClock != null) { |
| 438 | mNextAlarmClockMayChange = true; |
| 439 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 440 | } else { |
| 441 | if (alarm.whenElapsed > newStart) { |
| 442 | newStart = alarm.whenElapsed; |
| 443 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 444 | if (alarm.maxWhenElapsed < newEnd) { |
| 445 | newEnd = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 446 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 447 | newFlags |= alarm.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 448 | i++; |
| 449 | } |
| 450 | } |
| 451 | if (didRemove) { |
| 452 | // commit the new batch bounds |
| 453 | start = newStart; |
| 454 | end = newEnd; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 455 | flags = newFlags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 456 | } |
| 457 | return didRemove; |
| 458 | } |
| 459 | |
| 460 | boolean remove(final String packageName) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 461 | if (packageName == null) { |
| 462 | if (localLOGV) { |
| 463 | Slog.w(TAG, "requested remove() of null packageName", |
| 464 | new RuntimeException("here")); |
| 465 | } |
| 466 | return false; |
| 467 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 468 | boolean didRemove = false; |
| 469 | long newStart = 0; // recalculate endpoints as we go |
| 470 | long newEnd = Long.MAX_VALUE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 471 | int newFlags = 0; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 472 | for (int i = alarms.size()-1; i >= 0; i--) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 473 | Alarm alarm = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 474 | if (alarm.matches(packageName)) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 475 | alarms.remove(i); |
| 476 | didRemove = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 477 | if (alarm.alarmClock != null) { |
| 478 | mNextAlarmClockMayChange = true; |
| 479 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 480 | } else { |
| 481 | if (alarm.whenElapsed > newStart) { |
| 482 | newStart = alarm.whenElapsed; |
| 483 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 484 | if (alarm.maxWhenElapsed < newEnd) { |
| 485 | newEnd = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 486 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 487 | newFlags |= alarm.flags; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | if (didRemove) { |
| 491 | // commit the new batch bounds |
| 492 | start = newStart; |
| 493 | end = newEnd; |
| 494 | flags = newFlags; |
| 495 | } |
| 496 | return didRemove; |
| 497 | } |
| 498 | |
| 499 | boolean removeForStopped(final int uid) { |
| 500 | boolean didRemove = false; |
| 501 | long newStart = 0; // recalculate endpoints as we go |
| 502 | long newEnd = Long.MAX_VALUE; |
| 503 | int newFlags = 0; |
| 504 | for (int i = alarms.size()-1; i >= 0; i--) { |
| 505 | Alarm alarm = alarms.get(i); |
| 506 | try { |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 507 | if (alarm.uid == uid && ActivityManager.getService().isAppStartModeDisabled( |
| 508 | uid, alarm.packageName)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 509 | alarms.remove(i); |
| 510 | didRemove = true; |
| 511 | if (alarm.alarmClock != null) { |
| 512 | mNextAlarmClockMayChange = true; |
| 513 | } |
| 514 | } else { |
| 515 | if (alarm.whenElapsed > newStart) { |
| 516 | newStart = alarm.whenElapsed; |
| 517 | } |
| 518 | if (alarm.maxWhenElapsed < newEnd) { |
| 519 | newEnd = alarm.maxWhenElapsed; |
| 520 | } |
| 521 | newFlags |= alarm.flags; |
| 522 | } |
| 523 | } catch (RemoteException e) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | if (didRemove) { |
| 527 | // commit the new batch bounds |
| 528 | start = newStart; |
| 529 | end = newEnd; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 530 | flags = newFlags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 531 | } |
| 532 | return didRemove; |
| 533 | } |
| 534 | |
| 535 | boolean remove(final int userHandle) { |
| 536 | boolean didRemove = false; |
| 537 | long newStart = 0; // recalculate endpoints as we go |
| 538 | long newEnd = Long.MAX_VALUE; |
Christopher Tate | ae269d5 | 2013-09-26 13:11:55 -0700 | [diff] [blame] | 539 | for (int i = 0; i < alarms.size(); ) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 540 | Alarm alarm = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 541 | if (UserHandle.getUserId(alarm.creatorUid) == userHandle) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 542 | alarms.remove(i); |
| 543 | didRemove = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 544 | if (alarm.alarmClock != null) { |
| 545 | mNextAlarmClockMayChange = true; |
| 546 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 547 | } else { |
| 548 | if (alarm.whenElapsed > newStart) { |
| 549 | newStart = alarm.whenElapsed; |
| 550 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 551 | if (alarm.maxWhenElapsed < newEnd) { |
| 552 | newEnd = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 553 | } |
| 554 | i++; |
| 555 | } |
| 556 | } |
| 557 | if (didRemove) { |
| 558 | // commit the new batch bounds |
| 559 | start = newStart; |
| 560 | end = newEnd; |
| 561 | } |
| 562 | return didRemove; |
| 563 | } |
| 564 | |
| 565 | boolean hasPackage(final String packageName) { |
| 566 | final int N = alarms.size(); |
| 567 | for (int i = 0; i < N; i++) { |
| 568 | Alarm a = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 569 | if (a.matches(packageName)) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 570 | return true; |
| 571 | } |
| 572 | } |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | boolean hasWakeups() { |
| 577 | final int N = alarms.size(); |
| 578 | for (int i = 0; i < N; i++) { |
| 579 | Alarm a = alarms.get(i); |
| 580 | // non-wakeup alarms are types 1 and 3, i.e. have the low bit set |
| 581 | if ((a.type & TYPE_NONWAKEUP_MASK) == 0) { |
| 582 | return true; |
| 583 | } |
| 584 | } |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public String toString() { |
| 590 | StringBuilder b = new StringBuilder(40); |
| 591 | b.append("Batch{"); b.append(Integer.toHexString(this.hashCode())); |
| 592 | b.append(" num="); b.append(size()); |
| 593 | b.append(" start="); b.append(start); |
| 594 | b.append(" end="); b.append(end); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 595 | if (flags != 0) { |
| 596 | b.append(" flgs=0x"); |
| 597 | b.append(Integer.toHexString(flags)); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 598 | } |
| 599 | b.append('}'); |
| 600 | return b.toString(); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | static class BatchTimeOrder implements Comparator<Batch> { |
| 605 | public int compare(Batch b1, Batch b2) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 606 | long when1 = b1.start; |
| 607 | long when2 = b2.start; |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 608 | if (when1 > when2) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 609 | return 1; |
| 610 | } |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 611 | if (when1 < when2) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 612 | return -1; |
| 613 | } |
| 614 | return 0; |
| 615 | } |
| 616 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 617 | |
| 618 | final Comparator<Alarm> mAlarmDispatchComparator = new Comparator<Alarm>() { |
| 619 | @Override |
| 620 | public int compare(Alarm lhs, Alarm rhs) { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 621 | // priority class trumps everything. TICK < WAKEUP < NORMAL |
| 622 | if (lhs.priorityClass.priority < rhs.priorityClass.priority) { |
| 623 | return -1; |
| 624 | } else if (lhs.priorityClass.priority > rhs.priorityClass.priority) { |
| 625 | return 1; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 626 | } |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 627 | |
| 628 | // within each class, sort by nominal delivery time |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 629 | if (lhs.whenElapsed < rhs.whenElapsed) { |
| 630 | return -1; |
| 631 | } else if (lhs.whenElapsed > rhs.whenElapsed) { |
| 632 | return 1; |
| 633 | } |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 634 | |
| 635 | // same priority class + same target delivery time |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | }; |
| 639 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 640 | void calculateDeliveryPriorities(ArrayList<Alarm> alarms) { |
| 641 | final int N = alarms.size(); |
| 642 | for (int i = 0; i < N; i++) { |
| 643 | Alarm a = alarms.get(i); |
| 644 | |
| 645 | final int alarmPrio; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 646 | if (a.operation != null |
| 647 | && Intent.ACTION_TIME_TICK.equals(a.operation.getIntent().getAction())) { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 648 | alarmPrio = PRIO_TICK; |
| 649 | } else if (a.wakeup) { |
| 650 | alarmPrio = PRIO_WAKEUP; |
| 651 | } else { |
| 652 | alarmPrio = PRIO_NORMAL; |
| 653 | } |
| 654 | |
| 655 | PriorityClass packagePrio = a.priorityClass; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 656 | String alarmPackage = (a.operation != null) |
| 657 | ? a.operation.getCreatorPackage() |
| 658 | : a.packageName; |
| 659 | if (packagePrio == null) packagePrio = mPriorities.get(alarmPackage); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 660 | if (packagePrio == null) { |
| 661 | packagePrio = a.priorityClass = new PriorityClass(); // lowest prio & stale sequence |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 662 | mPriorities.put(alarmPackage, packagePrio); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 663 | } |
| 664 | a.priorityClass = packagePrio; |
| 665 | |
| 666 | if (packagePrio.seq != mCurrentSeq) { |
| 667 | // first alarm we've seen in the current delivery generation from this package |
| 668 | packagePrio.priority = alarmPrio; |
| 669 | packagePrio.seq = mCurrentSeq; |
| 670 | } else { |
| 671 | // Multiple alarms from this package being delivered in this generation; |
| 672 | // bump the package's delivery class if it's warranted. |
| 673 | // TICK < WAKEUP < NORMAL |
| 674 | if (alarmPrio < packagePrio.priority) { |
| 675 | packagePrio.priority = alarmPrio; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 681 | // minimum recurrence period or alarm futurity for us to be able to fuzz it |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 682 | static final long MIN_FUZZABLE_INTERVAL = 10000; |
| 683 | static final BatchTimeOrder sBatchOrder = new BatchTimeOrder(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 684 | final ArrayList<Batch> mAlarmBatches = new ArrayList<>(); |
| 685 | |
| 686 | // set to null if in idle mode; while in this mode, any alarms we don't want |
| 687 | // to run during this time are placed in mPendingWhileIdleAlarms |
| 688 | Alarm mPendingIdleUntil = null; |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 689 | Alarm mNextWakeFromIdle = null; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 690 | ArrayList<Alarm> mPendingWhileIdleAlarms = new ArrayList<>(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 691 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 692 | public AlarmManagerService(Context context) { |
| 693 | super(context); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 694 | mConstants = new Constants(mHandler); |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 697 | static long convertToElapsed(long when, int type) { |
| 698 | final boolean isRtc = (type == RTC || type == RTC_WAKEUP); |
| 699 | if (isRtc) { |
| 700 | when -= System.currentTimeMillis() - SystemClock.elapsedRealtime(); |
| 701 | } |
| 702 | return when; |
| 703 | } |
| 704 | |
| 705 | // Apply a heuristic to { recurrence interval, futurity of the trigger time } to |
| 706 | // calculate the end of our nominal delivery window for the alarm. |
| 707 | static long maxTriggerTime(long now, long triggerAtTime, long interval) { |
| 708 | // Current heuristic: batchable window is 75% of either the recurrence interval |
| 709 | // [for a periodic alarm] or of the time from now to the desired delivery time, |
| 710 | // with a minimum delay/interval of 10 seconds, under which we will simply not |
| 711 | // defer the alarm. |
| 712 | long futurity = (interval == 0) |
| 713 | ? (triggerAtTime - now) |
| 714 | : interval; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 715 | if (futurity < MIN_FUZZABLE_INTERVAL) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 716 | futurity = 0; |
| 717 | } |
| 718 | return triggerAtTime + (long)(.75 * futurity); |
| 719 | } |
| 720 | |
| 721 | // returns true if the batch was added at the head |
| 722 | static boolean addBatchLocked(ArrayList<Batch> list, Batch newBatch) { |
| 723 | int index = Collections.binarySearch(list, newBatch, sBatchOrder); |
| 724 | if (index < 0) { |
| 725 | index = 0 - index - 1; |
| 726 | } |
| 727 | list.add(index, newBatch); |
| 728 | return (index == 0); |
| 729 | } |
| 730 | |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 731 | // Return the index of the matching batch, or -1 if none found. |
| 732 | int attemptCoalesceLocked(long whenElapsed, long maxWhen) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 733 | final int N = mAlarmBatches.size(); |
| 734 | for (int i = 0; i < N; i++) { |
| 735 | Batch b = mAlarmBatches.get(i); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 736 | if ((b.flags&AlarmManager.FLAG_STANDALONE) == 0 && b.canHold(whenElapsed, maxWhen)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 737 | return i; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 740 | return -1; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // The RTC clock has moved arbitrarily, so we need to recalculate all the batching |
| 744 | void rebatchAllAlarms() { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 745 | synchronized (mLock) { |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 746 | rebatchAllAlarmsLocked(true); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | void rebatchAllAlarmsLocked(boolean doValidate) { |
| 751 | ArrayList<Batch> oldSet = (ArrayList<Batch>) mAlarmBatches.clone(); |
| 752 | mAlarmBatches.clear(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 753 | Alarm oldPendingIdleUntil = mPendingIdleUntil; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 754 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 755 | final int oldBatches = oldSet.size(); |
| 756 | for (int batchNum = 0; batchNum < oldBatches; batchNum++) { |
| 757 | Batch batch = oldSet.get(batchNum); |
| 758 | final int N = batch.size(); |
| 759 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 760 | reAddAlarmLocked(batch.get(i), nowElapsed, doValidate); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 761 | } |
| 762 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 763 | if (oldPendingIdleUntil != null && oldPendingIdleUntil != mPendingIdleUntil) { |
| 764 | Slog.wtf(TAG, "Rebatching: idle until changed from " + oldPendingIdleUntil |
| 765 | + " to " + mPendingIdleUntil); |
| 766 | if (mPendingIdleUntil == null) { |
| 767 | // Somehow we lost this... we need to restore all of the pending alarms. |
| 768 | restorePendingWhileIdleAlarmsLocked(); |
| 769 | } |
| 770 | } |
| 771 | rescheduleKernelAlarmsLocked(); |
| 772 | updateNextAlarmClockLocked(); |
| 773 | } |
| 774 | |
| 775 | void reAddAlarmLocked(Alarm a, long nowElapsed, boolean doValidate) { |
| 776 | a.when = a.origWhen; |
| 777 | long whenElapsed = convertToElapsed(a.when, a.type); |
| 778 | final long maxElapsed; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 779 | if (a.windowLength == AlarmManager.WINDOW_EXACT) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 780 | // Exact |
| 781 | maxElapsed = whenElapsed; |
| 782 | } else { |
| 783 | // Not exact. Preserve any explicit window, otherwise recalculate |
| 784 | // the window based on the alarm's new futurity. Note that this |
| 785 | // reflects a policy of preferring timely to deferred delivery. |
| 786 | maxElapsed = (a.windowLength > 0) |
| 787 | ? (whenElapsed + a.windowLength) |
| 788 | : maxTriggerTime(nowElapsed, whenElapsed, a.repeatInterval); |
| 789 | } |
| 790 | a.whenElapsed = whenElapsed; |
| 791 | a.maxWhenElapsed = maxElapsed; |
| 792 | setImplLocked(a, true, doValidate); |
| 793 | } |
| 794 | |
| 795 | void restorePendingWhileIdleAlarmsLocked() { |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 796 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 797 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 798 | ent.uid = 0; |
| 799 | ent.pkg = "FINISH IDLE"; |
| 800 | ent.elapsedRealtime = SystemClock.elapsedRealtime(); |
| 801 | mAllowWhileIdleDispatches.add(ent); |
| 802 | } |
| 803 | |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 804 | // Bring pending alarms back into the main list. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 805 | if (mPendingWhileIdleAlarms.size() > 0) { |
| 806 | ArrayList<Alarm> alarms = mPendingWhileIdleAlarms; |
| 807 | mPendingWhileIdleAlarms = new ArrayList<>(); |
| 808 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 809 | for (int i=alarms.size() - 1; i >= 0; i--) { |
| 810 | Alarm a = alarms.get(i); |
| 811 | reAddAlarmLocked(a, nowElapsed, false); |
| 812 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 813 | } |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 814 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 815 | // Make sure we are using the correct ALLOW_WHILE_IDLE min time. |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 816 | mConstants.updateAllowWhileIdleMinTimeLocked(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 817 | |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 818 | // Reschedule everything. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 819 | rescheduleKernelAlarmsLocked(); |
| 820 | updateNextAlarmClockLocked(); |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 821 | |
| 822 | // And send a TIME_TICK right now, since it is important to get the UI updated. |
| 823 | try { |
| 824 | mTimeTickSender.send(); |
| 825 | } catch (PendingIntent.CanceledException e) { |
| 826 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 829 | static final class InFlight { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 830 | final PendingIntent mPendingIntent; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 831 | final IBinder mListener; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 832 | final WorkSource mWorkSource; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 833 | final int mUid; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 834 | final String mTag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 835 | final BroadcastStats mBroadcastStats; |
| 836 | final FilterStats mFilterStats; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 837 | final int mAlarmType; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 838 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 839 | InFlight(AlarmManagerService service, PendingIntent pendingIntent, IAlarmListener listener, |
| 840 | WorkSource workSource, int uid, String alarmPkg, int alarmType, String tag, |
| 841 | long nowELAPSED) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 842 | mPendingIntent = pendingIntent; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 843 | mListener = listener != null ? listener.asBinder() : null; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 844 | mWorkSource = workSource; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 845 | mUid = uid; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 846 | mTag = tag; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 847 | mBroadcastStats = (pendingIntent != null) |
| 848 | ? service.getStatsLocked(pendingIntent) |
| 849 | : service.getStatsLocked(uid, alarmPkg); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 850 | FilterStats fs = mBroadcastStats.filterStats.get(mTag); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 851 | if (fs == null) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 852 | fs = new FilterStats(mBroadcastStats, mTag); |
| 853 | mBroadcastStats.filterStats.put(mTag, fs); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 854 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 855 | fs.lastTime = nowELAPSED; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 856 | mFilterStats = fs; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 857 | mAlarmType = alarmType; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 858 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 860 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 861 | static final class FilterStats { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 862 | final BroadcastStats mBroadcastStats; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 863 | final String mTag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 864 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 865 | long lastTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | long aggregateTime; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 867 | int count; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | int numWakeup; |
| 869 | long startTime; |
| 870 | int nesting; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 871 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 872 | FilterStats(BroadcastStats broadcastStats, String tag) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 873 | mBroadcastStats = broadcastStats; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 874 | mTag = tag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 875 | } |
| 876 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 877 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 878 | static final class BroadcastStats { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 879 | final int mUid; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 880 | final String mPackageName; |
| 881 | |
| 882 | long aggregateTime; |
| 883 | int count; |
| 884 | int numWakeup; |
| 885 | long startTime; |
| 886 | int nesting; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 887 | final ArrayMap<String, FilterStats> filterStats = new ArrayMap<String, FilterStats>(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 888 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 889 | BroadcastStats(int uid, String packageName) { |
| 890 | mUid = uid; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 891 | mPackageName = packageName; |
| 892 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 894 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 895 | final SparseArray<ArrayMap<String, BroadcastStats>> mBroadcastStats |
| 896 | = new SparseArray<ArrayMap<String, BroadcastStats>>(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 897 | |
| 898 | int mNumDelayedAlarms = 0; |
| 899 | long mTotalDelayTime = 0; |
| 900 | long mMaxDelayTime = 0; |
| 901 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 902 | @Override |
| 903 | public void onStart() { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 904 | mNativeData = init(); |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 905 | mNextWakeup = mNextNonWakeup = 0; |
Robert CH Chou | 64ba8e4 | 2009-11-04 21:38:49 +0800 | [diff] [blame] | 906 | |
| 907 | // We have to set current TimeZone info to kernel |
| 908 | // because kernel doesn't keep this after reboot |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 909 | setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY)); |
Robert CH Chou | 64ba8e4 | 2009-11-04 21:38:49 +0800 | [diff] [blame] | 910 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 911 | PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 912 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*alarm*"); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 913 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 914 | mTimeTickSender = PendingIntent.getBroadcastAsUser(getContext(), 0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | new Intent(Intent.ACTION_TIME_TICK).addFlags( |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 916 | Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 917 | | Intent.FLAG_RECEIVER_FOREGROUND), 0, |
Dianne Hackborn | db5aca9 | 2012-10-26 13:39:41 -0700 | [diff] [blame] | 918 | UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 919 | Intent intent = new Intent(Intent.ACTION_DATE_CHANGED); |
| 920 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 921 | mDateChangeSender = PendingIntent.getBroadcastAsUser(getContext(), 0, intent, |
Dianne Hackborn | db5aca9 | 2012-10-26 13:39:41 -0700 | [diff] [blame] | 922 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | |
| 924 | // now that we have initied the driver schedule the alarm |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 925 | mClockReceiver = new ClockReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | mClockReceiver.scheduleTimeTickEvent(); |
| 927 | mClockReceiver.scheduleDateChangedEvent(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 928 | mInteractiveStateReceiver = new InteractiveStateReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | mUninstallReceiver = new UninstallReceiver(); |
| 930 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 931 | if (mNativeData != 0) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 932 | AlarmThread waitThread = new AlarmThread(); |
| 933 | waitThread.start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 935 | 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] | 936 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 937 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 938 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 939 | ActivityManager.getService().registerUidObserver(new UidObserver(), |
Dianne Hackborn | 5614bf5 | 2016-11-07 17:26:41 -0800 | [diff] [blame] | 940 | ActivityManager.UID_OBSERVER_IDLE, ActivityManager.PROCESS_STATE_UNKNOWN, null); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 941 | } catch (RemoteException e) { |
| 942 | // ignored; both services live in system_server |
| 943 | } |
| 944 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 945 | publishBinderService(Context.ALARM_SERVICE, mService); |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 946 | publishLocalService(LocalService.class, new LocalService()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 948 | |
| 949 | @Override |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 950 | public void onBootPhase(int phase) { |
| 951 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
| 952 | mConstants.start(getContext().getContentResolver()); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 953 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 954 | mLocalDeviceIdleController |
| 955 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
| 959 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | protected void finalize() throws Throwable { |
| 961 | try { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 962 | close(mNativeData); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | } finally { |
| 964 | super.finalize(); |
| 965 | } |
| 966 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 967 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 968 | void setTimeZoneImpl(String tz) { |
| 969 | if (TextUtils.isEmpty(tz)) { |
| 970 | return; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 973 | TimeZone zone = TimeZone.getTimeZone(tz); |
| 974 | // Prevent reentrant calls from stepping on each other when writing |
| 975 | // the time zone property |
| 976 | boolean timeZoneWasChanged = false; |
| 977 | synchronized (this) { |
| 978 | String current = SystemProperties.get(TIMEZONE_PROPERTY); |
| 979 | if (current == null || !current.equals(zone.getID())) { |
| 980 | if (localLOGV) { |
| 981 | Slog.v(TAG, "timezone changed: " + current + ", new=" + zone.getID()); |
| 982 | } |
| 983 | timeZoneWasChanged = true; |
| 984 | SystemProperties.set(TIMEZONE_PROPERTY, zone.getID()); |
| 985 | } |
| 986 | |
| 987 | // Update the kernel timezone information |
| 988 | // Kernel tracks time offsets as 'minutes west of GMT' |
| 989 | int gmtOffset = zone.getOffset(System.currentTimeMillis()); |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 990 | setKernelTimezone(mNativeData, -(gmtOffset / 60000)); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | TimeZone.setDefault(null); |
| 994 | |
| 995 | if (timeZoneWasChanged) { |
| 996 | Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED); |
| 997 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 998 | intent.putExtra("time-zone", zone.getID()); |
| 999 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1000 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1002 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1003 | void removeImpl(PendingIntent operation) { |
| 1004 | if (operation == null) { |
| 1005 | return; |
| 1006 | } |
| 1007 | synchronized (mLock) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1008 | removeLocked(operation, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | void setImpl(int type, long triggerAtTime, long windowLength, long interval, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1013 | PendingIntent operation, IAlarmListener directReceiver, String listenerTag, |
| 1014 | int flags, WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock, |
| 1015 | int callingUid, String callingPackage) { |
| 1016 | // must be *either* PendingIntent or AlarmReceiver, but not both |
| 1017 | if ((operation == null && directReceiver == null) |
| 1018 | || (operation != null && directReceiver != null)) { |
| 1019 | Slog.w(TAG, "Alarms must either supply a PendingIntent or an AlarmReceiver"); |
| 1020 | // NB: previous releases failed silently here, so we are continuing to do the same |
| 1021 | // rather than throw an IllegalArgumentException. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | return; |
| 1023 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1024 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1025 | // Sanity check the window length. This will catch people mistakenly |
| 1026 | // trying to pass an end-of-window timestamp rather than a duration. |
| 1027 | if (windowLength > AlarmManager.INTERVAL_HALF_DAY) { |
| 1028 | Slog.w(TAG, "Window length " + windowLength |
| 1029 | + "ms suspiciously long; limiting to 1 hour"); |
| 1030 | windowLength = AlarmManager.INTERVAL_HOUR; |
| 1031 | } |
| 1032 | |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1033 | // Sanity check the recurrence interval. This will catch people who supply |
| 1034 | // seconds when the API expects milliseconds. |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1035 | final long minInterval = mConstants.MIN_INTERVAL; |
| 1036 | if (interval > 0 && interval < minInterval) { |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1037 | Slog.w(TAG, "Suspiciously short interval " + interval |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1038 | + " millis; expanding to " + (minInterval/1000) |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1039 | + " seconds"); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1040 | interval = minInterval; |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1043 | if (type < RTC_WAKEUP || type > ELAPSED_REALTIME) { |
| 1044 | throw new IllegalArgumentException("Invalid alarm type " + type); |
| 1045 | } |
| 1046 | |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1047 | if (triggerAtTime < 0) { |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1048 | final long what = Binder.getCallingPid(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1049 | Slog.w(TAG, "Invalid alarm trigger time! " + triggerAtTime + " from uid=" + callingUid |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1050 | + " pid=" + what); |
| 1051 | triggerAtTime = 0; |
| 1052 | } |
| 1053 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1054 | final long nowElapsed = SystemClock.elapsedRealtime(); |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1055 | final long nominalTrigger = convertToElapsed(triggerAtTime, type); |
| 1056 | // Try to prevent spamming by making sure we aren't firing alarms in the immediate future |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1057 | final long minTrigger = nowElapsed + mConstants.MIN_FUTURITY; |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1058 | final long triggerElapsed = (nominalTrigger > minTrigger) ? nominalTrigger : minTrigger; |
| 1059 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1060 | final long maxElapsed; |
| 1061 | if (windowLength == AlarmManager.WINDOW_EXACT) { |
| 1062 | maxElapsed = triggerElapsed; |
| 1063 | } else if (windowLength < 0) { |
| 1064 | maxElapsed = maxTriggerTime(nowElapsed, triggerElapsed, interval); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1065 | // Fix this window in place, so that as time approaches we don't collapse it. |
| 1066 | windowLength = maxElapsed - triggerElapsed; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1067 | } else { |
| 1068 | maxElapsed = triggerElapsed + windowLength; |
| 1069 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1070 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | synchronized (mLock) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1072 | if (DEBUG_BATCH) { |
| 1073 | Slog.v(TAG, "set(" + operation + ") : type=" + type |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1074 | + " triggerAtTime=" + triggerAtTime + " win=" + windowLength |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1075 | + " tElapsed=" + triggerElapsed + " maxElapsed=" + maxElapsed |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1076 | + " interval=" + interval + " flags=0x" + Integer.toHexString(flags)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | } |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1078 | setImplLocked(type, triggerAtTime, triggerElapsed, windowLength, maxElapsed, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1079 | interval, operation, directReceiver, listenerTag, flags, true, workSource, |
| 1080 | alarmClock, callingUid, callingPackage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | } |
| 1082 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1084 | private void setImplLocked(int type, long when, long whenElapsed, long windowLength, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1085 | long maxWhen, long interval, PendingIntent operation, IAlarmListener directReceiver, |
| 1086 | String listenerTag, int flags, boolean doValidate, WorkSource workSource, |
| 1087 | AlarmManager.AlarmClockInfo alarmClock, int callingUid, String callingPackage) { |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1088 | Alarm a = new Alarm(type, when, whenElapsed, windowLength, maxWhen, interval, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1089 | operation, directReceiver, listenerTag, workSource, flags, alarmClock, |
| 1090 | callingUid, callingPackage); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1091 | try { |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 1092 | if (ActivityManager.getService().isAppStartModeDisabled(callingUid, callingPackage)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1093 | Slog.w(TAG, "Not setting alarm from " + callingUid + ":" + a |
| 1094 | + " -- package not allowed to start"); |
| 1095 | return; |
| 1096 | } |
| 1097 | } catch (RemoteException e) { |
| 1098 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1099 | removeLocked(operation, directReceiver); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1100 | setImplLocked(a, false, doValidate); |
| 1101 | } |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 1102 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1103 | private void setImplLocked(Alarm a, boolean rebatching, boolean doValidate) { |
| 1104 | if ((a.flags&AlarmManager.FLAG_IDLE_UNTIL) != 0) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1105 | // This is a special alarm that will put the system into idle until it goes off. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1106 | // The caller has given the time they want this to happen at, however we need |
| 1107 | // to pull that earlier if there are existing alarms that have requested to |
Dianne Hackborn | f383129 | 2015-10-15 14:51:06 -0700 | [diff] [blame] | 1108 | // bring us out of idle at an earlier time. |
| 1109 | if (mNextWakeFromIdle != null && a.whenElapsed > mNextWakeFromIdle.whenElapsed) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1110 | a.when = a.whenElapsed = a.maxWhenElapsed = mNextWakeFromIdle.whenElapsed; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1111 | } |
| 1112 | // Add fuzz to make the alarm go off some time before the actual desired time. |
| 1113 | final long nowElapsed = SystemClock.elapsedRealtime(); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1114 | final int fuzz = fuzzForDuration(a.whenElapsed-nowElapsed); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1115 | if (fuzz > 0) { |
| 1116 | if (mRandom == null) { |
| 1117 | mRandom = new Random(); |
| 1118 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1119 | final int delta = mRandom.nextInt(fuzz); |
| 1120 | a.whenElapsed -= delta; |
| 1121 | if (false) { |
| 1122 | Slog.d(TAG, "Alarm when: " + a.whenElapsed); |
| 1123 | Slog.d(TAG, "Delta until alarm: " + (a.whenElapsed-nowElapsed)); |
| 1124 | Slog.d(TAG, "Applied fuzz: " + fuzz); |
| 1125 | Slog.d(TAG, "Final delta: " + delta); |
| 1126 | Slog.d(TAG, "Final when: " + a.whenElapsed); |
| 1127 | } |
| 1128 | a.when = a.maxWhenElapsed = a.whenElapsed; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | } else if (mPendingIdleUntil != null) { |
| 1132 | // We currently have an idle until alarm scheduled; if the new alarm has |
| 1133 | // not explicitly stated it wants to run while idle, then put it on hold. |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1134 | if ((a.flags&(AlarmManager.FLAG_ALLOW_WHILE_IDLE |
| 1135 | | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED |
| 1136 | | AlarmManager.FLAG_WAKE_FROM_IDLE)) |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1137 | == 0) { |
| 1138 | mPendingWhileIdleAlarms.add(a); |
| 1139 | return; |
| 1140 | } |
| 1141 | } |
| 1142 | |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1143 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1144 | if ((a.flags & AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0) { |
| 1145 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 1146 | ent.uid = a.uid; |
| 1147 | ent.pkg = a.operation.getCreatorPackage(); |
| 1148 | ent.tag = a.operation.getTag(""); |
| 1149 | ent.op = "SET"; |
| 1150 | ent.elapsedRealtime = SystemClock.elapsedRealtime(); |
| 1151 | ent.argRealtime = a.whenElapsed; |
| 1152 | mAllowWhileIdleDispatches.add(ent); |
| 1153 | } |
| 1154 | } |
| 1155 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1156 | int whichBatch = ((a.flags&AlarmManager.FLAG_STANDALONE) != 0) |
| 1157 | ? -1 : attemptCoalesceLocked(a.whenElapsed, a.maxWhenElapsed); |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1158 | if (whichBatch < 0) { |
| 1159 | Batch batch = new Batch(a); |
Christopher Tate | 7d57ed8 | 2013-10-25 20:18:03 -0700 | [diff] [blame] | 1160 | addBatchLocked(mAlarmBatches, batch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | } else { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1162 | Batch batch = mAlarmBatches.get(whichBatch); |
Christopher Tate | 7d57ed8 | 2013-10-25 20:18:03 -0700 | [diff] [blame] | 1163 | if (batch.add(a)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 1164 | // The start time of this batch advanced, so batch ordering may |
| 1165 | // have just been broken. Move it to where it now belongs. |
| 1166 | mAlarmBatches.remove(whichBatch); |
| 1167 | addBatchLocked(mAlarmBatches, batch); |
| 1168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1171 | if (a.alarmClock != null) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1172 | mNextAlarmClockMayChange = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1175 | boolean needRebatch = false; |
| 1176 | |
| 1177 | if ((a.flags&AlarmManager.FLAG_IDLE_UNTIL) != 0) { |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1178 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1179 | if (mPendingIdleUntil == null) { |
| 1180 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 1181 | ent.uid = 0; |
| 1182 | ent.pkg = "START IDLE"; |
| 1183 | ent.elapsedRealtime = SystemClock.elapsedRealtime(); |
| 1184 | mAllowWhileIdleDispatches.add(ent); |
| 1185 | } |
| 1186 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1187 | mPendingIdleUntil = a; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1188 | mConstants.updateAllowWhileIdleMinTimeLocked(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1189 | needRebatch = true; |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1190 | } else if ((a.flags&AlarmManager.FLAG_WAKE_FROM_IDLE) != 0) { |
| 1191 | if (mNextWakeFromIdle == null || mNextWakeFromIdle.whenElapsed > a.whenElapsed) { |
| 1192 | mNextWakeFromIdle = a; |
| 1193 | // If this wake from idle is earlier than whatever was previously scheduled, |
| 1194 | // and we are currently idling, then we need to rebatch alarms in case the idle |
| 1195 | // until time needs to be updated. |
| 1196 | if (mPendingIdleUntil != null) { |
| 1197 | needRebatch = true; |
| 1198 | } |
| 1199 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | if (!rebatching) { |
| 1203 | if (DEBUG_VALIDATE) { |
| 1204 | if (doValidate && !validateConsistencyLocked()) { |
| 1205 | Slog.v(TAG, "Tipping-point operation: type=" + a.type + " when=" + a.when |
| 1206 | + " when(hex)=" + Long.toHexString(a.when) |
| 1207 | + " whenElapsed=" + a.whenElapsed |
| 1208 | + " maxWhenElapsed=" + a.maxWhenElapsed |
| 1209 | + " interval=" + a.repeatInterval + " op=" + a.operation |
| 1210 | + " flags=0x" + Integer.toHexString(a.flags)); |
| 1211 | rebatchAllAlarmsLocked(false); |
| 1212 | needRebatch = false; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | if (needRebatch) { |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1217 | rebatchAllAlarmsLocked(false); |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1218 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1219 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1220 | rescheduleKernelAlarmsLocked(); |
| 1221 | updateNextAlarmClockLocked(); |
| 1222 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1225 | private final IBinder mService = new IAlarmManager.Stub() { |
| 1226 | @Override |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1227 | public void set(String callingPackage, |
| 1228 | int type, long triggerAtTime, long windowLength, long interval, int flags, |
| 1229 | PendingIntent operation, IAlarmListener directReceiver, String listenerTag, |
| 1230 | WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1231 | final int callingUid = Binder.getCallingUid(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1232 | |
| 1233 | // make sure the caller is not lying about which package should be blamed for |
| 1234 | // wakelock time spent in alarm delivery |
| 1235 | mAppOps.checkPackage(callingUid, callingPackage); |
| 1236 | |
| 1237 | // Repeating alarms must use PendingIntent, not direct listener |
| 1238 | if (interval != 0) { |
| 1239 | if (directReceiver != null) { |
| 1240 | throw new IllegalArgumentException("Repeating alarms cannot use AlarmReceivers"); |
| 1241 | } |
| 1242 | } |
| 1243 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1244 | if (workSource != null) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1245 | getContext().enforcePermission( |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1246 | android.Manifest.permission.UPDATE_DEVICE_STATS, |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1247 | Binder.getCallingPid(), callingUid, "AlarmManager.set"); |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1250 | // No incoming callers can request either WAKE_FROM_IDLE or |
| 1251 | // ALLOW_WHILE_IDLE_UNRESTRICTED -- we will apply those later as appropriate. |
| 1252 | flags &= ~(AlarmManager.FLAG_WAKE_FROM_IDLE |
| 1253 | | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED); |
| 1254 | |
| 1255 | // Only the system can use FLAG_IDLE_UNTIL -- this is used to tell the alarm |
| 1256 | // manager when to come out of idle mode, which is only for DeviceIdleController. |
| 1257 | if (callingUid != Process.SYSTEM_UID) { |
| 1258 | flags &= ~AlarmManager.FLAG_IDLE_UNTIL; |
| 1259 | } |
| 1260 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1261 | // If this is an exact time alarm, then it can't be batched with other alarms. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1262 | if (windowLength == AlarmManager.WINDOW_EXACT) { |
| 1263 | flags |= AlarmManager.FLAG_STANDALONE; |
| 1264 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1265 | |
| 1266 | // If this alarm is for an alarm clock, then it must be standalone and we will |
| 1267 | // use it to wake early from idle if needed. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1268 | if (alarmClock != null) { |
| 1269 | flags |= AlarmManager.FLAG_WAKE_FROM_IDLE | AlarmManager.FLAG_STANDALONE; |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1270 | |
| 1271 | // If the caller is a core system component or on the user's whitelist, and not calling |
| 1272 | // to do work on behalf of someone else, then always set ALLOW_WHILE_IDLE_UNRESTRICTED. |
| 1273 | // This means we will allow these alarms to go off as normal even while idle, with no |
| 1274 | // timing restrictions. |
| 1275 | } else if (workSource == null && (callingUid < Process.FIRST_APPLICATION_UID |
| 1276 | || Arrays.binarySearch(mDeviceIdleUserWhitelist, |
| 1277 | UserHandle.getAppId(callingUid)) >= 0)) { |
| 1278 | flags |= AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED; |
| 1279 | flags &= ~AlarmManager.FLAG_ALLOW_WHILE_IDLE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1280 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1281 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1282 | setImpl(type, triggerAtTime, windowLength, interval, operation, directReceiver, |
| 1283 | listenerTag, flags, workSource, alarmClock, callingUid, callingPackage); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1284 | } |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 1285 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1286 | @Override |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 1287 | public boolean setTime(long millis) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1288 | getContext().enforceCallingOrSelfPermission( |
| 1289 | "android.permission.SET_TIME", |
| 1290 | "setTime"); |
| 1291 | |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 1292 | if (mNativeData == 0) { |
| 1293 | Slog.w(TAG, "Not setting time since no alarm driver is available."); |
| 1294 | return false; |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 1295 | } |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 1296 | |
| 1297 | synchronized (mLock) { |
| 1298 | return setKernelTime(mNativeData, millis) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1301 | |
| 1302 | @Override |
| 1303 | public void setTimeZone(String tz) { |
| 1304 | getContext().enforceCallingOrSelfPermission( |
| 1305 | "android.permission.SET_TIME_ZONE", |
| 1306 | "setTimeZone"); |
| 1307 | |
| 1308 | final long oldId = Binder.clearCallingIdentity(); |
| 1309 | try { |
| 1310 | setTimeZoneImpl(tz); |
| 1311 | } finally { |
| 1312 | Binder.restoreCallingIdentity(oldId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | } |
| 1314 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1315 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1316 | @Override |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1317 | public void remove(PendingIntent operation, IAlarmListener listener) { |
| 1318 | if (operation == null && listener == null) { |
| 1319 | Slog.w(TAG, "remove() with no intent or listener"); |
| 1320 | return; |
| 1321 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1322 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1323 | synchronized (mLock) { |
| 1324 | removeLocked(operation, listener); |
| 1325 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1327 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1328 | @Override |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1329 | public long getNextWakeFromIdleTime() { |
| 1330 | return getNextWakeFromIdleTimeImpl(); |
| 1331 | } |
| 1332 | |
| 1333 | @Override |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1334 | public AlarmManager.AlarmClockInfo getNextAlarmClock(int userId) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1335 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1336 | Binder.getCallingUid(), userId, false /* allowAll */, false /* requireFull */, |
| 1337 | "getNextAlarmClock", null); |
| 1338 | |
| 1339 | return getNextAlarmClockImpl(userId); |
| 1340 | } |
| 1341 | |
| 1342 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1343 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1344 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1345 | != PackageManager.PERMISSION_GRANTED) { |
| 1346 | pw.println("Permission Denial: can't dump AlarmManager from from pid=" |
| 1347 | + Binder.getCallingPid() |
| 1348 | + ", uid=" + Binder.getCallingUid()); |
| 1349 | return; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1350 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1351 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1352 | dumpImpl(pw); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1353 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1354 | }; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1355 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1356 | public final class LocalService { |
| 1357 | public void setDeviceIdleUserWhitelist(int[] appids) { |
| 1358 | setDeviceIdleUserWhitelistImpl(appids); |
| 1359 | } |
| 1360 | } |
| 1361 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1362 | void dumpImpl(PrintWriter pw) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | synchronized (mLock) { |
| 1364 | pw.println("Current Alarm Manager state:"); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1365 | mConstants.dump(pw); |
| 1366 | pw.println(); |
| 1367 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1368 | final long nowRTC = System.currentTimeMillis(); |
| 1369 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 1370 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 1371 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1372 | pw.print(" nowRTC="); pw.print(nowRTC); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1373 | pw.print("="); pw.print(sdf.format(new Date(nowRTC))); |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 1374 | pw.print(" nowELAPSED="); pw.print(nowELAPSED); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1375 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1376 | pw.print(" mLastTimeChangeClockTime="); pw.print(mLastTimeChangeClockTime); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 1377 | pw.print("="); pw.println(sdf.format(new Date(mLastTimeChangeClockTime))); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1378 | pw.print(" mLastTimeChangeRealtime="); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 1379 | TimeUtils.formatDuration(mLastTimeChangeRealtime, pw); |
| 1380 | pw.println(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1381 | if (!mInteractive) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1382 | pw.print(" Time since non-interactive: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1383 | TimeUtils.formatDuration(nowELAPSED - mNonInteractiveStartTime, pw); |
| 1384 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1385 | pw.print(" Max wakeup delay: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1386 | TimeUtils.formatDuration(currentNonWakeupFuzzLocked(nowELAPSED), pw); |
| 1387 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1388 | pw.print(" Time since last dispatch: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1389 | TimeUtils.formatDuration(nowELAPSED - mLastAlarmDeliveryTime, pw); |
| 1390 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1391 | pw.print(" Next non-wakeup delivery time: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1392 | TimeUtils.formatDuration(nowELAPSED - mNextNonWakeupDeliveryTime, pw); |
| 1393 | pw.println(); |
| 1394 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1395 | |
| 1396 | long nextWakeupRTC = mNextWakeup + (nowRTC - nowELAPSED); |
| 1397 | long nextNonWakeupRTC = mNextNonWakeup + (nowRTC - nowELAPSED); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1398 | pw.print(" Next non-wakeup alarm: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1399 | TimeUtils.formatDuration(mNextNonWakeup, nowELAPSED, pw); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1400 | pw.print(" = "); pw.println(sdf.format(new Date(nextNonWakeupRTC))); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1401 | pw.print(" Next wakeup: "); TimeUtils.formatDuration(mNextWakeup, nowELAPSED, pw); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1402 | pw.print(" = "); pw.println(sdf.format(new Date(nextWakeupRTC))); |
Dianne Hackborn | aa9a59d | 2016-05-23 15:59:18 -0700 | [diff] [blame] | 1403 | pw.print(" Last wakeup: "); TimeUtils.formatDuration(mLastWakeup, nowELAPSED, pw); |
| 1404 | pw.print(" set at "); TimeUtils.formatDuration(mLastWakeupSet, nowELAPSED, pw); |
| 1405 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1406 | pw.print(" Num time change events: "); pw.println(mNumTimeChanged); |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1407 | pw.println(" mDeviceIdleUserWhitelist=" + Arrays.toString(mDeviceIdleUserWhitelist)); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1408 | |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 1409 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1410 | pw.println(" Next alarm clock information: "); |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 1411 | final TreeSet<Integer> users = new TreeSet<>(); |
| 1412 | for (int i = 0; i < mNextAlarmClockForUser.size(); i++) { |
| 1413 | users.add(mNextAlarmClockForUser.keyAt(i)); |
| 1414 | } |
| 1415 | for (int i = 0; i < mPendingSendNextAlarmClockChangedForUser.size(); i++) { |
| 1416 | users.add(mPendingSendNextAlarmClockChangedForUser.keyAt(i)); |
| 1417 | } |
| 1418 | for (int user : users) { |
| 1419 | final AlarmManager.AlarmClockInfo next = mNextAlarmClockForUser.get(user); |
| 1420 | final long time = next != null ? next.getTriggerTime() : 0; |
| 1421 | final boolean pendingSend = mPendingSendNextAlarmClockChangedForUser.get(user); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1422 | pw.print(" user:"); pw.print(user); |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 1423 | pw.print(" pendingSend:"); pw.print(pendingSend); |
| 1424 | pw.print(" time:"); pw.print(time); |
| 1425 | if (time > 0) { |
| 1426 | pw.print(" = "); pw.print(sdf.format(new Date(time))); |
| 1427 | pw.print(" = "); TimeUtils.formatDuration(time, nowRTC, pw); |
| 1428 | } |
| 1429 | pw.println(); |
| 1430 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1431 | if (mAlarmBatches.size() > 0) { |
| 1432 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1433 | pw.print(" Pending alarm batches: "); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1434 | pw.println(mAlarmBatches.size()); |
| 1435 | for (Batch b : mAlarmBatches) { |
| 1436 | pw.print(b); pw.println(':'); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1437 | dumpAlarmList(pw, b.alarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1440 | if (mPendingIdleUntil != null || mPendingWhileIdleAlarms.size() > 0) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1441 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1442 | pw.println(" Idle mode state:"); |
| 1443 | pw.print(" Idling until: "); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1444 | if (mPendingIdleUntil != null) { |
| 1445 | pw.println(mPendingIdleUntil); |
Dianne Hackborn | f383129 | 2015-10-15 14:51:06 -0700 | [diff] [blame] | 1446 | mPendingIdleUntil.dump(pw, " ", nowRTC, nowELAPSED, sdf); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1447 | } else { |
| 1448 | pw.println("null"); |
| 1449 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1450 | pw.println(" Pending alarms:"); |
| 1451 | dumpAlarmList(pw, mPendingWhileIdleAlarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1452 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1453 | if (mNextWakeFromIdle != null) { |
| 1454 | pw.println(); |
| 1455 | pw.print(" Next wake from idle: "); pw.println(mNextWakeFromIdle); |
| 1456 | mNextWakeFromIdle.dump(pw, " ", nowRTC, nowELAPSED, sdf); |
| 1457 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1458 | |
| 1459 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1460 | pw.print(" Past-due non-wakeup alarms: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1461 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 1462 | pw.println(mPendingNonWakeupAlarms.size()); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1463 | dumpAlarmList(pw, mPendingNonWakeupAlarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1464 | } else { |
| 1465 | pw.println("(none)"); |
| 1466 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1467 | pw.print(" Number of delayed alarms: "); pw.print(mNumDelayedAlarms); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1468 | pw.print(", total delay time: "); TimeUtils.formatDuration(mTotalDelayTime, pw); |
| 1469 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1470 | pw.print(" Max delay time: "); TimeUtils.formatDuration(mMaxDelayTime, pw); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1471 | pw.print(", max non-interactive time: "); |
| 1472 | TimeUtils.formatDuration(mNonInteractiveTime, pw); |
| 1473 | pw.println(); |
| 1474 | |
| 1475 | pw.println(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 1476 | pw.print(" Broadcast ref count: "); pw.println(mBroadcastRefCount); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1477 | pw.println(); |
| 1478 | |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 1479 | if (mInFlight.size() > 0) { |
| 1480 | pw.println("Outstanding deliveries:"); |
| 1481 | for (int i = 0; i < mInFlight.size(); i++) { |
| 1482 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 1483 | pw.println(mInFlight.get(i)); |
| 1484 | } |
| 1485 | pw.println(); |
| 1486 | } |
| 1487 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1488 | pw.print(" mAllowWhileIdleMinTime="); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1489 | TimeUtils.formatDuration(mAllowWhileIdleMinTime, pw); |
| 1490 | pw.println(); |
| 1491 | if (mLastAllowWhileIdleDispatch.size() > 0) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1492 | pw.println(" Last allow while idle dispatch times:"); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1493 | for (int i=0; i<mLastAllowWhileIdleDispatch.size(); i++) { |
| 1494 | pw.print(" UID "); |
| 1495 | UserHandle.formatUid(pw, mLastAllowWhileIdleDispatch.keyAt(i)); |
| 1496 | pw.print(": "); |
| 1497 | TimeUtils.formatDuration(mLastAllowWhileIdleDispatch.valueAt(i), |
| 1498 | nowELAPSED, pw); |
| 1499 | pw.println(); |
| 1500 | } |
| 1501 | } |
| 1502 | pw.println(); |
| 1503 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1504 | if (mLog.dump(pw, " Recent problems", " ")) { |
| 1505 | pw.println(); |
| 1506 | } |
| 1507 | |
| 1508 | final FilterStats[] topFilters = new FilterStats[10]; |
| 1509 | final Comparator<FilterStats> comparator = new Comparator<FilterStats>() { |
| 1510 | @Override |
| 1511 | public int compare(FilterStats lhs, FilterStats rhs) { |
| 1512 | if (lhs.aggregateTime < rhs.aggregateTime) { |
| 1513 | return 1; |
| 1514 | } else if (lhs.aggregateTime > rhs.aggregateTime) { |
| 1515 | return -1; |
| 1516 | } |
| 1517 | return 0; |
| 1518 | } |
| 1519 | }; |
| 1520 | int len = 0; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1521 | for (int iu=0; iu<mBroadcastStats.size(); iu++) { |
| 1522 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 1523 | for (int ip=0; ip<uidStats.size(); ip++) { |
| 1524 | BroadcastStats bs = uidStats.valueAt(ip); |
| 1525 | for (int is=0; is<bs.filterStats.size(); is++) { |
| 1526 | FilterStats fs = bs.filterStats.valueAt(is); |
| 1527 | int pos = len > 0 |
| 1528 | ? Arrays.binarySearch(topFilters, 0, len, fs, comparator) : 0; |
| 1529 | if (pos < 0) { |
| 1530 | pos = -pos - 1; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1531 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1532 | if (pos < topFilters.length) { |
| 1533 | int copylen = topFilters.length - pos - 1; |
| 1534 | if (copylen > 0) { |
| 1535 | System.arraycopy(topFilters, pos, topFilters, pos+1, copylen); |
| 1536 | } |
| 1537 | topFilters[pos] = fs; |
| 1538 | if (len < topFilters.length) { |
| 1539 | len++; |
| 1540 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | if (len > 0) { |
| 1546 | pw.println(" Top Alarms:"); |
| 1547 | for (int i=0; i<len; i++) { |
| 1548 | FilterStats fs = topFilters[i]; |
| 1549 | pw.print(" "); |
| 1550 | if (fs.nesting > 0) pw.print("*ACTIVE* "); |
| 1551 | TimeUtils.formatDuration(fs.aggregateTime, pw); |
| 1552 | pw.print(" running, "); pw.print(fs.numWakeup); |
| 1553 | pw.print(" wakeups, "); pw.print(fs.count); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1554 | pw.print(" alarms: "); UserHandle.formatUid(pw, fs.mBroadcastStats.mUid); |
| 1555 | pw.print(":"); pw.print(fs.mBroadcastStats.mPackageName); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1556 | pw.println(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1557 | pw.print(" "); pw.print(fs.mTag); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1558 | pw.println(); |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | pw.println(" "); |
| 1563 | pw.println(" Alarm Stats:"); |
| 1564 | final ArrayList<FilterStats> tmpFilters = new ArrayList<FilterStats>(); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1565 | for (int iu=0; iu<mBroadcastStats.size(); iu++) { |
| 1566 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 1567 | for (int ip=0; ip<uidStats.size(); ip++) { |
| 1568 | BroadcastStats bs = uidStats.valueAt(ip); |
| 1569 | pw.print(" "); |
| 1570 | if (bs.nesting > 0) pw.print("*ACTIVE* "); |
| 1571 | UserHandle.formatUid(pw, bs.mUid); |
| 1572 | pw.print(":"); |
| 1573 | pw.print(bs.mPackageName); |
| 1574 | pw.print(" "); TimeUtils.formatDuration(bs.aggregateTime, pw); |
| 1575 | pw.print(" running, "); pw.print(bs.numWakeup); |
| 1576 | pw.println(" wakeups:"); |
| 1577 | tmpFilters.clear(); |
| 1578 | for (int is=0; is<bs.filterStats.size(); is++) { |
| 1579 | tmpFilters.add(bs.filterStats.valueAt(is)); |
| 1580 | } |
| 1581 | Collections.sort(tmpFilters, comparator); |
| 1582 | for (int i=0; i<tmpFilters.size(); i++) { |
| 1583 | FilterStats fs = tmpFilters.get(i); |
| 1584 | pw.print(" "); |
| 1585 | if (fs.nesting > 0) pw.print("*ACTIVE* "); |
| 1586 | TimeUtils.formatDuration(fs.aggregateTime, pw); |
| 1587 | pw.print(" "); pw.print(fs.numWakeup); |
| 1588 | pw.print(" wakes " ); pw.print(fs.count); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1589 | pw.print(" alarms, last "); |
| 1590 | TimeUtils.formatDuration(fs.lastTime, nowELAPSED, pw); |
| 1591 | pw.println(":"); |
| 1592 | pw.print(" "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1593 | pw.print(fs.mTag); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1594 | pw.println(); |
| 1595 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | } |
| 1597 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1598 | |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1599 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1600 | pw.println(); |
| 1601 | pw.println(" Allow while idle dispatches:"); |
| 1602 | for (int i = 0; i < mAllowWhileIdleDispatches.size(); i++) { |
| 1603 | IdleDispatchEntry ent = mAllowWhileIdleDispatches.get(i); |
| 1604 | pw.print(" "); |
| 1605 | TimeUtils.formatDuration(ent.elapsedRealtime, nowELAPSED, pw); |
| 1606 | pw.print(": "); |
| 1607 | UserHandle.formatUid(pw, ent.uid); |
| 1608 | pw.print(":"); |
| 1609 | pw.println(ent.pkg); |
| 1610 | if (ent.op != null) { |
| 1611 | pw.print(" "); |
| 1612 | pw.print(ent.op); |
| 1613 | pw.print(" / "); |
| 1614 | pw.print(ent.tag); |
| 1615 | if (ent.argRealtime != 0) { |
| 1616 | pw.print(" ("); |
| 1617 | TimeUtils.formatDuration(ent.argRealtime, nowELAPSED, pw); |
| 1618 | pw.print(")"); |
| 1619 | } |
| 1620 | pw.println(); |
| 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1625 | if (WAKEUP_STATS) { |
| 1626 | pw.println(); |
| 1627 | pw.println(" Recent Wakeup History:"); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 1628 | long last = -1; |
| 1629 | for (WakeupEvent event : mRecentWakeups) { |
| 1630 | pw.print(" "); pw.print(sdf.format(new Date(event.when))); |
| 1631 | pw.print('|'); |
| 1632 | if (last < 0) { |
| 1633 | pw.print('0'); |
| 1634 | } else { |
| 1635 | pw.print(event.when - last); |
| 1636 | } |
| 1637 | last = event.when; |
| 1638 | pw.print('|'); pw.print(event.uid); |
| 1639 | pw.print('|'); pw.print(event.action); |
| 1640 | pw.println(); |
| 1641 | } |
| 1642 | pw.println(); |
| 1643 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1647 | private void logBatchesLocked(SimpleDateFormat sdf) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1648 | ByteArrayOutputStream bs = new ByteArrayOutputStream(2048); |
| 1649 | PrintWriter pw = new PrintWriter(bs); |
| 1650 | final long nowRTC = System.currentTimeMillis(); |
| 1651 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 1652 | final int NZ = mAlarmBatches.size(); |
| 1653 | for (int iz = 0; iz < NZ; iz++) { |
| 1654 | Batch bz = mAlarmBatches.get(iz); |
| 1655 | pw.append("Batch "); pw.print(iz); pw.append(": "); pw.println(bz); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1656 | dumpAlarmList(pw, bz.alarms, " ", nowELAPSED, nowRTC, sdf); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1657 | pw.flush(); |
| 1658 | Slog.v(TAG, bs.toString()); |
| 1659 | bs.reset(); |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | private boolean validateConsistencyLocked() { |
| 1664 | if (DEBUG_VALIDATE) { |
| 1665 | long lastTime = Long.MIN_VALUE; |
| 1666 | final int N = mAlarmBatches.size(); |
| 1667 | for (int i = 0; i < N; i++) { |
| 1668 | Batch b = mAlarmBatches.get(i); |
| 1669 | if (b.start >= lastTime) { |
| 1670 | // duplicate start times are okay because of standalone batches |
| 1671 | lastTime = b.start; |
| 1672 | } else { |
| 1673 | Slog.e(TAG, "CONSISTENCY FAILURE: Batch " + i + " is out of order"); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1674 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 1675 | logBatchesLocked(sdf); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1676 | return false; |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | return true; |
| 1681 | } |
| 1682 | |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 1683 | private Batch findFirstWakeupBatchLocked() { |
| 1684 | final int N = mAlarmBatches.size(); |
| 1685 | for (int i = 0; i < N; i++) { |
| 1686 | Batch b = mAlarmBatches.get(i); |
| 1687 | if (b.hasWakeups()) { |
| 1688 | return b; |
| 1689 | } |
| 1690 | } |
| 1691 | return null; |
| 1692 | } |
| 1693 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1694 | long getNextWakeFromIdleTimeImpl() { |
| 1695 | synchronized (mLock) { |
| 1696 | return mNextWakeFromIdle != null ? mNextWakeFromIdle.whenElapsed : Long.MAX_VALUE; |
| 1697 | } |
| 1698 | } |
| 1699 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1700 | void setDeviceIdleUserWhitelistImpl(int[] appids) { |
| 1701 | synchronized (mLock) { |
| 1702 | mDeviceIdleUserWhitelist = appids; |
| 1703 | } |
| 1704 | } |
| 1705 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1706 | AlarmManager.AlarmClockInfo getNextAlarmClockImpl(int userId) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1707 | synchronized (mLock) { |
| 1708 | return mNextAlarmClockForUser.get(userId); |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | /** |
| 1713 | * Recomputes the next alarm clock for all users. |
| 1714 | */ |
| 1715 | private void updateNextAlarmClockLocked() { |
| 1716 | if (!mNextAlarmClockMayChange) { |
| 1717 | return; |
| 1718 | } |
| 1719 | mNextAlarmClockMayChange = false; |
| 1720 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1721 | SparseArray<AlarmManager.AlarmClockInfo> nextForUser = mTmpSparseAlarmClockArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1722 | nextForUser.clear(); |
| 1723 | |
| 1724 | final int N = mAlarmBatches.size(); |
| 1725 | for (int i = 0; i < N; i++) { |
| 1726 | ArrayList<Alarm> alarms = mAlarmBatches.get(i).alarms; |
| 1727 | final int M = alarms.size(); |
| 1728 | |
| 1729 | for (int j = 0; j < M; j++) { |
| 1730 | Alarm a = alarms.get(j); |
| 1731 | if (a.alarmClock != null) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1732 | final int userId = UserHandle.getUserId(a.uid); |
Christopher Tate | 76389c0 | 2016-08-03 14:57:47 -0700 | [diff] [blame] | 1733 | AlarmManager.AlarmClockInfo current = mNextAlarmClockForUser.get(userId); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1734 | |
| 1735 | if (DEBUG_ALARM_CLOCK) { |
Christopher Tate | 76389c0 | 2016-08-03 14:57:47 -0700 | [diff] [blame] | 1736 | Log.v(TAG, "Found AlarmClockInfo " + a.alarmClock + " at " + |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 1737 | formatNextAlarm(getContext(), a.alarmClock, userId) + |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1738 | " for user " + userId); |
| 1739 | } |
| 1740 | |
| 1741 | // Alarms and batches are sorted by time, no need to compare times here. |
| 1742 | if (nextForUser.get(userId) == null) { |
| 1743 | nextForUser.put(userId, a.alarmClock); |
Christopher Tate | 76389c0 | 2016-08-03 14:57:47 -0700 | [diff] [blame] | 1744 | } else if (a.alarmClock.equals(current) |
| 1745 | && current.getTriggerTime() <= nextForUser.get(userId).getTriggerTime()) { |
| 1746 | // same/earlier time and it's the one we cited before, so stick with it |
| 1747 | nextForUser.put(userId, current); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | // Update mNextAlarmForUser with new values. |
| 1754 | final int NN = nextForUser.size(); |
| 1755 | for (int i = 0; i < NN; i++) { |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1756 | AlarmManager.AlarmClockInfo newAlarm = nextForUser.valueAt(i); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1757 | int userId = nextForUser.keyAt(i); |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1758 | AlarmManager.AlarmClockInfo currentAlarm = mNextAlarmClockForUser.get(userId); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1759 | if (!newAlarm.equals(currentAlarm)) { |
| 1760 | updateNextAlarmInfoForUserLocked(userId, newAlarm); |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | // Remove users without any alarm clocks scheduled. |
| 1765 | final int NNN = mNextAlarmClockForUser.size(); |
| 1766 | for (int i = NNN - 1; i >= 0; i--) { |
| 1767 | int userId = mNextAlarmClockForUser.keyAt(i); |
| 1768 | if (nextForUser.get(userId) == null) { |
| 1769 | updateNextAlarmInfoForUserLocked(userId, null); |
| 1770 | } |
| 1771 | } |
| 1772 | } |
| 1773 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1774 | private void updateNextAlarmInfoForUserLocked(int userId, |
| 1775 | AlarmManager.AlarmClockInfo alarmClock) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1776 | if (alarmClock != null) { |
| 1777 | if (DEBUG_ALARM_CLOCK) { |
| 1778 | Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): " + |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 1779 | formatNextAlarm(getContext(), alarmClock, userId)); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1780 | } |
| 1781 | mNextAlarmClockForUser.put(userId, alarmClock); |
| 1782 | } else { |
| 1783 | if (DEBUG_ALARM_CLOCK) { |
| 1784 | Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): None"); |
| 1785 | } |
| 1786 | mNextAlarmClockForUser.remove(userId); |
| 1787 | } |
| 1788 | |
| 1789 | mPendingSendNextAlarmClockChangedForUser.put(userId, true); |
| 1790 | mHandler.removeMessages(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED); |
| 1791 | mHandler.sendEmptyMessage(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED); |
| 1792 | } |
| 1793 | |
| 1794 | /** |
| 1795 | * Updates NEXT_ALARM_FORMATTED and sends NEXT_ALARM_CLOCK_CHANGED_INTENT for all users |
| 1796 | * for which alarm clocks have changed since the last call to this. |
| 1797 | * |
| 1798 | * Do not call with a lock held. Only call from mHandler's thread. |
| 1799 | * |
| 1800 | * @see AlarmHandler#SEND_NEXT_ALARM_CLOCK_CHANGED |
| 1801 | */ |
| 1802 | private void sendNextAlarmClockChanged() { |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1803 | SparseArray<AlarmManager.AlarmClockInfo> pendingUsers = mHandlerSparseAlarmClockArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1804 | pendingUsers.clear(); |
| 1805 | |
| 1806 | synchronized (mLock) { |
| 1807 | final int N = mPendingSendNextAlarmClockChangedForUser.size(); |
| 1808 | for (int i = 0; i < N; i++) { |
| 1809 | int userId = mPendingSendNextAlarmClockChangedForUser.keyAt(i); |
| 1810 | pendingUsers.append(userId, mNextAlarmClockForUser.get(userId)); |
| 1811 | } |
| 1812 | mPendingSendNextAlarmClockChangedForUser.clear(); |
| 1813 | } |
| 1814 | |
| 1815 | final int N = pendingUsers.size(); |
| 1816 | for (int i = 0; i < N; i++) { |
| 1817 | int userId = pendingUsers.keyAt(i); |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 1818 | AlarmManager.AlarmClockInfo alarmClock = pendingUsers.valueAt(i); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1819 | Settings.System.putStringForUser(getContext().getContentResolver(), |
| 1820 | Settings.System.NEXT_ALARM_FORMATTED, |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 1821 | formatNextAlarm(getContext(), alarmClock, userId), |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1822 | userId); |
| 1823 | |
| 1824 | getContext().sendBroadcastAsUser(NEXT_ALARM_CLOCK_CHANGED_INTENT, |
| 1825 | new UserHandle(userId)); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /** |
| 1830 | * Formats an alarm like platform/packages/apps/DeskClock used to. |
| 1831 | */ |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 1832 | private static String formatNextAlarm(final Context context, AlarmManager.AlarmClockInfo info, |
| 1833 | int userId) { |
| 1834 | String skeleton = DateFormat.is24HourFormat(context, userId) ? "EHm" : "Ehma"; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1835 | String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton); |
| 1836 | return (info == null) ? "" : |
| 1837 | DateFormat.format(pattern, info.getTriggerTime()).toString(); |
| 1838 | } |
| 1839 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1840 | void rescheduleKernelAlarmsLocked() { |
| 1841 | // Schedule the next upcoming wakeup alarm. If there is a deliverable batch |
| 1842 | // prior to that which contains no wakeups, we schedule that as well. |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1843 | long nextNonWakeup = 0; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1844 | if (mAlarmBatches.size() > 0) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 1845 | final Batch firstWakeup = findFirstWakeupBatchLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1846 | final Batch firstBatch = mAlarmBatches.get(0); |
Prashant Malani | 753e9e0 | 2015-06-10 17:43:49 -0700 | [diff] [blame] | 1847 | if (firstWakeup != null && mNextWakeup != firstWakeup.start) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 1848 | mNextWakeup = firstWakeup.start; |
Dianne Hackborn | aa9a59d | 2016-05-23 15:59:18 -0700 | [diff] [blame] | 1849 | mLastWakeupSet = SystemClock.elapsedRealtime(); |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 1850 | setLocked(ELAPSED_REALTIME_WAKEUP, firstWakeup.start); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1851 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1852 | if (firstBatch != firstWakeup) { |
| 1853 | nextNonWakeup = firstBatch.start; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1854 | } |
| 1855 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1856 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 1857 | if (nextNonWakeup == 0 || mNextNonWakeupDeliveryTime < nextNonWakeup) { |
| 1858 | nextNonWakeup = mNextNonWakeupDeliveryTime; |
| 1859 | } |
| 1860 | } |
Prashant Malani | 753e9e0 | 2015-06-10 17:43:49 -0700 | [diff] [blame] | 1861 | if (nextNonWakeup != 0 && mNextNonWakeup != nextNonWakeup) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1862 | mNextNonWakeup = nextNonWakeup; |
| 1863 | setLocked(ELAPSED_REALTIME, nextNonWakeup); |
| 1864 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1865 | } |
| 1866 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1867 | private void removeLocked(PendingIntent operation, IAlarmListener directReceiver) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1868 | boolean didRemove = false; |
| 1869 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 1870 | Batch b = mAlarmBatches.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1871 | didRemove |= b.remove(operation, directReceiver); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1872 | if (b.size() == 0) { |
| 1873 | mAlarmBatches.remove(i); |
| 1874 | } |
| 1875 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1876 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1877 | if (mPendingWhileIdleAlarms.get(i).matches(operation, directReceiver)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1878 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 1879 | mPendingWhileIdleAlarms.remove(i); |
| 1880 | } |
| 1881 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1882 | |
| 1883 | if (didRemove) { |
| 1884 | if (DEBUG_BATCH) { |
| 1885 | Slog.v(TAG, "remove(operation) changed bounds; rebatching"); |
| 1886 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1887 | boolean restorePending = false; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1888 | if (mPendingIdleUntil != null && mPendingIdleUntil.matches(operation, directReceiver)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1889 | mPendingIdleUntil = null; |
| 1890 | restorePending = true; |
| 1891 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1892 | if (mNextWakeFromIdle != null && mNextWakeFromIdle.matches(operation, directReceiver)) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1893 | mNextWakeFromIdle = null; |
| 1894 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1895 | rebatchAllAlarmsLocked(true); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1896 | if (restorePending) { |
| 1897 | restorePendingWhileIdleAlarmsLocked(); |
| 1898 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1899 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | void removeLocked(String packageName) { |
| 1904 | boolean didRemove = false; |
| 1905 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 1906 | Batch b = mAlarmBatches.get(i); |
| 1907 | didRemove |= b.remove(packageName); |
| 1908 | if (b.size() == 0) { |
| 1909 | mAlarmBatches.remove(i); |
| 1910 | } |
| 1911 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1912 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1913 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
| 1914 | if (a.matches(packageName)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1915 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 1916 | mPendingWhileIdleAlarms.remove(i); |
| 1917 | } |
| 1918 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1919 | |
| 1920 | if (didRemove) { |
| 1921 | if (DEBUG_BATCH) { |
| 1922 | Slog.v(TAG, "remove(package) changed bounds; rebatching"); |
| 1923 | } |
| 1924 | rebatchAllAlarmsLocked(true); |
| 1925 | rescheduleKernelAlarmsLocked(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1926 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1930 | void removeForStoppedLocked(int uid) { |
| 1931 | boolean didRemove = false; |
| 1932 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 1933 | Batch b = mAlarmBatches.get(i); |
| 1934 | didRemove |= b.removeForStopped(uid); |
| 1935 | if (b.size() == 0) { |
| 1936 | mAlarmBatches.remove(i); |
| 1937 | } |
| 1938 | } |
| 1939 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
| 1940 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 1941 | if (a.uid == uid) { |
| 1942 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 1943 | mPendingWhileIdleAlarms.remove(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | if (didRemove) { |
| 1948 | if (DEBUG_BATCH) { |
| 1949 | Slog.v(TAG, "remove(package) changed bounds; rebatching"); |
| 1950 | } |
| 1951 | rebatchAllAlarmsLocked(true); |
| 1952 | rescheduleKernelAlarmsLocked(); |
| 1953 | updateNextAlarmClockLocked(); |
| 1954 | } |
| 1955 | } |
| 1956 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1957 | void removeUserLocked(int userHandle) { |
| 1958 | boolean didRemove = false; |
| 1959 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 1960 | Batch b = mAlarmBatches.get(i); |
| 1961 | didRemove |= b.remove(userHandle); |
| 1962 | if (b.size() == 0) { |
| 1963 | mAlarmBatches.remove(i); |
| 1964 | } |
| 1965 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1966 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1967 | if (UserHandle.getUserId(mPendingWhileIdleAlarms.get(i).creatorUid) |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1968 | == userHandle) { |
| 1969 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 1970 | mPendingWhileIdleAlarms.remove(i); |
| 1971 | } |
| 1972 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1973 | for (int i = mLastAllowWhileIdleDispatch.size() - 1; i >= 0; i--) { |
| 1974 | if (UserHandle.getUserId(mLastAllowWhileIdleDispatch.keyAt(i)) == userHandle) { |
| 1975 | mLastAllowWhileIdleDispatch.removeAt(i); |
| 1976 | } |
| 1977 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1978 | |
| 1979 | if (didRemove) { |
| 1980 | if (DEBUG_BATCH) { |
| 1981 | Slog.v(TAG, "remove(user) changed bounds; rebatching"); |
| 1982 | } |
| 1983 | rebatchAllAlarmsLocked(true); |
| 1984 | rescheduleKernelAlarmsLocked(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1985 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1989 | void interactiveStateChangedLocked(boolean interactive) { |
| 1990 | if (mInteractive != interactive) { |
| 1991 | mInteractive = interactive; |
| 1992 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 1993 | if (interactive) { |
| 1994 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 1995 | final long thisDelayTime = nowELAPSED - mStartCurrentDelayTime; |
| 1996 | mTotalDelayTime += thisDelayTime; |
| 1997 | if (mMaxDelayTime < thisDelayTime) { |
| 1998 | mMaxDelayTime = thisDelayTime; |
| 1999 | } |
| 2000 | deliverAlarmsLocked(mPendingNonWakeupAlarms, nowELAPSED); |
| 2001 | mPendingNonWakeupAlarms.clear(); |
| 2002 | } |
| 2003 | if (mNonInteractiveStartTime > 0) { |
| 2004 | long dur = nowELAPSED - mNonInteractiveStartTime; |
| 2005 | if (dur > mNonInteractiveTime) { |
| 2006 | mNonInteractiveTime = dur; |
| 2007 | } |
| 2008 | } |
| 2009 | } else { |
| 2010 | mNonInteractiveStartTime = nowELAPSED; |
| 2011 | } |
| 2012 | } |
| 2013 | } |
| 2014 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2015 | boolean lookForPackageLocked(String packageName) { |
| 2016 | for (int i = 0; i < mAlarmBatches.size(); i++) { |
| 2017 | Batch b = mAlarmBatches.get(i); |
| 2018 | if (b.hasPackage(packageName)) { |
| 2019 | return true; |
| 2020 | } |
| 2021 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2022 | for (int i = 0; i < mPendingWhileIdleAlarms.size(); i++) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2023 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
| 2024 | if (a.matches(packageName)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2025 | return true; |
| 2026 | } |
| 2027 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2028 | return false; |
| 2029 | } |
| 2030 | |
| 2031 | private void setLocked(int type, long when) { |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 2032 | if (mNativeData != 0) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2033 | // The kernel never triggers alarms with negative wakeup times |
| 2034 | // so we ensure they are positive. |
| 2035 | long alarmSeconds, alarmNanoseconds; |
| 2036 | if (when < 0) { |
| 2037 | alarmSeconds = 0; |
| 2038 | alarmNanoseconds = 0; |
| 2039 | } else { |
| 2040 | alarmSeconds = when / 1000; |
| 2041 | alarmNanoseconds = (when % 1000) * 1000 * 1000; |
| 2042 | } |
| 2043 | |
Greg Hackmann | f1bdbdd | 2013-12-17 11:56:22 -0800 | [diff] [blame] | 2044 | set(mNativeData, type, alarmSeconds, alarmNanoseconds); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2045 | } else { |
| 2046 | Message msg = Message.obtain(); |
| 2047 | msg.what = ALARM_EVENT; |
| 2048 | |
| 2049 | mHandler.removeMessages(ALARM_EVENT); |
| 2050 | mHandler.sendMessageAtTime(msg, when); |
| 2051 | } |
| 2052 | } |
| 2053 | |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 2054 | private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2055 | String prefix, String label, long nowRTC, long nowELAPSED, SimpleDateFormat sdf) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2056 | for (int i=list.size()-1; i>=0; i--) { |
| 2057 | Alarm a = list.get(i); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2058 | pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i); |
| 2059 | pw.print(": "); pw.println(a); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2060 | a.dump(pw, prefix + " ", nowRTC, nowELAPSED, sdf); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2061 | } |
| 2062 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2063 | |
| 2064 | private static final String labelForType(int type) { |
| 2065 | switch (type) { |
| 2066 | case RTC: return "RTC"; |
| 2067 | case RTC_WAKEUP : return "RTC_WAKEUP"; |
| 2068 | case ELAPSED_REALTIME : return "ELAPSED"; |
| 2069 | case ELAPSED_REALTIME_WAKEUP: return "ELAPSED_WAKEUP"; |
| 2070 | default: |
| 2071 | break; |
| 2072 | } |
| 2073 | return "--unknown--"; |
| 2074 | } |
| 2075 | |
| 2076 | private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2077 | String prefix, long nowELAPSED, long nowRTC, SimpleDateFormat sdf) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2078 | for (int i=list.size()-1; i>=0; i--) { |
| 2079 | Alarm a = list.get(i); |
| 2080 | final String label = labelForType(a.type); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2081 | pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i); |
| 2082 | pw.print(": "); pw.println(a); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2083 | a.dump(pw, prefix + " ", nowRTC, nowELAPSED, sdf); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2084 | } |
| 2085 | } |
| 2086 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 2087 | private native long init(); |
| 2088 | private native void close(long nativeData); |
| 2089 | private native void set(long nativeData, int type, long seconds, long nanoseconds); |
| 2090 | private native int waitForAlarm(long nativeData); |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 2091 | private native int setKernelTime(long nativeData, long millis); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 2092 | private native int setKernelTimezone(long nativeData, int minuteswest); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2094 | boolean triggerAlarmsLocked(ArrayList<Alarm> triggerList, final long nowELAPSED, |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2095 | final long nowRTC) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2096 | boolean hasWakeup = false; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2097 | // batches are temporally sorted, so we need only pull from the |
| 2098 | // start of the list until we either empty it or hit a batch |
| 2099 | // that is not yet deliverable |
Christopher Tate | 6578ad1 | 2013-09-24 17:12:46 -0700 | [diff] [blame] | 2100 | while (mAlarmBatches.size() > 0) { |
| 2101 | Batch batch = mAlarmBatches.get(0); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2102 | if (batch.start > nowELAPSED) { |
| 2103 | // Everything else is scheduled for the future |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2104 | break; |
| 2105 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2106 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2107 | // We will (re)schedule some alarms now; don't let that interfere |
| 2108 | // with delivery of this current batch |
| 2109 | mAlarmBatches.remove(0); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2110 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2111 | final int N = batch.size(); |
| 2112 | for (int i = 0; i < N; i++) { |
| 2113 | Alarm alarm = batch.get(i); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2114 | |
| 2115 | if ((alarm.flags&AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0) { |
| 2116 | // If this is an ALLOW_WHILE_IDLE alarm, we constrain how frequently the app can |
| 2117 | // schedule such alarms. |
| 2118 | long lastTime = mLastAllowWhileIdleDispatch.get(alarm.uid, 0); |
| 2119 | long minTime = lastTime + mAllowWhileIdleMinTime; |
| 2120 | if (nowELAPSED < minTime) { |
| 2121 | // Whoops, it hasn't been long enough since the last ALLOW_WHILE_IDLE |
| 2122 | // alarm went off for this app. Reschedule the alarm to be in the |
| 2123 | // correct time period. |
| 2124 | alarm.whenElapsed = minTime; |
| 2125 | if (alarm.maxWhenElapsed < minTime) { |
| 2126 | alarm.maxWhenElapsed = minTime; |
| 2127 | } |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 2128 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 2129 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 2130 | ent.uid = alarm.uid; |
| 2131 | ent.pkg = alarm.operation.getCreatorPackage(); |
| 2132 | ent.tag = alarm.operation.getTag(""); |
| 2133 | ent.op = "RESCHEDULE"; |
| 2134 | ent.elapsedRealtime = nowELAPSED; |
| 2135 | ent.argRealtime = lastTime; |
| 2136 | mAllowWhileIdleDispatches.add(ent); |
| 2137 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2138 | setImplLocked(alarm, true, false); |
| 2139 | continue; |
| 2140 | } |
| 2141 | } |
| 2142 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2143 | alarm.count = 1; |
| 2144 | triggerList.add(alarm); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2145 | if ((alarm.flags&AlarmManager.FLAG_WAKE_FROM_IDLE) != 0) { |
| 2146 | EventLogTags.writeDeviceIdleWakeFromIdle(mPendingIdleUntil != null ? 1 : 0, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2147 | alarm.statsTag); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2148 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2149 | if (mPendingIdleUntil == alarm) { |
| 2150 | mPendingIdleUntil = null; |
| 2151 | rebatchAllAlarmsLocked(false); |
| 2152 | restorePendingWhileIdleAlarmsLocked(); |
| 2153 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2154 | if (mNextWakeFromIdle == alarm) { |
| 2155 | mNextWakeFromIdle = null; |
| 2156 | rebatchAllAlarmsLocked(false); |
| 2157 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2158 | |
| 2159 | // Recurring alarms may have passed several alarm intervals while the |
| 2160 | // phone was asleep or off, so pass a trigger count when sending them. |
| 2161 | if (alarm.repeatInterval > 0) { |
| 2162 | // this adjustment will be zero if we're late by |
| 2163 | // less than one full repeat interval |
| 2164 | alarm.count += (nowELAPSED - alarm.whenElapsed) / alarm.repeatInterval; |
| 2165 | |
| 2166 | // Also schedule its next recurrence |
| 2167 | final long delta = alarm.count * alarm.repeatInterval; |
| 2168 | final long nextElapsed = alarm.whenElapsed + delta; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 2169 | setImplLocked(alarm.type, alarm.when + delta, nextElapsed, alarm.windowLength, |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2170 | maxTriggerTime(nowELAPSED, nextElapsed, alarm.repeatInterval), |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2171 | alarm.repeatInterval, alarm.operation, null, null, alarm.flags, true, |
| 2172 | alarm.workSource, alarm.alarmClock, alarm.uid, alarm.packageName); |
Christopher Tate | 864d42e | 2014-12-02 11:48:53 -0800 | [diff] [blame] | 2173 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2174 | |
Christopher Tate | 864d42e | 2014-12-02 11:48:53 -0800 | [diff] [blame] | 2175 | if (alarm.wakeup) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2176 | hasWakeup = true; |
| 2177 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2178 | |
| 2179 | // We removed an alarm clock. Let the caller recompute the next alarm clock. |
| 2180 | if (alarm.alarmClock != null) { |
| 2181 | mNextAlarmClockMayChange = true; |
| 2182 | } |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2183 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2184 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2185 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 2186 | // This is a new alarm delivery set; bump the sequence number to indicate that |
| 2187 | // all apps' alarm delivery classes should be recalculated. |
| 2188 | mCurrentSeq++; |
| 2189 | calculateDeliveryPriorities(triggerList); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2190 | Collections.sort(triggerList, mAlarmDispatchComparator); |
| 2191 | |
| 2192 | if (localLOGV) { |
| 2193 | for (int i=0; i<triggerList.size(); i++) { |
| 2194 | Slog.v(TAG, "Triggering alarm #" + i + ": " + triggerList.get(i)); |
| 2195 | } |
| 2196 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2197 | |
| 2198 | return hasWakeup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2199 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2201 | /** |
| 2202 | * This Comparator sorts Alarms into increasing time order. |
| 2203 | */ |
| 2204 | public static class IncreasingTimeOrder implements Comparator<Alarm> { |
| 2205 | public int compare(Alarm a1, Alarm a2) { |
jinho.park | 1acd32a | 2015-05-27 14:44:18 +0900 | [diff] [blame] | 2206 | long when1 = a1.whenElapsed; |
| 2207 | long when2 = a2.whenElapsed; |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 2208 | if (when1 > when2) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2209 | return 1; |
| 2210 | } |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 2211 | if (when1 < when2) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2212 | return -1; |
| 2213 | } |
| 2214 | return 0; |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | private static class Alarm { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2219 | public final int type; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2220 | public final long origWhen; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2221 | public final boolean wakeup; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2222 | public final PendingIntent operation; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2223 | public final IAlarmListener listener; |
| 2224 | public final String listenerTag; |
| 2225 | public final String statsTag; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2226 | public final WorkSource workSource; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2227 | public final int flags; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2228 | public final AlarmManager.AlarmClockInfo alarmClock; |
| 2229 | public final int uid; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2230 | public final int creatorUid; |
| 2231 | public final String packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2232 | public int count; |
| 2233 | public long when; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 2234 | public long windowLength; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2235 | public long whenElapsed; // 'when' in the elapsed time base |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2236 | public long maxWhenElapsed; // also in the elapsed time base |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | public long repeatInterval; |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 2238 | public PriorityClass priorityClass; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2239 | |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 2240 | public Alarm(int _type, long _when, long _whenElapsed, long _windowLength, long _maxWhen, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2241 | long _interval, PendingIntent _op, IAlarmListener _rec, String _listenerTag, |
| 2242 | WorkSource _ws, int _flags, AlarmManager.AlarmClockInfo _info, |
| 2243 | int _uid, String _pkgName) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2244 | type = _type; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2245 | origWhen = _when; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2246 | wakeup = _type == AlarmManager.ELAPSED_REALTIME_WAKEUP |
| 2247 | || _type == AlarmManager.RTC_WAKEUP; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2248 | when = _when; |
| 2249 | whenElapsed = _whenElapsed; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 2250 | windowLength = _windowLength; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2251 | maxWhenElapsed = _maxWhen; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2252 | repeatInterval = _interval; |
| 2253 | operation = _op; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2254 | listener = _rec; |
| 2255 | listenerTag = _listenerTag; |
| 2256 | statsTag = makeTag(_op, _listenerTag, _type); |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2257 | workSource = _ws; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2258 | flags = _flags; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2259 | alarmClock = _info; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2260 | uid = _uid; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2261 | packageName = _pkgName; |
| 2262 | |
| 2263 | creatorUid = (operation != null) ? operation.getCreatorUid() : uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2264 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2265 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2266 | public static String makeTag(PendingIntent pi, String tag, int type) { |
| 2267 | final String alarmString = type == ELAPSED_REALTIME_WAKEUP || type == RTC_WAKEUP |
| 2268 | ? "*walarm*:" : "*alarm*:"; |
| 2269 | return (pi != null) ? pi.getTag(alarmString) : (alarmString + tag); |
| 2270 | } |
| 2271 | |
| 2272 | public WakeupEvent makeWakeupEvent(long nowRTC) { |
| 2273 | return new WakeupEvent(nowRTC, creatorUid, |
| 2274 | (operation != null) |
| 2275 | ? operation.getIntent().getAction() |
| 2276 | : ("<listener>:" + listenerTag)); |
| 2277 | } |
| 2278 | |
| 2279 | // Returns true if either matches |
| 2280 | public boolean matches(PendingIntent pi, IAlarmListener rec) { |
| 2281 | return (operation != null) |
| 2282 | ? operation.equals(pi) |
Lorenzo Colitti | 9b43ce0 | 2015-11-22 22:00:08 +0900 | [diff] [blame] | 2283 | : rec != null && listener.asBinder().equals(rec.asBinder()); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | public boolean matches(String packageName) { |
| 2287 | return (operation != null) |
| 2288 | ? packageName.equals(operation.getTargetPackage()) |
| 2289 | : packageName.equals(this.packageName); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2292 | @Override |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2293 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2294 | StringBuilder sb = new StringBuilder(128); |
| 2295 | sb.append("Alarm{"); |
| 2296 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 2297 | sb.append(" type "); |
| 2298 | sb.append(type); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2299 | sb.append(" when "); |
| 2300 | sb.append(when); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2301 | sb.append(" "); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2302 | if (operation != null) { |
| 2303 | sb.append(operation.getTargetPackage()); |
| 2304 | } else { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2305 | sb.append(packageName); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2306 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2307 | sb.append('}'); |
| 2308 | return sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2309 | } |
| 2310 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2311 | public void dump(PrintWriter pw, String prefix, long nowRTC, long nowELAPSED, |
| 2312 | SimpleDateFormat sdf) { |
| 2313 | final boolean isRtc = (type == RTC || type == RTC_WAKEUP); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2314 | pw.print(prefix); pw.print("tag="); pw.println(statsTag); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2315 | pw.print(prefix); pw.print("type="); pw.print(type); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2316 | pw.print(" whenElapsed="); TimeUtils.formatDuration(whenElapsed, |
| 2317 | nowELAPSED, pw); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2318 | pw.print(" when="); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2319 | if (isRtc) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2320 | pw.print(sdf.format(new Date(when))); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2321 | } else { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2322 | TimeUtils.formatDuration(when, nowELAPSED, pw); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2323 | } |
| 2324 | pw.println(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2325 | pw.print(prefix); pw.print("window="); TimeUtils.formatDuration(windowLength, pw); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2326 | pw.print(" repeatInterval="); pw.print(repeatInterval); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2327 | pw.print(" count="); pw.print(count); |
| 2328 | pw.print(" flags=0x"); pw.println(Integer.toHexString(flags)); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2329 | if (alarmClock != null) { |
| 2330 | pw.print(prefix); pw.println("Alarm clock:"); |
| 2331 | pw.print(prefix); pw.print(" triggerTime="); |
| 2332 | pw.println(sdf.format(new Date(alarmClock.getTriggerTime()))); |
| 2333 | pw.print(prefix); pw.print(" showIntent="); pw.println(alarmClock.getShowIntent()); |
| 2334 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2335 | pw.print(prefix); pw.print("operation="); pw.println(operation); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2336 | if (listener != null) { |
| 2337 | pw.print(prefix); pw.print("listener="); pw.println(listener.asBinder()); |
| 2338 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2339 | } |
| 2340 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2341 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2342 | void recordWakeupAlarms(ArrayList<Batch> batches, long nowELAPSED, long nowRTC) { |
| 2343 | final int numBatches = batches.size(); |
Christopher Tate | e982faf | 2013-07-19 14:51:44 -0700 | [diff] [blame] | 2344 | for (int nextBatch = 0; nextBatch < numBatches; nextBatch++) { |
| 2345 | Batch b = batches.get(nextBatch); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2346 | if (b.start > nowELAPSED) { |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2347 | break; |
| 2348 | } |
| 2349 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2350 | final int numAlarms = b.alarms.size(); |
Christopher Tate | e982faf | 2013-07-19 14:51:44 -0700 | [diff] [blame] | 2351 | for (int nextAlarm = 0; nextAlarm < numAlarms; nextAlarm++) { |
| 2352 | Alarm a = b.alarms.get(nextAlarm); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2353 | mRecentWakeups.add(a.makeWakeupEvent(nowRTC)); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2354 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2355 | } |
| 2356 | } |
| 2357 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2358 | long currentNonWakeupFuzzLocked(long nowELAPSED) { |
| 2359 | long timeSinceOn = nowELAPSED - mNonInteractiveStartTime; |
| 2360 | if (timeSinceOn < 5*60*1000) { |
| 2361 | // If the screen has been off for 5 minutes, only delay by at most two minutes. |
| 2362 | return 2*60*1000; |
| 2363 | } else if (timeSinceOn < 30*60*1000) { |
| 2364 | // If the screen has been off for 30 minutes, only delay by at most 15 minutes. |
| 2365 | return 15*60*1000; |
| 2366 | } else { |
| 2367 | // Otherwise, we will delay by at most an hour. |
| 2368 | return 60*60*1000; |
| 2369 | } |
| 2370 | } |
| 2371 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2372 | static int fuzzForDuration(long duration) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2373 | if (duration < 15*60*1000) { |
| 2374 | // If the duration until the time is less than 15 minutes, the maximum fuzz |
| 2375 | // is the duration. |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2376 | return (int)duration; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2377 | } else if (duration < 90*60*1000) { |
| 2378 | // If duration is less than 1 1/2 hours, the maximum fuzz is 15 minutes, |
| 2379 | return 15*60*1000; |
| 2380 | } else { |
| 2381 | // Otherwise, we will fuzz by at most half an hour. |
| 2382 | return 30*60*1000; |
| 2383 | } |
| 2384 | } |
| 2385 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2386 | boolean checkAllowNonWakeupDelayLocked(long nowELAPSED) { |
| 2387 | if (mInteractive) { |
| 2388 | return false; |
| 2389 | } |
| 2390 | if (mLastAlarmDeliveryTime <= 0) { |
| 2391 | return false; |
| 2392 | } |
minho.choo | 649acab | 2014-12-12 16:13:55 +0900 | [diff] [blame] | 2393 | if (mPendingNonWakeupAlarms.size() > 0 && mNextNonWakeupDeliveryTime < nowELAPSED) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2394 | // This is just a little paranoia, if somehow we have pending non-wakeup alarms |
| 2395 | // and the next delivery time is in the past, then just deliver them all. This |
| 2396 | // avoids bugs where we get stuck in a loop trying to poll for alarms. |
| 2397 | return false; |
| 2398 | } |
| 2399 | long timeSinceLast = nowELAPSED - mLastAlarmDeliveryTime; |
| 2400 | return timeSinceLast <= currentNonWakeupFuzzLocked(nowELAPSED); |
| 2401 | } |
| 2402 | |
| 2403 | void deliverAlarmsLocked(ArrayList<Alarm> triggerList, long nowELAPSED) { |
| 2404 | mLastAlarmDeliveryTime = nowELAPSED; |
| 2405 | for (int i=0; i<triggerList.size(); i++) { |
| 2406 | Alarm alarm = triggerList.get(i); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2407 | final boolean allowWhileIdle = (alarm.flags&AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2408 | try { |
Christopher Tate | 2ff5a73 | 2014-09-18 13:47:57 -0700 | [diff] [blame] | 2409 | if (localLOGV) { |
| 2410 | Slog.v(TAG, "sending alarm " + alarm); |
| 2411 | } |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 2412 | if (RECORD_ALARMS_IN_HISTORY) { |
| 2413 | if (alarm.workSource != null && alarm.workSource.size() > 0) { |
| 2414 | for (int wi=0; wi<alarm.workSource.size(); wi++) { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2415 | ActivityManager.noteAlarmStart( |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2416 | alarm.operation, alarm.workSource.get(wi), alarm.statsTag); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 2417 | } |
| 2418 | } else { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2419 | ActivityManager.noteAlarmStart( |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2420 | alarm.operation, alarm.uid, alarm.statsTag); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 2421 | } |
| 2422 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2423 | mDeliveryTracker.deliverLocked(alarm, nowELAPSED, allowWhileIdle); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2424 | } catch (RuntimeException e) { |
| 2425 | Slog.w(TAG, "Failure sending alarm.", e); |
| 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2430 | private class AlarmThread extends Thread |
| 2431 | { |
| 2432 | public AlarmThread() |
| 2433 | { |
| 2434 | super("AlarmManager"); |
| 2435 | } |
| 2436 | |
| 2437 | public void run() |
| 2438 | { |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2439 | ArrayList<Alarm> triggerList = new ArrayList<Alarm>(); |
| 2440 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2441 | while (true) |
| 2442 | { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 2443 | int result = waitForAlarm(mNativeData); |
Dianne Hackborn | aa9a59d | 2016-05-23 15:59:18 -0700 | [diff] [blame] | 2444 | mLastWakeup = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2445 | |
| 2446 | triggerList.clear(); |
| 2447 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2448 | final long nowRTC = System.currentTimeMillis(); |
| 2449 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 2450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2451 | if ((result & TIME_CHANGED_MASK) != 0) { |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2452 | // The kernel can give us spurious time change notifications due to |
| 2453 | // small adjustments it makes internally; we want to filter those out. |
| 2454 | final long lastTimeChangeClockTime; |
| 2455 | final long expectedClockTime; |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 2456 | synchronized (mLock) { |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2457 | lastTimeChangeClockTime = mLastTimeChangeClockTime; |
| 2458 | expectedClockTime = lastTimeChangeClockTime |
| 2459 | + (nowELAPSED - mLastTimeChangeRealtime); |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 2460 | } |
Christopher Tate | 2b6e459e | 2017-02-17 14:33:52 -0800 | [diff] [blame] | 2461 | if (lastTimeChangeClockTime == 0 || nowRTC < (expectedClockTime-1000) |
| 2462 | || nowRTC > (expectedClockTime+1000)) { |
| 2463 | // The change is by at least +/- 1000 ms (or this is the first change), |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2464 | // let's do it! |
| 2465 | if (DEBUG_BATCH) { |
| 2466 | Slog.v(TAG, "Time changed notification from kernel; rebatching"); |
| 2467 | } |
| 2468 | removeImpl(mTimeTickSender); |
Christopher Tate | cf02471 | 2016-08-05 13:21:45 -0700 | [diff] [blame] | 2469 | removeImpl(mDateChangeSender); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2470 | rebatchAllAlarms(); |
| 2471 | mClockReceiver.scheduleTimeTickEvent(); |
Christopher Tate | cf02471 | 2016-08-05 13:21:45 -0700 | [diff] [blame] | 2472 | mClockReceiver.scheduleDateChangedEvent(); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2473 | synchronized (mLock) { |
| 2474 | mNumTimeChanged++; |
| 2475 | mLastTimeChangeClockTime = nowRTC; |
| 2476 | mLastTimeChangeRealtime = nowELAPSED; |
| 2477 | } |
| 2478 | Intent intent = new Intent(Intent.ACTION_TIME_CHANGED); |
| 2479 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
Christopher Tate | 2b6e459e | 2017-02-17 14:33:52 -0800 | [diff] [blame] | 2480 | | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 2481 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2482 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2483 | |
| 2484 | // The world has changed on us, so we need to re-evaluate alarms |
| 2485 | // regardless of whether the kernel has told us one went off. |
| 2486 | result |= IS_WAKEUP_MASK; |
| 2487 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2488 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2489 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2490 | if (result != TIME_CHANGED_MASK) { |
| 2491 | // If this was anything besides just a time change, then figure what if |
| 2492 | // anything to do about alarms. |
| 2493 | synchronized (mLock) { |
| 2494 | if (localLOGV) Slog.v( |
| 2495 | TAG, "Checking for alarms... rtc=" + nowRTC |
| 2496 | + ", elapsed=" + nowELAPSED); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2497 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2498 | if (WAKEUP_STATS) { |
| 2499 | if ((result & IS_WAKEUP_MASK) != 0) { |
| 2500 | long newEarliest = nowRTC - RECENT_WAKEUP_PERIOD; |
| 2501 | int n = 0; |
| 2502 | for (WakeupEvent event : mRecentWakeups) { |
| 2503 | if (event.when > newEarliest) break; |
| 2504 | n++; // number of now-stale entries at the list head |
| 2505 | } |
| 2506 | for (int i = 0; i < n; i++) { |
| 2507 | mRecentWakeups.remove(); |
| 2508 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2509 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2510 | recordWakeupAlarms(mAlarmBatches, nowELAPSED, nowRTC); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2511 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2512 | } |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2513 | |
| 2514 | boolean hasWakeup = triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC); |
| 2515 | if (!hasWakeup && checkAllowNonWakeupDelayLocked(nowELAPSED)) { |
| 2516 | // if there are no wakeup alarms and the screen is off, we can |
| 2517 | // delay what we have so far until the future. |
| 2518 | if (mPendingNonWakeupAlarms.size() == 0) { |
| 2519 | mStartCurrentDelayTime = nowELAPSED; |
| 2520 | mNextNonWakeupDeliveryTime = nowELAPSED |
| 2521 | + ((currentNonWakeupFuzzLocked(nowELAPSED)*3)/2); |
| 2522 | } |
| 2523 | mPendingNonWakeupAlarms.addAll(triggerList); |
| 2524 | mNumDelayedAlarms += triggerList.size(); |
| 2525 | rescheduleKernelAlarmsLocked(); |
| 2526 | updateNextAlarmClockLocked(); |
| 2527 | } else { |
| 2528 | // now deliver the alarm intents; if there are pending non-wakeup |
| 2529 | // alarms, we need to merge them in to the list. note we don't |
| 2530 | // just deliver them first because we generally want non-wakeup |
| 2531 | // alarms delivered after wakeup alarms. |
| 2532 | rescheduleKernelAlarmsLocked(); |
| 2533 | updateNextAlarmClockLocked(); |
| 2534 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 2535 | calculateDeliveryPriorities(mPendingNonWakeupAlarms); |
| 2536 | triggerList.addAll(mPendingNonWakeupAlarms); |
| 2537 | Collections.sort(triggerList, mAlarmDispatchComparator); |
| 2538 | final long thisDelayTime = nowELAPSED - mStartCurrentDelayTime; |
| 2539 | mTotalDelayTime += thisDelayTime; |
| 2540 | if (mMaxDelayTime < thisDelayTime) { |
| 2541 | mMaxDelayTime = thisDelayTime; |
| 2542 | } |
| 2543 | mPendingNonWakeupAlarms.clear(); |
| 2544 | } |
| 2545 | deliverAlarmsLocked(triggerList, nowELAPSED); |
| 2546 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2547 | } |
Dianne Hackborn | aa9a59d | 2016-05-23 15:59:18 -0700 | [diff] [blame] | 2548 | |
| 2549 | } else { |
| 2550 | // Just in case -- even though no wakeup flag was set, make sure |
| 2551 | // we have updated the kernel to the next alarm time. |
Christopher Tate | 7b5b7cb | 2016-07-07 14:41:36 -0700 | [diff] [blame] | 2552 | synchronized (mLock) { |
| 2553 | rescheduleKernelAlarmsLocked(); |
| 2554 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | } |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 2559 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2560 | /** |
| 2561 | * Attribute blame for a WakeLock. |
| 2562 | * @param pi PendingIntent to attribute blame to if ws is null. |
| 2563 | * @param ws WorkSource to attribute blame. |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2564 | * @param knownUid attribution uid; < 0 if we need to derive it from the PendingIntent sender |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2565 | */ |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2566 | void setWakelockWorkSource(PendingIntent pi, WorkSource ws, int type, String tag, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2567 | int knownUid, boolean first) { |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 2568 | try { |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 2569 | final boolean unimportant = pi == mTimeTickSender; |
| 2570 | mWakeLock.setUnimportantForLogging(unimportant); |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 2571 | if (first || mLastWakeLockUnimportantForLogging) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2572 | mWakeLock.setHistoryTag(tag); |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 2573 | } else { |
| 2574 | mWakeLock.setHistoryTag(null); |
| 2575 | } |
| 2576 | mLastWakeLockUnimportantForLogging = unimportant; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2577 | if (ws != null) { |
| 2578 | mWakeLock.setWorkSource(ws); |
| 2579 | return; |
| 2580 | } |
| 2581 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2582 | final int uid = (knownUid >= 0) |
| 2583 | ? knownUid |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2584 | : ActivityManager.getService().getUidForIntentSender(pi.getTarget()); |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 2585 | if (uid >= 0) { |
| 2586 | mWakeLock.setWorkSource(new WorkSource(uid)); |
| 2587 | return; |
| 2588 | } |
| 2589 | } catch (Exception e) { |
| 2590 | } |
| 2591 | |
| 2592 | // Something went wrong; fall back to attributing the lock to the OS |
| 2593 | mWakeLock.setWorkSource(null); |
| 2594 | } |
| 2595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2596 | private class AlarmHandler extends Handler { |
| 2597 | public static final int ALARM_EVENT = 1; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2598 | public static final int SEND_NEXT_ALARM_CLOCK_CHANGED = 2; |
| 2599 | public static final int LISTENER_TIMEOUT = 3; |
| 2600 | public static final int REPORT_ALARMS_ACTIVE = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2601 | |
| 2602 | public AlarmHandler() { |
| 2603 | } |
| 2604 | |
| 2605 | public void handleMessage(Message msg) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2606 | switch (msg.what) { |
| 2607 | case ALARM_EVENT: { |
| 2608 | ArrayList<Alarm> triggerList = new ArrayList<Alarm>(); |
| 2609 | synchronized (mLock) { |
| 2610 | final long nowRTC = System.currentTimeMillis(); |
| 2611 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 2612 | triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC); |
| 2613 | updateNextAlarmClockLocked(); |
| 2614 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2615 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2616 | // now trigger the alarms without the lock held |
| 2617 | for (int i=0; i<triggerList.size(); i++) { |
| 2618 | Alarm alarm = triggerList.get(i); |
| 2619 | try { |
| 2620 | alarm.operation.send(); |
| 2621 | } catch (PendingIntent.CanceledException e) { |
| 2622 | if (alarm.repeatInterval > 0) { |
| 2623 | // This IntentSender is no longer valid, but this |
| 2624 | // is a repeating alarm, so toss the hoser. |
| 2625 | removeImpl(alarm.operation); |
| 2626 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2627 | } |
| 2628 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2629 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2630 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2631 | |
| 2632 | case SEND_NEXT_ALARM_CLOCK_CHANGED: |
| 2633 | sendNextAlarmClockChanged(); |
| 2634 | break; |
| 2635 | |
| 2636 | case LISTENER_TIMEOUT: |
| 2637 | mDeliveryTracker.alarmTimedOut((IBinder) msg.obj); |
| 2638 | break; |
| 2639 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2640 | case REPORT_ALARMS_ACTIVE: |
| 2641 | if (mLocalDeviceIdleController != null) { |
| 2642 | mLocalDeviceIdleController.setAlarmsActive(msg.arg1 != 0); |
| 2643 | } |
| 2644 | break; |
| 2645 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2646 | default: |
| 2647 | // nope, just ignore it |
| 2648 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | class ClockReceiver extends BroadcastReceiver { |
| 2654 | public ClockReceiver() { |
| 2655 | IntentFilter filter = new IntentFilter(); |
| 2656 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 2657 | filter.addAction(Intent.ACTION_DATE_CHANGED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2658 | getContext().registerReceiver(this, filter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | @Override |
| 2662 | public void onReceive(Context context, Intent intent) { |
| 2663 | if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 2664 | if (DEBUG_BATCH) { |
| 2665 | Slog.v(TAG, "Received TIME_TICK alarm; rescheduling"); |
| 2666 | } |
| 2667 | scheduleTimeTickEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2668 | } else if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) { |
| 2669 | // Since the kernel does not keep track of DST, we need to |
| 2670 | // reset the TZ information at the beginning of each day |
| 2671 | // based off of the current Zone gmt offset + userspace tracked |
| 2672 | // daylight savings information. |
| 2673 | TimeZone zone = TimeZone.getTimeZone(SystemProperties.get(TIMEZONE_PROPERTY)); |
Lavettacn Xiao | c84cc4f | 2010-08-30 12:47:23 +0200 | [diff] [blame] | 2674 | int gmtOffset = zone.getOffset(System.currentTimeMillis()); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 2675 | setKernelTimezone(mNativeData, -(gmtOffset / 60000)); |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 2676 | scheduleDateChangedEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | public void scheduleTimeTickEvent() { |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 2681 | final long currentTime = System.currentTimeMillis(); |
Sungmin Choi | 563914a | 2013-01-10 17:28:40 +0900 | [diff] [blame] | 2682 | final long nextTime = 60000 * ((currentTime / 60000) + 1); |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 2683 | |
| 2684 | // Schedule this event for the amount of time that it would take to get to |
| 2685 | // the top of the next minute. |
Sungmin Choi | 563914a | 2013-01-10 17:28:40 +0900 | [diff] [blame] | 2686 | final long tickEventDelay = nextTime - currentTime; |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 2687 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2688 | final WorkSource workSource = null; // Let system take blame for time tick events. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2689 | setImpl(ELAPSED_REALTIME, SystemClock.elapsedRealtime() + tickEventDelay, 0, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2690 | 0, mTimeTickSender, null, null, AlarmManager.FLAG_STANDALONE, workSource, |
| 2691 | null, Process.myUid(), "android"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2692 | } |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 2693 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2694 | public void scheduleDateChangedEvent() { |
| 2695 | Calendar calendar = Calendar.getInstance(); |
| 2696 | calendar.setTimeInMillis(System.currentTimeMillis()); |
Christopher Tate | afa8b98 | 2016-08-10 16:15:48 -0700 | [diff] [blame] | 2697 | calendar.set(Calendar.HOUR_OF_DAY, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2698 | calendar.set(Calendar.MINUTE, 0); |
| 2699 | calendar.set(Calendar.SECOND, 0); |
| 2700 | calendar.set(Calendar.MILLISECOND, 0); |
| 2701 | calendar.add(Calendar.DAY_OF_MONTH, 1); |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 2702 | |
| 2703 | final WorkSource workSource = null; // Let system take blame for date change events. |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2704 | setImpl(RTC, calendar.getTimeInMillis(), 0, 0, mDateChangeSender, null, null, |
| 2705 | AlarmManager.FLAG_STANDALONE, workSource, null, |
| 2706 | Process.myUid(), "android"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2710 | class InteractiveStateReceiver extends BroadcastReceiver { |
| 2711 | public InteractiveStateReceiver() { |
| 2712 | IntentFilter filter = new IntentFilter(); |
| 2713 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 2714 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 2715 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 2716 | getContext().registerReceiver(this, filter); |
| 2717 | } |
| 2718 | |
| 2719 | @Override |
| 2720 | public void onReceive(Context context, Intent intent) { |
| 2721 | synchronized (mLock) { |
| 2722 | interactiveStateChangedLocked(Intent.ACTION_SCREEN_ON.equals(intent.getAction())); |
| 2723 | } |
| 2724 | } |
| 2725 | } |
| 2726 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2727 | class UninstallReceiver extends BroadcastReceiver { |
| 2728 | public UninstallReceiver() { |
| 2729 | IntentFilter filter = new IntentFilter(); |
| 2730 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 2731 | filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2732 | filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2733 | filter.addDataScheme("package"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2734 | getContext().registerReceiver(this, filter); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2735 | // Register for events related to sdcard installation. |
| 2736 | IntentFilter sdFilter = new IntentFilter(); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 2737 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2738 | sdFilter.addAction(Intent.ACTION_USER_STOPPED); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2739 | sdFilter.addAction(Intent.ACTION_UID_REMOVED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2740 | getContext().registerReceiver(this, sdFilter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | @Override |
| 2744 | public void onReceive(Context context, Intent intent) { |
| 2745 | synchronized (mLock) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2746 | String action = intent.getAction(); |
| 2747 | String pkgList[] = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2748 | if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) { |
| 2749 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 2750 | for (String packageName : pkgList) { |
| 2751 | if (lookForPackageLocked(packageName)) { |
| 2752 | setResultCode(Activity.RESULT_OK); |
| 2753 | return; |
| 2754 | } |
| 2755 | } |
| 2756 | return; |
| 2757 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2758 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2759 | } else if (Intent.ACTION_USER_STOPPED.equals(action)) { |
| 2760 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 2761 | if (userHandle >= 0) { |
| 2762 | removeUserLocked(userHandle); |
| 2763 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2764 | } else if (Intent.ACTION_UID_REMOVED.equals(action)) { |
| 2765 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 2766 | if (uid >= 0) { |
| 2767 | mLastAllowWhileIdleDispatch.delete(uid); |
| 2768 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2769 | } else { |
Dianne Hackborn | 409578f | 2010-03-10 17:23:43 -0800 | [diff] [blame] | 2770 | if (Intent.ACTION_PACKAGE_REMOVED.equals(action) |
| 2771 | && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 2772 | // This package is being updated; don't kill its alarms. |
| 2773 | return; |
| 2774 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2775 | Uri data = intent.getData(); |
| 2776 | if (data != null) { |
| 2777 | String pkg = data.getSchemeSpecificPart(); |
| 2778 | if (pkg != null) { |
| 2779 | pkgList = new String[]{pkg}; |
| 2780 | } |
| 2781 | } |
| 2782 | } |
| 2783 | if (pkgList != null && (pkgList.length > 0)) { |
| 2784 | for (String pkg : pkgList) { |
| 2785 | removeLocked(pkg); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 2786 | mPriorities.remove(pkg); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2787 | for (int i=mBroadcastStats.size()-1; i>=0; i--) { |
| 2788 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(i); |
| 2789 | if (uidStats.remove(pkg) != null) { |
| 2790 | if (uidStats.size() <= 0) { |
| 2791 | mBroadcastStats.removeAt(i); |
| 2792 | } |
| 2793 | } |
| 2794 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 2795 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2800 | |
| 2801 | final class UidObserver extends IUidObserver.Stub { |
| 2802 | @Override public void onUidStateChanged(int uid, int procState) throws RemoteException { |
| 2803 | } |
| 2804 | |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 2805 | @Override public void onUidGone(int uid, boolean disabled) throws RemoteException { |
| 2806 | if (disabled) { |
| 2807 | synchronized (mLock) { |
| 2808 | removeForStoppedLocked(uid); |
| 2809 | } |
| 2810 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | @Override public void onUidActive(int uid) throws RemoteException { |
| 2814 | } |
| 2815 | |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 2816 | @Override public void onUidIdle(int uid, boolean disabled) throws RemoteException { |
| 2817 | if (disabled) { |
| 2818 | synchronized (mLock) { |
| 2819 | removeForStoppedLocked(uid); |
| 2820 | } |
Dianne Hackborn | de9c48b | 2015-11-20 14:49:59 -0800 | [diff] [blame] | 2821 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2822 | } |
| 2823 | }; |
| 2824 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2825 | private final BroadcastStats getStatsLocked(PendingIntent pi) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2826 | String pkg = pi.getCreatorPackage(); |
| 2827 | int uid = pi.getCreatorUid(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2828 | return getStatsLocked(uid, pkg); |
| 2829 | } |
| 2830 | |
| 2831 | private final BroadcastStats getStatsLocked(int uid, String pkgName) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2832 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.get(uid); |
| 2833 | if (uidStats == null) { |
| 2834 | uidStats = new ArrayMap<String, BroadcastStats>(); |
| 2835 | mBroadcastStats.put(uid, uidStats); |
| 2836 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2837 | BroadcastStats bs = uidStats.get(pkgName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2838 | if (bs == null) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2839 | bs = new BroadcastStats(uid, pkgName); |
| 2840 | uidStats.put(pkgName, bs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2841 | } |
| 2842 | return bs; |
| 2843 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2844 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2845 | class DeliveryTracker extends IAlarmCompleteListener.Stub implements PendingIntent.OnFinished { |
| 2846 | private InFlight removeLocked(PendingIntent pi, Intent intent) { |
| 2847 | for (int i = 0; i < mInFlight.size(); i++) { |
| 2848 | if (mInFlight.get(i).mPendingIntent == pi) { |
| 2849 | return mInFlight.remove(i); |
| 2850 | } |
| 2851 | } |
| 2852 | mLog.w("No in-flight alarm for " + pi + " " + intent); |
| 2853 | return null; |
| 2854 | } |
| 2855 | |
| 2856 | private InFlight removeLocked(IBinder listener) { |
| 2857 | for (int i = 0; i < mInFlight.size(); i++) { |
| 2858 | if (mInFlight.get(i).mListener == listener) { |
| 2859 | return mInFlight.remove(i); |
| 2860 | } |
| 2861 | } |
| 2862 | mLog.w("No in-flight alarm for listener " + listener); |
| 2863 | return null; |
| 2864 | } |
| 2865 | |
| 2866 | private void updateStatsLocked(InFlight inflight) { |
| 2867 | final long nowELAPSED = SystemClock.elapsedRealtime(); |
| 2868 | BroadcastStats bs = inflight.mBroadcastStats; |
| 2869 | bs.nesting--; |
| 2870 | if (bs.nesting <= 0) { |
| 2871 | bs.nesting = 0; |
| 2872 | bs.aggregateTime += nowELAPSED - bs.startTime; |
| 2873 | } |
| 2874 | FilterStats fs = inflight.mFilterStats; |
| 2875 | fs.nesting--; |
| 2876 | if (fs.nesting <= 0) { |
| 2877 | fs.nesting = 0; |
| 2878 | fs.aggregateTime += nowELAPSED - fs.startTime; |
| 2879 | } |
| 2880 | if (RECORD_ALARMS_IN_HISTORY) { |
| 2881 | if (inflight.mWorkSource != null && inflight.mWorkSource.size() > 0) { |
| 2882 | for (int wi=0; wi<inflight.mWorkSource.size(); wi++) { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2883 | ActivityManager.noteAlarmFinish( |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2884 | inflight.mPendingIntent, inflight.mWorkSource.get(wi), inflight.mTag); |
| 2885 | } |
| 2886 | } else { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2887 | ActivityManager.noteAlarmFinish( |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2888 | inflight.mPendingIntent, inflight.mUid, inflight.mTag); |
| 2889 | } |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | private void updateTrackingLocked(InFlight inflight) { |
| 2894 | if (inflight != null) { |
| 2895 | updateStatsLocked(inflight); |
| 2896 | } |
| 2897 | mBroadcastRefCount--; |
| 2898 | if (mBroadcastRefCount == 0) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2899 | mHandler.obtainMessage(AlarmHandler.REPORT_ALARMS_ACTIVE, 0).sendToTarget(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2900 | mWakeLock.release(); |
| 2901 | if (mInFlight.size() > 0) { |
| 2902 | mLog.w("Finished all dispatches with " + mInFlight.size() |
| 2903 | + " remaining inflights"); |
| 2904 | for (int i=0; i<mInFlight.size(); i++) { |
| 2905 | mLog.w(" Remaining #" + i + ": " + mInFlight.get(i)); |
| 2906 | } |
| 2907 | mInFlight.clear(); |
| 2908 | } |
| 2909 | } else { |
| 2910 | // the next of our alarms is now in flight. reattribute the wakelock. |
| 2911 | if (mInFlight.size() > 0) { |
| 2912 | InFlight inFlight = mInFlight.get(0); |
| 2913 | setWakelockWorkSource(inFlight.mPendingIntent, inFlight.mWorkSource, |
| 2914 | inFlight.mAlarmType, inFlight.mTag, -1, false); |
| 2915 | } else { |
| 2916 | // should never happen |
| 2917 | mLog.w("Alarm wakelock still held but sent queue empty"); |
| 2918 | mWakeLock.setWorkSource(null); |
| 2919 | } |
| 2920 | } |
| 2921 | } |
| 2922 | |
| 2923 | /** |
| 2924 | * Callback that arrives when a direct-call alarm reports that delivery has finished |
| 2925 | */ |
| 2926 | @Override |
| 2927 | public void alarmComplete(IBinder who) { |
| 2928 | if (who == null) { |
| 2929 | Slog.w(TAG, "Invalid alarmComplete: uid=" + Binder.getCallingUid() |
| 2930 | + " pid=" + Binder.getCallingPid()); |
| 2931 | return; |
| 2932 | } |
| 2933 | |
| 2934 | final long ident = Binder.clearCallingIdentity(); |
| 2935 | try { |
| 2936 | synchronized (mLock) { |
| 2937 | mHandler.removeMessages(AlarmHandler.LISTENER_TIMEOUT, who); |
| 2938 | InFlight inflight = removeLocked(who); |
| 2939 | if (inflight != null) { |
| 2940 | if (DEBUG_LISTENER_CALLBACK) { |
| 2941 | Slog.i(TAG, "alarmComplete() from " + who); |
| 2942 | } |
| 2943 | updateTrackingLocked(inflight); |
| 2944 | } else { |
| 2945 | // Delivery timed out, and the timeout handling already took care of |
| 2946 | // updating our tracking here, so we needn't do anything further. |
| 2947 | if (DEBUG_LISTENER_CALLBACK) { |
| 2948 | Slog.i(TAG, "Late alarmComplete() from " + who); |
| 2949 | } |
| 2950 | } |
| 2951 | } |
| 2952 | } finally { |
| 2953 | Binder.restoreCallingIdentity(ident); |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | /** |
| 2958 | * Callback that arrives when a PendingIntent alarm has finished delivery |
| 2959 | */ |
| 2960 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2961 | public void onSendFinished(PendingIntent pi, Intent intent, int resultCode, |
| 2962 | String resultData, Bundle resultExtras) { |
| 2963 | synchronized (mLock) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2964 | updateTrackingLocked(removeLocked(pi, intent)); |
| 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | /** |
| 2969 | * Timeout of a direct-call alarm delivery |
| 2970 | */ |
| 2971 | public void alarmTimedOut(IBinder who) { |
| 2972 | synchronized (mLock) { |
| 2973 | InFlight inflight = removeLocked(who); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2974 | if (inflight != null) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2975 | // TODO: implement ANR policy for the target |
| 2976 | if (DEBUG_LISTENER_CALLBACK) { |
| 2977 | Slog.i(TAG, "Alarm listener " + who + " timed out in delivery"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2978 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2979 | updateTrackingLocked(inflight); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2980 | } else { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2981 | if (DEBUG_LISTENER_CALLBACK) { |
| 2982 | Slog.i(TAG, "Spurious timeout of listener " + who); |
| 2983 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2984 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | /** |
| 2989 | * Deliver an alarm and set up the post-delivery handling appropriately |
| 2990 | */ |
| 2991 | public void deliverLocked(Alarm alarm, long nowELAPSED, boolean allowWhileIdle) { |
| 2992 | if (alarm.operation != null) { |
| 2993 | // PendingIntent alarm |
| 2994 | try { |
| 2995 | alarm.operation.send(getContext(), 0, |
| 2996 | mBackgroundIntent.putExtra( |
| 2997 | Intent.EXTRA_ALARM_COUNT, alarm.count), |
| 2998 | mDeliveryTracker, mHandler, null, |
| 2999 | allowWhileIdle ? mIdleOptions : null); |
| 3000 | } catch (PendingIntent.CanceledException e) { |
| 3001 | if (alarm.repeatInterval > 0) { |
| 3002 | // This IntentSender is no longer valid, but this |
| 3003 | // is a repeating alarm, so toss it |
| 3004 | removeImpl(alarm.operation); |
| 3005 | } |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 3006 | // No actual delivery was possible, so the delivery tracker's |
| 3007 | // 'finished' callback won't be invoked. We also don't need |
| 3008 | // to do any wakelock or stats tracking, so we have nothing |
| 3009 | // left to do here but go on to the next thing. |
| 3010 | return; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3011 | } |
| 3012 | } else { |
| 3013 | // Direct listener callback alarm |
| 3014 | try { |
| 3015 | if (DEBUG_LISTENER_CALLBACK) { |
| 3016 | Slog.v(TAG, "Alarm to uid=" + alarm.uid |
| 3017 | + " listener=" + alarm.listener.asBinder()); |
| 3018 | } |
| 3019 | alarm.listener.doAlarm(this); |
| 3020 | mHandler.sendMessageDelayed( |
| 3021 | mHandler.obtainMessage(AlarmHandler.LISTENER_TIMEOUT, |
| 3022 | alarm.listener.asBinder()), |
| 3023 | mConstants.LISTENER_TIMEOUT); |
| 3024 | } catch (Exception e) { |
| 3025 | if (DEBUG_LISTENER_CALLBACK) { |
| 3026 | Slog.i(TAG, "Alarm undeliverable to listener " |
| 3027 | + alarm.listener.asBinder(), e); |
| 3028 | } |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 3029 | // As in the PendingIntent.CanceledException case, delivery of the |
| 3030 | // alarm was not possible, so we have no wakelock or timeout or |
| 3031 | // stats management to do. It threw before we posted the delayed |
| 3032 | // timeout message, so we're done here. |
| 3033 | return; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | // The alarm is now in flight; now arrange wakelock and stats tracking |
| 3038 | if (mBroadcastRefCount == 0) { |
| 3039 | setWakelockWorkSource(alarm.operation, alarm.workSource, |
| 3040 | alarm.type, alarm.statsTag, (alarm.operation == null) ? alarm.uid : -1, |
| 3041 | true); |
| 3042 | mWakeLock.acquire(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3043 | mHandler.obtainMessage(AlarmHandler.REPORT_ALARMS_ACTIVE, 1).sendToTarget(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3044 | } |
| 3045 | final InFlight inflight = new InFlight(AlarmManagerService.this, |
| 3046 | alarm.operation, alarm.listener, alarm.workSource, alarm.uid, |
| 3047 | alarm.packageName, alarm.type, alarm.statsTag, nowELAPSED); |
| 3048 | mInFlight.add(inflight); |
| 3049 | mBroadcastRefCount++; |
| 3050 | |
| 3051 | if (allowWhileIdle) { |
| 3052 | // Record the last time this uid handled an ALLOW_WHILE_IDLE alarm. |
| 3053 | mLastAllowWhileIdleDispatch.put(alarm.uid, nowELAPSED); |
| 3054 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 3055 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 3056 | ent.uid = alarm.uid; |
| 3057 | ent.pkg = alarm.packageName; |
| 3058 | ent.tag = alarm.statsTag; |
| 3059 | ent.op = "DELIVER"; |
| 3060 | ent.elapsedRealtime = nowELAPSED; |
| 3061 | mAllowWhileIdleDispatches.add(ent); |
| 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | final BroadcastStats bs = inflight.mBroadcastStats; |
| 3066 | bs.count++; |
| 3067 | if (bs.nesting == 0) { |
| 3068 | bs.nesting = 1; |
| 3069 | bs.startTime = nowELAPSED; |
| 3070 | } else { |
| 3071 | bs.nesting++; |
| 3072 | } |
| 3073 | final FilterStats fs = inflight.mFilterStats; |
| 3074 | fs.count++; |
| 3075 | if (fs.nesting == 0) { |
| 3076 | fs.nesting = 1; |
| 3077 | fs.startTime = nowELAPSED; |
| 3078 | } else { |
| 3079 | fs.nesting++; |
| 3080 | } |
| 3081 | if (alarm.type == ELAPSED_REALTIME_WAKEUP |
| 3082 | || alarm.type == RTC_WAKEUP) { |
| 3083 | bs.numWakeup++; |
| 3084 | fs.numWakeup++; |
| 3085 | if (alarm.workSource != null && alarm.workSource.size() > 0) { |
| 3086 | for (int wi=0; wi<alarm.workSource.size(); wi++) { |
Christopher Tate | 0029f57 | 2016-06-14 10:16:28 -0700 | [diff] [blame] | 3087 | final String wsName = alarm.workSource.getName(wi); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 3088 | ActivityManager.noteWakeupAlarm( |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3089 | alarm.operation, alarm.workSource.get(wi), |
Christopher Tate | 0029f57 | 2016-06-14 10:16:28 -0700 | [diff] [blame] | 3090 | (wsName != null) ? wsName : alarm.packageName, |
| 3091 | alarm.statsTag); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 3092 | } |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 3093 | } else { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 3094 | ActivityManager.noteWakeupAlarm( |
Joe Onorato | 78bcdf9 | 2016-05-09 10:10:29 -0700 | [diff] [blame] | 3095 | alarm.operation, alarm.uid, alarm.packageName, alarm.statsTag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3096 | } |
| 3097 | } |
| 3098 | } |
| 3099 | } |
| 3100 | } |