blob: 74f79e0d40f520d2eb9a8f6aa78f7270250f693d [file] [log] [blame]
Makoto Onuki9be01402017-11-10 13:22:26 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server;
17
18import android.annotation.NonNull;
Makoto Onuki9be01402017-11-10 13:22:26 -080019import android.app.ActivityManager;
Makoto Onuki15407842018-01-19 14:23:11 -080020import android.app.ActivityManagerInternal;
Makoto Onuki9be01402017-11-10 13:22:26 -080021import android.app.AppOpsManager;
22import android.app.AppOpsManager.PackageOps;
Makoto Onuki2206af32017-11-21 16:25:35 -080023import android.app.IActivityManager;
Makoto Onuki9be01402017-11-10 13:22:26 -080024import android.app.IUidObserver;
Makoto Onukieb898f12018-01-23 15:26:27 -080025import android.app.usage.UsageStatsManager;
26import android.app.usage.UsageStatsManagerInternal;
Makoto Onuki2206af32017-11-21 16:25:35 -080027import android.content.BroadcastReceiver;
Makoto Onuki9be01402017-11-10 13:22:26 -080028import android.content.Context;
Makoto Onuki2206af32017-11-21 16:25:35 -080029import android.content.Intent;
30import android.content.IntentFilter;
Suprabh Shukla4deb8522018-01-08 16:27:10 -080031import android.database.ContentObserver;
Nancy Zheng525aaa12018-01-12 11:45:37 -080032import android.net.Uri;
33import android.os.BatteryManager;
Makoto Onuki9be01402017-11-10 13:22:26 -080034import android.os.Handler;
Makoto Onuki2206af32017-11-21 16:25:35 -080035import android.os.Looper;
36import android.os.Message;
Makoto Onuki9be01402017-11-10 13:22:26 -080037import android.os.PowerManager.ServiceType;
38import android.os.PowerManagerInternal;
39import android.os.RemoteException;
40import android.os.ServiceManager;
41import android.os.UserHandle;
Suprabh Shukla4deb8522018-01-08 16:27:10 -080042import android.provider.Settings;
Makoto Onuki9be01402017-11-10 13:22:26 -080043import android.util.ArraySet;
44import android.util.Pair;
Suprabh Shukla4deb8522018-01-08 16:27:10 -080045import android.util.Slog;
Makoto Onuki9be01402017-11-10 13:22:26 -080046import android.util.SparseBooleanArray;
Makoto Onukieb898f12018-01-23 15:26:27 -080047import android.util.SparseSetArray;
Makoto Onuki2206af32017-11-21 16:25:35 -080048import android.util.proto.ProtoOutputStream;
Makoto Onuki9be01402017-11-10 13:22:26 -080049
50import com.android.internal.annotations.GuardedBy;
Makoto Onuki2206af32017-11-21 16:25:35 -080051import com.android.internal.annotations.VisibleForTesting;
Makoto Onuki9be01402017-11-10 13:22:26 -080052import com.android.internal.app.IAppOpsCallback;
53import com.android.internal.app.IAppOpsService;
Makoto Onuki2206af32017-11-21 16:25:35 -080054import com.android.internal.util.ArrayUtils;
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -070055import com.android.internal.util.IndentingPrintWriter;
Makoto Onuki9be01402017-11-10 13:22:26 -080056import com.android.internal.util.Preconditions;
Makoto Onuki49392d32018-04-11 13:51:02 -070057import com.android.internal.util.StatLogger;
Kweku Adamsc3f9ba62019-08-01 12:14:55 -070058import com.android.server.AppStateTrackerProto.ExemptedPackage;
59import com.android.server.AppStateTrackerProto.RunAnyInBackgroundRestrictedPackages;
Christopher Tateb909c4d52019-10-21 12:50:37 -070060import com.android.server.usage.AppStandbyInternal;
61import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener;
Makoto Onuki9be01402017-11-10 13:22:26 -080062
Makoto Onuki2206af32017-11-21 16:25:35 -080063import java.io.PrintWriter;
64import java.util.Arrays;
Makoto Onuki9be01402017-11-10 13:22:26 -080065import java.util.List;
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +000066import java.util.Objects;
Makoto Onuki9be01402017-11-10 13:22:26 -080067
68/**
Makoto Onuki2206af32017-11-21 16:25:35 -080069 * Class to keep track of the information related to "force app standby", which includes:
70 * - OP_RUN_ANY_IN_BACKGROUND for each package
Makoto Onukiadb50d82018-01-29 16:20:30 -080071 * - UID foreground/active state
Makoto Onuki2206af32017-11-21 16:25:35 -080072 * - User+system power save whitelist
73 * - Temporary power save whitelist
74 * - Global "force all apps standby" mode enforced by battery saver.
Makoto Onuki9be01402017-11-10 13:22:26 -080075 *
Kweku Adamsdf33ae12019-10-08 11:51:41 -070076 * Test: atest com.android.server.AppStateTrackerTest
Makoto Onuki9be01402017-11-10 13:22:26 -080077 */
Makoto Onukie4918212018-02-06 11:30:15 -080078public class AppStateTracker {
Makoto Onukic871c872018-03-19 09:39:56 -070079 private static final String TAG = "AppStateTracker";
80 private static final boolean DEBUG = false;
Makoto Onuki9be01402017-11-10 13:22:26 -080081
82 private final Object mLock = new Object();
83 private final Context mContext;
84
Makoto Onuki2206af32017-11-21 16:25:35 -080085 @VisibleForTesting
86 static final int TARGET_OP = AppOpsManager.OP_RUN_ANY_IN_BACKGROUND;
87
88 IActivityManager mIActivityManager;
Makoto Onukie4918212018-02-06 11:30:15 -080089 ActivityManagerInternal mActivityManagerInternal;
Makoto Onuki9be01402017-11-10 13:22:26 -080090 AppOpsManager mAppOpsManager;
91 IAppOpsService mAppOpsService;
92 PowerManagerInternal mPowerManagerInternal;
Makoto Onukieb898f12018-01-23 15:26:27 -080093 StandbyTracker mStandbyTracker;
Christopher Tateb909c4d52019-10-21 12:50:37 -070094 AppStandbyInternal mAppStandbyInternal;
Makoto Onuki9be01402017-11-10 13:22:26 -080095
Makoto Onuki2206af32017-11-21 16:25:35 -080096 private final MyHandler mHandler;
Makoto Onuki9be01402017-11-10 13:22:26 -080097
Nancy Zheng525aaa12018-01-12 11:45:37 -080098 @VisibleForTesting
99 FeatureFlagsObserver mFlagsObserver;
100
Makoto Onuki9be01402017-11-10 13:22:26 -0800101 /**
102 * Pair of (uid (not user-id), packageName) with OP_RUN_ANY_IN_BACKGROUND *not* allowed.
103 */
104 @GuardedBy("mLock")
Makoto Onuki2206af32017-11-21 16:25:35 -0800105 final ArraySet<Pair<Integer, String>> mRunAnyRestrictedPackages = new ArraySet<>();
Makoto Onuki9be01402017-11-10 13:22:26 -0800106
Makoto Onukiadb50d82018-01-29 16:20:30 -0800107 /** UIDs that are active. */
108 @GuardedBy("mLock")
109 final SparseBooleanArray mActiveUids = new SparseBooleanArray();
110
111 /** UIDs that are in the foreground. */
Makoto Onuki9be01402017-11-10 13:22:26 -0800112 @GuardedBy("mLock")
113 final SparseBooleanArray mForegroundUids = new SparseBooleanArray();
114
Makoto Onuki71755c92018-01-16 14:15:44 -0800115 /**
116 * System except-idle + user whitelist in the device idle controller.
117 */
Makoto Onuki9be01402017-11-10 13:22:26 -0800118 @GuardedBy("mLock")
Makoto Onuki2206af32017-11-21 16:25:35 -0800119 private int[] mPowerWhitelistedAllAppIds = new int[0];
120
Suprabh Shukla5bf49812018-05-24 18:38:50 -0700121 /**
122 * User whitelisted apps in the device idle controller.
123 */
124 @GuardedBy("mLock")
125 private int[] mPowerWhitelistedUserAppIds = new int[0];
126
Makoto Onuki2206af32017-11-21 16:25:35 -0800127 @GuardedBy("mLock")
128 private int[] mTempWhitelistedAppIds = mPowerWhitelistedAllAppIds;
129
Makoto Onukieb898f12018-01-23 15:26:27 -0800130 /**
131 * Per-user packages that are in the EXEMPT bucket.
132 */
133 @GuardedBy("mLock")
134 private final SparseSetArray<String> mExemptedPackages = new SparseSetArray<>();
135
Makoto Onuki2206af32017-11-21 16:25:35 -0800136 @GuardedBy("mLock")
Makoto Onuki9be01402017-11-10 13:22:26 -0800137 final ArraySet<Listener> mListeners = new ArraySet<>();
138
139 @GuardedBy("mLock")
140 boolean mStarted;
141
Nancy Zheng525aaa12018-01-12 11:45:37 -0800142 /**
143 * Only used for small battery use-case.
144 */
Makoto Onuki9be01402017-11-10 13:22:26 -0800145 @GuardedBy("mLock")
Nancy Zheng525aaa12018-01-12 11:45:37 -0800146 boolean mIsPluggedIn;
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800147
148 @GuardedBy("mLock")
Nancy Zheng525aaa12018-01-12 11:45:37 -0800149 boolean mBatterySaverEnabled;
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800150
Nancy Zheng525aaa12018-01-12 11:45:37 -0800151 /**
152 * True if the forced app standby is currently enabled
153 */
154 @GuardedBy("mLock")
155 boolean mForceAllAppsStandby;
156
157 /**
158 * True if the forced app standby for small battery devices feature is enabled in settings
159 */
160 @GuardedBy("mLock")
161 boolean mForceAllAppStandbyForSmallBattery;
162
163 /**
164 * True if the forced app standby feature is enabled in settings
165 */
166 @GuardedBy("mLock")
167 boolean mForcedAppStandbyEnabled;
168
Makoto Onukieb898f12018-01-23 15:26:27 -0800169 interface Stats {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800170 int UID_FG_STATE_CHANGED = 0;
171 int UID_ACTIVE_STATE_CHANGED = 1;
172 int RUN_ANY_CHANGED = 2;
173 int ALL_UNWHITELISTED = 3;
174 int ALL_WHITELIST_CHANGED = 4;
175 int TEMP_WHITELIST_CHANGED = 5;
176 int EXEMPT_CHANGED = 6;
177 int FORCE_ALL_CHANGED = 7;
178 int FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED = 8;
Makoto Onukie4918212018-02-06 11:30:15 -0800179
180 int IS_UID_ACTIVE_CACHED = 9;
181 int IS_UID_ACTIVE_RAW = 10;
Makoto Onukieb898f12018-01-23 15:26:27 -0800182 }
183
184 private final StatLogger mStatLogger = new StatLogger(new String[] {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800185 "UID_FG_STATE_CHANGED",
186 "UID_ACTIVE_STATE_CHANGED",
Makoto Onukieb898f12018-01-23 15:26:27 -0800187 "RUN_ANY_CHANGED",
188 "ALL_UNWHITELISTED",
189 "ALL_WHITELIST_CHANGED",
190 "TEMP_WHITELIST_CHANGED",
191 "EXEMPT_CHANGED",
192 "FORCE_ALL_CHANGED",
193 "FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED",
Makoto Onukie4918212018-02-06 11:30:15 -0800194
195 "IS_UID_ACTIVE_CACHED",
196 "IS_UID_ACTIVE_RAW",
Makoto Onukieb898f12018-01-23 15:26:27 -0800197 });
198
Nancy Zheng525aaa12018-01-12 11:45:37 -0800199 @VisibleForTesting
200 class FeatureFlagsObserver extends ContentObserver {
201 FeatureFlagsObserver() {
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800202 super(null);
203 }
204
205 void register() {
206 mContext.getContentResolver().registerContentObserver(
207 Settings.Global.getUriFor(Settings.Global.FORCED_APP_STANDBY_ENABLED),
208 false, this);
Nancy Zheng525aaa12018-01-12 11:45:37 -0800209
210 mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor(
211 Settings.Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED), false, this);
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800212 }
213
214 boolean isForcedAppStandbyEnabled() {
Makoto Onukieb898f12018-01-23 15:26:27 -0800215 return injectGetGlobalSettingInt(Settings.Global.FORCED_APP_STANDBY_ENABLED, 1) == 1;
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800216 }
217
Nancy Zheng525aaa12018-01-12 11:45:37 -0800218 boolean isForcedAppStandbyForSmallBatteryEnabled() {
Makoto Onukieb898f12018-01-23 15:26:27 -0800219 return injectGetGlobalSettingInt(
Nancy Zheng525aaa12018-01-12 11:45:37 -0800220 Settings.Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED, 0) == 1;
221 }
222
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800223 @Override
Nancy Zheng525aaa12018-01-12 11:45:37 -0800224 public void onChange(boolean selfChange, Uri uri) {
225 if (Settings.Global.getUriFor(Settings.Global.FORCED_APP_STANDBY_ENABLED).equals(uri)) {
226 final boolean enabled = isForcedAppStandbyEnabled();
227 synchronized (mLock) {
228 if (mForcedAppStandbyEnabled == enabled) {
229 return;
230 }
231 mForcedAppStandbyEnabled = enabled;
232 if (DEBUG) {
233 Slog.d(TAG,"Forced app standby feature flag changed: "
234 + mForcedAppStandbyEnabled);
235 }
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800236 }
Nancy Zheng525aaa12018-01-12 11:45:37 -0800237 mHandler.notifyForcedAppStandbyFeatureFlagChanged();
238 } else if (Settings.Global.getUriFor(
239 Settings.Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED).equals(uri)) {
240 final boolean enabled = isForcedAppStandbyForSmallBatteryEnabled();
241 synchronized (mLock) {
242 if (mForceAllAppStandbyForSmallBattery == enabled) {
243 return;
244 }
245 mForceAllAppStandbyForSmallBattery = enabled;
246 if (DEBUG) {
247 Slog.d(TAG, "Forced app standby for small battery feature flag changed: "
248 + mForceAllAppStandbyForSmallBattery);
249 }
250 updateForceAllAppStandbyState();
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800251 }
Nancy Zheng525aaa12018-01-12 11:45:37 -0800252 } else {
253 Slog.w(TAG, "Unexpected feature flag uri encountered: " + uri);
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800254 }
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800255 }
256 }
Makoto Onuki9be01402017-11-10 13:22:26 -0800257
258 public static abstract class Listener {
Makoto Onuki2206af32017-11-21 16:25:35 -0800259 /**
260 * This is called when the OP_RUN_ANY_IN_BACKGROUND appops changed for a package.
261 */
Makoto Onukie4918212018-02-06 11:30:15 -0800262 private void onRunAnyAppOpsChanged(AppStateTracker sender,
Makoto Onuki2206af32017-11-21 16:25:35 -0800263 int uid, @NonNull String packageName) {
Christopher Tate20afddd2018-02-28 15:19:19 -0800264 updateJobsForUidPackage(uid, packageName, sender.isUidActive(uid));
Makoto Onuki2206af32017-11-21 16:25:35 -0800265
Suprabh Shuklac25447d2018-01-19 16:43:35 -0800266 if (!sender.areAlarmsRestricted(uid, packageName, /*allowWhileIdle=*/ false)) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800267 unblockAlarmsForUidPackage(uid, packageName);
Suprabh Shuklac25447d2018-01-19 16:43:35 -0800268 } else if (!sender.areAlarmsRestricted(uid, packageName, /*allowWhileIdle=*/ true)){
269 // we need to deliver the allow-while-idle alarms for this uid, package
270 unblockAllUnrestrictedAlarms();
Makoto Onuki2206af32017-11-21 16:25:35 -0800271 }
Christopher Tatec7933ac2018-03-12 17:57:09 -0700272
273 if (!sender.isRunAnyInBackgroundAppOpsAllowed(uid, packageName)) {
274 Slog.v(TAG, "Package " + packageName + "/" + uid
275 + " toggled into fg service restriction");
276 stopForegroundServicesForUidPackage(uid, packageName);
277 }
Makoto Onuki9be01402017-11-10 13:22:26 -0800278 }
279
Makoto Onuki2206af32017-11-21 16:25:35 -0800280 /**
281 * This is called when the foreground state changed for a UID.
282 */
Makoto Onukie4918212018-02-06 11:30:15 -0800283 private void onUidForegroundStateChanged(AppStateTracker sender, int uid) {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800284 onUidForeground(uid, sender.isUidInForeground(uid));
285 }
286
287 /**
288 * This is called when the active/idle state changed for a UID.
289 */
Makoto Onukie4918212018-02-06 11:30:15 -0800290 private void onUidActiveStateChanged(AppStateTracker sender, int uid) {
Christopher Tate20afddd2018-02-28 15:19:19 -0800291 final boolean isActive = sender.isUidActive(uid);
Makoto Onuki2206af32017-11-21 16:25:35 -0800292
Christopher Tate20afddd2018-02-28 15:19:19 -0800293 updateJobsForUid(uid, isActive);
294
295 if (isActive) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800296 unblockAlarmsForUid(uid);
297 }
298 }
299
300 /**
301 * This is called when an app-id(s) is removed from the power save whitelist.
302 */
Makoto Onukie4918212018-02-06 11:30:15 -0800303 private void onPowerSaveUnwhitelisted(AppStateTracker sender) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800304 updateAllJobs();
305 unblockAllUnrestrictedAlarms();
306 }
307
308 /**
309 * This is called when the power save whitelist changes, excluding the
310 * {@link #onPowerSaveUnwhitelisted} case.
311 */
Makoto Onukie4918212018-02-06 11:30:15 -0800312 private void onPowerSaveWhitelistedChanged(AppStateTracker sender) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800313 updateAllJobs();
314 }
315
316 /**
317 * This is called when the temp whitelist changes.
318 */
Makoto Onukie4918212018-02-06 11:30:15 -0800319 private void onTempPowerSaveWhitelistChanged(AppStateTracker sender) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800320
321 // TODO This case happens rather frequently; consider optimizing and update jobs
322 // only for affected app-ids.
323
324 updateAllJobs();
Makoto Onukieb898f12018-01-23 15:26:27 -0800325
326 // Note when an app is just put in the temp whitelist, we do *not* drain pending alarms.
327 }
328
329 /**
330 * This is called when the EXEMPT bucket is updated.
331 */
Makoto Onukie4918212018-02-06 11:30:15 -0800332 private void onExemptChanged(AppStateTracker sender) {
Makoto Onukieb898f12018-01-23 15:26:27 -0800333 // This doesn't happen very often, so just re-evaluate all jobs / alarms.
334 updateAllJobs();
335 unblockAllUnrestrictedAlarms();
Makoto Onuki2206af32017-11-21 16:25:35 -0800336 }
337
338 /**
339 * This is called when the global "force all apps standby" flag changes.
340 */
Makoto Onukie4918212018-02-06 11:30:15 -0800341 private void onForceAllAppsStandbyChanged(AppStateTracker sender) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800342 updateAllJobs();
343
344 if (!sender.isForceAllAppsStandbyEnabled()) {
345 unblockAllUnrestrictedAlarms();
346 }
347 }
348
349 /**
350 * Called when the job restrictions for multiple UIDs might have changed, so the job
351 * scheduler should re-evaluate all restrictions for all jobs.
352 */
353 public void updateAllJobs() {
354 }
355
356 /**
357 * Called when the job restrictions for a UID might have changed, so the job
358 * scheduler should re-evaluate all restrictions for all jobs.
359 */
Christopher Tate20afddd2018-02-28 15:19:19 -0800360 public void updateJobsForUid(int uid, boolean isNowActive) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800361 }
362
363 /**
364 * Called when the job restrictions for a UID - package might have changed, so the job
365 * scheduler should re-evaluate all restrictions for all jobs.
366 */
Christopher Tate20afddd2018-02-28 15:19:19 -0800367 public void updateJobsForUidPackage(int uid, String packageName, boolean isNowActive) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800368 }
369
370 /**
Christopher Tatec7933ac2018-03-12 17:57:09 -0700371 * Called when an app goes into forced app standby and its foreground
372 * services need to be removed from that state.
373 */
374 public void stopForegroundServicesForUidPackage(int uid, String packageName) {
375 }
376
377 /**
Makoto Onuki2206af32017-11-21 16:25:35 -0800378 * Called when the job restrictions for multiple UIDs might have changed, so the alarm
379 * manager should re-evaluate all restrictions for all blocked jobs.
380 */
381 public void unblockAllUnrestrictedAlarms() {
382 }
383
384 /**
385 * Called when all jobs for a specific UID are unblocked.
386 */
387 public void unblockAlarmsForUid(int uid) {
388 }
389
390 /**
391 * Called when all alarms for a specific UID - package are unblocked.
392 */
393 public void unblockAlarmsForUidPackage(int uid, String packageName) {
Makoto Onuki9be01402017-11-10 13:22:26 -0800394 }
Makoto Onukiadb50d82018-01-29 16:20:30 -0800395
396 /**
397 * Called when a UID comes into the foreground or the background.
398 *
399 * @see #isUidInForeground(int)
400 */
401 public void onUidForeground(int uid, boolean foreground) {
402 }
Makoto Onuki9be01402017-11-10 13:22:26 -0800403 }
404
Makoto Onukie4918212018-02-06 11:30:15 -0800405 public AppStateTracker(Context context, Looper looper) {
Makoto Onuki9be01402017-11-10 13:22:26 -0800406 mContext = context;
Makoto Onuki2206af32017-11-21 16:25:35 -0800407 mHandler = new MyHandler(looper);
408 }
409
Makoto Onuki9be01402017-11-10 13:22:26 -0800410 /**
411 * Call it when the system is ready.
412 */
Makoto Onukie4918212018-02-06 11:30:15 -0800413 public void onSystemServicesReady() {
Makoto Onuki9be01402017-11-10 13:22:26 -0800414 synchronized (mLock) {
415 if (mStarted) {
416 return;
417 }
418 mStarted = true;
419
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +0000420 mIActivityManager = Objects.requireNonNull(injectIActivityManager());
421 mActivityManagerInternal = Objects.requireNonNull(injectActivityManagerInternal());
422 mAppOpsManager = Objects.requireNonNull(injectAppOpsManager());
423 mAppOpsService = Objects.requireNonNull(injectIAppOpsService());
424 mPowerManagerInternal = Objects.requireNonNull(injectPowerManagerInternal());
425 mAppStandbyInternal = Objects.requireNonNull(injectAppStandbyInternal());
Makoto Onukieb898f12018-01-23 15:26:27 -0800426
Nancy Zheng525aaa12018-01-12 11:45:37 -0800427 mFlagsObserver = new FeatureFlagsObserver();
428 mFlagsObserver.register();
429 mForcedAppStandbyEnabled = mFlagsObserver.isForcedAppStandbyEnabled();
430 mForceAllAppStandbyForSmallBattery =
431 mFlagsObserver.isForcedAppStandbyForSmallBatteryEnabled();
Makoto Onukieb898f12018-01-23 15:26:27 -0800432 mStandbyTracker = new StandbyTracker();
Christopher Tateb909c4d52019-10-21 12:50:37 -0700433 mAppStandbyInternal.addListener(mStandbyTracker);
Makoto Onuki9be01402017-11-10 13:22:26 -0800434
435 try {
Makoto Onuki2206af32017-11-21 16:25:35 -0800436 mIActivityManager.registerUidObserver(new UidObserver(),
Makoto Onukiadb50d82018-01-29 16:20:30 -0800437 ActivityManager.UID_OBSERVER_GONE
438 | ActivityManager.UID_OBSERVER_IDLE
439 | ActivityManager.UID_OBSERVER_ACTIVE
440 | ActivityManager.UID_OBSERVER_PROCSTATE,
Makoto Onuki9be01402017-11-10 13:22:26 -0800441 ActivityManager.PROCESS_STATE_UNKNOWN, null);
Makoto Onuki2206af32017-11-21 16:25:35 -0800442 mAppOpsService.startWatchingMode(TARGET_OP, null,
Makoto Onuki9be01402017-11-10 13:22:26 -0800443 new AppOpsWatcher());
444 } catch (RemoteException e) {
445 // shouldn't happen.
446 }
447
Makoto Onuki2206af32017-11-21 16:25:35 -0800448 IntentFilter filter = new IntentFilter();
449 filter.addAction(Intent.ACTION_USER_REMOVED);
Nancy Zheng525aaa12018-01-12 11:45:37 -0800450 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Kweku Adams52b52a62020-05-26 12:21:16 -0700451 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Makoto Onuki2206af32017-11-21 16:25:35 -0800452 mContext.registerReceiver(new MyReceiver(), filter);
Makoto Onuki9be01402017-11-10 13:22:26 -0800453
454 refreshForcedAppStandbyUidPackagesLocked();
Makoto Onuki2206af32017-11-21 16:25:35 -0800455
456 mPowerManagerInternal.registerLowPowerModeObserver(
457 ServiceType.FORCE_ALL_APPS_STANDBY,
Nancy Zheng525aaa12018-01-12 11:45:37 -0800458 (state) -> {
459 synchronized (mLock) {
460 mBatterySaverEnabled = state.batterySaverEnabled;
461 updateForceAllAppStandbyState();
462 }
463 });
Makoto Onuki2206af32017-11-21 16:25:35 -0800464
Nancy Zheng525aaa12018-01-12 11:45:37 -0800465 mBatterySaverEnabled = mPowerManagerInternal.getLowPowerState(
466 ServiceType.FORCE_ALL_APPS_STANDBY).batterySaverEnabled;
467
468 updateForceAllAppStandbyState();
Makoto Onuki9be01402017-11-10 13:22:26 -0800469 }
470 }
471
Makoto Onuki2206af32017-11-21 16:25:35 -0800472 @VisibleForTesting
473 AppOpsManager injectAppOpsManager() {
474 return mContext.getSystemService(AppOpsManager.class);
475 }
476
477 @VisibleForTesting
478 IAppOpsService injectIAppOpsService() {
479 return IAppOpsService.Stub.asInterface(
480 ServiceManager.getService(Context.APP_OPS_SERVICE));
481 }
482
483 @VisibleForTesting
484 IActivityManager injectIActivityManager() {
485 return ActivityManager.getService();
486 }
487
488 @VisibleForTesting
Makoto Onukie4918212018-02-06 11:30:15 -0800489 ActivityManagerInternal injectActivityManagerInternal() {
490 return LocalServices.getService(ActivityManagerInternal.class);
491 }
492
493 @VisibleForTesting
Makoto Onuki2206af32017-11-21 16:25:35 -0800494 PowerManagerInternal injectPowerManagerInternal() {
495 return LocalServices.getService(PowerManagerInternal.class);
496 }
497
Nancy Zheng525aaa12018-01-12 11:45:37 -0800498 @VisibleForTesting
Christopher Tateb909c4d52019-10-21 12:50:37 -0700499 AppStandbyInternal injectAppStandbyInternal() {
500 return LocalServices.getService(AppStandbyInternal.class);
Makoto Onukieb898f12018-01-23 15:26:27 -0800501 }
502
503 @VisibleForTesting
Nancy Zheng525aaa12018-01-12 11:45:37 -0800504 boolean isSmallBatteryDevice() {
505 return ActivityManager.isSmallBatteryDevice();
506 }
507
Makoto Onukieb898f12018-01-23 15:26:27 -0800508 @VisibleForTesting
509 int injectGetGlobalSettingInt(String key, int def) {
510 return Settings.Global.getInt(mContext.getContentResolver(), key, def);
511 }
512
Makoto Onuki9be01402017-11-10 13:22:26 -0800513 /**
Makoto Onuki2206af32017-11-21 16:25:35 -0800514 * Update {@link #mRunAnyRestrictedPackages} with the current app ops state.
Makoto Onuki9be01402017-11-10 13:22:26 -0800515 */
Andreas Gampea36dc622018-02-05 17:19:22 -0800516 @GuardedBy("mLock")
Makoto Onuki9be01402017-11-10 13:22:26 -0800517 private void refreshForcedAppStandbyUidPackagesLocked() {
Makoto Onuki2206af32017-11-21 16:25:35 -0800518 mRunAnyRestrictedPackages.clear();
519 final List<PackageOps> ops = mAppOpsManager.getPackagesForOps(
520 new int[] {TARGET_OP});
Makoto Onuki9be01402017-11-10 13:22:26 -0800521
522 if (ops == null) {
523 return;
524 }
525 final int size = ops.size();
526 for (int i = 0; i < size; i++) {
527 final AppOpsManager.PackageOps pkg = ops.get(i);
528 final List<AppOpsManager.OpEntry> entries = ops.get(i).getOps();
529
530 for (int j = 0; j < entries.size(); j++) {
531 AppOpsManager.OpEntry ent = entries.get(j);
Makoto Onuki2206af32017-11-21 16:25:35 -0800532 if (ent.getOp() != TARGET_OP) {
Makoto Onuki9be01402017-11-10 13:22:26 -0800533 continue;
534 }
535 if (ent.getMode() != AppOpsManager.MODE_ALLOWED) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800536 mRunAnyRestrictedPackages.add(Pair.create(
Makoto Onuki9be01402017-11-10 13:22:26 -0800537 pkg.getUid(), pkg.getPackageName()));
538 }
539 }
540 }
541 }
542
Nancy Zheng525aaa12018-01-12 11:45:37 -0800543 private void updateForceAllAppStandbyState() {
544 synchronized (mLock) {
545 if (mForceAllAppStandbyForSmallBattery && isSmallBatteryDevice()) {
546 toggleForceAllAppsStandbyLocked(!mIsPluggedIn);
547 } else {
548 toggleForceAllAppsStandbyLocked(mBatterySaverEnabled);
549 }
550 }
551 }
552
Makoto Onuki2206af32017-11-21 16:25:35 -0800553 /**
554 * Update {@link #mForceAllAppsStandby} and notifies the listeners.
555 */
Andreas Gampea36dc622018-02-05 17:19:22 -0800556 @GuardedBy("mLock")
Nancy Zheng525aaa12018-01-12 11:45:37 -0800557 private void toggleForceAllAppsStandbyLocked(boolean enable) {
558 if (enable == mForceAllAppsStandby) {
559 return;
Makoto Onuki12391f22018-01-18 21:44:28 +0000560 }
Nancy Zheng525aaa12018-01-12 11:45:37 -0800561 mForceAllAppsStandby = enable;
562
563 mHandler.notifyForceAllAppsStandbyChanged();
Makoto Onuki9be01402017-11-10 13:22:26 -0800564 }
565
Andreas Gampea36dc622018-02-05 17:19:22 -0800566 @GuardedBy("mLock")
Makoto Onuki9be01402017-11-10 13:22:26 -0800567 private int findForcedAppStandbyUidPackageIndexLocked(int uid, @NonNull String packageName) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800568 final int size = mRunAnyRestrictedPackages.size();
569 if (size > 8) {
570 return mRunAnyRestrictedPackages.indexOf(Pair.create(uid, packageName));
571 }
Makoto Onuki9be01402017-11-10 13:22:26 -0800572 for (int i = 0; i < size; i++) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800573 final Pair<Integer, String> pair = mRunAnyRestrictedPackages.valueAt(i);
Makoto Onuki9be01402017-11-10 13:22:26 -0800574
575 if ((pair.first == uid) && packageName.equals(pair.second)) {
576 return i;
577 }
578 }
579 return -1;
580 }
581
582 /**
Makoto Onuki2206af32017-11-21 16:25:35 -0800583 * @return whether a uid package-name pair is in mRunAnyRestrictedPackages.
Makoto Onuki9be01402017-11-10 13:22:26 -0800584 */
Andreas Gampea36dc622018-02-05 17:19:22 -0800585 @GuardedBy("mLock")
Makoto Onuki2206af32017-11-21 16:25:35 -0800586 boolean isRunAnyRestrictedLocked(int uid, @NonNull String packageName) {
Makoto Onuki9be01402017-11-10 13:22:26 -0800587 return findForcedAppStandbyUidPackageIndexLocked(uid, packageName) >= 0;
588 }
589
Makoto Onuki2206af32017-11-21 16:25:35 -0800590 /**
591 * Add to / remove from {@link #mRunAnyRestrictedPackages}.
592 */
Andreas Gampea36dc622018-02-05 17:19:22 -0800593 @GuardedBy("mLock")
Makoto Onuki2206af32017-11-21 16:25:35 -0800594 boolean updateForcedAppStandbyUidPackageLocked(int uid, @NonNull String packageName,
Makoto Onuki9be01402017-11-10 13:22:26 -0800595 boolean restricted) {
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800596 final int index = findForcedAppStandbyUidPackageIndexLocked(uid, packageName);
Makoto Onuki9be01402017-11-10 13:22:26 -0800597 final boolean wasRestricted = index >= 0;
598 if (wasRestricted == restricted) {
599 return false;
600 }
601 if (restricted) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800602 mRunAnyRestrictedPackages.add(Pair.create(uid, packageName));
Makoto Onuki9be01402017-11-10 13:22:26 -0800603 } else {
Makoto Onuki2206af32017-11-21 16:25:35 -0800604 mRunAnyRestrictedPackages.removeAt(index);
Makoto Onuki9be01402017-11-10 13:22:26 -0800605 }
606 return true;
607 }
608
Makoto Onukiadb50d82018-01-29 16:20:30 -0800609 private static boolean addUidToArray(SparseBooleanArray array, int uid) {
610 if (UserHandle.isCore(uid)) {
611 return false;
Makoto Onuki9be01402017-11-10 13:22:26 -0800612 }
Makoto Onukiadb50d82018-01-29 16:20:30 -0800613 if (array.get(uid)) {
614 return false;
615 }
616 array.put(uid, true);
617 return true;
Makoto Onuki9be01402017-11-10 13:22:26 -0800618 }
619
Makoto Onukiadb50d82018-01-29 16:20:30 -0800620 private static boolean removeUidFromArray(SparseBooleanArray array, int uid, boolean remove) {
621 if (UserHandle.isCore(uid)) {
622 return false;
Makoto Onuki9be01402017-11-10 13:22:26 -0800623 }
Makoto Onukiadb50d82018-01-29 16:20:30 -0800624 if (!array.get(uid)) {
625 return false;
626 }
627 if (remove) {
628 array.delete(uid);
629 } else {
630 array.put(uid, false);
631 }
632 return true;
Makoto Onuki9be01402017-11-10 13:22:26 -0800633 }
634
Makoto Onuki2206af32017-11-21 16:25:35 -0800635 private final class UidObserver extends IUidObserver.Stub {
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800636 @Override
Hui Yu26969322019-08-21 14:56:35 -0700637 public void onUidStateChanged(int uid, int procState, long procStateSeq, int capability) {
Makoto Onuki4d298b52018-02-05 10:54:58 -0800638 mHandler.onUidStateChanged(uid, procState);
Makoto Onuki9be01402017-11-10 13:22:26 -0800639 }
640
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800641 @Override
642 public void onUidActive(int uid) {
Makoto Onuki4d298b52018-02-05 10:54:58 -0800643 mHandler.onUidActive(uid);
644 }
645
646 @Override
647 public void onUidGone(int uid, boolean disabled) {
648 mHandler.onUidGone(uid, disabled);
Makoto Onuki9be01402017-11-10 13:22:26 -0800649 }
650
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800651 @Override
652 public void onUidIdle(int uid, boolean disabled) {
Makoto Onuki4d298b52018-02-05 10:54:58 -0800653 mHandler.onUidIdle(uid, disabled);
Makoto Onuki9be01402017-11-10 13:22:26 -0800654 }
655
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800656 @Override
657 public void onUidCachedChanged(int uid, boolean cached) {
Makoto Onuki9be01402017-11-10 13:22:26 -0800658 }
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800659 }
Makoto Onuki9be01402017-11-10 13:22:26 -0800660
661 private final class AppOpsWatcher extends IAppOpsCallback.Stub {
662 @Override
663 public void opChanged(int op, int uid, String packageName) throws RemoteException {
Makoto Onuki2206af32017-11-21 16:25:35 -0800664 boolean restricted = false;
665 try {
666 restricted = mAppOpsService.checkOperation(TARGET_OP,
667 uid, packageName) != AppOpsManager.MODE_ALLOWED;
668 } catch (RemoteException e) {
669 // Shouldn't happen
670 }
Makoto Onuki9be01402017-11-10 13:22:26 -0800671 synchronized (mLock) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800672 if (updateForcedAppStandbyUidPackageLocked(uid, packageName, restricted)) {
673 mHandler.notifyRunAnyAppOpsChanged(uid, packageName);
674 }
675 }
676 }
677 }
Makoto Onuki9be01402017-11-10 13:22:26 -0800678
Makoto Onuki2206af32017-11-21 16:25:35 -0800679 private final class MyReceiver extends BroadcastReceiver {
680 @Override
681 public void onReceive(Context context, Intent intent) {
682 if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) {
683 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
684 if (userId > 0) {
685 mHandler.doUserRemoved(userId);
Makoto Onuki9be01402017-11-10 13:22:26 -0800686 }
Nancy Zheng525aaa12018-01-12 11:45:37 -0800687 } else if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
688 synchronized (mLock) {
689 mIsPluggedIn = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0);
690 }
691 updateForceAllAppStandbyState();
Kweku Adams52b52a62020-05-26 12:21:16 -0700692 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
693 && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
694 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
695 final String pkgName = intent.getData().getSchemeSpecificPart();
696 if (mExemptedPackages.remove(userId, pkgName)) {
697 mHandler.notifyExemptChanged();
698 }
Makoto Onuki9be01402017-11-10 13:22:26 -0800699 }
700 }
701 }
702
Makoto Onukieb898f12018-01-23 15:26:27 -0800703 final class StandbyTracker extends AppIdleStateChangeListener {
704 @Override
705 public void onAppIdleStateChanged(String packageName, int userId, boolean idle,
Amith Yamasani119be9a2018-02-18 22:23:00 -0800706 int bucket, int reason) {
Makoto Onukieb898f12018-01-23 15:26:27 -0800707 if (DEBUG) {
708 Slog.d(TAG,"onAppIdleStateChanged: " + packageName + " u" + userId
709 + (idle ? " idle" : " active") + " " + bucket);
710 }
liulvpinge7dade12019-11-06 15:12:20 +0800711 synchronized (mLock) {
712 final boolean changed;
713 if (bucket == UsageStatsManager.STANDBY_BUCKET_EXEMPTED) {
714 changed = mExemptedPackages.add(userId, packageName);
715 } else {
716 changed = mExemptedPackages.remove(userId, packageName);
717 }
718 if (changed) {
719 mHandler.notifyExemptChanged();
720 }
Makoto Onukieb898f12018-01-23 15:26:27 -0800721 }
722 }
Makoto Onukieb898f12018-01-23 15:26:27 -0800723 }
724
Makoto Onuki9be01402017-11-10 13:22:26 -0800725 private Listener[] cloneListeners() {
726 synchronized (mLock) {
727 return mListeners.toArray(new Listener[mListeners.size()]);
728 }
729 }
730
Makoto Onuki2206af32017-11-21 16:25:35 -0800731 private class MyHandler extends Handler {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800732 private static final int MSG_UID_ACTIVE_STATE_CHANGED = 0;
733 private static final int MSG_UID_FG_STATE_CHANGED = 1;
734 private static final int MSG_RUN_ANY_CHANGED = 3;
735 private static final int MSG_ALL_UNWHITELISTED = 4;
736 private static final int MSG_ALL_WHITELIST_CHANGED = 5;
737 private static final int MSG_TEMP_WHITELIST_CHANGED = 6;
738 private static final int MSG_FORCE_ALL_CHANGED = 7;
739 private static final int MSG_USER_REMOVED = 8;
740 private static final int MSG_FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED = 9;
741 private static final int MSG_EXEMPT_CHANGED = 10;
Makoto Onuki9be01402017-11-10 13:22:26 -0800742
Makoto Onuki4d298b52018-02-05 10:54:58 -0800743 private static final int MSG_ON_UID_STATE_CHANGED = 11;
744 private static final int MSG_ON_UID_ACTIVE = 12;
745 private static final int MSG_ON_UID_GONE = 13;
746 private static final int MSG_ON_UID_IDLE = 14;
747
Makoto Onuki2206af32017-11-21 16:25:35 -0800748 public MyHandler(Looper looper) {
749 super(looper);
Makoto Onuki9be01402017-11-10 13:22:26 -0800750 }
Makoto Onuki2206af32017-11-21 16:25:35 -0800751
Makoto Onukiadb50d82018-01-29 16:20:30 -0800752 public void notifyUidActiveStateChanged(int uid) {
753 obtainMessage(MSG_UID_ACTIVE_STATE_CHANGED, uid, 0).sendToTarget();
754 }
755
Makoto Onuki2206af32017-11-21 16:25:35 -0800756 public void notifyUidForegroundStateChanged(int uid) {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800757 obtainMessage(MSG_UID_FG_STATE_CHANGED, uid, 0).sendToTarget();
Makoto Onuki2206af32017-11-21 16:25:35 -0800758 }
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800759
Makoto Onuki2206af32017-11-21 16:25:35 -0800760 public void notifyRunAnyAppOpsChanged(int uid, @NonNull String packageName) {
761 obtainMessage(MSG_RUN_ANY_CHANGED, uid, 0, packageName).sendToTarget();
762 }
763
764 public void notifyAllUnwhitelisted() {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800765 removeMessages(MSG_ALL_UNWHITELISTED);
Makoto Onuki2206af32017-11-21 16:25:35 -0800766 obtainMessage(MSG_ALL_UNWHITELISTED).sendToTarget();
767 }
768
769 public void notifyAllWhitelistChanged() {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800770 removeMessages(MSG_ALL_WHITELIST_CHANGED);
Makoto Onuki2206af32017-11-21 16:25:35 -0800771 obtainMessage(MSG_ALL_WHITELIST_CHANGED).sendToTarget();
772 }
773
774 public void notifyTempWhitelistChanged() {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800775 removeMessages(MSG_TEMP_WHITELIST_CHANGED);
Makoto Onuki2206af32017-11-21 16:25:35 -0800776 obtainMessage(MSG_TEMP_WHITELIST_CHANGED).sendToTarget();
777 }
778
779 public void notifyForceAllAppsStandbyChanged() {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800780 removeMessages(MSG_FORCE_ALL_CHANGED);
Makoto Onuki2206af32017-11-21 16:25:35 -0800781 obtainMessage(MSG_FORCE_ALL_CHANGED).sendToTarget();
782 }
783
Nancy Zheng525aaa12018-01-12 11:45:37 -0800784 public void notifyForcedAppStandbyFeatureFlagChanged() {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800785 removeMessages(MSG_FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED);
Nancy Zheng525aaa12018-01-12 11:45:37 -0800786 obtainMessage(MSG_FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED).sendToTarget();
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800787 }
788
Makoto Onukieb898f12018-01-23 15:26:27 -0800789 public void notifyExemptChanged() {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800790 removeMessages(MSG_EXEMPT_CHANGED);
Makoto Onukieb898f12018-01-23 15:26:27 -0800791 obtainMessage(MSG_EXEMPT_CHANGED).sendToTarget();
792 }
793
Makoto Onuki2206af32017-11-21 16:25:35 -0800794 public void doUserRemoved(int userId) {
795 obtainMessage(MSG_USER_REMOVED, userId, 0).sendToTarget();
796 }
797
Makoto Onuki4d298b52018-02-05 10:54:58 -0800798 public void onUidStateChanged(int uid, int procState) {
799 obtainMessage(MSG_ON_UID_STATE_CHANGED, uid, procState).sendToTarget();
800 }
801
802 public void onUidActive(int uid) {
803 obtainMessage(MSG_ON_UID_ACTIVE, uid, 0).sendToTarget();
804 }
805
806 public void onUidGone(int uid, boolean disabled) {
807 obtainMessage(MSG_ON_UID_GONE, uid, disabled ? 1 : 0).sendToTarget();
808 }
809
810 public void onUidIdle(int uid, boolean disabled) {
811 obtainMessage(MSG_ON_UID_IDLE, uid, disabled ? 1 : 0).sendToTarget();
812 }
813
Makoto Onuki2206af32017-11-21 16:25:35 -0800814 @Override
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800815 public void handleMessage(Message msg) {
Makoto Onuki2206af32017-11-21 16:25:35 -0800816 switch (msg.what) {
817 case MSG_USER_REMOVED:
818 handleUserRemoved(msg.arg1);
819 return;
820 }
821
822 // Only notify the listeners when started.
823 synchronized (mLock) {
824 if (!mStarted) {
825 return;
826 }
827 }
Makoto Onukie4918212018-02-06 11:30:15 -0800828 final AppStateTracker sender = AppStateTracker.this;
Makoto Onuki2206af32017-11-21 16:25:35 -0800829
Makoto Onukieb898f12018-01-23 15:26:27 -0800830 long start = mStatLogger.getTime();
Makoto Onuki2206af32017-11-21 16:25:35 -0800831 switch (msg.what) {
Makoto Onukiadb50d82018-01-29 16:20:30 -0800832 case MSG_UID_ACTIVE_STATE_CHANGED:
833 for (Listener l : cloneListeners()) {
834 l.onUidActiveStateChanged(sender, msg.arg1);
835 }
836 mStatLogger.logDurationStat(Stats.UID_ACTIVE_STATE_CHANGED, start);
837 return;
838
839 case MSG_UID_FG_STATE_CHANGED:
Makoto Onuki2206af32017-11-21 16:25:35 -0800840 for (Listener l : cloneListeners()) {
841 l.onUidForegroundStateChanged(sender, msg.arg1);
842 }
Makoto Onukiadb50d82018-01-29 16:20:30 -0800843 mStatLogger.logDurationStat(Stats.UID_FG_STATE_CHANGED, start);
Makoto Onuki2206af32017-11-21 16:25:35 -0800844 return;
Makoto Onukieb898f12018-01-23 15:26:27 -0800845
Makoto Onuki2206af32017-11-21 16:25:35 -0800846 case MSG_RUN_ANY_CHANGED:
847 for (Listener l : cloneListeners()) {
848 l.onRunAnyAppOpsChanged(sender, msg.arg1, (String) msg.obj);
849 }
Makoto Onukieb898f12018-01-23 15:26:27 -0800850 mStatLogger.logDurationStat(Stats.RUN_ANY_CHANGED, start);
Makoto Onuki2206af32017-11-21 16:25:35 -0800851 return;
Makoto Onukieb898f12018-01-23 15:26:27 -0800852
Makoto Onuki2206af32017-11-21 16:25:35 -0800853 case MSG_ALL_UNWHITELISTED:
854 for (Listener l : cloneListeners()) {
855 l.onPowerSaveUnwhitelisted(sender);
856 }
Makoto Onukieb898f12018-01-23 15:26:27 -0800857 mStatLogger.logDurationStat(Stats.ALL_UNWHITELISTED, start);
Makoto Onuki2206af32017-11-21 16:25:35 -0800858 return;
Makoto Onukieb898f12018-01-23 15:26:27 -0800859
Makoto Onuki2206af32017-11-21 16:25:35 -0800860 case MSG_ALL_WHITELIST_CHANGED:
861 for (Listener l : cloneListeners()) {
862 l.onPowerSaveWhitelistedChanged(sender);
863 }
Makoto Onukieb898f12018-01-23 15:26:27 -0800864 mStatLogger.logDurationStat(Stats.ALL_WHITELIST_CHANGED, start);
Makoto Onuki2206af32017-11-21 16:25:35 -0800865 return;
Makoto Onukieb898f12018-01-23 15:26:27 -0800866
Makoto Onuki2206af32017-11-21 16:25:35 -0800867 case MSG_TEMP_WHITELIST_CHANGED:
868 for (Listener l : cloneListeners()) {
869 l.onTempPowerSaveWhitelistChanged(sender);
870 }
Makoto Onukieb898f12018-01-23 15:26:27 -0800871 mStatLogger.logDurationStat(Stats.TEMP_WHITELIST_CHANGED, start);
Makoto Onuki2206af32017-11-21 16:25:35 -0800872 return;
Makoto Onukieb898f12018-01-23 15:26:27 -0800873
874 case MSG_EXEMPT_CHANGED:
875 for (Listener l : cloneListeners()) {
876 l.onExemptChanged(sender);
877 }
878 mStatLogger.logDurationStat(Stats.EXEMPT_CHANGED, start);
879 return;
880
Makoto Onuki2206af32017-11-21 16:25:35 -0800881 case MSG_FORCE_ALL_CHANGED:
882 for (Listener l : cloneListeners()) {
883 l.onForceAllAppsStandbyChanged(sender);
884 }
Makoto Onukieb898f12018-01-23 15:26:27 -0800885 mStatLogger.logDurationStat(Stats.FORCE_ALL_CHANGED, start);
Makoto Onuki2206af32017-11-21 16:25:35 -0800886 return;
Makoto Onukieb898f12018-01-23 15:26:27 -0800887
Nancy Zheng525aaa12018-01-12 11:45:37 -0800888 case MSG_FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED:
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800889 // Feature flag for forced app standby changed.
890 final boolean unblockAlarms;
891 synchronized (mLock) {
892 unblockAlarms = !mForcedAppStandbyEnabled && !mForceAllAppsStandby;
893 }
Suprabh Shuklac25447d2018-01-19 16:43:35 -0800894 for (Listener l : cloneListeners()) {
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800895 l.updateAllJobs();
896 if (unblockAlarms) {
897 l.unblockAllUnrestrictedAlarms();
898 }
899 }
Makoto Onukieb898f12018-01-23 15:26:27 -0800900 mStatLogger.logDurationStat(
901 Stats.FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED, start);
Suprabh Shukla4deb8522018-01-08 16:27:10 -0800902 return;
Makoto Onukieb898f12018-01-23 15:26:27 -0800903
Makoto Onuki2206af32017-11-21 16:25:35 -0800904 case MSG_USER_REMOVED:
905 handleUserRemoved(msg.arg1);
906 return;
Makoto Onuki4d298b52018-02-05 10:54:58 -0800907
908 case MSG_ON_UID_STATE_CHANGED:
909 handleUidStateChanged(msg.arg1, msg.arg2);
910 return;
911 case MSG_ON_UID_ACTIVE:
912 handleUidActive(msg.arg1);
913 return;
914 case MSG_ON_UID_GONE:
915 handleUidGone(msg.arg1, msg.arg1 != 0);
916 return;
917 case MSG_ON_UID_IDLE:
918 handleUidIdle(msg.arg1, msg.arg1 != 0);
919 return;
920 }
921 }
922
923 public void handleUidStateChanged(int uid, int procState) {
924 synchronized (mLock) {
925 if (procState > ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
926 if (removeUidFromArray(mForegroundUids, uid, false)) {
927 mHandler.notifyUidForegroundStateChanged(uid);
928 }
929 } else {
930 if (addUidToArray(mForegroundUids, uid)) {
931 mHandler.notifyUidForegroundStateChanged(uid);
932 }
933 }
934 }
935 }
936
937 public void handleUidActive(int uid) {
938 synchronized (mLock) {
939 if (addUidToArray(mActiveUids, uid)) {
940 mHandler.notifyUidActiveStateChanged(uid);
941 }
942 }
943 }
944
945 public void handleUidGone(int uid, boolean disabled) {
946 removeUid(uid, true);
947 }
948
949 public void handleUidIdle(int uid, boolean disabled) {
950 // Just to avoid excessive memcpy, don't remove from the array in this case.
951 removeUid(uid, false);
952 }
953
954 private void removeUid(int uid, boolean remove) {
955 synchronized (mLock) {
956 if (removeUidFromArray(mActiveUids, uid, remove)) {
957 mHandler.notifyUidActiveStateChanged(uid);
958 }
959 if (removeUidFromArray(mForegroundUids, uid, remove)) {
960 mHandler.notifyUidForegroundStateChanged(uid);
961 }
Makoto Onuki2206af32017-11-21 16:25:35 -0800962 }
963 }
964 }
965
966 void handleUserRemoved(int removedUserId) {
967 synchronized (mLock) {
968 for (int i = mRunAnyRestrictedPackages.size() - 1; i >= 0; i--) {
969 final Pair<Integer, String> pair = mRunAnyRestrictedPackages.valueAt(i);
970 final int uid = pair.first;
971 final int userId = UserHandle.getUserId(uid);
972
973 if (userId == removedUserId) {
974 mRunAnyRestrictedPackages.removeAt(i);
975 }
976 }
Makoto Onukiadb50d82018-01-29 16:20:30 -0800977 cleanUpArrayForUser(mActiveUids, removedUserId);
978 cleanUpArrayForUser(mForegroundUids, removedUserId);
Makoto Onukieb898f12018-01-23 15:26:27 -0800979 mExemptedPackages.remove(removedUserId);
Makoto Onuki2206af32017-11-21 16:25:35 -0800980 }
981 }
982
Makoto Onukiadb50d82018-01-29 16:20:30 -0800983 private void cleanUpArrayForUser(SparseBooleanArray array, int removedUserId) {
984 for (int i = array.size() - 1; i >= 0; i--) {
985 final int uid = array.keyAt(i);
986 final int userId = UserHandle.getUserId(uid);
987
988 if (userId == removedUserId) {
989 array.removeAt(i);
990 }
991 }
992 }
993
Makoto Onuki2206af32017-11-21 16:25:35 -0800994 /**
995 * Called by device idle controller to update the power save whitelists.
996 */
997 public void setPowerSaveWhitelistAppIds(
Suprabh Shukla5bf49812018-05-24 18:38:50 -0700998 int[] powerSaveWhitelistExceptIdleAppIdArray,
999 int[] powerSaveWhitelistUserAppIdArray,
1000 int[] tempWhitelistAppIdArray) {
Makoto Onuki2206af32017-11-21 16:25:35 -08001001 synchronized (mLock) {
1002 final int[] previousWhitelist = mPowerWhitelistedAllAppIds;
1003 final int[] previousTempWhitelist = mTempWhitelistedAppIds;
1004
Suprabh Shukla5bf49812018-05-24 18:38:50 -07001005 mPowerWhitelistedAllAppIds = powerSaveWhitelistExceptIdleAppIdArray;
Makoto Onuki2206af32017-11-21 16:25:35 -08001006 mTempWhitelistedAppIds = tempWhitelistAppIdArray;
Suprabh Shukla5bf49812018-05-24 18:38:50 -07001007 mPowerWhitelistedUserAppIds = powerSaveWhitelistUserAppIdArray;
Makoto Onuki2206af32017-11-21 16:25:35 -08001008
1009 if (isAnyAppIdUnwhitelisted(previousWhitelist, mPowerWhitelistedAllAppIds)) {
1010 mHandler.notifyAllUnwhitelisted();
1011 } else if (!Arrays.equals(previousWhitelist, mPowerWhitelistedAllAppIds)) {
1012 mHandler.notifyAllWhitelistChanged();
1013 }
1014
1015 if (!Arrays.equals(previousTempWhitelist, mTempWhitelistedAppIds)) {
1016 mHandler.notifyTempWhitelistChanged();
1017 }
1018
1019 }
1020 }
1021
1022 /**
1023 * @retunr true if a sorted app-id array {@code prevArray} has at least one element
1024 * that's not in a sorted app-id array {@code newArray}.
1025 */
1026 @VisibleForTesting
1027 static boolean isAnyAppIdUnwhitelisted(int[] prevArray, int[] newArray) {
1028 int i1 = 0;
1029 int i2 = 0;
1030 boolean prevFinished;
1031 boolean newFinished;
1032
1033 for (;;) {
1034 prevFinished = i1 >= prevArray.length;
1035 newFinished = i2 >= newArray.length;
1036 if (prevFinished || newFinished) {
1037 break;
1038 }
1039 int a1 = prevArray[i1];
1040 int a2 = newArray[i2];
1041
1042 if (a1 == a2) {
1043 i1++;
1044 i2++;
1045 continue;
1046 }
1047 if (a1 < a2) {
1048 // prevArray has an element that's not in a2.
1049 return true;
1050 }
1051 i2++;
1052 }
1053 if (prevFinished) {
1054 return false;
1055 }
1056 return newFinished;
Makoto Onuki9be01402017-11-10 13:22:26 -08001057 }
1058
1059 // Public interface.
1060
1061 /**
1062 * Register a new listener.
1063 */
1064 public void addListener(@NonNull Listener listener) {
1065 synchronized (mLock) {
1066 mListeners.add(listener);
1067 }
1068 }
1069
1070 /**
Makoto Onuki2206af32017-11-21 16:25:35 -08001071 * @return whether alarms should be restricted for a UID package-name.
Makoto Onuki9be01402017-11-10 13:22:26 -08001072 */
Suprabh Shuklac25447d2018-01-19 16:43:35 -08001073 public boolean areAlarmsRestricted(int uid, @NonNull String packageName,
Christopher Tateda3dc922018-05-09 13:49:41 -07001074 boolean isExemptOnBatterySaver) {
Makoto Onukieb8cfd12018-01-19 15:43:16 -08001075 return isRestricted(uid, packageName, /*useTempWhitelistToo=*/ false,
Christopher Tateda3dc922018-05-09 13:49:41 -07001076 isExemptOnBatterySaver);
Makoto Onuki2206af32017-11-21 16:25:35 -08001077 }
1078
1079 /**
1080 * @return whether jobs should be restricted for a UID package-name.
1081 */
Makoto Onuki15407842018-01-19 14:23:11 -08001082 public boolean areJobsRestricted(int uid, @NonNull String packageName,
1083 boolean hasForegroundExemption) {
Makoto Onukieb8cfd12018-01-19 15:43:16 -08001084 return isRestricted(uid, packageName, /*useTempWhitelistToo=*/ true,
Makoto Onuki15407842018-01-19 14:23:11 -08001085 hasForegroundExemption);
Makoto Onuki2206af32017-11-21 16:25:35 -08001086 }
1087
1088 /**
Christopher Tatec7933ac2018-03-12 17:57:09 -07001089 * @return whether foreground services should be suppressed in the background
1090 * due to forced app standby for the given app
1091 */
1092 public boolean areForegroundServicesRestricted(int uid, @NonNull String packageName) {
1093 synchronized (mLock) {
1094 return isRunAnyRestrictedLocked(uid, packageName);
1095 }
1096 }
1097
1098 /**
Makoto Onuki2206af32017-11-21 16:25:35 -08001099 * @return whether force-app-standby is effective for a UID package-name.
1100 */
1101 private boolean isRestricted(int uid, @NonNull String packageName,
Makoto Onukieb8cfd12018-01-19 15:43:16 -08001102 boolean useTempWhitelistToo, boolean exemptOnBatterySaver) {
Makoto Onukiadb50d82018-01-29 16:20:30 -08001103 if (isUidActive(uid)) {
Makoto Onuki9be01402017-11-10 13:22:26 -08001104 return false;
1105 }
1106 synchronized (mLock) {
Makoto Onuki2206af32017-11-21 16:25:35 -08001107 // Whitelisted?
1108 final int appId = UserHandle.getAppId(uid);
1109 if (ArrayUtils.contains(mPowerWhitelistedAllAppIds, appId)) {
1110 return false;
1111 }
1112 if (useTempWhitelistToo &&
1113 ArrayUtils.contains(mTempWhitelistedAppIds, appId)) {
1114 return false;
1115 }
Makoto Onukieb8cfd12018-01-19 15:43:16 -08001116 if (mForcedAppStandbyEnabled && isRunAnyRestrictedLocked(uid, packageName)) {
Makoto Onuki9be01402017-11-10 13:22:26 -08001117 return true;
1118 }
Makoto Onukieb8cfd12018-01-19 15:43:16 -08001119 if (exemptOnBatterySaver) {
1120 return false;
1121 }
Makoto Onukieb898f12018-01-23 15:26:27 -08001122 final int userId = UserHandle.getUserId(uid);
Kweku Adams15eaef32020-06-05 12:43:44 -07001123 if (mAppStandbyInternal.isAppIdleEnabled() && !mAppStandbyInternal.isInParole()
1124 && mExemptedPackages.contains(userId, packageName)) {
Makoto Onukieb898f12018-01-23 15:26:27 -08001125 return false;
1126 }
Makoto Onukieb8cfd12018-01-19 15:43:16 -08001127 return mForceAllAppsStandby;
Makoto Onuki9be01402017-11-10 13:22:26 -08001128 }
1129 }
1130
Makoto Onuki2206af32017-11-21 16:25:35 -08001131 /**
Makoto Onukie4918212018-02-06 11:30:15 -08001132 * @return whether a UID is in active or not *based on cached information.*
Makoto Onukiadb50d82018-01-29 16:20:30 -08001133 *
1134 * Note this information is based on the UID proc state callback, meaning it's updated
1135 * asynchronously and may subtly be stale. If the fresh data is needed, use
Makoto Onukie4918212018-02-06 11:30:15 -08001136 * {@link #isUidActiveSynced} instead.
Makoto Onukiadb50d82018-01-29 16:20:30 -08001137 */
1138 public boolean isUidActive(int uid) {
1139 if (UserHandle.isCore(uid)) {
1140 return true;
1141 }
1142 synchronized (mLock) {
1143 return mActiveUids.get(uid);
1144 }
1145 }
1146
1147 /**
Makoto Onukie4918212018-02-06 11:30:15 -08001148 * @return whether a UID is in active or not *right now.*
1149 *
1150 * This gives the fresh information, but may access the activity manager so is slower.
1151 */
1152 public boolean isUidActiveSynced(int uid) {
1153 if (isUidActive(uid)) { // Use the cached one first.
1154 return true;
1155 }
1156 final long start = mStatLogger.getTime();
1157
1158 final boolean ret = mActivityManagerInternal.isUidActive(uid);
1159 mStatLogger.logDurationStat(Stats.IS_UID_ACTIVE_RAW, start);
1160
1161 return ret;
1162 }
1163
1164 /**
Makoto Onuki2206af32017-11-21 16:25:35 -08001165 * @return whether a UID is in the foreground or not.
1166 *
Makoto Onuki15407842018-01-19 14:23:11 -08001167 * Note this information is based on the UID proc state callback, meaning it's updated
1168 * asynchronously and may subtly be stale. If the fresh data is needed, use
1169 * {@link ActivityManagerInternal#getUidProcessState} instead.
Makoto Onuki2206af32017-11-21 16:25:35 -08001170 */
Makoto Onukiadb50d82018-01-29 16:20:30 -08001171 public boolean isUidInForeground(int uid) {
Makoto Onuki9cc471c2018-01-23 12:33:56 -08001172 if (UserHandle.isCore(uid)) {
Makoto Onuki9be01402017-11-10 13:22:26 -08001173 return true;
1174 }
1175 synchronized (mLock) {
1176 return mForegroundUids.get(uid);
1177 }
1178 }
1179
Makoto Onuki2206af32017-11-21 16:25:35 -08001180 /**
1181 * @return whether force all apps standby is enabled or not.
1182 *
Makoto Onuki2206af32017-11-21 16:25:35 -08001183 */
1184 boolean isForceAllAppsStandbyEnabled() {
Makoto Onuki9be01402017-11-10 13:22:26 -08001185 synchronized (mLock) {
1186 return mForceAllAppsStandby;
1187 }
1188 }
1189
Makoto Onuki2206af32017-11-21 16:25:35 -08001190 /**
1191 * @return whether a UID/package has {@code OP_RUN_ANY_IN_BACKGROUND} allowed or not.
1192 *
1193 * Note clients normally shouldn't need to access it. It's only for dumpsys.
1194 */
Makoto Onuki9be01402017-11-10 13:22:26 -08001195 public boolean isRunAnyInBackgroundAppOpsAllowed(int uid, @NonNull String packageName) {
1196 synchronized (mLock) {
Makoto Onuki2206af32017-11-21 16:25:35 -08001197 return !isRunAnyRestrictedLocked(uid, packageName);
Makoto Onuki9be01402017-11-10 13:22:26 -08001198 }
1199 }
1200
Makoto Onuki2206af32017-11-21 16:25:35 -08001201 /**
1202 * @return whether a UID is in the user / system defined power-save whitelist or not.
1203 *
1204 * Note clients normally shouldn't need to access it. It's only for dumpsys.
1205 */
1206 public boolean isUidPowerSaveWhitelisted(int uid) {
Makoto Onuki9be01402017-11-10 13:22:26 -08001207 synchronized (mLock) {
Makoto Onuki2206af32017-11-21 16:25:35 -08001208 return ArrayUtils.contains(mPowerWhitelistedAllAppIds, UserHandle.getAppId(uid));
Makoto Onuki9be01402017-11-10 13:22:26 -08001209 }
1210 }
1211
Makoto Onuki2206af32017-11-21 16:25:35 -08001212 /**
Suprabh Shukla5bf49812018-05-24 18:38:50 -07001213 * @param uid the uid to check for
1214 * @return whether a UID is in the user defined power-save whitelist or not.
1215 */
1216 public boolean isUidPowerSaveUserWhitelisted(int uid) {
1217 synchronized (mLock) {
1218 return ArrayUtils.contains(mPowerWhitelistedUserAppIds, UserHandle.getAppId(uid));
1219 }
1220 }
1221
1222 /**
Makoto Onuki2206af32017-11-21 16:25:35 -08001223 * @return whether a UID is in the temp power-save whitelist or not.
1224 *
1225 * Note clients normally shouldn't need to access it. It's only for dumpsys.
1226 */
1227 public boolean isUidTempPowerSaveWhitelisted(int uid) {
Makoto Onuki9be01402017-11-10 13:22:26 -08001228 synchronized (mLock) {
Makoto Onuki2206af32017-11-21 16:25:35 -08001229 return ArrayUtils.contains(mTempWhitelistedAppIds, UserHandle.getAppId(uid));
1230 }
1231 }
1232
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001233 @Deprecated
1234 public void dump(PrintWriter pw, String prefix) {
1235 dump(new IndentingPrintWriter(pw, " ").setIndent(prefix));
1236 }
1237
1238 public void dump(IndentingPrintWriter pw) {
Makoto Onuki2206af32017-11-21 16:25:35 -08001239 synchronized (mLock) {
Suprabh Shukla4deb8522018-01-08 16:27:10 -08001240 pw.println("Forced App Standby Feature enabled: " + mForcedAppStandbyEnabled);
1241
Makoto Onuki2206af32017-11-21 16:25:35 -08001242 pw.print("Force all apps standby: ");
1243 pw.println(isForceAllAppsStandbyEnabled());
1244
Nancy Zheng525aaa12018-01-12 11:45:37 -08001245 pw.print("Small Battery Device: ");
1246 pw.println(isSmallBatteryDevice());
1247
Nancy Zheng525aaa12018-01-12 11:45:37 -08001248 pw.print("Force all apps standby for small battery device: ");
1249 pw.println(mForceAllAppStandbyForSmallBattery);
1250
Nancy Zheng525aaa12018-01-12 11:45:37 -08001251 pw.print("Plugged In: ");
1252 pw.println(mIsPluggedIn);
1253
Makoto Onukiadb50d82018-01-29 16:20:30 -08001254 pw.print("Active uids: ");
1255 dumpUids(pw, mActiveUids);
Makoto Onuki2206af32017-11-21 16:25:35 -08001256
Makoto Onukiadb50d82018-01-29 16:20:30 -08001257 pw.print("Foreground uids: ");
1258 dumpUids(pw, mForegroundUids);
Makoto Onuki2206af32017-11-21 16:25:35 -08001259
Suprabh Shukla5bf49812018-05-24 18:38:50 -07001260 pw.print("Except-idle + user whitelist appids: ");
Makoto Onuki2206af32017-11-21 16:25:35 -08001261 pw.println(Arrays.toString(mPowerWhitelistedAllAppIds));
1262
Suprabh Shukla5bf49812018-05-24 18:38:50 -07001263 pw.print("User whitelist appids: ");
1264 pw.println(Arrays.toString(mPowerWhitelistedUserAppIds));
1265
Makoto Onuki2206af32017-11-21 16:25:35 -08001266 pw.print("Temp whitelist appids: ");
1267 pw.println(Arrays.toString(mTempWhitelistedAppIds));
1268
Makoto Onukieb898f12018-01-23 15:26:27 -08001269 pw.println("Exempted packages:");
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001270 pw.increaseIndent();
Makoto Onukieb898f12018-01-23 15:26:27 -08001271 for (int i = 0; i < mExemptedPackages.size(); i++) {
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001272 pw.print("User ");
Makoto Onukieb898f12018-01-23 15:26:27 -08001273 pw.print(mExemptedPackages.keyAt(i));
1274 pw.println();
1275
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001276 pw.increaseIndent();
Makoto Onukieb898f12018-01-23 15:26:27 -08001277 for (int j = 0; j < mExemptedPackages.sizeAt(i); j++) {
Makoto Onukieb898f12018-01-23 15:26:27 -08001278 pw.print(mExemptedPackages.valueAt(i, j));
1279 pw.println();
1280 }
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001281 pw.decreaseIndent();
Makoto Onukieb898f12018-01-23 15:26:27 -08001282 }
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001283 pw.decreaseIndent();
Makoto Onukieb898f12018-01-23 15:26:27 -08001284 pw.println();
1285
Makoto Onuki2206af32017-11-21 16:25:35 -08001286 pw.println("Restricted packages:");
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001287 pw.increaseIndent();
Makoto Onuki2206af32017-11-21 16:25:35 -08001288 for (Pair<Integer, String> uidAndPackage : mRunAnyRestrictedPackages) {
Makoto Onuki2206af32017-11-21 16:25:35 -08001289 pw.print(UserHandle.formatUid(uidAndPackage.first));
1290 pw.print(" ");
1291 pw.print(uidAndPackage.second);
1292 pw.println();
1293 }
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001294 pw.decreaseIndent();
Makoto Onukieb898f12018-01-23 15:26:27 -08001295
Jeff Sharkeyfee8c7b2018-02-21 22:18:45 -07001296 mStatLogger.dump(pw);
Makoto Onuki2206af32017-11-21 16:25:35 -08001297 }
1298 }
1299
Makoto Onukiadb50d82018-01-29 16:20:30 -08001300 private void dumpUids(PrintWriter pw, SparseBooleanArray array) {
1301 pw.print("[");
1302
1303 String sep = "";
1304 for (int i = 0; i < array.size(); i++) {
1305 if (array.valueAt(i)) {
1306 pw.print(sep);
1307 pw.print(UserHandle.formatUid(array.keyAt(i)));
1308 sep = " ";
1309 }
1310 }
1311 pw.println("]");
1312 }
1313
Makoto Onuki2206af32017-11-21 16:25:35 -08001314 public void dumpProto(ProtoOutputStream proto, long fieldId) {
1315 synchronized (mLock) {
1316 final long token = proto.start(fieldId);
1317
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001318 proto.write(AppStateTrackerProto.FORCED_APP_STANDBY_FEATURE_ENABLED,
1319 mForcedAppStandbyEnabled);
1320 proto.write(AppStateTrackerProto.FORCE_ALL_APPS_STANDBY,
1321 isForceAllAppsStandbyEnabled());
1322 proto.write(AppStateTrackerProto.IS_SMALL_BATTERY_DEVICE, isSmallBatteryDevice());
1323 proto.write(AppStateTrackerProto.FORCE_ALL_APPS_STANDBY_FOR_SMALL_BATTERY,
Nancy Zheng525aaa12018-01-12 11:45:37 -08001324 mForceAllAppStandbyForSmallBattery);
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001325 proto.write(AppStateTrackerProto.IS_PLUGGED_IN, mIsPluggedIn);
Makoto Onuki2206af32017-11-21 16:25:35 -08001326
Makoto Onukiadb50d82018-01-29 16:20:30 -08001327 for (int i = 0; i < mActiveUids.size(); i++) {
1328 if (mActiveUids.valueAt(i)) {
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001329 proto.write(AppStateTrackerProto.ACTIVE_UIDS, mActiveUids.keyAt(i));
Makoto Onukiadb50d82018-01-29 16:20:30 -08001330 }
1331 }
1332
Makoto Onuki2206af32017-11-21 16:25:35 -08001333 for (int i = 0; i < mForegroundUids.size(); i++) {
1334 if (mForegroundUids.valueAt(i)) {
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001335 proto.write(AppStateTrackerProto.FOREGROUND_UIDS, mForegroundUids.keyAt(i));
Makoto Onuki2206af32017-11-21 16:25:35 -08001336 }
1337 }
1338
1339 for (int appId : mPowerWhitelistedAllAppIds) {
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001340 proto.write(AppStateTrackerProto.POWER_SAVE_WHITELIST_APP_IDS, appId);
Makoto Onuki2206af32017-11-21 16:25:35 -08001341 }
1342
Suprabh Shukla5bf49812018-05-24 18:38:50 -07001343 for (int appId : mPowerWhitelistedUserAppIds) {
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001344 proto.write(AppStateTrackerProto.POWER_SAVE_USER_WHITELIST_APP_IDS, appId);
Suprabh Shukla5bf49812018-05-24 18:38:50 -07001345 }
1346
Makoto Onuki2206af32017-11-21 16:25:35 -08001347 for (int appId : mTempWhitelistedAppIds) {
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001348 proto.write(AppStateTrackerProto.TEMP_POWER_SAVE_WHITELIST_APP_IDS, appId);
Makoto Onuki2206af32017-11-21 16:25:35 -08001349 }
1350
Makoto Onukieb898f12018-01-23 15:26:27 -08001351 for (int i = 0; i < mExemptedPackages.size(); i++) {
1352 for (int j = 0; j < mExemptedPackages.sizeAt(i); j++) {
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001353 final long token2 = proto.start(AppStateTrackerProto.EXEMPTED_PACKAGES);
Makoto Onukieb898f12018-01-23 15:26:27 -08001354
1355 proto.write(ExemptedPackage.USER_ID, mExemptedPackages.keyAt(i));
1356 proto.write(ExemptedPackage.PACKAGE_NAME, mExemptedPackages.valueAt(i, j));
1357
1358 proto.end(token2);
1359 }
1360 }
1361
Makoto Onuki2206af32017-11-21 16:25:35 -08001362 for (Pair<Integer, String> uidAndPackage : mRunAnyRestrictedPackages) {
1363 final long token2 = proto.start(
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001364 AppStateTrackerProto.RUN_ANY_IN_BACKGROUND_RESTRICTED_PACKAGES);
Makoto Onuki2206af32017-11-21 16:25:35 -08001365 proto.write(RunAnyInBackgroundRestrictedPackages.UID, uidAndPackage.first);
1366 proto.write(RunAnyInBackgroundRestrictedPackages.PACKAGE_NAME,
1367 uidAndPackage.second);
1368 proto.end(token2);
1369 }
Makoto Onukieb898f12018-01-23 15:26:27 -08001370
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001371 mStatLogger.dumpProto(proto, AppStateTrackerProto.STATS);
Makoto Onukieb898f12018-01-23 15:26:27 -08001372
Makoto Onuki2206af32017-11-21 16:25:35 -08001373 proto.end(token);
Makoto Onuki9be01402017-11-10 13:22:26 -08001374 }
1375 }
1376}