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