Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server; |
| 17 | |
| 18 | import android.annotation.NonNull; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 20 | import android.app.ActivityManagerInternal; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 21 | import android.app.AppOpsManager; |
| 22 | import android.app.AppOpsManager.PackageOps; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 23 | import android.app.IActivityManager; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 24 | import android.app.IUidObserver; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 25 | import android.app.usage.UsageStatsManager; |
| 26 | import android.app.usage.UsageStatsManagerInternal; |
| 27 | import android.app.usage.UsageStatsManagerInternal.AppIdleStateChangeListener; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 32 | import android.database.ContentObserver; |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 33 | import android.net.Uri; |
| 34 | import android.os.BatteryManager; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 35 | import android.os.Handler; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 36 | import android.os.Looper; |
| 37 | import android.os.Message; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 38 | import android.os.PowerManager.ServiceType; |
| 39 | import android.os.PowerManagerInternal; |
| 40 | import android.os.RemoteException; |
| 41 | import android.os.ServiceManager; |
| 42 | import android.os.UserHandle; |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 43 | import android.provider.Settings; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 44 | import android.util.ArraySet; |
| 45 | import android.util.Pair; |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 46 | import android.util.Slog; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 47 | import android.util.SparseBooleanArray; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 48 | import android.util.SparseSetArray; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 49 | import android.util.proto.ProtoOutputStream; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 50 | |
| 51 | import com.android.internal.annotations.GuardedBy; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 52 | import com.android.internal.annotations.VisibleForTesting; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 53 | import com.android.internal.app.IAppOpsCallback; |
| 54 | import com.android.internal.app.IAppOpsService; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 55 | import com.android.internal.util.ArrayUtils; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 56 | import com.android.internal.util.Preconditions; |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 57 | import com.android.server.DeviceIdleController.LocalService; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 58 | import com.android.server.ForceAppStandbyTrackerProto.ExemptedPackage; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 59 | import com.android.server.ForceAppStandbyTrackerProto.RunAnyInBackgroundRestrictedPackages; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 60 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 61 | import java.io.PrintWriter; |
| 62 | import java.util.Arrays; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 63 | import java.util.List; |
| 64 | |
| 65 | /** |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 66 | * Class to keep track of the information related to "force app standby", which includes: |
| 67 | * - OP_RUN_ANY_IN_BACKGROUND for each package |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 68 | * - UID foreground/active state |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 69 | * - User+system power save whitelist |
| 70 | * - Temporary power save whitelist |
| 71 | * - Global "force all apps standby" mode enforced by battery saver. |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 72 | * |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 73 | * TODO: Make it a LocalService. |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 74 | * |
| 75 | * Test: |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 76 | atest $ANDROID_BUILD_TOP/frameworks/base/services/tests/servicestests/src/com/android/server/ForceAppStandbyTrackerTest.java |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 77 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 78 | public class ForceAppStandbyTracker { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 79 | private static final String TAG = "ForceAppStandbyTracker"; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 80 | private static final boolean DEBUG = true; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 81 | |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 82 | @GuardedBy("ForceAppStandbyTracker.class") |
| 83 | private static ForceAppStandbyTracker sInstance; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 84 | |
| 85 | private final Object mLock = new Object(); |
| 86 | private final Context mContext; |
| 87 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 88 | @VisibleForTesting |
| 89 | static final int TARGET_OP = AppOpsManager.OP_RUN_ANY_IN_BACKGROUND; |
| 90 | |
| 91 | IActivityManager mIActivityManager; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 92 | AppOpsManager mAppOpsManager; |
| 93 | IAppOpsService mAppOpsService; |
| 94 | PowerManagerInternal mPowerManagerInternal; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 95 | StandbyTracker mStandbyTracker; |
| 96 | UsageStatsManagerInternal mUsageStatsManagerInternal; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 97 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 98 | private final MyHandler mHandler; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 99 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 100 | @VisibleForTesting |
| 101 | FeatureFlagsObserver mFlagsObserver; |
| 102 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Pair of (uid (not user-id), packageName) with OP_RUN_ANY_IN_BACKGROUND *not* allowed. |
| 105 | */ |
| 106 | @GuardedBy("mLock") |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 107 | final ArraySet<Pair<Integer, String>> mRunAnyRestrictedPackages = new ArraySet<>(); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 108 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 109 | /** UIDs that are active. */ |
| 110 | @GuardedBy("mLock") |
| 111 | final SparseBooleanArray mActiveUids = new SparseBooleanArray(); |
| 112 | |
| 113 | /** UIDs that are in the foreground. */ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 114 | @GuardedBy("mLock") |
| 115 | final SparseBooleanArray mForegroundUids = new SparseBooleanArray(); |
| 116 | |
Makoto Onuki | 71755c9 | 2018-01-16 14:15:44 -0800 | [diff] [blame] | 117 | /** |
| 118 | * System except-idle + user whitelist in the device idle controller. |
| 119 | */ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 120 | @GuardedBy("mLock") |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 121 | private int[] mPowerWhitelistedAllAppIds = new int[0]; |
| 122 | |
| 123 | @GuardedBy("mLock") |
| 124 | private int[] mTempWhitelistedAppIds = mPowerWhitelistedAllAppIds; |
| 125 | |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Per-user packages that are in the EXEMPT bucket. |
| 128 | */ |
| 129 | @GuardedBy("mLock") |
| 130 | private final SparseSetArray<String> mExemptedPackages = new SparseSetArray<>(); |
| 131 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 132 | @GuardedBy("mLock") |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 133 | final ArraySet<Listener> mListeners = new ArraySet<>(); |
| 134 | |
| 135 | @GuardedBy("mLock") |
| 136 | boolean mStarted; |
| 137 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Only used for small battery use-case. |
| 140 | */ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 141 | @GuardedBy("mLock") |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 142 | boolean mIsPluggedIn; |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 143 | |
| 144 | @GuardedBy("mLock") |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 145 | boolean mBatterySaverEnabled; |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 146 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 147 | /** |
| 148 | * True if the forced app standby is currently enabled |
| 149 | */ |
| 150 | @GuardedBy("mLock") |
| 151 | boolean mForceAllAppsStandby; |
| 152 | |
| 153 | /** |
| 154 | * True if the forced app standby for small battery devices feature is enabled in settings |
| 155 | */ |
| 156 | @GuardedBy("mLock") |
| 157 | boolean mForceAllAppStandbyForSmallBattery; |
| 158 | |
| 159 | /** |
| 160 | * True if the forced app standby feature is enabled in settings |
| 161 | */ |
| 162 | @GuardedBy("mLock") |
| 163 | boolean mForcedAppStandbyEnabled; |
| 164 | |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 165 | interface Stats { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 166 | int UID_FG_STATE_CHANGED = 0; |
| 167 | int UID_ACTIVE_STATE_CHANGED = 1; |
| 168 | int RUN_ANY_CHANGED = 2; |
| 169 | int ALL_UNWHITELISTED = 3; |
| 170 | int ALL_WHITELIST_CHANGED = 4; |
| 171 | int TEMP_WHITELIST_CHANGED = 5; |
| 172 | int EXEMPT_CHANGED = 6; |
| 173 | int FORCE_ALL_CHANGED = 7; |
| 174 | int FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED = 8; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private final StatLogger mStatLogger = new StatLogger(new String[] { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 178 | "UID_FG_STATE_CHANGED", |
| 179 | "UID_ACTIVE_STATE_CHANGED", |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 180 | "RUN_ANY_CHANGED", |
| 181 | "ALL_UNWHITELISTED", |
| 182 | "ALL_WHITELIST_CHANGED", |
| 183 | "TEMP_WHITELIST_CHANGED", |
| 184 | "EXEMPT_CHANGED", |
| 185 | "FORCE_ALL_CHANGED", |
| 186 | "FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED", |
| 187 | }); |
| 188 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 189 | @VisibleForTesting |
| 190 | class FeatureFlagsObserver extends ContentObserver { |
| 191 | FeatureFlagsObserver() { |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 192 | super(null); |
| 193 | } |
| 194 | |
| 195 | void register() { |
| 196 | mContext.getContentResolver().registerContentObserver( |
| 197 | Settings.Global.getUriFor(Settings.Global.FORCED_APP_STANDBY_ENABLED), |
| 198 | false, this); |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 199 | |
| 200 | mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor( |
| 201 | Settings.Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED), false, this); |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | boolean isForcedAppStandbyEnabled() { |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 205 | return injectGetGlobalSettingInt(Settings.Global.FORCED_APP_STANDBY_ENABLED, 1) == 1; |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 208 | boolean isForcedAppStandbyForSmallBatteryEnabled() { |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 209 | return injectGetGlobalSettingInt( |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 210 | Settings.Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED, 0) == 1; |
| 211 | } |
| 212 | |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 213 | @Override |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 214 | public void onChange(boolean selfChange, Uri uri) { |
| 215 | if (Settings.Global.getUriFor(Settings.Global.FORCED_APP_STANDBY_ENABLED).equals(uri)) { |
| 216 | final boolean enabled = isForcedAppStandbyEnabled(); |
| 217 | synchronized (mLock) { |
| 218 | if (mForcedAppStandbyEnabled == enabled) { |
| 219 | return; |
| 220 | } |
| 221 | mForcedAppStandbyEnabled = enabled; |
| 222 | if (DEBUG) { |
| 223 | Slog.d(TAG,"Forced app standby feature flag changed: " |
| 224 | + mForcedAppStandbyEnabled); |
| 225 | } |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 226 | } |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 227 | mHandler.notifyForcedAppStandbyFeatureFlagChanged(); |
| 228 | } else if (Settings.Global.getUriFor( |
| 229 | Settings.Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED).equals(uri)) { |
| 230 | final boolean enabled = isForcedAppStandbyForSmallBatteryEnabled(); |
| 231 | synchronized (mLock) { |
| 232 | if (mForceAllAppStandbyForSmallBattery == enabled) { |
| 233 | return; |
| 234 | } |
| 235 | mForceAllAppStandbyForSmallBattery = enabled; |
| 236 | if (DEBUG) { |
| 237 | Slog.d(TAG, "Forced app standby for small battery feature flag changed: " |
| 238 | + mForceAllAppStandbyForSmallBattery); |
| 239 | } |
| 240 | updateForceAllAppStandbyState(); |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 241 | } |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 242 | } else { |
| 243 | Slog.w(TAG, "Unexpected feature flag uri encountered: " + uri); |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 244 | } |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 247 | |
| 248 | public static abstract class Listener { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 249 | /** |
| 250 | * This is called when the OP_RUN_ANY_IN_BACKGROUND appops changed for a package. |
| 251 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 252 | private void onRunAnyAppOpsChanged(ForceAppStandbyTracker sender, |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 253 | int uid, @NonNull String packageName) { |
| 254 | updateJobsForUidPackage(uid, packageName); |
| 255 | |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 256 | if (!sender.areAlarmsRestricted(uid, packageName, /*allowWhileIdle=*/ false)) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 257 | unblockAlarmsForUidPackage(uid, packageName); |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 258 | } else if (!sender.areAlarmsRestricted(uid, packageName, /*allowWhileIdle=*/ true)){ |
| 259 | // we need to deliver the allow-while-idle alarms for this uid, package |
| 260 | unblockAllUnrestrictedAlarms(); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 261 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 264 | /** |
| 265 | * This is called when the foreground state changed for a UID. |
| 266 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 267 | private void onUidForegroundStateChanged(ForceAppStandbyTracker sender, int uid) { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 268 | onUidForeground(uid, sender.isUidInForeground(uid)); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * This is called when the active/idle state changed for a UID. |
| 273 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 274 | private void onUidActiveStateChanged(ForceAppStandbyTracker sender, int uid) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 275 | updateJobsForUid(uid); |
| 276 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 277 | if (sender.isUidActive(uid)) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 278 | unblockAlarmsForUid(uid); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * This is called when an app-id(s) is removed from the power save whitelist. |
| 284 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 285 | private void onPowerSaveUnwhitelisted(ForceAppStandbyTracker sender) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 286 | updateAllJobs(); |
| 287 | unblockAllUnrestrictedAlarms(); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * This is called when the power save whitelist changes, excluding the |
| 292 | * {@link #onPowerSaveUnwhitelisted} case. |
| 293 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 294 | private void onPowerSaveWhitelistedChanged(ForceAppStandbyTracker sender) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 295 | updateAllJobs(); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * This is called when the temp whitelist changes. |
| 300 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 301 | private void onTempPowerSaveWhitelistChanged(ForceAppStandbyTracker sender) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 302 | |
| 303 | // TODO This case happens rather frequently; consider optimizing and update jobs |
| 304 | // only for affected app-ids. |
| 305 | |
| 306 | updateAllJobs(); |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 307 | |
| 308 | // Note when an app is just put in the temp whitelist, we do *not* drain pending alarms. |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * This is called when the EXEMPT bucket is updated. |
| 313 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 314 | private void onExemptChanged(ForceAppStandbyTracker sender) { |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 315 | // This doesn't happen very often, so just re-evaluate all jobs / alarms. |
| 316 | updateAllJobs(); |
| 317 | unblockAllUnrestrictedAlarms(); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
| 321 | * This is called when the global "force all apps standby" flag changes. |
| 322 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 323 | private void onForceAllAppsStandbyChanged(ForceAppStandbyTracker sender) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 324 | updateAllJobs(); |
| 325 | |
| 326 | if (!sender.isForceAllAppsStandbyEnabled()) { |
| 327 | unblockAllUnrestrictedAlarms(); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Called when the job restrictions for multiple UIDs might have changed, so the job |
| 333 | * scheduler should re-evaluate all restrictions for all jobs. |
| 334 | */ |
| 335 | public void updateAllJobs() { |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Called when the job restrictions for a UID might have changed, so the job |
| 340 | * scheduler should re-evaluate all restrictions for all jobs. |
| 341 | */ |
| 342 | public void updateJobsForUid(int uid) { |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Called when the job restrictions for a UID - package might have changed, so the job |
| 347 | * scheduler should re-evaluate all restrictions for all jobs. |
| 348 | */ |
| 349 | public void updateJobsForUidPackage(int uid, String packageName) { |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Called when the job restrictions for multiple UIDs might have changed, so the alarm |
| 354 | * manager should re-evaluate all restrictions for all blocked jobs. |
| 355 | */ |
| 356 | public void unblockAllUnrestrictedAlarms() { |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Called when all jobs for a specific UID are unblocked. |
| 361 | */ |
| 362 | public void unblockAlarmsForUid(int uid) { |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Called when all alarms for a specific UID - package are unblocked. |
| 367 | */ |
| 368 | public void unblockAlarmsForUidPackage(int uid, String packageName) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 369 | } |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * Called when a UID comes into the foreground or the background. |
| 373 | * |
| 374 | * @see #isUidInForeground(int) |
| 375 | */ |
| 376 | public void onUidForeground(int uid, boolean foreground) { |
| 377 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 380 | @VisibleForTesting |
| 381 | ForceAppStandbyTracker(Context context, Looper looper) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 382 | mContext = context; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 383 | mHandler = new MyHandler(looper); |
| 384 | } |
| 385 | |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 386 | private ForceAppStandbyTracker(Context context) { |
| 387 | this(context, FgThread.get().getLooper()); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Get the singleton instance. |
| 392 | */ |
| 393 | public static synchronized ForceAppStandbyTracker getInstance(Context context) { |
| 394 | if (sInstance == null) { |
| 395 | sInstance = new ForceAppStandbyTracker(context); |
| 396 | } |
| 397 | return sInstance; |
| 398 | } |
| 399 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 400 | /** |
| 401 | * Call it when the system is ready. |
| 402 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 403 | public void start() { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 404 | synchronized (mLock) { |
| 405 | if (mStarted) { |
| 406 | return; |
| 407 | } |
| 408 | mStarted = true; |
| 409 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 410 | mIActivityManager = Preconditions.checkNotNull(injectIActivityManager()); |
| 411 | mAppOpsManager = Preconditions.checkNotNull(injectAppOpsManager()); |
| 412 | mAppOpsService = Preconditions.checkNotNull(injectIAppOpsService()); |
| 413 | mPowerManagerInternal = Preconditions.checkNotNull(injectPowerManagerInternal()); |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 414 | mUsageStatsManagerInternal = Preconditions.checkNotNull( |
| 415 | injectUsageStatsManagerInternal()); |
| 416 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 417 | mFlagsObserver = new FeatureFlagsObserver(); |
| 418 | mFlagsObserver.register(); |
| 419 | mForcedAppStandbyEnabled = mFlagsObserver.isForcedAppStandbyEnabled(); |
| 420 | mForceAllAppStandbyForSmallBattery = |
| 421 | mFlagsObserver.isForcedAppStandbyForSmallBatteryEnabled(); |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 422 | mStandbyTracker = new StandbyTracker(); |
| 423 | mUsageStatsManagerInternal.addAppIdleStateChangeListener(mStandbyTracker); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 424 | |
| 425 | try { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 426 | mIActivityManager.registerUidObserver(new UidObserver(), |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 427 | ActivityManager.UID_OBSERVER_GONE |
| 428 | | ActivityManager.UID_OBSERVER_IDLE |
| 429 | | ActivityManager.UID_OBSERVER_ACTIVE |
| 430 | | ActivityManager.UID_OBSERVER_PROCSTATE, |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 431 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 432 | mAppOpsService.startWatchingMode(TARGET_OP, null, |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 433 | new AppOpsWatcher()); |
| 434 | } catch (RemoteException e) { |
| 435 | // shouldn't happen. |
| 436 | } |
| 437 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 438 | IntentFilter filter = new IntentFilter(); |
| 439 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 440 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 441 | mContext.registerReceiver(new MyReceiver(), filter); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 442 | |
| 443 | refreshForcedAppStandbyUidPackagesLocked(); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 444 | |
| 445 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 446 | ServiceType.FORCE_ALL_APPS_STANDBY, |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 447 | (state) -> { |
| 448 | synchronized (mLock) { |
| 449 | mBatterySaverEnabled = state.batterySaverEnabled; |
| 450 | updateForceAllAppStandbyState(); |
| 451 | } |
| 452 | }); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 453 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 454 | mBatterySaverEnabled = mPowerManagerInternal.getLowPowerState( |
| 455 | ServiceType.FORCE_ALL_APPS_STANDBY).batterySaverEnabled; |
| 456 | |
| 457 | updateForceAllAppStandbyState(); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 461 | @VisibleForTesting |
| 462 | AppOpsManager injectAppOpsManager() { |
| 463 | return mContext.getSystemService(AppOpsManager.class); |
| 464 | } |
| 465 | |
| 466 | @VisibleForTesting |
| 467 | IAppOpsService injectIAppOpsService() { |
| 468 | return IAppOpsService.Stub.asInterface( |
| 469 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
| 470 | } |
| 471 | |
| 472 | @VisibleForTesting |
| 473 | IActivityManager injectIActivityManager() { |
| 474 | return ActivityManager.getService(); |
| 475 | } |
| 476 | |
| 477 | @VisibleForTesting |
| 478 | PowerManagerInternal injectPowerManagerInternal() { |
| 479 | return LocalServices.getService(PowerManagerInternal.class); |
| 480 | } |
| 481 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 482 | @VisibleForTesting |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 483 | UsageStatsManagerInternal injectUsageStatsManagerInternal() { |
| 484 | return LocalServices.getService(UsageStatsManagerInternal.class); |
| 485 | } |
| 486 | |
| 487 | @VisibleForTesting |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 488 | boolean isSmallBatteryDevice() { |
| 489 | return ActivityManager.isSmallBatteryDevice(); |
| 490 | } |
| 491 | |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 492 | @VisibleForTesting |
| 493 | int injectGetGlobalSettingInt(String key, int def) { |
| 494 | return Settings.Global.getInt(mContext.getContentResolver(), key, def); |
| 495 | } |
| 496 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 497 | /** |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 498 | * Update {@link #mRunAnyRestrictedPackages} with the current app ops state. |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 499 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 500 | @GuardedBy("mLock") |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 501 | private void refreshForcedAppStandbyUidPackagesLocked() { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 502 | mRunAnyRestrictedPackages.clear(); |
| 503 | final List<PackageOps> ops = mAppOpsManager.getPackagesForOps( |
| 504 | new int[] {TARGET_OP}); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 505 | |
| 506 | if (ops == null) { |
| 507 | return; |
| 508 | } |
| 509 | final int size = ops.size(); |
| 510 | for (int i = 0; i < size; i++) { |
| 511 | final AppOpsManager.PackageOps pkg = ops.get(i); |
| 512 | final List<AppOpsManager.OpEntry> entries = ops.get(i).getOps(); |
| 513 | |
| 514 | for (int j = 0; j < entries.size(); j++) { |
| 515 | AppOpsManager.OpEntry ent = entries.get(j); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 516 | if (ent.getOp() != TARGET_OP) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 517 | continue; |
| 518 | } |
| 519 | if (ent.getMode() != AppOpsManager.MODE_ALLOWED) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 520 | mRunAnyRestrictedPackages.add(Pair.create( |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 521 | pkg.getUid(), pkg.getPackageName())); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 527 | private void updateForceAllAppStandbyState() { |
| 528 | synchronized (mLock) { |
| 529 | if (mForceAllAppStandbyForSmallBattery && isSmallBatteryDevice()) { |
| 530 | toggleForceAllAppsStandbyLocked(!mIsPluggedIn); |
| 531 | } else { |
| 532 | toggleForceAllAppsStandbyLocked(mBatterySaverEnabled); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 537 | /** |
| 538 | * Update {@link #mForceAllAppsStandby} and notifies the listeners. |
| 539 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 540 | @GuardedBy("mLock") |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 541 | private void toggleForceAllAppsStandbyLocked(boolean enable) { |
| 542 | if (enable == mForceAllAppsStandby) { |
| 543 | return; |
Makoto Onuki | 12391f2 | 2018-01-18 21:44:28 +0000 | [diff] [blame] | 544 | } |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 545 | mForceAllAppsStandby = enable; |
| 546 | |
| 547 | mHandler.notifyForceAllAppsStandbyChanged(); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 550 | @GuardedBy("mLock") |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 551 | private int findForcedAppStandbyUidPackageIndexLocked(int uid, @NonNull String packageName) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 552 | final int size = mRunAnyRestrictedPackages.size(); |
| 553 | if (size > 8) { |
| 554 | return mRunAnyRestrictedPackages.indexOf(Pair.create(uid, packageName)); |
| 555 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 556 | for (int i = 0; i < size; i++) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 557 | final Pair<Integer, String> pair = mRunAnyRestrictedPackages.valueAt(i); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 558 | |
| 559 | if ((pair.first == uid) && packageName.equals(pair.second)) { |
| 560 | return i; |
| 561 | } |
| 562 | } |
| 563 | return -1; |
| 564 | } |
| 565 | |
| 566 | /** |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 567 | * @return whether a uid package-name pair is in mRunAnyRestrictedPackages. |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 568 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 569 | @GuardedBy("mLock") |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 570 | boolean isRunAnyRestrictedLocked(int uid, @NonNull String packageName) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 571 | return findForcedAppStandbyUidPackageIndexLocked(uid, packageName) >= 0; |
| 572 | } |
| 573 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 574 | /** |
| 575 | * Add to / remove from {@link #mRunAnyRestrictedPackages}. |
| 576 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 577 | @GuardedBy("mLock") |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 578 | boolean updateForcedAppStandbyUidPackageLocked(int uid, @NonNull String packageName, |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 579 | boolean restricted) { |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 580 | final int index = findForcedAppStandbyUidPackageIndexLocked(uid, packageName); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 581 | final boolean wasRestricted = index >= 0; |
| 582 | if (wasRestricted == restricted) { |
| 583 | return false; |
| 584 | } |
| 585 | if (restricted) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 586 | mRunAnyRestrictedPackages.add(Pair.create(uid, packageName)); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 587 | } else { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 588 | mRunAnyRestrictedPackages.removeAt(index); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 589 | } |
| 590 | return true; |
| 591 | } |
| 592 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 593 | private static boolean addUidToArray(SparseBooleanArray array, int uid) { |
| 594 | if (UserHandle.isCore(uid)) { |
| 595 | return false; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 596 | } |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 597 | if (array.get(uid)) { |
| 598 | return false; |
| 599 | } |
| 600 | array.put(uid, true); |
| 601 | return true; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 604 | private static boolean removeUidFromArray(SparseBooleanArray array, int uid, boolean remove) { |
| 605 | if (UserHandle.isCore(uid)) { |
| 606 | return false; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 607 | } |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 608 | if (!array.get(uid)) { |
| 609 | return false; |
| 610 | } |
| 611 | if (remove) { |
| 612 | array.delete(uid); |
| 613 | } else { |
| 614 | array.put(uid, false); |
| 615 | } |
| 616 | return true; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 619 | private final class UidObserver extends IUidObserver.Stub { |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 620 | @Override |
| 621 | public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 622 | mHandler.onUidStateChanged(uid, procState); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 625 | @Override |
| 626 | public void onUidActive(int uid) { |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 627 | mHandler.onUidActive(uid); |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public void onUidGone(int uid, boolean disabled) { |
| 632 | mHandler.onUidGone(uid, disabled); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 635 | @Override |
| 636 | public void onUidIdle(int uid, boolean disabled) { |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 637 | mHandler.onUidIdle(uid, disabled); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 640 | @Override |
| 641 | public void onUidCachedChanged(int uid, boolean cached) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 642 | } |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 643 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 644 | |
| 645 | private final class AppOpsWatcher extends IAppOpsCallback.Stub { |
| 646 | @Override |
| 647 | public void opChanged(int op, int uid, String packageName) throws RemoteException { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 648 | boolean restricted = false; |
| 649 | try { |
| 650 | restricted = mAppOpsService.checkOperation(TARGET_OP, |
| 651 | uid, packageName) != AppOpsManager.MODE_ALLOWED; |
| 652 | } catch (RemoteException e) { |
| 653 | // Shouldn't happen |
| 654 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 655 | synchronized (mLock) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 656 | if (updateForcedAppStandbyUidPackageLocked(uid, packageName, restricted)) { |
| 657 | mHandler.notifyRunAnyAppOpsChanged(uid, packageName); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 662 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 663 | private final class MyReceiver extends BroadcastReceiver { |
| 664 | @Override |
| 665 | public void onReceive(Context context, Intent intent) { |
| 666 | if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) { |
| 667 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 668 | if (userId > 0) { |
| 669 | mHandler.doUserRemoved(userId); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 670 | } |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 671 | } else if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) { |
| 672 | synchronized (mLock) { |
| 673 | mIsPluggedIn = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0); |
| 674 | } |
| 675 | updateForceAllAppStandbyState(); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 680 | final class StandbyTracker extends AppIdleStateChangeListener { |
| 681 | @Override |
| 682 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle, |
| 683 | int bucket) { |
| 684 | if (DEBUG) { |
| 685 | Slog.d(TAG,"onAppIdleStateChanged: " + packageName + " u" + userId |
| 686 | + (idle ? " idle" : " active") + " " + bucket); |
| 687 | } |
| 688 | final boolean changed; |
| 689 | if (bucket == UsageStatsManager.STANDBY_BUCKET_EXEMPTED) { |
| 690 | changed = mExemptedPackages.add(userId, packageName); |
| 691 | } else { |
| 692 | changed = mExemptedPackages.remove(userId, packageName); |
| 693 | } |
| 694 | if (changed) { |
| 695 | mHandler.notifyExemptChanged(); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | @Override |
| 700 | public void onParoleStateChanged(boolean isParoleOn) { |
| 701 | } |
| 702 | } |
| 703 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 704 | private Listener[] cloneListeners() { |
| 705 | synchronized (mLock) { |
| 706 | return mListeners.toArray(new Listener[mListeners.size()]); |
| 707 | } |
| 708 | } |
| 709 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 710 | private class MyHandler extends Handler { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 711 | private static final int MSG_UID_ACTIVE_STATE_CHANGED = 0; |
| 712 | private static final int MSG_UID_FG_STATE_CHANGED = 1; |
| 713 | private static final int MSG_RUN_ANY_CHANGED = 3; |
| 714 | private static final int MSG_ALL_UNWHITELISTED = 4; |
| 715 | private static final int MSG_ALL_WHITELIST_CHANGED = 5; |
| 716 | private static final int MSG_TEMP_WHITELIST_CHANGED = 6; |
| 717 | private static final int MSG_FORCE_ALL_CHANGED = 7; |
| 718 | private static final int MSG_USER_REMOVED = 8; |
| 719 | private static final int MSG_FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED = 9; |
| 720 | private static final int MSG_EXEMPT_CHANGED = 10; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 721 | |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 722 | private static final int MSG_ON_UID_STATE_CHANGED = 11; |
| 723 | private static final int MSG_ON_UID_ACTIVE = 12; |
| 724 | private static final int MSG_ON_UID_GONE = 13; |
| 725 | private static final int MSG_ON_UID_IDLE = 14; |
| 726 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 727 | public MyHandler(Looper looper) { |
| 728 | super(looper); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 729 | } |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 730 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 731 | public void notifyUidActiveStateChanged(int uid) { |
| 732 | obtainMessage(MSG_UID_ACTIVE_STATE_CHANGED, uid, 0).sendToTarget(); |
| 733 | } |
| 734 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 735 | public void notifyUidForegroundStateChanged(int uid) { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 736 | obtainMessage(MSG_UID_FG_STATE_CHANGED, uid, 0).sendToTarget(); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 737 | } |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 738 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 739 | public void notifyRunAnyAppOpsChanged(int uid, @NonNull String packageName) { |
| 740 | obtainMessage(MSG_RUN_ANY_CHANGED, uid, 0, packageName).sendToTarget(); |
| 741 | } |
| 742 | |
| 743 | public void notifyAllUnwhitelisted() { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 744 | removeMessages(MSG_ALL_UNWHITELISTED); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 745 | obtainMessage(MSG_ALL_UNWHITELISTED).sendToTarget(); |
| 746 | } |
| 747 | |
| 748 | public void notifyAllWhitelistChanged() { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 749 | removeMessages(MSG_ALL_WHITELIST_CHANGED); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 750 | obtainMessage(MSG_ALL_WHITELIST_CHANGED).sendToTarget(); |
| 751 | } |
| 752 | |
| 753 | public void notifyTempWhitelistChanged() { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 754 | removeMessages(MSG_TEMP_WHITELIST_CHANGED); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 755 | obtainMessage(MSG_TEMP_WHITELIST_CHANGED).sendToTarget(); |
| 756 | } |
| 757 | |
| 758 | public void notifyForceAllAppsStandbyChanged() { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 759 | removeMessages(MSG_FORCE_ALL_CHANGED); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 760 | obtainMessage(MSG_FORCE_ALL_CHANGED).sendToTarget(); |
| 761 | } |
| 762 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 763 | public void notifyForcedAppStandbyFeatureFlagChanged() { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 764 | removeMessages(MSG_FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED); |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 765 | obtainMessage(MSG_FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED).sendToTarget(); |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 768 | public void notifyExemptChanged() { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 769 | removeMessages(MSG_EXEMPT_CHANGED); |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 770 | obtainMessage(MSG_EXEMPT_CHANGED).sendToTarget(); |
| 771 | } |
| 772 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 773 | public void doUserRemoved(int userId) { |
| 774 | obtainMessage(MSG_USER_REMOVED, userId, 0).sendToTarget(); |
| 775 | } |
| 776 | |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 777 | public void onUidStateChanged(int uid, int procState) { |
| 778 | obtainMessage(MSG_ON_UID_STATE_CHANGED, uid, procState).sendToTarget(); |
| 779 | } |
| 780 | |
| 781 | public void onUidActive(int uid) { |
| 782 | obtainMessage(MSG_ON_UID_ACTIVE, uid, 0).sendToTarget(); |
| 783 | } |
| 784 | |
| 785 | public void onUidGone(int uid, boolean disabled) { |
| 786 | obtainMessage(MSG_ON_UID_GONE, uid, disabled ? 1 : 0).sendToTarget(); |
| 787 | } |
| 788 | |
| 789 | public void onUidIdle(int uid, boolean disabled) { |
| 790 | obtainMessage(MSG_ON_UID_IDLE, uid, disabled ? 1 : 0).sendToTarget(); |
| 791 | } |
| 792 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 793 | @Override |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 794 | public void handleMessage(Message msg) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 795 | switch (msg.what) { |
| 796 | case MSG_USER_REMOVED: |
| 797 | handleUserRemoved(msg.arg1); |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | // Only notify the listeners when started. |
| 802 | synchronized (mLock) { |
| 803 | if (!mStarted) { |
| 804 | return; |
| 805 | } |
| 806 | } |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 807 | final ForceAppStandbyTracker sender = ForceAppStandbyTracker.this; |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 808 | |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 809 | long start = mStatLogger.getTime(); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 810 | switch (msg.what) { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 811 | case MSG_UID_ACTIVE_STATE_CHANGED: |
| 812 | for (Listener l : cloneListeners()) { |
| 813 | l.onUidActiveStateChanged(sender, msg.arg1); |
| 814 | } |
| 815 | mStatLogger.logDurationStat(Stats.UID_ACTIVE_STATE_CHANGED, start); |
| 816 | return; |
| 817 | |
| 818 | case MSG_UID_FG_STATE_CHANGED: |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 819 | for (Listener l : cloneListeners()) { |
| 820 | l.onUidForegroundStateChanged(sender, msg.arg1); |
| 821 | } |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 822 | mStatLogger.logDurationStat(Stats.UID_FG_STATE_CHANGED, start); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 823 | return; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 824 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 825 | case MSG_RUN_ANY_CHANGED: |
| 826 | for (Listener l : cloneListeners()) { |
| 827 | l.onRunAnyAppOpsChanged(sender, msg.arg1, (String) msg.obj); |
| 828 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 829 | mStatLogger.logDurationStat(Stats.RUN_ANY_CHANGED, start); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 830 | return; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 831 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 832 | case MSG_ALL_UNWHITELISTED: |
| 833 | for (Listener l : cloneListeners()) { |
| 834 | l.onPowerSaveUnwhitelisted(sender); |
| 835 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 836 | mStatLogger.logDurationStat(Stats.ALL_UNWHITELISTED, start); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 837 | return; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 838 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 839 | case MSG_ALL_WHITELIST_CHANGED: |
| 840 | for (Listener l : cloneListeners()) { |
| 841 | l.onPowerSaveWhitelistedChanged(sender); |
| 842 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 843 | mStatLogger.logDurationStat(Stats.ALL_WHITELIST_CHANGED, start); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 844 | return; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 845 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 846 | case MSG_TEMP_WHITELIST_CHANGED: |
| 847 | for (Listener l : cloneListeners()) { |
| 848 | l.onTempPowerSaveWhitelistChanged(sender); |
| 849 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 850 | mStatLogger.logDurationStat(Stats.TEMP_WHITELIST_CHANGED, start); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 851 | return; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 852 | |
| 853 | case MSG_EXEMPT_CHANGED: |
| 854 | for (Listener l : cloneListeners()) { |
| 855 | l.onExemptChanged(sender); |
| 856 | } |
| 857 | mStatLogger.logDurationStat(Stats.EXEMPT_CHANGED, start); |
| 858 | return; |
| 859 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 860 | case MSG_FORCE_ALL_CHANGED: |
| 861 | for (Listener l : cloneListeners()) { |
| 862 | l.onForceAllAppsStandbyChanged(sender); |
| 863 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 864 | mStatLogger.logDurationStat(Stats.FORCE_ALL_CHANGED, start); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 865 | return; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 866 | |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 867 | case MSG_FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED: |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 868 | // Feature flag for forced app standby changed. |
| 869 | final boolean unblockAlarms; |
| 870 | synchronized (mLock) { |
| 871 | unblockAlarms = !mForcedAppStandbyEnabled && !mForceAllAppsStandby; |
| 872 | } |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 873 | for (Listener l : cloneListeners()) { |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 874 | l.updateAllJobs(); |
| 875 | if (unblockAlarms) { |
| 876 | l.unblockAllUnrestrictedAlarms(); |
| 877 | } |
| 878 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 879 | mStatLogger.logDurationStat( |
| 880 | Stats.FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED, start); |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 881 | return; |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 882 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 883 | case MSG_USER_REMOVED: |
| 884 | handleUserRemoved(msg.arg1); |
| 885 | return; |
Makoto Onuki | 4d298b5 | 2018-02-05 10:54:58 -0800 | [diff] [blame] | 886 | |
| 887 | case MSG_ON_UID_STATE_CHANGED: |
| 888 | handleUidStateChanged(msg.arg1, msg.arg2); |
| 889 | return; |
| 890 | case MSG_ON_UID_ACTIVE: |
| 891 | handleUidActive(msg.arg1); |
| 892 | return; |
| 893 | case MSG_ON_UID_GONE: |
| 894 | handleUidGone(msg.arg1, msg.arg1 != 0); |
| 895 | return; |
| 896 | case MSG_ON_UID_IDLE: |
| 897 | handleUidIdle(msg.arg1, msg.arg1 != 0); |
| 898 | return; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | public void handleUidStateChanged(int uid, int procState) { |
| 903 | synchronized (mLock) { |
| 904 | if (procState > ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) { |
| 905 | if (removeUidFromArray(mForegroundUids, uid, false)) { |
| 906 | mHandler.notifyUidForegroundStateChanged(uid); |
| 907 | } |
| 908 | } else { |
| 909 | if (addUidToArray(mForegroundUids, uid)) { |
| 910 | mHandler.notifyUidForegroundStateChanged(uid); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | public void handleUidActive(int uid) { |
| 917 | synchronized (mLock) { |
| 918 | if (addUidToArray(mActiveUids, uid)) { |
| 919 | mHandler.notifyUidActiveStateChanged(uid); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | public void handleUidGone(int uid, boolean disabled) { |
| 925 | removeUid(uid, true); |
| 926 | } |
| 927 | |
| 928 | public void handleUidIdle(int uid, boolean disabled) { |
| 929 | // Just to avoid excessive memcpy, don't remove from the array in this case. |
| 930 | removeUid(uid, false); |
| 931 | } |
| 932 | |
| 933 | private void removeUid(int uid, boolean remove) { |
| 934 | synchronized (mLock) { |
| 935 | if (removeUidFromArray(mActiveUids, uid, remove)) { |
| 936 | mHandler.notifyUidActiveStateChanged(uid); |
| 937 | } |
| 938 | if (removeUidFromArray(mForegroundUids, uid, remove)) { |
| 939 | mHandler.notifyUidForegroundStateChanged(uid); |
| 940 | } |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | void handleUserRemoved(int removedUserId) { |
| 946 | synchronized (mLock) { |
| 947 | for (int i = mRunAnyRestrictedPackages.size() - 1; i >= 0; i--) { |
| 948 | final Pair<Integer, String> pair = mRunAnyRestrictedPackages.valueAt(i); |
| 949 | final int uid = pair.first; |
| 950 | final int userId = UserHandle.getUserId(uid); |
| 951 | |
| 952 | if (userId == removedUserId) { |
| 953 | mRunAnyRestrictedPackages.removeAt(i); |
| 954 | } |
| 955 | } |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 956 | cleanUpArrayForUser(mActiveUids, removedUserId); |
| 957 | cleanUpArrayForUser(mForegroundUids, removedUserId); |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 958 | mExemptedPackages.remove(removedUserId); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 962 | private void cleanUpArrayForUser(SparseBooleanArray array, int removedUserId) { |
| 963 | for (int i = array.size() - 1; i >= 0; i--) { |
| 964 | final int uid = array.keyAt(i); |
| 965 | final int userId = UserHandle.getUserId(uid); |
| 966 | |
| 967 | if (userId == removedUserId) { |
| 968 | array.removeAt(i); |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 973 | /** |
| 974 | * Called by device idle controller to update the power save whitelists. |
| 975 | */ |
| 976 | public void setPowerSaveWhitelistAppIds( |
| 977 | int[] powerSaveWhitelistAllAppIdArray, int[] tempWhitelistAppIdArray) { |
| 978 | synchronized (mLock) { |
| 979 | final int[] previousWhitelist = mPowerWhitelistedAllAppIds; |
| 980 | final int[] previousTempWhitelist = mTempWhitelistedAppIds; |
| 981 | |
| 982 | mPowerWhitelistedAllAppIds = powerSaveWhitelistAllAppIdArray; |
| 983 | mTempWhitelistedAppIds = tempWhitelistAppIdArray; |
| 984 | |
| 985 | if (isAnyAppIdUnwhitelisted(previousWhitelist, mPowerWhitelistedAllAppIds)) { |
| 986 | mHandler.notifyAllUnwhitelisted(); |
| 987 | } else if (!Arrays.equals(previousWhitelist, mPowerWhitelistedAllAppIds)) { |
| 988 | mHandler.notifyAllWhitelistChanged(); |
| 989 | } |
| 990 | |
| 991 | if (!Arrays.equals(previousTempWhitelist, mTempWhitelistedAppIds)) { |
| 992 | mHandler.notifyTempWhitelistChanged(); |
| 993 | } |
| 994 | |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * @retunr true if a sorted app-id array {@code prevArray} has at least one element |
| 1000 | * that's not in a sorted app-id array {@code newArray}. |
| 1001 | */ |
| 1002 | @VisibleForTesting |
| 1003 | static boolean isAnyAppIdUnwhitelisted(int[] prevArray, int[] newArray) { |
| 1004 | int i1 = 0; |
| 1005 | int i2 = 0; |
| 1006 | boolean prevFinished; |
| 1007 | boolean newFinished; |
| 1008 | |
| 1009 | for (;;) { |
| 1010 | prevFinished = i1 >= prevArray.length; |
| 1011 | newFinished = i2 >= newArray.length; |
| 1012 | if (prevFinished || newFinished) { |
| 1013 | break; |
| 1014 | } |
| 1015 | int a1 = prevArray[i1]; |
| 1016 | int a2 = newArray[i2]; |
| 1017 | |
| 1018 | if (a1 == a2) { |
| 1019 | i1++; |
| 1020 | i2++; |
| 1021 | continue; |
| 1022 | } |
| 1023 | if (a1 < a2) { |
| 1024 | // prevArray has an element that's not in a2. |
| 1025 | return true; |
| 1026 | } |
| 1027 | i2++; |
| 1028 | } |
| 1029 | if (prevFinished) { |
| 1030 | return false; |
| 1031 | } |
| 1032 | return newFinished; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | // Public interface. |
| 1036 | |
| 1037 | /** |
| 1038 | * Register a new listener. |
| 1039 | */ |
| 1040 | public void addListener(@NonNull Listener listener) { |
| 1041 | synchronized (mLock) { |
| 1042 | mListeners.add(listener); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /** |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1047 | * @return whether alarms should be restricted for a UID package-name. |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1048 | */ |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 1049 | public boolean areAlarmsRestricted(int uid, @NonNull String packageName, |
| 1050 | boolean allowWhileIdle) { |
Makoto Onuki | eb8cfd1 | 2018-01-19 15:43:16 -0800 | [diff] [blame] | 1051 | return isRestricted(uid, packageName, /*useTempWhitelistToo=*/ false, |
Suprabh Shukla | c25447d | 2018-01-19 16:43:35 -0800 | [diff] [blame] | 1052 | /* exemptOnBatterySaver =*/ allowWhileIdle); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * @return whether jobs should be restricted for a UID package-name. |
| 1057 | */ |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1058 | public boolean areJobsRestricted(int uid, @NonNull String packageName, |
| 1059 | boolean hasForegroundExemption) { |
Makoto Onuki | eb8cfd1 | 2018-01-19 15:43:16 -0800 | [diff] [blame] | 1060 | return isRestricted(uid, packageName, /*useTempWhitelistToo=*/ true, |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1061 | hasForegroundExemption); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * @return whether force-app-standby is effective for a UID package-name. |
| 1066 | */ |
| 1067 | private boolean isRestricted(int uid, @NonNull String packageName, |
Makoto Onuki | eb8cfd1 | 2018-01-19 15:43:16 -0800 | [diff] [blame] | 1068 | boolean useTempWhitelistToo, boolean exemptOnBatterySaver) { |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 1069 | if (isUidActive(uid)) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
| 1072 | synchronized (mLock) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1073 | // Whitelisted? |
| 1074 | final int appId = UserHandle.getAppId(uid); |
| 1075 | if (ArrayUtils.contains(mPowerWhitelistedAllAppIds, appId)) { |
| 1076 | return false; |
| 1077 | } |
| 1078 | if (useTempWhitelistToo && |
| 1079 | ArrayUtils.contains(mTempWhitelistedAppIds, appId)) { |
| 1080 | return false; |
| 1081 | } |
Makoto Onuki | eb8cfd1 | 2018-01-19 15:43:16 -0800 | [diff] [blame] | 1082 | if (mForcedAppStandbyEnabled && isRunAnyRestrictedLocked(uid, packageName)) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1083 | return true; |
| 1084 | } |
Makoto Onuki | eb8cfd1 | 2018-01-19 15:43:16 -0800 | [diff] [blame] | 1085 | if (exemptOnBatterySaver) { |
| 1086 | return false; |
| 1087 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 1088 | final int userId = UserHandle.getUserId(uid); |
| 1089 | if (mExemptedPackages.contains(userId, packageName)) { |
| 1090 | return false; |
| 1091 | } |
Makoto Onuki | eb8cfd1 | 2018-01-19 15:43:16 -0800 | [diff] [blame] | 1092 | return mForceAllAppsStandby; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1096 | /** |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 1097 | * @return whether a UID is in active or not. |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 1098 | * |
| 1099 | * Note this information is based on the UID proc state callback, meaning it's updated |
| 1100 | * asynchronously and may subtly be stale. If the fresh data is needed, use |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 1101 | * {@link ActivityManagerInternal#getUidProcessState} instead. |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 1102 | */ |
| 1103 | public boolean isUidActive(int uid) { |
| 1104 | if (UserHandle.isCore(uid)) { |
| 1105 | return true; |
| 1106 | } |
| 1107 | synchronized (mLock) { |
| 1108 | return mActiveUids.get(uid); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /** |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1113 | * @return whether a UID is in the foreground or not. |
| 1114 | * |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1115 | * Note this information is based on the UID proc state callback, meaning it's updated |
| 1116 | * asynchronously and may subtly be stale. If the fresh data is needed, use |
| 1117 | * {@link ActivityManagerInternal#getUidProcessState} instead. |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1118 | */ |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 1119 | public boolean isUidInForeground(int uid) { |
Makoto Onuki | 9cc471c | 2018-01-23 12:33:56 -0800 | [diff] [blame] | 1120 | if (UserHandle.isCore(uid)) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1121 | return true; |
| 1122 | } |
| 1123 | synchronized (mLock) { |
| 1124 | return mForegroundUids.get(uid); |
| 1125 | } |
| 1126 | } |
| 1127 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1128 | /** |
| 1129 | * @return whether force all apps standby is enabled or not. |
| 1130 | * |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1131 | */ |
| 1132 | boolean isForceAllAppsStandbyEnabled() { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1133 | synchronized (mLock) { |
| 1134 | return mForceAllAppsStandby; |
| 1135 | } |
| 1136 | } |
| 1137 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1138 | /** |
| 1139 | * @return whether a UID/package has {@code OP_RUN_ANY_IN_BACKGROUND} allowed or not. |
| 1140 | * |
| 1141 | * Note clients normally shouldn't need to access it. It's only for dumpsys. |
| 1142 | */ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1143 | public boolean isRunAnyInBackgroundAppOpsAllowed(int uid, @NonNull String packageName) { |
| 1144 | synchronized (mLock) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1145 | return !isRunAnyRestrictedLocked(uid, packageName); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1149 | /** |
| 1150 | * @return whether a UID is in the user / system defined power-save whitelist or not. |
| 1151 | * |
| 1152 | * Note clients normally shouldn't need to access it. It's only for dumpsys. |
| 1153 | */ |
| 1154 | public boolean isUidPowerSaveWhitelisted(int uid) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1155 | synchronized (mLock) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1156 | return ArrayUtils.contains(mPowerWhitelistedAllAppIds, UserHandle.getAppId(uid)); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1160 | /** |
| 1161 | * @return whether a UID is in the temp power-save whitelist or not. |
| 1162 | * |
| 1163 | * Note clients normally shouldn't need to access it. It's only for dumpsys. |
| 1164 | */ |
| 1165 | public boolean isUidTempPowerSaveWhitelisted(int uid) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1166 | synchronized (mLock) { |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1167 | return ArrayUtils.contains(mTempWhitelistedAppIds, UserHandle.getAppId(uid)); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | public void dump(PrintWriter pw, String indent) { |
| 1172 | synchronized (mLock) { |
| 1173 | pw.print(indent); |
Suprabh Shukla | 4deb852 | 2018-01-08 16:27:10 -0800 | [diff] [blame] | 1174 | pw.println("Forced App Standby Feature enabled: " + mForcedAppStandbyEnabled); |
| 1175 | |
| 1176 | pw.print(indent); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1177 | pw.print("Force all apps standby: "); |
| 1178 | pw.println(isForceAllAppsStandbyEnabled()); |
| 1179 | |
| 1180 | pw.print(indent); |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 1181 | pw.print("Small Battery Device: "); |
| 1182 | pw.println(isSmallBatteryDevice()); |
| 1183 | |
| 1184 | pw.print(indent); |
| 1185 | pw.print("Force all apps standby for small battery device: "); |
| 1186 | pw.println(mForceAllAppStandbyForSmallBattery); |
| 1187 | |
| 1188 | pw.print(indent); |
| 1189 | pw.print("Plugged In: "); |
| 1190 | pw.println(mIsPluggedIn); |
| 1191 | |
| 1192 | pw.print(indent); |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 1193 | pw.print("Active uids: "); |
| 1194 | dumpUids(pw, mActiveUids); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1195 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 1196 | pw.print(indent); |
| 1197 | pw.print("Foreground uids: "); |
| 1198 | dumpUids(pw, mForegroundUids); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1199 | |
| 1200 | pw.print(indent); |
| 1201 | pw.print("Whitelist appids: "); |
| 1202 | pw.println(Arrays.toString(mPowerWhitelistedAllAppIds)); |
| 1203 | |
| 1204 | pw.print(indent); |
| 1205 | pw.print("Temp whitelist appids: "); |
| 1206 | pw.println(Arrays.toString(mTempWhitelistedAppIds)); |
| 1207 | |
| 1208 | pw.print(indent); |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 1209 | pw.println("Exempted packages:"); |
| 1210 | for (int i = 0; i < mExemptedPackages.size(); i++) { |
| 1211 | pw.print(indent); |
| 1212 | pw.print(" User "); |
| 1213 | pw.print(mExemptedPackages.keyAt(i)); |
| 1214 | pw.println(); |
| 1215 | |
| 1216 | for (int j = 0; j < mExemptedPackages.sizeAt(i); j++) { |
| 1217 | pw.print(indent); |
| 1218 | pw.print(" "); |
| 1219 | pw.print(mExemptedPackages.valueAt(i, j)); |
| 1220 | pw.println(); |
| 1221 | } |
| 1222 | } |
| 1223 | pw.println(); |
| 1224 | |
| 1225 | pw.print(indent); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1226 | pw.println("Restricted packages:"); |
| 1227 | for (Pair<Integer, String> uidAndPackage : mRunAnyRestrictedPackages) { |
| 1228 | pw.print(indent); |
| 1229 | pw.print(" "); |
| 1230 | pw.print(UserHandle.formatUid(uidAndPackage.first)); |
| 1231 | pw.print(" "); |
| 1232 | pw.print(uidAndPackage.second); |
| 1233 | pw.println(); |
| 1234 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 1235 | |
| 1236 | mStatLogger.dump(pw, indent); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 1240 | private void dumpUids(PrintWriter pw, SparseBooleanArray array) { |
| 1241 | pw.print("["); |
| 1242 | |
| 1243 | String sep = ""; |
| 1244 | for (int i = 0; i < array.size(); i++) { |
| 1245 | if (array.valueAt(i)) { |
| 1246 | pw.print(sep); |
| 1247 | pw.print(UserHandle.formatUid(array.keyAt(i))); |
| 1248 | sep = " "; |
| 1249 | } |
| 1250 | } |
| 1251 | pw.println("]"); |
| 1252 | } |
| 1253 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1254 | public void dumpProto(ProtoOutputStream proto, long fieldId) { |
| 1255 | synchronized (mLock) { |
| 1256 | final long token = proto.start(fieldId); |
| 1257 | |
| 1258 | proto.write(ForceAppStandbyTrackerProto.FORCE_ALL_APPS_STANDBY, mForceAllAppsStandby); |
Nancy Zheng | 525aaa1 | 2018-01-12 11:45:37 -0800 | [diff] [blame] | 1259 | proto.write(ForceAppStandbyTrackerProto.IS_SMALL_BATTERY_DEVICE, |
| 1260 | isSmallBatteryDevice()); |
| 1261 | proto.write(ForceAppStandbyTrackerProto.FORCE_ALL_APPS_STANDBY_FOR_SMALL_BATTERY, |
| 1262 | mForceAllAppStandbyForSmallBattery); |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 1263 | proto.write(ForceAppStandbyTrackerProto.IS_PLUGGED_IN, mIsPluggedIn); |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1264 | |
Makoto Onuki | adb50d8 | 2018-01-29 16:20:30 -0800 | [diff] [blame] | 1265 | for (int i = 0; i < mActiveUids.size(); i++) { |
| 1266 | if (mActiveUids.valueAt(i)) { |
| 1267 | proto.write(ForceAppStandbyTrackerProto.ACTIVE_UIDS, |
| 1268 | mActiveUids.keyAt(i)); |
| 1269 | } |
| 1270 | } |
| 1271 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1272 | for (int i = 0; i < mForegroundUids.size(); i++) { |
| 1273 | if (mForegroundUids.valueAt(i)) { |
| 1274 | proto.write(ForceAppStandbyTrackerProto.FOREGROUND_UIDS, |
| 1275 | mForegroundUids.keyAt(i)); |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | for (int appId : mPowerWhitelistedAllAppIds) { |
| 1280 | proto.write(ForceAppStandbyTrackerProto.POWER_SAVE_WHITELIST_APP_IDS, appId); |
| 1281 | } |
| 1282 | |
| 1283 | for (int appId : mTempWhitelistedAppIds) { |
| 1284 | proto.write(ForceAppStandbyTrackerProto.TEMP_POWER_SAVE_WHITELIST_APP_IDS, appId); |
| 1285 | } |
| 1286 | |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 1287 | for (int i = 0; i < mExemptedPackages.size(); i++) { |
| 1288 | for (int j = 0; j < mExemptedPackages.sizeAt(i); j++) { |
| 1289 | final long token2 = proto.start( |
| 1290 | ForceAppStandbyTrackerProto.EXEMPTED_PACKAGES); |
| 1291 | |
| 1292 | proto.write(ExemptedPackage.USER_ID, mExemptedPackages.keyAt(i)); |
| 1293 | proto.write(ExemptedPackage.PACKAGE_NAME, mExemptedPackages.valueAt(i, j)); |
| 1294 | |
| 1295 | proto.end(token2); |
| 1296 | } |
| 1297 | } |
| 1298 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1299 | for (Pair<Integer, String> uidAndPackage : mRunAnyRestrictedPackages) { |
| 1300 | final long token2 = proto.start( |
| 1301 | ForceAppStandbyTrackerProto.RUN_ANY_IN_BACKGROUND_RESTRICTED_PACKAGES); |
| 1302 | proto.write(RunAnyInBackgroundRestrictedPackages.UID, uidAndPackage.first); |
| 1303 | proto.write(RunAnyInBackgroundRestrictedPackages.PACKAGE_NAME, |
| 1304 | uidAndPackage.second); |
| 1305 | proto.end(token2); |
| 1306 | } |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 1307 | |
| 1308 | mStatLogger.dumpProto(proto, ForceAppStandbyTrackerProto.STATS); |
| 1309 | |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 1310 | proto.end(token); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | } |