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