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