Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.usage; |
| 18 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 19 | import static android.app.usage.UsageStatsManager.REASON_MAIN_DEFAULT; |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 20 | import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_SYSTEM; |
| 21 | import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_USER; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 22 | import static android.app.usage.UsageStatsManager.REASON_MAIN_MASK; |
| 23 | import static android.app.usage.UsageStatsManager.REASON_MAIN_PREDICTED; |
| 24 | import static android.app.usage.UsageStatsManager.REASON_MAIN_TIMEOUT; |
| 25 | import static android.app.usage.UsageStatsManager.REASON_MAIN_USAGE; |
Kweku Adams | 917f8a4 | 2020-02-26 17:18:03 -0800 | [diff] [blame] | 26 | import static android.app.usage.UsageStatsManager.REASON_SUB_DEFAULT_APP_UPDATE; |
| 27 | import static android.app.usage.UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_BUGGY; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 28 | import static android.app.usage.UsageStatsManager.REASON_SUB_MASK; |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 29 | import static android.app.usage.UsageStatsManager.REASON_SUB_PREDICTED_RESTORED; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 30 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_ACTIVE_TIMEOUT; |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 31 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_EXEMPTED_SYNC_SCHEDULED_DOZE; |
| 32 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_EXEMPTED_SYNC_SCHEDULED_NON_DOZE; |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 33 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_EXEMPTED_SYNC_START; |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 34 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_FOREGROUND_SERVICE_START; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 35 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_MOVE_TO_BACKGROUND; |
| 36 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_MOVE_TO_FOREGROUND; |
| 37 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_NOTIFICATION_SEEN; |
Kweku Adams | 1e8947c | 2018-11-05 18:06:13 -0800 | [diff] [blame] | 38 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_SLICE_PINNED; |
| 39 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_SLICE_PINNED_PRIV; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 40 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_SYNC_ADAPTER; |
| 41 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_SYSTEM_INTERACTION; |
| 42 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_SYSTEM_UPDATE; |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 43 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_UNEXEMPTED_SYNC_SCHEDULED; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 44 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_USER_INTERACTION; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 45 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_ACTIVE; |
Amith Yamasani | 5a8cc72 | 2017-12-06 10:26:10 -0800 | [diff] [blame] | 46 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 47 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_FREQUENT; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 48 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_NEVER; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 49 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RARE; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 50 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RESTRICTED; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 51 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_WORKING_SET; |
Kweku Adams | 1e8947c | 2018-11-05 18:06:13 -0800 | [diff] [blame] | 52 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 53 | import static com.android.server.SystemService.PHASE_BOOT_COMPLETED; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 54 | import static com.android.server.SystemService.PHASE_SYSTEM_SERVICES_READY; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 55 | |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 56 | import android.annotation.NonNull; |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 57 | import android.annotation.Nullable; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 58 | import android.annotation.UserIdInt; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 59 | import android.app.ActivityManager; |
| 60 | import android.app.AppGlobals; |
Suprabh Shukla | 868bde2 | 2018-02-20 20:59:52 -0800 | [diff] [blame] | 61 | import android.app.usage.AppStandbyInfo; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 62 | import android.app.usage.UsageEvents; |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 63 | import android.app.usage.UsageStatsManager.StandbyBuckets; |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 64 | import android.app.usage.UsageStatsManager.SystemForcedReasons; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 65 | import android.appwidget.AppWidgetManager; |
| 66 | import android.content.BroadcastReceiver; |
| 67 | import android.content.ContentResolver; |
| 68 | import android.content.Context; |
| 69 | import android.content.Intent; |
| 70 | import android.content.IntentFilter; |
| 71 | import android.content.pm.ApplicationInfo; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 72 | import android.content.pm.CrossProfileAppsInternal; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 73 | import android.content.pm.PackageInfo; |
| 74 | import android.content.pm.PackageManager; |
| 75 | import android.content.pm.PackageManagerInternal; |
| 76 | import android.content.pm.ParceledListSlice; |
| 77 | import android.database.ContentObserver; |
| 78 | import android.hardware.display.DisplayManager; |
| 79 | import android.net.NetworkScoreManager; |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 80 | import android.os.BatteryManager; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 81 | import android.os.BatteryStats; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 82 | import android.os.Build; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 83 | import android.os.Environment; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 84 | import android.os.Handler; |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 85 | import android.os.IDeviceIdleController; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 86 | import android.os.Looper; |
| 87 | import android.os.Message; |
| 88 | import android.os.PowerManager; |
| 89 | import android.os.Process; |
| 90 | import android.os.RemoteException; |
| 91 | import android.os.ServiceManager; |
| 92 | import android.os.SystemClock; |
Amith Yamasani | 62b1991 | 2020-05-29 15:20:18 -0700 | [diff] [blame] | 93 | import android.os.Trace; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 94 | import android.os.UserHandle; |
Lei Yu | 4b976ad | 2018-04-19 10:38:58 -0700 | [diff] [blame] | 95 | import android.provider.Settings.Global; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 96 | import android.telephony.TelephonyManager; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 97 | import android.util.ArraySet; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 98 | import android.util.KeyValueListParser; |
| 99 | import android.util.Slog; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 100 | import android.util.SparseArray; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 101 | import android.util.SparseIntArray; |
| 102 | import android.util.TimeUtils; |
| 103 | import android.view.Display; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 104 | import android.widget.Toast; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 105 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 106 | import com.android.internal.R; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 107 | import com.android.internal.annotations.GuardedBy; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 108 | import com.android.internal.annotations.VisibleForTesting; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 109 | import com.android.internal.app.IBatteryStats; |
| 110 | import com.android.internal.os.SomeArgs; |
| 111 | import com.android.internal.util.ArrayUtils; |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 112 | import com.android.internal.util.ConcurrentUtils; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 113 | import com.android.internal.util.IndentingPrintWriter; |
| 114 | import com.android.server.LocalServices; |
Kweku Adams | 259403e | 2020-04-27 09:25:07 -0700 | [diff] [blame] | 115 | import com.android.server.pm.parsing.pkg.AndroidPackage; |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 116 | import com.android.server.usage.AppIdleHistory.AppUsageHistory; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 117 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 118 | import java.io.File; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 119 | import java.io.PrintWriter; |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 120 | import java.time.Duration; |
| 121 | import java.time.format.DateTimeParseException; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 122 | import java.util.ArrayList; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 123 | import java.util.Arrays; |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 124 | import java.util.Collections; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 125 | import java.util.List; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 126 | import java.util.Set; |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 127 | import java.util.concurrent.CountDownLatch; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Manages the standby state of an app, listening to various events. |
Makoto Onuki | a0058b4 | 2018-05-22 16:32:23 -0700 | [diff] [blame] | 131 | * |
| 132 | * Unit test: |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 133 | atest com.android.server.usage.AppStandbyControllerTests |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 134 | */ |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 135 | public class AppStandbyController implements AppStandbyInternal { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 136 | |
| 137 | private static final String TAG = "AppStandbyController"; |
Kweku Adams | a52d533 | 2020-03-04 10:37:57 -0800 | [diff] [blame] | 138 | // Do not submit with true. |
| 139 | static final boolean DEBUG = false; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 140 | |
| 141 | static final boolean COMPRESS_TIME = false; |
| 142 | private static final long ONE_MINUTE = 60 * 1000; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 143 | private static final long ONE_HOUR = ONE_MINUTE * 60; |
| 144 | private static final long ONE_DAY = ONE_HOUR * 24; |
| 145 | |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 146 | /** |
| 147 | * The minimum amount of time the screen must have been on before an app can time out from its |
| 148 | * current bucket to the next bucket. |
| 149 | */ |
| 150 | private static final long[] SCREEN_TIME_THRESHOLDS = { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 151 | 0, |
| 152 | 0, |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 153 | COMPRESS_TIME ? 2 * ONE_MINUTE : 1 * ONE_HOUR, |
| 154 | COMPRESS_TIME ? 4 * ONE_MINUTE : 2 * ONE_HOUR, |
| 155 | COMPRESS_TIME ? 8 * ONE_MINUTE : 6 * ONE_HOUR |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 158 | /** The minimum allowed values for each index in {@link #SCREEN_TIME_THRESHOLDS}. */ |
| 159 | private static final long[] MINIMUM_SCREEN_TIME_THRESHOLDS = COMPRESS_TIME |
| 160 | ? new long[SCREEN_TIME_THRESHOLDS.length] |
| 161 | : new long[]{ |
| 162 | 0, |
| 163 | 0, |
| 164 | 0, |
| 165 | 30 * ONE_MINUTE, |
| 166 | ONE_HOUR |
| 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * The minimum amount of elapsed time that must have passed before an app can time out from its |
| 171 | * current bucket to the next bucket. |
| 172 | */ |
| 173 | private static final long[] ELAPSED_TIME_THRESHOLDS = { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 174 | 0, |
| 175 | COMPRESS_TIME ? 1 * ONE_MINUTE : 12 * ONE_HOUR, |
Amith Yamasani | 301e94a | 2017-11-17 16:35:44 -0800 | [diff] [blame] | 176 | COMPRESS_TIME ? 4 * ONE_MINUTE : 24 * ONE_HOUR, |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 177 | COMPRESS_TIME ? 16 * ONE_MINUTE : 48 * ONE_HOUR, |
Kweku Adams | 23aeb08 | 2020-04-13 08:34:36 -0700 | [diff] [blame] | 178 | COMPRESS_TIME ? 32 * ONE_MINUTE : 30 * ONE_DAY |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 181 | /** The minimum allowed values for each index in {@link #ELAPSED_TIME_THRESHOLDS}. */ |
| 182 | private static final long[] MINIMUM_ELAPSED_TIME_THRESHOLDS = COMPRESS_TIME |
| 183 | ? new long[ELAPSED_TIME_THRESHOLDS.length] |
| 184 | : new long[]{ |
| 185 | 0, |
| 186 | ONE_HOUR, |
| 187 | ONE_HOUR, |
| 188 | 2 * ONE_HOUR, |
| 189 | 4 * ONE_DAY |
| 190 | }; |
| 191 | |
| 192 | private static final int[] THRESHOLD_BUCKETS = { |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 193 | STANDBY_BUCKET_ACTIVE, |
| 194 | STANDBY_BUCKET_WORKING_SET, |
| 195 | STANDBY_BUCKET_FREQUENT, |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 196 | STANDBY_BUCKET_RARE, |
| 197 | STANDBY_BUCKET_RESTRICTED |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 198 | }; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 199 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 200 | /** Default expiration time for bucket prediction. After this, use thresholds to downgrade. */ |
| 201 | private static final long DEFAULT_PREDICTION_TIMEOUT = 12 * ONE_HOUR; |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 202 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 203 | /** |
| 204 | * Indicates the maximum wait time for admin data to be available; |
| 205 | */ |
| 206 | private static final long WAIT_FOR_ADMIN_DATA_TIMEOUT_MS = 10_000; |
| 207 | |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 208 | private static final int HEADLESS_APP_CHECK_FLAGS = |
| 209 | PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 210 | | PackageManager.GET_ACTIVITIES | PackageManager.MATCH_DISABLED_COMPONENTS; |
| 211 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 212 | // To name the lock for stack traces |
| 213 | static class Lock {} |
| 214 | |
| 215 | /** Lock to protect the app's standby state. Required for calls into AppIdleHistory */ |
| 216 | private final Object mAppIdleLock = new Lock(); |
| 217 | |
| 218 | /** Keeps the history and state for each app. */ |
| 219 | @GuardedBy("mAppIdleLock") |
| 220 | private AppIdleHistory mAppIdleHistory; |
| 221 | |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 222 | @GuardedBy("mPackageAccessListeners") |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 223 | private final ArrayList<AppIdleStateChangeListener> mPackageAccessListeners = new ArrayList<>(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 224 | |
| 225 | /** Whether we've queried the list of carrier privileged apps. */ |
| 226 | @GuardedBy("mAppIdleLock") |
| 227 | private boolean mHaveCarrierPrivilegedApps; |
| 228 | |
| 229 | /** List of carrier-privileged apps that should be excluded from standby */ |
| 230 | @GuardedBy("mAppIdleLock") |
| 231 | private List<String> mCarrierPrivilegedApps; |
| 232 | |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 233 | @GuardedBy("mActiveAdminApps") |
| 234 | private final SparseArray<Set<String>> mActiveAdminApps = new SparseArray<>(); |
| 235 | |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 236 | /** |
| 237 | * Set of system apps that are headless (don't have any declared activities, enabled or |
| 238 | * disabled). Presence in this map indicates that the app is a headless system app. |
| 239 | */ |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 240 | @GuardedBy("mHeadlessSystemApps") |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 241 | private final ArraySet<String> mHeadlessSystemApps = new ArraySet<>(); |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 242 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 243 | private final CountDownLatch mAdminDataAvailableLatch = new CountDownLatch(1); |
| 244 | |
Amith Yamasani | 62b1991 | 2020-05-29 15:20:18 -0700 | [diff] [blame] | 245 | // Cache the active network scorer queried from the network scorer service |
| 246 | private volatile String mCachedNetworkScorer = null; |
| 247 | // The last time the network scorer service was queried |
| 248 | private volatile long mCachedNetworkScorerAtMillis = 0L; |
| 249 | // How long before querying the network scorer again. During this time, subsequent queries will |
| 250 | // get the cached value |
| 251 | private static final long NETWORK_SCORER_CACHE_DURATION_MILLIS = 5000L; |
| 252 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 253 | // Messages for the handler |
| 254 | static final int MSG_INFORM_LISTENERS = 3; |
| 255 | static final int MSG_FORCE_IDLE_STATE = 4; |
| 256 | static final int MSG_CHECK_IDLE_STATES = 5; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 257 | static final int MSG_REPORT_CONTENT_PROVIDER_USAGE = 8; |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 258 | static final int MSG_PAROLE_STATE_CHANGED = 9; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 259 | static final int MSG_ONE_TIME_CHECK_IDLE_STATES = 10; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 260 | /** Check the state of one app: arg1 = userId, arg2 = uid, obj = (String) packageName */ |
| 261 | static final int MSG_CHECK_PACKAGE_IDLE_STATE = 11; |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 262 | static final int MSG_REPORT_SYNC_SCHEDULED = 12; |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 263 | static final int MSG_REPORT_EXEMPTED_SYNC_START = 13; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 264 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 265 | long mCheckIdleIntervalMillis; |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 266 | /** |
| 267 | * The minimum amount of time the screen must have been on before an app can time out from its |
| 268 | * current bucket to the next bucket. |
| 269 | */ |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 270 | long[] mAppStandbyScreenThresholds = SCREEN_TIME_THRESHOLDS; |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 271 | /** |
| 272 | * The minimum amount of elapsed time that must have passed before an app can time out from its |
| 273 | * current bucket to the next bucket. |
| 274 | */ |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 275 | long[] mAppStandbyElapsedThresholds = ELAPSED_TIME_THRESHOLDS; |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 276 | /** Minimum time a strong usage event should keep the bucket elevated. */ |
| 277 | long mStrongUsageTimeoutMillis; |
| 278 | /** Minimum time a notification seen event should keep the bucket elevated. */ |
| 279 | long mNotificationSeenTimeoutMillis; |
| 280 | /** Minimum time a system update event should keep the buckets elevated. */ |
| 281 | long mSystemUpdateUsageTimeoutMillis; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 282 | /** Maximum time to wait for a prediction before using simple timeouts to downgrade buckets. */ |
| 283 | long mPredictionTimeoutMillis; |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 284 | /** Maximum time a sync adapter associated with a CP should keep the buckets elevated. */ |
| 285 | long mSyncAdapterTimeoutMillis; |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 286 | /** |
| 287 | * Maximum time an exempted sync should keep the buckets elevated, when sync is scheduled in |
| 288 | * non-doze |
| 289 | */ |
| 290 | long mExemptedSyncScheduledNonDozeTimeoutMillis; |
| 291 | /** |
| 292 | * Maximum time an exempted sync should keep the buckets elevated, when sync is scheduled in |
| 293 | * doze |
| 294 | */ |
| 295 | long mExemptedSyncScheduledDozeTimeoutMillis; |
| 296 | /** |
| 297 | * Maximum time an exempted sync should keep the buckets elevated, when sync is started. |
| 298 | */ |
| 299 | long mExemptedSyncStartTimeoutMillis; |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 300 | /** |
| 301 | * Maximum time an unexempted sync should keep the buckets elevated, when sync is scheduled |
| 302 | */ |
| 303 | long mUnexemptedSyncScheduledTimeoutMillis; |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 304 | /** Maximum time a system interaction should keep the buckets elevated. */ |
| 305 | long mSystemInteractionTimeoutMillis; |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Maximum time a foreground service start should keep the buckets elevated if the service |
| 308 | * start is the first usage of the app |
| 309 | */ |
| 310 | long mInitialForegroundServiceStartTimeoutMillis; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 311 | /** |
| 312 | * User usage that would elevate an app's standby bucket will also elevate the standby bucket of |
| 313 | * cross profile connected apps. Explicit standby bucket setting via |
| 314 | * {@link #setAppStandbyBucket(String, int, int, int, int)} will not be propagated. |
| 315 | */ |
| 316 | boolean mLinkCrossProfileApps; |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Whether we should allow apps into the |
| 319 | * {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED} bucket or not. |
| 320 | * If false, any attempts to put an app into the bucket will put the app into the |
| 321 | * {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RARE} bucket instead. |
| 322 | */ |
| 323 | private boolean mAllowRestrictedBucket; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 324 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 325 | private volatile boolean mAppIdleEnabled; |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 326 | private boolean mIsCharging; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 327 | private boolean mSystemServicesReady = false; |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 328 | // There was a system update, defaults need to be initialized after services are ready |
| 329 | private boolean mPendingInitializeDefaults; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 330 | |
| 331 | private volatile boolean mPendingOneTimeCheckIdleStates; |
| 332 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 333 | private final AppStandbyHandler mHandler; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 334 | private final Context mContext; |
| 335 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 336 | private AppWidgetManager mAppWidgetManager; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 337 | private PackageManager mPackageManager; |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 338 | Injector mInjector; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 339 | |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 340 | static final ArrayList<StandbyUpdateRecord> sStandbyUpdatePool = new ArrayList<>(4); |
| 341 | |
| 342 | public static class StandbyUpdateRecord { |
| 343 | // Identity of the app whose standby state has changed |
| 344 | String packageName; |
| 345 | int userId; |
| 346 | |
| 347 | // What the standby bucket the app is now in |
| 348 | int bucket; |
| 349 | |
| 350 | // Whether the bucket change is because the user has started interacting with the app |
| 351 | boolean isUserInteraction; |
| 352 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 353 | // Reason for bucket change |
| 354 | int reason; |
| 355 | |
| 356 | StandbyUpdateRecord(String pkgName, int userId, int bucket, int reason, |
| 357 | boolean isInteraction) { |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 358 | this.packageName = pkgName; |
| 359 | this.userId = userId; |
| 360 | this.bucket = bucket; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 361 | this.reason = reason; |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 362 | this.isUserInteraction = isInteraction; |
| 363 | } |
| 364 | |
| 365 | public static StandbyUpdateRecord obtain(String pkgName, int userId, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 366 | int bucket, int reason, boolean isInteraction) { |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 367 | synchronized (sStandbyUpdatePool) { |
| 368 | final int size = sStandbyUpdatePool.size(); |
| 369 | if (size < 1) { |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 370 | return new StandbyUpdateRecord(pkgName, userId, bucket, reason, isInteraction); |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 371 | } |
| 372 | StandbyUpdateRecord r = sStandbyUpdatePool.remove(size - 1); |
| 373 | r.packageName = pkgName; |
| 374 | r.userId = userId; |
| 375 | r.bucket = bucket; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 376 | r.reason = reason; |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 377 | r.isUserInteraction = isInteraction; |
| 378 | return r; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | public void recycle() { |
| 383 | synchronized (sStandbyUpdatePool) { |
| 384 | sStandbyUpdatePool.add(this); |
| 385 | } |
| 386 | } |
| 387 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 388 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 389 | public AppStandbyController(Context context, Looper looper) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 390 | this(new Injector(context, looper)); |
| 391 | } |
| 392 | |
| 393 | AppStandbyController(Injector injector) { |
| 394 | mInjector = injector; |
| 395 | mContext = mInjector.getContext(); |
| 396 | mHandler = new AppStandbyHandler(mInjector.getLooper()); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 397 | mPackageManager = mContext.getPackageManager(); |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 398 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 399 | DeviceStateReceiver deviceStateReceiver = new DeviceStateReceiver(); |
| 400 | IntentFilter deviceStates = new IntentFilter(BatteryManager.ACTION_CHARGING); |
| 401 | deviceStates.addAction(BatteryManager.ACTION_DISCHARGING); |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 402 | deviceStates.addAction(PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 403 | mContext.registerReceiver(deviceStateReceiver, deviceStates); |
| 404 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 405 | synchronized (mAppIdleLock) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 406 | mAppIdleHistory = new AppIdleHistory(mInjector.getDataSystemDirectory(), |
| 407 | mInjector.elapsedRealtime()); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | IntentFilter packageFilter = new IntentFilter(); |
| 411 | packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 412 | packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 413 | packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 414 | packageFilter.addDataScheme("package"); |
| 415 | |
| 416 | mContext.registerReceiverAsUser(new PackageReceiver(), UserHandle.ALL, packageFilter, |
| 417 | null, mHandler); |
| 418 | } |
| 419 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 420 | @VisibleForTesting |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 421 | void setAppIdleEnabled(boolean enabled) { |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 422 | synchronized (mAppIdleLock) { |
| 423 | if (mAppIdleEnabled != enabled) { |
| 424 | final boolean oldParoleState = isInParole(); |
| 425 | mAppIdleEnabled = enabled; |
| 426 | if (isInParole() != oldParoleState) { |
| 427 | postParoleStateChanged(); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 434 | @Override |
| 435 | public boolean isAppIdleEnabled() { |
| 436 | return mAppIdleEnabled; |
| 437 | } |
| 438 | |
| 439 | @Override |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 440 | public void onBootPhase(int phase) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 441 | mInjector.onBootPhase(phase); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 442 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 443 | Slog.d(TAG, "Setting app idle enabled state"); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 444 | // Observe changes to the threshold |
| 445 | SettingsObserver settingsObserver = new SettingsObserver(mHandler); |
| 446 | settingsObserver.registerObserver(); |
| 447 | settingsObserver.updateSettings(); |
| 448 | |
| 449 | mAppWidgetManager = mContext.getSystemService(AppWidgetManager.class); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 450 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 451 | mInjector.registerDisplayListener(mDisplayListener, mHandler); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 452 | synchronized (mAppIdleLock) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 453 | mAppIdleHistory.updateDisplay(isDisplayOn(), mInjector.elapsedRealtime()); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 456 | mSystemServicesReady = true; |
| 457 | |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 458 | // Offload to handler thread to avoid boot time impact. |
| 459 | mHandler.post(mInjector::updatePowerWhitelistCache); |
| 460 | |
Michael Wachenschwanz | d1d8aa6 | 2019-02-28 16:38:37 -0800 | [diff] [blame] | 461 | boolean userFileExists; |
| 462 | synchronized (mAppIdleLock) { |
| 463 | userFileExists = mAppIdleHistory.userFileExists(UserHandle.USER_SYSTEM); |
| 464 | } |
| 465 | |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 466 | // Offload to handler thread to avoid boottime impact. |
| 467 | mHandler.post(this::loadHeadlessSystemAppCache); |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 468 | |
Michael Wachenschwanz | d1d8aa6 | 2019-02-28 16:38:37 -0800 | [diff] [blame] | 469 | if (mPendingInitializeDefaults || !userFileExists) { |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 470 | initializeDefaultsForSystemApps(UserHandle.USER_SYSTEM); |
| 471 | } |
| 472 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 473 | if (mPendingOneTimeCheckIdleStates) { |
| 474 | postOneTimeCheckIdleStates(); |
| 475 | } |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 476 | } else if (phase == PHASE_BOOT_COMPLETED) { |
| 477 | setChargingState(mInjector.isCharging()); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 481 | private void reportContentProviderUsage(String authority, String providerPkgName, int userId) { |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 482 | if (!mAppIdleEnabled) return; |
| 483 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 484 | // Get sync adapters for the authority |
| 485 | String[] packages = ContentResolver.getSyncAdapterPackagesForAuthorityAsUser( |
| 486 | authority, userId); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 487 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 488 | for (String packageName: packages) { |
| 489 | // Only force the sync adapters to active if the provider is not in the same package and |
| 490 | // the sync adapter is a system package. |
| 491 | try { |
| 492 | PackageInfo pi = mPackageManager.getPackageInfoAsUser( |
| 493 | packageName, PackageManager.MATCH_SYSTEM_ONLY, userId); |
| 494 | if (pi == null || pi.applicationInfo == null) { |
| 495 | continue; |
| 496 | } |
| 497 | if (!packageName.equals(providerPkgName)) { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 498 | final List<UserHandle> linkedProfiles = getCrossProfileTargets(packageName, |
| 499 | userId); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 500 | synchronized (mAppIdleLock) { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 501 | reportNoninteractiveUsageCrossUserLocked(packageName, userId, |
| 502 | STANDBY_BUCKET_ACTIVE, REASON_SUB_USAGE_SYNC_ADAPTER, |
| 503 | elapsedRealtime, mSyncAdapterTimeoutMillis, linkedProfiles); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 504 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 505 | } |
| 506 | } catch (PackageManager.NameNotFoundException e) { |
| 507 | // Shouldn't happen |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 512 | private void reportExemptedSyncScheduled(String packageName, int userId) { |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 513 | if (!mAppIdleEnabled) return; |
| 514 | |
| 515 | final int bucketToPromote; |
| 516 | final int usageReason; |
| 517 | final long durationMillis; |
| 518 | |
| 519 | if (!mInjector.isDeviceIdleMode()) { |
| 520 | // Not dozing. |
| 521 | bucketToPromote = STANDBY_BUCKET_ACTIVE; |
| 522 | usageReason = REASON_SUB_USAGE_EXEMPTED_SYNC_SCHEDULED_NON_DOZE; |
| 523 | durationMillis = mExemptedSyncScheduledNonDozeTimeoutMillis; |
| 524 | } else { |
| 525 | // Dozing. |
| 526 | bucketToPromote = STANDBY_BUCKET_WORKING_SET; |
| 527 | usageReason = REASON_SUB_USAGE_EXEMPTED_SYNC_SCHEDULED_DOZE; |
| 528 | durationMillis = mExemptedSyncScheduledDozeTimeoutMillis; |
| 529 | } |
| 530 | |
| 531 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 532 | final List<UserHandle> linkedProfiles = getCrossProfileTargets(packageName, userId); |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 533 | synchronized (mAppIdleLock) { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 534 | reportNoninteractiveUsageCrossUserLocked(packageName, userId, bucketToPromote, |
| 535 | usageReason, elapsedRealtime, durationMillis, linkedProfiles); |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 539 | private void reportUnexemptedSyncScheduled(String packageName, int userId) { |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 540 | if (!mAppIdleEnabled) return; |
| 541 | |
| 542 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
| 543 | synchronized (mAppIdleLock) { |
| 544 | final int currentBucket = |
| 545 | mAppIdleHistory.getAppStandbyBucket(packageName, userId, elapsedRealtime); |
| 546 | if (currentBucket == STANDBY_BUCKET_NEVER) { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 547 | final List<UserHandle> linkedProfiles = getCrossProfileTargets(packageName, userId); |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 548 | // Bring the app out of the never bucket |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 549 | reportNoninteractiveUsageCrossUserLocked(packageName, userId, |
| 550 | STANDBY_BUCKET_WORKING_SET, REASON_SUB_USAGE_UNEXEMPTED_SYNC_SCHEDULED, |
| 551 | elapsedRealtime, mUnexemptedSyncScheduledTimeoutMillis, linkedProfiles); |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 556 | private void reportExemptedSyncStart(String packageName, int userId) { |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 557 | if (!mAppIdleEnabled) return; |
| 558 | |
| 559 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 560 | final List<UserHandle> linkedProfiles = getCrossProfileTargets(packageName, userId); |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 561 | synchronized (mAppIdleLock) { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 562 | reportNoninteractiveUsageCrossUserLocked(packageName, userId, STANDBY_BUCKET_ACTIVE, |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 563 | REASON_SUB_USAGE_EXEMPTED_SYNC_START, elapsedRealtime, |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 564 | mExemptedSyncStartTimeoutMillis, linkedProfiles); |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 568 | /** |
| 569 | * Helper method to report indirect user usage of an app and handle reporting the usage |
| 570 | * against cross profile connected apps. <br> |
| 571 | * Use {@link #reportNoninteractiveUsageLocked(String, int, int, int, long, long)} if |
| 572 | * cross profile connected apps do not need to be handled. |
| 573 | */ |
| 574 | private void reportNoninteractiveUsageCrossUserLocked(String packageName, int userId, |
| 575 | int bucket, int subReason, long elapsedRealtime, long nextCheckDelay, |
| 576 | List<UserHandle> otherProfiles) { |
| 577 | reportNoninteractiveUsageLocked(packageName, userId, bucket, subReason, elapsedRealtime, |
| 578 | nextCheckDelay); |
| 579 | final int size = otherProfiles.size(); |
| 580 | for (int profileIndex = 0; profileIndex < size; profileIndex++) { |
| 581 | final int otherUserId = otherProfiles.get(profileIndex).getIdentifier(); |
| 582 | reportNoninteractiveUsageLocked(packageName, otherUserId, bucket, subReason, |
| 583 | elapsedRealtime, nextCheckDelay); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Helper method to report indirect user usage of an app. <br> |
| 589 | * Use |
| 590 | * {@link #reportNoninteractiveUsageCrossUserLocked(String, int, int, int, long, long, List)} |
| 591 | * if cross profile connected apps need to be handled. |
| 592 | */ |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 593 | private void reportNoninteractiveUsageLocked(String packageName, int userId, int bucket, |
| 594 | int subReason, long elapsedRealtime, long nextCheckDelay) { |
| 595 | final AppUsageHistory appUsage = mAppIdleHistory.reportUsage(packageName, userId, bucket, |
| 596 | subReason, 0, elapsedRealtime + nextCheckDelay); |
| 597 | mHandler.sendMessageDelayed( |
| 598 | mHandler.obtainMessage(MSG_CHECK_PACKAGE_IDLE_STATE, userId, -1, packageName), |
| 599 | nextCheckDelay); |
| 600 | maybeInformListeners(packageName, userId, elapsedRealtime, appUsage.currentBucket, |
| 601 | appUsage.bucketingReason, false); |
| 602 | } |
| 603 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 604 | @VisibleForTesting |
| 605 | void setChargingState(boolean isCharging) { |
| 606 | synchronized (mAppIdleLock) { |
| 607 | if (mIsCharging != isCharging) { |
| 608 | if (DEBUG) Slog.d(TAG, "Setting mIsCharging to " + isCharging); |
| 609 | mIsCharging = isCharging; |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 610 | postParoleStateChanged(); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 615 | @Override |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 616 | public boolean isInParole() { |
| 617 | return !mAppIdleEnabled || mIsCharging; |
| 618 | } |
| 619 | |
| 620 | private void postParoleStateChanged() { |
| 621 | if (DEBUG) Slog.d(TAG, "Posting MSG_PAROLE_STATE_CHANGED"); |
| 622 | mHandler.removeMessages(MSG_PAROLE_STATE_CHANGED); |
| 623 | mHandler.sendEmptyMessage(MSG_PAROLE_STATE_CHANGED); |
| 624 | } |
| 625 | |
| 626 | @Override |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 627 | public void postCheckIdleStates(int userId) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 628 | mHandler.sendMessage(mHandler.obtainMessage(MSG_CHECK_IDLE_STATES, userId, 0)); |
| 629 | } |
| 630 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 631 | @Override |
| 632 | public void postOneTimeCheckIdleStates() { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 633 | if (mInjector.getBootPhase() < PHASE_SYSTEM_SERVICES_READY) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 634 | // Not booted yet; wait for it! |
| 635 | mPendingOneTimeCheckIdleStates = true; |
| 636 | } else { |
| 637 | mHandler.sendEmptyMessage(MSG_ONE_TIME_CHECK_IDLE_STATES); |
| 638 | mPendingOneTimeCheckIdleStates = false; |
| 639 | } |
| 640 | } |
| 641 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 642 | @VisibleForTesting |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 643 | boolean checkIdleStates(int checkUserId) { |
| 644 | if (!mAppIdleEnabled) { |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | final int[] runningUserIds; |
| 649 | try { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 650 | runningUserIds = mInjector.getRunningUserIds(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 651 | if (checkUserId != UserHandle.USER_ALL |
| 652 | && !ArrayUtils.contains(runningUserIds, checkUserId)) { |
| 653 | return false; |
| 654 | } |
| 655 | } catch (RemoteException re) { |
| 656 | throw re.rethrowFromSystemServer(); |
| 657 | } |
| 658 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 659 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 660 | for (int i = 0; i < runningUserIds.length; i++) { |
| 661 | final int userId = runningUserIds[i]; |
| 662 | if (checkUserId != UserHandle.USER_ALL && checkUserId != userId) { |
| 663 | continue; |
| 664 | } |
| 665 | if (DEBUG) { |
| 666 | Slog.d(TAG, "Checking idle state for user " + userId); |
| 667 | } |
| 668 | List<PackageInfo> packages = mPackageManager.getInstalledPackagesAsUser( |
| 669 | PackageManager.MATCH_DISABLED_COMPONENTS, |
| 670 | userId); |
| 671 | final int packageCount = packages.size(); |
| 672 | for (int p = 0; p < packageCount; p++) { |
| 673 | final PackageInfo pi = packages.get(p); |
| 674 | final String packageName = pi.packageName; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 675 | checkAndUpdateStandbyState(packageName, userId, pi.applicationInfo.uid, |
| 676 | elapsedRealtime); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | if (DEBUG) { |
| 680 | Slog.d(TAG, "checkIdleStates took " |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 681 | + (mInjector.elapsedRealtime() - elapsedRealtime)); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 682 | } |
| 683 | return true; |
| 684 | } |
| 685 | |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 686 | /** Check if we need to update the standby state of a specific app. */ |
| 687 | private void checkAndUpdateStandbyState(String packageName, @UserIdInt int userId, |
| 688 | int uid, long elapsedRealtime) { |
| 689 | if (uid <= 0) { |
| 690 | try { |
| 691 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 692 | } catch (PackageManager.NameNotFoundException e) { |
| 693 | // Not a valid package for this user, nothing to do |
| 694 | // TODO: Remove any history of removed packages |
| 695 | return; |
| 696 | } |
| 697 | } |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 698 | final int minBucket = getAppMinBucket(packageName, |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 699 | UserHandle.getAppId(uid), |
| 700 | userId); |
| 701 | if (DEBUG) { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 702 | Slog.d(TAG, " Checking idle state for " + packageName |
| 703 | + " minBucket=" + minBucket); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 704 | } |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 705 | if (minBucket <= STANDBY_BUCKET_ACTIVE) { |
| 706 | // No extra processing needed for ACTIVE or higher since apps can't drop into lower |
| 707 | // buckets. |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 708 | synchronized (mAppIdleLock) { |
| 709 | mAppIdleHistory.setAppStandbyBucket(packageName, userId, elapsedRealtime, |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 710 | minBucket, REASON_MAIN_DEFAULT); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 711 | } |
| 712 | maybeInformListeners(packageName, userId, elapsedRealtime, |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 713 | minBucket, REASON_MAIN_DEFAULT, false); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 714 | } else { |
| 715 | synchronized (mAppIdleLock) { |
| 716 | final AppIdleHistory.AppUsageHistory app = |
| 717 | mAppIdleHistory.getAppUsageHistory(packageName, |
| 718 | userId, elapsedRealtime); |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 719 | int reason = app.bucketingReason; |
| 720 | final int oldMainReason = reason & REASON_MAIN_MASK; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 721 | |
| 722 | // If the bucket was forced by the user/developer, leave it alone. |
| 723 | // A usage event will be the only way to bring it out of this forced state |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 724 | if (oldMainReason == REASON_MAIN_FORCED_BY_USER) { |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 725 | return; |
| 726 | } |
| 727 | final int oldBucket = app.currentBucket; |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 728 | if (oldBucket == STANDBY_BUCKET_NEVER) { |
| 729 | // None of this should bring an app out of the NEVER bucket. |
| 730 | return; |
| 731 | } |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 732 | int newBucket = Math.max(oldBucket, STANDBY_BUCKET_ACTIVE); // Undo EXEMPTED |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 733 | boolean predictionLate = predictionTimedOut(app, elapsedRealtime); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 734 | // Compute age-based bucket |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 735 | if (oldMainReason == REASON_MAIN_DEFAULT |
| 736 | || oldMainReason == REASON_MAIN_USAGE |
| 737 | || oldMainReason == REASON_MAIN_TIMEOUT |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 738 | || predictionLate) { |
| 739 | |
| 740 | if (!predictionLate && app.lastPredictedBucket >= STANDBY_BUCKET_ACTIVE |
| 741 | && app.lastPredictedBucket <= STANDBY_BUCKET_RARE) { |
| 742 | newBucket = app.lastPredictedBucket; |
| 743 | reason = REASON_MAIN_PREDICTED | REASON_SUB_PREDICTED_RESTORED; |
| 744 | if (DEBUG) { |
| 745 | Slog.d(TAG, "Restored predicted newBucket = " + newBucket); |
| 746 | } |
| 747 | } else { |
| 748 | newBucket = getBucketForLocked(packageName, userId, |
| 749 | elapsedRealtime); |
| 750 | if (DEBUG) { |
| 751 | Slog.d(TAG, "Evaluated AOSP newBucket = " + newBucket); |
| 752 | } |
| 753 | reason = REASON_MAIN_TIMEOUT; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 754 | } |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 755 | } |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 756 | |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 757 | // Check if the app is within one of the timeouts for forced bucket elevation |
| 758 | final long elapsedTimeAdjusted = mAppIdleHistory.getElapsedTime(elapsedRealtime); |
| 759 | if (newBucket >= STANDBY_BUCKET_ACTIVE |
| 760 | && app.bucketActiveTimeoutTime > elapsedTimeAdjusted) { |
| 761 | newBucket = STANDBY_BUCKET_ACTIVE; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 762 | reason = app.bucketingReason; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 763 | if (DEBUG) { |
| 764 | Slog.d(TAG, " Keeping at ACTIVE due to min timeout"); |
| 765 | } |
| 766 | } else if (newBucket >= STANDBY_BUCKET_WORKING_SET |
| 767 | && app.bucketWorkingSetTimeoutTime > elapsedTimeAdjusted) { |
| 768 | newBucket = STANDBY_BUCKET_WORKING_SET; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 769 | // If it was already there, keep the reason, else assume timeout to WS |
| 770 | reason = (newBucket == oldBucket) |
| 771 | ? app.bucketingReason |
| 772 | : REASON_MAIN_USAGE | REASON_SUB_USAGE_ACTIVE_TIMEOUT; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 773 | if (DEBUG) { |
| 774 | Slog.d(TAG, " Keeping at WORKING_SET due to min timeout"); |
| 775 | } |
| 776 | } |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 777 | |
| 778 | if (app.lastRestrictAttemptElapsedTime > app.lastUsedByUserElapsedTime |
| 779 | && elapsedTimeAdjusted - app.lastUsedByUserElapsedTime |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 780 | >= mInjector.getAutoRestrictedBucketDelayMs()) { |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 781 | newBucket = STANDBY_BUCKET_RESTRICTED; |
| 782 | reason = app.lastRestrictReason; |
| 783 | if (DEBUG) { |
| 784 | Slog.d(TAG, "Bringing down to RESTRICTED due to timeout"); |
| 785 | } |
| 786 | } |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 787 | if (newBucket == STANDBY_BUCKET_RESTRICTED && !mAllowRestrictedBucket) { |
| 788 | newBucket = STANDBY_BUCKET_RARE; |
| 789 | // Leave the reason alone. |
| 790 | if (DEBUG) { |
| 791 | Slog.d(TAG, "Bringing up from RESTRICTED to RARE due to off switch"); |
| 792 | } |
| 793 | } |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 794 | if (newBucket > minBucket) { |
| 795 | newBucket = minBucket; |
| 796 | // Leave the reason alone. |
| 797 | if (DEBUG) { |
| 798 | Slog.d(TAG, "Bringing up from " + newBucket + " to " + minBucket |
| 799 | + " due to min bucketing"); |
| 800 | } |
| 801 | } |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 802 | if (DEBUG) { |
| 803 | Slog.d(TAG, " Old bucket=" + oldBucket |
| 804 | + ", newBucket=" + newBucket); |
| 805 | } |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 806 | if (oldBucket != newBucket || predictionLate) { |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 807 | mAppIdleHistory.setAppStandbyBucket(packageName, userId, |
| 808 | elapsedRealtime, newBucket, reason); |
| 809 | maybeInformListeners(packageName, userId, elapsedRealtime, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 810 | newBucket, reason, false); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 816 | /** Returns true if there hasn't been a prediction for the app in a while. */ |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 817 | private boolean predictionTimedOut(AppIdleHistory.AppUsageHistory app, long elapsedRealtime) { |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 818 | return app.lastPredictedTime > 0 |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 819 | && mAppIdleHistory.getElapsedTime(elapsedRealtime) |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 820 | - app.lastPredictedTime > mPredictionTimeoutMillis; |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 821 | } |
| 822 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 823 | /** Inform listeners if the bucket has changed since it was last reported to listeners */ |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 824 | private void maybeInformListeners(String packageName, int userId, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 825 | long elapsedRealtime, int bucket, int reason, boolean userStartedInteracting) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 826 | synchronized (mAppIdleLock) { |
| 827 | if (mAppIdleHistory.shouldInformListeners(packageName, userId, |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 828 | elapsedRealtime, bucket)) { |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 829 | final StandbyUpdateRecord r = StandbyUpdateRecord.obtain(packageName, userId, |
| 830 | bucket, reason, userStartedInteracting); |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 831 | if (DEBUG) Slog.d(TAG, "Standby bucket for " + packageName + "=" + bucket); |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 832 | mHandler.sendMessage(mHandler.obtainMessage(MSG_INFORM_LISTENERS, r)); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 837 | /** |
| 838 | * Evaluates next bucket based on time since last used and the bucketing thresholds. |
| 839 | * @param packageName the app |
| 840 | * @param userId the user |
| 841 | * @param elapsedRealtime as the name suggests, current elapsed time |
| 842 | * @return the bucket for the app, based on time since last used |
| 843 | */ |
Andreas Gampe | bbab23f | 2018-02-07 15:34:27 -0800 | [diff] [blame] | 844 | @GuardedBy("mAppIdleLock") |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 845 | @StandbyBuckets |
| 846 | private int getBucketForLocked(String packageName, int userId, |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 847 | long elapsedRealtime) { |
| 848 | int bucketIndex = mAppIdleHistory.getThresholdIndex(packageName, userId, |
| 849 | elapsedRealtime, mAppStandbyScreenThresholds, mAppStandbyElapsedThresholds); |
| 850 | return THRESHOLD_BUCKETS[bucketIndex]; |
| 851 | } |
| 852 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 853 | private void notifyBatteryStats(String packageName, int userId, boolean idle) { |
| 854 | try { |
| 855 | final int uid = mPackageManager.getPackageUidAsUser(packageName, |
| 856 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 857 | if (idle) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 858 | mInjector.noteEvent(BatteryStats.HistoryItem.EVENT_PACKAGE_INACTIVE, |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 859 | packageName, uid); |
| 860 | } else { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 861 | mInjector.noteEvent(BatteryStats.HistoryItem.EVENT_PACKAGE_ACTIVE, |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 862 | packageName, uid); |
| 863 | } |
| 864 | } catch (PackageManager.NameNotFoundException | RemoteException e) { |
| 865 | } |
| 866 | } |
| 867 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 868 | @Override |
Michael Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 869 | public void reportEvent(UsageEvents.Event event, int userId) { |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 870 | if (!mAppIdleEnabled) return; |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 871 | final int eventType = event.getEventType(); |
| 872 | if ((eventType == UsageEvents.Event.ACTIVITY_RESUMED |
| 873 | || eventType == UsageEvents.Event.ACTIVITY_PAUSED |
| 874 | || eventType == UsageEvents.Event.SYSTEM_INTERACTION |
| 875 | || eventType == UsageEvents.Event.USER_INTERACTION |
| 876 | || eventType == UsageEvents.Event.NOTIFICATION_SEEN |
| 877 | || eventType == UsageEvents.Event.SLICE_PINNED |
| 878 | || eventType == UsageEvents.Event.SLICE_PINNED_PRIV |
| 879 | || eventType == UsageEvents.Event.FOREGROUND_SERVICE_START)) { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 880 | final String pkg = event.getPackageName(); |
| 881 | final List<UserHandle> linkedProfiles = getCrossProfileTargets(pkg, userId); |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 882 | synchronized (mAppIdleLock) { |
Michael Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 883 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 884 | reportEventLocked(pkg, eventType, elapsedRealtime, userId); |
| 885 | |
| 886 | final int size = linkedProfiles.size(); |
| 887 | for (int profileIndex = 0; profileIndex < size; profileIndex++) { |
| 888 | final int linkedUserId = linkedProfiles.get(profileIndex).getIdentifier(); |
| 889 | reportEventLocked(pkg, eventType, elapsedRealtime, linkedUserId); |
| 890 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 891 | } |
| 892 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 895 | private void reportEventLocked(String pkg, int eventType, long elapsedRealtime, int userId) { |
| 896 | // TODO: Ideally this should call isAppIdleFiltered() to avoid calling back |
| 897 | // about apps that are on some kind of whitelist anyway. |
| 898 | final boolean previouslyIdle = mAppIdleHistory.isIdle( |
| 899 | pkg, userId, elapsedRealtime); |
| 900 | |
| 901 | final AppUsageHistory appHistory = mAppIdleHistory.getAppUsageHistory( |
| 902 | pkg, userId, elapsedRealtime); |
| 903 | final int prevBucket = appHistory.currentBucket; |
| 904 | final int prevBucketReason = appHistory.bucketingReason; |
| 905 | final long nextCheckDelay; |
| 906 | final int subReason = usageEventToSubReason(eventType); |
| 907 | final int reason = REASON_MAIN_USAGE | subReason; |
| 908 | if (eventType == UsageEvents.Event.NOTIFICATION_SEEN |
| 909 | || eventType == UsageEvents.Event.SLICE_PINNED) { |
| 910 | // Mild usage elevates to WORKING_SET but doesn't change usage time. |
Kweku Adams | 1275213 | 2020-02-18 15:36:48 -0800 | [diff] [blame] | 911 | mAppIdleHistory.reportUsage(appHistory, pkg, userId, |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 912 | STANDBY_BUCKET_WORKING_SET, subReason, |
| 913 | 0, elapsedRealtime + mNotificationSeenTimeoutMillis); |
| 914 | nextCheckDelay = mNotificationSeenTimeoutMillis; |
| 915 | } else if (eventType == UsageEvents.Event.SYSTEM_INTERACTION) { |
Kweku Adams | 1275213 | 2020-02-18 15:36:48 -0800 | [diff] [blame] | 916 | mAppIdleHistory.reportUsage(appHistory, pkg, userId, |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 917 | STANDBY_BUCKET_ACTIVE, subReason, |
| 918 | 0, elapsedRealtime + mSystemInteractionTimeoutMillis); |
| 919 | nextCheckDelay = mSystemInteractionTimeoutMillis; |
| 920 | } else if (eventType == UsageEvents.Event.FOREGROUND_SERVICE_START) { |
| 921 | // Only elevate bucket if this is the first usage of the app |
| 922 | if (prevBucket != STANDBY_BUCKET_NEVER) return; |
Kweku Adams | 1275213 | 2020-02-18 15:36:48 -0800 | [diff] [blame] | 923 | mAppIdleHistory.reportUsage(appHistory, pkg, userId, |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 924 | STANDBY_BUCKET_ACTIVE, subReason, |
| 925 | 0, elapsedRealtime + mInitialForegroundServiceStartTimeoutMillis); |
| 926 | nextCheckDelay = mInitialForegroundServiceStartTimeoutMillis; |
| 927 | } else { |
Kweku Adams | 1275213 | 2020-02-18 15:36:48 -0800 | [diff] [blame] | 928 | mAppIdleHistory.reportUsage(appHistory, pkg, userId, |
Michael Wachenschwanz | 081ce5d | 2020-02-10 12:47:53 -0800 | [diff] [blame] | 929 | STANDBY_BUCKET_ACTIVE, subReason, |
| 930 | elapsedRealtime, elapsedRealtime + mStrongUsageTimeoutMillis); |
| 931 | nextCheckDelay = mStrongUsageTimeoutMillis; |
| 932 | } |
| 933 | if (appHistory.currentBucket != prevBucket) { |
| 934 | mHandler.sendMessageDelayed( |
| 935 | mHandler.obtainMessage(MSG_CHECK_PACKAGE_IDLE_STATE, userId, -1, pkg), |
| 936 | nextCheckDelay); |
| 937 | final boolean userStartedInteracting = |
| 938 | appHistory.currentBucket == STANDBY_BUCKET_ACTIVE |
| 939 | && (prevBucketReason & REASON_MAIN_MASK) != REASON_MAIN_USAGE; |
| 940 | maybeInformListeners(pkg, userId, elapsedRealtime, |
| 941 | appHistory.currentBucket, reason, userStartedInteracting); |
| 942 | } |
| 943 | |
| 944 | if (previouslyIdle) { |
| 945 | notifyBatteryStats(pkg, userId, false); |
| 946 | } |
| 947 | } |
| 948 | |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 949 | /** |
| 950 | * Note: don't call this with the lock held since it makes calls to other system services. |
| 951 | */ |
| 952 | private @NonNull List<UserHandle> getCrossProfileTargets(String pkg, int userId) { |
| 953 | synchronized (mAppIdleLock) { |
| 954 | if (!mLinkCrossProfileApps) return Collections.emptyList(); |
| 955 | } |
| 956 | return mInjector.getValidCrossProfileTargets(pkg, userId); |
| 957 | } |
| 958 | |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 959 | private int usageEventToSubReason(int eventType) { |
| 960 | switch (eventType) { |
Hui Yu | 03d1240 | 2018-12-06 18:00:37 -0800 | [diff] [blame] | 961 | case UsageEvents.Event.ACTIVITY_RESUMED: return REASON_SUB_USAGE_MOVE_TO_FOREGROUND; |
| 962 | case UsageEvents.Event.ACTIVITY_PAUSED: return REASON_SUB_USAGE_MOVE_TO_BACKGROUND; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 963 | case UsageEvents.Event.SYSTEM_INTERACTION: return REASON_SUB_USAGE_SYSTEM_INTERACTION; |
| 964 | case UsageEvents.Event.USER_INTERACTION: return REASON_SUB_USAGE_USER_INTERACTION; |
| 965 | case UsageEvents.Event.NOTIFICATION_SEEN: return REASON_SUB_USAGE_NOTIFICATION_SEEN; |
Amith Yamasani | 80c4be8 | 2018-03-26 10:54:04 -0700 | [diff] [blame] | 966 | case UsageEvents.Event.SLICE_PINNED: return REASON_SUB_USAGE_SLICE_PINNED; |
| 967 | case UsageEvents.Event.SLICE_PINNED_PRIV: return REASON_SUB_USAGE_SLICE_PINNED_PRIV; |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 968 | case UsageEvents.Event.FOREGROUND_SERVICE_START: |
| 969 | return REASON_SUB_USAGE_FOREGROUND_SERVICE_START; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 970 | default: return 0; |
| 971 | } |
| 972 | } |
| 973 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 974 | @VisibleForTesting |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 975 | void forceIdleState(String packageName, int userId, boolean idle) { |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 976 | if (!mAppIdleEnabled) return; |
| 977 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 978 | final int appId = getAppId(packageName); |
| 979 | if (appId < 0) return; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 980 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 981 | |
| 982 | final boolean previouslyIdle = isAppIdleFiltered(packageName, appId, |
| 983 | userId, elapsedRealtime); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 984 | final int standbyBucket; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 985 | synchronized (mAppIdleLock) { |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 986 | standbyBucket = mAppIdleHistory.setIdle(packageName, userId, idle, elapsedRealtime); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 987 | } |
| 988 | final boolean stillIdle = isAppIdleFiltered(packageName, appId, |
| 989 | userId, elapsedRealtime); |
| 990 | // Inform listeners if necessary |
| 991 | if (previouslyIdle != stillIdle) { |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 992 | maybeInformListeners(packageName, userId, elapsedRealtime, standbyBucket, |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 993 | REASON_MAIN_FORCED_BY_USER, false); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 994 | if (!stillIdle) { |
| 995 | notifyBatteryStats(packageName, userId, idle); |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1000 | @Override |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1001 | public void setLastJobRunTime(String packageName, int userId, long elapsedRealtime) { |
| 1002 | synchronized (mAppIdleLock) { |
| 1003 | mAppIdleHistory.setLastJobRunTime(packageName, userId, elapsedRealtime); |
| 1004 | } |
| 1005 | } |
| 1006 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1007 | @Override |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1008 | public long getTimeSinceLastJobRun(String packageName, int userId) { |
| 1009 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
| 1010 | synchronized (mAppIdleLock) { |
| 1011 | return mAppIdleHistory.getTimeSinceLastJobRun(packageName, userId, elapsedRealtime); |
| 1012 | } |
| 1013 | } |
| 1014 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1015 | @Override |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1016 | public void onUserRemoved(int userId) { |
| 1017 | synchronized (mAppIdleLock) { |
| 1018 | mAppIdleHistory.onUserRemoved(userId); |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 1019 | synchronized (mActiveAdminApps) { |
| 1020 | mActiveAdminApps.remove(userId); |
| 1021 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | private boolean isAppIdleUnfiltered(String packageName, int userId, long elapsedRealtime) { |
| 1026 | synchronized (mAppIdleLock) { |
| 1027 | return mAppIdleHistory.isIdle(packageName, userId, elapsedRealtime); |
| 1028 | } |
| 1029 | } |
| 1030 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1031 | @Override |
| 1032 | public void addListener(AppIdleStateChangeListener listener) { |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 1033 | synchronized (mPackageAccessListeners) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1034 | if (!mPackageAccessListeners.contains(listener)) { |
| 1035 | mPackageAccessListeners.add(listener); |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1040 | @Override |
| 1041 | public void removeListener(AppIdleStateChangeListener listener) { |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 1042 | synchronized (mPackageAccessListeners) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1043 | mPackageAccessListeners.remove(listener); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1047 | @Override |
| 1048 | public int getAppId(String packageName) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1049 | try { |
| 1050 | ApplicationInfo ai = mPackageManager.getApplicationInfo(packageName, |
| 1051 | PackageManager.MATCH_ANY_USER |
| 1052 | | PackageManager.MATCH_DISABLED_COMPONENTS); |
| 1053 | return ai.uid; |
| 1054 | } catch (PackageManager.NameNotFoundException re) { |
| 1055 | return -1; |
| 1056 | } |
| 1057 | } |
| 1058 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1059 | @Override |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 1060 | public boolean isAppIdleFiltered(String packageName, int userId, long elapsedRealtime, |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1061 | boolean shouldObfuscateInstantApps) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1062 | if (shouldObfuscateInstantApps && |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1063 | mInjector.isPackageEphemeral(userId, packageName)) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1064 | return false; |
| 1065 | } |
| 1066 | return isAppIdleFiltered(packageName, getAppId(packageName), userId, elapsedRealtime); |
| 1067 | } |
| 1068 | |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1069 | @StandbyBuckets |
| 1070 | private int getAppMinBucket(String packageName, int userId) { |
| 1071 | try { |
| 1072 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 1073 | return getAppMinBucket(packageName, UserHandle.getAppId(uid), userId); |
| 1074 | } catch (PackageManager.NameNotFoundException e) { |
| 1075 | // Not a valid package for this user, nothing to do |
| 1076 | return STANDBY_BUCKET_NEVER; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * Return the lowest bucket this app should ever enter. |
| 1082 | */ |
| 1083 | @StandbyBuckets |
| 1084 | private int getAppMinBucket(String packageName, int appId, int userId) { |
| 1085 | if (packageName == null) return STANDBY_BUCKET_NEVER; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1086 | // If not enabled at all, of course nobody is ever idle. |
| 1087 | if (!mAppIdleEnabled) { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1088 | return STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1089 | } |
| 1090 | if (appId < Process.FIRST_APPLICATION_UID) { |
| 1091 | // System uids never go idle. |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1092 | return STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1093 | } |
| 1094 | if (packageName.equals("android")) { |
| 1095 | // Nor does the framework (which should be redundant with the above, but for MR1 we will |
| 1096 | // retain this for safety). |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1097 | return STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1098 | } |
| 1099 | if (mSystemServicesReady) { |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 1100 | // We allow all whitelisted apps, including those that don't want to be whitelisted |
| 1101 | // for idle mode, because app idle (aka app standby) is really not as big an issue |
| 1102 | // for controlling who participates vs. doze mode. |
| 1103 | if (mInjector.isNonIdleWhitelisted(packageName)) { |
| 1104 | return STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | if (isActiveDeviceAdmin(packageName, userId)) { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1108 | return STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | if (isActiveNetworkScorer(packageName)) { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1112 | return STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | if (mAppWidgetManager != null |
| 1116 | && mInjector.isBoundWidgetPackage(mAppWidgetManager, packageName, userId)) { |
Kweku Adams | 107be82 | 2020-05-14 08:02:45 -0700 | [diff] [blame] | 1117 | return STANDBY_BUCKET_ACTIVE; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | if (isDeviceProvisioningPackage(packageName)) { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1121 | return STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | // Check this last, as it can be the most expensive check |
| 1126 | if (isCarrierApp(packageName)) { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1127 | return STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1130 | if (isHeadlessSystemApp(packageName)) { |
| 1131 | return STANDBY_BUCKET_ACTIVE; |
| 1132 | } |
| 1133 | |
| 1134 | return STANDBY_BUCKET_NEVER; |
| 1135 | } |
| 1136 | |
| 1137 | private boolean isHeadlessSystemApp(String packageName) { |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1138 | synchronized (mHeadlessSystemApps) { |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 1139 | return mHeadlessSystemApps.contains(packageName); |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1140 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1143 | @Override |
| 1144 | public boolean isAppIdleFiltered(String packageName, int appId, int userId, |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1145 | long elapsedRealtime) { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1146 | if (getAppMinBucket(packageName, appId, userId) < AppIdleHistory.IDLE_BUCKET_CUTOFF) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1147 | return false; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1148 | } else { |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 1149 | synchronized (mAppIdleLock) { |
| 1150 | if (!mAppIdleEnabled || mIsCharging) { |
| 1151 | return false; |
| 1152 | } |
| 1153 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1154 | return isAppIdleUnfiltered(packageName, userId, elapsedRealtime); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1155 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1158 | static boolean isUserUsage(int reason) { |
| 1159 | if ((reason & REASON_MAIN_MASK) == REASON_MAIN_USAGE) { |
| 1160 | final int subReason = reason & REASON_SUB_MASK; |
| 1161 | return subReason == REASON_SUB_USAGE_USER_INTERACTION |
| 1162 | || subReason == REASON_SUB_USAGE_MOVE_TO_FOREGROUND; |
| 1163 | } |
| 1164 | return false; |
| 1165 | } |
| 1166 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1167 | @Override |
| 1168 | public int[] getIdleUidsForUser(int userId) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1169 | if (!mAppIdleEnabled) { |
| 1170 | return new int[0]; |
| 1171 | } |
| 1172 | |
Amith Yamasani | 62b1991 | 2020-05-29 15:20:18 -0700 | [diff] [blame] | 1173 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "getIdleUidsForUser"); |
| 1174 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1175 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1176 | |
| 1177 | List<ApplicationInfo> apps; |
| 1178 | try { |
| 1179 | ParceledListSlice<ApplicationInfo> slice = AppGlobals.getPackageManager() |
| 1180 | .getInstalledApplications(/* flags= */ 0, userId); |
| 1181 | if (slice == null) { |
| 1182 | return new int[0]; |
| 1183 | } |
| 1184 | apps = slice.getList(); |
| 1185 | } catch (RemoteException e) { |
| 1186 | throw e.rethrowFromSystemServer(); |
| 1187 | } |
| 1188 | |
| 1189 | // State of each uid. Key is the uid. Value lower 16 bits is the number of apps |
| 1190 | // associated with that uid, upper 16 bits is the number of those apps that is idle. |
| 1191 | SparseIntArray uidStates = new SparseIntArray(); |
| 1192 | |
| 1193 | // Now resolve all app state. Iterating over all apps, keeping track of how many |
| 1194 | // we find for each uid and how many of those are idle. |
| 1195 | for (int i = apps.size() - 1; i >= 0; i--) { |
| 1196 | ApplicationInfo ai = apps.get(i); |
| 1197 | |
| 1198 | // Check whether this app is idle. |
| 1199 | boolean idle = isAppIdleFiltered(ai.packageName, UserHandle.getAppId(ai.uid), |
| 1200 | userId, elapsedRealtime); |
| 1201 | |
| 1202 | int index = uidStates.indexOfKey(ai.uid); |
| 1203 | if (index < 0) { |
| 1204 | uidStates.put(ai.uid, 1 + (idle ? 1<<16 : 0)); |
| 1205 | } else { |
| 1206 | int value = uidStates.valueAt(index); |
| 1207 | uidStates.setValueAt(index, value + 1 + (idle ? 1<<16 : 0)); |
| 1208 | } |
| 1209 | } |
Amith Yamasani | 62b1991 | 2020-05-29 15:20:18 -0700 | [diff] [blame] | 1210 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1211 | if (DEBUG) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1212 | Slog.d(TAG, "getIdleUids took " + (mInjector.elapsedRealtime() - elapsedRealtime)); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1213 | } |
| 1214 | int numIdle = 0; |
| 1215 | for (int i = uidStates.size() - 1; i >= 0; i--) { |
| 1216 | int value = uidStates.valueAt(i); |
| 1217 | if ((value&0x7fff) == (value>>16)) { |
| 1218 | numIdle++; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | int[] res = new int[numIdle]; |
| 1223 | numIdle = 0; |
| 1224 | for (int i = uidStates.size() - 1; i >= 0; i--) { |
| 1225 | int value = uidStates.valueAt(i); |
| 1226 | if ((value&0x7fff) == (value>>16)) { |
| 1227 | res[numIdle] = uidStates.keyAt(i); |
| 1228 | numIdle++; |
| 1229 | } |
| 1230 | } |
| 1231 | |
Amith Yamasani | 62b1991 | 2020-05-29 15:20:18 -0700 | [diff] [blame] | 1232 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 1233 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1234 | return res; |
| 1235 | } |
| 1236 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1237 | @Override |
| 1238 | public void setAppIdleAsync(String packageName, boolean idle, int userId) { |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 1239 | if (packageName == null || !mAppIdleEnabled) return; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1240 | |
| 1241 | mHandler.obtainMessage(MSG_FORCE_IDLE_STATE, userId, idle ? 1 : 0, packageName) |
| 1242 | .sendToTarget(); |
| 1243 | } |
| 1244 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1245 | @Override |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1246 | @StandbyBuckets public int getAppStandbyBucket(String packageName, int userId, |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1247 | long elapsedRealtime, boolean shouldObfuscateInstantApps) { |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 1248 | if (!mAppIdleEnabled || (shouldObfuscateInstantApps |
| 1249 | && mInjector.isPackageEphemeral(userId, packageName))) { |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 1250 | return STANDBY_BUCKET_ACTIVE; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 1253 | synchronized (mAppIdleLock) { |
| 1254 | return mAppIdleHistory.getAppStandbyBucket(packageName, userId, elapsedRealtime); |
| 1255 | } |
| 1256 | } |
| 1257 | |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 1258 | @VisibleForTesting |
| 1259 | int getAppStandbyBucketReason(String packageName, int userId, long elapsedRealtime) { |
| 1260 | synchronized (mAppIdleLock) { |
| 1261 | return mAppIdleHistory.getAppStandbyReason(packageName, userId, elapsedRealtime); |
| 1262 | } |
| 1263 | } |
| 1264 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1265 | @Override |
Suprabh Shukla | 868bde2 | 2018-02-20 20:59:52 -0800 | [diff] [blame] | 1266 | public List<AppStandbyInfo> getAppStandbyBuckets(int userId) { |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 1267 | synchronized (mAppIdleLock) { |
Suprabh Shukla | 868bde2 | 2018-02-20 20:59:52 -0800 | [diff] [blame] | 1268 | return mAppIdleHistory.getAppStandbyBuckets(userId, mAppIdleEnabled); |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 1269 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1272 | @Override |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 1273 | public void restrictApp(@NonNull String packageName, int userId, |
| 1274 | @SystemForcedReasons int restrictReason) { |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1275 | // If the package is not installed, don't allow the bucket to be set. |
| 1276 | if (!mInjector.isPackageInstalled(packageName, 0, userId)) { |
| 1277 | Slog.e(TAG, "Tried to restrict uninstalled app: " + packageName); |
| 1278 | return; |
| 1279 | } |
| 1280 | |
| 1281 | final int reason = REASON_MAIN_FORCED_BY_SYSTEM | (REASON_SUB_MASK & restrictReason); |
| 1282 | final long nowElapsed = mInjector.elapsedRealtime(); |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 1283 | final int bucket = mAllowRestrictedBucket ? STANDBY_BUCKET_RESTRICTED : STANDBY_BUCKET_RARE; |
| 1284 | setAppStandbyBucket(packageName, userId, bucket, reason, nowElapsed, false); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | @Override |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1288 | public void setAppStandbyBucket(@NonNull String packageName, int bucket, int userId, |
| 1289 | int callingUid, int callingPid) { |
| 1290 | setAppStandbyBuckets( |
| 1291 | Collections.singletonList(new AppStandbyInfo(packageName, bucket)), |
| 1292 | userId, callingUid, callingPid); |
Makoto Onuki | a0058b4 | 2018-05-22 16:32:23 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1295 | @Override |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1296 | public void setAppStandbyBuckets(@NonNull List<AppStandbyInfo> appBuckets, int userId, |
| 1297 | int callingUid, int callingPid) { |
| 1298 | userId = ActivityManager.handleIncomingUser( |
| 1299 | callingPid, callingUid, userId, false, true, "setAppStandbyBucket", null); |
| 1300 | final boolean shellCaller = callingUid == Process.ROOT_UID |
| 1301 | || callingUid == Process.SHELL_UID; |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1302 | final int reason; |
| 1303 | // The Settings app runs in the system UID but in a separate process. Assume |
| 1304 | // things coming from other processes are due to the user. |
| 1305 | if ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) && callingPid != Process.myPid()) |
| 1306 | || shellCaller) { |
| 1307 | reason = REASON_MAIN_FORCED_BY_USER; |
| 1308 | } else if (UserHandle.isCore(callingUid)) { |
| 1309 | reason = REASON_MAIN_FORCED_BY_SYSTEM; |
| 1310 | } else { |
| 1311 | reason = REASON_MAIN_PREDICTED; |
| 1312 | } |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1313 | final int packageFlags = PackageManager.MATCH_ANY_USER |
| 1314 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 1315 | | PackageManager.MATCH_DIRECT_BOOT_AWARE; |
| 1316 | final int numApps = appBuckets.size(); |
| 1317 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
| 1318 | for (int i = 0; i < numApps; ++i) { |
| 1319 | final AppStandbyInfo bucketInfo = appBuckets.get(i); |
| 1320 | final String packageName = bucketInfo.mPackageName; |
| 1321 | final int bucket = bucketInfo.mStandbyBucket; |
| 1322 | if (bucket < STANDBY_BUCKET_ACTIVE || bucket > STANDBY_BUCKET_NEVER) { |
| 1323 | throw new IllegalArgumentException("Cannot set the standby bucket to " + bucket); |
| 1324 | } |
| 1325 | final int packageUid = mInjector.getPackageManagerInternal() |
| 1326 | .getPackageUid(packageName, packageFlags, userId); |
| 1327 | // Caller cannot set their own standby state |
| 1328 | if (packageUid == callingUid) { |
| 1329 | throw new IllegalArgumentException("Cannot set your own standby bucket"); |
| 1330 | } |
| 1331 | if (packageUid < 0) { |
| 1332 | throw new IllegalArgumentException( |
| 1333 | "Cannot set standby bucket for non existent package (" + packageName + ")"); |
| 1334 | } |
| 1335 | setAppStandbyBucket(packageName, userId, bucket, reason, elapsedRealtime, shellCaller); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | @VisibleForTesting |
| 1340 | void setAppStandbyBucket(String packageName, int userId, @StandbyBuckets int newBucket, |
| 1341 | int reason) { |
| 1342 | setAppStandbyBucket( |
| 1343 | packageName, userId, newBucket, reason, mInjector.elapsedRealtime(), false); |
| 1344 | } |
| 1345 | |
| 1346 | private void setAppStandbyBucket(String packageName, int userId, @StandbyBuckets int newBucket, |
Makoto Onuki | a0058b4 | 2018-05-22 16:32:23 -0700 | [diff] [blame] | 1347 | int reason, long elapsedRealtime, boolean resetTimeout) { |
Kweku Adams | 52b52a6 | 2020-05-26 12:21:16 -0700 | [diff] [blame] | 1348 | if (!mAppIdleEnabled) return; |
| 1349 | |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 1350 | synchronized (mAppIdleLock) { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1351 | // If the package is not installed, don't allow the bucket to be set. |
| 1352 | if (!mInjector.isPackageInstalled(packageName, 0, userId)) { |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1353 | Slog.e(TAG, "Tried to set bucket of uninstalled app: " + packageName); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1354 | return; |
| 1355 | } |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 1356 | if (newBucket == STANDBY_BUCKET_RESTRICTED && !mAllowRestrictedBucket) { |
| 1357 | newBucket = STANDBY_BUCKET_RARE; |
| 1358 | } |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 1359 | AppIdleHistory.AppUsageHistory app = mAppIdleHistory.getAppUsageHistory(packageName, |
| 1360 | userId, elapsedRealtime); |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 1361 | boolean predicted = (reason & REASON_MAIN_MASK) == REASON_MAIN_PREDICTED; |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1362 | |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 1363 | // Don't allow changing bucket if higher than ACTIVE |
| 1364 | if (app.currentBucket < STANDBY_BUCKET_ACTIVE) return; |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1365 | |
Kweku Adams | 496594c | 2020-03-16 12:26:01 -0700 | [diff] [blame] | 1366 | // Don't allow prediction to change from/to NEVER. |
| 1367 | if ((app.currentBucket == STANDBY_BUCKET_NEVER || newBucket == STANDBY_BUCKET_NEVER) |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 1368 | && predicted) { |
| 1369 | return; |
| 1370 | } |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1371 | |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 1372 | final boolean wasForcedBySystem = |
| 1373 | (app.bucketingReason & REASON_MAIN_MASK) == REASON_MAIN_FORCED_BY_SYSTEM; |
| 1374 | |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 1375 | // If the bucket was forced, don't allow prediction to override |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1376 | if (predicted |
| 1377 | && ((app.bucketingReason & REASON_MAIN_MASK) == REASON_MAIN_FORCED_BY_USER |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 1378 | || wasForcedBySystem)) { |
| 1379 | return; |
| 1380 | } |
| 1381 | |
| 1382 | final boolean isForcedBySystem = |
| 1383 | (reason & REASON_MAIN_MASK) == REASON_MAIN_FORCED_BY_SYSTEM; |
| 1384 | |
| 1385 | if (app.currentBucket == newBucket && wasForcedBySystem && isForcedBySystem) { |
| 1386 | mAppIdleHistory |
| 1387 | .noteRestrictionAttempt(packageName, userId, elapsedRealtime, reason); |
| 1388 | // Keep track of all restricting reasons |
| 1389 | reason = REASON_MAIN_FORCED_BY_SYSTEM |
| 1390 | | (app.bucketingReason & REASON_SUB_MASK) |
| 1391 | | (reason & REASON_SUB_MASK); |
| 1392 | mAppIdleHistory.setAppStandbyBucket(packageName, userId, elapsedRealtime, |
| 1393 | newBucket, reason, resetTimeout); |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1394 | return; |
| 1395 | } |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1396 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1397 | final boolean isForcedByUser = |
| 1398 | (reason & REASON_MAIN_MASK) == REASON_MAIN_FORCED_BY_USER; |
| 1399 | |
Kweku Adams | 496594c | 2020-03-16 12:26:01 -0700 | [diff] [blame] | 1400 | if (app.currentBucket == STANDBY_BUCKET_RESTRICTED) { |
| 1401 | if ((app.bucketingReason & REASON_MAIN_MASK) == REASON_MAIN_TIMEOUT) { |
| 1402 | if (predicted && newBucket >= STANDBY_BUCKET_RARE) { |
| 1403 | // Predicting into RARE or below means we don't expect the user to use the |
| 1404 | // app anytime soon, so don't elevate it from RESTRICTED. |
| 1405 | return; |
| 1406 | } |
| 1407 | } else if (!isUserUsage(reason) && !isForcedByUser) { |
| 1408 | // If the current bucket is RESTRICTED, only user force or usage should bring |
| 1409 | // it out, unless the app was put into the bucket due to timing out. |
| 1410 | return; |
| 1411 | } |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | if (newBucket == STANDBY_BUCKET_RESTRICTED) { |
| 1415 | mAppIdleHistory |
| 1416 | .noteRestrictionAttempt(packageName, userId, elapsedRealtime, reason); |
| 1417 | |
| 1418 | if (isForcedByUser) { |
| 1419 | // Only user force can bypass the delay restriction. If the user forced the |
| 1420 | // app into the RESTRICTED bucket, then a toast confirming the action |
| 1421 | // shouldn't be surprising. |
| 1422 | if (Build.IS_DEBUGGABLE) { |
| 1423 | Toast.makeText(mContext, |
| 1424 | // Since AppStandbyController sits low in the lock hierarchy, |
| 1425 | // make sure not to call out with the lock held. |
| 1426 | mHandler.getLooper(), |
| 1427 | mContext.getResources().getString( |
| 1428 | R.string.as_app_forced_to_restricted_bucket, packageName), |
| 1429 | Toast.LENGTH_SHORT) |
| 1430 | .show(); |
| 1431 | } else { |
| 1432 | Slog.i(TAG, packageName + " restricted by user"); |
| 1433 | } |
| 1434 | } else { |
| 1435 | final long timeUntilRestrictPossibleMs = app.lastUsedByUserElapsedTime |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 1436 | + mInjector.getAutoRestrictedBucketDelayMs() - elapsedRealtime; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1437 | if (timeUntilRestrictPossibleMs > 0) { |
| 1438 | Slog.w(TAG, "Tried to restrict recently used app: " + packageName |
| 1439 | + " due to " + reason); |
| 1440 | mHandler.sendMessageDelayed( |
| 1441 | mHandler.obtainMessage( |
| 1442 | MSG_CHECK_PACKAGE_IDLE_STATE, userId, -1, packageName), |
| 1443 | timeUntilRestrictPossibleMs); |
| 1444 | return; |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1449 | // If the bucket is required to stay in a higher state for a specified duration, don't |
| 1450 | // override unless the duration has passed |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1451 | if (predicted) { |
| 1452 | // Check if the app is within one of the timeouts for forced bucket elevation |
| 1453 | final long elapsedTimeAdjusted = mAppIdleHistory.getElapsedTime(elapsedRealtime); |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 1454 | // In case of not using the prediction, just keep track of it for applying after |
| 1455 | // ACTIVE or WORKING_SET timeout. |
| 1456 | mAppIdleHistory.updateLastPrediction(app, elapsedTimeAdjusted, newBucket); |
| 1457 | |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1458 | if (newBucket > STANDBY_BUCKET_ACTIVE |
| 1459 | && app.bucketActiveTimeoutTime > elapsedTimeAdjusted) { |
| 1460 | newBucket = STANDBY_BUCKET_ACTIVE; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 1461 | reason = app.bucketingReason; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1462 | if (DEBUG) { |
| 1463 | Slog.d(TAG, " Keeping at ACTIVE due to min timeout"); |
| 1464 | } |
| 1465 | } else if (newBucket > STANDBY_BUCKET_WORKING_SET |
| 1466 | && app.bucketWorkingSetTimeoutTime > elapsedTimeAdjusted) { |
| 1467 | newBucket = STANDBY_BUCKET_WORKING_SET; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 1468 | if (app.currentBucket != newBucket) { |
| 1469 | reason = REASON_MAIN_USAGE | REASON_SUB_USAGE_ACTIVE_TIMEOUT; |
| 1470 | } else { |
| 1471 | reason = app.bucketingReason; |
| 1472 | } |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1473 | if (DEBUG) { |
| 1474 | Slog.d(TAG, " Keeping at WORKING_SET due to min timeout"); |
| 1475 | } |
Kweku Adams | 5439133 | 2020-04-08 14:56:46 -0700 | [diff] [blame] | 1476 | } else if (newBucket == STANDBY_BUCKET_RARE |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 1477 | && mAllowRestrictedBucket |
Kweku Adams | 5439133 | 2020-04-08 14:56:46 -0700 | [diff] [blame] | 1478 | && getBucketForLocked(packageName, userId, elapsedRealtime) |
| 1479 | == STANDBY_BUCKET_RESTRICTED) { |
| 1480 | // Prediction doesn't think the app will be used anytime soon and |
| 1481 | // it's been long enough that it could just time out into restricted, |
| 1482 | // so time it out there instead. Using TIMEOUT will allow prediction |
| 1483 | // to raise the bucket when it needs to. |
| 1484 | newBucket = STANDBY_BUCKET_RESTRICTED; |
| 1485 | reason = REASON_MAIN_TIMEOUT; |
| 1486 | if (DEBUG) { |
| 1487 | Slog.d(TAG, |
| 1488 | "Prediction to RARE overridden by timeout into RESTRICTED"); |
| 1489 | } |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1490 | } |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1493 | // Make sure we don't put the app in a lower bucket than it's supposed to be in. |
| 1494 | newBucket = Math.min(newBucket, getAppMinBucket(packageName, userId)); |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 1495 | mAppIdleHistory.setAppStandbyBucket(packageName, userId, elapsedRealtime, newBucket, |
Makoto Onuki | a0058b4 | 2018-05-22 16:32:23 -0700 | [diff] [blame] | 1496 | reason, resetTimeout); |
Amith Yamasani | e878931 | 2017-12-10 14:34:26 -0800 | [diff] [blame] | 1497 | } |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 1498 | maybeInformListeners(packageName, userId, elapsedRealtime, newBucket, reason, false); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 1501 | @VisibleForTesting |
| 1502 | boolean isActiveDeviceAdmin(String packageName, int userId) { |
| 1503 | synchronized (mActiveAdminApps) { |
| 1504 | final Set<String> adminPkgs = mActiveAdminApps.get(userId); |
| 1505 | return adminPkgs != null && adminPkgs.contains(packageName); |
| 1506 | } |
| 1507 | } |
| 1508 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1509 | @Override |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 1510 | public void addActiveDeviceAdmin(String adminPkg, int userId) { |
| 1511 | synchronized (mActiveAdminApps) { |
| 1512 | Set<String> adminPkgs = mActiveAdminApps.get(userId); |
| 1513 | if (adminPkgs == null) { |
| 1514 | adminPkgs = new ArraySet<>(); |
| 1515 | mActiveAdminApps.put(userId, adminPkgs); |
| 1516 | } |
| 1517 | adminPkgs.add(adminPkg); |
| 1518 | } |
| 1519 | } |
| 1520 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1521 | @Override |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 1522 | public void setActiveAdminApps(Set<String> adminPkgs, int userId) { |
| 1523 | synchronized (mActiveAdminApps) { |
| 1524 | if (adminPkgs == null) { |
| 1525 | mActiveAdminApps.remove(userId); |
| 1526 | } else { |
| 1527 | mActiveAdminApps.put(userId, adminPkgs); |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1532 | @Override |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 1533 | public void onAdminDataAvailable() { |
| 1534 | mAdminDataAvailableLatch.countDown(); |
| 1535 | } |
| 1536 | |
| 1537 | /** |
| 1538 | * This will only ever be called once - during device boot. |
| 1539 | */ |
| 1540 | private void waitForAdminData() { |
| 1541 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { |
| 1542 | ConcurrentUtils.waitForCountDownNoInterrupt(mAdminDataAvailableLatch, |
| 1543 | WAIT_FOR_ADMIN_DATA_TIMEOUT_MS, "Wait for admin data"); |
| 1544 | } |
| 1545 | } |
| 1546 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1547 | @VisibleForTesting |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 1548 | Set<String> getActiveAdminAppsForTest(int userId) { |
| 1549 | synchronized (mActiveAdminApps) { |
| 1550 | return mActiveAdminApps.get(userId); |
| 1551 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /** |
| 1555 | * Returns {@code true} if the supplied package is the device provisioning app. Otherwise, |
| 1556 | * returns {@code false}. |
| 1557 | */ |
| 1558 | private boolean isDeviceProvisioningPackage(String packageName) { |
| 1559 | String deviceProvisioningPackage = mContext.getResources().getString( |
| 1560 | com.android.internal.R.string.config_deviceProvisioningPackage); |
| 1561 | return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName); |
| 1562 | } |
| 1563 | |
| 1564 | private boolean isCarrierApp(String packageName) { |
| 1565 | synchronized (mAppIdleLock) { |
| 1566 | if (!mHaveCarrierPrivilegedApps) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1567 | fetchCarrierPrivilegedAppsLocked(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1568 | } |
| 1569 | if (mCarrierPrivilegedApps != null) { |
| 1570 | return mCarrierPrivilegedApps.contains(packageName); |
| 1571 | } |
| 1572 | return false; |
| 1573 | } |
| 1574 | } |
| 1575 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1576 | @Override |
| 1577 | public void clearCarrierPrivilegedApps() { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1578 | if (DEBUG) { |
| 1579 | Slog.i(TAG, "Clearing carrier privileged apps list"); |
| 1580 | } |
| 1581 | synchronized (mAppIdleLock) { |
| 1582 | mHaveCarrierPrivilegedApps = false; |
| 1583 | mCarrierPrivilegedApps = null; // Need to be refetched. |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | @GuardedBy("mAppIdleLock") |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1588 | private void fetchCarrierPrivilegedAppsLocked() { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1589 | TelephonyManager telephonyManager = |
| 1590 | mContext.getSystemService(TelephonyManager.class); |
Shuo Qian | 6f27c57 | 2019-12-03 23:52:13 +0000 | [diff] [blame] | 1591 | mCarrierPrivilegedApps = |
| 1592 | telephonyManager.getCarrierPrivilegedPackagesForAllActiveSubscriptions(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1593 | mHaveCarrierPrivilegedApps = true; |
| 1594 | if (DEBUG) { |
| 1595 | Slog.d(TAG, "apps with carrier privilege " + mCarrierPrivilegedApps); |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | private boolean isActiveNetworkScorer(String packageName) { |
Amith Yamasani | 62b1991 | 2020-05-29 15:20:18 -0700 | [diff] [blame] | 1600 | // Validity of network scorer cache is limited to a few seconds. Fetch it again |
| 1601 | // if longer since query. |
| 1602 | // This is a temporary optimization until there's a callback mechanism for changes to network scorer. |
| 1603 | final long now = SystemClock.elapsedRealtime(); |
| 1604 | if (mCachedNetworkScorer == null |
| 1605 | || mCachedNetworkScorerAtMillis < now - NETWORK_SCORER_CACHE_DURATION_MILLIS) { |
| 1606 | mCachedNetworkScorer = mInjector.getActiveNetworkScorer(); |
| 1607 | mCachedNetworkScorerAtMillis = now; |
| 1608 | } |
| 1609 | return packageName != null && packageName.equals(mCachedNetworkScorer); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1612 | private void informListeners(String packageName, int userId, int bucket, int reason, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 1613 | boolean userInteraction) { |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 1614 | final boolean idle = bucket >= STANDBY_BUCKET_RARE; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 1615 | synchronized (mPackageAccessListeners) { |
| 1616 | for (AppIdleStateChangeListener listener : mPackageAccessListeners) { |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 1617 | listener.onAppIdleStateChanged(packageName, userId, idle, bucket, reason); |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 1618 | if (userInteraction) { |
| 1619 | listener.onUserInteractionStarted(packageName, userId); |
| 1620 | } |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 1621 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 1625 | private void informParoleStateChanged() { |
| 1626 | final boolean paroled = isInParole(); |
| 1627 | synchronized (mPackageAccessListeners) { |
| 1628 | for (AppIdleStateChangeListener listener : mPackageAccessListeners) { |
| 1629 | listener.onParoleStateChanged(paroled); |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1634 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1635 | @Override |
Michael Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 1636 | public void flushToDisk() { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1637 | synchronized (mAppIdleLock) { |
Michael Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 1638 | mAppIdleHistory.writeAppIdleTimes(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1639 | mAppIdleHistory.writeAppIdleDurations(); |
| 1640 | } |
| 1641 | } |
| 1642 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1643 | private boolean isDisplayOn() { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1644 | return mInjector.isDefaultDisplayOn(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1647 | @VisibleForTesting |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1648 | void clearAppIdleForPackage(String packageName, int userId) { |
| 1649 | synchronized (mAppIdleLock) { |
| 1650 | mAppIdleHistory.clearUsage(packageName, userId); |
| 1651 | } |
| 1652 | } |
| 1653 | |
Kweku Adams | 917f8a4 | 2020-02-26 17:18:03 -0800 | [diff] [blame] | 1654 | /** |
| 1655 | * Remove an app from the {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED} |
| 1656 | * bucket if it was forced into the bucket by the system because it was buggy. |
| 1657 | */ |
| 1658 | @VisibleForTesting |
| 1659 | void maybeUnrestrictBuggyApp(String packageName, int userId) { |
| 1660 | synchronized (mAppIdleLock) { |
| 1661 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
| 1662 | final AppIdleHistory.AppUsageHistory app = |
| 1663 | mAppIdleHistory.getAppUsageHistory(packageName, userId, elapsedRealtime); |
| 1664 | if (app.currentBucket != STANDBY_BUCKET_RESTRICTED |
| 1665 | || (app.bucketingReason & REASON_MAIN_MASK) != REASON_MAIN_FORCED_BY_SYSTEM) { |
| 1666 | return; |
| 1667 | } |
| 1668 | |
| 1669 | final int newBucket; |
| 1670 | final int newReason; |
| 1671 | if ((app.bucketingReason & REASON_SUB_MASK) == REASON_SUB_FORCED_SYSTEM_FLAG_BUGGY) { |
| 1672 | // If bugginess was the only reason the app should be restricted, then lift it out. |
| 1673 | newBucket = STANDBY_BUCKET_RARE; |
| 1674 | newReason = REASON_MAIN_DEFAULT | REASON_SUB_DEFAULT_APP_UPDATE; |
| 1675 | } else { |
| 1676 | // There's another reason the app was restricted. Remove the buggy bit and call |
| 1677 | // it a day. |
| 1678 | newBucket = STANDBY_BUCKET_RESTRICTED; |
| 1679 | newReason = app.bucketingReason & ~REASON_SUB_FORCED_SYSTEM_FLAG_BUGGY; |
| 1680 | } |
| 1681 | mAppIdleHistory.setAppStandbyBucket( |
| 1682 | packageName, userId, elapsedRealtime, newBucket, newReason); |
| 1683 | } |
| 1684 | } |
| 1685 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1686 | private class PackageReceiver extends BroadcastReceiver { |
| 1687 | @Override |
| 1688 | public void onReceive(Context context, Intent intent) { |
| 1689 | final String action = intent.getAction(); |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1690 | final String pkgName = intent.getData().getSchemeSpecificPart(); |
| 1691 | final int userId = getSendingUserId(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1692 | if (Intent.ACTION_PACKAGE_ADDED.equals(action) |
| 1693 | || Intent.ACTION_PACKAGE_CHANGED.equals(action)) { |
| 1694 | clearCarrierPrivilegedApps(); |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1695 | // ACTION_PACKAGE_ADDED is called even for system app downgrades. |
| 1696 | evaluateSystemAppException(pkgName, userId); |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1697 | mHandler.obtainMessage(MSG_CHECK_PACKAGE_IDLE_STATE, userId, -1, pkgName) |
| 1698 | .sendToTarget(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1699 | } |
| 1700 | if ((Intent.ACTION_PACKAGE_REMOVED.equals(action) || |
Kweku Adams | 917f8a4 | 2020-02-26 17:18:03 -0800 | [diff] [blame] | 1701 | Intent.ACTION_PACKAGE_ADDED.equals(action))) { |
Kweku Adams | 917f8a4 | 2020-02-26 17:18:03 -0800 | [diff] [blame] | 1702 | if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 1703 | maybeUnrestrictBuggyApp(pkgName, userId); |
| 1704 | } else { |
| 1705 | clearAppIdleForPackage(pkgName, userId); |
| 1706 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | } |
| 1710 | |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1711 | private void evaluateSystemAppException(String packageName, int userId) { |
| 1712 | if (!mSystemServicesReady) { |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1713 | // The app will be evaluated in when services are ready. |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1714 | return; |
| 1715 | } |
| 1716 | try { |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 1717 | PackageInfo pi = mPackageManager.getPackageInfoAsUser( |
| 1718 | packageName, HEADLESS_APP_CHECK_FLAGS, userId); |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1719 | evaluateSystemAppException(pi); |
| 1720 | } catch (PackageManager.NameNotFoundException e) { |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1721 | synchronized (mHeadlessSystemApps) { |
| 1722 | mHeadlessSystemApps.remove(packageName); |
| 1723 | } |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1727 | /** Returns true if the exception status changed. */ |
| 1728 | private boolean evaluateSystemAppException(@Nullable PackageInfo pkgInfo) { |
| 1729 | if (pkgInfo == null || pkgInfo.applicationInfo == null |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 1730 | || (!pkgInfo.applicationInfo.isSystemApp() |
| 1731 | && !pkgInfo.applicationInfo.isUpdatedSystemApp())) { |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1732 | return false; |
| 1733 | } |
| 1734 | synchronized (mHeadlessSystemApps) { |
| 1735 | if (pkgInfo.activities == null || pkgInfo.activities.length == 0) { |
| 1736 | // Headless system app. |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 1737 | return mHeadlessSystemApps.add(pkgInfo.packageName); |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1738 | } else { |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 1739 | return mHeadlessSystemApps.remove(pkgInfo.packageName); |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1744 | /** Call on a system version update to temporarily reset system app buckets. */ |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1745 | @Override |
| 1746 | public void initializeDefaultsForSystemApps(int userId) { |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 1747 | if (!mSystemServicesReady) { |
| 1748 | // Do it later, since SettingsProvider wasn't queried yet for app_standby_enabled |
| 1749 | mPendingInitializeDefaults = true; |
| 1750 | return; |
| 1751 | } |
| 1752 | Slog.d(TAG, "Initializing defaults for system apps on user " + userId + ", " |
| 1753 | + "appIdleEnabled=" + mAppIdleEnabled); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1754 | final long elapsedRealtime = mInjector.elapsedRealtime(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1755 | List<PackageInfo> packages = mPackageManager.getInstalledPackagesAsUser( |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1756 | PackageManager.MATCH_DISABLED_COMPONENTS, |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1757 | userId); |
| 1758 | final int packageCount = packages.size(); |
| 1759 | synchronized (mAppIdleLock) { |
| 1760 | for (int i = 0; i < packageCount; i++) { |
| 1761 | final PackageInfo pi = packages.get(i); |
| 1762 | String packageName = pi.packageName; |
| 1763 | if (pi.applicationInfo != null && pi.applicationInfo.isSystemApp()) { |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1764 | // Mark app as used for 2 hours. After that it can timeout to whatever the |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1765 | // past usage pattern was. |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 1766 | mAppIdleHistory.reportUsage(packageName, userId, STANDBY_BUCKET_ACTIVE, |
| 1767 | REASON_SUB_USAGE_SYSTEM_UPDATE, 0, |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1768 | elapsedRealtime + mSystemUpdateUsageTimeoutMillis); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1769 | } |
| 1770 | } |
Michael Wachenschwanz | d1d8aa6 | 2019-02-28 16:38:37 -0800 | [diff] [blame] | 1771 | // Immediately persist defaults to disk |
| 1772 | mAppIdleHistory.writeAppIdleTimes(userId); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 1776 | /** Call on system boot to get the initial set of headless system apps. */ |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1777 | private void loadHeadlessSystemAppCache() { |
| 1778 | Slog.d(TAG, "Loading headless system app cache. appIdleEnabled=" + mAppIdleEnabled); |
| 1779 | final List<PackageInfo> packages = mPackageManager.getInstalledPackagesAsUser( |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 1780 | HEADLESS_APP_CHECK_FLAGS, UserHandle.USER_SYSTEM); |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1781 | final int packageCount = packages.size(); |
| 1782 | for (int i = 0; i < packageCount; i++) { |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1783 | PackageInfo pkgInfo = packages.get(i); |
| 1784 | if (pkgInfo != null && evaluateSystemAppException(pkgInfo)) { |
| 1785 | mHandler.obtainMessage(MSG_CHECK_PACKAGE_IDLE_STATE, |
| 1786 | UserHandle.USER_SYSTEM, -1, pkgInfo.packageName) |
| 1787 | .sendToTarget(); |
| 1788 | } |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1789 | } |
| 1790 | } |
| 1791 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1792 | @Override |
| 1793 | public void postReportContentProviderUsage(String name, String packageName, int userId) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1794 | SomeArgs args = SomeArgs.obtain(); |
| 1795 | args.arg1 = name; |
| 1796 | args.arg2 = packageName; |
| 1797 | args.arg3 = userId; |
| 1798 | mHandler.obtainMessage(MSG_REPORT_CONTENT_PROVIDER_USAGE, args) |
| 1799 | .sendToTarget(); |
| 1800 | } |
| 1801 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1802 | @Override |
| 1803 | public void postReportSyncScheduled(String packageName, int userId, boolean exempted) { |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 1804 | mHandler.obtainMessage(MSG_REPORT_SYNC_SCHEDULED, userId, exempted ? 1 : 0, packageName) |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 1805 | .sendToTarget(); |
| 1806 | } |
| 1807 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1808 | @Override |
| 1809 | public void postReportExemptedSyncStart(String packageName, int userId) { |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1810 | mHandler.obtainMessage(MSG_REPORT_EXEMPTED_SYNC_START, userId, 0, packageName) |
| 1811 | .sendToTarget(); |
| 1812 | } |
| 1813 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1814 | @Override |
Michael Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 1815 | public void dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1816 | synchronized (mAppIdleLock) { |
Michael Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 1817 | mAppIdleHistory.dumpUsers(idpw, userIds, pkgs); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1821 | @Override |
| 1822 | public void dumpState(String[] args, PrintWriter pw) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1823 | synchronized (mAppIdleLock) { |
| 1824 | pw.println("Carrier privileged apps (have=" + mHaveCarrierPrivilegedApps |
| 1825 | + "): " + mCarrierPrivilegedApps); |
| 1826 | } |
| 1827 | |
| 1828 | pw.println(); |
| 1829 | pw.println("Settings:"); |
| 1830 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1831 | pw.print(" mCheckIdleIntervalMillis="); |
| 1832 | TimeUtils.formatDuration(mCheckIdleIntervalMillis, pw); |
| 1833 | pw.println(); |
| 1834 | |
Esteban Talavera | 5b79bfa | 2018-07-05 15:15:22 +0100 | [diff] [blame] | 1835 | pw.print(" mStrongUsageTimeoutMillis="); |
| 1836 | TimeUtils.formatDuration(mStrongUsageTimeoutMillis, pw); |
| 1837 | pw.println(); |
| 1838 | pw.print(" mNotificationSeenTimeoutMillis="); |
| 1839 | TimeUtils.formatDuration(mNotificationSeenTimeoutMillis, pw); |
| 1840 | pw.println(); |
| 1841 | pw.print(" mSyncAdapterTimeoutMillis="); |
| 1842 | TimeUtils.formatDuration(mSyncAdapterTimeoutMillis, pw); |
| 1843 | pw.println(); |
| 1844 | pw.print(" mSystemInteractionTimeoutMillis="); |
| 1845 | TimeUtils.formatDuration(mSystemInteractionTimeoutMillis, pw); |
| 1846 | pw.println(); |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 1847 | pw.print(" mInitialForegroundServiceStartTimeoutMillis="); |
| 1848 | TimeUtils.formatDuration(mInitialForegroundServiceStartTimeoutMillis, pw); |
| 1849 | pw.println(); |
Esteban Talavera | 5b79bfa | 2018-07-05 15:15:22 +0100 | [diff] [blame] | 1850 | |
| 1851 | pw.print(" mPredictionTimeoutMillis="); |
| 1852 | TimeUtils.formatDuration(mPredictionTimeoutMillis, pw); |
| 1853 | pw.println(); |
| 1854 | |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 1855 | pw.print(" mExemptedSyncScheduledNonDozeTimeoutMillis="); |
| 1856 | TimeUtils.formatDuration(mExemptedSyncScheduledNonDozeTimeoutMillis, pw); |
| 1857 | pw.println(); |
| 1858 | pw.print(" mExemptedSyncScheduledDozeTimeoutMillis="); |
| 1859 | TimeUtils.formatDuration(mExemptedSyncScheduledDozeTimeoutMillis, pw); |
| 1860 | pw.println(); |
| 1861 | pw.print(" mExemptedSyncStartTimeoutMillis="); |
| 1862 | TimeUtils.formatDuration(mExemptedSyncStartTimeoutMillis, pw); |
| 1863 | pw.println(); |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 1864 | pw.print(" mUnexemptedSyncScheduledTimeoutMillis="); |
| 1865 | TimeUtils.formatDuration(mUnexemptedSyncScheduledTimeoutMillis, pw); |
| 1866 | pw.println(); |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 1867 | |
Esteban Talavera | 5b79bfa | 2018-07-05 15:15:22 +0100 | [diff] [blame] | 1868 | pw.print(" mSystemUpdateUsageTimeoutMillis="); |
| 1869 | TimeUtils.formatDuration(mSystemUpdateUsageTimeoutMillis, pw); |
| 1870 | pw.println(); |
| 1871 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1872 | pw.println(); |
| 1873 | pw.print("mAppIdleEnabled="); pw.print(mAppIdleEnabled); |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 1874 | pw.print(" mAllowRestrictedBucket="); |
| 1875 | pw.print(mAllowRestrictedBucket); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 1876 | pw.print(" mIsCharging="); |
| 1877 | pw.print(mIsCharging); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 1878 | pw.println(); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1879 | pw.print("mScreenThresholds="); pw.println(Arrays.toString(mAppStandbyScreenThresholds)); |
| 1880 | pw.print("mElapsedThresholds="); pw.println(Arrays.toString(mAppStandbyElapsedThresholds)); |
Michael Wachenschwanz | 5ca5cb6 | 2018-04-25 16:12:59 -0700 | [diff] [blame] | 1881 | pw.println(); |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1882 | |
| 1883 | pw.println("mHeadlessSystemApps=["); |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1884 | synchronized (mHeadlessSystemApps) { |
| 1885 | for (int i = mHeadlessSystemApps.size() - 1; i >= 0; --i) { |
| 1886 | pw.print(" "); |
Kweku Adams | f197eef | 2020-06-16 11:59:37 -0700 | [diff] [blame] | 1887 | pw.print(mHeadlessSystemApps.valueAt(i)); |
Kweku Adams | 8604fd7 | 2020-06-02 14:04:27 -0700 | [diff] [blame] | 1888 | pw.println(","); |
| 1889 | } |
Kweku Adams | c86da5f | 2020-05-28 12:26:35 -0700 | [diff] [blame] | 1890 | } |
| 1891 | pw.println("]"); |
| 1892 | pw.println(); |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 1893 | |
| 1894 | mInjector.dump(pw); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * Injector for interaction with external code. Override methods to provide a mock |
| 1899 | * implementation for tests. |
| 1900 | * onBootPhase() must be called with at least the PHASE_SYSTEM_SERVICES_READY |
| 1901 | */ |
| 1902 | static class Injector { |
| 1903 | |
| 1904 | private final Context mContext; |
| 1905 | private final Looper mLooper; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1906 | private IBatteryStats mBatteryStats; |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 1907 | private BatteryManager mBatteryManager; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1908 | private PackageManagerInternal mPackageManagerInternal; |
| 1909 | private DisplayManager mDisplayManager; |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 1910 | private PowerManager mPowerManager; |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 1911 | private IDeviceIdleController mDeviceIdleController; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 1912 | private CrossProfileAppsInternal mCrossProfileAppsInternal; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1913 | int mBootPhase; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1914 | /** |
| 1915 | * The minimum amount of time required since the last user interaction before an app can be |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 1916 | * automatically placed in the RESTRICTED bucket. |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1917 | */ |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 1918 | long mAutoRestrictedBucketDelayMs = ONE_DAY; |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 1919 | /** |
| 1920 | * Cached set of apps that are power whitelisted, including those not whitelisted from idle. |
| 1921 | */ |
| 1922 | @GuardedBy("mPowerWhitelistedApps") |
| 1923 | private final ArraySet<String> mPowerWhitelistedApps = new ArraySet<>(); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1924 | |
| 1925 | Injector(Context context, Looper looper) { |
| 1926 | mContext = context; |
| 1927 | mLooper = looper; |
| 1928 | } |
| 1929 | |
| 1930 | Context getContext() { |
| 1931 | return mContext; |
| 1932 | } |
| 1933 | |
| 1934 | Looper getLooper() { |
| 1935 | return mLooper; |
| 1936 | } |
| 1937 | |
| 1938 | void onBootPhase(int phase) { |
| 1939 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 1940 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 1941 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1942 | mBatteryStats = IBatteryStats.Stub.asInterface( |
| 1943 | ServiceManager.getService(BatteryStats.SERVICE_NAME)); |
| 1944 | mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); |
| 1945 | mDisplayManager = (DisplayManager) mContext.getSystemService( |
| 1946 | Context.DISPLAY_SERVICE); |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 1947 | mPowerManager = mContext.getSystemService(PowerManager.class); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 1948 | mBatteryManager = mContext.getSystemService(BatteryManager.class); |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 1949 | mCrossProfileAppsInternal = LocalServices.getService( |
| 1950 | CrossProfileAppsInternal.class); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1951 | |
| 1952 | final ActivityManager activityManager = |
| 1953 | (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 1954 | if (activityManager.isLowRamDevice() || ActivityManager.isSmallBatteryDevice()) { |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 1955 | mAutoRestrictedBucketDelayMs = 12 * ONE_HOUR; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1956 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1957 | } |
| 1958 | mBootPhase = phase; |
| 1959 | } |
| 1960 | |
| 1961 | int getBootPhase() { |
| 1962 | return mBootPhase; |
| 1963 | } |
| 1964 | |
| 1965 | /** |
| 1966 | * Returns the elapsed realtime since the device started. Override this |
| 1967 | * to control the clock. |
| 1968 | * @return elapsed realtime |
| 1969 | */ |
| 1970 | long elapsedRealtime() { |
| 1971 | return SystemClock.elapsedRealtime(); |
| 1972 | } |
| 1973 | |
| 1974 | long currentTimeMillis() { |
| 1975 | return System.currentTimeMillis(); |
| 1976 | } |
| 1977 | |
| 1978 | boolean isAppIdleEnabled() { |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 1979 | final boolean buildFlag = mContext.getResources().getBoolean( |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1980 | com.android.internal.R.bool.config_enableAutoPowerModes); |
Lei Yu | 4b976ad | 2018-04-19 10:38:58 -0700 | [diff] [blame] | 1981 | final boolean runtimeFlag = Global.getInt(mContext.getContentResolver(), |
| 1982 | Global.APP_STANDBY_ENABLED, 1) == 1 |
| 1983 | && Global.getInt(mContext.getContentResolver(), |
| 1984 | Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 1) == 1; |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 1985 | return buildFlag && runtimeFlag; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 1988 | boolean isCharging() { |
| 1989 | return mBatteryManager.isCharging(); |
| 1990 | } |
| 1991 | |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 1992 | boolean isNonIdleWhitelisted(String packageName) { |
| 1993 | if (mBootPhase < PHASE_SYSTEM_SERVICES_READY) { |
| 1994 | return false; |
| 1995 | } |
| 1996 | synchronized (mPowerWhitelistedApps) { |
| 1997 | return mPowerWhitelistedApps.contains(packageName); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | private void updatePowerWhitelistCache() { |
| 2002 | if (mBootPhase < PHASE_SYSTEM_SERVICES_READY) { |
| 2003 | return; |
| 2004 | } |
| 2005 | try { |
| 2006 | // Don't call out to DeviceIdleController with the lock held. |
| 2007 | final String[] whitelistedPkgs = |
| 2008 | mDeviceIdleController.getFullPowerWhitelistExceptIdle(); |
| 2009 | synchronized (mPowerWhitelistedApps) { |
| 2010 | mPowerWhitelistedApps.clear(); |
| 2011 | final int len = whitelistedPkgs.length; |
| 2012 | for (int i = 0; i < len; ++i) { |
| 2013 | mPowerWhitelistedApps.add(whitelistedPkgs[i]); |
| 2014 | } |
| 2015 | } |
| 2016 | } catch (RemoteException e) { |
| 2017 | // Should not happen. |
| 2018 | Slog.wtf(TAG, "Failed to get power whitelist", e); |
| 2019 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 2022 | boolean isRestrictedBucketEnabled() { |
| 2023 | return Global.getInt(mContext.getContentResolver(), |
| 2024 | Global.ENABLE_RESTRICTED_BUCKET, |
| 2025 | Global.DEFAULT_ENABLE_RESTRICTED_BUCKET) == 1; |
| 2026 | } |
| 2027 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2028 | File getDataSystemDirectory() { |
| 2029 | return Environment.getDataSystemDirectory(); |
| 2030 | } |
| 2031 | |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 2032 | /** |
| 2033 | * Return the minimum amount of time that must have passed since the last user usage before |
| 2034 | * an app can be automatically put into the |
| 2035 | * {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED} bucket. |
| 2036 | */ |
| 2037 | long getAutoRestrictedBucketDelayMs() { |
| 2038 | return mAutoRestrictedBucketDelayMs; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 2039 | } |
| 2040 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2041 | void noteEvent(int event, String packageName, int uid) throws RemoteException { |
| 2042 | mBatteryStats.noteEvent(event, packageName, uid); |
| 2043 | } |
| 2044 | |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 2045 | PackageManagerInternal getPackageManagerInternal() { |
| 2046 | return mPackageManagerInternal; |
| 2047 | } |
| 2048 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2049 | boolean isPackageEphemeral(int userId, String packageName) { |
| 2050 | return mPackageManagerInternal.isPackageEphemeral(userId, packageName); |
| 2051 | } |
| 2052 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 2053 | boolean isPackageInstalled(String packageName, int flags, int userId) { |
| 2054 | return mPackageManagerInternal.getPackageUid(packageName, flags, userId) >= 0; |
| 2055 | } |
| 2056 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2057 | int[] getRunningUserIds() throws RemoteException { |
| 2058 | return ActivityManager.getService().getRunningUserIds(); |
| 2059 | } |
| 2060 | |
| 2061 | boolean isDefaultDisplayOn() { |
| 2062 | return mDisplayManager |
| 2063 | .getDisplay(Display.DEFAULT_DISPLAY).getState() == Display.STATE_ON; |
| 2064 | } |
| 2065 | |
| 2066 | void registerDisplayListener(DisplayManager.DisplayListener listener, Handler handler) { |
| 2067 | mDisplayManager.registerDisplayListener(listener, handler); |
| 2068 | } |
| 2069 | |
| 2070 | String getActiveNetworkScorer() { |
| 2071 | NetworkScoreManager nsm = (NetworkScoreManager) mContext.getSystemService( |
| 2072 | Context.NETWORK_SCORE_SERVICE); |
| 2073 | return nsm.getActiveScorerPackage(); |
| 2074 | } |
| 2075 | |
| 2076 | public boolean isBoundWidgetPackage(AppWidgetManager appWidgetManager, String packageName, |
| 2077 | int userId) { |
| 2078 | return appWidgetManager.isBoundWidgetPackage(packageName, userId); |
| 2079 | } |
Amith Yamasani | 301e94a | 2017-11-17 16:35:44 -0800 | [diff] [blame] | 2080 | |
| 2081 | String getAppIdleSettings() { |
Lei Yu | 4b976ad | 2018-04-19 10:38:58 -0700 | [diff] [blame] | 2082 | return Global.getString(mContext.getContentResolver(), |
| 2083 | Global.APP_IDLE_CONSTANTS); |
Amith Yamasani | 301e94a | 2017-11-17 16:35:44 -0800 | [diff] [blame] | 2084 | } |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 2085 | |
| 2086 | /** Whether the device is in doze or not. */ |
| 2087 | public boolean isDeviceIdleMode() { |
| 2088 | return mPowerManager.isDeviceIdleMode(); |
| 2089 | } |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 2090 | |
| 2091 | public List<UserHandle> getValidCrossProfileTargets(String pkg, int userId) { |
| 2092 | final int uid = mPackageManagerInternal.getPackageUidInternal(pkg, 0, userId); |
Kweku Adams | 259403e | 2020-04-27 09:25:07 -0700 | [diff] [blame] | 2093 | final AndroidPackage aPkg = mPackageManagerInternal.getPackage(uid); |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 2094 | if (uid < 0 |
Kweku Adams | 259403e | 2020-04-27 09:25:07 -0700 | [diff] [blame] | 2095 | || aPkg == null |
| 2096 | || !aPkg.isCrossProfile() |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 2097 | || !mCrossProfileAppsInternal |
| 2098 | .verifyUidHasInteractAcrossProfilePermission(pkg, uid)) { |
Kweku Adams | 259403e | 2020-04-27 09:25:07 -0700 | [diff] [blame] | 2099 | if (uid >= 0 && aPkg == null) { |
| 2100 | Slog.wtf(TAG, "Null package retrieved for UID " + uid); |
| 2101 | } |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 2102 | return Collections.emptyList(); |
| 2103 | } |
| 2104 | return mCrossProfileAppsInternal.getTargetUserProfiles(pkg, userId); |
| 2105 | } |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 2106 | |
| 2107 | void dump(PrintWriter pw) { |
| 2108 | pw.println("mPowerWhitelistedApps=["); |
| 2109 | synchronized (mPowerWhitelistedApps) { |
| 2110 | for (int i = mPowerWhitelistedApps.size() - 1; i >= 0; --i) { |
| 2111 | pw.print(" "); |
| 2112 | pw.print(mPowerWhitelistedApps.valueAt(i)); |
| 2113 | pw.println(","); |
| 2114 | } |
| 2115 | } |
| 2116 | pw.println("]"); |
| 2117 | pw.println(); |
| 2118 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | class AppStandbyHandler extends Handler { |
| 2122 | |
| 2123 | AppStandbyHandler(Looper looper) { |
| 2124 | super(looper); |
| 2125 | } |
| 2126 | |
| 2127 | @Override |
| 2128 | public void handleMessage(Message msg) { |
| 2129 | switch (msg.what) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2130 | case MSG_INFORM_LISTENERS: |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 2131 | StandbyUpdateRecord r = (StandbyUpdateRecord) msg.obj; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 2132 | informListeners(r.packageName, r.userId, r.bucket, r.reason, |
| 2133 | r.isUserInteraction); |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 2134 | r.recycle(); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2135 | break; |
| 2136 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2137 | case MSG_FORCE_IDLE_STATE: |
| 2138 | forceIdleState((String) msg.obj, msg.arg1, msg.arg2 == 1); |
| 2139 | break; |
| 2140 | |
| 2141 | case MSG_CHECK_IDLE_STATES: |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 2142 | if (checkIdleStates(msg.arg1) && mAppIdleEnabled) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2143 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 2144 | MSG_CHECK_IDLE_STATES, msg.arg1, 0), |
| 2145 | mCheckIdleIntervalMillis); |
| 2146 | } |
| 2147 | break; |
| 2148 | |
| 2149 | case MSG_ONE_TIME_CHECK_IDLE_STATES: |
| 2150 | mHandler.removeMessages(MSG_ONE_TIME_CHECK_IDLE_STATES); |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 2151 | waitForAdminData(); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2152 | checkIdleStates(UserHandle.USER_ALL); |
| 2153 | break; |
| 2154 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2155 | case MSG_REPORT_CONTENT_PROVIDER_USAGE: |
| 2156 | SomeArgs args = (SomeArgs) msg.obj; |
| 2157 | reportContentProviderUsage((String) args.arg1, // authority name |
| 2158 | (String) args.arg2, // package name |
| 2159 | (int) args.arg3); // userId |
| 2160 | args.recycle(); |
| 2161 | break; |
| 2162 | |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 2163 | case MSG_PAROLE_STATE_CHANGED: |
| 2164 | if (DEBUG) Slog.d(TAG, "Parole state: " + isInParole()); |
| 2165 | informParoleStateChanged(); |
| 2166 | break; |
| 2167 | |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 2168 | case MSG_CHECK_PACKAGE_IDLE_STATE: |
| 2169 | checkAndUpdateStandbyState((String) msg.obj, msg.arg1, msg.arg2, |
| 2170 | mInjector.elapsedRealtime()); |
| 2171 | break; |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2172 | |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2173 | case MSG_REPORT_SYNC_SCHEDULED: |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 2174 | final boolean exempted = msg.arg2 > 0 ? true : false; |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2175 | if (exempted) { |
| 2176 | reportExemptedSyncScheduled((String) msg.obj, msg.arg1); |
| 2177 | } else { |
| 2178 | reportUnexemptedSyncScheduled((String) msg.obj, msg.arg1); |
| 2179 | } |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 2180 | break; |
| 2181 | |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2182 | case MSG_REPORT_EXEMPTED_SYNC_START: |
| 2183 | reportExemptedSyncStart((String) msg.obj, msg.arg1); |
| 2184 | break; |
| 2185 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2186 | default: |
| 2187 | super.handleMessage(msg); |
| 2188 | break; |
| 2189 | |
| 2190 | } |
| 2191 | } |
| 2192 | }; |
| 2193 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 2194 | private class DeviceStateReceiver extends BroadcastReceiver { |
Michael Wachenschwanz | 113a0fa | 2018-03-30 12:45:18 -0700 | [diff] [blame] | 2195 | @Override |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 2196 | public void onReceive(Context context, Intent intent) { |
| 2197 | switch (intent.getAction()) { |
| 2198 | case BatteryManager.ACTION_CHARGING: |
| 2199 | setChargingState(true); |
| 2200 | break; |
| 2201 | case BatteryManager.ACTION_DISCHARGING: |
| 2202 | setChargingState(false); |
| 2203 | break; |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 2204 | case PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED: |
| 2205 | if (mSystemServicesReady) { |
| 2206 | mHandler.post(mInjector::updatePowerWhitelistCache); |
| 2207 | } |
| 2208 | break; |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 2209 | } |
Michael Wachenschwanz | 113a0fa | 2018-03-30 12:45:18 -0700 | [diff] [blame] | 2210 | } |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 2211 | } |
Michael Wachenschwanz | 113a0fa | 2018-03-30 12:45:18 -0700 | [diff] [blame] | 2212 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2213 | private final DisplayManager.DisplayListener mDisplayListener |
| 2214 | = new DisplayManager.DisplayListener() { |
| 2215 | |
| 2216 | @Override public void onDisplayAdded(int displayId) { |
| 2217 | } |
| 2218 | |
| 2219 | @Override public void onDisplayRemoved(int displayId) { |
| 2220 | } |
| 2221 | |
| 2222 | @Override public void onDisplayChanged(int displayId) { |
| 2223 | if (displayId == Display.DEFAULT_DISPLAY) { |
| 2224 | final boolean displayOn = isDisplayOn(); |
| 2225 | synchronized (mAppIdleLock) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2226 | mAppIdleHistory.updateDisplay(displayOn, mInjector.elapsedRealtime()); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | } |
| 2230 | }; |
| 2231 | |
| 2232 | /** |
Lei Yu | 4b976ad | 2018-04-19 10:38:58 -0700 | [diff] [blame] | 2233 | * Observe settings changes for {@link Global#APP_IDLE_CONSTANTS}. |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2234 | */ |
| 2235 | private class SettingsObserver extends ContentObserver { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2236 | private static final String KEY_SCREEN_TIME_THRESHOLDS = "screen_thresholds"; |
| 2237 | private static final String KEY_ELAPSED_TIME_THRESHOLDS = "elapsed_thresholds"; |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 2238 | private static final String KEY_STRONG_USAGE_HOLD_DURATION = "strong_usage_duration"; |
| 2239 | private static final String KEY_NOTIFICATION_SEEN_HOLD_DURATION = |
| 2240 | "notification_seen_duration"; |
| 2241 | private static final String KEY_SYSTEM_UPDATE_HOLD_DURATION = |
| 2242 | "system_update_usage_duration"; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 2243 | private static final String KEY_PREDICTION_TIMEOUT = "prediction_timeout"; |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 2244 | private static final String KEY_SYNC_ADAPTER_HOLD_DURATION = "sync_adapter_duration"; |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2245 | private static final String KEY_EXEMPTED_SYNC_SCHEDULED_NON_DOZE_HOLD_DURATION = |
| 2246 | "exempted_sync_scheduled_nd_duration"; |
| 2247 | private static final String KEY_EXEMPTED_SYNC_SCHEDULED_DOZE_HOLD_DURATION = |
| 2248 | "exempted_sync_scheduled_d_duration"; |
| 2249 | private static final String KEY_EXEMPTED_SYNC_START_HOLD_DURATION = |
| 2250 | "exempted_sync_start_duration"; |
| 2251 | private static final String KEY_UNEXEMPTED_SYNC_SCHEDULED_HOLD_DURATION = |
| 2252 | "unexempted_sync_scheduled_duration"; |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 2253 | private static final String KEY_SYSTEM_INTERACTION_HOLD_DURATION = |
| 2254 | "system_interaction_duration"; |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 2255 | private static final String KEY_INITIAL_FOREGROUND_SERVICE_START_HOLD_DURATION = |
| 2256 | "initial_foreground_service_start_duration"; |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 2257 | private static final String KEY_AUTO_RESTRICTED_BUCKET_DELAY_MS = |
| 2258 | "auto_restricted_bucket_delay_ms"; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 2259 | private static final String KEY_CROSS_PROFILE_APPS_SHARE_STANDBY_BUCKETS = |
| 2260 | "cross_profile_apps_share_standby_buckets"; |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 2261 | public static final long DEFAULT_STRONG_USAGE_TIMEOUT = 1 * ONE_HOUR; |
| 2262 | public static final long DEFAULT_NOTIFICATION_TIMEOUT = 12 * ONE_HOUR; |
| 2263 | public static final long DEFAULT_SYSTEM_UPDATE_TIMEOUT = 2 * ONE_HOUR; |
| 2264 | public static final long DEFAULT_SYSTEM_INTERACTION_TIMEOUT = 10 * ONE_MINUTE; |
| 2265 | public static final long DEFAULT_SYNC_ADAPTER_TIMEOUT = 10 * ONE_MINUTE; |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 2266 | public static final long DEFAULT_EXEMPTED_SYNC_SCHEDULED_NON_DOZE_TIMEOUT = 10 * ONE_MINUTE; |
| 2267 | public static final long DEFAULT_EXEMPTED_SYNC_SCHEDULED_DOZE_TIMEOUT = 4 * ONE_HOUR; |
| 2268 | public static final long DEFAULT_EXEMPTED_SYNC_START_TIMEOUT = 10 * ONE_MINUTE; |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2269 | public static final long DEFAULT_UNEXEMPTED_SYNC_SCHEDULED_TIMEOUT = 10 * ONE_MINUTE; |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 2270 | public static final long DEFAULT_INITIAL_FOREGROUND_SERVICE_START_TIMEOUT = 30 * ONE_MINUTE; |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 2271 | public static final long DEFAULT_AUTO_RESTRICTED_BUCKET_DELAY_MS = ONE_DAY; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 2272 | public static final boolean DEFAULT_CROSS_PROFILE_APPS_SHARE_STANDBY_BUCKETS = true; |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2273 | |
| 2274 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 2275 | |
| 2276 | SettingsObserver(Handler handler) { |
| 2277 | super(handler); |
| 2278 | } |
| 2279 | |
| 2280 | void registerObserver() { |
Lei Yu | 4b976ad | 2018-04-19 10:38:58 -0700 | [diff] [blame] | 2281 | final ContentResolver cr = mContext.getContentResolver(); |
| 2282 | cr.registerContentObserver(Global.getUriFor(Global.APP_IDLE_CONSTANTS), false, this); |
| 2283 | cr.registerContentObserver(Global.getUriFor(Global.APP_STANDBY_ENABLED), false, this); |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 2284 | cr.registerContentObserver(Global.getUriFor(Global.ENABLE_RESTRICTED_BUCKET), |
| 2285 | false, this); |
Lei Yu | 4b976ad | 2018-04-19 10:38:58 -0700 | [diff] [blame] | 2286 | cr.registerContentObserver(Global.getUriFor(Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED), |
| 2287 | false, this); |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | @Override |
| 2291 | public void onChange(boolean selfChange) { |
| 2292 | updateSettings(); |
| 2293 | postOneTimeCheckIdleStates(); |
| 2294 | } |
| 2295 | |
| 2296 | void updateSettings() { |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 2297 | if (DEBUG) { |
| 2298 | Slog.d(TAG, |
Lei Yu | 4b976ad | 2018-04-19 10:38:58 -0700 | [diff] [blame] | 2299 | "appidle=" + Global.getString(mContext.getContentResolver(), |
| 2300 | Global.APP_STANDBY_ENABLED)); |
| 2301 | Slog.d(TAG, |
| 2302 | "adaptivebat=" + Global.getString(mContext.getContentResolver(), |
| 2303 | Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED)); |
| 2304 | Slog.d(TAG, "appidleconstants=" + Global.getString( |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 2305 | mContext.getContentResolver(), |
Lei Yu | 4b976ad | 2018-04-19 10:38:58 -0700 | [diff] [blame] | 2306 | Global.APP_IDLE_CONSTANTS)); |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 2307 | } |
Amith Yamasani | 172612c | 2017-12-15 10:51:53 -0800 | [diff] [blame] | 2308 | |
| 2309 | // Look at global settings for this. |
| 2310 | // TODO: Maybe apply different thresholds for different users. |
| 2311 | try { |
| 2312 | mParser.setString(mInjector.getAppIdleSettings()); |
| 2313 | } catch (IllegalArgumentException e) { |
| 2314 | Slog.e(TAG, "Bad value for app idle settings: " + e.getMessage()); |
| 2315 | // fallthrough, mParser is empty and all defaults will be returned. |
| 2316 | } |
| 2317 | |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2318 | synchronized (mAppIdleLock) { |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2319 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2320 | String screenThresholdsValue = mParser.getString(KEY_SCREEN_TIME_THRESHOLDS, null); |
| 2321 | mAppStandbyScreenThresholds = parseLongArray(screenThresholdsValue, |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 2322 | SCREEN_TIME_THRESHOLDS, MINIMUM_SCREEN_TIME_THRESHOLDS); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2323 | |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 2324 | String elapsedThresholdsValue = mParser.getString(KEY_ELAPSED_TIME_THRESHOLDS, |
| 2325 | null); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2326 | mAppStandbyElapsedThresholds = parseLongArray(elapsedThresholdsValue, |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 2327 | ELAPSED_TIME_THRESHOLDS, MINIMUM_ELAPSED_TIME_THRESHOLDS); |
Amith Yamasani | a0ef1ca | 2017-11-20 09:43:56 -0800 | [diff] [blame] | 2328 | mCheckIdleIntervalMillis = Math.min(mAppStandbyElapsedThresholds[1] / 4, |
| 2329 | COMPRESS_TIME ? ONE_MINUTE : 4 * 60 * ONE_MINUTE); // 4 hours |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2330 | mStrongUsageTimeoutMillis = mParser.getDurationMillis( |
| 2331 | KEY_STRONG_USAGE_HOLD_DURATION, |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 2332 | COMPRESS_TIME ? ONE_MINUTE : DEFAULT_STRONG_USAGE_TIMEOUT); |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2333 | mNotificationSeenTimeoutMillis = mParser.getDurationMillis( |
| 2334 | KEY_NOTIFICATION_SEEN_HOLD_DURATION, |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 2335 | COMPRESS_TIME ? 12 * ONE_MINUTE : DEFAULT_NOTIFICATION_TIMEOUT); |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2336 | mSystemUpdateUsageTimeoutMillis = mParser.getDurationMillis( |
| 2337 | KEY_SYSTEM_UPDATE_HOLD_DURATION, |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 2338 | COMPRESS_TIME ? 2 * ONE_MINUTE : DEFAULT_SYSTEM_UPDATE_TIMEOUT); |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2339 | mPredictionTimeoutMillis = mParser.getDurationMillis( |
| 2340 | KEY_PREDICTION_TIMEOUT, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 2341 | COMPRESS_TIME ? 10 * ONE_MINUTE : DEFAULT_PREDICTION_TIMEOUT); |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2342 | mSyncAdapterTimeoutMillis = mParser.getDurationMillis( |
| 2343 | KEY_SYNC_ADAPTER_HOLD_DURATION, |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 2344 | COMPRESS_TIME ? ONE_MINUTE : DEFAULT_SYNC_ADAPTER_TIMEOUT); |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 2345 | |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2346 | mExemptedSyncScheduledNonDozeTimeoutMillis = mParser.getDurationMillis( |
| 2347 | KEY_EXEMPTED_SYNC_SCHEDULED_NON_DOZE_HOLD_DURATION, |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 2348 | COMPRESS_TIME ? (ONE_MINUTE / 2) |
| 2349 | : DEFAULT_EXEMPTED_SYNC_SCHEDULED_NON_DOZE_TIMEOUT); |
| 2350 | |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2351 | mExemptedSyncScheduledDozeTimeoutMillis = mParser.getDurationMillis( |
| 2352 | KEY_EXEMPTED_SYNC_SCHEDULED_DOZE_HOLD_DURATION, |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 2353 | COMPRESS_TIME ? ONE_MINUTE |
| 2354 | : DEFAULT_EXEMPTED_SYNC_SCHEDULED_DOZE_TIMEOUT); |
| 2355 | |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2356 | mExemptedSyncStartTimeoutMillis = mParser.getDurationMillis( |
| 2357 | KEY_EXEMPTED_SYNC_START_HOLD_DURATION, |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 2358 | COMPRESS_TIME ? ONE_MINUTE |
| 2359 | : DEFAULT_EXEMPTED_SYNC_START_TIMEOUT); |
| 2360 | |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2361 | mUnexemptedSyncScheduledTimeoutMillis = mParser.getDurationMillis( |
Kweku Adams | 0140ff8 | 2019-10-08 17:04:28 -0700 | [diff] [blame] | 2362 | KEY_UNEXEMPTED_SYNC_SCHEDULED_HOLD_DURATION, |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 2363 | COMPRESS_TIME |
| 2364 | ? ONE_MINUTE : DEFAULT_UNEXEMPTED_SYNC_SCHEDULED_TIMEOUT); |
Michael Wachenschwanz | c329520 | 2019-02-20 17:19:52 -0800 | [diff] [blame] | 2365 | |
| 2366 | mSystemInteractionTimeoutMillis = mParser.getDurationMillis( |
| 2367 | KEY_SYSTEM_INTERACTION_HOLD_DURATION, |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 2368 | COMPRESS_TIME ? ONE_MINUTE : DEFAULT_SYSTEM_INTERACTION_TIMEOUT); |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 2369 | |
| 2370 | mInitialForegroundServiceStartTimeoutMillis = mParser.getDurationMillis( |
| 2371 | KEY_INITIAL_FOREGROUND_SERVICE_START_HOLD_DURATION, |
| 2372 | COMPRESS_TIME ? ONE_MINUTE : |
| 2373 | DEFAULT_INITIAL_FOREGROUND_SERVICE_START_TIMEOUT); |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 2374 | |
| 2375 | mInjector.mAutoRestrictedBucketDelayMs = Math.max( |
| 2376 | COMPRESS_TIME ? ONE_MINUTE : 2 * ONE_HOUR, |
| 2377 | mParser.getDurationMillis(KEY_AUTO_RESTRICTED_BUCKET_DELAY_MS, |
| 2378 | COMPRESS_TIME |
| 2379 | ? ONE_MINUTE : DEFAULT_AUTO_RESTRICTED_BUCKET_DELAY_MS)); |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 2380 | |
| 2381 | mLinkCrossProfileApps = mParser.getBoolean( |
| 2382 | KEY_CROSS_PROFILE_APPS_SHARE_STANDBY_BUCKETS, |
| 2383 | DEFAULT_CROSS_PROFILE_APPS_SHARE_STANDBY_BUCKETS); |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 2384 | |
| 2385 | mAllowRestrictedBucket = mInjector.isRestrictedBucketEnabled(); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2386 | } |
Kweku Adams | 1e8947c | 2018-11-05 18:06:13 -0800 | [diff] [blame] | 2387 | |
| 2388 | // Check if app_idle_enabled has changed. Do this after getting the rest of the settings |
| 2389 | // in case we need to change something based on the new values. |
| 2390 | setAppIdleEnabled(mInjector.isAppIdleEnabled()); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2391 | } |
| 2392 | |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 2393 | long[] parseLongArray(String values, long[] defaults, long[] minValues) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2394 | if (values == null) return defaults; |
| 2395 | if (values.isEmpty()) { |
| 2396 | // Reset to defaults |
| 2397 | return defaults; |
| 2398 | } else { |
| 2399 | String[] thresholds = values.split("/"); |
| 2400 | if (thresholds.length == THRESHOLD_BUCKETS.length) { |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 2401 | if (minValues.length != THRESHOLD_BUCKETS.length) { |
| 2402 | Slog.wtf(TAG, "minValues array is the wrong size"); |
| 2403 | // Use zeroes as the minimums. |
| 2404 | minValues = new long[THRESHOLD_BUCKETS.length]; |
| 2405 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2406 | long[] array = new long[THRESHOLD_BUCKETS.length]; |
| 2407 | for (int i = 0; i < THRESHOLD_BUCKETS.length; i++) { |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 2408 | try { |
| 2409 | if (thresholds[i].startsWith("P") || thresholds[i].startsWith("p")) { |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 2410 | array[i] = Math.max(minValues[i], |
| 2411 | Duration.parse(thresholds[i]).toMillis()); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 2412 | } else { |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 2413 | array[i] = Math.max(minValues[i], Long.parseLong(thresholds[i])); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 2414 | } |
| 2415 | } catch (NumberFormatException|DateTimeParseException e) { |
| 2416 | return defaults; |
| 2417 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 2418 | } |
| 2419 | return array; |
| 2420 | } else { |
| 2421 | return defaults; |
| 2422 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | } |
Amith Yamasani | 7e528f3 | 2017-10-05 16:06:16 -0700 | [diff] [blame] | 2426 | } |