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 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 19 | import static android.app.AlarmManager.ELAPSED_REALTIME; |
| 20 | import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP; |
| 21 | import static android.app.AlarmManager.FLAG_ALLOW_WHILE_IDLE; |
| 22 | import static android.app.AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED; |
| 23 | import static android.app.AlarmManager.RTC; |
| 24 | import static android.app.AlarmManager.RTC_WAKEUP; |
| 25 | |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 26 | import android.annotation.UserIdInt; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 27 | import android.app.Activity; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 28 | import android.app.ActivityManager; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 29 | import android.app.AlarmManager; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 30 | import android.app.AppOpsManager; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 31 | import android.app.BroadcastOptions; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 32 | import android.app.IAlarmCompleteListener; |
| 33 | import android.app.IAlarmListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.app.IAlarmManager; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 35 | import android.app.IUidObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.app.PendingIntent; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 37 | import android.app.usage.UsageStatsManager; |
| 38 | import android.app.usage.UsageStatsManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.content.BroadcastReceiver; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 40 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.Context; |
| 42 | import android.content.Intent; |
| 43 | import android.content.IntentFilter; |
Christopher Tate | bb9cce5 | 2017-04-18 14:19:43 -0700 | [diff] [blame] | 44 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
Christopher Tate | bb9cce5 | 2017-04-18 14:19:43 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageManager.NameNotFoundException; |
| 47 | import android.content.pm.PermissionInfo; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 48 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.net.Uri; |
| 50 | import android.os.Binder; |
Christopher Tate | 3d65d4d | 2018-12-11 14:55:19 -0800 | [diff] [blame] | 51 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.Bundle; |
Christopher Tate | 24757146 | 2017-04-10 11:45:05 -0700 | [diff] [blame] | 53 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.Handler; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 55 | import android.os.IBinder; |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 56 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.os.Message; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 58 | import android.os.ParcelableException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.os.PowerManager; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 60 | import android.os.Process; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 61 | import android.os.RemoteException; |
mswest46 | 3f4c99d | 2018-02-01 10:13:10 -0800 | [diff] [blame] | 62 | import android.os.ResultReceiver; |
| 63 | import android.os.ShellCallback; |
| 64 | import android.os.ShellCommand; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.os.SystemClock; |
| 66 | import android.os.SystemProperties; |
Tim Murray | 175c0f9 | 2017-11-28 15:01:04 -0800 | [diff] [blame] | 67 | import android.os.Trace; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 68 | import android.os.UserHandle; |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 69 | import android.os.WorkSource; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 70 | import android.provider.Settings; |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 71 | import android.system.Os; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.text.TextUtils; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 73 | import android.text.format.DateFormat; |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 74 | import android.text.format.DateUtils; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 75 | import android.util.ArrayMap; |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 76 | import android.util.ArraySet; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 77 | import android.util.KeyValueListParser; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 78 | import android.util.Log; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 79 | import android.util.LongArrayQueue; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 80 | import android.util.NtpTrustedTime; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 81 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 82 | import android.util.Slog; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 83 | import android.util.SparseArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 84 | import android.util.SparseBooleanArray; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 85 | import android.util.SparseLongArray; |
Bookatz | 7948c87 | 2018-09-04 12:58:33 -0700 | [diff] [blame] | 86 | import android.util.StatsLog; |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 87 | import android.util.TimeUtils; |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 88 | import android.util.proto.ProtoOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 90 | import com.android.internal.annotations.GuardedBy; |
| 91 | import com.android.internal.annotations.VisibleForTesting; |
| 92 | import com.android.internal.util.ArrayUtils; |
| 93 | import com.android.internal.util.DumpUtils; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 94 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 95 | import com.android.internal.util.LocalLog; |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 96 | import com.android.internal.util.StatLogger; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 97 | import com.android.server.AppStateTracker.Listener; |
| 98 | |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 99 | import java.io.ByteArrayOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import java.io.FileDescriptor; |
| 101 | import java.io.PrintWriter; |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 102 | import java.text.SimpleDateFormat; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 103 | import java.time.DateTimeException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | import java.util.ArrayList; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 105 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import java.util.Calendar; |
| 107 | import java.util.Collections; |
| 108 | import java.util.Comparator; |
Mike Lockwood | 1f7b413 | 2009-11-20 15:12:51 -0500 | [diff] [blame] | 109 | import java.util.Date; |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 110 | import java.util.HashMap; |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 111 | import java.util.LinkedList; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 112 | import java.util.Locale; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 113 | import java.util.Random; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | import java.util.TimeZone; |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 115 | import java.util.TreeSet; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 116 | import java.util.function.Predicate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 118 | /** |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 119 | * Alarm manager implementation. |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 120 | * |
| 121 | * Unit test: |
| 122 | atest $ANDROID_BUILD_TOP/frameworks/base/services/tests/servicestests/src/com/android/server/AlarmManagerServiceTest.java |
| 123 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 124 | class AlarmManagerService extends SystemService { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 125 | private static final int RTC_WAKEUP_MASK = 1 << RTC_WAKEUP; |
| 126 | private static final int RTC_MASK = 1 << RTC; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 127 | private static final int ELAPSED_REALTIME_WAKEUP_MASK = 1 << ELAPSED_REALTIME_WAKEUP; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 128 | private static final int ELAPSED_REALTIME_MASK = 1 << ELAPSED_REALTIME; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 129 | static final int TIME_CHANGED_MASK = 1 << 16; |
| 130 | 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] | 131 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 132 | // 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] | 133 | static final int TYPE_NONWAKEUP_MASK = 0x1; // low bit => non-wakeup |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 134 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 135 | static final String TAG = "AlarmManager"; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 136 | static final boolean localLOGV = false; |
| 137 | static final boolean DEBUG_BATCH = localLOGV || false; |
| 138 | static final boolean DEBUG_VALIDATE = localLOGV || false; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 139 | static final boolean DEBUG_ALARM_CLOCK = localLOGV || false; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 140 | static final boolean DEBUG_LISTENER_CALLBACK = localLOGV || false; |
Makoto Onuki | 3e7d845 | 2017-03-02 15:33:17 -0800 | [diff] [blame] | 141 | static final boolean DEBUG_WAKELOCK = localLOGV || false; |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 142 | static final boolean DEBUG_BG_LIMIT = localLOGV || false; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 143 | static final boolean DEBUG_STANDBY = localLOGV || false; |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 144 | static final boolean RECORD_ALARMS_IN_HISTORY = true; |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 145 | static final boolean RECORD_DEVICE_IDLE_ALARMS = false; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 146 | static final String TIMEZONE_PROPERTY = "persist.sys.timezone"; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 147 | |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 148 | static final int TICK_HISTORY_DEPTH = 10; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 149 | static final long MILLIS_IN_DAY = 24 * 60 * 60 * 1000; |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 150 | |
Christopher Tate | f215971 | 2018-03-27 16:04:14 -0700 | [diff] [blame] | 151 | // Indices into the APP_STANDBY_MIN_DELAYS and KEYS_APP_STANDBY_DELAY arrays |
| 152 | static final int ACTIVE_INDEX = 0; |
| 153 | static final int WORKING_INDEX = 1; |
| 154 | static final int FREQUENT_INDEX = 2; |
| 155 | static final int RARE_INDEX = 3; |
| 156 | static final int NEVER_INDEX = 4; |
| 157 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 158 | private final Intent mBackgroundIntent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | = new Intent().addFlags(Intent.FLAG_FROM_BACKGROUND); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 160 | static final IncreasingTimeOrder sIncreasingTimeOrder = new IncreasingTimeOrder(); |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 161 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 162 | static final boolean WAKEUP_STATS = false; |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 163 | |
Christopher Tate | 24cd46f | 2016-02-02 14:28:01 -0800 | [diff] [blame] | 164 | private static final Intent NEXT_ALARM_CLOCK_CHANGED_INTENT = |
| 165 | new Intent(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED) |
Christopher Tate | 50191db | 2017-05-09 16:02:25 -0700 | [diff] [blame] | 166 | .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
| 167 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 168 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 169 | final LocalLog mLog = new LocalLog(TAG); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 170 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 171 | AppOpsManager mAppOps; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 172 | DeviceIdleController.LocalService mLocalDeviceIdleController; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 173 | private UsageStatsManagerInternal mUsageStatsManagerInternal; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 174 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 175 | final Object mLock = new Object(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 176 | |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 177 | // List of alarms per uid deferred due to user applied background restrictions on the source app |
| 178 | SparseArray<ArrayList<Alarm>> mPendingBackgroundAlarms = new SparseArray<>(); |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 179 | private long mNextWakeup; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 180 | private long mNextNonWakeup; |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 181 | private long mNextWakeUpSetAt; |
| 182 | private long mNextNonWakeUpSetAt; |
Dianne Hackborn | aa9a59d | 2016-05-23 15:59:18 -0700 | [diff] [blame] | 183 | private long mLastWakeup; |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 184 | private long mLastTrigger; |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 185 | |
Christopher Tate | 12cf0b6 | 2018-01-05 18:40:14 -0800 | [diff] [blame] | 186 | private long mLastTickSet; |
Christopher Tate | 12cf0b6 | 2018-01-05 18:40:14 -0800 | [diff] [blame] | 187 | private long mLastTickReceived; |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 188 | private long mLastTickAdded; |
| 189 | private long mLastTickRemoved; |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 190 | // ring buffer of recent TIME_TICK issuance, in the elapsed timebase |
| 191 | private final long[] mTickHistory = new long[TICK_HISTORY_DEPTH]; |
| 192 | private int mNextTickHistory; |
| 193 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 194 | private final Injector mInjector; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 195 | int mBroadcastRefCount = 0; |
| 196 | PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 197 | ArrayList<Alarm> mPendingNonWakeupAlarms = new ArrayList<>(); |
| 198 | ArrayList<InFlight> mInFlight = new ArrayList<>(); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 199 | AlarmHandler mHandler; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 200 | AppWakeupHistory mAppWakeupHistory; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 201 | ClockReceiver mClockReceiver; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 202 | final DeliveryTracker mDeliveryTracker = new DeliveryTracker(); |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 203 | Intent mTimeTickIntent; |
| 204 | IAlarmListener mTimeTickTrigger; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 205 | PendingIntent mDateChangeSender; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 206 | Random mRandom; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 207 | boolean mInteractive = true; |
| 208 | long mNonInteractiveStartTime; |
| 209 | long mNonInteractiveTime; |
| 210 | long mLastAlarmDeliveryTime; |
| 211 | long mStartCurrentDelayTime; |
| 212 | long mNextNonWakeupDeliveryTime; |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 213 | long mLastTimeChangeClockTime; |
| 214 | long mLastTimeChangeRealtime; |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 215 | int mNumTimeChanged; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 216 | |
Christopher Tate | bb9cce5 | 2017-04-18 14:19:43 -0700 | [diff] [blame] | 217 | // Bookkeeping about the identity of the "System UI" package, determined at runtime. |
| 218 | |
| 219 | /** |
| 220 | * This permission must be defined by the canonical System UI package, |
| 221 | * with protection level "signature". |
| 222 | */ |
| 223 | private static final String SYSTEM_UI_SELF_PERMISSION = |
| 224 | "android.permission.systemui.IDENTITY"; |
| 225 | |
| 226 | /** |
| 227 | * At boot we use SYSTEM_UI_SELF_PERMISSION to look up the definer's uid. |
| 228 | */ |
| 229 | int mSystemUiUid; |
| 230 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 231 | /** |
| 232 | * 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] | 233 | * used to determine the earliest we can dispatch the next such alarm. Times are in the |
| 234 | * 'elapsed' timebase. |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 235 | */ |
| 236 | final SparseLongArray mLastAllowWhileIdleDispatch = new SparseLongArray(); |
| 237 | |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 238 | /** |
| 239 | * For each uid, we store whether the last allow-while-idle alarm was dispatched while |
| 240 | * the uid was in foreground or not. We will use the allow_while_idle_short_time in such cases. |
| 241 | */ |
| 242 | final SparseBooleanArray mUseAllowWhileIdleShortTime = new SparseBooleanArray(); |
| 243 | |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 244 | final static class IdleDispatchEntry { |
| 245 | int uid; |
| 246 | String pkg; |
| 247 | String tag; |
| 248 | String op; |
| 249 | long elapsedRealtime; |
| 250 | long argRealtime; |
| 251 | } |
| 252 | final ArrayList<IdleDispatchEntry> mAllowWhileIdleDispatches = new ArrayList(); |
| 253 | |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 254 | interface Stats { |
| 255 | int REBATCH_ALL_ALARMS = 0; |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 256 | int REORDER_ALARMS_FOR_STANDBY = 1; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | private final StatLogger mStatLogger = new StatLogger(new String[] { |
| 260 | "REBATCH_ALL_ALARMS", |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 261 | "REORDER_ALARMS_FOR_STANDBY", |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 262 | }); |
| 263 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Broadcast options to use for FLAG_ALLOW_WHILE_IDLE. |
| 266 | */ |
| 267 | Bundle mIdleOptions; |
| 268 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 269 | private final SparseArray<AlarmManager.AlarmClockInfo> mNextAlarmClockForUser = |
| 270 | new SparseArray<>(); |
| 271 | private final SparseArray<AlarmManager.AlarmClockInfo> mTmpSparseAlarmClockArray = |
| 272 | new SparseArray<>(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 273 | private final SparseBooleanArray mPendingSendNextAlarmClockChangedForUser = |
| 274 | new SparseBooleanArray(); |
| 275 | private boolean mNextAlarmClockMayChange; |
| 276 | |
| 277 | // May only use on mHandler's thread, locking not required. |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 278 | private final SparseArray<AlarmManager.AlarmClockInfo> mHandlerSparseAlarmClockArray = |
| 279 | new SparseArray<>(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 280 | |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 281 | private AppStateTracker mAppStateTracker; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 282 | private boolean mAppStandbyParole; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 283 | |
| 284 | /** |
| 285 | * A rolling window history of previous times when an alarm was sent to a package. |
| 286 | */ |
| 287 | private static class AppWakeupHistory { |
| 288 | private ArrayMap<Pair<String, Integer>, LongArrayQueue> mPackageHistory = |
| 289 | new ArrayMap<>(); |
| 290 | private long mWindowSize; |
| 291 | |
| 292 | AppWakeupHistory(long windowSize) { |
| 293 | mWindowSize = windowSize; |
| 294 | } |
| 295 | |
| 296 | void recordAlarmForPackage(String packageName, int userId, long nowElapsed) { |
| 297 | final Pair<String, Integer> packageUser = Pair.create(packageName, userId); |
| 298 | LongArrayQueue history = mPackageHistory.get(packageUser); |
| 299 | if (history == null) { |
| 300 | history = new LongArrayQueue(); |
| 301 | mPackageHistory.put(packageUser, history); |
| 302 | } |
| 303 | if (history.size() == 0 || history.peekLast() < nowElapsed) { |
| 304 | history.addLast(nowElapsed); |
| 305 | } |
| 306 | snapToWindow(history); |
| 307 | } |
| 308 | |
| 309 | void removeForUser(int userId) { |
| 310 | for (int i = mPackageHistory.size() - 1; i >= 0; i--) { |
| 311 | final Pair<String, Integer> packageUserKey = mPackageHistory.keyAt(i); |
| 312 | if (packageUserKey.second == userId) { |
| 313 | mPackageHistory.removeAt(i); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void removeForPackage(String packageName, int userId) { |
| 319 | final Pair<String, Integer> packageUser = Pair.create(packageName, userId); |
| 320 | mPackageHistory.remove(packageUser); |
| 321 | } |
| 322 | |
| 323 | private void snapToWindow(LongArrayQueue history) { |
| 324 | while (history.peekFirst() + mWindowSize < history.peekLast()) { |
| 325 | history.removeFirst(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | int getTotalWakeupsInWindow(String packageName, int userId) { |
| 330 | final LongArrayQueue history = mPackageHistory.get(Pair.create(packageName, userId)); |
| 331 | return (history == null) ? 0 : history.size(); |
| 332 | } |
| 333 | |
| 334 | long getLastWakeupForPackage(String packageName, int userId, int positionFromEnd) { |
| 335 | final LongArrayQueue history = mPackageHistory.get(Pair.create(packageName, userId)); |
| 336 | if (history == null) { |
| 337 | return 0; |
| 338 | } |
| 339 | final int i = history.size() - positionFromEnd; |
| 340 | return (i < 0) ? 0 : history.get(i); |
| 341 | } |
| 342 | |
| 343 | void dump(PrintWriter pw, String prefix, long nowElapsed) { |
| 344 | dump(new IndentingPrintWriter(pw, " ").setIndent(prefix), nowElapsed); |
| 345 | } |
| 346 | |
| 347 | void dump(IndentingPrintWriter pw, long nowElapsed) { |
| 348 | pw.println("App Alarm history:"); |
| 349 | pw.increaseIndent(); |
| 350 | for (int i = 0; i < mPackageHistory.size(); i++) { |
| 351 | final Pair<String, Integer> packageUser = mPackageHistory.keyAt(i); |
| 352 | final LongArrayQueue timestamps = mPackageHistory.valueAt(i); |
| 353 | pw.print(packageUser.first); |
| 354 | pw.print(", u"); |
| 355 | pw.print(packageUser.second); |
| 356 | pw.print(": "); |
| 357 | // limit dumping to a max of 100 values |
| 358 | final int lastIdx = Math.max(0, timestamps.size() - 100); |
| 359 | for (int j = timestamps.size() - 1; j >= lastIdx; j--) { |
| 360 | TimeUtils.formatDuration(timestamps.get(j), nowElapsed, pw); |
| 361 | pw.print(", "); |
| 362 | } |
| 363 | pw.println(); |
| 364 | } |
| 365 | pw.decreaseIndent(); |
| 366 | } |
| 367 | } |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 368 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 369 | /** |
| 370 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 371 | * global Settings. Any access to this class or its fields should be done while |
| 372 | * holding the AlarmManagerService.mLock lock. |
| 373 | */ |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 374 | @VisibleForTesting |
| 375 | final class Constants extends ContentObserver { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 376 | // Key names stored in the settings value. |
Suprabh Shukla | adfa6e8 | 2018-10-19 17:32:08 -0700 | [diff] [blame] | 377 | @VisibleForTesting |
| 378 | static final String KEY_MIN_FUTURITY = "min_futurity"; |
| 379 | @VisibleForTesting |
| 380 | static final String KEY_MIN_INTERVAL = "min_interval"; |
| 381 | @VisibleForTesting |
| 382 | static final String KEY_MAX_INTERVAL = "max_interval"; |
| 383 | @VisibleForTesting |
| 384 | static final String KEY_ALLOW_WHILE_IDLE_SHORT_TIME = "allow_while_idle_short_time"; |
| 385 | @VisibleForTesting |
| 386 | static final String KEY_ALLOW_WHILE_IDLE_LONG_TIME = "allow_while_idle_long_time"; |
| 387 | @VisibleForTesting |
| 388 | static final String KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 389 | = "allow_while_idle_whitelist_duration"; |
Suprabh Shukla | adfa6e8 | 2018-10-19 17:32:08 -0700 | [diff] [blame] | 390 | @VisibleForTesting |
| 391 | static final String KEY_LISTENER_TIMEOUT = "listener_timeout"; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 392 | @VisibleForTesting |
| 393 | static final String KEY_APP_STANDBY_QUOTAS_ENABLED = "app_standby_quotas_enabled"; |
| 394 | private static final String KEY_APP_STANDBY_WINDOW = "app_standby_window"; |
| 395 | @VisibleForTesting |
| 396 | final String[] KEYS_APP_STANDBY_QUOTAS = { |
| 397 | "standby_active_quota", |
| 398 | "standby_working_quota", |
| 399 | "standby_frequent_quota", |
| 400 | "standby_rare_quota", |
| 401 | "standby_never_quota", |
| 402 | }; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 403 | |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 404 | // Keys for specifying throttling delay based on app standby bucketing |
| 405 | private final String[] KEYS_APP_STANDBY_DELAY = { |
| 406 | "standby_active_delay", |
| 407 | "standby_working_delay", |
| 408 | "standby_frequent_delay", |
| 409 | "standby_rare_delay", |
| 410 | "standby_never_delay", |
| 411 | }; |
| 412 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 413 | private static final long DEFAULT_MIN_FUTURITY = 5 * 1000; |
| 414 | private static final long DEFAULT_MIN_INTERVAL = 60 * 1000; |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 415 | private static final long DEFAULT_MAX_INTERVAL = 365 * DateUtils.DAY_IN_MILLIS; |
Christopher Tate | aa244da | 2015-08-27 15:57:57 -0700 | [diff] [blame] | 416 | 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] | 417 | 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] | 418 | private static final long DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION = 10*1000; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 419 | private static final long DEFAULT_LISTENER_TIMEOUT = 5 * 1000; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 420 | private static final boolean DEFAULT_APP_STANDBY_QUOTAS_ENABLED = true; |
| 421 | private static final long DEFAULT_APP_STANDBY_WINDOW = 60 * 60 * 1000; // 1 hr |
| 422 | /** |
| 423 | * Max number of times an app can receive alarms in {@link #APP_STANDBY_WINDOW} |
| 424 | */ |
| 425 | private final int[] DEFAULT_APP_STANDBY_QUOTAS = { |
| 426 | 720, // Active |
| 427 | 10, // Working |
| 428 | 2, // Frequent |
| 429 | 1, // Rare |
| 430 | 0 // Never |
| 431 | }; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 432 | private final long[] DEFAULT_APP_STANDBY_DELAYS = { |
| 433 | 0, // Active |
| 434 | 6 * 60_000, // Working |
| 435 | 30 * 60_000, // Frequent |
| 436 | 2 * 60 * 60_000, // Rare |
| 437 | 10 * 24 * 60 * 60_000 // Never |
| 438 | }; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 439 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 440 | // Minimum futurity of a new alarm |
| 441 | public long MIN_FUTURITY = DEFAULT_MIN_FUTURITY; |
| 442 | |
| 443 | // Minimum alarm recurrence interval |
| 444 | public long MIN_INTERVAL = DEFAULT_MIN_INTERVAL; |
| 445 | |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 446 | // Maximum alarm recurrence interval |
| 447 | public long MAX_INTERVAL = DEFAULT_MAX_INTERVAL; |
| 448 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 449 | // Minimum time between ALLOW_WHILE_IDLE alarms when system is not idle. |
| 450 | public long ALLOW_WHILE_IDLE_SHORT_TIME = DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME; |
| 451 | |
| 452 | // Minimum time between ALLOW_WHILE_IDLE alarms when system is idling. |
| 453 | public long ALLOW_WHILE_IDLE_LONG_TIME = DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME; |
| 454 | |
| 455 | // BroadcastOptions.setTemporaryAppWhitelistDuration() to use for FLAG_ALLOW_WHILE_IDLE. |
| 456 | public long ALLOW_WHILE_IDLE_WHITELIST_DURATION |
| 457 | = DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION; |
| 458 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 459 | // Direct alarm listener callback timeout |
| 460 | public long LISTENER_TIMEOUT = DEFAULT_LISTENER_TIMEOUT; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 461 | public boolean APP_STANDBY_QUOTAS_ENABLED = DEFAULT_APP_STANDBY_QUOTAS_ENABLED; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 462 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 463 | public long APP_STANDBY_WINDOW = DEFAULT_APP_STANDBY_WINDOW; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 464 | public long[] APP_STANDBY_MIN_DELAYS = new long[DEFAULT_APP_STANDBY_DELAYS.length]; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 465 | public int[] APP_STANDBY_QUOTAS = new int[DEFAULT_APP_STANDBY_QUOTAS.length]; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 466 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 467 | private ContentResolver mResolver; |
| 468 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 469 | private long mLastAllowWhileIdleWhitelistDuration = -1; |
| 470 | |
| 471 | public Constants(Handler handler) { |
| 472 | super(handler); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 473 | updateAllowWhileIdleWhitelistDurationLocked(); |
| 474 | } |
| 475 | |
| 476 | public void start(ContentResolver resolver) { |
| 477 | mResolver = resolver; |
| 478 | mResolver.registerContentObserver(Settings.Global.getUriFor( |
| 479 | Settings.Global.ALARM_MANAGER_CONSTANTS), false, this); |
| 480 | updateConstants(); |
| 481 | } |
| 482 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 483 | public void updateAllowWhileIdleWhitelistDurationLocked() { |
| 484 | if (mLastAllowWhileIdleWhitelistDuration != ALLOW_WHILE_IDLE_WHITELIST_DURATION) { |
| 485 | mLastAllowWhileIdleWhitelistDuration = ALLOW_WHILE_IDLE_WHITELIST_DURATION; |
| 486 | BroadcastOptions opts = BroadcastOptions.makeBasic(); |
| 487 | opts.setTemporaryAppWhitelistDuration(ALLOW_WHILE_IDLE_WHITELIST_DURATION); |
| 488 | mIdleOptions = opts.toBundle(); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public void onChange(boolean selfChange, Uri uri) { |
| 494 | updateConstants(); |
| 495 | } |
| 496 | |
| 497 | private void updateConstants() { |
| 498 | synchronized (mLock) { |
| 499 | try { |
| 500 | mParser.setString(Settings.Global.getString(mResolver, |
| 501 | Settings.Global.ALARM_MANAGER_CONSTANTS)); |
| 502 | } catch (IllegalArgumentException e) { |
| 503 | // Failed to parse the settings string, log this and move on |
| 504 | // with defaults. |
Dianne Hackborn | 0ef403e | 2017-01-24 18:22:15 -0800 | [diff] [blame] | 505 | Slog.e(TAG, "Bad alarm manager settings", e); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | MIN_FUTURITY = mParser.getLong(KEY_MIN_FUTURITY, DEFAULT_MIN_FUTURITY); |
| 509 | MIN_INTERVAL = mParser.getLong(KEY_MIN_INTERVAL, DEFAULT_MIN_INTERVAL); |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 510 | MAX_INTERVAL = mParser.getLong(KEY_MAX_INTERVAL, DEFAULT_MAX_INTERVAL); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 511 | ALLOW_WHILE_IDLE_SHORT_TIME = mParser.getLong(KEY_ALLOW_WHILE_IDLE_SHORT_TIME, |
| 512 | DEFAULT_ALLOW_WHILE_IDLE_SHORT_TIME); |
| 513 | ALLOW_WHILE_IDLE_LONG_TIME = mParser.getLong(KEY_ALLOW_WHILE_IDLE_LONG_TIME, |
| 514 | DEFAULT_ALLOW_WHILE_IDLE_LONG_TIME); |
| 515 | ALLOW_WHILE_IDLE_WHITELIST_DURATION = mParser.getLong( |
| 516 | KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION, |
| 517 | DEFAULT_ALLOW_WHILE_IDLE_WHITELIST_DURATION); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 518 | LISTENER_TIMEOUT = mParser.getLong(KEY_LISTENER_TIMEOUT, |
| 519 | DEFAULT_LISTENER_TIMEOUT); |
Christopher Tate | f215971 | 2018-03-27 16:04:14 -0700 | [diff] [blame] | 520 | APP_STANDBY_MIN_DELAYS[ACTIVE_INDEX] = mParser.getDurationMillis( |
| 521 | KEYS_APP_STANDBY_DELAY[ACTIVE_INDEX], |
| 522 | DEFAULT_APP_STANDBY_DELAYS[ACTIVE_INDEX]); |
| 523 | for (int i = WORKING_INDEX; i < KEYS_APP_STANDBY_DELAY.length; i++) { |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 524 | APP_STANDBY_MIN_DELAYS[i] = mParser.getDurationMillis(KEYS_APP_STANDBY_DELAY[i], |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 525 | Math.max(APP_STANDBY_MIN_DELAYS[i - 1], DEFAULT_APP_STANDBY_DELAYS[i])); |
| 526 | } |
| 527 | |
| 528 | APP_STANDBY_QUOTAS_ENABLED = mParser.getBoolean(KEY_APP_STANDBY_QUOTAS_ENABLED, |
| 529 | DEFAULT_APP_STANDBY_QUOTAS_ENABLED); |
| 530 | |
| 531 | APP_STANDBY_WINDOW = mParser.getLong(KEY_APP_STANDBY_WINDOW, |
| 532 | DEFAULT_APP_STANDBY_WINDOW); |
| 533 | if (APP_STANDBY_WINDOW > DEFAULT_APP_STANDBY_WINDOW) { |
| 534 | Slog.w(TAG, "Cannot exceed the app_standby_window size of " |
| 535 | + DEFAULT_APP_STANDBY_WINDOW); |
| 536 | APP_STANDBY_WINDOW = DEFAULT_APP_STANDBY_WINDOW; |
| 537 | } else if (APP_STANDBY_WINDOW < DEFAULT_APP_STANDBY_WINDOW) { |
| 538 | // Not recommended outside of testing. |
| 539 | Slog.w(TAG, "Using a non-default app_standby_window of " + APP_STANDBY_WINDOW); |
| 540 | } |
| 541 | |
| 542 | APP_STANDBY_QUOTAS[ACTIVE_INDEX] = mParser.getInt( |
| 543 | KEYS_APP_STANDBY_QUOTAS[ACTIVE_INDEX], |
| 544 | DEFAULT_APP_STANDBY_QUOTAS[ACTIVE_INDEX]); |
| 545 | for (int i = WORKING_INDEX; i < KEYS_APP_STANDBY_QUOTAS.length; i++) { |
| 546 | APP_STANDBY_QUOTAS[i] = mParser.getInt(KEYS_APP_STANDBY_QUOTAS[i], |
| 547 | Math.min(APP_STANDBY_QUOTAS[i - 1], DEFAULT_APP_STANDBY_QUOTAS[i])); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 548 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 549 | updateAllowWhileIdleWhitelistDurationLocked(); |
| 550 | } |
| 551 | } |
| 552 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 553 | void dump(PrintWriter pw, String prefix) { |
| 554 | dump(new IndentingPrintWriter(pw, " ").setIndent(prefix)); |
| 555 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 556 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 557 | void dump(IndentingPrintWriter pw) { |
| 558 | pw.println("Settings:"); |
| 559 | |
| 560 | pw.increaseIndent(); |
| 561 | |
| 562 | pw.print(KEY_MIN_FUTURITY); pw.print("="); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 563 | TimeUtils.formatDuration(MIN_FUTURITY, pw); |
| 564 | pw.println(); |
| 565 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 566 | pw.print(KEY_MIN_INTERVAL); pw.print("="); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 567 | TimeUtils.formatDuration(MIN_INTERVAL, pw); |
| 568 | pw.println(); |
| 569 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 570 | pw.print(KEY_MAX_INTERVAL); pw.print("="); |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 571 | TimeUtils.formatDuration(MAX_INTERVAL, pw); |
| 572 | pw.println(); |
| 573 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 574 | pw.print(KEY_LISTENER_TIMEOUT); pw.print("="); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 575 | TimeUtils.formatDuration(LISTENER_TIMEOUT, pw); |
| 576 | pw.println(); |
| 577 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 578 | pw.print(KEY_ALLOW_WHILE_IDLE_SHORT_TIME); pw.print("="); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 579 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_SHORT_TIME, pw); |
| 580 | pw.println(); |
| 581 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 582 | pw.print(KEY_ALLOW_WHILE_IDLE_LONG_TIME); pw.print("="); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 583 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_LONG_TIME, pw); |
| 584 | pw.println(); |
| 585 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 586 | pw.print(KEY_ALLOW_WHILE_IDLE_WHITELIST_DURATION); pw.print("="); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 587 | TimeUtils.formatDuration(ALLOW_WHILE_IDLE_WHITELIST_DURATION, pw); |
| 588 | pw.println(); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 589 | |
| 590 | for (int i = 0; i < KEYS_APP_STANDBY_DELAY.length; i++) { |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 591 | pw.print(KEYS_APP_STANDBY_DELAY[i]); pw.print("="); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 592 | TimeUtils.formatDuration(APP_STANDBY_MIN_DELAYS[i], pw); |
| 593 | pw.println(); |
| 594 | } |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 595 | |
| 596 | pw.print(KEY_APP_STANDBY_QUOTAS_ENABLED); pw.print("="); |
| 597 | pw.println(APP_STANDBY_QUOTAS_ENABLED); |
| 598 | |
| 599 | pw.print(KEY_APP_STANDBY_WINDOW); pw.print("="); |
| 600 | TimeUtils.formatDuration(APP_STANDBY_WINDOW, pw); |
| 601 | pw.println(); |
| 602 | |
| 603 | for (int i = 0; i < KEYS_APP_STANDBY_QUOTAS.length; i++) { |
| 604 | pw.print(KEYS_APP_STANDBY_QUOTAS[i]); pw.print("="); |
| 605 | pw.println(APP_STANDBY_QUOTAS[i]); |
| 606 | } |
| 607 | |
| 608 | pw.decreaseIndent(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 609 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 610 | |
| 611 | void dumpProto(ProtoOutputStream proto, long fieldId) { |
| 612 | final long token = proto.start(fieldId); |
| 613 | |
| 614 | proto.write(ConstantsProto.MIN_FUTURITY_DURATION_MS, MIN_FUTURITY); |
| 615 | proto.write(ConstantsProto.MIN_INTERVAL_DURATION_MS, MIN_INTERVAL); |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 616 | proto.write(ConstantsProto.MAX_INTERVAL_DURATION_MS, MAX_INTERVAL); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 617 | proto.write(ConstantsProto.LISTENER_TIMEOUT_DURATION_MS, LISTENER_TIMEOUT); |
| 618 | proto.write(ConstantsProto.ALLOW_WHILE_IDLE_SHORT_DURATION_MS, |
| 619 | ALLOW_WHILE_IDLE_SHORT_TIME); |
| 620 | proto.write(ConstantsProto.ALLOW_WHILE_IDLE_LONG_DURATION_MS, |
| 621 | ALLOW_WHILE_IDLE_LONG_TIME); |
| 622 | proto.write(ConstantsProto.ALLOW_WHILE_IDLE_WHITELIST_DURATION_MS, |
| 623 | ALLOW_WHILE_IDLE_WHITELIST_DURATION); |
| 624 | |
| 625 | proto.end(token); |
| 626 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 629 | Constants mConstants; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 630 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 631 | // Alarm delivery ordering bookkeeping |
| 632 | static final int PRIO_TICK = 0; |
| 633 | static final int PRIO_WAKEUP = 1; |
| 634 | static final int PRIO_NORMAL = 2; |
| 635 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 636 | final class PriorityClass { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 637 | int seq; |
| 638 | int priority; |
| 639 | |
| 640 | PriorityClass() { |
| 641 | seq = mCurrentSeq - 1; |
| 642 | priority = PRIO_NORMAL; |
| 643 | } |
| 644 | } |
| 645 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 646 | final HashMap<String, PriorityClass> mPriorities = new HashMap<>(); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 647 | int mCurrentSeq = 0; |
| 648 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 649 | static final class WakeupEvent { |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 650 | public long when; |
| 651 | public int uid; |
| 652 | public String action; |
| 653 | |
| 654 | public WakeupEvent(long theTime, int theUid, String theAction) { |
| 655 | when = theTime; |
| 656 | uid = theUid; |
| 657 | action = theAction; |
| 658 | } |
| 659 | } |
| 660 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 661 | final LinkedList<WakeupEvent> mRecentWakeups = new LinkedList<WakeupEvent>(); |
| 662 | final long RECENT_WAKEUP_PERIOD = 1000L * 60 * 60 * 24; // one day |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 663 | |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 664 | final class Batch { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 665 | long start; // These endpoints are always in ELAPSED |
| 666 | long end; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 667 | int flags; // Flags for alarms, such as FLAG_STANDALONE. |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 668 | |
| 669 | final ArrayList<Alarm> alarms = new ArrayList<Alarm>(); |
| 670 | |
| 671 | Batch() { |
| 672 | start = 0; |
| 673 | end = Long.MAX_VALUE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 674 | flags = 0; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | Batch(Alarm seed) { |
| 678 | start = seed.whenElapsed; |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 679 | end = clampPositive(seed.maxWhenElapsed); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 680 | flags = seed.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 681 | alarms.add(seed); |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 682 | if (seed.listener == mTimeTickTrigger) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 683 | mLastTickAdded = mInjector.getCurrentTimeMillis(); |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 684 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | int size() { |
| 688 | return alarms.size(); |
| 689 | } |
| 690 | |
| 691 | Alarm get(int index) { |
| 692 | return alarms.get(index); |
| 693 | } |
| 694 | |
| 695 | boolean canHold(long whenElapsed, long maxWhen) { |
| 696 | return (end >= whenElapsed) && (start <= maxWhen); |
| 697 | } |
| 698 | |
| 699 | boolean add(Alarm alarm) { |
| 700 | boolean newStart = false; |
| 701 | // narrows the batch if necessary; presumes that canHold(alarm) is true |
| 702 | int index = Collections.binarySearch(alarms, alarm, sIncreasingTimeOrder); |
| 703 | if (index < 0) { |
| 704 | index = 0 - index - 1; |
| 705 | } |
| 706 | alarms.add(index, alarm); |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 707 | if (alarm.listener == mTimeTickTrigger) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 708 | mLastTickAdded = mInjector.getCurrentTimeMillis(); |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 709 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 710 | if (DEBUG_BATCH) { |
| 711 | Slog.v(TAG, "Adding " + alarm + " to " + this); |
| 712 | } |
| 713 | if (alarm.whenElapsed > start) { |
| 714 | start = alarm.whenElapsed; |
| 715 | newStart = true; |
| 716 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 717 | if (alarm.maxWhenElapsed < end) { |
| 718 | end = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 719 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 720 | flags |= alarm.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 721 | |
| 722 | if (DEBUG_BATCH) { |
| 723 | Slog.v(TAG, " => now " + this); |
| 724 | } |
| 725 | return newStart; |
| 726 | } |
| 727 | |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 728 | boolean remove(Alarm alarm) { |
| 729 | return remove(a -> (a == alarm)); |
| 730 | } |
| 731 | |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 732 | boolean remove(Predicate<Alarm> predicate) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 733 | boolean didRemove = false; |
| 734 | long newStart = 0; // recalculate endpoints as we go |
| 735 | long newEnd = Long.MAX_VALUE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 736 | int newFlags = 0; |
Christopher Tate | ae269d5 | 2013-09-26 13:11:55 -0700 | [diff] [blame] | 737 | for (int i = 0; i < alarms.size(); ) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 738 | Alarm alarm = alarms.get(i); |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 739 | if (predicate.test(alarm)) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 740 | alarms.remove(i); |
| 741 | didRemove = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 742 | if (alarm.alarmClock != null) { |
| 743 | mNextAlarmClockMayChange = true; |
| 744 | } |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 745 | if (alarm.listener == mTimeTickTrigger) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 746 | mLastTickRemoved = mInjector.getCurrentTimeMillis(); |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 747 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 748 | } else { |
| 749 | if (alarm.whenElapsed > newStart) { |
| 750 | newStart = alarm.whenElapsed; |
| 751 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 752 | if (alarm.maxWhenElapsed < newEnd) { |
| 753 | newEnd = alarm.maxWhenElapsed; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 754 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 755 | newFlags |= alarm.flags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 756 | i++; |
| 757 | } |
| 758 | } |
| 759 | if (didRemove) { |
| 760 | // commit the new batch bounds |
| 761 | start = newStart; |
| 762 | end = newEnd; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 763 | flags = newFlags; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 764 | } |
| 765 | return didRemove; |
| 766 | } |
| 767 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 768 | boolean hasPackage(final String packageName) { |
| 769 | final int N = alarms.size(); |
| 770 | for (int i = 0; i < N; i++) { |
| 771 | Alarm a = alarms.get(i); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 772 | if (a.matches(packageName)) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 773 | return true; |
| 774 | } |
| 775 | } |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | boolean hasWakeups() { |
| 780 | final int N = alarms.size(); |
| 781 | for (int i = 0; i < N; i++) { |
| 782 | Alarm a = alarms.get(i); |
| 783 | // non-wakeup alarms are types 1 and 3, i.e. have the low bit set |
| 784 | if ((a.type & TYPE_NONWAKEUP_MASK) == 0) { |
| 785 | return true; |
| 786 | } |
| 787 | } |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | @Override |
| 792 | public String toString() { |
| 793 | StringBuilder b = new StringBuilder(40); |
| 794 | b.append("Batch{"); b.append(Integer.toHexString(this.hashCode())); |
| 795 | b.append(" num="); b.append(size()); |
| 796 | b.append(" start="); b.append(start); |
| 797 | b.append(" end="); b.append(end); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 798 | if (flags != 0) { |
| 799 | b.append(" flgs=0x"); |
| 800 | b.append(Integer.toHexString(flags)); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 801 | } |
| 802 | b.append('}'); |
| 803 | return b.toString(); |
| 804 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 805 | |
| 806 | public void writeToProto(ProtoOutputStream proto, long fieldId, long nowElapsed, |
| 807 | long nowRTC) { |
| 808 | final long token = proto.start(fieldId); |
| 809 | |
| 810 | proto.write(BatchProto.START_REALTIME, start); |
| 811 | proto.write(BatchProto.END_REALTIME, end); |
| 812 | proto.write(BatchProto.FLAGS, flags); |
| 813 | for (Alarm a : alarms) { |
| 814 | a.writeToProto(proto, BatchProto.ALARMS, nowElapsed, nowRTC); |
| 815 | } |
| 816 | |
| 817 | proto.end(token); |
| 818 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static class BatchTimeOrder implements Comparator<Batch> { |
| 822 | public int compare(Batch b1, Batch b2) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 823 | long when1 = b1.start; |
| 824 | long when2 = b2.start; |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 825 | if (when1 > when2) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 826 | return 1; |
| 827 | } |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 828 | if (when1 < when2) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 829 | return -1; |
| 830 | } |
| 831 | return 0; |
| 832 | } |
| 833 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 834 | |
| 835 | final Comparator<Alarm> mAlarmDispatchComparator = new Comparator<Alarm>() { |
| 836 | @Override |
| 837 | public int compare(Alarm lhs, Alarm rhs) { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 838 | // priority class trumps everything. TICK < WAKEUP < NORMAL |
| 839 | if (lhs.priorityClass.priority < rhs.priorityClass.priority) { |
| 840 | return -1; |
| 841 | } else if (lhs.priorityClass.priority > rhs.priorityClass.priority) { |
| 842 | return 1; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 843 | } |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 844 | |
| 845 | // within each class, sort by nominal delivery time |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 846 | if (lhs.whenElapsed < rhs.whenElapsed) { |
| 847 | return -1; |
| 848 | } else if (lhs.whenElapsed > rhs.whenElapsed) { |
| 849 | return 1; |
| 850 | } |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 851 | |
| 852 | // same priority class + same target delivery time |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | }; |
| 856 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 857 | void calculateDeliveryPriorities(ArrayList<Alarm> alarms) { |
| 858 | final int N = alarms.size(); |
| 859 | for (int i = 0; i < N; i++) { |
| 860 | Alarm a = alarms.get(i); |
| 861 | |
| 862 | final int alarmPrio; |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 863 | if (a.listener == mTimeTickTrigger) { |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 864 | alarmPrio = PRIO_TICK; |
| 865 | } else if (a.wakeup) { |
| 866 | alarmPrio = PRIO_WAKEUP; |
| 867 | } else { |
| 868 | alarmPrio = PRIO_NORMAL; |
| 869 | } |
| 870 | |
| 871 | PriorityClass packagePrio = a.priorityClass; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 872 | String alarmPackage = a.sourcePackage; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 873 | if (packagePrio == null) packagePrio = mPriorities.get(alarmPackage); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 874 | if (packagePrio == null) { |
| 875 | packagePrio = a.priorityClass = new PriorityClass(); // lowest prio & stale sequence |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 876 | mPriorities.put(alarmPackage, packagePrio); |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 877 | } |
| 878 | a.priorityClass = packagePrio; |
| 879 | |
| 880 | if (packagePrio.seq != mCurrentSeq) { |
| 881 | // first alarm we've seen in the current delivery generation from this package |
| 882 | packagePrio.priority = alarmPrio; |
| 883 | packagePrio.seq = mCurrentSeq; |
| 884 | } else { |
| 885 | // Multiple alarms from this package being delivered in this generation; |
| 886 | // bump the package's delivery class if it's warranted. |
| 887 | // TICK < WAKEUP < NORMAL |
| 888 | if (alarmPrio < packagePrio.priority) { |
| 889 | packagePrio.priority = alarmPrio; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 895 | // 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] | 896 | static final long MIN_FUZZABLE_INTERVAL = 10000; |
| 897 | static final BatchTimeOrder sBatchOrder = new BatchTimeOrder(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 898 | final ArrayList<Batch> mAlarmBatches = new ArrayList<>(); |
| 899 | |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 900 | // 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] | 901 | // to run during this time are placed in mPendingWhileIdleAlarms |
| 902 | Alarm mPendingIdleUntil = null; |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 903 | Alarm mNextWakeFromIdle = null; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 904 | ArrayList<Alarm> mPendingWhileIdleAlarms = new ArrayList<>(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 905 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 906 | @VisibleForTesting |
| 907 | AlarmManagerService(Context context, Injector injector) { |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 908 | super(context); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 909 | mInjector = injector; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 912 | AlarmManagerService(Context context) { |
| 913 | this(context, new Injector(context)); |
| 914 | } |
| 915 | |
| 916 | private long convertToElapsed(long when, int type) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 917 | final boolean isRtc = (type == RTC || type == RTC_WAKEUP); |
| 918 | if (isRtc) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 919 | when -= mInjector.getCurrentTimeMillis() - mInjector.getElapsedRealtime(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 920 | } |
| 921 | return when; |
| 922 | } |
| 923 | |
| 924 | // Apply a heuristic to { recurrence interval, futurity of the trigger time } to |
| 925 | // calculate the end of our nominal delivery window for the alarm. |
| 926 | static long maxTriggerTime(long now, long triggerAtTime, long interval) { |
| 927 | // Current heuristic: batchable window is 75% of either the recurrence interval |
| 928 | // [for a periodic alarm] or of the time from now to the desired delivery time, |
| 929 | // with a minimum delay/interval of 10 seconds, under which we will simply not |
| 930 | // defer the alarm. |
| 931 | long futurity = (interval == 0) |
| 932 | ? (triggerAtTime - now) |
| 933 | : interval; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 934 | if (futurity < MIN_FUZZABLE_INTERVAL) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 935 | futurity = 0; |
| 936 | } |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 937 | return clampPositive(triggerAtTime + (long)(.75 * futurity)); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | // returns true if the batch was added at the head |
| 941 | static boolean addBatchLocked(ArrayList<Batch> list, Batch newBatch) { |
| 942 | int index = Collections.binarySearch(list, newBatch, sBatchOrder); |
| 943 | if (index < 0) { |
| 944 | index = 0 - index - 1; |
| 945 | } |
| 946 | list.add(index, newBatch); |
| 947 | return (index == 0); |
| 948 | } |
| 949 | |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 950 | private void insertAndBatchAlarmLocked(Alarm alarm) { |
| 951 | final int whichBatch = ((alarm.flags & AlarmManager.FLAG_STANDALONE) != 0) ? -1 |
| 952 | : attemptCoalesceLocked(alarm.whenElapsed, alarm.maxWhenElapsed); |
| 953 | |
| 954 | if (whichBatch < 0) { |
| 955 | addBatchLocked(mAlarmBatches, new Batch(alarm)); |
| 956 | } else { |
| 957 | final Batch batch = mAlarmBatches.get(whichBatch); |
| 958 | if (batch.add(alarm)) { |
| 959 | // The start time of this batch advanced, so batch ordering may |
| 960 | // have just been broken. Move it to where it now belongs. |
| 961 | mAlarmBatches.remove(whichBatch); |
| 962 | addBatchLocked(mAlarmBatches, batch); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 967 | // Return the index of the matching batch, or -1 if none found. |
| 968 | int attemptCoalesceLocked(long whenElapsed, long maxWhen) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 969 | final int N = mAlarmBatches.size(); |
| 970 | for (int i = 0; i < N; i++) { |
| 971 | Batch b = mAlarmBatches.get(i); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 972 | if ((b.flags&AlarmManager.FLAG_STANDALONE) == 0 && b.canHold(whenElapsed, maxWhen)) { |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 973 | return i; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 974 | } |
| 975 | } |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 976 | return -1; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 977 | } |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 978 | /** @return total count of the alarms in a set of alarm batches. */ |
| 979 | static int getAlarmCount(ArrayList<Batch> batches) { |
| 980 | int ret = 0; |
| 981 | |
| 982 | final int size = batches.size(); |
| 983 | for (int i = 0; i < size; i++) { |
| 984 | ret += batches.get(i).size(); |
| 985 | } |
| 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | boolean haveAlarmsTimeTickAlarm(ArrayList<Alarm> alarms) { |
| 990 | if (alarms.size() == 0) { |
| 991 | return false; |
| 992 | } |
| 993 | final int batchSize = alarms.size(); |
| 994 | for (int j = 0; j < batchSize; j++) { |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 995 | if (alarms.get(j).listener == mTimeTickTrigger) { |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 996 | return true; |
| 997 | } |
| 998 | } |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | boolean haveBatchesTimeTickAlarm(ArrayList<Batch> batches) { |
| 1003 | final int numBatches = batches.size(); |
| 1004 | for (int i = 0; i < numBatches; i++) { |
| 1005 | if (haveAlarmsTimeTickAlarm(batches.get(i).alarms)) { |
| 1006 | return true; |
| 1007 | } |
| 1008 | } |
| 1009 | return false; |
| 1010 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1011 | |
| 1012 | // The RTC clock has moved arbitrarily, so we need to recalculate all the batching |
| 1013 | void rebatchAllAlarms() { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1014 | synchronized (mLock) { |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1015 | rebatchAllAlarmsLocked(true); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | void rebatchAllAlarmsLocked(boolean doValidate) { |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1020 | final long start = mStatLogger.getTime(); |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 1021 | final int oldCount = |
| 1022 | getAlarmCount(mAlarmBatches) + ArrayUtils.size(mPendingWhileIdleAlarms); |
| 1023 | final boolean oldHasTick = haveBatchesTimeTickAlarm(mAlarmBatches) |
| 1024 | || haveAlarmsTimeTickAlarm(mPendingWhileIdleAlarms); |
| 1025 | |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1026 | ArrayList<Batch> oldSet = (ArrayList<Batch>) mAlarmBatches.clone(); |
| 1027 | mAlarmBatches.clear(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1028 | Alarm oldPendingIdleUntil = mPendingIdleUntil; |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1029 | final long nowElapsed = mInjector.getElapsedRealtime(); |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1030 | final int oldBatches = oldSet.size(); |
| 1031 | for (int batchNum = 0; batchNum < oldBatches; batchNum++) { |
| 1032 | Batch batch = oldSet.get(batchNum); |
| 1033 | final int N = batch.size(); |
| 1034 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1035 | reAddAlarmLocked(batch.get(i), nowElapsed, doValidate); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1038 | if (oldPendingIdleUntil != null && oldPendingIdleUntil != mPendingIdleUntil) { |
| 1039 | Slog.wtf(TAG, "Rebatching: idle until changed from " + oldPendingIdleUntil |
| 1040 | + " to " + mPendingIdleUntil); |
| 1041 | if (mPendingIdleUntil == null) { |
| 1042 | // Somehow we lost this... we need to restore all of the pending alarms. |
| 1043 | restorePendingWhileIdleAlarmsLocked(); |
| 1044 | } |
| 1045 | } |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 1046 | final int newCount = |
| 1047 | getAlarmCount(mAlarmBatches) + ArrayUtils.size(mPendingWhileIdleAlarms); |
| 1048 | final boolean newHasTick = haveBatchesTimeTickAlarm(mAlarmBatches) |
| 1049 | || haveAlarmsTimeTickAlarm(mPendingWhileIdleAlarms); |
| 1050 | |
| 1051 | if (oldCount != newCount) { |
| 1052 | Slog.wtf(TAG, "Rebatching: total count changed from " + oldCount + " to " + newCount); |
| 1053 | } |
| 1054 | if (oldHasTick != newHasTick) { |
| 1055 | Slog.wtf(TAG, "Rebatching: hasTick changed from " + oldHasTick + " to " + newHasTick); |
| 1056 | } |
| 1057 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1058 | rescheduleKernelAlarmsLocked(); |
| 1059 | updateNextAlarmClockLocked(); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1060 | mStatLogger.logDurationStat(Stats.REBATCH_ALL_ALARMS, start); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1063 | /** |
| 1064 | * Re-orders the alarm batches based on newly evaluated send times based on the current |
| 1065 | * app-standby buckets |
| 1066 | * @param targetPackages [Package, User] pairs for which alarms need to be re-evaluated, |
| 1067 | * null indicates all |
| 1068 | * @return True if there was any reordering done to the current list. |
| 1069 | */ |
| 1070 | boolean reorderAlarmsBasedOnStandbyBuckets(ArraySet<Pair<String, Integer>> targetPackages) { |
| 1071 | final long start = mStatLogger.getTime(); |
| 1072 | final ArrayList<Alarm> rescheduledAlarms = new ArrayList<>(); |
| 1073 | |
| 1074 | for (int batchIndex = mAlarmBatches.size() - 1; batchIndex >= 0; batchIndex--) { |
| 1075 | final Batch batch = mAlarmBatches.get(batchIndex); |
| 1076 | for (int alarmIndex = batch.size() - 1; alarmIndex >= 0; alarmIndex--) { |
| 1077 | final Alarm alarm = batch.get(alarmIndex); |
| 1078 | final Pair<String, Integer> packageUser = |
| 1079 | Pair.create(alarm.sourcePackage, UserHandle.getUserId(alarm.creatorUid)); |
| 1080 | if (targetPackages != null && !targetPackages.contains(packageUser)) { |
| 1081 | continue; |
| 1082 | } |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 1083 | if (adjustDeliveryTimeBasedOnBucketLocked(alarm)) { |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1084 | batch.remove(alarm); |
| 1085 | rescheduledAlarms.add(alarm); |
| 1086 | } |
| 1087 | } |
| 1088 | if (batch.size() == 0) { |
| 1089 | mAlarmBatches.remove(batchIndex); |
| 1090 | } |
| 1091 | } |
| 1092 | for (int i = 0; i < rescheduledAlarms.size(); i++) { |
| 1093 | final Alarm a = rescheduledAlarms.get(i); |
| 1094 | insertAndBatchAlarmLocked(a); |
| 1095 | } |
| 1096 | |
| 1097 | mStatLogger.logDurationStat(Stats.REORDER_ALARMS_FOR_STANDBY, start); |
| 1098 | return rescheduledAlarms.size() > 0; |
| 1099 | } |
| 1100 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1101 | void reAddAlarmLocked(Alarm a, long nowElapsed, boolean doValidate) { |
| 1102 | a.when = a.origWhen; |
| 1103 | long whenElapsed = convertToElapsed(a.when, a.type); |
| 1104 | final long maxElapsed; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1105 | if (a.windowLength == AlarmManager.WINDOW_EXACT) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1106 | // Exact |
| 1107 | maxElapsed = whenElapsed; |
| 1108 | } else { |
| 1109 | // Not exact. Preserve any explicit window, otherwise recalculate |
| 1110 | // the window based on the alarm's new futurity. Note that this |
| 1111 | // reflects a policy of preferring timely to deferred delivery. |
| 1112 | maxElapsed = (a.windowLength > 0) |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 1113 | ? clampPositive(whenElapsed + a.windowLength) |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1114 | : maxTriggerTime(nowElapsed, whenElapsed, a.repeatInterval); |
| 1115 | } |
| 1116 | a.whenElapsed = whenElapsed; |
| 1117 | a.maxWhenElapsed = maxElapsed; |
| 1118 | setImplLocked(a, true, doValidate); |
| 1119 | } |
| 1120 | |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 1121 | static long clampPositive(long val) { |
| 1122 | return (val >= 0) ? val : Long.MAX_VALUE; |
| 1123 | } |
| 1124 | |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 1125 | /** |
| 1126 | * Sends alarms that were blocked due to user applied background restrictions - either because |
| 1127 | * the user lifted those or the uid came to foreground. |
| 1128 | * |
| 1129 | * @param uid uid to filter on |
| 1130 | * @param packageName package to filter on, or null for all packages in uid |
| 1131 | */ |
| 1132 | void sendPendingBackgroundAlarmsLocked(int uid, String packageName) { |
| 1133 | final ArrayList<Alarm> alarmsForUid = mPendingBackgroundAlarms.get(uid); |
| 1134 | if (alarmsForUid == null || alarmsForUid.size() == 0) { |
| 1135 | return; |
| 1136 | } |
| 1137 | final ArrayList<Alarm> alarmsToDeliver; |
| 1138 | if (packageName != null) { |
| 1139 | if (DEBUG_BG_LIMIT) { |
| 1140 | Slog.d(TAG, "Sending blocked alarms for uid " + uid + ", package " + packageName); |
| 1141 | } |
| 1142 | alarmsToDeliver = new ArrayList<>(); |
| 1143 | for (int i = alarmsForUid.size() - 1; i >= 0; i--) { |
| 1144 | final Alarm a = alarmsForUid.get(i); |
| 1145 | if (a.matches(packageName)) { |
| 1146 | alarmsToDeliver.add(alarmsForUid.remove(i)); |
| 1147 | } |
| 1148 | } |
| 1149 | if (alarmsForUid.size() == 0) { |
| 1150 | mPendingBackgroundAlarms.remove(uid); |
| 1151 | } |
| 1152 | } else { |
| 1153 | if (DEBUG_BG_LIMIT) { |
| 1154 | Slog.d(TAG, "Sending blocked alarms for uid " + uid); |
| 1155 | } |
| 1156 | alarmsToDeliver = alarmsForUid; |
| 1157 | mPendingBackgroundAlarms.remove(uid); |
| 1158 | } |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1159 | deliverPendingBackgroundAlarmsLocked(alarmsToDeliver, mInjector.getElapsedRealtime()); |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1162 | /** |
| 1163 | * Check all alarms in {@link #mPendingBackgroundAlarms} and send the ones that are not |
| 1164 | * restricted. |
| 1165 | * |
| 1166 | * This is only called when the global "force all apps-standby" flag changes or when the |
| 1167 | * power save whitelist changes, so it's okay to be slow. |
| 1168 | */ |
| 1169 | void sendAllUnrestrictedPendingBackgroundAlarmsLocked() { |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 1170 | final ArrayList<Alarm> alarmsToDeliver = new ArrayList<>(); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1171 | |
| 1172 | findAllUnrestrictedPendingBackgroundAlarmsLockedInner( |
| 1173 | mPendingBackgroundAlarms, alarmsToDeliver, this::isBackgroundRestricted); |
| 1174 | |
| 1175 | if (alarmsToDeliver.size() > 0) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1176 | deliverPendingBackgroundAlarmsLocked(alarmsToDeliver, mInjector.getElapsedRealtime()); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | @VisibleForTesting |
| 1181 | static void findAllUnrestrictedPendingBackgroundAlarmsLockedInner( |
| 1182 | SparseArray<ArrayList<Alarm>> pendingAlarms, ArrayList<Alarm> unrestrictedAlarms, |
| 1183 | Predicate<Alarm> isBackgroundRestricted) { |
| 1184 | |
| 1185 | for (int uidIndex = pendingAlarms.size() - 1; uidIndex >= 0; uidIndex--) { |
| 1186 | final int uid = pendingAlarms.keyAt(uidIndex); |
| 1187 | final ArrayList<Alarm> alarmsForUid = pendingAlarms.valueAt(uidIndex); |
| 1188 | |
| 1189 | for (int alarmIndex = alarmsForUid.size() - 1; alarmIndex >= 0; alarmIndex--) { |
| 1190 | final Alarm alarm = alarmsForUid.get(alarmIndex); |
| 1191 | |
| 1192 | if (isBackgroundRestricted.test(alarm)) { |
| 1193 | continue; |
| 1194 | } |
| 1195 | |
| 1196 | unrestrictedAlarms.add(alarm); |
| 1197 | alarmsForUid.remove(alarmIndex); |
| 1198 | } |
| 1199 | |
| 1200 | if (alarmsForUid.size() == 0) { |
| 1201 | pendingAlarms.removeAt(uidIndex); |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | private void deliverPendingBackgroundAlarmsLocked(ArrayList<Alarm> alarms, long nowELAPSED) { |
| 1207 | final int N = alarms.size(); |
| 1208 | boolean hasWakeup = false; |
| 1209 | for (int i = 0; i < N; i++) { |
| 1210 | final Alarm alarm = alarms.get(i); |
| 1211 | if (alarm.wakeup) { |
| 1212 | hasWakeup = true; |
| 1213 | } |
| 1214 | alarm.count = 1; |
| 1215 | // Recurring alarms may have passed several alarm intervals while the |
| 1216 | // alarm was kept pending. Send the appropriate trigger count. |
| 1217 | if (alarm.repeatInterval > 0) { |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 1218 | alarm.count += (nowELAPSED - alarm.expectedWhenElapsed) / alarm.repeatInterval; |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 1219 | // Also schedule its next recurrence |
| 1220 | final long delta = alarm.count * alarm.repeatInterval; |
| 1221 | final long nextElapsed = alarm.whenElapsed + delta; |
| 1222 | setImplLocked(alarm.type, alarm.when + delta, nextElapsed, alarm.windowLength, |
| 1223 | maxTriggerTime(nowELAPSED, nextElapsed, alarm.repeatInterval), |
| 1224 | alarm.repeatInterval, alarm.operation, null, null, alarm.flags, true, |
| 1225 | alarm.workSource, alarm.alarmClock, alarm.uid, alarm.packageName); |
| 1226 | // Kernel alarms will be rescheduled as needed in setImplLocked |
| 1227 | } |
| 1228 | } |
| 1229 | if (!hasWakeup && checkAllowNonWakeupDelayLocked(nowELAPSED)) { |
| 1230 | // No need to wakeup for non wakeup alarms |
| 1231 | if (mPendingNonWakeupAlarms.size() == 0) { |
| 1232 | mStartCurrentDelayTime = nowELAPSED; |
| 1233 | mNextNonWakeupDeliveryTime = nowELAPSED |
| 1234 | + ((currentNonWakeupFuzzLocked(nowELAPSED)*3)/2); |
| 1235 | } |
| 1236 | mPendingNonWakeupAlarms.addAll(alarms); |
| 1237 | mNumDelayedAlarms += alarms.size(); |
| 1238 | } else { |
| 1239 | if (DEBUG_BG_LIMIT) { |
| 1240 | Slog.d(TAG, "Waking up to deliver pending blocked alarms"); |
| 1241 | } |
| 1242 | // Since we are waking up, also deliver any pending non wakeup alarms we have. |
| 1243 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 1244 | alarms.addAll(mPendingNonWakeupAlarms); |
| 1245 | final long thisDelayTime = nowELAPSED - mStartCurrentDelayTime; |
| 1246 | mTotalDelayTime += thisDelayTime; |
| 1247 | if (mMaxDelayTime < thisDelayTime) { |
| 1248 | mMaxDelayTime = thisDelayTime; |
| 1249 | } |
| 1250 | mPendingNonWakeupAlarms.clear(); |
| 1251 | } |
| 1252 | calculateDeliveryPriorities(alarms); |
| 1253 | Collections.sort(alarms, mAlarmDispatchComparator); |
| 1254 | deliverAlarmsLocked(alarms, nowELAPSED); |
| 1255 | } |
| 1256 | } |
| 1257 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1258 | void restorePendingWhileIdleAlarmsLocked() { |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1259 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1260 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 1261 | ent.uid = 0; |
| 1262 | ent.pkg = "FINISH IDLE"; |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1263 | ent.elapsedRealtime = mInjector.getElapsedRealtime(); |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1264 | mAllowWhileIdleDispatches.add(ent); |
| 1265 | } |
| 1266 | |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 1267 | // Bring pending alarms back into the main list. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1268 | if (mPendingWhileIdleAlarms.size() > 0) { |
| 1269 | ArrayList<Alarm> alarms = mPendingWhileIdleAlarms; |
| 1270 | mPendingWhileIdleAlarms = new ArrayList<>(); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1271 | final long nowElapsed = mInjector.getElapsedRealtime(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1272 | for (int i=alarms.size() - 1; i >= 0; i--) { |
| 1273 | Alarm a = alarms.get(i); |
| 1274 | reAddAlarmLocked(a, nowElapsed, false); |
| 1275 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1276 | } |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 1277 | |
| 1278 | // Reschedule everything. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1279 | rescheduleKernelAlarmsLocked(); |
| 1280 | updateNextAlarmClockLocked(); |
Dianne Hackborn | 35d5403 | 2015-04-23 10:30:43 -0700 | [diff] [blame] | 1281 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1284 | static final class InFlight { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1285 | final PendingIntent mPendingIntent; |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 1286 | final long mWhenElapsed; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1287 | final IBinder mListener; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1288 | final WorkSource mWorkSource; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1289 | final int mUid; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1290 | final String mTag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1291 | final BroadcastStats mBroadcastStats; |
| 1292 | final FilterStats mFilterStats; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1293 | final int mAlarmType; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1294 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1295 | InFlight(AlarmManagerService service, PendingIntent pendingIntent, IAlarmListener listener, |
| 1296 | WorkSource workSource, int uid, String alarmPkg, int alarmType, String tag, |
| 1297 | long nowELAPSED) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1298 | mPendingIntent = pendingIntent; |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 1299 | mWhenElapsed = nowELAPSED; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1300 | mListener = listener != null ? listener.asBinder() : null; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1301 | mWorkSource = workSource; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1302 | mUid = uid; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1303 | mTag = tag; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1304 | mBroadcastStats = (pendingIntent != null) |
| 1305 | ? service.getStatsLocked(pendingIntent) |
| 1306 | : service.getStatsLocked(uid, alarmPkg); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1307 | FilterStats fs = mBroadcastStats.filterStats.get(mTag); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1308 | if (fs == null) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1309 | fs = new FilterStats(mBroadcastStats, mTag); |
| 1310 | mBroadcastStats.filterStats.put(mTag, fs); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1311 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1312 | fs.lastTime = nowELAPSED; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1313 | mFilterStats = fs; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1314 | mAlarmType = alarmType; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1315 | } |
Makoto Onuki | 33955e1 | 2017-03-01 18:11:00 -0800 | [diff] [blame] | 1316 | |
| 1317 | @Override |
| 1318 | public String toString() { |
| 1319 | return "InFlight{" |
| 1320 | + "pendingIntent=" + mPendingIntent |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 1321 | + ", when=" + mWhenElapsed |
Makoto Onuki | 33955e1 | 2017-03-01 18:11:00 -0800 | [diff] [blame] | 1322 | + ", workSource=" + mWorkSource |
| 1323 | + ", uid=" + mUid |
| 1324 | + ", tag=" + mTag |
| 1325 | + ", broadcastStats=" + mBroadcastStats |
| 1326 | + ", filterStats=" + mFilterStats |
| 1327 | + ", alarmType=" + mAlarmType |
| 1328 | + "}"; |
| 1329 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 1330 | |
| 1331 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 1332 | final long token = proto.start(fieldId); |
| 1333 | |
| 1334 | proto.write(InFlightProto.UID, mUid); |
| 1335 | proto.write(InFlightProto.TAG, mTag); |
| 1336 | proto.write(InFlightProto.WHEN_ELAPSED_MS, mWhenElapsed); |
| 1337 | proto.write(InFlightProto.ALARM_TYPE, mAlarmType); |
| 1338 | if (mPendingIntent != null) { |
| 1339 | mPendingIntent.writeToProto(proto, InFlightProto.PENDING_INTENT); |
| 1340 | } |
| 1341 | if (mBroadcastStats != null) { |
| 1342 | mBroadcastStats.writeToProto(proto, InFlightProto.BROADCAST_STATS); |
| 1343 | } |
| 1344 | if (mFilterStats != null) { |
| 1345 | mFilterStats.writeToProto(proto, InFlightProto.FILTER_STATS); |
| 1346 | } |
| 1347 | if (mWorkSource != null) { |
| 1348 | mWorkSource.writeToProto(proto, InFlightProto.WORK_SOURCE); |
| 1349 | } |
| 1350 | |
| 1351 | proto.end(token); |
| 1352 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1354 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1355 | static final class FilterStats { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1356 | final BroadcastStats mBroadcastStats; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1357 | final String mTag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1358 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1359 | long lastTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | long aggregateTime; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1361 | int count; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1362 | int numWakeup; |
| 1363 | long startTime; |
| 1364 | int nesting; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1365 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1366 | FilterStats(BroadcastStats broadcastStats, String tag) { |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1367 | mBroadcastStats = broadcastStats; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1368 | mTag = tag; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1369 | } |
Makoto Onuki | 33955e1 | 2017-03-01 18:11:00 -0800 | [diff] [blame] | 1370 | |
| 1371 | @Override |
| 1372 | public String toString() { |
| 1373 | return "FilterStats{" |
| 1374 | + "tag=" + mTag |
| 1375 | + ", lastTime=" + lastTime |
| 1376 | + ", aggregateTime=" + aggregateTime |
| 1377 | + ", count=" + count |
| 1378 | + ", numWakeup=" + numWakeup |
| 1379 | + ", startTime=" + startTime |
| 1380 | + ", nesting=" + nesting |
| 1381 | + "}"; |
| 1382 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 1383 | |
| 1384 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 1385 | final long token = proto.start(fieldId); |
| 1386 | |
| 1387 | proto.write(FilterStatsProto.TAG, mTag); |
| 1388 | proto.write(FilterStatsProto.LAST_FLIGHT_TIME_REALTIME, lastTime); |
| 1389 | proto.write(FilterStatsProto.TOTAL_FLIGHT_DURATION_MS, aggregateTime); |
| 1390 | proto.write(FilterStatsProto.COUNT, count); |
| 1391 | proto.write(FilterStatsProto.WAKEUP_COUNT, numWakeup); |
| 1392 | proto.write(FilterStatsProto.START_TIME_REALTIME, startTime); |
| 1393 | proto.write(FilterStatsProto.NESTING, nesting); |
| 1394 | |
| 1395 | proto.end(token); |
| 1396 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1397 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1398 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1399 | static final class BroadcastStats { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1400 | final int mUid; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1401 | final String mPackageName; |
| 1402 | |
| 1403 | long aggregateTime; |
| 1404 | int count; |
| 1405 | int numWakeup; |
| 1406 | long startTime; |
| 1407 | int nesting; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1408 | final ArrayMap<String, FilterStats> filterStats = new ArrayMap<String, FilterStats>(); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1409 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1410 | BroadcastStats(int uid, String packageName) { |
| 1411 | mUid = uid; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 1412 | mPackageName = packageName; |
| 1413 | } |
Makoto Onuki | 33955e1 | 2017-03-01 18:11:00 -0800 | [diff] [blame] | 1414 | |
| 1415 | @Override |
| 1416 | public String toString() { |
| 1417 | return "BroadcastStats{" |
| 1418 | + "uid=" + mUid |
| 1419 | + ", packageName=" + mPackageName |
| 1420 | + ", aggregateTime=" + aggregateTime |
| 1421 | + ", count=" + count |
| 1422 | + ", numWakeup=" + numWakeup |
| 1423 | + ", startTime=" + startTime |
| 1424 | + ", nesting=" + nesting |
| 1425 | + "}"; |
| 1426 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 1427 | |
| 1428 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 1429 | final long token = proto.start(fieldId); |
| 1430 | |
| 1431 | proto.write(BroadcastStatsProto.UID, mUid); |
| 1432 | proto.write(BroadcastStatsProto.PACKAGE_NAME, mPackageName); |
| 1433 | proto.write(BroadcastStatsProto.TOTAL_FLIGHT_DURATION_MS, aggregateTime); |
| 1434 | proto.write(BroadcastStatsProto.COUNT, count); |
| 1435 | proto.write(BroadcastStatsProto.WAKEUP_COUNT, numWakeup); |
| 1436 | proto.write(BroadcastStatsProto.START_TIME_REALTIME, startTime); |
| 1437 | proto.write(BroadcastStatsProto.NESTING, nesting); |
| 1438 | |
| 1439 | proto.end(token); |
| 1440 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1442 | |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1443 | final SparseArray<ArrayMap<String, BroadcastStats>> mBroadcastStats |
| 1444 | = new SparseArray<ArrayMap<String, BroadcastStats>>(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 1445 | |
| 1446 | int mNumDelayedAlarms = 0; |
| 1447 | long mTotalDelayTime = 0; |
| 1448 | long mMaxDelayTime = 0; |
| 1449 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1450 | @Override |
| 1451 | public void onStart() { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1452 | mInjector.init(); |
Robert CH Chou | 64ba8e4 | 2009-11-04 21:38:49 +0800 | [diff] [blame] | 1453 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1454 | synchronized (mLock) { |
Suprabh Shukla | 12bd016 | 2018-11-12 18:00:18 -0800 | [diff] [blame] | 1455 | mHandler = new AlarmHandler(); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1456 | mConstants = new Constants(mHandler); |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 1457 | mAppWakeupHistory = new AppWakeupHistory(Constants.DEFAULT_APP_STANDBY_WINDOW); |
Robert CH Chou | 64ba8e4 | 2009-11-04 21:38:49 +0800 | [diff] [blame] | 1458 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1459 | mNextWakeup = mNextNonWakeup = 0; |
| 1460 | |
| 1461 | // We have to set current TimeZone info to kernel |
| 1462 | // because kernel doesn't keep this after reboot |
| 1463 | setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY)); |
| 1464 | |
Christopher Tate | 3d65d4d | 2018-12-11 14:55:19 -0800 | [diff] [blame] | 1465 | // Ensure that we're booting with a halfway sensible current time. Use the |
| 1466 | // most recent of Build.TIME, the root file system's timestamp, and the |
| 1467 | // value of the ro.build.date.utc system property (which is in seconds). |
| 1468 | final long systemBuildTime = Long.max( |
| 1469 | 1000L * SystemProperties.getLong("ro.build.date.utc", -1L), |
| 1470 | Long.max(Environment.getRootDirectory().lastModified(), Build.TIME)); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1471 | if (mInjector.getCurrentTimeMillis() < systemBuildTime) { |
| 1472 | Slog.i(TAG, "Current time only " + mInjector.getCurrentTimeMillis() |
Christopher Tate | 24757146 | 2017-04-10 11:45:05 -0700 | [diff] [blame] | 1473 | + ", advancing to build time " + systemBuildTime); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1474 | mInjector.setKernelTime(systemBuildTime); |
Christopher Tate | 24757146 | 2017-04-10 11:45:05 -0700 | [diff] [blame] | 1475 | } |
Christopher Tate | 24757146 | 2017-04-10 11:45:05 -0700 | [diff] [blame] | 1476 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1477 | // Determine SysUI's uid |
| 1478 | mSystemUiUid = mInjector.getSystemUiUid(); |
| 1479 | if (mSystemUiUid <= 0) { |
| 1480 | Slog.wtf(TAG, "SysUI package not found!"); |
| 1481 | } |
| 1482 | mWakeLock = mInjector.getAlarmWakeLock(); |
| 1483 | |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 1484 | mTimeTickIntent = new Intent(Intent.ACTION_TIME_TICK).addFlags( |
| 1485 | Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1486 | | Intent.FLAG_RECEIVER_FOREGROUND |
| 1487 | | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
| 1488 | |
| 1489 | mTimeTickTrigger = new IAlarmListener.Stub() { |
| 1490 | @Override |
| 1491 | public void doAlarm(final IAlarmCompleteListener callback) throws RemoteException { |
| 1492 | if (DEBUG_BATCH) { |
| 1493 | Slog.v(TAG, "Received TIME_TICK alarm; rescheduling"); |
| 1494 | } |
| 1495 | |
| 1496 | // Via handler because dispatch invokes this within its lock. OnAlarmListener |
| 1497 | // takes care of this automatically, but we're using the direct internal |
| 1498 | // interface here rather than that client-side wrapper infrastructure. |
| 1499 | mHandler.post(() -> { |
| 1500 | getContext().sendBroadcastAsUser(mTimeTickIntent, UserHandle.ALL); |
| 1501 | |
| 1502 | try { |
| 1503 | callback.alarmComplete(this); |
| 1504 | } catch (RemoteException e) { /* local method call */ } |
| 1505 | }); |
| 1506 | |
| 1507 | synchronized (mLock) { |
| 1508 | mLastTickReceived = mInjector.getCurrentTimeMillis(); |
| 1509 | } |
| 1510 | mClockReceiver.scheduleTimeTickEvent(); |
| 1511 | } |
| 1512 | }; |
| 1513 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1514 | Intent intent = new Intent(Intent.ACTION_DATE_CHANGED); |
| 1515 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
| 1516 | | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
| 1517 | mDateChangeSender = PendingIntent.getBroadcastAsUser(getContext(), 0, intent, |
| 1518 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, UserHandle.ALL); |
| 1519 | |
| 1520 | mClockReceiver = mInjector.getClockReceiver(this); |
| 1521 | new InteractiveStateReceiver(); |
| 1522 | new UninstallReceiver(); |
| 1523 | |
| 1524 | if (mInjector.isAlarmDriverPresent()) { |
| 1525 | AlarmThread waitThread = new AlarmThread(); |
| 1526 | waitThread.start(); |
Christopher Tate | bb9cce5 | 2017-04-18 14:19:43 -0700 | [diff] [blame] | 1527 | } else { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1528 | Slog.w(TAG, "Failed to open alarm driver. Falling back to a handler."); |
Christopher Tate | bb9cce5 | 2017-04-18 14:19:43 -0700 | [diff] [blame] | 1529 | } |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1530 | |
| 1531 | try { |
| 1532 | ActivityManager.getService().registerUidObserver(new UidObserver(), |
| 1533 | ActivityManager.UID_OBSERVER_GONE | ActivityManager.UID_OBSERVER_IDLE |
| 1534 | | ActivityManager.UID_OBSERVER_ACTIVE, |
| 1535 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
| 1536 | } catch (RemoteException e) { |
| 1537 | // ignored; both services live in system_server |
| 1538 | } |
Christopher Tate | bb9cce5 | 2017-04-18 14:19:43 -0700 | [diff] [blame] | 1539 | } |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1540 | publishLocalService(AlarmManagerInternal.class, new LocalService()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1541 | publishBinderService(Context.ALARM_SERVICE, mService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1543 | |
| 1544 | @Override |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1545 | public void onBootPhase(int phase) { |
| 1546 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1547 | synchronized (mLock) { |
| 1548 | mConstants.start(getContext().getContentResolver()); |
| 1549 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 1550 | mLocalDeviceIdleController = |
| 1551 | LocalServices.getService(DeviceIdleController.LocalService.class); |
| 1552 | mUsageStatsManagerInternal = |
| 1553 | LocalServices.getService(UsageStatsManagerInternal.class); |
| 1554 | mUsageStatsManagerInternal.addAppIdleStateChangeListener(new AppStandbyTracker()); |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 1555 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1556 | mAppStateTracker = LocalServices.getService(AppStateTracker.class); |
| 1557 | mAppStateTracker.addListener(mForceAppStandbyListener); |
| 1558 | |
| 1559 | mClockReceiver.scheduleTimeTickEvent(); |
| 1560 | mClockReceiver.scheduleDateChangedEvent(); |
| 1561 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1566 | protected void finalize() throws Throwable { |
| 1567 | try { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1568 | mInjector.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1569 | } finally { |
| 1570 | super.finalize(); |
| 1571 | } |
| 1572 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1573 | |
mswest46 | 3f4c99d | 2018-02-01 10:13:10 -0800 | [diff] [blame] | 1574 | boolean setTimeImpl(long millis) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1575 | if (!mInjector.isAlarmDriverPresent()) { |
mswest46 | 3f4c99d | 2018-02-01 10:13:10 -0800 | [diff] [blame] | 1576 | Slog.w(TAG, "Not setting time since no alarm driver is available."); |
| 1577 | return false; |
| 1578 | } |
| 1579 | |
| 1580 | synchronized (mLock) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1581 | final long currentTimeMillis = mInjector.getCurrentTimeMillis(); |
| 1582 | mInjector.setKernelTime(millis); |
Christine Hallstrom | f5d585a | 2018-05-21 14:27:23 -0700 | [diff] [blame] | 1583 | final TimeZone timeZone = TimeZone.getDefault(); |
| 1584 | final int currentTzOffset = timeZone.getOffset(currentTimeMillis); |
| 1585 | final int newTzOffset = timeZone.getOffset(millis); |
| 1586 | if (currentTzOffset != newTzOffset) { |
| 1587 | Slog.i(TAG, "Timezone offset has changed, updating kernel timezone"); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1588 | mInjector.setKernelTimezone(-(newTzOffset / 60000)); |
Christine Hallstrom | f5d585a | 2018-05-21 14:27:23 -0700 | [diff] [blame] | 1589 | } |
| 1590 | // The native implementation of setKernelTime can return -1 even when the kernel |
| 1591 | // time was set correctly, so assume setting kernel time was successful and always |
| 1592 | // return true. |
| 1593 | return true; |
mswest46 | 3f4c99d | 2018-02-01 10:13:10 -0800 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1597 | void setTimeZoneImpl(String tz) { |
| 1598 | if (TextUtils.isEmpty(tz)) { |
| 1599 | return; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1602 | TimeZone zone = TimeZone.getTimeZone(tz); |
| 1603 | // Prevent reentrant calls from stepping on each other when writing |
| 1604 | // the time zone property |
| 1605 | boolean timeZoneWasChanged = false; |
| 1606 | synchronized (this) { |
| 1607 | String current = SystemProperties.get(TIMEZONE_PROPERTY); |
| 1608 | if (current == null || !current.equals(zone.getID())) { |
| 1609 | if (localLOGV) { |
| 1610 | Slog.v(TAG, "timezone changed: " + current + ", new=" + zone.getID()); |
| 1611 | } |
| 1612 | timeZoneWasChanged = true; |
| 1613 | SystemProperties.set(TIMEZONE_PROPERTY, zone.getID()); |
| 1614 | } |
| 1615 | |
| 1616 | // Update the kernel timezone information |
| 1617 | // Kernel tracks time offsets as 'minutes west of GMT' |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1618 | int gmtOffset = zone.getOffset(mInjector.getCurrentTimeMillis()); |
| 1619 | mInjector.setKernelTimezone(-(gmtOffset / 60000)); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | TimeZone.setDefault(null); |
| 1623 | |
| 1624 | if (timeZoneWasChanged) { |
Christopher Tate | a4618b0 | 2018-10-11 16:22:55 -0700 | [diff] [blame] | 1625 | // Don't wait for broadcasts to update our midnight alarm |
| 1626 | mClockReceiver.scheduleDateChangedEvent(); |
| 1627 | |
| 1628 | // And now let everyone else know |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1629 | Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED); |
Chad Brubaker | 291df4f | 2017-03-14 10:23:02 -0700 | [diff] [blame] | 1630 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
Christopher Tate | 5cdf9f8 | 2017-05-03 18:10:39 -0700 | [diff] [blame] | 1631 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
Chad Brubaker | 291df4f | 2017-03-14 10:23:02 -0700 | [diff] [blame] | 1632 | | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1633 | intent.putExtra("time-zone", zone.getID()); |
| 1634 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1637 | |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 1638 | void removeImpl(PendingIntent operation, IAlarmListener listener) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1639 | synchronized (mLock) { |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 1640 | removeLocked(operation, listener); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | void setImpl(int type, long triggerAtTime, long windowLength, long interval, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1645 | PendingIntent operation, IAlarmListener directReceiver, String listenerTag, |
| 1646 | int flags, WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock, |
| 1647 | int callingUid, String callingPackage) { |
| 1648 | // must be *either* PendingIntent or AlarmReceiver, but not both |
| 1649 | if ((operation == null && directReceiver == null) |
| 1650 | || (operation != null && directReceiver != null)) { |
| 1651 | Slog.w(TAG, "Alarms must either supply a PendingIntent or an AlarmReceiver"); |
| 1652 | // NB: previous releases failed silently here, so we are continuing to do the same |
| 1653 | // rather than throw an IllegalArgumentException. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | return; |
| 1655 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1656 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1657 | // Sanity check the window length. This will catch people mistakenly |
| 1658 | // trying to pass an end-of-window timestamp rather than a duration. |
| 1659 | if (windowLength > AlarmManager.INTERVAL_HALF_DAY) { |
| 1660 | Slog.w(TAG, "Window length " + windowLength |
| 1661 | + "ms suspiciously long; limiting to 1 hour"); |
| 1662 | windowLength = AlarmManager.INTERVAL_HOUR; |
| 1663 | } |
| 1664 | |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1665 | // Sanity check the recurrence interval. This will catch people who supply |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 1666 | // seconds when the API expects milliseconds, or apps trying shenanigans |
| 1667 | // around intentional period overflow, etc. |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1668 | final long minInterval = mConstants.MIN_INTERVAL; |
| 1669 | if (interval > 0 && interval < minInterval) { |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1670 | Slog.w(TAG, "Suspiciously short interval " + interval |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1671 | + " millis; expanding to " + (minInterval/1000) |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1672 | + " seconds"); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1673 | interval = minInterval; |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 1674 | } else if (interval > mConstants.MAX_INTERVAL) { |
| 1675 | Slog.w(TAG, "Suspiciously long interval " + interval |
| 1676 | + " millis; clamping"); |
| 1677 | interval = mConstants.MAX_INTERVAL; |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1680 | if (type < RTC_WAKEUP || type > ELAPSED_REALTIME) { |
| 1681 | throw new IllegalArgumentException("Invalid alarm type " + type); |
| 1682 | } |
| 1683 | |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1684 | if (triggerAtTime < 0) { |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1685 | final long what = Binder.getCallingPid(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1686 | Slog.w(TAG, "Invalid alarm trigger time! " + triggerAtTime + " from uid=" + callingUid |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 1687 | + " pid=" + what); |
| 1688 | triggerAtTime = 0; |
| 1689 | } |
| 1690 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1691 | final long nowElapsed = mInjector.getElapsedRealtime(); |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1692 | final long nominalTrigger = convertToElapsed(triggerAtTime, type); |
| 1693 | // 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] | 1694 | final long minTrigger = nowElapsed + mConstants.MIN_FUTURITY; |
Christopher Tate | 498c6cb | 2014-11-17 16:09:27 -0800 | [diff] [blame] | 1695 | final long triggerElapsed = (nominalTrigger > minTrigger) ? nominalTrigger : minTrigger; |
| 1696 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1697 | final long maxElapsed; |
| 1698 | if (windowLength == AlarmManager.WINDOW_EXACT) { |
| 1699 | maxElapsed = triggerElapsed; |
| 1700 | } else if (windowLength < 0) { |
| 1701 | maxElapsed = maxTriggerTime(nowElapsed, triggerElapsed, interval); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1702 | // Fix this window in place, so that as time approaches we don't collapse it. |
| 1703 | windowLength = maxElapsed - triggerElapsed; |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1704 | } else { |
| 1705 | maxElapsed = triggerElapsed + windowLength; |
| 1706 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1707 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | synchronized (mLock) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1709 | if (DEBUG_BATCH) { |
| 1710 | Slog.v(TAG, "set(" + operation + ") : type=" + type |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 1711 | + " triggerAtTime=" + triggerAtTime + " win=" + windowLength |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1712 | + " tElapsed=" + triggerElapsed + " maxElapsed=" + maxElapsed |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1713 | + " interval=" + interval + " flags=0x" + Integer.toHexString(flags)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1714 | } |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1715 | setImplLocked(type, triggerAtTime, triggerElapsed, windowLength, maxElapsed, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1716 | interval, operation, directReceiver, listenerTag, flags, true, workSource, |
| 1717 | alarmClock, callingUid, callingPackage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1718 | } |
| 1719 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1721 | private void setImplLocked(int type, long when, long whenElapsed, long windowLength, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1722 | long maxWhen, long interval, PendingIntent operation, IAlarmListener directReceiver, |
| 1723 | String listenerTag, int flags, boolean doValidate, WorkSource workSource, |
| 1724 | AlarmManager.AlarmClockInfo alarmClock, int callingUid, String callingPackage) { |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 1725 | Alarm a = new Alarm(type, when, whenElapsed, windowLength, maxWhen, interval, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1726 | operation, directReceiver, listenerTag, workSource, flags, alarmClock, |
| 1727 | callingUid, callingPackage); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1728 | try { |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 1729 | if (ActivityManager.getService().isAppStartModeDisabled(callingUid, callingPackage)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1730 | Slog.w(TAG, "Not setting alarm from " + callingUid + ":" + a |
| 1731 | + " -- package not allowed to start"); |
| 1732 | return; |
| 1733 | } |
| 1734 | } catch (RemoteException e) { |
| 1735 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1736 | removeLocked(operation, directReceiver); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1737 | setImplLocked(a, false, doValidate); |
| 1738 | } |
Christopher Tate | b8849c1 | 2011-02-08 13:39:01 -0800 | [diff] [blame] | 1739 | |
Christopher Tate | f215971 | 2018-03-27 16:04:14 -0700 | [diff] [blame] | 1740 | /** |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 1741 | * Returns the maximum alarms that an app in the specified bucket can receive in a rolling time |
| 1742 | * window given by {@link Constants#APP_STANDBY_WINDOW} |
| 1743 | */ |
| 1744 | @VisibleForTesting |
| 1745 | int getQuotaForBucketLocked(int bucket) { |
| 1746 | final int index; |
| 1747 | if (bucket <= UsageStatsManager.STANDBY_BUCKET_ACTIVE) { |
| 1748 | index = ACTIVE_INDEX; |
| 1749 | } else if (bucket <= UsageStatsManager.STANDBY_BUCKET_WORKING_SET) { |
| 1750 | index = WORKING_INDEX; |
| 1751 | } else if (bucket <= UsageStatsManager.STANDBY_BUCKET_FREQUENT) { |
| 1752 | index = FREQUENT_INDEX; |
| 1753 | } else if (bucket < UsageStatsManager.STANDBY_BUCKET_NEVER) { |
| 1754 | index = RARE_INDEX; |
| 1755 | } else { |
| 1756 | index = NEVER_INDEX; |
| 1757 | } |
| 1758 | return mConstants.APP_STANDBY_QUOTAS[index]; |
| 1759 | } |
| 1760 | |
| 1761 | /** |
Christopher Tate | f215971 | 2018-03-27 16:04:14 -0700 | [diff] [blame] | 1762 | * Return the minimum time that should elapse before an app in the specified bucket |
| 1763 | * can receive alarms again |
| 1764 | */ |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1765 | @VisibleForTesting |
| 1766 | long getMinDelayForBucketLocked(int bucket) { |
Christopher Tate | f215971 | 2018-03-27 16:04:14 -0700 | [diff] [blame] | 1767 | // UsageStats bucket values are treated as floors of their behavioral range. |
| 1768 | // In other words, a bucket value between WORKING and ACTIVE is treated as |
| 1769 | // WORKING, not as ACTIVE. The ACTIVE and NEVER bucket apply only at specific |
| 1770 | // values. |
| 1771 | final int index; |
| 1772 | |
| 1773 | if (bucket == UsageStatsManager.STANDBY_BUCKET_NEVER) index = NEVER_INDEX; |
| 1774 | else if (bucket > UsageStatsManager.STANDBY_BUCKET_FREQUENT) index = RARE_INDEX; |
| 1775 | else if (bucket > UsageStatsManager.STANDBY_BUCKET_WORKING_SET) index = FREQUENT_INDEX; |
| 1776 | else if (bucket > UsageStatsManager.STANDBY_BUCKET_ACTIVE) index = WORKING_INDEX; |
| 1777 | else index = ACTIVE_INDEX; |
| 1778 | |
| 1779 | return mConstants.APP_STANDBY_MIN_DELAYS[index]; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1782 | /** |
| 1783 | * Adjusts the alarm delivery time based on the current app standby bucket. |
| 1784 | * @param alarm The alarm to adjust |
| 1785 | * @return true if the alarm delivery time was updated. |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1786 | */ |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 1787 | private boolean adjustDeliveryTimeBasedOnBucketLocked(Alarm alarm) { |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 1788 | if (isExemptFromAppStandby(alarm)) { |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1789 | return false; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1790 | } |
| 1791 | if (mAppStandbyParole) { |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 1792 | if (alarm.whenElapsed > alarm.expectedWhenElapsed) { |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1793 | // We did defer this alarm earlier, restore original requirements |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 1794 | alarm.whenElapsed = alarm.expectedWhenElapsed; |
| 1795 | alarm.maxWhenElapsed = alarm.expectedMaxWhenElapsed; |
| 1796 | return true; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1797 | } |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 1798 | return false; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1799 | } |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1800 | final long oldWhenElapsed = alarm.whenElapsed; |
| 1801 | final long oldMaxWhenElapsed = alarm.maxWhenElapsed; |
| 1802 | |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1803 | final String sourcePackage = alarm.sourcePackage; |
| 1804 | final int sourceUserId = UserHandle.getUserId(alarm.creatorUid); |
| 1805 | final int standbyBucket = mUsageStatsManagerInternal.getAppStandbyBucket( |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1806 | sourcePackage, sourceUserId, mInjector.getElapsedRealtime()); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1807 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 1808 | if (mConstants.APP_STANDBY_QUOTAS_ENABLED) { |
| 1809 | // Quota deferring implementation: |
| 1810 | final int wakeupsInWindow = mAppWakeupHistory.getTotalWakeupsInWindow(sourcePackage, |
| 1811 | sourceUserId); |
| 1812 | final int quotaForBucket = getQuotaForBucketLocked(standbyBucket); |
| 1813 | boolean deferred = false; |
| 1814 | if (wakeupsInWindow >= quotaForBucket) { |
| 1815 | final long minElapsed; |
| 1816 | if (quotaForBucket <= 0) { |
| 1817 | // Just keep deferring for a day till the quota changes |
| 1818 | minElapsed = mInjector.getElapsedRealtime() + MILLIS_IN_DAY; |
| 1819 | } else { |
| 1820 | // Suppose the quota for window was q, and the qth last delivery time for this |
| 1821 | // package was t(q) then the next delivery must be after t(q) + <window_size> |
| 1822 | final long t = mAppWakeupHistory.getLastWakeupForPackage(sourcePackage, |
| 1823 | sourceUserId, quotaForBucket); |
| 1824 | minElapsed = t + 1 + mConstants.APP_STANDBY_WINDOW; |
| 1825 | } |
| 1826 | if (alarm.expectedWhenElapsed < minElapsed) { |
| 1827 | alarm.whenElapsed = alarm.maxWhenElapsed = minElapsed; |
| 1828 | deferred = true; |
| 1829 | } |
| 1830 | } |
| 1831 | if (!deferred) { |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1832 | // Restore original requirements in case they were changed earlier. |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 1833 | alarm.whenElapsed = alarm.expectedWhenElapsed; |
| 1834 | alarm.maxWhenElapsed = alarm.expectedMaxWhenElapsed; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1835 | } |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 1836 | } else { |
| 1837 | // Minimum delay deferring implementation: |
| 1838 | final long lastElapsed = mAppWakeupHistory.getLastWakeupForPackage(sourcePackage, |
| 1839 | sourceUserId, 1); |
| 1840 | if (lastElapsed > 0) { |
| 1841 | final long minElapsed = lastElapsed + getMinDelayForBucketLocked(standbyBucket); |
| 1842 | if (alarm.expectedWhenElapsed < minElapsed) { |
| 1843 | alarm.whenElapsed = alarm.maxWhenElapsed = minElapsed; |
| 1844 | } else { |
| 1845 | // app is now eligible to run alarms at the originally requested window. |
| 1846 | // Restore original requirements in case they were changed earlier. |
| 1847 | alarm.whenElapsed = alarm.expectedWhenElapsed; |
| 1848 | alarm.maxWhenElapsed = alarm.expectedMaxWhenElapsed; |
| 1849 | } |
| 1850 | } |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1851 | } |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1852 | return (oldWhenElapsed != alarm.whenElapsed || oldMaxWhenElapsed != alarm.maxWhenElapsed); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 1853 | } |
| 1854 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1855 | private void setImplLocked(Alarm a, boolean rebatching, boolean doValidate) { |
| 1856 | if ((a.flags&AlarmManager.FLAG_IDLE_UNTIL) != 0) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1857 | // 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] | 1858 | // The caller has given the time they want this to happen at, however we need |
| 1859 | // 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] | 1860 | // bring us out of idle at an earlier time. |
| 1861 | if (mNextWakeFromIdle != null && a.whenElapsed > mNextWakeFromIdle.whenElapsed) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1862 | a.when = a.whenElapsed = a.maxWhenElapsed = mNextWakeFromIdle.whenElapsed; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1863 | } |
| 1864 | // Add fuzz to make the alarm go off some time before the actual desired time. |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1865 | final long nowElapsed = mInjector.getElapsedRealtime(); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1866 | final int fuzz = fuzzForDuration(a.whenElapsed-nowElapsed); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1867 | if (fuzz > 0) { |
| 1868 | if (mRandom == null) { |
| 1869 | mRandom = new Random(); |
| 1870 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1871 | final int delta = mRandom.nextInt(fuzz); |
| 1872 | a.whenElapsed -= delta; |
| 1873 | if (false) { |
| 1874 | Slog.d(TAG, "Alarm when: " + a.whenElapsed); |
| 1875 | Slog.d(TAG, "Delta until alarm: " + (a.whenElapsed-nowElapsed)); |
| 1876 | Slog.d(TAG, "Applied fuzz: " + fuzz); |
| 1877 | Slog.d(TAG, "Final delta: " + delta); |
| 1878 | Slog.d(TAG, "Final when: " + a.whenElapsed); |
| 1879 | } |
| 1880 | a.when = a.maxWhenElapsed = a.whenElapsed; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | } else if (mPendingIdleUntil != null) { |
| 1884 | // We currently have an idle until alarm scheduled; if the new alarm has |
| 1885 | // 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] | 1886 | if ((a.flags&(AlarmManager.FLAG_ALLOW_WHILE_IDLE |
| 1887 | | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED |
| 1888 | | AlarmManager.FLAG_WAKE_FROM_IDLE)) |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1889 | == 0) { |
| 1890 | mPendingWhileIdleAlarms.add(a); |
| 1891 | return; |
| 1892 | } |
| 1893 | } |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1894 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1895 | if ((a.flags & AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0) { |
| 1896 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 1897 | ent.uid = a.uid; |
| 1898 | ent.pkg = a.operation.getCreatorPackage(); |
| 1899 | ent.tag = a.operation.getTag(""); |
| 1900 | ent.op = "SET"; |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1901 | ent.elapsedRealtime = mInjector.getElapsedRealtime(); |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1902 | ent.argRealtime = a.whenElapsed; |
| 1903 | mAllowWhileIdleDispatches.add(ent); |
| 1904 | } |
| 1905 | } |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 1906 | adjustDeliveryTimeBasedOnBucketLocked(a); |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 1907 | insertAndBatchAlarmLocked(a); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1908 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1909 | if (a.alarmClock != null) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1910 | mNextAlarmClockMayChange = true; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 1911 | } |
| 1912 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1913 | boolean needRebatch = false; |
| 1914 | |
| 1915 | if ((a.flags&AlarmManager.FLAG_IDLE_UNTIL) != 0) { |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1916 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 1917 | if (mPendingIdleUntil == null) { |
| 1918 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 1919 | ent.uid = 0; |
| 1920 | ent.pkg = "START IDLE"; |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 1921 | ent.elapsedRealtime = mInjector.getElapsedRealtime(); |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 1922 | mAllowWhileIdleDispatches.add(ent); |
| 1923 | } |
| 1924 | } |
Makoto Onuki | 4274a6d | 2017-04-21 12:21:25 -0700 | [diff] [blame] | 1925 | if ((mPendingIdleUntil != a) && (mPendingIdleUntil != null)) { |
| 1926 | Slog.wtfStack(TAG, "setImplLocked: idle until changed from " + mPendingIdleUntil |
| 1927 | + " to " + a); |
| 1928 | } |
| 1929 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1930 | mPendingIdleUntil = a; |
| 1931 | needRebatch = true; |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 1932 | } else if ((a.flags&AlarmManager.FLAG_WAKE_FROM_IDLE) != 0) { |
| 1933 | if (mNextWakeFromIdle == null || mNextWakeFromIdle.whenElapsed > a.whenElapsed) { |
| 1934 | mNextWakeFromIdle = a; |
| 1935 | // If this wake from idle is earlier than whatever was previously scheduled, |
| 1936 | // and we are currently idling, then we need to rebatch alarms in case the idle |
| 1937 | // until time needs to be updated. |
| 1938 | if (mPendingIdleUntil != null) { |
| 1939 | needRebatch = true; |
| 1940 | } |
| 1941 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | if (!rebatching) { |
| 1945 | if (DEBUG_VALIDATE) { |
| 1946 | if (doValidate && !validateConsistencyLocked()) { |
| 1947 | Slog.v(TAG, "Tipping-point operation: type=" + a.type + " when=" + a.when |
| 1948 | + " when(hex)=" + Long.toHexString(a.when) |
| 1949 | + " whenElapsed=" + a.whenElapsed |
| 1950 | + " maxWhenElapsed=" + a.maxWhenElapsed |
| 1951 | + " interval=" + a.repeatInterval + " op=" + a.operation |
| 1952 | + " flags=0x" + Integer.toHexString(a.flags)); |
| 1953 | rebatchAllAlarmsLocked(false); |
| 1954 | needRebatch = false; |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | if (needRebatch) { |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1959 | rebatchAllAlarmsLocked(false); |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1960 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 1961 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 1962 | rescheduleKernelAlarmsLocked(); |
| 1963 | updateNextAlarmClockLocked(); |
| 1964 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 1967 | /** |
| 1968 | * System-process internal API |
| 1969 | */ |
| 1970 | private final class LocalService implements AlarmManagerInternal { |
| 1971 | @Override |
| 1972 | public void removeAlarmsForUid(int uid) { |
| 1973 | synchronized (mLock) { |
| 1974 | removeLocked(uid); |
| 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | /** |
| 1980 | * Public-facing binder interface |
| 1981 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1982 | private final IBinder mService = new IAlarmManager.Stub() { |
| 1983 | @Override |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1984 | public void set(String callingPackage, |
| 1985 | int type, long triggerAtTime, long windowLength, long interval, int flags, |
| 1986 | PendingIntent operation, IAlarmListener directReceiver, String listenerTag, |
| 1987 | WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 1988 | final int callingUid = Binder.getCallingUid(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 1989 | |
| 1990 | // make sure the caller is not lying about which package should be blamed for |
| 1991 | // wakelock time spent in alarm delivery |
| 1992 | mAppOps.checkPackage(callingUid, callingPackage); |
| 1993 | |
| 1994 | // Repeating alarms must use PendingIntent, not direct listener |
| 1995 | if (interval != 0) { |
| 1996 | if (directReceiver != null) { |
| 1997 | throw new IllegalArgumentException("Repeating alarms cannot use AlarmReceivers"); |
| 1998 | } |
| 1999 | } |
| 2000 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2001 | if (workSource != null) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2002 | getContext().enforcePermission( |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2003 | android.Manifest.permission.UPDATE_DEVICE_STATS, |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2004 | Binder.getCallingPid(), callingUid, "AlarmManager.set"); |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2007 | // No incoming callers can request either WAKE_FROM_IDLE or |
| 2008 | // ALLOW_WHILE_IDLE_UNRESTRICTED -- we will apply those later as appropriate. |
| 2009 | flags &= ~(AlarmManager.FLAG_WAKE_FROM_IDLE |
| 2010 | | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED); |
| 2011 | |
| 2012 | // Only the system can use FLAG_IDLE_UNTIL -- this is used to tell the alarm |
| 2013 | // manager when to come out of idle mode, which is only for DeviceIdleController. |
| 2014 | if (callingUid != Process.SYSTEM_UID) { |
| 2015 | flags &= ~AlarmManager.FLAG_IDLE_UNTIL; |
| 2016 | } |
| 2017 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2018 | // 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] | 2019 | if (windowLength == AlarmManager.WINDOW_EXACT) { |
| 2020 | flags |= AlarmManager.FLAG_STANDALONE; |
| 2021 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2022 | |
| 2023 | // If this alarm is for an alarm clock, then it must be standalone and we will |
| 2024 | // use it to wake early from idle if needed. |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2025 | if (alarmClock != null) { |
| 2026 | flags |= AlarmManager.FLAG_WAKE_FROM_IDLE | AlarmManager.FLAG_STANDALONE; |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2027 | |
| 2028 | // If the caller is a core system component or on the user's whitelist, and not calling |
| 2029 | // to do work on behalf of someone else, then always set ALLOW_WHILE_IDLE_UNRESTRICTED. |
| 2030 | // This means we will allow these alarms to go off as normal even while idle, with no |
| 2031 | // timing restrictions. |
| 2032 | } else if (workSource == null && (callingUid < Process.FIRST_APPLICATION_UID |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 2033 | || UserHandle.isSameApp(callingUid, mSystemUiUid) |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 2034 | || ((mAppStateTracker != null) |
Suprabh Shukla | 5bf4981 | 2018-05-24 18:38:50 -0700 | [diff] [blame] | 2035 | && mAppStateTracker.isUidPowerSaveUserWhitelisted(callingUid)))) { |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2036 | flags |= AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED; |
| 2037 | flags &= ~AlarmManager.FLAG_ALLOW_WHILE_IDLE; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2038 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2039 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2040 | setImpl(type, triggerAtTime, windowLength, interval, operation, directReceiver, |
| 2041 | listenerTag, flags, workSource, alarmClock, callingUid, callingPackage); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2042 | } |
Christopher Tate | 8977982 | 2012-08-31 14:40:03 -0700 | [diff] [blame] | 2043 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2044 | @Override |
Greg Hackmann | 0cab896 | 2014-02-21 16:35:52 -0800 | [diff] [blame] | 2045 | public boolean setTime(long millis) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2046 | getContext().enforceCallingOrSelfPermission( |
| 2047 | "android.permission.SET_TIME", |
| 2048 | "setTime"); |
| 2049 | |
mswest46 | 3f4c99d | 2018-02-01 10:13:10 -0800 | [diff] [blame] | 2050 | return setTimeImpl(millis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2051 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2052 | |
| 2053 | @Override |
| 2054 | public void setTimeZone(String tz) { |
| 2055 | getContext().enforceCallingOrSelfPermission( |
| 2056 | "android.permission.SET_TIME_ZONE", |
| 2057 | "setTimeZone"); |
| 2058 | |
| 2059 | final long oldId = Binder.clearCallingIdentity(); |
| 2060 | try { |
| 2061 | setTimeZoneImpl(tz); |
| 2062 | } finally { |
| 2063 | Binder.restoreCallingIdentity(oldId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2064 | } |
| 2065 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 2066 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2067 | @Override |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2068 | public void remove(PendingIntent operation, IAlarmListener listener) { |
| 2069 | if (operation == null && listener == null) { |
| 2070 | Slog.w(TAG, "remove() with no intent or listener"); |
| 2071 | return; |
| 2072 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2073 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 2074 | synchronized (mLock) { |
| 2075 | removeLocked(operation, listener); |
| 2076 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2077 | } |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 2078 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2079 | @Override |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2080 | public long getNextWakeFromIdleTime() { |
| 2081 | return getNextWakeFromIdleTimeImpl(); |
| 2082 | } |
| 2083 | |
| 2084 | @Override |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 2085 | public AlarmManager.AlarmClockInfo getNextAlarmClock(int userId) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2086 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2087 | Binder.getCallingUid(), userId, false /* allowAll */, false /* requireFull */, |
| 2088 | "getNextAlarmClock", null); |
| 2089 | |
| 2090 | return getNextAlarmClockImpl(userId); |
| 2091 | } |
| 2092 | |
| 2093 | @Override |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 2094 | public long currentNetworkTimeMillis() { |
| 2095 | final NtpTrustedTime time = NtpTrustedTime.getInstance(getContext()); |
| 2096 | if (time.hasCache()) { |
| 2097 | return time.currentTimeMillis(); |
| 2098 | } else { |
| 2099 | throw new ParcelableException(new DateTimeException("Missing NTP fix")); |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2104 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2105 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2106 | |
| 2107 | if (args.length > 0 && "--proto".equals(args[0])) { |
| 2108 | dumpProto(fd); |
| 2109 | } else { |
| 2110 | dumpImpl(pw); |
| 2111 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2112 | } |
mswest46 | 3f4c99d | 2018-02-01 10:13:10 -0800 | [diff] [blame] | 2113 | |
| 2114 | @Override |
| 2115 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 2116 | FileDescriptor err, String[] args, ShellCallback callback, |
| 2117 | ResultReceiver resultReceiver) { |
| 2118 | (new ShellCmd()).exec(this, in, out, err, args, callback, resultReceiver); |
| 2119 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2120 | }; |
Christopher Tate | 4cb338d | 2013-07-26 13:11:31 -0700 | [diff] [blame] | 2121 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2122 | void dumpImpl(PrintWriter pw) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2123 | synchronized (mLock) { |
| 2124 | pw.println("Current Alarm Manager state:"); |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 2125 | mConstants.dump(pw, " "); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2126 | pw.println(); |
| 2127 | |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 2128 | if (mAppStateTracker != null) { |
| 2129 | mAppStateTracker.dump(pw, " "); |
| 2130 | pw.println(); |
| 2131 | } |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 2132 | |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 2133 | pw.println(" App Standby Parole: " + mAppStandbyParole); |
| 2134 | pw.println(); |
| 2135 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2136 | final long nowELAPSED = mInjector.getElapsedRealtime(); |
Makoto Onuki | e8edbcf | 2018-03-02 16:49:29 -0800 | [diff] [blame] | 2137 | final long nowUPTIME = SystemClock.uptimeMillis(); |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 2138 | final long nowRTC = mInjector.getCurrentTimeMillis(); |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 2139 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2140 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2141 | pw.print(" nowRTC="); pw.print(nowRTC); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2142 | pw.print("="); pw.print(sdf.format(new Date(nowRTC))); |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 2143 | pw.print(" nowELAPSED="); pw.print(nowELAPSED); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2144 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2145 | pw.print(" mLastTimeChangeClockTime="); pw.print(mLastTimeChangeClockTime); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2146 | pw.print("="); pw.println(sdf.format(new Date(mLastTimeChangeClockTime))); |
Christopher Tate | 12cf0b6 | 2018-01-05 18:40:14 -0800 | [diff] [blame] | 2147 | pw.print(" mLastTimeChangeRealtime="); pw.println(mLastTimeChangeRealtime); |
Christopher Tate | 12cf0b6 | 2018-01-05 18:40:14 -0800 | [diff] [blame] | 2148 | pw.print(" mLastTickReceived="); pw.println(sdf.format(new Date(mLastTickReceived))); |
| 2149 | pw.print(" mLastTickSet="); pw.println(sdf.format(new Date(mLastTickSet))); |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 2150 | pw.print(" mLastTickAdded="); pw.println(sdf.format(new Date(mLastTickAdded))); |
| 2151 | pw.print(" mLastTickRemoved="); pw.println(sdf.format(new Date(mLastTickRemoved))); |
Makoto Onuki | e8edbcf | 2018-03-02 16:49:29 -0800 | [diff] [blame] | 2152 | |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 2153 | if (RECORD_ALARMS_IN_HISTORY) { |
| 2154 | pw.println(); |
| 2155 | pw.println(" Recent TIME_TICK history:"); |
| 2156 | int i = mNextTickHistory; |
| 2157 | do { |
| 2158 | i--; |
| 2159 | if (i < 0) i = TICK_HISTORY_DEPTH - 1; |
| 2160 | final long time = mTickHistory[i]; |
| 2161 | pw.print(" "); |
| 2162 | pw.println((time > 0) |
| 2163 | ? sdf.format(new Date(nowRTC - (nowELAPSED - time))) |
| 2164 | : "-"); |
| 2165 | } while (i != mNextTickHistory); |
| 2166 | pw.println(); |
| 2167 | } |
| 2168 | |
Makoto Onuki | e8edbcf | 2018-03-02 16:49:29 -0800 | [diff] [blame] | 2169 | SystemServiceManager ssm = LocalServices.getService(SystemServiceManager.class); |
| 2170 | if (ssm != null) { |
| 2171 | pw.println(); |
| 2172 | pw.print(" RuntimeStarted="); |
| 2173 | pw.print(sdf.format( |
| 2174 | new Date(nowRTC - nowELAPSED + ssm.getRuntimeStartElapsedTime()))); |
| 2175 | if (ssm.isRuntimeRestarted()) { |
| 2176 | pw.print(" (Runtime restarted)"); |
| 2177 | } |
| 2178 | pw.println(); |
| 2179 | pw.print(" Runtime uptime (elapsed): "); |
| 2180 | TimeUtils.formatDuration(nowELAPSED, ssm.getRuntimeStartElapsedTime(), pw); |
| 2181 | pw.println(); |
| 2182 | pw.print(" Runtime uptime (uptime): "); |
| 2183 | TimeUtils.formatDuration(nowUPTIME, ssm.getRuntimeStartUptime(), pw); |
| 2184 | pw.println(); |
| 2185 | } |
| 2186 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 2187 | pw.println(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2188 | if (!mInteractive) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2189 | pw.print(" Time since non-interactive: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2190 | TimeUtils.formatDuration(nowELAPSED - mNonInteractiveStartTime, pw); |
| 2191 | pw.println(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2192 | } |
Makoto Onuki | 0b05aa6 | 2018-02-09 16:14:26 -0800 | [diff] [blame] | 2193 | pw.print(" Max wakeup delay: "); |
| 2194 | TimeUtils.formatDuration(currentNonWakeupFuzzLocked(nowELAPSED), pw); |
| 2195 | pw.println(); |
| 2196 | pw.print(" Time since last dispatch: "); |
| 2197 | TimeUtils.formatDuration(nowELAPSED - mLastAlarmDeliveryTime, pw); |
| 2198 | pw.println(); |
| 2199 | pw.print(" Next non-wakeup delivery time: "); |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2200 | TimeUtils.formatDuration(mNextNonWakeupDeliveryTime, nowELAPSED, pw); |
Makoto Onuki | 0b05aa6 | 2018-02-09 16:14:26 -0800 | [diff] [blame] | 2201 | pw.println(); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2202 | |
| 2203 | long nextWakeupRTC = mNextWakeup + (nowRTC - nowELAPSED); |
| 2204 | long nextNonWakeupRTC = mNextNonWakeup + (nowRTC - nowELAPSED); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2205 | pw.print(" Next non-wakeup alarm: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2206 | TimeUtils.formatDuration(mNextNonWakeup, nowELAPSED, pw); |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 2207 | pw.print(" = "); pw.print(mNextNonWakeup); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2208 | pw.print(" = "); pw.println(sdf.format(new Date(nextNonWakeupRTC))); |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2209 | pw.print(" set at "); TimeUtils.formatDuration(mNextNonWakeUpSetAt, nowELAPSED, pw); |
| 2210 | pw.println(); |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 2211 | pw.print(" Next wakeup alarm: "); TimeUtils.formatDuration(mNextWakeup, nowELAPSED, pw); |
| 2212 | pw.print(" = "); pw.print(mNextWakeup); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2213 | pw.print(" = "); pw.println(sdf.format(new Date(nextWakeupRTC))); |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2214 | pw.print(" set at "); TimeUtils.formatDuration(mNextWakeUpSetAt, nowELAPSED, pw); |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 2215 | pw.println(); |
Suprabh Shukla | 2324e98 | 2018-06-05 21:06:22 -0700 | [diff] [blame] | 2216 | |
| 2217 | pw.print(" Next kernel non-wakeup alarm: "); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2218 | TimeUtils.formatDuration(mInjector.getNextAlarm(ELAPSED_REALTIME), pw); |
Suprabh Shukla | 2324e98 | 2018-06-05 21:06:22 -0700 | [diff] [blame] | 2219 | pw.println(); |
| 2220 | pw.print(" Next kernel wakeup alarm: "); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2221 | TimeUtils.formatDuration(mInjector.getNextAlarm(ELAPSED_REALTIME_WAKEUP), pw); |
Suprabh Shukla | 2324e98 | 2018-06-05 21:06:22 -0700 | [diff] [blame] | 2222 | pw.println(); |
| 2223 | |
Dianne Hackborn | aa9a59d | 2016-05-23 15:59:18 -0700 | [diff] [blame] | 2224 | pw.print(" Last wakeup: "); TimeUtils.formatDuration(mLastWakeup, nowELAPSED, pw); |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 2225 | pw.print(" = "); pw.println(mLastWakeup); |
| 2226 | pw.print(" Last trigger: "); TimeUtils.formatDuration(mLastTrigger, nowELAPSED, pw); |
| 2227 | pw.print(" = "); pw.println(mLastTrigger); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2228 | pw.print(" Num time change events: "); pw.println(mNumTimeChanged); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2229 | |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 2230 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2231 | pw.println(" Next alarm clock information: "); |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 2232 | final TreeSet<Integer> users = new TreeSet<>(); |
| 2233 | for (int i = 0; i < mNextAlarmClockForUser.size(); i++) { |
| 2234 | users.add(mNextAlarmClockForUser.keyAt(i)); |
| 2235 | } |
| 2236 | for (int i = 0; i < mPendingSendNextAlarmClockChangedForUser.size(); i++) { |
| 2237 | users.add(mPendingSendNextAlarmClockChangedForUser.keyAt(i)); |
| 2238 | } |
| 2239 | for (int user : users) { |
| 2240 | final AlarmManager.AlarmClockInfo next = mNextAlarmClockForUser.get(user); |
| 2241 | final long time = next != null ? next.getTriggerTime() : 0; |
| 2242 | final boolean pendingSend = mPendingSendNextAlarmClockChangedForUser.get(user); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2243 | pw.print(" user:"); pw.print(user); |
John Spurlock | 604a5ee | 2015-06-01 12:27:22 -0400 | [diff] [blame] | 2244 | pw.print(" pendingSend:"); pw.print(pendingSend); |
| 2245 | pw.print(" time:"); pw.print(time); |
| 2246 | if (time > 0) { |
| 2247 | pw.print(" = "); pw.print(sdf.format(new Date(time))); |
| 2248 | pw.print(" = "); TimeUtils.formatDuration(time, nowRTC, pw); |
| 2249 | } |
| 2250 | pw.println(); |
| 2251 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2252 | if (mAlarmBatches.size() > 0) { |
| 2253 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2254 | pw.print(" Pending alarm batches: "); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 2255 | pw.println(mAlarmBatches.size()); |
| 2256 | for (Batch b : mAlarmBatches) { |
| 2257 | pw.print(b); pw.println(':'); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2258 | dumpAlarmList(pw, b.alarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2259 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2260 | } |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 2261 | pw.println(); |
| 2262 | pw.println(" Pending user blocked background alarms: "); |
| 2263 | boolean blocked = false; |
| 2264 | for (int i = 0; i < mPendingBackgroundAlarms.size(); i++) { |
| 2265 | final ArrayList<Alarm> blockedAlarms = mPendingBackgroundAlarms.valueAt(i); |
| 2266 | if (blockedAlarms != null && blockedAlarms.size() > 0) { |
| 2267 | blocked = true; |
| 2268 | dumpAlarmList(pw, blockedAlarms, " ", nowELAPSED, nowRTC, sdf); |
| 2269 | } |
| 2270 | } |
| 2271 | if (!blocked) { |
| 2272 | pw.println(" none"); |
| 2273 | } |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 2274 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 2275 | mAppWakeupHistory.dump(pw, " ", nowELAPSED); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 2276 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2277 | if (mPendingIdleUntil != null || mPendingWhileIdleAlarms.size() > 0) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2278 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2279 | pw.println(" Idle mode state:"); |
| 2280 | pw.print(" Idling until: "); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2281 | if (mPendingIdleUntil != null) { |
| 2282 | pw.println(mPendingIdleUntil); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2283 | mPendingIdleUntil.dump(pw, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2284 | } else { |
| 2285 | pw.println("null"); |
| 2286 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2287 | pw.println(" Pending alarms:"); |
| 2288 | dumpAlarmList(pw, mPendingWhileIdleAlarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 2289 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2290 | if (mNextWakeFromIdle != null) { |
| 2291 | pw.println(); |
| 2292 | pw.print(" Next wake from idle: "); pw.println(mNextWakeFromIdle); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2293 | mNextWakeFromIdle.dump(pw, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2294 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2295 | |
| 2296 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2297 | pw.print(" Past-due non-wakeup alarms: "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2298 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 2299 | pw.println(mPendingNonWakeupAlarms.size()); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2300 | dumpAlarmList(pw, mPendingNonWakeupAlarms, " ", nowELAPSED, nowRTC, sdf); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2301 | } else { |
| 2302 | pw.println("(none)"); |
| 2303 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2304 | pw.print(" Number of delayed alarms: "); pw.print(mNumDelayedAlarms); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2305 | pw.print(", total delay time: "); TimeUtils.formatDuration(mTotalDelayTime, pw); |
| 2306 | pw.println(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2307 | pw.print(" Max delay time: "); TimeUtils.formatDuration(mMaxDelayTime, pw); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2308 | pw.print(", max non-interactive time: "); |
| 2309 | TimeUtils.formatDuration(mNonInteractiveTime, pw); |
| 2310 | pw.println(); |
| 2311 | |
| 2312 | pw.println(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 2313 | pw.print(" Broadcast ref count: "); pw.println(mBroadcastRefCount); |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 2314 | pw.print(" PendingIntent send count: "); pw.println(mSendCount); |
| 2315 | pw.print(" PendingIntent finish count: "); pw.println(mSendFinishCount); |
| 2316 | pw.print(" Listener send count: "); pw.println(mListenerCount); |
| 2317 | pw.print(" Listener finish count: "); pw.println(mListenerFinishCount); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2318 | pw.println(); |
| 2319 | |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 2320 | if (mInFlight.size() > 0) { |
| 2321 | pw.println("Outstanding deliveries:"); |
| 2322 | for (int i = 0; i < mInFlight.size(); i++) { |
| 2323 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 2324 | pw.println(mInFlight.get(i)); |
| 2325 | } |
| 2326 | pw.println(); |
| 2327 | } |
| 2328 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2329 | if (mLastAllowWhileIdleDispatch.size() > 0) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2330 | pw.println(" Last allow while idle dispatch times:"); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2331 | for (int i=0; i<mLastAllowWhileIdleDispatch.size(); i++) { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 2332 | pw.print(" UID "); |
| 2333 | final int uid = mLastAllowWhileIdleDispatch.keyAt(i); |
| 2334 | UserHandle.formatUid(pw, uid); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2335 | pw.print(": "); |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 2336 | final long lastTime = mLastAllowWhileIdleDispatch.valueAt(i); |
| 2337 | TimeUtils.formatDuration(lastTime, nowELAPSED, pw); |
| 2338 | |
| 2339 | final long minInterval = getWhileIdleMinIntervalLocked(uid); |
| 2340 | pw.print(" Next allowed:"); |
| 2341 | TimeUtils.formatDuration(lastTime + minInterval, nowELAPSED, pw); |
| 2342 | pw.print(" ("); |
| 2343 | TimeUtils.formatDuration(minInterval, 0, pw); |
| 2344 | pw.print(")"); |
| 2345 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2346 | pw.println(); |
| 2347 | } |
| 2348 | } |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 2349 | |
| 2350 | pw.print(" mUseAllowWhileIdleShortTime: ["); |
| 2351 | for (int i = 0; i < mUseAllowWhileIdleShortTime.size(); i++) { |
| 2352 | if (mUseAllowWhileIdleShortTime.valueAt(i)) { |
| 2353 | UserHandle.formatUid(pw, mUseAllowWhileIdleShortTime.keyAt(i)); |
| 2354 | pw.print(" "); |
| 2355 | } |
| 2356 | } |
| 2357 | pw.println("]"); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2358 | pw.println(); |
| 2359 | |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2360 | if (mLog.dump(pw, " Recent problems", " ")) { |
| 2361 | pw.println(); |
| 2362 | } |
| 2363 | |
| 2364 | final FilterStats[] topFilters = new FilterStats[10]; |
| 2365 | final Comparator<FilterStats> comparator = new Comparator<FilterStats>() { |
| 2366 | @Override |
| 2367 | public int compare(FilterStats lhs, FilterStats rhs) { |
| 2368 | if (lhs.aggregateTime < rhs.aggregateTime) { |
| 2369 | return 1; |
| 2370 | } else if (lhs.aggregateTime > rhs.aggregateTime) { |
| 2371 | return -1; |
| 2372 | } |
| 2373 | return 0; |
| 2374 | } |
| 2375 | }; |
| 2376 | int len = 0; |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2377 | // Get the top 10 FilterStats, ordered by aggregateTime. |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2378 | for (int iu=0; iu<mBroadcastStats.size(); iu++) { |
| 2379 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 2380 | for (int ip=0; ip<uidStats.size(); ip++) { |
| 2381 | BroadcastStats bs = uidStats.valueAt(ip); |
| 2382 | for (int is=0; is<bs.filterStats.size(); is++) { |
| 2383 | FilterStats fs = bs.filterStats.valueAt(is); |
| 2384 | int pos = len > 0 |
| 2385 | ? Arrays.binarySearch(topFilters, 0, len, fs, comparator) : 0; |
| 2386 | if (pos < 0) { |
| 2387 | pos = -pos - 1; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2388 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2389 | if (pos < topFilters.length) { |
| 2390 | int copylen = topFilters.length - pos - 1; |
| 2391 | if (copylen > 0) { |
| 2392 | System.arraycopy(topFilters, pos, topFilters, pos+1, copylen); |
| 2393 | } |
| 2394 | topFilters[pos] = fs; |
| 2395 | if (len < topFilters.length) { |
| 2396 | len++; |
| 2397 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2398 | } |
| 2399 | } |
| 2400 | } |
| 2401 | } |
| 2402 | if (len > 0) { |
| 2403 | pw.println(" Top Alarms:"); |
| 2404 | for (int i=0; i<len; i++) { |
| 2405 | FilterStats fs = topFilters[i]; |
| 2406 | pw.print(" "); |
| 2407 | if (fs.nesting > 0) pw.print("*ACTIVE* "); |
| 2408 | TimeUtils.formatDuration(fs.aggregateTime, pw); |
| 2409 | pw.print(" running, "); pw.print(fs.numWakeup); |
| 2410 | pw.print(" wakeups, "); pw.print(fs.count); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2411 | pw.print(" alarms: "); UserHandle.formatUid(pw, fs.mBroadcastStats.mUid); |
| 2412 | pw.print(":"); pw.print(fs.mBroadcastStats.mPackageName); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2413 | pw.println(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2414 | pw.print(" "); pw.print(fs.mTag); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 2415 | pw.println(); |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | pw.println(" "); |
| 2420 | pw.println(" Alarm Stats:"); |
| 2421 | final ArrayList<FilterStats> tmpFilters = new ArrayList<FilterStats>(); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2422 | for (int iu=0; iu<mBroadcastStats.size(); iu++) { |
| 2423 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 2424 | for (int ip=0; ip<uidStats.size(); ip++) { |
| 2425 | BroadcastStats bs = uidStats.valueAt(ip); |
| 2426 | pw.print(" "); |
| 2427 | if (bs.nesting > 0) pw.print("*ACTIVE* "); |
| 2428 | UserHandle.formatUid(pw, bs.mUid); |
| 2429 | pw.print(":"); |
| 2430 | pw.print(bs.mPackageName); |
| 2431 | pw.print(" "); TimeUtils.formatDuration(bs.aggregateTime, pw); |
| 2432 | pw.print(" running, "); pw.print(bs.numWakeup); |
| 2433 | pw.println(" wakeups:"); |
| 2434 | tmpFilters.clear(); |
| 2435 | for (int is=0; is<bs.filterStats.size(); is++) { |
| 2436 | tmpFilters.add(bs.filterStats.valueAt(is)); |
| 2437 | } |
| 2438 | Collections.sort(tmpFilters, comparator); |
| 2439 | for (int i=0; i<tmpFilters.size(); i++) { |
| 2440 | FilterStats fs = tmpFilters.get(i); |
| 2441 | pw.print(" "); |
| 2442 | if (fs.nesting > 0) pw.print("*ACTIVE* "); |
| 2443 | TimeUtils.formatDuration(fs.aggregateTime, pw); |
| 2444 | pw.print(" "); pw.print(fs.numWakeup); |
| 2445 | pw.print(" wakes " ); pw.print(fs.count); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2446 | pw.print(" alarms, last "); |
| 2447 | TimeUtils.formatDuration(fs.lastTime, nowELAPSED, pw); |
| 2448 | pw.println(":"); |
| 2449 | pw.print(" "); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2450 | pw.print(fs.mTag); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 2451 | pw.println(); |
| 2452 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2453 | } |
| 2454 | } |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 2455 | pw.println(); |
| 2456 | mStatLogger.dump(pw, " "); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2457 | |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 2458 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 2459 | pw.println(); |
| 2460 | pw.println(" Allow while idle dispatches:"); |
| 2461 | for (int i = 0; i < mAllowWhileIdleDispatches.size(); i++) { |
| 2462 | IdleDispatchEntry ent = mAllowWhileIdleDispatches.get(i); |
| 2463 | pw.print(" "); |
| 2464 | TimeUtils.formatDuration(ent.elapsedRealtime, nowELAPSED, pw); |
| 2465 | pw.print(": "); |
| 2466 | UserHandle.formatUid(pw, ent.uid); |
| 2467 | pw.print(":"); |
| 2468 | pw.println(ent.pkg); |
| 2469 | if (ent.op != null) { |
| 2470 | pw.print(" "); |
| 2471 | pw.print(ent.op); |
| 2472 | pw.print(" / "); |
| 2473 | pw.print(ent.tag); |
| 2474 | if (ent.argRealtime != 0) { |
| 2475 | pw.print(" ("); |
| 2476 | TimeUtils.formatDuration(ent.argRealtime, nowELAPSED, pw); |
| 2477 | pw.print(")"); |
| 2478 | } |
| 2479 | pw.println(); |
| 2480 | } |
| 2481 | } |
| 2482 | } |
| 2483 | |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2484 | if (WAKEUP_STATS) { |
| 2485 | pw.println(); |
| 2486 | pw.println(" Recent Wakeup History:"); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 2487 | long last = -1; |
| 2488 | for (WakeupEvent event : mRecentWakeups) { |
| 2489 | pw.print(" "); pw.print(sdf.format(new Date(event.when))); |
| 2490 | pw.print('|'); |
| 2491 | if (last < 0) { |
| 2492 | pw.print('0'); |
| 2493 | } else { |
| 2494 | pw.print(event.when - last); |
| 2495 | } |
| 2496 | last = event.when; |
| 2497 | pw.print('|'); pw.print(event.uid); |
| 2498 | pw.print('|'); pw.print(event.action); |
| 2499 | pw.println(); |
| 2500 | } |
| 2501 | pw.println(); |
| 2502 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2503 | } |
| 2504 | } |
| 2505 | |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2506 | void dumpProto(FileDescriptor fd) { |
| 2507 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 2508 | |
| 2509 | synchronized (mLock) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2510 | final long nowRTC = mInjector.getCurrentTimeMillis(); |
| 2511 | final long nowElapsed = mInjector.getElapsedRealtime(); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2512 | proto.write(AlarmManagerServiceDumpProto.CURRENT_TIME, nowRTC); |
| 2513 | proto.write(AlarmManagerServiceDumpProto.ELAPSED_REALTIME, nowElapsed); |
| 2514 | proto.write(AlarmManagerServiceDumpProto.LAST_TIME_CHANGE_CLOCK_TIME, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2515 | mLastTimeChangeClockTime); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2516 | proto.write(AlarmManagerServiceDumpProto.LAST_TIME_CHANGE_REALTIME, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2517 | mLastTimeChangeRealtime); |
| 2518 | |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2519 | mConstants.dumpProto(proto, AlarmManagerServiceDumpProto.SETTINGS); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2520 | |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 2521 | if (mAppStateTracker != null) { |
| 2522 | mAppStateTracker.dumpProto(proto, |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2523 | AlarmManagerServiceDumpProto.FORCE_APP_STANDBY_TRACKER); |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 2524 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2525 | |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2526 | proto.write(AlarmManagerServiceDumpProto.IS_INTERACTIVE, mInteractive); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2527 | if (!mInteractive) { |
| 2528 | // Durations |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2529 | proto.write(AlarmManagerServiceDumpProto.TIME_SINCE_NON_INTERACTIVE_MS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2530 | nowElapsed - mNonInteractiveStartTime); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2531 | proto.write(AlarmManagerServiceDumpProto.MAX_WAKEUP_DELAY_MS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2532 | currentNonWakeupFuzzLocked(nowElapsed)); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2533 | proto.write(AlarmManagerServiceDumpProto.TIME_SINCE_LAST_DISPATCH_MS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2534 | nowElapsed - mLastAlarmDeliveryTime); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2535 | proto.write(AlarmManagerServiceDumpProto.TIME_UNTIL_NEXT_NON_WAKEUP_DELIVERY_MS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2536 | nowElapsed - mNextNonWakeupDeliveryTime); |
| 2537 | } |
| 2538 | |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2539 | proto.write(AlarmManagerServiceDumpProto.TIME_UNTIL_NEXT_NON_WAKEUP_ALARM_MS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2540 | mNextNonWakeup - nowElapsed); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2541 | proto.write(AlarmManagerServiceDumpProto.TIME_UNTIL_NEXT_WAKEUP_MS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2542 | mNextWakeup - nowElapsed); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2543 | proto.write(AlarmManagerServiceDumpProto.TIME_SINCE_LAST_WAKEUP_MS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2544 | nowElapsed - mLastWakeup); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2545 | proto.write(AlarmManagerServiceDumpProto.TIME_SINCE_LAST_WAKEUP_SET_MS, |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2546 | nowElapsed - mNextWakeUpSetAt); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2547 | proto.write(AlarmManagerServiceDumpProto.TIME_CHANGE_EVENT_COUNT, mNumTimeChanged); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2548 | |
| 2549 | final TreeSet<Integer> users = new TreeSet<>(); |
| 2550 | final int nextAlarmClockForUserSize = mNextAlarmClockForUser.size(); |
| 2551 | for (int i = 0; i < nextAlarmClockForUserSize; i++) { |
| 2552 | users.add(mNextAlarmClockForUser.keyAt(i)); |
| 2553 | } |
| 2554 | final int pendingSendNextAlarmClockChangedForUserSize = |
| 2555 | mPendingSendNextAlarmClockChangedForUser.size(); |
| 2556 | for (int i = 0; i < pendingSendNextAlarmClockChangedForUserSize; i++) { |
| 2557 | users.add(mPendingSendNextAlarmClockChangedForUser.keyAt(i)); |
| 2558 | } |
| 2559 | for (int user : users) { |
| 2560 | final AlarmManager.AlarmClockInfo next = mNextAlarmClockForUser.get(user); |
| 2561 | final long time = next != null ? next.getTriggerTime() : 0; |
| 2562 | final boolean pendingSend = mPendingSendNextAlarmClockChangedForUser.get(user); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2563 | final long aToken = proto.start(AlarmManagerServiceDumpProto.NEXT_ALARM_CLOCK_METADATA); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2564 | proto.write(AlarmClockMetadataProto.USER, user); |
| 2565 | proto.write(AlarmClockMetadataProto.IS_PENDING_SEND, pendingSend); |
| 2566 | proto.write(AlarmClockMetadataProto.TRIGGER_TIME_MS, time); |
| 2567 | proto.end(aToken); |
| 2568 | } |
| 2569 | for (Batch b : mAlarmBatches) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2570 | b.writeToProto(proto, AlarmManagerServiceDumpProto.PENDING_ALARM_BATCHES, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2571 | nowElapsed, nowRTC); |
| 2572 | } |
| 2573 | for (int i = 0; i < mPendingBackgroundAlarms.size(); i++) { |
| 2574 | final ArrayList<Alarm> blockedAlarms = mPendingBackgroundAlarms.valueAt(i); |
| 2575 | if (blockedAlarms != null) { |
| 2576 | for (Alarm a : blockedAlarms) { |
| 2577 | a.writeToProto(proto, |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2578 | AlarmManagerServiceDumpProto.PENDING_USER_BLOCKED_BACKGROUND_ALARMS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2579 | nowElapsed, nowRTC); |
| 2580 | } |
| 2581 | } |
| 2582 | } |
| 2583 | if (mPendingIdleUntil != null) { |
| 2584 | mPendingIdleUntil.writeToProto( |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2585 | proto, AlarmManagerServiceDumpProto.PENDING_IDLE_UNTIL, nowElapsed, nowRTC); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2586 | } |
| 2587 | for (Alarm a : mPendingWhileIdleAlarms) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2588 | a.writeToProto(proto, AlarmManagerServiceDumpProto.PENDING_WHILE_IDLE_ALARMS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2589 | nowElapsed, nowRTC); |
| 2590 | } |
| 2591 | if (mNextWakeFromIdle != null) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2592 | mNextWakeFromIdle.writeToProto(proto, AlarmManagerServiceDumpProto.NEXT_WAKE_FROM_IDLE, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2593 | nowElapsed, nowRTC); |
| 2594 | } |
| 2595 | |
| 2596 | for (Alarm a : mPendingNonWakeupAlarms) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2597 | a.writeToProto(proto, AlarmManagerServiceDumpProto.PAST_DUE_NON_WAKEUP_ALARMS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2598 | nowElapsed, nowRTC); |
| 2599 | } |
| 2600 | |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2601 | proto.write(AlarmManagerServiceDumpProto.DELAYED_ALARM_COUNT, mNumDelayedAlarms); |
| 2602 | proto.write(AlarmManagerServiceDumpProto.TOTAL_DELAY_TIME_MS, mTotalDelayTime); |
| 2603 | proto.write(AlarmManagerServiceDumpProto.MAX_DELAY_DURATION_MS, mMaxDelayTime); |
| 2604 | proto.write(AlarmManagerServiceDumpProto.MAX_NON_INTERACTIVE_DURATION_MS, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2605 | mNonInteractiveTime); |
| 2606 | |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2607 | proto.write(AlarmManagerServiceDumpProto.BROADCAST_REF_COUNT, mBroadcastRefCount); |
| 2608 | proto.write(AlarmManagerServiceDumpProto.PENDING_INTENT_SEND_COUNT, mSendCount); |
| 2609 | proto.write(AlarmManagerServiceDumpProto.PENDING_INTENT_FINISH_COUNT, mSendFinishCount); |
| 2610 | proto.write(AlarmManagerServiceDumpProto.LISTENER_SEND_COUNT, mListenerCount); |
| 2611 | proto.write(AlarmManagerServiceDumpProto.LISTENER_FINISH_COUNT, mListenerFinishCount); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2612 | |
| 2613 | for (InFlight f : mInFlight) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2614 | f.writeToProto(proto, AlarmManagerServiceDumpProto.OUTSTANDING_DELIVERIES); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2615 | } |
| 2616 | |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2617 | for (int i = 0; i < mLastAllowWhileIdleDispatch.size(); ++i) { |
| 2618 | final long token = proto.start( |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2619 | AlarmManagerServiceDumpProto.LAST_ALLOW_WHILE_IDLE_DISPATCH_TIMES); |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 2620 | final int uid = mLastAllowWhileIdleDispatch.keyAt(i); |
| 2621 | final long lastTime = mLastAllowWhileIdleDispatch.valueAt(i); |
| 2622 | |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2623 | proto.write(AlarmManagerServiceDumpProto.LastAllowWhileIdleDispatch.UID, uid); |
| 2624 | proto.write(AlarmManagerServiceDumpProto.LastAllowWhileIdleDispatch.TIME_MS, lastTime); |
| 2625 | proto.write(AlarmManagerServiceDumpProto.LastAllowWhileIdleDispatch.NEXT_ALLOWED_MS, |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 2626 | lastTime + getWhileIdleMinIntervalLocked(uid)); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2627 | proto.end(token); |
| 2628 | } |
| 2629 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 2630 | for (int i = 0; i < mUseAllowWhileIdleShortTime.size(); i++) { |
| 2631 | if (mUseAllowWhileIdleShortTime.valueAt(i)) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2632 | proto.write(AlarmManagerServiceDumpProto.USE_ALLOW_WHILE_IDLE_SHORT_TIME, |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 2633 | mUseAllowWhileIdleShortTime.keyAt(i)); |
| 2634 | } |
| 2635 | } |
| 2636 | |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2637 | mLog.writeToProto(proto, AlarmManagerServiceDumpProto.RECENT_PROBLEMS); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2638 | |
| 2639 | final FilterStats[] topFilters = new FilterStats[10]; |
| 2640 | final Comparator<FilterStats> comparator = new Comparator<FilterStats>() { |
| 2641 | @Override |
| 2642 | public int compare(FilterStats lhs, FilterStats rhs) { |
| 2643 | if (lhs.aggregateTime < rhs.aggregateTime) { |
| 2644 | return 1; |
| 2645 | } else if (lhs.aggregateTime > rhs.aggregateTime) { |
| 2646 | return -1; |
| 2647 | } |
| 2648 | return 0; |
| 2649 | } |
| 2650 | }; |
| 2651 | int len = 0; |
| 2652 | // Get the top 10 FilterStats, ordered by aggregateTime. |
| 2653 | for (int iu = 0; iu < mBroadcastStats.size(); ++iu) { |
| 2654 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 2655 | for (int ip = 0; ip < uidStats.size(); ++ip) { |
| 2656 | BroadcastStats bs = uidStats.valueAt(ip); |
| 2657 | for (int is = 0; is < bs.filterStats.size(); ++is) { |
| 2658 | FilterStats fs = bs.filterStats.valueAt(is); |
| 2659 | int pos = len > 0 |
| 2660 | ? Arrays.binarySearch(topFilters, 0, len, fs, comparator) : 0; |
| 2661 | if (pos < 0) { |
| 2662 | pos = -pos - 1; |
| 2663 | } |
| 2664 | if (pos < topFilters.length) { |
| 2665 | int copylen = topFilters.length - pos - 1; |
| 2666 | if (copylen > 0) { |
| 2667 | System.arraycopy(topFilters, pos, topFilters, pos+1, copylen); |
| 2668 | } |
| 2669 | topFilters[pos] = fs; |
| 2670 | if (len < topFilters.length) { |
| 2671 | len++; |
| 2672 | } |
| 2673 | } |
| 2674 | } |
| 2675 | } |
| 2676 | } |
| 2677 | for (int i = 0; i < len; ++i) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2678 | final long token = proto.start(AlarmManagerServiceDumpProto.TOP_ALARMS); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2679 | FilterStats fs = topFilters[i]; |
| 2680 | |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2681 | proto.write(AlarmManagerServiceDumpProto.TopAlarm.UID, fs.mBroadcastStats.mUid); |
| 2682 | proto.write(AlarmManagerServiceDumpProto.TopAlarm.PACKAGE_NAME, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2683 | fs.mBroadcastStats.mPackageName); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2684 | fs.writeToProto(proto, AlarmManagerServiceDumpProto.TopAlarm.FILTER); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2685 | |
| 2686 | proto.end(token); |
| 2687 | } |
| 2688 | |
| 2689 | final ArrayList<FilterStats> tmpFilters = new ArrayList<FilterStats>(); |
| 2690 | for (int iu = 0; iu < mBroadcastStats.size(); ++iu) { |
| 2691 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(iu); |
| 2692 | for (int ip = 0; ip < uidStats.size(); ++ip) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2693 | final long token = proto.start(AlarmManagerServiceDumpProto.ALARM_STATS); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2694 | |
| 2695 | BroadcastStats bs = uidStats.valueAt(ip); |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2696 | bs.writeToProto(proto, AlarmManagerServiceDumpProto.AlarmStat.BROADCAST); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2697 | |
| 2698 | // uidStats is an ArrayMap, which we can't sort. |
| 2699 | tmpFilters.clear(); |
| 2700 | for (int is = 0; is < bs.filterStats.size(); ++is) { |
| 2701 | tmpFilters.add(bs.filterStats.valueAt(is)); |
| 2702 | } |
| 2703 | Collections.sort(tmpFilters, comparator); |
| 2704 | for (FilterStats fs : tmpFilters) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2705 | fs.writeToProto(proto, AlarmManagerServiceDumpProto.AlarmStat.FILTERS); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | proto.end(token); |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 2713 | for (int i = 0; i < mAllowWhileIdleDispatches.size(); i++) { |
| 2714 | IdleDispatchEntry ent = mAllowWhileIdleDispatches.get(i); |
| 2715 | final long token = proto.start( |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2716 | AlarmManagerServiceDumpProto.ALLOW_WHILE_IDLE_DISPATCHES); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2717 | |
| 2718 | proto.write(IdleDispatchEntryProto.UID, ent.uid); |
| 2719 | proto.write(IdleDispatchEntryProto.PKG, ent.pkg); |
| 2720 | proto.write(IdleDispatchEntryProto.TAG, ent.tag); |
| 2721 | proto.write(IdleDispatchEntryProto.OP, ent.op); |
| 2722 | proto.write(IdleDispatchEntryProto.ENTRY_CREATION_REALTIME, |
| 2723 | ent.elapsedRealtime); |
| 2724 | proto.write(IdleDispatchEntryProto.ARG_REALTIME, ent.argRealtime); |
| 2725 | |
| 2726 | proto.end(token); |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | if (WAKEUP_STATS) { |
| 2731 | for (WakeupEvent event : mRecentWakeups) { |
Yi Jin | 2b30f32 | 2018-02-20 15:41:47 -0800 | [diff] [blame] | 2732 | final long token = proto.start(AlarmManagerServiceDumpProto.RECENT_WAKEUP_HISTORY); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 2733 | proto.write(WakeupEventProto.UID, event.uid); |
| 2734 | proto.write(WakeupEventProto.ACTION, event.action); |
| 2735 | proto.write(WakeupEventProto.WHEN, event.when); |
| 2736 | proto.end(token); |
| 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | proto.flush(); |
| 2742 | } |
| 2743 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2744 | private void logBatchesLocked(SimpleDateFormat sdf) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2745 | ByteArrayOutputStream bs = new ByteArrayOutputStream(2048); |
| 2746 | PrintWriter pw = new PrintWriter(bs); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2747 | final long nowRTC = mInjector.getCurrentTimeMillis(); |
| 2748 | final long nowELAPSED = mInjector.getElapsedRealtime(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2749 | final int NZ = mAlarmBatches.size(); |
| 2750 | for (int iz = 0; iz < NZ; iz++) { |
| 2751 | Batch bz = mAlarmBatches.get(iz); |
| 2752 | pw.append("Batch "); pw.print(iz); pw.append(": "); pw.println(bz); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2753 | dumpAlarmList(pw, bz.alarms, " ", nowELAPSED, nowRTC, sdf); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2754 | pw.flush(); |
| 2755 | Slog.v(TAG, bs.toString()); |
| 2756 | bs.reset(); |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | private boolean validateConsistencyLocked() { |
| 2761 | if (DEBUG_VALIDATE) { |
| 2762 | long lastTime = Long.MIN_VALUE; |
| 2763 | final int N = mAlarmBatches.size(); |
| 2764 | for (int i = 0; i < N; i++) { |
| 2765 | Batch b = mAlarmBatches.get(i); |
| 2766 | if (b.start >= lastTime) { |
| 2767 | // duplicate start times are okay because of standalone batches |
| 2768 | lastTime = b.start; |
| 2769 | } else { |
| 2770 | Slog.e(TAG, "CONSISTENCY FAILURE: Batch " + i + " is out of order"); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2771 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 2772 | logBatchesLocked(sdf); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2773 | return false; |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | return true; |
| 2778 | } |
| 2779 | |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 2780 | private Batch findFirstWakeupBatchLocked() { |
| 2781 | final int N = mAlarmBatches.size(); |
| 2782 | for (int i = 0; i < N; i++) { |
| 2783 | Batch b = mAlarmBatches.get(i); |
| 2784 | if (b.hasWakeups()) { |
| 2785 | return b; |
| 2786 | } |
| 2787 | } |
| 2788 | return null; |
| 2789 | } |
| 2790 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 2791 | long getNextWakeFromIdleTimeImpl() { |
| 2792 | synchronized (mLock) { |
| 2793 | return mNextWakeFromIdle != null ? mNextWakeFromIdle.whenElapsed : Long.MAX_VALUE; |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | AlarmManager.AlarmClockInfo getNextAlarmClockImpl(int userId) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2798 | synchronized (mLock) { |
| 2799 | return mNextAlarmClockForUser.get(userId); |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | /** |
| 2804 | * Recomputes the next alarm clock for all users. |
| 2805 | */ |
| 2806 | private void updateNextAlarmClockLocked() { |
| 2807 | if (!mNextAlarmClockMayChange) { |
| 2808 | return; |
| 2809 | } |
| 2810 | mNextAlarmClockMayChange = false; |
| 2811 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 2812 | SparseArray<AlarmManager.AlarmClockInfo> nextForUser = mTmpSparseAlarmClockArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2813 | nextForUser.clear(); |
| 2814 | |
| 2815 | final int N = mAlarmBatches.size(); |
| 2816 | for (int i = 0; i < N; i++) { |
| 2817 | ArrayList<Alarm> alarms = mAlarmBatches.get(i).alarms; |
| 2818 | final int M = alarms.size(); |
| 2819 | |
| 2820 | for (int j = 0; j < M; j++) { |
| 2821 | Alarm a = alarms.get(j); |
| 2822 | if (a.alarmClock != null) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 2823 | final int userId = UserHandle.getUserId(a.uid); |
Christopher Tate | 76389c0 | 2016-08-03 14:57:47 -0700 | [diff] [blame] | 2824 | AlarmManager.AlarmClockInfo current = mNextAlarmClockForUser.get(userId); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2825 | |
| 2826 | if (DEBUG_ALARM_CLOCK) { |
Christopher Tate | 76389c0 | 2016-08-03 14:57:47 -0700 | [diff] [blame] | 2827 | Log.v(TAG, "Found AlarmClockInfo " + a.alarmClock + " at " + |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 2828 | formatNextAlarm(getContext(), a.alarmClock, userId) + |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2829 | " for user " + userId); |
| 2830 | } |
| 2831 | |
| 2832 | // Alarms and batches are sorted by time, no need to compare times here. |
| 2833 | if (nextForUser.get(userId) == null) { |
| 2834 | nextForUser.put(userId, a.alarmClock); |
Christopher Tate | 76389c0 | 2016-08-03 14:57:47 -0700 | [diff] [blame] | 2835 | } else if (a.alarmClock.equals(current) |
| 2836 | && current.getTriggerTime() <= nextForUser.get(userId).getTriggerTime()) { |
| 2837 | // same/earlier time and it's the one we cited before, so stick with it |
| 2838 | nextForUser.put(userId, current); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | // Update mNextAlarmForUser with new values. |
| 2845 | final int NN = nextForUser.size(); |
| 2846 | for (int i = 0; i < NN; i++) { |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 2847 | AlarmManager.AlarmClockInfo newAlarm = nextForUser.valueAt(i); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2848 | int userId = nextForUser.keyAt(i); |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 2849 | AlarmManager.AlarmClockInfo currentAlarm = mNextAlarmClockForUser.get(userId); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2850 | if (!newAlarm.equals(currentAlarm)) { |
| 2851 | updateNextAlarmInfoForUserLocked(userId, newAlarm); |
| 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | // Remove users without any alarm clocks scheduled. |
| 2856 | final int NNN = mNextAlarmClockForUser.size(); |
| 2857 | for (int i = NNN - 1; i >= 0; i--) { |
| 2858 | int userId = mNextAlarmClockForUser.keyAt(i); |
| 2859 | if (nextForUser.get(userId) == null) { |
| 2860 | updateNextAlarmInfoForUserLocked(userId, null); |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 2865 | private void updateNextAlarmInfoForUserLocked(int userId, |
| 2866 | AlarmManager.AlarmClockInfo alarmClock) { |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2867 | if (alarmClock != null) { |
| 2868 | if (DEBUG_ALARM_CLOCK) { |
| 2869 | Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): " + |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 2870 | formatNextAlarm(getContext(), alarmClock, userId)); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2871 | } |
| 2872 | mNextAlarmClockForUser.put(userId, alarmClock); |
| 2873 | } else { |
| 2874 | if (DEBUG_ALARM_CLOCK) { |
| 2875 | Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): None"); |
| 2876 | } |
| 2877 | mNextAlarmClockForUser.remove(userId); |
| 2878 | } |
| 2879 | |
| 2880 | mPendingSendNextAlarmClockChangedForUser.put(userId, true); |
| 2881 | mHandler.removeMessages(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED); |
| 2882 | mHandler.sendEmptyMessage(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED); |
| 2883 | } |
| 2884 | |
| 2885 | /** |
| 2886 | * Updates NEXT_ALARM_FORMATTED and sends NEXT_ALARM_CLOCK_CHANGED_INTENT for all users |
| 2887 | * for which alarm clocks have changed since the last call to this. |
| 2888 | * |
| 2889 | * Do not call with a lock held. Only call from mHandler's thread. |
| 2890 | * |
| 2891 | * @see AlarmHandler#SEND_NEXT_ALARM_CLOCK_CHANGED |
| 2892 | */ |
| 2893 | private void sendNextAlarmClockChanged() { |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 2894 | SparseArray<AlarmManager.AlarmClockInfo> pendingUsers = mHandlerSparseAlarmClockArray; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2895 | pendingUsers.clear(); |
| 2896 | |
| 2897 | synchronized (mLock) { |
| 2898 | final int N = mPendingSendNextAlarmClockChangedForUser.size(); |
| 2899 | for (int i = 0; i < N; i++) { |
| 2900 | int userId = mPendingSendNextAlarmClockChangedForUser.keyAt(i); |
| 2901 | pendingUsers.append(userId, mNextAlarmClockForUser.get(userId)); |
| 2902 | } |
| 2903 | mPendingSendNextAlarmClockChangedForUser.clear(); |
| 2904 | } |
| 2905 | |
| 2906 | final int N = pendingUsers.size(); |
| 2907 | for (int i = 0; i < N; i++) { |
| 2908 | int userId = pendingUsers.keyAt(i); |
Jose Lima | 235510e | 2014-08-13 12:50:01 -0700 | [diff] [blame] | 2909 | AlarmManager.AlarmClockInfo alarmClock = pendingUsers.valueAt(i); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2910 | Settings.System.putStringForUser(getContext().getContentResolver(), |
| 2911 | Settings.System.NEXT_ALARM_FORMATTED, |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 2912 | formatNextAlarm(getContext(), alarmClock, userId), |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2913 | userId); |
| 2914 | |
| 2915 | getContext().sendBroadcastAsUser(NEXT_ALARM_CLOCK_CHANGED_INTENT, |
| 2916 | new UserHandle(userId)); |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | /** |
| 2921 | * Formats an alarm like platform/packages/apps/DeskClock used to. |
| 2922 | */ |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 2923 | private static String formatNextAlarm(final Context context, AlarmManager.AlarmClockInfo info, |
| 2924 | int userId) { |
| 2925 | String skeleton = DateFormat.is24HourFormat(context, userId) ? "EHm" : "Ehma"; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 2926 | String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton); |
| 2927 | return (info == null) ? "" : |
| 2928 | DateFormat.format(pattern, info.getTriggerTime()).toString(); |
| 2929 | } |
| 2930 | |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2931 | /** |
| 2932 | * If the last time AlarmThread woke up precedes any due wakeup or non-wakeup alarm that we set |
| 2933 | * by more than half a minute, log a wtf. |
| 2934 | */ |
| 2935 | private void validateLastAlarmExpiredLocked(long nowElapsed) { |
| 2936 | final StringBuilder errorMsg = new StringBuilder(); |
| 2937 | boolean stuck = false; |
Suprabh Shukla | b50f43f | 2018-06-27 16:40:55 -0700 | [diff] [blame] | 2938 | if (mNextNonWakeup < (nowElapsed - 10_000) && mLastWakeup < mNextNonWakeup) { |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2939 | stuck = true; |
| 2940 | errorMsg.append("[mNextNonWakeup="); |
| 2941 | TimeUtils.formatDuration(mNextNonWakeup - nowElapsed, errorMsg); |
Suprabh Shukla | b50f43f | 2018-06-27 16:40:55 -0700 | [diff] [blame] | 2942 | errorMsg.append(" set at "); |
Suprabh Shukla | 673d34b | 2018-07-03 14:00:29 -0700 | [diff] [blame] | 2943 | TimeUtils.formatDuration(mNextNonWakeUpSetAt - nowElapsed, errorMsg); |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2944 | errorMsg.append(", mLastWakeup="); |
| 2945 | TimeUtils.formatDuration(mLastWakeup - nowElapsed, errorMsg); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2946 | errorMsg.append(", timerfd_gettime=" + mInjector.getNextAlarm(ELAPSED_REALTIME)); |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2947 | errorMsg.append("];"); |
| 2948 | } |
Suprabh Shukla | b50f43f | 2018-06-27 16:40:55 -0700 | [diff] [blame] | 2949 | if (mNextWakeup < (nowElapsed - 10_000) && mLastWakeup < mNextWakeup) { |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2950 | stuck = true; |
| 2951 | errorMsg.append("[mNextWakeup="); |
| 2952 | TimeUtils.formatDuration(mNextWakeup - nowElapsed, errorMsg); |
Suprabh Shukla | b50f43f | 2018-06-27 16:40:55 -0700 | [diff] [blame] | 2953 | errorMsg.append(" set at "); |
Suprabh Shukla | 673d34b | 2018-07-03 14:00:29 -0700 | [diff] [blame] | 2954 | TimeUtils.formatDuration(mNextWakeUpSetAt - nowElapsed, errorMsg); |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2955 | errorMsg.append(", mLastWakeup="); |
| 2956 | TimeUtils.formatDuration(mLastWakeup - nowElapsed, errorMsg); |
| 2957 | errorMsg.append(", timerfd_gettime=" |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2958 | + mInjector.getNextAlarm(ELAPSED_REALTIME_WAKEUP)); |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2959 | errorMsg.append("];"); |
| 2960 | } |
| 2961 | if (stuck) { |
| 2962 | Slog.wtf(TAG, "Alarm delivery stuck: " + errorMsg.toString()); |
| 2963 | } |
| 2964 | } |
| 2965 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2966 | void rescheduleKernelAlarmsLocked() { |
| 2967 | // Schedule the next upcoming wakeup alarm. If there is a deliverable batch |
| 2968 | // prior to that which contains no wakeups, we schedule that as well. |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2969 | final long nowElapsed = mInjector.getElapsedRealtime(); |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2970 | validateLastAlarmExpiredLocked(nowElapsed); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2971 | long nextNonWakeup = 0; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2972 | if (mAlarmBatches.size() > 0) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 2973 | final Batch firstWakeup = findFirstWakeupBatchLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2974 | final Batch firstBatch = mAlarmBatches.get(0); |
Suprabh Shukla | 2fa9545 | 2018-02-12 12:53:23 -0800 | [diff] [blame] | 2975 | if (firstWakeup != null) { |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 2976 | mNextWakeup = firstWakeup.start; |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2977 | mNextWakeUpSetAt = nowElapsed; |
Christopher Tate | 0dab4dc | 2014-12-16 12:14:06 -0800 | [diff] [blame] | 2978 | setLocked(ELAPSED_REALTIME_WAKEUP, firstWakeup.start); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2979 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2980 | if (firstBatch != firstWakeup) { |
| 2981 | nextNonWakeup = firstBatch.start; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2982 | } |
| 2983 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2984 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 2985 | if (nextNonWakeup == 0 || mNextNonWakeupDeliveryTime < nextNonWakeup) { |
| 2986 | nextNonWakeup = mNextNonWakeupDeliveryTime; |
| 2987 | } |
| 2988 | } |
Suprabh Shukla | 2fa9545 | 2018-02-12 12:53:23 -0800 | [diff] [blame] | 2989 | if (nextNonWakeup != 0) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2990 | mNextNonWakeup = nextNonWakeup; |
Suprabh Shukla | 0e4510d | 2018-06-20 17:28:26 -0700 | [diff] [blame] | 2991 | mNextNonWakeUpSetAt = nowElapsed; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 2992 | setLocked(ELAPSED_REALTIME, nextNonWakeup); |
| 2993 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2994 | } |
| 2995 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 2996 | void removeLocked(PendingIntent operation, IAlarmListener directReceiver) { |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 2997 | if (operation == null && directReceiver == null) { |
| 2998 | if (localLOGV) { |
| 2999 | Slog.w(TAG, "requested remove() of null operation", |
| 3000 | new RuntimeException("here")); |
| 3001 | } |
| 3002 | return; |
| 3003 | } |
| 3004 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3005 | boolean didRemove = false; |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3006 | final Predicate<Alarm> whichAlarms = (Alarm a) -> a.matches(operation, directReceiver); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3007 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 3008 | Batch b = mAlarmBatches.get(i); |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3009 | didRemove |= b.remove(whichAlarms); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3010 | if (b.size() == 0) { |
| 3011 | mAlarmBatches.remove(i); |
| 3012 | } |
| 3013 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3014 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3015 | if (mPendingWhileIdleAlarms.get(i).matches(operation, directReceiver)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3016 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 3017 | mPendingWhileIdleAlarms.remove(i); |
| 3018 | } |
| 3019 | } |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3020 | for (int i = mPendingBackgroundAlarms.size() - 1; i >= 0; i--) { |
| 3021 | final ArrayList<Alarm> alarmsForUid = mPendingBackgroundAlarms.valueAt(i); |
| 3022 | for (int j = alarmsForUid.size() - 1; j >= 0; j--) { |
| 3023 | if (alarmsForUid.get(j).matches(operation, directReceiver)) { |
| 3024 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 3025 | alarmsForUid.remove(j); |
| 3026 | } |
| 3027 | } |
| 3028 | if (alarmsForUid.size() == 0) { |
| 3029 | mPendingBackgroundAlarms.removeAt(i); |
| 3030 | } |
| 3031 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3032 | if (didRemove) { |
| 3033 | if (DEBUG_BATCH) { |
| 3034 | Slog.v(TAG, "remove(operation) changed bounds; rebatching"); |
| 3035 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3036 | boolean restorePending = false; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3037 | if (mPendingIdleUntil != null && mPendingIdleUntil.matches(operation, directReceiver)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3038 | mPendingIdleUntil = null; |
| 3039 | restorePending = true; |
| 3040 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3041 | if (mNextWakeFromIdle != null && mNextWakeFromIdle.matches(operation, directReceiver)) { |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 3042 | mNextWakeFromIdle = null; |
| 3043 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3044 | rebatchAllAlarmsLocked(true); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3045 | if (restorePending) { |
| 3046 | restorePendingWhileIdleAlarmsLocked(); |
| 3047 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 3048 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3049 | } |
| 3050 | } |
| 3051 | |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3052 | void removeLocked(final int uid) { |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 3053 | if (uid == Process.SYSTEM_UID) { |
| 3054 | Slog.wtf(TAG, "removeLocked: Shouldn't for UID=" + uid); |
| 3055 | return; |
| 3056 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3057 | boolean didRemove = false; |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3058 | final Predicate<Alarm> whichAlarms = (Alarm a) -> a.uid == uid; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3059 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 3060 | Batch b = mAlarmBatches.get(i); |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3061 | didRemove |= b.remove(whichAlarms); |
| 3062 | if (b.size() == 0) { |
| 3063 | mAlarmBatches.remove(i); |
| 3064 | } |
| 3065 | } |
| 3066 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
| 3067 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
| 3068 | if (a.uid == uid) { |
| 3069 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 3070 | mPendingWhileIdleAlarms.remove(i); |
| 3071 | } |
| 3072 | } |
| 3073 | for (int i = mPendingBackgroundAlarms.size() - 1; i >= 0; i --) { |
| 3074 | final ArrayList<Alarm> alarmsForUid = mPendingBackgroundAlarms.valueAt(i); |
| 3075 | for (int j = alarmsForUid.size() - 1; j >= 0; j--) { |
| 3076 | if (alarmsForUid.get(j).uid == uid) { |
| 3077 | alarmsForUid.remove(j); |
| 3078 | } |
| 3079 | } |
| 3080 | if (alarmsForUid.size() == 0) { |
| 3081 | mPendingBackgroundAlarms.removeAt(i); |
| 3082 | } |
| 3083 | } |
| 3084 | if (didRemove) { |
| 3085 | if (DEBUG_BATCH) { |
| 3086 | Slog.v(TAG, "remove(uid) changed bounds; rebatching"); |
| 3087 | } |
| 3088 | rebatchAllAlarmsLocked(true); |
| 3089 | rescheduleKernelAlarmsLocked(); |
| 3090 | updateNextAlarmClockLocked(); |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | void removeLocked(final String packageName) { |
| 3095 | if (packageName == null) { |
| 3096 | if (localLOGV) { |
| 3097 | Slog.w(TAG, "requested remove() of null packageName", |
| 3098 | new RuntimeException("here")); |
| 3099 | } |
| 3100 | return; |
| 3101 | } |
| 3102 | |
| 3103 | boolean didRemove = false; |
| 3104 | final Predicate<Alarm> whichAlarms = (Alarm a) -> a.matches(packageName); |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 3105 | final boolean oldHasTick = haveBatchesTimeTickAlarm(mAlarmBatches); |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3106 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 3107 | Batch b = mAlarmBatches.get(i); |
| 3108 | didRemove |= b.remove(whichAlarms); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3109 | if (b.size() == 0) { |
| 3110 | mAlarmBatches.remove(i); |
| 3111 | } |
| 3112 | } |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 3113 | final boolean newHasTick = haveBatchesTimeTickAlarm(mAlarmBatches); |
| 3114 | if (oldHasTick != newHasTick) { |
| 3115 | Slog.wtf(TAG, "removeLocked: hasTick changed from " + oldHasTick + " to " + newHasTick); |
| 3116 | } |
| 3117 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3118 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3119 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
| 3120 | if (a.matches(packageName)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3121 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 3122 | mPendingWhileIdleAlarms.remove(i); |
| 3123 | } |
| 3124 | } |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3125 | for (int i = mPendingBackgroundAlarms.size() - 1; i >= 0; i --) { |
| 3126 | final ArrayList<Alarm> alarmsForUid = mPendingBackgroundAlarms.valueAt(i); |
| 3127 | for (int j = alarmsForUid.size() - 1; j >= 0; j--) { |
| 3128 | if (alarmsForUid.get(j).matches(packageName)) { |
| 3129 | alarmsForUid.remove(j); |
| 3130 | } |
| 3131 | } |
| 3132 | if (alarmsForUid.size() == 0) { |
| 3133 | mPendingBackgroundAlarms.removeAt(i); |
| 3134 | } |
| 3135 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3136 | if (didRemove) { |
| 3137 | if (DEBUG_BATCH) { |
| 3138 | Slog.v(TAG, "remove(package) changed bounds; rebatching"); |
| 3139 | } |
| 3140 | rebatchAllAlarmsLocked(true); |
| 3141 | rescheduleKernelAlarmsLocked(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 3142 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3143 | } |
| 3144 | } |
| 3145 | |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3146 | void removeForStoppedLocked(final int uid) { |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 3147 | if (uid == Process.SYSTEM_UID) { |
| 3148 | Slog.wtf(TAG, "removeForStoppedLocked: Shouldn't for UID=" + uid); |
| 3149 | return; |
| 3150 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 3151 | boolean didRemove = false; |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3152 | final Predicate<Alarm> whichAlarms = (Alarm a) -> { |
| 3153 | try { |
| 3154 | if (a.uid == uid && ActivityManager.getService().isAppStartModeDisabled( |
| 3155 | uid, a.packageName)) { |
| 3156 | return true; |
| 3157 | } |
| 3158 | } catch (RemoteException e) { /* fall through */} |
| 3159 | return false; |
| 3160 | }; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 3161 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 3162 | Batch b = mAlarmBatches.get(i); |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3163 | didRemove |= b.remove(whichAlarms); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 3164 | if (b.size() == 0) { |
| 3165 | mAlarmBatches.remove(i); |
| 3166 | } |
| 3167 | } |
| 3168 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
| 3169 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 3170 | if (a.uid == uid) { |
| 3171 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 3172 | mPendingWhileIdleAlarms.remove(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 3173 | } |
| 3174 | } |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3175 | for (int i = mPendingBackgroundAlarms.size() - 1; i >= 0; i--) { |
| 3176 | if (mPendingBackgroundAlarms.keyAt(i) == uid) { |
| 3177 | mPendingBackgroundAlarms.removeAt(i); |
| 3178 | } |
| 3179 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 3180 | if (didRemove) { |
| 3181 | if (DEBUG_BATCH) { |
| 3182 | Slog.v(TAG, "remove(package) changed bounds; rebatching"); |
| 3183 | } |
| 3184 | rebatchAllAlarmsLocked(true); |
| 3185 | rescheduleKernelAlarmsLocked(); |
| 3186 | updateNextAlarmClockLocked(); |
| 3187 | } |
| 3188 | } |
| 3189 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3190 | void removeUserLocked(int userHandle) { |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 3191 | if (userHandle == UserHandle.USER_SYSTEM) { |
| 3192 | Slog.wtf(TAG, "removeForStoppedLocked: Shouldn't for user=" + userHandle); |
| 3193 | return; |
| 3194 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3195 | boolean didRemove = false; |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3196 | final Predicate<Alarm> whichAlarms = |
| 3197 | (Alarm a) -> UserHandle.getUserId(a.creatorUid) == userHandle; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3198 | for (int i = mAlarmBatches.size() - 1; i >= 0; i--) { |
| 3199 | Batch b = mAlarmBatches.get(i); |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 3200 | didRemove |= b.remove(whichAlarms); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3201 | if (b.size() == 0) { |
| 3202 | mAlarmBatches.remove(i); |
| 3203 | } |
| 3204 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3205 | for (int i = mPendingWhileIdleAlarms.size() - 1; i >= 0; i--) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3206 | if (UserHandle.getUserId(mPendingWhileIdleAlarms.get(i).creatorUid) |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3207 | == userHandle) { |
| 3208 | // Don't set didRemove, since this doesn't impact the scheduled alarms. |
| 3209 | mPendingWhileIdleAlarms.remove(i); |
| 3210 | } |
| 3211 | } |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3212 | for (int i = mPendingBackgroundAlarms.size() - 1; i >= 0; i--) { |
| 3213 | if (UserHandle.getUserId(mPendingBackgroundAlarms.keyAt(i)) == userHandle) { |
| 3214 | mPendingBackgroundAlarms.removeAt(i); |
| 3215 | } |
| 3216 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3217 | for (int i = mLastAllowWhileIdleDispatch.size() - 1; i >= 0; i--) { |
| 3218 | if (UserHandle.getUserId(mLastAllowWhileIdleDispatch.keyAt(i)) == userHandle) { |
| 3219 | mLastAllowWhileIdleDispatch.removeAt(i); |
| 3220 | } |
| 3221 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3222 | |
| 3223 | if (didRemove) { |
| 3224 | if (DEBUG_BATCH) { |
| 3225 | Slog.v(TAG, "remove(user) changed bounds; rebatching"); |
| 3226 | } |
| 3227 | rebatchAllAlarmsLocked(true); |
| 3228 | rescheduleKernelAlarmsLocked(); |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 3229 | updateNextAlarmClockLocked(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3230 | } |
| 3231 | } |
| 3232 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3233 | void interactiveStateChangedLocked(boolean interactive) { |
| 3234 | if (mInteractive != interactive) { |
| 3235 | mInteractive = interactive; |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3236 | final long nowELAPSED = mInjector.getElapsedRealtime(); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3237 | if (interactive) { |
| 3238 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 3239 | final long thisDelayTime = nowELAPSED - mStartCurrentDelayTime; |
| 3240 | mTotalDelayTime += thisDelayTime; |
| 3241 | if (mMaxDelayTime < thisDelayTime) { |
| 3242 | mMaxDelayTime = thisDelayTime; |
| 3243 | } |
| 3244 | deliverAlarmsLocked(mPendingNonWakeupAlarms, nowELAPSED); |
| 3245 | mPendingNonWakeupAlarms.clear(); |
| 3246 | } |
| 3247 | if (mNonInteractiveStartTime > 0) { |
| 3248 | long dur = nowELAPSED - mNonInteractiveStartTime; |
| 3249 | if (dur > mNonInteractiveTime) { |
| 3250 | mNonInteractiveTime = dur; |
| 3251 | } |
| 3252 | } |
Suprabh Shukla | 12bd016 | 2018-11-12 18:00:18 -0800 | [diff] [blame] | 3253 | // And send a TIME_TICK right now, since it is important to get the UI updated. |
| 3254 | mHandler.post(() -> |
| 3255 | getContext().sendBroadcastAsUser(mTimeTickIntent, UserHandle.ALL)); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3256 | } else { |
| 3257 | mNonInteractiveStartTime = nowELAPSED; |
| 3258 | } |
| 3259 | } |
| 3260 | } |
| 3261 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3262 | boolean lookForPackageLocked(String packageName) { |
| 3263 | for (int i = 0; i < mAlarmBatches.size(); i++) { |
| 3264 | Batch b = mAlarmBatches.get(i); |
| 3265 | if (b.hasPackage(packageName)) { |
| 3266 | return true; |
| 3267 | } |
| 3268 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3269 | for (int i = 0; i < mPendingWhileIdleAlarms.size(); i++) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3270 | final Alarm a = mPendingWhileIdleAlarms.get(i); |
| 3271 | if (a.matches(packageName)) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3272 | return true; |
| 3273 | } |
| 3274 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3275 | return false; |
| 3276 | } |
| 3277 | |
| 3278 | private void setLocked(int type, long when) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3279 | if (mInjector.isAlarmDriverPresent()) { |
| 3280 | mInjector.setAlarm(type, when); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3281 | } else { |
| 3282 | Message msg = Message.obtain(); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3283 | msg.what = AlarmHandler.ALARM_EVENT; |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3284 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3285 | mHandler.removeMessages(msg.what); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3286 | mHandler.sendMessageAtTime(msg, when); |
| 3287 | } |
| 3288 | } |
| 3289 | |
Dianne Hackborn | 043fcd9 | 2010-10-06 14:27:34 -0700 | [diff] [blame] | 3290 | private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3291 | 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] | 3292 | for (int i=list.size()-1; i>=0; i--) { |
| 3293 | Alarm a = list.get(i); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 3294 | pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i); |
| 3295 | pw.print(": "); pw.println(a); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3296 | a.dump(pw, prefix + " ", nowELAPSED, nowRTC, sdf); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3297 | } |
| 3298 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3299 | |
| 3300 | private static final String labelForType(int type) { |
| 3301 | switch (type) { |
| 3302 | case RTC: return "RTC"; |
| 3303 | case RTC_WAKEUP : return "RTC_WAKEUP"; |
| 3304 | case ELAPSED_REALTIME : return "ELAPSED"; |
| 3305 | case ELAPSED_REALTIME_WAKEUP: return "ELAPSED_WAKEUP"; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3306 | } |
| 3307 | return "--unknown--"; |
| 3308 | } |
| 3309 | |
| 3310 | private static final void dumpAlarmList(PrintWriter pw, ArrayList<Alarm> list, |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3311 | String prefix, long nowELAPSED, long nowRTC, SimpleDateFormat sdf) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3312 | for (int i=list.size()-1; i>=0; i--) { |
| 3313 | Alarm a = list.get(i); |
| 3314 | final String label = labelForType(a.type); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3315 | pw.print(prefix); pw.print(label); pw.print(" #"); pw.print(i); |
| 3316 | pw.print(": "); pw.println(a); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3317 | a.dump(pw, prefix + " ", nowELAPSED, nowRTC, sdf); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3318 | } |
| 3319 | } |
| 3320 | |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3321 | private boolean isBackgroundRestricted(Alarm alarm) { |
Christopher Tate | da3dc92 | 2018-05-09 13:49:41 -0700 | [diff] [blame] | 3322 | boolean exemptOnBatterySaver = (alarm.flags & FLAG_ALLOW_WHILE_IDLE) != 0; |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3323 | if (alarm.alarmClock != null) { |
Christopher Tate | da3dc92 | 2018-05-09 13:49:41 -0700 | [diff] [blame] | 3324 | // Don't defer alarm clocks |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3325 | return false; |
| 3326 | } |
Christopher Tate | da3dc92 | 2018-05-09 13:49:41 -0700 | [diff] [blame] | 3327 | if (alarm.operation != null) { |
| 3328 | if (alarm.operation.isActivity()) { |
| 3329 | // Don't defer starting actual UI |
| 3330 | return false; |
| 3331 | } |
| 3332 | if (alarm.operation.isForegroundService()) { |
| 3333 | // FG service alarms are nearly as important; consult AST policy |
| 3334 | exemptOnBatterySaver = true; |
| 3335 | } |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3336 | } |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 3337 | final String sourcePackage = alarm.sourcePackage; |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3338 | final int sourceUid = alarm.creatorUid; |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 3339 | return (mAppStateTracker != null) && |
Christopher Tate | da3dc92 | 2018-05-09 13:49:41 -0700 | [diff] [blame] | 3340 | mAppStateTracker.areAlarmsRestricted(sourceUid, sourcePackage, |
| 3341 | exemptOnBatterySaver); |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3342 | } |
| 3343 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3344 | private static native long init(); |
| 3345 | private static native void close(long nativeData); |
| 3346 | private static native int set(long nativeData, int type, long seconds, long nanoseconds); |
| 3347 | private static native int waitForAlarm(long nativeData); |
| 3348 | private static native int setKernelTime(long nativeData, long millis); |
| 3349 | private static native int setKernelTimezone(long nativeData, int minuteswest); |
| 3350 | private static native long getNextAlarm(long nativeData, int type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3351 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 3352 | private long getWhileIdleMinIntervalLocked(int uid) { |
| 3353 | final boolean dozing = mPendingIdleUntil != null; |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 3354 | final boolean ebs = (mAppStateTracker != null) |
| 3355 | && mAppStateTracker.isForceAllAppsStandbyEnabled(); |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 3356 | if (!dozing && !ebs) { |
| 3357 | return mConstants.ALLOW_WHILE_IDLE_SHORT_TIME; |
| 3358 | } |
| 3359 | if (dozing) { |
| 3360 | return mConstants.ALLOW_WHILE_IDLE_LONG_TIME; |
| 3361 | } |
| 3362 | if (mUseAllowWhileIdleShortTime.get(uid)) { |
| 3363 | // if the last allow-while-idle went off while uid was fg, or the uid |
| 3364 | // recently came into fg, don't block the alarm for long. |
| 3365 | return mConstants.ALLOW_WHILE_IDLE_SHORT_TIME; |
| 3366 | } |
| 3367 | return mConstants.ALLOW_WHILE_IDLE_LONG_TIME; |
| 3368 | } |
| 3369 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3370 | boolean triggerAlarmsLocked(ArrayList<Alarm> triggerList, final long nowELAPSED, |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 3371 | final long nowRTC) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3372 | boolean hasWakeup = false; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3373 | // batches are temporally sorted, so we need only pull from the |
| 3374 | // start of the list until we either empty it or hit a batch |
| 3375 | // that is not yet deliverable |
Christopher Tate | 6578ad1 | 2013-09-24 17:12:46 -0700 | [diff] [blame] | 3376 | while (mAlarmBatches.size() > 0) { |
| 3377 | Batch batch = mAlarmBatches.get(0); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3378 | if (batch.start > nowELAPSED) { |
| 3379 | // Everything else is scheduled for the future |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3380 | break; |
| 3381 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3382 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3383 | // We will (re)schedule some alarms now; don't let that interfere |
| 3384 | // with delivery of this current batch |
| 3385 | mAlarmBatches.remove(0); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 3386 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3387 | final int N = batch.size(); |
| 3388 | for (int i = 0; i < N; i++) { |
| 3389 | Alarm alarm = batch.get(i); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3390 | |
| 3391 | if ((alarm.flags&AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0) { |
| 3392 | // If this is an ALLOW_WHILE_IDLE alarm, we constrain how frequently the app can |
Christopher Tate | ddd1da14 | 2018-04-13 13:41:51 -0700 | [diff] [blame] | 3393 | // schedule such alarms. The first such alarm from an app is always delivered. |
| 3394 | final long lastTime = mLastAllowWhileIdleDispatch.get(alarm.creatorUid, -1); |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 3395 | final long minTime = lastTime + getWhileIdleMinIntervalLocked(alarm.creatorUid); |
Christopher Tate | ddd1da14 | 2018-04-13 13:41:51 -0700 | [diff] [blame] | 3396 | if (lastTime >= 0 && nowELAPSED < minTime) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3397 | // Whoops, it hasn't been long enough since the last ALLOW_WHILE_IDLE |
| 3398 | // alarm went off for this app. Reschedule the alarm to be in the |
| 3399 | // correct time period. |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 3400 | alarm.expectedWhenElapsed = alarm.whenElapsed = minTime; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3401 | if (alarm.maxWhenElapsed < minTime) { |
| 3402 | alarm.maxWhenElapsed = minTime; |
| 3403 | } |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 3404 | alarm.expectedMaxWhenElapsed = alarm.maxWhenElapsed; |
Dianne Hackborn | ae78bf8 | 2015-10-26 13:33:20 -0700 | [diff] [blame] | 3405 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 3406 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 3407 | ent.uid = alarm.uid; |
| 3408 | ent.pkg = alarm.operation.getCreatorPackage(); |
| 3409 | ent.tag = alarm.operation.getTag(""); |
| 3410 | ent.op = "RESCHEDULE"; |
| 3411 | ent.elapsedRealtime = nowELAPSED; |
| 3412 | ent.argRealtime = lastTime; |
| 3413 | mAllowWhileIdleDispatches.add(ent); |
| 3414 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3415 | setImplLocked(alarm, true, false); |
| 3416 | continue; |
| 3417 | } |
| 3418 | } |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 3419 | if (isBackgroundRestricted(alarm)) { |
| 3420 | // Alarms with FLAG_WAKE_FROM_IDLE or mPendingIdleUntil alarm are not deferred |
| 3421 | if (DEBUG_BG_LIMIT) { |
| 3422 | Slog.d(TAG, "Deferring alarm " + alarm + " due to user forced app standby"); |
| 3423 | } |
| 3424 | ArrayList<Alarm> alarmsForUid = mPendingBackgroundAlarms.get(alarm.creatorUid); |
| 3425 | if (alarmsForUid == null) { |
| 3426 | alarmsForUid = new ArrayList<>(); |
| 3427 | mPendingBackgroundAlarms.put(alarm.creatorUid, alarmsForUid); |
| 3428 | } |
| 3429 | alarmsForUid.add(alarm); |
| 3430 | continue; |
| 3431 | } |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3432 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3433 | alarm.count = 1; |
| 3434 | triggerList.add(alarm); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 3435 | if ((alarm.flags&AlarmManager.FLAG_WAKE_FROM_IDLE) != 0) { |
| 3436 | EventLogTags.writeDeviceIdleWakeFromIdle(mPendingIdleUntil != null ? 1 : 0, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3437 | alarm.statsTag); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 3438 | } |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3439 | if (mPendingIdleUntil == alarm) { |
| 3440 | mPendingIdleUntil = null; |
| 3441 | rebatchAllAlarmsLocked(false); |
| 3442 | restorePendingWhileIdleAlarmsLocked(); |
| 3443 | } |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 3444 | if (mNextWakeFromIdle == alarm) { |
| 3445 | mNextWakeFromIdle = null; |
| 3446 | rebatchAllAlarmsLocked(false); |
| 3447 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3448 | |
| 3449 | // Recurring alarms may have passed several alarm intervals while the |
| 3450 | // phone was asleep or off, so pass a trigger count when sending them. |
| 3451 | if (alarm.repeatInterval > 0) { |
| 3452 | // this adjustment will be zero if we're late by |
| 3453 | // less than one full repeat interval |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 3454 | alarm.count += (nowELAPSED - alarm.expectedWhenElapsed) / alarm.repeatInterval; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3455 | |
| 3456 | // Also schedule its next recurrence |
| 3457 | final long delta = alarm.count * alarm.repeatInterval; |
| 3458 | final long nextElapsed = alarm.whenElapsed + delta; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 3459 | setImplLocked(alarm.type, alarm.when + delta, nextElapsed, alarm.windowLength, |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3460 | maxTriggerTime(nowELAPSED, nextElapsed, alarm.repeatInterval), |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3461 | alarm.repeatInterval, alarm.operation, null, null, alarm.flags, true, |
| 3462 | alarm.workSource, alarm.alarmClock, alarm.uid, alarm.packageName); |
Christopher Tate | 864d42e | 2014-12-02 11:48:53 -0800 | [diff] [blame] | 3463 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3464 | |
Christopher Tate | 864d42e | 2014-12-02 11:48:53 -0800 | [diff] [blame] | 3465 | if (alarm.wakeup) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3466 | hasWakeup = true; |
| 3467 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 3468 | |
| 3469 | // We removed an alarm clock. Let the caller recompute the next alarm clock. |
| 3470 | if (alarm.alarmClock != null) { |
| 3471 | mNextAlarmClockMayChange = true; |
| 3472 | } |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 3473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3474 | } |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 3475 | |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 3476 | // This is a new alarm delivery set; bump the sequence number to indicate that |
| 3477 | // all apps' alarm delivery classes should be recalculated. |
| 3478 | mCurrentSeq++; |
| 3479 | calculateDeliveryPriorities(triggerList); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 3480 | Collections.sort(triggerList, mAlarmDispatchComparator); |
| 3481 | |
| 3482 | if (localLOGV) { |
| 3483 | for (int i=0; i<triggerList.size(); i++) { |
| 3484 | Slog.v(TAG, "Triggering alarm #" + i + ": " + triggerList.get(i)); |
| 3485 | } |
| 3486 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3487 | |
| 3488 | return hasWakeup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3489 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3491 | /** |
| 3492 | * This Comparator sorts Alarms into increasing time order. |
| 3493 | */ |
| 3494 | public static class IncreasingTimeOrder implements Comparator<Alarm> { |
| 3495 | public int compare(Alarm a1, Alarm a2) { |
jinho.park | 1acd32a | 2015-05-27 14:44:18 +0900 | [diff] [blame] | 3496 | long when1 = a1.whenElapsed; |
| 3497 | long when2 = a2.whenElapsed; |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 3498 | if (when1 > when2) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3499 | return 1; |
| 3500 | } |
Charles Tsai | cdaaeee | 2015-05-20 18:16:48 +0800 | [diff] [blame] | 3501 | if (when1 < when2) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3502 | return -1; |
| 3503 | } |
| 3504 | return 0; |
| 3505 | } |
| 3506 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3507 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 3508 | @VisibleForTesting |
| 3509 | static class Alarm { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 3510 | public final int type; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3511 | public final long origWhen; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 3512 | public final boolean wakeup; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3513 | public final PendingIntent operation; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3514 | public final IAlarmListener listener; |
| 3515 | public final String listenerTag; |
| 3516 | public final String statsTag; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3517 | public final WorkSource workSource; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3518 | public final int flags; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3519 | public final AlarmManager.AlarmClockInfo alarmClock; |
| 3520 | public final int uid; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3521 | public final int creatorUid; |
| 3522 | public final String packageName; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 3523 | public final String sourcePackage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3524 | public int count; |
| 3525 | public long when; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 3526 | public long windowLength; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3527 | public long whenElapsed; // 'when' in the elapsed time base |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3528 | public long maxWhenElapsed; // also in the elapsed time base |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 3529 | // Expected alarm expiry time before app standby deferring is applied. |
| 3530 | public long expectedWhenElapsed; |
| 3531 | public long expectedMaxWhenElapsed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3532 | public long repeatInterval; |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 3533 | public PriorityClass priorityClass; |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3534 | |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 3535 | public Alarm(int _type, long _when, long _whenElapsed, long _windowLength, long _maxWhen, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3536 | long _interval, PendingIntent _op, IAlarmListener _rec, String _listenerTag, |
| 3537 | WorkSource _ws, int _flags, AlarmManager.AlarmClockInfo _info, |
| 3538 | int _uid, String _pkgName) { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3539 | type = _type; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3540 | origWhen = _when; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 3541 | wakeup = _type == AlarmManager.ELAPSED_REALTIME_WAKEUP |
| 3542 | || _type == AlarmManager.RTC_WAKEUP; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3543 | when = _when; |
| 3544 | whenElapsed = _whenElapsed; |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 3545 | expectedWhenElapsed = _whenElapsed; |
Christopher Tate | 3e04b47 | 2013-10-21 17:51:31 -0700 | [diff] [blame] | 3546 | windowLength = _windowLength; |
Christopher Tate | 22e919d | 2018-02-16 16:16:50 -0800 | [diff] [blame] | 3547 | maxWhenElapsed = expectedMaxWhenElapsed = clampPositive(_maxWhen); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3548 | repeatInterval = _interval; |
| 3549 | operation = _op; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3550 | listener = _rec; |
| 3551 | listenerTag = _listenerTag; |
| 3552 | statsTag = makeTag(_op, _listenerTag, _type); |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 3553 | workSource = _ws; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3554 | flags = _flags; |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 3555 | alarmClock = _info; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3556 | uid = _uid; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3557 | packageName = _pkgName; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 3558 | sourcePackage = (operation != null) ? operation.getCreatorPackage() : packageName; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3559 | creatorUid = (operation != null) ? operation.getCreatorUid() : uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3560 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3561 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3562 | public static String makeTag(PendingIntent pi, String tag, int type) { |
| 3563 | final String alarmString = type == ELAPSED_REALTIME_WAKEUP || type == RTC_WAKEUP |
| 3564 | ? "*walarm*:" : "*alarm*:"; |
| 3565 | return (pi != null) ? pi.getTag(alarmString) : (alarmString + tag); |
| 3566 | } |
| 3567 | |
| 3568 | public WakeupEvent makeWakeupEvent(long nowRTC) { |
| 3569 | return new WakeupEvent(nowRTC, creatorUid, |
| 3570 | (operation != null) |
| 3571 | ? operation.getIntent().getAction() |
| 3572 | : ("<listener>:" + listenerTag)); |
| 3573 | } |
| 3574 | |
| 3575 | // Returns true if either matches |
| 3576 | public boolean matches(PendingIntent pi, IAlarmListener rec) { |
| 3577 | return (operation != null) |
| 3578 | ? operation.equals(pi) |
Lorenzo Colitti | 9b43ce0 | 2015-11-22 22:00:08 +0900 | [diff] [blame] | 3579 | : rec != null && listener.asBinder().equals(rec.asBinder()); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | public boolean matches(String packageName) { |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 3583 | return packageName.equals(sourcePackage); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3584 | } |
| 3585 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3586 | @Override |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3587 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 3588 | StringBuilder sb = new StringBuilder(128); |
| 3589 | sb.append("Alarm{"); |
| 3590 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 3591 | sb.append(" type "); |
| 3592 | sb.append(type); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 3593 | sb.append(" when "); |
| 3594 | sb.append(when); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 3595 | sb.append(" "); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 3596 | sb.append(sourcePackage); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 3597 | sb.append('}'); |
| 3598 | return sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3599 | } |
| 3600 | |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3601 | public void dump(PrintWriter pw, String prefix, long nowELAPSED, long nowRTC, |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3602 | SimpleDateFormat sdf) { |
| 3603 | final boolean isRtc = (type == RTC || type == RTC_WAKEUP); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3604 | pw.print(prefix); pw.print("tag="); pw.println(statsTag); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 3605 | pw.print(prefix); pw.print("type="); pw.print(type); |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 3606 | pw.print(" expectedWhenElapsed="); TimeUtils.formatDuration( |
| 3607 | expectedWhenElapsed, nowELAPSED, pw); |
| 3608 | pw.print(" expectedMaxWhenElapsed="); TimeUtils.formatDuration( |
| 3609 | expectedMaxWhenElapsed, nowELAPSED, pw); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3610 | pw.print(" whenElapsed="); TimeUtils.formatDuration(whenElapsed, |
| 3611 | nowELAPSED, pw); |
Suprabh Shukla | 2fa9545 | 2018-02-12 12:53:23 -0800 | [diff] [blame] | 3612 | pw.print(" maxWhenElapsed="); TimeUtils.formatDuration(maxWhenElapsed, |
| 3613 | nowELAPSED, pw); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3614 | pw.print(" when="); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3615 | if (isRtc) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3616 | pw.print(sdf.format(new Date(when))); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3617 | } else { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3618 | TimeUtils.formatDuration(when, nowELAPSED, pw); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3619 | } |
| 3620 | pw.println(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 3621 | pw.print(prefix); pw.print("window="); TimeUtils.formatDuration(windowLength, pw); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 3622 | pw.print(" repeatInterval="); pw.print(repeatInterval); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3623 | pw.print(" count="); pw.print(count); |
| 3624 | pw.print(" flags=0x"); pw.println(Integer.toHexString(flags)); |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 3625 | if (alarmClock != null) { |
| 3626 | pw.print(prefix); pw.println("Alarm clock:"); |
| 3627 | pw.print(prefix); pw.print(" triggerTime="); |
| 3628 | pw.println(sdf.format(new Date(alarmClock.getTriggerTime()))); |
| 3629 | pw.print(prefix); pw.print(" showIntent="); pw.println(alarmClock.getShowIntent()); |
| 3630 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 3631 | pw.print(prefix); pw.print("operation="); pw.println(operation); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3632 | if (listener != null) { |
| 3633 | pw.print(prefix); pw.print("listener="); pw.println(listener.asBinder()); |
| 3634 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3635 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3636 | |
| 3637 | public void writeToProto(ProtoOutputStream proto, long fieldId, long nowElapsed, |
| 3638 | long nowRTC) { |
| 3639 | final long token = proto.start(fieldId); |
| 3640 | |
| 3641 | proto.write(AlarmProto.TAG, statsTag); |
| 3642 | proto.write(AlarmProto.TYPE, type); |
Kweku Adams | 923ec43 | 2017-12-11 18:05:38 -0800 | [diff] [blame] | 3643 | proto.write(AlarmProto.TIME_UNTIL_WHEN_ELAPSED_MS, whenElapsed - nowElapsed); |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3644 | proto.write(AlarmProto.WINDOW_LENGTH_MS, windowLength); |
| 3645 | proto.write(AlarmProto.REPEAT_INTERVAL_MS, repeatInterval); |
| 3646 | proto.write(AlarmProto.COUNT, count); |
| 3647 | proto.write(AlarmProto.FLAGS, flags); |
| 3648 | if (alarmClock != null) { |
| 3649 | alarmClock.writeToProto(proto, AlarmProto.ALARM_CLOCK); |
| 3650 | } |
| 3651 | if (operation != null) { |
| 3652 | operation.writeToProto(proto, AlarmProto.OPERATION); |
| 3653 | } |
| 3654 | if (listener != null) { |
| 3655 | proto.write(AlarmProto.LISTENER, listener.asBinder().toString()); |
| 3656 | } |
| 3657 | |
| 3658 | proto.end(token); |
| 3659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3660 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 3661 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3662 | void recordWakeupAlarms(ArrayList<Batch> batches, long nowELAPSED, long nowRTC) { |
| 3663 | final int numBatches = batches.size(); |
Christopher Tate | e982faf | 2013-07-19 14:51:44 -0700 | [diff] [blame] | 3664 | for (int nextBatch = 0; nextBatch < numBatches; nextBatch++) { |
| 3665 | Batch b = batches.get(nextBatch); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3666 | if (b.start > nowELAPSED) { |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 3667 | break; |
| 3668 | } |
| 3669 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3670 | final int numAlarms = b.alarms.size(); |
Christopher Tate | e982faf | 2013-07-19 14:51:44 -0700 | [diff] [blame] | 3671 | for (int nextAlarm = 0; nextAlarm < numAlarms; nextAlarm++) { |
| 3672 | Alarm a = b.alarms.get(nextAlarm); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3673 | mRecentWakeups.add(a.makeWakeupEvent(nowRTC)); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 3674 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 3675 | } |
| 3676 | } |
| 3677 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3678 | long currentNonWakeupFuzzLocked(long nowELAPSED) { |
| 3679 | long timeSinceOn = nowELAPSED - mNonInteractiveStartTime; |
| 3680 | if (timeSinceOn < 5*60*1000) { |
| 3681 | // If the screen has been off for 5 minutes, only delay by at most two minutes. |
| 3682 | return 2*60*1000; |
| 3683 | } else if (timeSinceOn < 30*60*1000) { |
| 3684 | // If the screen has been off for 30 minutes, only delay by at most 15 minutes. |
| 3685 | return 15*60*1000; |
| 3686 | } else { |
| 3687 | // Otherwise, we will delay by at most an hour. |
| 3688 | return 60*60*1000; |
| 3689 | } |
| 3690 | } |
| 3691 | |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 3692 | static int fuzzForDuration(long duration) { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3693 | if (duration < 15*60*1000) { |
| 3694 | // If the duration until the time is less than 15 minutes, the maximum fuzz |
| 3695 | // is the duration. |
Dianne Hackborn | f70faed | 2015-04-21 14:11:38 -0700 | [diff] [blame] | 3696 | return (int)duration; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 3697 | } else if (duration < 90*60*1000) { |
| 3698 | // If duration is less than 1 1/2 hours, the maximum fuzz is 15 minutes, |
| 3699 | return 15*60*1000; |
| 3700 | } else { |
| 3701 | // Otherwise, we will fuzz by at most half an hour. |
| 3702 | return 30*60*1000; |
| 3703 | } |
| 3704 | } |
| 3705 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3706 | boolean checkAllowNonWakeupDelayLocked(long nowELAPSED) { |
| 3707 | if (mInteractive) { |
| 3708 | return false; |
| 3709 | } |
| 3710 | if (mLastAlarmDeliveryTime <= 0) { |
| 3711 | return false; |
| 3712 | } |
minho.choo | 649acab | 2014-12-12 16:13:55 +0900 | [diff] [blame] | 3713 | if (mPendingNonWakeupAlarms.size() > 0 && mNextNonWakeupDeliveryTime < nowELAPSED) { |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3714 | // This is just a little paranoia, if somehow we have pending non-wakeup alarms |
| 3715 | // and the next delivery time is in the past, then just deliver them all. This |
| 3716 | // avoids bugs where we get stuck in a loop trying to poll for alarms. |
| 3717 | return false; |
| 3718 | } |
| 3719 | long timeSinceLast = nowELAPSED - mLastAlarmDeliveryTime; |
| 3720 | return timeSinceLast <= currentNonWakeupFuzzLocked(nowELAPSED); |
| 3721 | } |
| 3722 | |
| 3723 | void deliverAlarmsLocked(ArrayList<Alarm> triggerList, long nowELAPSED) { |
| 3724 | mLastAlarmDeliveryTime = nowELAPSED; |
| 3725 | for (int i=0; i<triggerList.size(); i++) { |
| 3726 | Alarm alarm = triggerList.get(i); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 3727 | final boolean allowWhileIdle = (alarm.flags&AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0; |
Tim Murray | 175c0f9 | 2017-11-28 15:01:04 -0800 | [diff] [blame] | 3728 | if (alarm.wakeup) { |
| 3729 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "Dispatch wakeup alarm to " + alarm.packageName); |
| 3730 | } else { |
| 3731 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "Dispatch non-wakeup alarm to " + alarm.packageName); |
| 3732 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3733 | try { |
Christopher Tate | 2ff5a73 | 2014-09-18 13:47:57 -0700 | [diff] [blame] | 3734 | if (localLOGV) { |
| 3735 | Slog.v(TAG, "sending alarm " + alarm); |
| 3736 | } |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 3737 | if (RECORD_ALARMS_IN_HISTORY) { |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 3738 | ActivityManager.noteAlarmStart(alarm.operation, alarm.workSource, alarm.uid, |
| 3739 | alarm.statsTag); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 3740 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 3741 | mDeliveryTracker.deliverLocked(alarm, nowELAPSED, allowWhileIdle); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3742 | } catch (RuntimeException e) { |
| 3743 | Slog.w(TAG, "Failure sending alarm.", e); |
| 3744 | } |
Tim Murray | 175c0f9 | 2017-11-28 15:01:04 -0800 | [diff] [blame] | 3745 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3746 | } |
| 3747 | } |
| 3748 | |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 3749 | private boolean isExemptFromAppStandby(Alarm a) { |
| 3750 | return a.alarmClock != null || UserHandle.isCore(a.creatorUid) |
| 3751 | || (a.flags & FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED) != 0; |
| 3752 | } |
| 3753 | |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3754 | @VisibleForTesting |
| 3755 | static class Injector { |
| 3756 | private long mNativeData; |
| 3757 | private Context mContext; |
| 3758 | |
| 3759 | Injector(Context context) { |
| 3760 | mContext = context; |
| 3761 | } |
| 3762 | |
| 3763 | void init() { |
| 3764 | mNativeData = AlarmManagerService.init(); |
| 3765 | } |
| 3766 | |
| 3767 | int waitForAlarm() { |
| 3768 | return AlarmManagerService.waitForAlarm(mNativeData); |
| 3769 | } |
| 3770 | |
| 3771 | boolean isAlarmDriverPresent() { |
| 3772 | return mNativeData != 0; |
| 3773 | } |
| 3774 | |
| 3775 | void setAlarm(int type, long millis) { |
| 3776 | // The kernel never triggers alarms with negative wakeup times |
| 3777 | // so we ensure they are positive. |
| 3778 | final long alarmSeconds, alarmNanoseconds; |
| 3779 | if (millis < 0) { |
| 3780 | alarmSeconds = 0; |
| 3781 | alarmNanoseconds = 0; |
| 3782 | } else { |
| 3783 | alarmSeconds = millis / 1000; |
| 3784 | alarmNanoseconds = (millis % 1000) * 1000 * 1000; |
| 3785 | } |
| 3786 | |
| 3787 | final int result = AlarmManagerService.set(mNativeData, type, alarmSeconds, |
| 3788 | alarmNanoseconds); |
| 3789 | if (result != 0) { |
| 3790 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 3791 | Slog.wtf(TAG, "Unable to set kernel alarm, now=" + nowElapsed |
| 3792 | + " type=" + type + " @ (" + alarmSeconds + "," + alarmNanoseconds |
| 3793 | + "), ret = " + result + " = " + Os.strerror(result)); |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | long getNextAlarm(int type) { |
| 3798 | return AlarmManagerService.getNextAlarm(mNativeData, type); |
| 3799 | } |
| 3800 | |
| 3801 | void setKernelTimezone(int minutesWest) { |
| 3802 | AlarmManagerService.setKernelTimezone(mNativeData, minutesWest); |
| 3803 | } |
| 3804 | |
| 3805 | void setKernelTime(long millis) { |
| 3806 | if (mNativeData != 0) { |
| 3807 | AlarmManagerService.setKernelTime(mNativeData, millis); |
| 3808 | } |
| 3809 | } |
| 3810 | |
| 3811 | void close() { |
| 3812 | AlarmManagerService.close(mNativeData); |
| 3813 | } |
| 3814 | |
| 3815 | long getElapsedRealtime() { |
| 3816 | return SystemClock.elapsedRealtime(); |
| 3817 | } |
| 3818 | |
| 3819 | long getCurrentTimeMillis() { |
| 3820 | return System.currentTimeMillis(); |
| 3821 | } |
| 3822 | |
| 3823 | PowerManager.WakeLock getAlarmWakeLock() { |
| 3824 | final PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 3825 | return pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*alarm*"); |
| 3826 | } |
| 3827 | |
| 3828 | int getSystemUiUid() { |
| 3829 | int sysUiUid = -1; |
| 3830 | final PackageManager pm = mContext.getPackageManager(); |
| 3831 | try { |
| 3832 | PermissionInfo sysUiPerm = pm.getPermissionInfo(SYSTEM_UI_SELF_PERMISSION, 0); |
| 3833 | ApplicationInfo sysUi = pm.getApplicationInfo(sysUiPerm.packageName, 0); |
| 3834 | if ((sysUi.privateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 3835 | sysUiUid = sysUi.uid; |
| 3836 | } else { |
| 3837 | Slog.e(TAG, "SysUI permission " + SYSTEM_UI_SELF_PERMISSION |
| 3838 | + " defined by non-privileged app " + sysUi.packageName |
| 3839 | + " - ignoring"); |
| 3840 | } |
| 3841 | } catch (NameNotFoundException e) { |
| 3842 | } |
| 3843 | return sysUiUid; |
| 3844 | } |
| 3845 | |
| 3846 | ClockReceiver getClockReceiver(AlarmManagerService service) { |
| 3847 | return service.new ClockReceiver(); |
| 3848 | } |
| 3849 | } |
| 3850 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3851 | private class AlarmThread extends Thread |
| 3852 | { |
Suprabh Shukla | 1d6a403 | 2018-07-09 16:59:27 -0700 | [diff] [blame] | 3853 | private int mFalseWakeups; |
| 3854 | private int mWtfThreshold; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3855 | public AlarmThread() |
| 3856 | { |
| 3857 | super("AlarmManager"); |
Suprabh Shukla | 1d6a403 | 2018-07-09 16:59:27 -0700 | [diff] [blame] | 3858 | mFalseWakeups = 0; |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3859 | mWtfThreshold = 100; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3860 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 3861 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3862 | public void run() |
| 3863 | { |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 3864 | ArrayList<Alarm> triggerList = new ArrayList<Alarm>(); |
| 3865 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3866 | while (true) |
| 3867 | { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3868 | int result = mInjector.waitForAlarm(); |
| 3869 | final long nowRTC = mInjector.getCurrentTimeMillis(); |
| 3870 | final long nowELAPSED = mInjector.getElapsedRealtime(); |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 3871 | synchronized (mLock) { |
| 3872 | mLastWakeup = nowELAPSED; |
| 3873 | } |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 3874 | if (result == 0) { |
| 3875 | Slog.wtf(TAG, "waitForAlarm returned 0, nowRTC = " + nowRTC |
| 3876 | + ", nowElapsed = " + nowELAPSED); |
| 3877 | } |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 3878 | triggerList.clear(); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | if ((result & TIME_CHANGED_MASK) != 0) { |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3881 | // The kernel can give us spurious time change notifications due to |
| 3882 | // small adjustments it makes internally; we want to filter those out. |
| 3883 | final long lastTimeChangeClockTime; |
| 3884 | final long expectedClockTime; |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 3885 | synchronized (mLock) { |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3886 | lastTimeChangeClockTime = mLastTimeChangeClockTime; |
| 3887 | expectedClockTime = lastTimeChangeClockTime |
| 3888 | + (nowELAPSED - mLastTimeChangeRealtime); |
Dianne Hackborn | 998e608 | 2014-09-11 19:13:23 -0700 | [diff] [blame] | 3889 | } |
Christopher Tate | 2b6e459e | 2017-02-17 14:33:52 -0800 | [diff] [blame] | 3890 | if (lastTimeChangeClockTime == 0 || nowRTC < (expectedClockTime-1000) |
| 3891 | || nowRTC > (expectedClockTime+1000)) { |
| 3892 | // 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] | 3893 | // let's do it! |
| 3894 | if (DEBUG_BATCH) { |
| 3895 | Slog.v(TAG, "Time changed notification from kernel; rebatching"); |
| 3896 | } |
Bookatz | 7948c87 | 2018-09-04 12:58:33 -0700 | [diff] [blame] | 3897 | // StatsLog requires currentTimeMillis(), which == nowRTC to within usecs. |
| 3898 | StatsLog.write(StatsLog.WALL_CLOCK_TIME_SHIFTED, nowRTC); |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 3899 | removeImpl(null, mTimeTickTrigger); |
| 3900 | removeImpl(mDateChangeSender, null); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3901 | rebatchAllAlarms(); |
| 3902 | mClockReceiver.scheduleTimeTickEvent(); |
Christopher Tate | cf02471 | 2016-08-05 13:21:45 -0700 | [diff] [blame] | 3903 | mClockReceiver.scheduleDateChangedEvent(); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3904 | synchronized (mLock) { |
| 3905 | mNumTimeChanged++; |
| 3906 | mLastTimeChangeClockTime = nowRTC; |
| 3907 | mLastTimeChangeRealtime = nowELAPSED; |
| 3908 | } |
| 3909 | Intent intent = new Intent(Intent.ACTION_TIME_CHANGED); |
| 3910 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
Christopher Tate | 2b6e459e | 2017-02-17 14:33:52 -0800 | [diff] [blame] | 3911 | | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
Chad Brubaker | 291df4f | 2017-03-14 10:23:02 -0700 | [diff] [blame] | 3912 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
| 3913 | | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3914 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 3915 | |
| 3916 | // The world has changed on us, so we need to re-evaluate alarms |
| 3917 | // regardless of whether the kernel has told us one went off. |
| 3918 | result |= IS_WAKEUP_MASK; |
| 3919 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3920 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3921 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3922 | if (result != TIME_CHANGED_MASK) { |
| 3923 | // If this was anything besides just a time change, then figure what if |
| 3924 | // anything to do about alarms. |
| 3925 | synchronized (mLock) { |
| 3926 | if (localLOGV) Slog.v( |
| 3927 | TAG, "Checking for alarms... rtc=" + nowRTC |
| 3928 | + ", elapsed=" + nowELAPSED); |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 3929 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3930 | if (WAKEUP_STATS) { |
| 3931 | if ((result & IS_WAKEUP_MASK) != 0) { |
| 3932 | long newEarliest = nowRTC - RECENT_WAKEUP_PERIOD; |
| 3933 | int n = 0; |
| 3934 | for (WakeupEvent event : mRecentWakeups) { |
| 3935 | if (event.when > newEarliest) break; |
| 3936 | n++; // number of now-stale entries at the list head |
| 3937 | } |
| 3938 | for (int i = 0; i < n; i++) { |
| 3939 | mRecentWakeups.remove(); |
| 3940 | } |
Christopher Tate | 18a75f1 | 2013-07-01 18:18:59 -0700 | [diff] [blame] | 3941 | |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3942 | recordWakeupAlarms(mAlarmBatches, nowELAPSED, nowRTC); |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3943 | } |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 3944 | } |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3945 | |
Christopher Tate | 8b98ade | 2018-02-09 11:13:19 -0800 | [diff] [blame] | 3946 | mLastTrigger = nowELAPSED; |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3947 | boolean hasWakeup = triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC); |
| 3948 | if (!hasWakeup && checkAllowNonWakeupDelayLocked(nowELAPSED)) { |
| 3949 | // if there are no wakeup alarms and the screen is off, we can |
| 3950 | // delay what we have so far until the future. |
| 3951 | if (mPendingNonWakeupAlarms.size() == 0) { |
| 3952 | mStartCurrentDelayTime = nowELAPSED; |
| 3953 | mNextNonWakeupDeliveryTime = nowELAPSED |
| 3954 | + ((currentNonWakeupFuzzLocked(nowELAPSED)*3)/2); |
| 3955 | } |
| 3956 | mPendingNonWakeupAlarms.addAll(triggerList); |
| 3957 | mNumDelayedAlarms += triggerList.size(); |
| 3958 | rescheduleKernelAlarmsLocked(); |
| 3959 | updateNextAlarmClockLocked(); |
| 3960 | } else { |
| 3961 | // now deliver the alarm intents; if there are pending non-wakeup |
| 3962 | // alarms, we need to merge them in to the list. note we don't |
| 3963 | // just deliver them first because we generally want non-wakeup |
| 3964 | // alarms delivered after wakeup alarms. |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3965 | if (mPendingNonWakeupAlarms.size() > 0) { |
| 3966 | calculateDeliveryPriorities(mPendingNonWakeupAlarms); |
| 3967 | triggerList.addAll(mPendingNonWakeupAlarms); |
| 3968 | Collections.sort(triggerList, mAlarmDispatchComparator); |
| 3969 | final long thisDelayTime = nowELAPSED - mStartCurrentDelayTime; |
| 3970 | mTotalDelayTime += thisDelayTime; |
| 3971 | if (mMaxDelayTime < thisDelayTime) { |
| 3972 | mMaxDelayTime = thisDelayTime; |
| 3973 | } |
| 3974 | mPendingNonWakeupAlarms.clear(); |
| 3975 | } |
Suprabh Shukla | 1d6a403 | 2018-07-09 16:59:27 -0700 | [diff] [blame] | 3976 | if (mLastTimeChangeRealtime != nowELAPSED && triggerList.isEmpty()) { |
| 3977 | if (++mFalseWakeups >= mWtfThreshold) { |
| 3978 | Slog.wtf(TAG, "Too many (" + mFalseWakeups |
| 3979 | + ") false wakeups, nowElapsed=" + nowELAPSED); |
| 3980 | if (mWtfThreshold < 100_000) { |
| 3981 | mWtfThreshold *= 10; |
| 3982 | } else { |
| 3983 | mFalseWakeups = 0; |
| 3984 | } |
| 3985 | } |
| 3986 | } |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 3987 | final ArraySet<Pair<String, Integer>> triggerPackages = |
| 3988 | new ArraySet<>(); |
| 3989 | for (int i = 0; i < triggerList.size(); i++) { |
| 3990 | final Alarm a = triggerList.get(i); |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 3991 | if (!isExemptFromAppStandby(a)) { |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 3992 | triggerPackages.add(Pair.create( |
| 3993 | a.sourcePackage, UserHandle.getUserId(a.creatorUid))); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 3994 | } |
| 3995 | } |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 3996 | deliverAlarmsLocked(triggerList, nowELAPSED); |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 3997 | reorderAlarmsBasedOnStandbyBuckets(triggerPackages); |
| 3998 | rescheduleKernelAlarmsLocked(); |
| 3999 | updateNextAlarmClockLocked(); |
Dianne Hackborn | c352722 | 2015-05-13 14:03:20 -0700 | [diff] [blame] | 4000 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4001 | } |
Dianne Hackborn | aa9a59d | 2016-05-23 15:59:18 -0700 | [diff] [blame] | 4002 | |
| 4003 | } else { |
| 4004 | // Just in case -- even though no wakeup flag was set, make sure |
| 4005 | // we have updated the kernel to the next alarm time. |
Christopher Tate | 7b5b7cb | 2016-07-07 14:41:36 -0700 | [diff] [blame] | 4006 | synchronized (mLock) { |
| 4007 | rescheduleKernelAlarmsLocked(); |
| 4008 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4009 | } |
| 4010 | } |
| 4011 | } |
| 4012 | } |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4013 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 4014 | /** |
| 4015 | * Attribute blame for a WakeLock. |
| 4016 | * @param pi PendingIntent to attribute blame to if ws is null. |
| 4017 | * @param ws WorkSource to attribute blame. |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4018 | * @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] | 4019 | */ |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 4020 | void setWakelockWorkSource(PendingIntent pi, WorkSource ws, int type, String tag, |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4021 | int knownUid, boolean first) { |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4022 | try { |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 4023 | mWakeLock.setHistoryTag(first ? tag : null); |
| 4024 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 4025 | if (ws != null) { |
| 4026 | mWakeLock.setWorkSource(ws); |
| 4027 | return; |
| 4028 | } |
| 4029 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4030 | final int uid = (knownUid >= 0) |
| 4031 | ? knownUid |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4032 | : ActivityManager.getService().getUidForIntentSender(pi.getTarget()); |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4033 | if (uid >= 0) { |
| 4034 | mWakeLock.setWorkSource(new WorkSource(uid)); |
| 4035 | return; |
| 4036 | } |
| 4037 | } catch (Exception e) { |
| 4038 | } |
| 4039 | |
| 4040 | // Something went wrong; fall back to attributing the lock to the OS |
| 4041 | mWakeLock.setWorkSource(null); |
| 4042 | } |
| 4043 | |
Suprabh Shukla | 12bd016 | 2018-11-12 18:00:18 -0800 | [diff] [blame] | 4044 | @VisibleForTesting |
| 4045 | class AlarmHandler extends Handler { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4046 | public static final int ALARM_EVENT = 1; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 4047 | public static final int SEND_NEXT_ALARM_CLOCK_CHANGED = 2; |
| 4048 | public static final int LISTENER_TIMEOUT = 3; |
| 4049 | public static final int REPORT_ALARMS_ACTIVE = 4; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4050 | public static final int APP_STANDBY_BUCKET_CHANGED = 5; |
| 4051 | public static final int APP_STANDBY_PAROLE_CHANGED = 6; |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 4052 | public static final int REMOVE_FOR_STOPPED = 7; |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 4053 | |
Suprabh Shukla | 12bd016 | 2018-11-12 18:00:18 -0800 | [diff] [blame] | 4054 | AlarmHandler() { |
| 4055 | super(Looper.myLooper()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4056 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 4057 | |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 4058 | public void postRemoveForStopped(int uid) { |
| 4059 | obtainMessage(REMOVE_FOR_STOPPED, uid, 0).sendToTarget(); |
| 4060 | } |
| 4061 | |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 4062 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4063 | public void handleMessage(Message msg) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4064 | switch (msg.what) { |
| 4065 | case ALARM_EVENT: { |
| 4066 | ArrayList<Alarm> triggerList = new ArrayList<Alarm>(); |
| 4067 | synchronized (mLock) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 4068 | final long nowRTC = mInjector.getCurrentTimeMillis(); |
| 4069 | final long nowELAPSED = mInjector.getElapsedRealtime(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4070 | triggerAlarmsLocked(triggerList, nowELAPSED, nowRTC); |
| 4071 | updateNextAlarmClockLocked(); |
| 4072 | } |
Adrian Roos | c42a1e1 | 2014-07-07 23:35:53 +0200 | [diff] [blame] | 4073 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4074 | // now trigger the alarms without the lock held |
| 4075 | for (int i=0; i<triggerList.size(); i++) { |
| 4076 | Alarm alarm = triggerList.get(i); |
| 4077 | try { |
| 4078 | alarm.operation.send(); |
| 4079 | } catch (PendingIntent.CanceledException e) { |
| 4080 | if (alarm.repeatInterval > 0) { |
| 4081 | // This IntentSender is no longer valid, but this |
| 4082 | // is a repeating alarm, so toss the hoser. |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 4083 | removeImpl(alarm.operation, null); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4084 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4085 | } |
| 4086 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4087 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4088 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4089 | |
| 4090 | case SEND_NEXT_ALARM_CLOCK_CHANGED: |
| 4091 | sendNextAlarmClockChanged(); |
| 4092 | break; |
| 4093 | |
| 4094 | case LISTENER_TIMEOUT: |
| 4095 | mDeliveryTracker.alarmTimedOut((IBinder) msg.obj); |
| 4096 | break; |
| 4097 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 4098 | case REPORT_ALARMS_ACTIVE: |
| 4099 | if (mLocalDeviceIdleController != null) { |
| 4100 | mLocalDeviceIdleController.setAlarmsActive(msg.arg1 != 0); |
| 4101 | } |
| 4102 | break; |
| 4103 | |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4104 | case APP_STANDBY_PAROLE_CHANGED: |
| 4105 | synchronized (mLock) { |
| 4106 | mAppStandbyParole = (Boolean) msg.obj; |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 4107 | if (reorderAlarmsBasedOnStandbyBuckets(null)) { |
| 4108 | rescheduleKernelAlarmsLocked(); |
| 4109 | updateNextAlarmClockLocked(); |
| 4110 | } |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4111 | } |
| 4112 | break; |
| 4113 | |
| 4114 | case APP_STANDBY_BUCKET_CHANGED: |
| 4115 | synchronized (mLock) { |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 4116 | final ArraySet<Pair<String, Integer>> filterPackages = new ArraySet<>(); |
| 4117 | filterPackages.add(Pair.create((String) msg.obj, msg.arg1)); |
| 4118 | if (reorderAlarmsBasedOnStandbyBuckets(filterPackages)) { |
| 4119 | rescheduleKernelAlarmsLocked(); |
| 4120 | updateNextAlarmClockLocked(); |
| 4121 | } |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4122 | } |
| 4123 | break; |
| 4124 | |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 4125 | case REMOVE_FOR_STOPPED: |
| 4126 | synchronized (mLock) { |
| 4127 | removeForStoppedLocked(msg.arg1); |
| 4128 | } |
| 4129 | break; |
| 4130 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4131 | default: |
| 4132 | // nope, just ignore it |
| 4133 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4134 | } |
| 4135 | } |
| 4136 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 4137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4138 | class ClockReceiver extends BroadcastReceiver { |
| 4139 | public ClockReceiver() { |
| 4140 | IntentFilter filter = new IntentFilter(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4141 | filter.addAction(Intent.ACTION_DATE_CHANGED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4142 | getContext().registerReceiver(this, filter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4143 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 4144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4145 | @Override |
| 4146 | public void onReceive(Context context, Intent intent) { |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 4147 | if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4148 | // Since the kernel does not keep track of DST, we need to |
| 4149 | // reset the TZ information at the beginning of each day |
| 4150 | // based off of the current Zone gmt offset + userspace tracked |
| 4151 | // daylight savings information. |
| 4152 | TimeZone zone = TimeZone.getTimeZone(SystemProperties.get(TIMEZONE_PROPERTY)); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 4153 | int gmtOffset = zone.getOffset(mInjector.getCurrentTimeMillis()); |
| 4154 | mInjector.setKernelTimezone(-(gmtOffset / 60000)); |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 4155 | scheduleDateChangedEvent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4156 | } |
| 4157 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 4158 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4159 | public void scheduleTimeTickEvent() { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 4160 | final long currentTime = mInjector.getCurrentTimeMillis(); |
Sungmin Choi | 563914a | 2013-01-10 17:28:40 +0900 | [diff] [blame] | 4161 | final long nextTime = 60000 * ((currentTime / 60000) + 1); |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 4162 | |
| 4163 | // Schedule this event for the amount of time that it would take to get to |
| 4164 | // the top of the next minute. |
Sungmin Choi | 563914a | 2013-01-10 17:28:40 +0900 | [diff] [blame] | 4165 | final long tickEventDelay = nextTime - currentTime; |
Paul Westbrook | 51608a5 | 2011-08-25 13:18:54 -0700 | [diff] [blame] | 4166 | |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 4167 | final WorkSource workSource = null; // Let system take blame for time tick events. |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 4168 | setImpl(ELAPSED_REALTIME, mInjector.getElapsedRealtime() + tickEventDelay, 0, |
Suprabh Shukla | 12bd016 | 2018-11-12 18:00:18 -0800 | [diff] [blame] | 4169 | 0, null, mTimeTickTrigger, "TIME_TICK", AlarmManager.FLAG_STANDALONE, |
| 4170 | workSource, null, Process.myUid(), "android"); |
Christopher Tate | 12cf0b6 | 2018-01-05 18:40:14 -0800 | [diff] [blame] | 4171 | |
| 4172 | // Finally, remember when we set the tick alarm |
| 4173 | synchronized (mLock) { |
| 4174 | mLastTickSet = currentTime; |
| 4175 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4176 | } |
Christopher Tate | 385e498 | 2013-07-23 18:22:29 -0700 | [diff] [blame] | 4177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4178 | public void scheduleDateChangedEvent() { |
| 4179 | Calendar calendar = Calendar.getInstance(); |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 4180 | calendar.setTimeInMillis(mInjector.getCurrentTimeMillis()); |
Christopher Tate | afa8b98 | 2016-08-10 16:15:48 -0700 | [diff] [blame] | 4181 | calendar.set(Calendar.HOUR_OF_DAY, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4182 | calendar.set(Calendar.MINUTE, 0); |
| 4183 | calendar.set(Calendar.SECOND, 0); |
| 4184 | calendar.set(Calendar.MILLISECOND, 0); |
| 4185 | calendar.add(Calendar.DAY_OF_MONTH, 1); |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 4186 | |
| 4187 | final WorkSource workSource = null; // Let system take blame for date change events. |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4188 | setImpl(RTC, calendar.getTimeInMillis(), 0, 0, mDateChangeSender, null, null, |
| 4189 | AlarmManager.FLAG_STANDALONE, workSource, null, |
| 4190 | Process.myUid(), "android"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4191 | } |
| 4192 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 4193 | |
Dianne Hackborn | d4e6d46 | 2014-05-16 16:32:37 -0700 | [diff] [blame] | 4194 | class InteractiveStateReceiver extends BroadcastReceiver { |
| 4195 | public InteractiveStateReceiver() { |
| 4196 | IntentFilter filter = new IntentFilter(); |
| 4197 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 4198 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 4199 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 4200 | getContext().registerReceiver(this, filter); |
| 4201 | } |
| 4202 | |
| 4203 | @Override |
| 4204 | public void onReceive(Context context, Intent intent) { |
| 4205 | synchronized (mLock) { |
| 4206 | interactiveStateChangedLocked(Intent.ACTION_SCREEN_ON.equals(intent.getAction())); |
| 4207 | } |
| 4208 | } |
| 4209 | } |
| 4210 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4211 | class UninstallReceiver extends BroadcastReceiver { |
| 4212 | public UninstallReceiver() { |
| 4213 | IntentFilter filter = new IntentFilter(); |
| 4214 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 4215 | filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4216 | filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4217 | filter.addDataScheme("package"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4218 | getContext().registerReceiver(this, filter); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4219 | // Register for events related to sdcard installation. |
| 4220 | IntentFilter sdFilter = new IntentFilter(); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 4221 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4222 | sdFilter.addAction(Intent.ACTION_USER_STOPPED); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 4223 | sdFilter.addAction(Intent.ACTION_UID_REMOVED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4224 | getContext().registerReceiver(this, sdFilter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4225 | } |
Kweku Adams | 61e0329 | 2017-10-19 14:27:12 -0700 | [diff] [blame] | 4226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4227 | @Override |
| 4228 | public void onReceive(Context context, Intent intent) { |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 4229 | final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4230 | synchronized (mLock) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4231 | String pkgList[] = null; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 4232 | switch (intent.getAction()) { |
| 4233 | case Intent.ACTION_QUERY_PACKAGE_RESTART: |
| 4234 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4235 | for (String packageName : pkgList) { |
| 4236 | if (lookForPackageLocked(packageName)) { |
| 4237 | setResultCode(Activity.RESULT_OK); |
| 4238 | return; |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4239 | } |
| 4240 | } |
Dianne Hackborn | 409578f | 2010-03-10 17:23:43 -0800 | [diff] [blame] | 4241 | return; |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 4242 | case Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE: |
| 4243 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 4244 | break; |
| 4245 | case Intent.ACTION_USER_STOPPED: |
| 4246 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 4247 | if (userHandle >= 0) { |
| 4248 | removeUserLocked(userHandle); |
| 4249 | mAppWakeupHistory.removeForUser(userHandle); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4250 | } |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 4251 | return; |
| 4252 | case Intent.ACTION_UID_REMOVED: |
| 4253 | if (uid >= 0) { |
| 4254 | mLastAllowWhileIdleDispatch.delete(uid); |
| 4255 | mUseAllowWhileIdleShortTime.delete(uid); |
| 4256 | } |
| 4257 | return; |
| 4258 | case Intent.ACTION_PACKAGE_REMOVED: |
| 4259 | if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 4260 | // This package is being updated; don't kill its alarms. |
| 4261 | return; |
| 4262 | } |
| 4263 | // Intentional fall-through. |
| 4264 | case Intent.ACTION_PACKAGE_RESTARTED: |
| 4265 | final Uri data = intent.getData(); |
| 4266 | if (data != null) { |
| 4267 | final String pkg = data.getSchemeSpecificPart(); |
| 4268 | if (pkg != null) { |
| 4269 | pkgList = new String[]{pkg}; |
| 4270 | } |
| 4271 | } |
| 4272 | break; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4273 | } |
| 4274 | if (pkgList != null && (pkgList.length > 0)) { |
| 4275 | for (String pkg : pkgList) { |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 4276 | if (uid >= 0) { |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 4277 | // package-removed and package-restarted case |
| 4278 | mAppWakeupHistory.removeForPackage(pkg, UserHandle.getUserId(uid)); |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 4279 | removeLocked(uid); |
| 4280 | } else { |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 4281 | // external-applications-unavailable case |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 4282 | removeLocked(pkg); |
| 4283 | } |
Christopher Tate | 1590f1e | 2014-10-02 17:27:57 -0700 | [diff] [blame] | 4284 | mPriorities.remove(pkg); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 4285 | for (int i=mBroadcastStats.size()-1; i>=0; i--) { |
| 4286 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.valueAt(i); |
| 4287 | if (uidStats.remove(pkg) != null) { |
| 4288 | if (uidStats.size() <= 0) { |
| 4289 | mBroadcastStats.removeAt(i); |
| 4290 | } |
| 4291 | } |
| 4292 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4293 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | } |
| 4297 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 4298 | |
| 4299 | final class UidObserver extends IUidObserver.Stub { |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 4300 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 4301 | } |
| 4302 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 4303 | @Override public void onUidGone(int uid, boolean disabled) { |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 4304 | if (disabled) { |
| 4305 | mHandler.postRemoveForStopped(uid); |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 4306 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 4309 | @Override public void onUidActive(int uid) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 4312 | @Override public void onUidIdle(int uid, boolean disabled) { |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 4313 | if (disabled) { |
| 4314 | mHandler.postRemoveForStopped(uid); |
Dianne Hackborn | de9c48b | 2015-11-20 14:49:59 -0800 | [diff] [blame] | 4315 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 4316 | } |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 4317 | |
| 4318 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
| 4319 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 4320 | }; |
| 4321 | |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4322 | /** |
| 4323 | * Tracking of app assignments to standby buckets |
| 4324 | */ |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 4325 | private final class AppStandbyTracker extends |
| 4326 | UsageStatsManagerInternal.AppIdleStateChangeListener { |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4327 | @Override |
| 4328 | public void onAppIdleStateChanged(final String packageName, final @UserIdInt int userId, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 4329 | boolean idle, int bucket, int reason) { |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4330 | if (DEBUG_STANDBY) { |
| 4331 | Slog.d(TAG, "Package " + packageName + " for user " + userId + " now in bucket " + |
| 4332 | bucket); |
| 4333 | } |
| 4334 | mHandler.removeMessages(AlarmHandler.APP_STANDBY_BUCKET_CHANGED); |
Suprabh Shukla | 92994ab | 2018-01-31 17:39:30 -0800 | [diff] [blame] | 4335 | mHandler.obtainMessage(AlarmHandler.APP_STANDBY_BUCKET_CHANGED, userId, -1, packageName) |
| 4336 | .sendToTarget(); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | @Override |
| 4340 | public void onParoleStateChanged(boolean isParoleOn) { |
| 4341 | if (DEBUG_STANDBY) { |
| 4342 | Slog.d(TAG, "Global parole state now " + (isParoleOn ? "ON" : "OFF")); |
| 4343 | } |
| 4344 | mHandler.removeMessages(AlarmHandler.APP_STANDBY_BUCKET_CHANGED); |
| 4345 | mHandler.removeMessages(AlarmHandler.APP_STANDBY_PAROLE_CHANGED); |
| 4346 | mHandler.obtainMessage(AlarmHandler.APP_STANDBY_PAROLE_CHANGED, |
| 4347 | Boolean.valueOf(isParoleOn)).sendToTarget(); |
| 4348 | } |
| 4349 | }; |
| 4350 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 4351 | private final Listener mForceAppStandbyListener = new Listener() { |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 4352 | @Override |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 4353 | public void unblockAllUnrestrictedAlarms() { |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 4354 | synchronized (mLock) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 4355 | sendAllUnrestrictedPendingBackgroundAlarmsLocked(); |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 4356 | } |
| 4357 | } |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 4358 | |
| 4359 | @Override |
| 4360 | public void unblockAlarmsForUid(int uid) { |
| 4361 | synchronized (mLock) { |
| 4362 | sendPendingBackgroundAlarmsLocked(uid, null); |
| 4363 | } |
| 4364 | } |
| 4365 | |
| 4366 | @Override |
| 4367 | public void unblockAlarmsForUidPackage(int uid, String packageName) { |
| 4368 | synchronized (mLock) { |
| 4369 | sendPendingBackgroundAlarmsLocked(uid, packageName); |
| 4370 | } |
| 4371 | } |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 4372 | |
| 4373 | @Override |
| 4374 | public void onUidForeground(int uid, boolean foreground) { |
| 4375 | synchronized (mLock) { |
| 4376 | if (foreground) { |
| 4377 | mUseAllowWhileIdleShortTime.put(uid, true); |
| 4378 | |
| 4379 | // Note we don't have to drain the pending while-idle alarms here, because |
| 4380 | // this event should coincide with unblockAlarmsForUid(). |
| 4381 | } |
| 4382 | } |
| 4383 | } |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 4384 | }; |
Suprabh Shukla | db6bf66 | 2017-08-30 15:41:53 -0700 | [diff] [blame] | 4385 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4386 | private final BroadcastStats getStatsLocked(PendingIntent pi) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 4387 | String pkg = pi.getCreatorPackage(); |
| 4388 | int uid = pi.getCreatorUid(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4389 | return getStatsLocked(uid, pkg); |
| 4390 | } |
| 4391 | |
| 4392 | private final BroadcastStats getStatsLocked(int uid, String pkgName) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 4393 | ArrayMap<String, BroadcastStats> uidStats = mBroadcastStats.get(uid); |
| 4394 | if (uidStats == null) { |
| 4395 | uidStats = new ArrayMap<String, BroadcastStats>(); |
| 4396 | mBroadcastStats.put(uid, uidStats); |
| 4397 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4398 | BroadcastStats bs = uidStats.get(pkgName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4399 | if (bs == null) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4400 | bs = new BroadcastStats(uid, pkgName); |
| 4401 | uidStats.put(pkgName, bs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4402 | } |
| 4403 | return bs; |
| 4404 | } |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 4405 | |
Christopher Tate | 21e9f19 | 2017-08-08 13:49:11 -0700 | [diff] [blame] | 4406 | /** |
| 4407 | * Canonical count of (operation.send() - onSendFinished()) and |
| 4408 | * listener send/complete/timeout invocations. |
| 4409 | * Guarded by the usual lock. |
| 4410 | */ |
| 4411 | @GuardedBy("mLock") |
| 4412 | private int mSendCount = 0; |
| 4413 | @GuardedBy("mLock") |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 4414 | private int mSendFinishCount = 0; |
| 4415 | @GuardedBy("mLock") |
Christopher Tate | 21e9f19 | 2017-08-08 13:49:11 -0700 | [diff] [blame] | 4416 | private int mListenerCount = 0; |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 4417 | @GuardedBy("mLock") |
| 4418 | private int mListenerFinishCount = 0; |
Christopher Tate | 21e9f19 | 2017-08-08 13:49:11 -0700 | [diff] [blame] | 4419 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4420 | class DeliveryTracker extends IAlarmCompleteListener.Stub implements PendingIntent.OnFinished { |
Christopher Tate | 21e9f19 | 2017-08-08 13:49:11 -0700 | [diff] [blame] | 4421 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4422 | private InFlight removeLocked(PendingIntent pi, Intent intent) { |
| 4423 | for (int i = 0; i < mInFlight.size(); i++) { |
Christopher Tate | db9ae42 | 2017-08-21 11:24:30 -0700 | [diff] [blame] | 4424 | if (mInFlight.get(i).mPendingIntent == pi) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4425 | return mInFlight.remove(i); |
| 4426 | } |
| 4427 | } |
| 4428 | mLog.w("No in-flight alarm for " + pi + " " + intent); |
| 4429 | return null; |
| 4430 | } |
| 4431 | |
| 4432 | private InFlight removeLocked(IBinder listener) { |
| 4433 | for (int i = 0; i < mInFlight.size(); i++) { |
| 4434 | if (mInFlight.get(i).mListener == listener) { |
| 4435 | return mInFlight.remove(i); |
| 4436 | } |
| 4437 | } |
| 4438 | mLog.w("No in-flight alarm for listener " + listener); |
| 4439 | return null; |
| 4440 | } |
| 4441 | |
| 4442 | private void updateStatsLocked(InFlight inflight) { |
Suprabh Shukla | 6226c2f | 2018-08-22 18:15:04 -0700 | [diff] [blame] | 4443 | final long nowELAPSED = mInjector.getElapsedRealtime(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4444 | BroadcastStats bs = inflight.mBroadcastStats; |
| 4445 | bs.nesting--; |
| 4446 | if (bs.nesting <= 0) { |
| 4447 | bs.nesting = 0; |
| 4448 | bs.aggregateTime += nowELAPSED - bs.startTime; |
| 4449 | } |
| 4450 | FilterStats fs = inflight.mFilterStats; |
| 4451 | fs.nesting--; |
| 4452 | if (fs.nesting <= 0) { |
| 4453 | fs.nesting = 0; |
| 4454 | fs.aggregateTime += nowELAPSED - fs.startTime; |
| 4455 | } |
| 4456 | if (RECORD_ALARMS_IN_HISTORY) { |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 4457 | ActivityManager.noteAlarmFinish(inflight.mPendingIntent, inflight.mWorkSource, |
| 4458 | inflight.mUid, inflight.mTag); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | |
| 4462 | private void updateTrackingLocked(InFlight inflight) { |
| 4463 | if (inflight != null) { |
| 4464 | updateStatsLocked(inflight); |
| 4465 | } |
| 4466 | mBroadcastRefCount--; |
Makoto Onuki | 3e7d845 | 2017-03-02 15:33:17 -0800 | [diff] [blame] | 4467 | if (DEBUG_WAKELOCK) { |
| 4468 | Slog.d(TAG, "mBroadcastRefCount -> " + mBroadcastRefCount); |
| 4469 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4470 | if (mBroadcastRefCount == 0) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 4471 | mHandler.obtainMessage(AlarmHandler.REPORT_ALARMS_ACTIVE, 0).sendToTarget(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4472 | mWakeLock.release(); |
| 4473 | if (mInFlight.size() > 0) { |
| 4474 | mLog.w("Finished all dispatches with " + mInFlight.size() |
| 4475 | + " remaining inflights"); |
| 4476 | for (int i=0; i<mInFlight.size(); i++) { |
| 4477 | mLog.w(" Remaining #" + i + ": " + mInFlight.get(i)); |
| 4478 | } |
| 4479 | mInFlight.clear(); |
| 4480 | } |
| 4481 | } else { |
| 4482 | // the next of our alarms is now in flight. reattribute the wakelock. |
| 4483 | if (mInFlight.size() > 0) { |
| 4484 | InFlight inFlight = mInFlight.get(0); |
| 4485 | setWakelockWorkSource(inFlight.mPendingIntent, inFlight.mWorkSource, |
| 4486 | inFlight.mAlarmType, inFlight.mTag, -1, false); |
| 4487 | } else { |
| 4488 | // should never happen |
| 4489 | mLog.w("Alarm wakelock still held but sent queue empty"); |
| 4490 | mWakeLock.setWorkSource(null); |
| 4491 | } |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | /** |
| 4496 | * Callback that arrives when a direct-call alarm reports that delivery has finished |
| 4497 | */ |
| 4498 | @Override |
| 4499 | public void alarmComplete(IBinder who) { |
| 4500 | if (who == null) { |
Christopher Tate | 21e9f19 | 2017-08-08 13:49:11 -0700 | [diff] [blame] | 4501 | mLog.w("Invalid alarmComplete: uid=" + Binder.getCallingUid() |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4502 | + " pid=" + Binder.getCallingPid()); |
| 4503 | return; |
| 4504 | } |
| 4505 | |
| 4506 | final long ident = Binder.clearCallingIdentity(); |
| 4507 | try { |
| 4508 | synchronized (mLock) { |
| 4509 | mHandler.removeMessages(AlarmHandler.LISTENER_TIMEOUT, who); |
| 4510 | InFlight inflight = removeLocked(who); |
| 4511 | if (inflight != null) { |
| 4512 | if (DEBUG_LISTENER_CALLBACK) { |
| 4513 | Slog.i(TAG, "alarmComplete() from " + who); |
| 4514 | } |
| 4515 | updateTrackingLocked(inflight); |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 4516 | mListenerFinishCount++; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4517 | } else { |
| 4518 | // Delivery timed out, and the timeout handling already took care of |
| 4519 | // updating our tracking here, so we needn't do anything further. |
| 4520 | if (DEBUG_LISTENER_CALLBACK) { |
| 4521 | Slog.i(TAG, "Late alarmComplete() from " + who); |
| 4522 | } |
| 4523 | } |
| 4524 | } |
| 4525 | } finally { |
| 4526 | Binder.restoreCallingIdentity(ident); |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | /** |
| 4531 | * Callback that arrives when a PendingIntent alarm has finished delivery |
| 4532 | */ |
| 4533 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4534 | public void onSendFinished(PendingIntent pi, Intent intent, int resultCode, |
| 4535 | String resultData, Bundle resultExtras) { |
| 4536 | synchronized (mLock) { |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 4537 | mSendFinishCount++; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4538 | updateTrackingLocked(removeLocked(pi, intent)); |
| 4539 | } |
| 4540 | } |
| 4541 | |
| 4542 | /** |
| 4543 | * Timeout of a direct-call alarm delivery |
| 4544 | */ |
| 4545 | public void alarmTimedOut(IBinder who) { |
| 4546 | synchronized (mLock) { |
| 4547 | InFlight inflight = removeLocked(who); |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 4548 | if (inflight != null) { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4549 | // TODO: implement ANR policy for the target |
| 4550 | if (DEBUG_LISTENER_CALLBACK) { |
| 4551 | 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] | 4552 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4553 | updateTrackingLocked(inflight); |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 4554 | mListenerFinishCount++; |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 4555 | } else { |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4556 | if (DEBUG_LISTENER_CALLBACK) { |
| 4557 | Slog.i(TAG, "Spurious timeout of listener " + who); |
| 4558 | } |
Christopher Tate | 21e9f19 | 2017-08-08 13:49:11 -0700 | [diff] [blame] | 4559 | mLog.w("Spurious timeout of listener " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4560 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4561 | } |
| 4562 | } |
| 4563 | |
| 4564 | /** |
| 4565 | * Deliver an alarm and set up the post-delivery handling appropriately |
| 4566 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 4567 | @GuardedBy("mLock") |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4568 | public void deliverLocked(Alarm alarm, long nowELAPSED, boolean allowWhileIdle) { |
| 4569 | if (alarm.operation != null) { |
| 4570 | // PendingIntent alarm |
Christopher Tate | 21e9f19 | 2017-08-08 13:49:11 -0700 | [diff] [blame] | 4571 | mSendCount++; |
Christopher Tate | 12cf0b6 | 2018-01-05 18:40:14 -0800 | [diff] [blame] | 4572 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4573 | try { |
| 4574 | alarm.operation.send(getContext(), 0, |
| 4575 | mBackgroundIntent.putExtra( |
| 4576 | Intent.EXTRA_ALARM_COUNT, alarm.count), |
| 4577 | mDeliveryTracker, mHandler, null, |
| 4578 | allowWhileIdle ? mIdleOptions : null); |
| 4579 | } catch (PendingIntent.CanceledException e) { |
| 4580 | if (alarm.repeatInterval > 0) { |
| 4581 | // This IntentSender is no longer valid, but this |
| 4582 | // is a repeating alarm, so toss it |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 4583 | removeImpl(alarm.operation, null); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4584 | } |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 4585 | // No actual delivery was possible, so the delivery tracker's |
| 4586 | // 'finished' callback won't be invoked. We also don't need |
| 4587 | // to do any wakelock or stats tracking, so we have nothing |
| 4588 | // left to do here but go on to the next thing. |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 4589 | mSendFinishCount++; |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 4590 | return; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4591 | } |
| 4592 | } else { |
| 4593 | // Direct listener callback alarm |
Christopher Tate | 21e9f19 | 2017-08-08 13:49:11 -0700 | [diff] [blame] | 4594 | mListenerCount++; |
Christopher Tate | c8b7f43 | 2018-09-28 16:23:10 -0700 | [diff] [blame] | 4595 | |
| 4596 | if (RECORD_ALARMS_IN_HISTORY) { |
| 4597 | if (alarm.listener == mTimeTickTrigger) { |
| 4598 | mTickHistory[mNextTickHistory++] = nowELAPSED; |
| 4599 | if (mNextTickHistory >= TICK_HISTORY_DEPTH) { |
| 4600 | mNextTickHistory = 0; |
| 4601 | } |
| 4602 | } |
| 4603 | } |
| 4604 | |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4605 | try { |
| 4606 | if (DEBUG_LISTENER_CALLBACK) { |
| 4607 | Slog.v(TAG, "Alarm to uid=" + alarm.uid |
| 4608 | + " listener=" + alarm.listener.asBinder()); |
| 4609 | } |
| 4610 | alarm.listener.doAlarm(this); |
| 4611 | mHandler.sendMessageDelayed( |
| 4612 | mHandler.obtainMessage(AlarmHandler.LISTENER_TIMEOUT, |
| 4613 | alarm.listener.asBinder()), |
| 4614 | mConstants.LISTENER_TIMEOUT); |
| 4615 | } catch (Exception e) { |
| 4616 | if (DEBUG_LISTENER_CALLBACK) { |
| 4617 | Slog.i(TAG, "Alarm undeliverable to listener " |
| 4618 | + alarm.listener.asBinder(), e); |
| 4619 | } |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 4620 | // As in the PendingIntent.CanceledException case, delivery of the |
| 4621 | // alarm was not possible, so we have no wakelock or timeout or |
| 4622 | // stats management to do. It threw before we posted the delayed |
| 4623 | // timeout message, so we're done here. |
Christopher Tate | eabba73 | 2017-08-17 17:12:52 -0700 | [diff] [blame] | 4624 | mListenerFinishCount++; |
Christopher Tate | 7f2a035 | 2015-12-08 10:24:33 -0800 | [diff] [blame] | 4625 | return; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4626 | } |
| 4627 | } |
| 4628 | |
| 4629 | // The alarm is now in flight; now arrange wakelock and stats tracking |
Makoto Onuki | 3e7d845 | 2017-03-02 15:33:17 -0800 | [diff] [blame] | 4630 | if (DEBUG_WAKELOCK) { |
| 4631 | Slog.d(TAG, "mBroadcastRefCount -> " + (mBroadcastRefCount + 1)); |
| 4632 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4633 | if (mBroadcastRefCount == 0) { |
| 4634 | setWakelockWorkSource(alarm.operation, alarm.workSource, |
| 4635 | alarm.type, alarm.statsTag, (alarm.operation == null) ? alarm.uid : -1, |
| 4636 | true); |
| 4637 | mWakeLock.acquire(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 4638 | mHandler.obtainMessage(AlarmHandler.REPORT_ALARMS_ACTIVE, 1).sendToTarget(); |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4639 | } |
| 4640 | final InFlight inflight = new InFlight(AlarmManagerService.this, |
| 4641 | alarm.operation, alarm.listener, alarm.workSource, alarm.uid, |
| 4642 | alarm.packageName, alarm.type, alarm.statsTag, nowELAPSED); |
| 4643 | mInFlight.add(inflight); |
| 4644 | mBroadcastRefCount++; |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4645 | if (allowWhileIdle) { |
| 4646 | // Record the last time this uid handled an ALLOW_WHILE_IDLE alarm. |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 4647 | mLastAllowWhileIdleDispatch.put(alarm.creatorUid, nowELAPSED); |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 4648 | if ((mAppStateTracker == null) |
| 4649 | || mAppStateTracker.isUidInForeground(alarm.creatorUid)) { |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 4650 | mUseAllowWhileIdleShortTime.put(alarm.creatorUid, true); |
| 4651 | } else { |
| 4652 | mUseAllowWhileIdleShortTime.put(alarm.creatorUid, false); |
| 4653 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4654 | if (RECORD_DEVICE_IDLE_ALARMS) { |
| 4655 | IdleDispatchEntry ent = new IdleDispatchEntry(); |
| 4656 | ent.uid = alarm.uid; |
| 4657 | ent.pkg = alarm.packageName; |
| 4658 | ent.tag = alarm.statsTag; |
| 4659 | ent.op = "DELIVER"; |
| 4660 | ent.elapsedRealtime = nowELAPSED; |
| 4661 | mAllowWhileIdleDispatches.add(ent); |
| 4662 | } |
| 4663 | } |
Suprabh Shukla | d65e58a | 2018-02-05 20:36:08 -0800 | [diff] [blame] | 4664 | if (!isExemptFromAppStandby(alarm)) { |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4665 | final Pair<String, Integer> packageUser = Pair.create(alarm.sourcePackage, |
| 4666 | UserHandle.getUserId(alarm.creatorUid)); |
Suprabh Shukla | 47ca6fc | 2019-01-15 18:15:10 -0800 | [diff] [blame^] | 4667 | mAppWakeupHistory.recordAlarmForPackage(alarm.sourcePackage, |
| 4668 | UserHandle.getUserId(alarm.creatorUid), nowELAPSED); |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 4669 | } |
Christopher Tate | 14a7bb0 | 2015-10-01 10:24:31 -0700 | [diff] [blame] | 4670 | |
| 4671 | final BroadcastStats bs = inflight.mBroadcastStats; |
| 4672 | bs.count++; |
| 4673 | if (bs.nesting == 0) { |
| 4674 | bs.nesting = 1; |
| 4675 | bs.startTime = nowELAPSED; |
| 4676 | } else { |
| 4677 | bs.nesting++; |
| 4678 | } |
| 4679 | final FilterStats fs = inflight.mFilterStats; |
| 4680 | fs.count++; |
| 4681 | if (fs.nesting == 0) { |
| 4682 | fs.nesting = 1; |
| 4683 | fs.startTime = nowELAPSED; |
| 4684 | } else { |
| 4685 | fs.nesting++; |
| 4686 | } |
| 4687 | if (alarm.type == ELAPSED_REALTIME_WAKEUP |
| 4688 | || alarm.type == RTC_WAKEUP) { |
| 4689 | bs.numWakeup++; |
| 4690 | fs.numWakeup++; |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 4691 | ActivityManager.noteWakeupAlarm( |
| 4692 | alarm.operation, alarm.workSource, alarm.uid, alarm.packageName, |
| 4693 | alarm.statsTag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4694 | } |
| 4695 | } |
| 4696 | } |
mswest46 | 3f4c99d | 2018-02-01 10:13:10 -0800 | [diff] [blame] | 4697 | |
| 4698 | private class ShellCmd extends ShellCommand { |
| 4699 | |
| 4700 | IAlarmManager getBinderService() { |
| 4701 | return IAlarmManager.Stub.asInterface(mService); |
| 4702 | } |
| 4703 | |
| 4704 | @Override |
| 4705 | public int onCommand(String cmd) { |
| 4706 | if (cmd == null) { |
| 4707 | return handleDefaultCommands(cmd); |
| 4708 | } |
| 4709 | |
| 4710 | final PrintWriter pw = getOutPrintWriter(); |
| 4711 | try { |
| 4712 | switch (cmd) { |
| 4713 | case "set-time": |
| 4714 | final long millis = Long.parseLong(getNextArgRequired()); |
| 4715 | return (getBinderService().setTime(millis)) ? 0 : -1; |
| 4716 | case "set-timezone": |
| 4717 | final String tz = getNextArgRequired(); |
| 4718 | getBinderService().setTimeZone(tz); |
| 4719 | return 0; |
| 4720 | default: |
| 4721 | return handleDefaultCommands(cmd); |
| 4722 | } |
| 4723 | } catch (Exception e) { |
| 4724 | pw.println(e); |
| 4725 | } |
| 4726 | return -1; |
| 4727 | } |
| 4728 | |
| 4729 | @Override |
| 4730 | public void onHelp() { |
| 4731 | PrintWriter pw = getOutPrintWriter(); |
| 4732 | pw.println("Alarm manager service (alarm) commands:"); |
| 4733 | pw.println(" help"); |
| 4734 | pw.println(" Print this help text."); |
| 4735 | pw.println(" set-time TIME"); |
| 4736 | pw.println(" Set the system clock time to TIME where TIME is milliseconds"); |
| 4737 | pw.println(" since the Epoch."); |
| 4738 | pw.println(" set-timezone TZ"); |
| 4739 | pw.println(" Set the system timezone to TZ where TZ is an Olson id."); |
| 4740 | } |
| 4741 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4742 | } |