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 static android.app.usage.UsageStatsManager.REASON_MAIN_DEFAULT; 19 import static android.app.usage.UsageStatsManager.REASON_MAIN_USAGE; 20 21 import static com.android.server.AppStateTrackerImpl.TARGET_OP; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assert.assertNotNull; 26 import static org.junit.Assert.assertTrue; 27 import static org.mockito.ArgumentMatchers.any; 28 import static org.mockito.ArgumentMatchers.anyBoolean; 29 import static org.mockito.ArgumentMatchers.anyInt; 30 import static org.mockito.ArgumentMatchers.anyString; 31 import static org.mockito.ArgumentMatchers.eq; 32 import static org.mockito.ArgumentMatchers.isNull; 33 import static org.mockito.Mockito.mock; 34 import static org.mockito.Mockito.reset; 35 import static org.mockito.Mockito.times; 36 import static org.mockito.Mockito.verify; 37 import static org.mockito.Mockito.when; 38 39 import android.app.ActivityManager; 40 import android.app.ActivityManagerInternal; 41 import android.app.AppOpsManager; 42 import android.app.AppOpsManager.OpEntry; 43 import android.app.AppOpsManager.PackageOps; 44 import android.app.IActivityManager; 45 import android.app.IUidObserver; 46 import android.app.usage.UsageStatsManager; 47 import android.companion.virtual.VirtualDeviceManager; 48 import android.content.BroadcastReceiver; 49 import android.content.Context; 50 import android.content.Intent; 51 import android.content.IntentFilter; 52 import android.net.Uri; 53 import android.os.BatteryManager; 54 import android.os.Handler; 55 import android.os.Looper; 56 import android.os.PowerManager.ServiceType; 57 import android.os.PowerManagerInternal; 58 import android.os.PowerSaveState; 59 import android.os.Process; 60 import android.os.RemoteException; 61 import android.os.UserHandle; 62 import android.platform.test.annotations.Presubmit; 63 import android.provider.Settings.Global; 64 import android.test.mock.MockContentResolver; 65 import android.util.ArraySet; 66 import android.util.Pair; 67 68 import androidx.test.filters.SmallTest; 69 import androidx.test.runner.AndroidJUnit4; 70 71 import com.android.internal.app.IAppOpsCallback; 72 import com.android.internal.app.IAppOpsService; 73 import com.android.server.AppStateTrackerImpl.Listener; 74 import com.android.server.usage.AppStandbyInternal; 75 import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener; 76 77 import org.junit.Before; 78 import org.junit.Test; 79 import org.junit.runner.RunWith; 80 import org.mockito.ArgumentCaptor; 81 import org.mockito.Mock; 82 import org.mockito.MockitoAnnotations; 83 import org.mockito.stubbing.Answer; 84 85 import java.util.ArrayList; 86 import java.util.Arrays; 87 import java.util.Collections; 88 import java.util.HashMap; 89 import java.util.List; 90 import java.util.Random; 91 import java.util.concurrent.CountDownLatch; 92 import java.util.concurrent.TimeUnit; 93 import java.util.function.Consumer; 94 95 /** 96 * Tests for {@link AppStateTrackerImpl} 97 * 98 * Run with: atest com.android.server.AppStateTrackerTest 99 */ 100 @Presubmit 101 @SmallTest 102 @RunWith(AndroidJUnit4.class) 103 public class AppStateTrackerTest { 104 105 private class AppStateTrackerTestable extends AppStateTrackerImpl { AppStateTrackerTestable()106 AppStateTrackerTestable() { 107 super(mMockContext, Looper.getMainLooper()); 108 } 109 110 @Override injectAppOpsManager()111 AppOpsManager injectAppOpsManager() { 112 return mMockAppOpsManager; 113 } 114 115 @Override injectIAppOpsService()116 IAppOpsService injectIAppOpsService() { 117 return mMockIAppOpsService; 118 } 119 120 @Override injectIActivityManager()121 IActivityManager injectIActivityManager() { 122 return mMockIActivityManager; 123 } 124 125 @Override injectActivityManagerInternal()126 ActivityManagerInternal injectActivityManagerInternal() { 127 return mMockIActivityManagerInternal; 128 } 129 130 @Override injectPowerManagerInternal()131 PowerManagerInternal injectPowerManagerInternal() { 132 return mMockPowerManagerInternal; 133 } 134 135 @Override injectAppStandbyInternal()136 AppStandbyInternal injectAppStandbyInternal() { 137 when(mMockAppStandbyInternal.isAppIdleEnabled()).thenReturn(true); 138 return mMockAppStandbyInternal; 139 } 140 141 @Override injectGetGlobalSettingInt(String key, int def)142 int injectGetGlobalSettingInt(String key, int def) { 143 Integer val = mGlobalSettings.get(key); 144 145 return (val == null) ? def : val; 146 } 147 148 @Override isSmallBatteryDevice()149 boolean isSmallBatteryDevice() { 150 return mIsSmallBatteryDevice; 151 } 152 } 153 154 private static final int UID_1 = Process.FIRST_APPLICATION_UID + 1; 155 private static final int UID_2 = Process.FIRST_APPLICATION_UID + 2; 156 private static final int UID_3 = Process.FIRST_APPLICATION_UID + 3; 157 private static final int UID_10_1 = UserHandle.getUid(10, UID_1); 158 private static final int UID_10_2 = UserHandle.getUid(10, UID_2); 159 private static final int UID_10_3 = UserHandle.getUid(10, UID_3); 160 private static final String PACKAGE_1 = "package1"; 161 private static final String PACKAGE_2 = "package2"; 162 private static final String PACKAGE_3 = "package3"; 163 private static final String PACKAGE_SYSTEM = "android"; 164 165 private Handler mMainHandler; 166 167 @Mock 168 private Context mMockContext; 169 170 @Mock 171 private IActivityManager mMockIActivityManager; 172 173 @Mock 174 private ActivityManagerInternal mMockIActivityManagerInternal; 175 176 @Mock 177 private AppOpsManager mMockAppOpsManager; 178 179 @Mock 180 private IAppOpsService mMockIAppOpsService; 181 182 @Mock 183 private PowerManagerInternal mMockPowerManagerInternal; 184 185 @Mock 186 private AppStandbyInternal mMockAppStandbyInternal; 187 188 private MockContentResolver mMockContentResolver; 189 190 private IUidObserver mIUidObserver; 191 private IAppOpsCallback.Stub mAppOpsCallback; 192 private Consumer<PowerSaveState> mPowerSaveObserver; 193 private BroadcastReceiver mReceiver; 194 private AppIdleStateChangeListener mAppIdleStateChangeListener; 195 196 private boolean mPowerSaveMode; 197 private boolean mIsSmallBatteryDevice; 198 199 private final ArraySet<Pair<Integer, String>> mRestrictedPackages = new ArraySet(); 200 201 private final HashMap<String, Integer> mGlobalSettings = new HashMap<>(); 202 203 private Answer<List<PackageOps>> mGetPackagesForOps = 204 inv -> new ArrayList<PackageOps>(); 205 206 @Before setUp()207 public void setUp() { 208 mMainHandler = new Handler(Looper.getMainLooper()); 209 } 210 211 /** 212 * Enqueues a message and waits for it to complete. This ensures that any messages posted until 213 * now have been executed. 214 * 215 * Note that these messages may have enqueued more messages, which may or may not have executed 216 * when this method returns. 217 */ waitUntilMainHandlerDrain()218 private void waitUntilMainHandlerDrain() throws Exception { 219 final CountDownLatch l = new CountDownLatch(1); 220 mMainHandler.post(() -> { 221 l.countDown(); 222 }); 223 assertTrue(l.await(5, TimeUnit.SECONDS)); 224 } 225 getPowerSaveState()226 private PowerSaveState getPowerSaveState() { 227 return new PowerSaveState.Builder().setBatterySaverEnabled(mPowerSaveMode).build(); 228 } 229 newInstance()230 private AppStateTrackerTestable newInstance() throws Exception { 231 MockitoAnnotations.initMocks(this); 232 233 when(mMockIAppOpsService.checkOperation(eq(TARGET_OP), anyInt(), anyString())) 234 .thenAnswer(inv -> { 235 return mRestrictedPackages.indexOf( 236 Pair.create(inv.getArgument(1), inv.getArgument(2))) >= 0 ? 237 AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED; 238 }); 239 240 final AppStateTrackerTestable instance = new AppStateTrackerTestable(); 241 242 return instance; 243 } 244 callStart(AppStateTrackerTestable instance)245 private void callStart(AppStateTrackerTestable instance) throws RemoteException { 246 247 // Set up functions that start() calls. 248 when(mMockPowerManagerInternal.getLowPowerState(eq(ServiceType.FORCE_ALL_APPS_STANDBY))) 249 .thenAnswer(inv -> getPowerSaveState()); 250 when(mMockAppOpsManager.getPackagesForOps( 251 any(int[].class) 252 )).thenAnswer(mGetPackagesForOps); 253 254 mMockContentResolver = new MockContentResolver(); 255 when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver); 256 257 // Call start. 258 instance.onSystemServicesReady(); 259 260 // Capture the listeners. 261 ArgumentCaptor<IUidObserver> uidObserverArgumentCaptor = 262 ArgumentCaptor.forClass(IUidObserver.class); 263 ArgumentCaptor<IAppOpsCallback.Stub> appOpsCallbackCaptor = 264 ArgumentCaptor.forClass(IAppOpsCallback.Stub.class); 265 ArgumentCaptor<Consumer<PowerSaveState>> powerSaveObserverCaptor = 266 ArgumentCaptor.forClass(Consumer.class); 267 ArgumentCaptor<BroadcastReceiver> receiverCaptor = 268 ArgumentCaptor.forClass(BroadcastReceiver.class); 269 ArgumentCaptor<AppIdleStateChangeListener> appIdleStateChangeListenerCaptor = 270 ArgumentCaptor.forClass(AppIdleStateChangeListener.class); 271 272 verify(mMockIActivityManager).registerUidObserver( 273 uidObserverArgumentCaptor.capture(), 274 eq(ActivityManager.UID_OBSERVER_GONE | ActivityManager.UID_OBSERVER_IDLE 275 | ActivityManager.UID_OBSERVER_ACTIVE 276 | ActivityManager.UID_OBSERVER_CACHED), 277 eq(ActivityManager.PROCESS_STATE_UNKNOWN), 278 isNull()); 279 verify(mMockIAppOpsService).startWatchingMode( 280 eq(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND), 281 isNull(), 282 appOpsCallbackCaptor.capture()); 283 verify(mMockPowerManagerInternal).registerLowPowerModeObserver( 284 eq(ServiceType.FORCE_ALL_APPS_STANDBY), 285 powerSaveObserverCaptor.capture()); 286 287 verify(mMockContext, times(2)).registerReceiver( 288 receiverCaptor.capture(), any(IntentFilter.class)); 289 verify(mMockAppStandbyInternal).addListener( 290 appIdleStateChangeListenerCaptor.capture()); 291 292 mIUidObserver = uidObserverArgumentCaptor.getValue(); 293 mAppOpsCallback = appOpsCallbackCaptor.getValue(); 294 mPowerSaveObserver = powerSaveObserverCaptor.getValue(); 295 mReceiver = receiverCaptor.getValue(); 296 mAppIdleStateChangeListener = appIdleStateChangeListenerCaptor.getValue(); 297 298 assertNotNull(mIUidObserver); 299 assertNotNull(mAppOpsCallback); 300 assertNotNull(mPowerSaveObserver); 301 assertNotNull(mReceiver); 302 assertNotNull(instance.mFlagsObserver); 303 } 304 setAppOps(int uid, String packageName, boolean restrict)305 private void setAppOps(int uid, String packageName, boolean restrict) throws RemoteException { 306 final Pair p = Pair.create(uid, packageName); 307 if (restrict) { 308 mRestrictedPackages.add(p); 309 } else { 310 mRestrictedPackages.remove(p); 311 } 312 if (mAppOpsCallback != null) { 313 mAppOpsCallback.opChanged(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, uid, packageName, 314 VirtualDeviceManager.PERSISTENT_DEVICE_ID_DEFAULT); 315 } 316 } 317 areJobsRestricted(AppStateTrackerTestable instance, int[] uids, String[] packages, boolean[] restricted, boolean exemption)318 private void areJobsRestricted(AppStateTrackerTestable instance, int[] uids, String[] packages, 319 boolean[] restricted, boolean exemption) { 320 assertTrue(uids.length == packages.length && uids.length == restricted.length); 321 for (int i = 0; i < uids.length; i++) { 322 assertEquals(restricted[i], 323 instance.areJobsRestricted(uids[i], packages[i], exemption)); 324 } 325 } 326 areAlarmsRestrictedByFAS(AppStateTrackerTestable instance, int[] uids, String[] packages, boolean[] restricted)327 private void areAlarmsRestrictedByFAS(AppStateTrackerTestable instance, int[] uids, 328 String[] packages, boolean[] restricted) { 329 assertTrue(uids.length == packages.length && uids.length == restricted.length); 330 for (int i = 0; i < uids.length; i++) { 331 assertEquals(restricted[i], instance.areAlarmsRestricted(uids[i], packages[i])); 332 } 333 } 334 areAlarmsRestrictedByBatterySaver(AppStateTrackerTestable instance, int[] uids, String[] packages, boolean[] restricted)335 private void areAlarmsRestrictedByBatterySaver(AppStateTrackerTestable instance, int[] uids, 336 String[] packages, boolean[] restricted) { 337 assertTrue(uids.length == packages.length && uids.length == restricted.length); 338 for (int i = 0; i < uids.length; i++) { 339 assertEquals(restricted[i], 340 instance.areAlarmsRestrictedByBatterySaver(uids[i], packages[i])); 341 } 342 } 343 344 @Test testAll()345 public void testAll() throws Exception { 346 final AppStateTrackerTestable instance = newInstance(); 347 callStart(instance); 348 349 assertFalse(instance.isForceAllAppsStandbyEnabled()); 350 351 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 352 .thenReturn(false); 353 354 areJobsRestricted(instance, 355 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 356 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 357 new boolean[] {false, false, false, false}, 358 false); 359 areJobsRestricted(instance, 360 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 361 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 362 new boolean[] {false, false, false, false}, 363 true); 364 areAlarmsRestrictedByBatterySaver(instance, 365 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 366 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 367 new boolean[] {false, false, false, false}); 368 369 // Toggle the auto restricted bucket feature flag on bg restriction, shouldn't make a 370 // difference. 371 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 372 .thenReturn(true); 373 374 areJobsRestricted(instance, 375 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 376 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 377 new boolean[] {false, false, false, false}, 378 false); 379 areJobsRestricted(instance, 380 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 381 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 382 new boolean[] {false, false, false, false}, 383 true); 384 areAlarmsRestrictedByBatterySaver(instance, 385 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 386 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 387 new boolean[] {false, false, false, false}); 388 389 mPowerSaveMode = true; 390 mPowerSaveObserver.accept(getPowerSaveState()); 391 392 assertTrue(instance.isForceAllAppsStandbyEnabled()); 393 394 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 395 .thenReturn(false); 396 397 areJobsRestricted(instance, 398 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 399 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 400 new boolean[] {true, true, true, false}, 401 false); 402 areJobsRestricted(instance, 403 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 404 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 405 new boolean[] {false, false, false, false}, 406 true); 407 areAlarmsRestrictedByBatterySaver(instance, 408 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 409 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 410 new boolean[] {true, true, true, false}); 411 412 // Toggle the auto restricted bucket feature flag on bg restriction, shouldn't make a 413 // difference. 414 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 415 .thenReturn(true); 416 417 areJobsRestricted(instance, 418 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 419 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 420 new boolean[] {true, true, true, false}, 421 false); 422 areJobsRestricted(instance, 423 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 424 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 425 new boolean[] {false, false, false, false}, 426 true); 427 areAlarmsRestrictedByBatterySaver(instance, 428 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 429 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 430 new boolean[] {true, true, true, false}); 431 432 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 433 .thenReturn(false); 434 435 // Toggle the foreground state. 436 437 assertFalse(instance.isUidActive(UID_1)); 438 assertFalse(instance.isUidActive(UID_2)); 439 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 440 441 mIUidObserver.onUidActive(UID_1); 442 waitUntilMainHandlerDrain(); 443 waitUntilMainHandlerDrain(); 444 445 areJobsRestricted(instance, 446 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 447 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 448 new boolean[] {false, true, true, false}, 449 false); 450 areAlarmsRestrictedByBatterySaver(instance, 451 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 452 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 453 new boolean[] {false, true, true, false}); 454 455 assertTrue(instance.isUidActive(UID_1)); 456 assertFalse(instance.isUidActive(UID_2)); 457 458 mIUidObserver.onUidGone(UID_1, /*disable=*/ false); 459 waitUntilMainHandlerDrain(); 460 waitUntilMainHandlerDrain(); 461 462 areJobsRestricted(instance, 463 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 464 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 465 new boolean[] {true, true, true, false}, 466 false); 467 areAlarmsRestrictedByBatterySaver(instance, 468 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 469 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 470 new boolean[] {true, true, true, false}); 471 472 assertFalse(instance.isUidActive(UID_1)); 473 assertFalse(instance.isUidActive(UID_2)); 474 475 mIUidObserver.onUidActive(UID_1); 476 waitUntilMainHandlerDrain(); 477 waitUntilMainHandlerDrain(); 478 479 areJobsRestricted(instance, 480 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 481 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 482 new boolean[] {false, true, true, false}, 483 false); 484 areAlarmsRestrictedByBatterySaver(instance, 485 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 486 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 487 new boolean[] {false, true, true, false}); 488 489 mIUidObserver.onUidIdle(UID_1, /*disable=*/ false); 490 waitUntilMainHandlerDrain(); 491 waitUntilMainHandlerDrain(); 492 493 areJobsRestricted(instance, 494 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 495 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 496 new boolean[] {true, true, true, false}, 497 false); 498 areAlarmsRestrictedByBatterySaver(instance, 499 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 500 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 501 new boolean[] {true, true, true, false}); 502 503 assertFalse(instance.isUidActive(UID_1)); 504 assertFalse(instance.isUidActive(UID_2)); 505 506 // Toggle the app ops. 507 mPowerSaveMode = false; 508 mPowerSaveObserver.accept(getPowerSaveState()); 509 510 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_1, PACKAGE_1)); 511 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_1, PACKAGE_1)); 512 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 513 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 514 515 areJobsRestricted(instance, 516 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 517 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 518 new boolean[] {false, false, false, false, false}, 519 false); 520 areAlarmsRestrictedByBatterySaver(instance, 521 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 522 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 523 new boolean[] {false, false, false, false, false}); 524 areAlarmsRestrictedByFAS(instance, 525 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 526 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 527 new boolean[] {false, false, false, false, false}); 528 529 setAppOps(UID_1, PACKAGE_1, true); 530 setAppOps(UID_10_2, PACKAGE_2, true); 531 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_1, PACKAGE_1)); 532 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_1, PACKAGE_1)); 533 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 534 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 535 536 areJobsRestricted(instance, 537 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 538 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 539 new boolean[] {true, false, false, true, false}, 540 false); 541 areJobsRestricted(instance, 542 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 543 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 544 new boolean[] {true, false, false, true, false}, 545 true); 546 547 areAlarmsRestrictedByBatterySaver(instance, 548 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 549 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 550 new boolean[] {false, false, false, false, false}); 551 areAlarmsRestrictedByFAS(instance, 552 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 553 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 554 new boolean[] {true, false, false, true, false}); 555 556 // Toggle the auto restricted bucket feature flag on bg restriction. 557 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 558 .thenReturn(true); 559 560 areJobsRestricted(instance, 561 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 562 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 563 new boolean[] {false, false, false, false, false}, 564 false); 565 areJobsRestricted(instance, 566 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 567 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 568 new boolean[] {false, false, false, false, false}, 569 true); 570 571 areAlarmsRestrictedByBatterySaver(instance, 572 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 573 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 574 new boolean[] {false, false, false, false, false}); 575 areAlarmsRestrictedByFAS(instance, 576 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 577 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 578 new boolean[] {false, false, false, false, false}); 579 580 // Toggle power saver, should still be the same. 581 mPowerSaveMode = true; 582 mPowerSaveObserver.accept(getPowerSaveState()); 583 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 584 .thenReturn(false); 585 586 areJobsRestricted(instance, 587 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 588 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 589 new boolean[] {true, true, true, true, false}, 590 false); 591 areJobsRestricted(instance, 592 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 593 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 594 new boolean[] {true, false, false, true, false}, 595 true); 596 597 areAlarmsRestrictedByBatterySaver(instance, 598 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 599 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 600 new boolean[] {true, true, true, true, false}); 601 areAlarmsRestrictedByFAS(instance, 602 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 603 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 604 new boolean[] {true, false, false, true, false}); 605 606 // Toggle the auto restricted bucket feature flag on bg restriction. 607 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 608 .thenReturn(true); 609 610 areJobsRestricted(instance, 611 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 612 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 613 new boolean[] {true, true, true, true, false}, 614 false); 615 areJobsRestricted(instance, 616 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 617 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 618 new boolean[] {false, false, false, false, false}, 619 true); 620 621 areAlarmsRestrictedByBatterySaver(instance, 622 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 623 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 624 new boolean[] {true, true, true, true, false}); 625 areAlarmsRestrictedByFAS(instance, 626 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 627 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 628 new boolean[] {false, false, false, false, false}); 629 630 mPowerSaveMode = false; 631 mPowerSaveObserver.accept(getPowerSaveState()); 632 633 when(mMockIActivityManagerInternal.isBgAutoRestrictedBucketFeatureFlagEnabled()) 634 .thenReturn(false); 635 636 areJobsRestricted(instance, 637 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 638 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 639 new boolean[] {true, false, false, true, false}, 640 false); 641 areJobsRestricted(instance, 642 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 643 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 644 new boolean[] {true, false, false, true, false}, 645 true); 646 647 areAlarmsRestrictedByBatterySaver(instance, 648 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 649 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 650 new boolean[] {false, false, false, false, false}); 651 areAlarmsRestrictedByFAS(instance, 652 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 653 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 654 new boolean[] {true, false, false, true, false}); 655 656 // Clear the app ops and update the exemption list. 657 setAppOps(UID_1, PACKAGE_1, false); 658 setAppOps(UID_10_2, PACKAGE_2, false); 659 660 mPowerSaveMode = true; 661 mPowerSaveObserver.accept(getPowerSaveState()); 662 663 areJobsRestricted(instance, 664 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 665 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 666 new boolean[] {true, true, true, true, false}, 667 false); 668 areJobsRestricted(instance, 669 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 670 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 671 new boolean[] {false, false, false, false, false}, 672 true); 673 674 areAlarmsRestrictedByBatterySaver(instance, 675 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 676 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 677 new boolean[] {true, true, true, true, false}); 678 areAlarmsRestrictedByFAS(instance, 679 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 680 new String[] {PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 681 new boolean[] {false, false, false, false, false}); 682 683 instance.setPowerSaveExemptionListAppIds(new int[] {UID_1}, new int[] {}, 684 new int[] {UID_2}); 685 686 areJobsRestricted(instance, 687 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, UID_3, UID_10_3, Process.SYSTEM_UID}, 688 new String[]{PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_3, PACKAGE_3, 689 PACKAGE_SYSTEM}, 690 new boolean[] {false, false, false, false, true, true, false}, 691 false); 692 693 areAlarmsRestrictedByBatterySaver(instance, 694 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, UID_3, UID_10_3, Process.SYSTEM_UID}, 695 new String[]{PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_3, PACKAGE_3, 696 PACKAGE_SYSTEM}, 697 new boolean[] {false, false, true, true, true, true, false}); 698 699 // Again, make sure toggling the global state doesn't change it. 700 mPowerSaveMode = false; 701 mPowerSaveObserver.accept(getPowerSaveState()); 702 703 mPowerSaveMode = true; 704 mPowerSaveObserver.accept(getPowerSaveState()); 705 706 areJobsRestricted(instance, 707 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, UID_3, UID_10_3, Process.SYSTEM_UID}, 708 new String[]{PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_3, PACKAGE_3, 709 PACKAGE_SYSTEM}, 710 new boolean[] {false, false, false, false, true, true, false}, 711 false); 712 713 areAlarmsRestrictedByBatterySaver(instance, 714 new int[] {UID_1, UID_10_1, UID_2, UID_10_2, UID_3, UID_10_3, Process.SYSTEM_UID}, 715 new String[]{PACKAGE_1, PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_3, PACKAGE_3, 716 PACKAGE_SYSTEM}, 717 new boolean[] {false, false, true, true, true, true, false}); 718 719 assertTrue(instance.isUidPowerSaveExempt(UID_1)); 720 assertTrue(instance.isUidPowerSaveExempt(UID_10_1)); 721 assertFalse(instance.isUidPowerSaveExempt(UID_2)); 722 assertFalse(instance.isUidPowerSaveExempt(UID_10_2)); 723 724 assertFalse(instance.isUidTempPowerSaveExempt(UID_1)); 725 assertFalse(instance.isUidTempPowerSaveExempt(UID_10_1)); 726 assertTrue(instance.isUidTempPowerSaveExempt(UID_2)); 727 assertTrue(instance.isUidTempPowerSaveExempt(UID_10_2)); 728 } 729 730 @Test testPowerSaveUserExemptionList()731 public void testPowerSaveUserExemptionList() throws Exception { 732 final AppStateTrackerTestable instance = newInstance(); 733 instance.setPowerSaveExemptionListAppIds(new int[] {}, new int[] {UID_1, UID_2}, 734 new int[] {}); 735 assertTrue(instance.isUidPowerSaveUserExempt(UID_1)); 736 assertTrue(instance.isUidPowerSaveUserExempt(UID_2)); 737 assertFalse(instance.isUidPowerSaveUserExempt(UID_3)); 738 } 739 740 @Test testUidStateForeground()741 public void testUidStateForeground() throws Exception { 742 final AppStateTrackerTestable instance = newInstance(); 743 callStart(instance); 744 745 mIUidObserver.onUidActive(UID_1); 746 747 waitUntilMainHandlerDrain(); 748 waitUntilMainHandlerDrain(); 749 750 assertTrue(instance.isUidActive(UID_1)); 751 assertFalse(instance.isUidActive(UID_2)); 752 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 753 754 assertTrue(instance.isUidActiveSynced(UID_1)); 755 assertFalse(instance.isUidActiveSynced(UID_2)); 756 assertTrue(instance.isUidActiveSynced(Process.SYSTEM_UID)); 757 758 mIUidObserver.onUidStateChanged(UID_2, 759 ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE, 0, 760 ActivityManager.PROCESS_CAPABILITY_NONE); 761 762 waitUntilMainHandlerDrain(); 763 waitUntilMainHandlerDrain(); 764 765 assertTrue(instance.isUidActive(UID_1)); 766 assertFalse(instance.isUidActive(UID_2)); 767 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 768 769 assertTrue(instance.isUidActiveSynced(UID_1)); 770 assertFalse(instance.isUidActiveSynced(UID_2)); 771 assertTrue(instance.isUidActiveSynced(Process.SYSTEM_UID)); 772 773 mIUidObserver.onUidStateChanged(UID_1, 774 ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0, 775 ActivityManager.PROCESS_CAPABILITY_NONE); 776 777 waitUntilMainHandlerDrain(); 778 waitUntilMainHandlerDrain(); 779 780 assertTrue(instance.isUidActive(UID_1)); 781 assertFalse(instance.isUidActive(UID_2)); 782 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 783 784 mIUidObserver.onUidGone(UID_1, true); 785 786 waitUntilMainHandlerDrain(); 787 waitUntilMainHandlerDrain(); 788 789 assertFalse(instance.isUidActive(UID_1)); 790 assertFalse(instance.isUidActive(UID_2)); 791 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 792 793 mIUidObserver.onUidIdle(UID_2, true); 794 795 waitUntilMainHandlerDrain(); 796 waitUntilMainHandlerDrain(); 797 798 assertFalse(instance.isUidActive(UID_1)); 799 assertFalse(instance.isUidActive(UID_2)); 800 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 801 802 mIUidObserver.onUidStateChanged(UID_1, 803 ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND, 0, 804 ActivityManager.PROCESS_CAPABILITY_NONE); 805 806 waitUntilMainHandlerDrain(); 807 waitUntilMainHandlerDrain(); 808 809 assertFalse(instance.isUidActive(UID_1)); 810 assertFalse(instance.isUidActive(UID_2)); 811 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 812 813 mIUidObserver.onUidStateChanged(UID_1, 814 ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND, 0, 815 ActivityManager.PROCESS_CAPABILITY_NONE); 816 817 waitUntilMainHandlerDrain(); 818 waitUntilMainHandlerDrain(); 819 820 assertFalse(instance.isUidActive(UID_1)); 821 assertFalse(instance.isUidActive(UID_2)); 822 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 823 824 assertFalse(instance.isUidActiveSynced(UID_1)); 825 assertFalse(instance.isUidActiveSynced(UID_2)); 826 assertTrue(instance.isUidActiveSynced(Process.SYSTEM_UID)); 827 828 // The result from AMI.isUidActive() only affects isUidActiveSynced(). 829 when(mMockIActivityManagerInternal.isUidActive(anyInt())).thenReturn(true); 830 831 assertFalse(instance.isUidActive(UID_1)); 832 assertFalse(instance.isUidActive(UID_2)); 833 assertTrue(instance.isUidActive(Process.SYSTEM_UID)); 834 835 assertTrue(instance.isUidActiveSynced(UID_1)); 836 assertTrue(instance.isUidActiveSynced(UID_2)); 837 assertTrue(instance.isUidActiveSynced(Process.SYSTEM_UID)); 838 } 839 840 @Test testExemptedBucket()841 public void testExemptedBucket() throws Exception { 842 final AppStateTrackerTestable instance = newInstance(); 843 callStart(instance); 844 845 assertFalse(instance.isForceAllAppsStandbyEnabled()); 846 847 areJobsRestricted(instance, 848 new int[] {UID_1, UID_2, Process.SYSTEM_UID}, 849 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_SYSTEM}, 850 new boolean[] {false, false, false}, 851 false); 852 areAlarmsRestrictedByBatterySaver(instance, 853 new int[] {UID_1, UID_2, Process.SYSTEM_UID}, 854 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_SYSTEM}, 855 new boolean[] {false, false, false}); 856 857 mPowerSaveMode = true; 858 mPowerSaveObserver.accept(getPowerSaveState()); 859 860 assertTrue(instance.isForceAllAppsStandbyEnabled()); 861 862 areJobsRestricted(instance, 863 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 864 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 865 new boolean[] {true, true, true, false}, 866 false); 867 areJobsRestricted(instance, 868 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 869 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 870 new boolean[] {false, false, false, false}, 871 true); 872 areAlarmsRestrictedByBatterySaver(instance, 873 new int[] {UID_1, UID_2, UID_10_2, Process.SYSTEM_UID}, 874 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2, PACKAGE_SYSTEM}, 875 new boolean[] {true, true, true, false}); 876 877 // Exempt package 2 on user-10. 878 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_2, /*user=*/ 10, false, 879 UsageStatsManager.STANDBY_BUCKET_EXEMPTED, REASON_MAIN_DEFAULT); 880 881 areJobsRestricted(instance, 882 new int[] {UID_1, UID_2, UID_10_2}, 883 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 884 new boolean[] {true, true, false}, 885 false); 886 areJobsRestricted(instance, 887 new int[] {UID_1, UID_2, UID_10_2}, 888 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 889 new boolean[] {false, false, false}, 890 true); 891 areAlarmsRestrictedByBatterySaver(instance, 892 new int[] {UID_1, UID_2, UID_10_2}, 893 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 894 new boolean[] {true, true, false}); 895 896 // Exempt package 1 on user-0. 897 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_1, /*user=*/ 0, false, 898 UsageStatsManager.STANDBY_BUCKET_EXEMPTED, REASON_MAIN_DEFAULT); 899 900 areJobsRestricted(instance, 901 new int[] {UID_1, UID_2, UID_10_2}, 902 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 903 new boolean[] {false, true, false}, 904 false); 905 areJobsRestricted(instance, 906 new int[] {UID_1, UID_2, UID_10_2}, 907 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 908 new boolean[] {false, false, false}, 909 true); 910 areAlarmsRestrictedByBatterySaver(instance, 911 new int[] {UID_1, UID_2, UID_10_2}, 912 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 913 new boolean[] {false, true, false}); 914 915 // Unexempt package 2 on user-10. 916 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_2, /*user=*/ 10, false, 917 UsageStatsManager.STANDBY_BUCKET_ACTIVE, REASON_MAIN_USAGE); 918 919 areJobsRestricted(instance, 920 new int[] {UID_1, UID_2, UID_10_2}, 921 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 922 new boolean[] {false, true, true}, 923 false); 924 areJobsRestricted(instance, 925 new int[] {UID_1, UID_2, UID_10_2}, 926 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 927 new boolean[] {false, false, false}, 928 true); 929 areAlarmsRestrictedByBatterySaver(instance, 930 new int[] {UID_1, UID_2, UID_10_2}, 931 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 932 new boolean[] {false, true, true}); 933 934 // Check force-app-standby. 935 // EXEMPT doesn't exempt from force-app-standby. 936 mPowerSaveMode = false; 937 mPowerSaveObserver.accept(getPowerSaveState()); 938 939 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_1, /*user=*/ 0, false, 940 UsageStatsManager.STANDBY_BUCKET_EXEMPTED, REASON_MAIN_DEFAULT); 941 mAppIdleStateChangeListener.onAppIdleStateChanged(PACKAGE_2, /*user=*/ 0, false, 942 UsageStatsManager.STANDBY_BUCKET_EXEMPTED, REASON_MAIN_DEFAULT); 943 944 // All 3 packages (u0:p1, u0:p2, u10:p2) are now in the exempted bucket. 945 setAppOps(UID_1, PACKAGE_1, true); 946 947 areJobsRestricted(instance, 948 new int[] {UID_1, UID_2, UID_10_2}, 949 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 950 new boolean[] {true, false, false}, 951 false); 952 areJobsRestricted(instance, 953 new int[] {UID_1, UID_2, UID_10_2}, 954 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 955 new boolean[] {true, false, false}, 956 true); 957 958 areAlarmsRestrictedByBatterySaver(instance, 959 new int[] {UID_1, UID_2, UID_10_2}, 960 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 961 new boolean[] {false, false, false}); 962 areAlarmsRestrictedByFAS(instance, 963 new int[] {UID_1, UID_2, UID_10_2}, 964 new String[] {PACKAGE_1, PACKAGE_2, PACKAGE_2}, 965 new boolean[] {true, false, false}); 966 } 967 968 @Test loadPersistedAppOps()969 public void loadPersistedAppOps() throws Exception { 970 final AppStateTrackerTestable instance = newInstance(); 971 972 final List<PackageOps> ops = new ArrayList<>(); 973 974 //-------------------------------------------------- 975 List<OpEntry> entries = new ArrayList<>(); 976 entries.add(new OpEntry( 977 AppOpsManager.OP_ACCESS_NOTIFICATIONS, 978 AppOpsManager.MODE_IGNORED, 979 Collections.emptyMap())); 980 entries.add(new OpEntry( 981 AppStateTrackerImpl.TARGET_OP, 982 AppOpsManager.MODE_IGNORED, 983 Collections.emptyMap())); 984 985 ops.add(new PackageOps(PACKAGE_1, UID_1, entries)); 986 987 //-------------------------------------------------- 988 entries = new ArrayList<>(); 989 entries.add(new OpEntry( 990 AppStateTrackerImpl.TARGET_OP, 991 AppOpsManager.MODE_IGNORED, 992 Collections.emptyMap())); 993 994 ops.add(new PackageOps(PACKAGE_2, UID_2, entries)); 995 996 //-------------------------------------------------- 997 entries = new ArrayList<>(); 998 entries.add(new OpEntry( 999 AppStateTrackerImpl.TARGET_OP, 1000 AppOpsManager.MODE_ALLOWED, 1001 Collections.emptyMap())); 1002 1003 ops.add(new PackageOps(PACKAGE_1, UID_10_1, entries)); 1004 1005 //-------------------------------------------------- 1006 entries = new ArrayList<>(); 1007 entries.add(new OpEntry( 1008 AppStateTrackerImpl.TARGET_OP, 1009 AppOpsManager.MODE_IGNORED, 1010 Collections.emptyMap())); 1011 entries.add(new OpEntry( 1012 AppOpsManager.OP_ACCESS_NOTIFICATIONS, 1013 AppOpsManager.MODE_IGNORED, 1014 Collections.emptyMap())); 1015 1016 ops.add(new PackageOps(PACKAGE_3, UID_10_3, entries)); 1017 1018 mGetPackagesForOps = inv -> { 1019 final int[] arg = (int[]) inv.getArgument(0); 1020 assertEquals(1, arg.length); 1021 assertEquals(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, arg[0]); 1022 return ops; 1023 }; 1024 1025 callStart(instance); 1026 1027 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_1, PACKAGE_1)); 1028 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 1029 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_3, PACKAGE_3)); 1030 1031 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_1, PACKAGE_1)); 1032 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 1033 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_3, PACKAGE_3)); 1034 } 1035 assertNoCallbacks(Listener l)1036 private void assertNoCallbacks(Listener l) throws Exception { 1037 waitUntilMainHandlerDrain(); 1038 verify(l, times(0)).updateAllJobs(); 1039 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1040 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1041 1042 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1043 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1044 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1045 reset(l); 1046 } 1047 1048 @Test testPowerSaveListener()1049 public void testPowerSaveListener() throws Exception { 1050 final AppStateTrackerTestable instance = newInstance(); 1051 callStart(instance); 1052 1053 Listener l = mock(Listener.class); 1054 instance.addListener(l); 1055 1056 // Power save on. 1057 mPowerSaveMode = true; 1058 mPowerSaveObserver.accept(getPowerSaveState()); 1059 1060 waitUntilMainHandlerDrain(); 1061 verify(l, times(1)).updateAllJobs(); 1062 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1063 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1064 1065 verify(l, times(1)).updateAllAlarms(); 1066 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1067 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1068 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1069 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1070 reset(l); 1071 1072 // Power save off. 1073 mPowerSaveMode = false; 1074 mPowerSaveObserver.accept(getPowerSaveState()); 1075 1076 waitUntilMainHandlerDrain(); 1077 verify(l, times(1)).updateAllJobs(); 1078 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1079 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1080 1081 verify(l, times(1)).updateAllAlarms(); 1082 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1083 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1084 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1085 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1086 reset(l); 1087 1088 // Updating to the same state should not fire listener 1089 mPowerSaveMode = false; 1090 mPowerSaveObserver.accept(getPowerSaveState()); 1091 1092 assertNoCallbacks(l); 1093 } 1094 1095 @Test testAllListeners()1096 public void testAllListeners() throws Exception { 1097 final AppStateTrackerTestable instance = newInstance(); 1098 callStart(instance); 1099 1100 Listener l = mock(Listener.class); 1101 instance.addListener(l); 1102 1103 // ------------------------------------------------------------------------- 1104 // Test with apppops. 1105 1106 setAppOps(UID_10_2, PACKAGE_2, true); 1107 1108 waitUntilMainHandlerDrain(); 1109 verify(l, times(0)).updateAllJobs(); 1110 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1111 verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean()); 1112 verify(l, times(1)).updateBackgroundRestrictedForUidPackage(eq(UID_10_2), eq(PACKAGE_2), 1113 eq(true)); 1114 1115 verify(l, times(0)).updateAllAlarms(); 1116 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1117 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1118 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1119 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1120 reset(l); 1121 1122 setAppOps(UID_10_2, PACKAGE_2, false); 1123 1124 waitUntilMainHandlerDrain(); 1125 verify(l, times(0)).updateAllJobs(); 1126 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1127 verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean()); 1128 verify(l, times(1)).updateBackgroundRestrictedForUidPackage(eq(UID_10_2), eq(PACKAGE_2), 1129 eq(false)); 1130 1131 verify(l, times(0)).updateAllAlarms(); 1132 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1133 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1134 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1135 verify(l, times(1)).unblockAlarmsForUidPackage(eq(UID_10_2), eq(PACKAGE_2)); 1136 reset(l); 1137 1138 setAppOps(UID_10_2, PACKAGE_2, false); 1139 1140 verify(l, times(0)).updateAllJobs(); 1141 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1142 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1143 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1144 anyBoolean()); 1145 1146 verify(l, times(0)).updateAllAlarms(); 1147 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1148 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1149 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1150 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1151 1152 // Test overlap with battery saver 1153 mPowerSaveMode = true; 1154 mPowerSaveObserver.accept(getPowerSaveState()); 1155 1156 setAppOps(UID_10_2, PACKAGE_2, true); 1157 1158 waitUntilMainHandlerDrain(); 1159 verify(l, times(1)).updateAllJobs(); 1160 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1161 verify(l, times(1)).updateJobsForUidPackage(eq(UID_10_2), eq(PACKAGE_2), anyBoolean()); 1162 verify(l, times(1)).updateBackgroundRestrictedForUidPackage(eq(UID_10_2), eq(PACKAGE_2), 1163 eq(true)); 1164 1165 verify(l, times(1)).updateAllAlarms(); 1166 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1167 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1168 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1169 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1170 reset(l); 1171 1172 // Battery saver off. 1173 mPowerSaveMode = false; 1174 mPowerSaveObserver.accept(getPowerSaveState()); 1175 1176 waitUntilMainHandlerDrain(); 1177 verify(l, times(1)).updateAllJobs(); 1178 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1179 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1180 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1181 anyBoolean()); 1182 1183 verify(l, times(1)).updateAllAlarms(); 1184 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1185 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1186 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1187 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1188 reset(l); 1189 1190 // ------------------------------------------------------------------------- 1191 // Tests with system/user/temp exemption list. 1192 1193 instance.setPowerSaveExemptionListAppIds(new int[] {UID_1, UID_2}, new int[] {}, 1194 new int[] {}); 1195 1196 waitUntilMainHandlerDrain(); 1197 verify(l, times(1)).updateAllJobs(); 1198 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1199 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1200 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1201 anyBoolean()); 1202 1203 verify(l, times(1)).updateAllAlarms(); 1204 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1205 verify(l, times(1)).unblockAllUnrestrictedAlarms(); 1206 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1207 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1208 reset(l); 1209 1210 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, new int[] {}); 1211 1212 waitUntilMainHandlerDrain(); 1213 verify(l, times(1)).updateAllJobs(); 1214 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1215 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1216 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1217 anyBoolean()); 1218 1219 verify(l, times(1)).updateAllAlarms(); 1220 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1221 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1222 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1223 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1224 reset(l); 1225 1226 // Update temp exemption list. 1227 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, 1228 new int[] {UID_1, UID_3}); 1229 1230 waitUntilMainHandlerDrain(); 1231 verify(l, times(1)).updateAllJobs(); 1232 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1233 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1234 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1235 anyBoolean()); 1236 1237 verify(l, times(0)).updateAllAlarms(); 1238 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1239 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1240 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1241 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1242 reset(l); 1243 1244 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, 1245 new int[] {UID_3}); 1246 1247 waitUntilMainHandlerDrain(); 1248 verify(l, times(1)).updateAllJobs(); 1249 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1250 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1251 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1252 anyBoolean()); 1253 1254 verify(l, times(0)).updateAllAlarms(); 1255 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1256 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1257 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1258 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1259 reset(l); 1260 1261 // Do the same thing with battery saver on. 1262 mPowerSaveMode = true; 1263 mPowerSaveObserver.accept(getPowerSaveState()); 1264 1265 waitUntilMainHandlerDrain(); 1266 verify(l, times(1)).updateAllJobs(); 1267 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1268 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1269 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1270 anyBoolean()); 1271 1272 verify(l, times(1)).updateAllAlarms(); 1273 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1274 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1275 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1276 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1277 reset(l); 1278 1279 instance.setPowerSaveExemptionListAppIds(new int[] {UID_1, UID_2}, new int[] {}, 1280 new int[] {}); 1281 1282 waitUntilMainHandlerDrain(); 1283 // Called once for updating all exemption list and once for updating temp exemption list 1284 verify(l, times(2)).updateAllJobs(); 1285 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1286 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1287 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1288 anyBoolean()); 1289 1290 verify(l, times(1)).updateAllAlarms(); 1291 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1292 verify(l, times(1)).unblockAllUnrestrictedAlarms(); 1293 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1294 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1295 reset(l); 1296 1297 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, new int[] {}); 1298 1299 waitUntilMainHandlerDrain(); 1300 verify(l, times(1)).updateAllJobs(); 1301 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1302 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1303 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1304 anyBoolean()); 1305 1306 verify(l, times(1)).updateAllAlarms(); 1307 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1308 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1309 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1310 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1311 reset(l); 1312 1313 // Update temp exemption list. 1314 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, 1315 new int[] {UID_1, UID_3}); 1316 1317 waitUntilMainHandlerDrain(); 1318 verify(l, times(1)).updateAllJobs(); 1319 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1320 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1321 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1322 anyBoolean()); 1323 1324 verify(l, times(0)).updateAllAlarms(); 1325 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1326 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1327 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1328 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1329 reset(l); 1330 1331 instance.setPowerSaveExemptionListAppIds(new int[] {UID_2}, new int[] {}, 1332 new int[] {UID_3}); 1333 1334 waitUntilMainHandlerDrain(); 1335 verify(l, times(1)).updateAllJobs(); 1336 verify(l, times(0)).updateJobsForUid(anyInt(), anyBoolean()); 1337 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1338 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1339 anyBoolean()); 1340 1341 verify(l, times(0)).updateAllAlarms(); 1342 verify(l, times(0)).updateAlarmsForUid(anyInt()); 1343 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1344 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1345 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1346 reset(l); 1347 1348 1349 // ------------------------------------------------------------------------- 1350 // Tests with proc state changes. 1351 1352 // With battery saver. 1353 // Battery saver is already on. 1354 1355 mIUidObserver.onUidActive(UID_10_1); 1356 1357 waitUntilMainHandlerDrain(); 1358 waitUntilMainHandlerDrain(); 1359 verify(l, times(0)).updateAllJobs(); 1360 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1361 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1362 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1363 anyBoolean()); 1364 1365 verify(l, times(0)).updateAllAlarms(); 1366 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1367 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1368 verify(l, times(1)).unblockAlarmsForUid(eq(UID_10_1)); 1369 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1370 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1371 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1372 reset(l); 1373 1374 mIUidObserver.onUidGone(UID_10_1, true); 1375 1376 waitUntilMainHandlerDrain(); 1377 waitUntilMainHandlerDrain(); 1378 verify(l, times(0)).updateAllJobs(); 1379 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1380 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1381 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1382 anyBoolean()); 1383 1384 verify(l, times(0)).updateAllAlarms(); 1385 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1386 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1387 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1388 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1389 verify(l, times(1)).removeAlarmsForUid(UID_10_1); 1390 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1391 reset(l); 1392 1393 mIUidObserver.onUidActive(UID_10_1); 1394 1395 waitUntilMainHandlerDrain(); 1396 waitUntilMainHandlerDrain(); 1397 verify(l, times(0)).updateAllJobs(); 1398 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1399 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1400 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1401 anyBoolean()); 1402 1403 verify(l, times(0)).updateAllAlarms(); 1404 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1405 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1406 verify(l, times(1)).unblockAlarmsForUid(eq(UID_10_1)); 1407 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1408 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1409 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1410 reset(l); 1411 1412 mIUidObserver.onUidIdle(UID_10_1, true); 1413 1414 waitUntilMainHandlerDrain(); 1415 waitUntilMainHandlerDrain(); 1416 verify(l, times(0)).updateAllJobs(); 1417 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1418 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1419 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1420 anyBoolean()); 1421 1422 verify(l, times(0)).updateAllAlarms(); 1423 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1424 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1425 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1426 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1427 verify(l, times(1)).removeAlarmsForUid(UID_10_1); 1428 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1429 reset(l); 1430 1431 mIUidObserver.onUidCachedChanged(UID_10_1, true); 1432 1433 waitUntilMainHandlerDrain(); 1434 waitUntilMainHandlerDrain(); 1435 verify(l, times(0)).updateAllJobs(); 1436 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1437 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1438 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1439 anyBoolean()); 1440 1441 verify(l, times(0)).updateAllAlarms(); 1442 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1443 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1444 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1445 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1446 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1447 verify(l, times(1)).handleUidCachedChanged(UID_10_1, true); 1448 reset(l); 1449 1450 mIUidObserver.onUidCachedChanged(UID_10_1, false); 1451 1452 waitUntilMainHandlerDrain(); 1453 waitUntilMainHandlerDrain(); 1454 verify(l, times(0)).updateAllJobs(); 1455 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1456 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1457 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1458 anyBoolean()); 1459 1460 verify(l, times(0)).updateAllAlarms(); 1461 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1462 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1463 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1464 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1465 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1466 verify(l, times(1)).handleUidCachedChanged(UID_10_1, false); 1467 reset(l); 1468 1469 1470 // Without battery saver. 1471 mPowerSaveMode = false; 1472 mPowerSaveObserver.accept(getPowerSaveState()); 1473 1474 waitUntilMainHandlerDrain(); 1475 verify(l, times(1)).updateAllJobs(); 1476 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1477 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1478 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1479 anyBoolean()); 1480 1481 verify(l, times(1)).updateAllAlarms(); 1482 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1483 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1484 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1485 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1486 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1487 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1488 reset(l); 1489 1490 mIUidObserver.onUidActive(UID_10_1); 1491 1492 waitUntilMainHandlerDrain(); 1493 waitUntilMainHandlerDrain(); 1494 verify(l, times(0)).updateAllJobs(); 1495 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1496 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1497 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1498 anyBoolean()); 1499 1500 verify(l, times(0)).updateAllAlarms(); 1501 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1502 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1503 verify(l, times(1)).unblockAlarmsForUid(eq(UID_10_1)); 1504 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1505 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1506 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1507 reset(l); 1508 1509 mIUidObserver.onUidGone(UID_10_1, true); 1510 1511 waitUntilMainHandlerDrain(); 1512 waitUntilMainHandlerDrain(); 1513 verify(l, times(0)).updateAllJobs(); 1514 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1515 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1516 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1517 anyBoolean()); 1518 1519 verify(l, times(0)).updateAllAlarms(); 1520 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1521 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1522 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1523 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1524 verify(l, times(1)).removeAlarmsForUid(UID_10_1); 1525 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1526 reset(l); 1527 1528 mIUidObserver.onUidActive(UID_10_1); 1529 1530 waitUntilMainHandlerDrain(); 1531 waitUntilMainHandlerDrain(); 1532 verify(l, times(0)).updateAllJobs(); 1533 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1534 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1535 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1536 anyBoolean()); 1537 1538 verify(l, times(0)).updateAllAlarms(); 1539 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1540 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1541 verify(l, times(1)).unblockAlarmsForUid(eq(UID_10_1)); 1542 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1543 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1544 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1545 reset(l); 1546 1547 mIUidObserver.onUidIdle(UID_10_1, true); 1548 1549 waitUntilMainHandlerDrain(); 1550 waitUntilMainHandlerDrain(); 1551 verify(l, times(0)).updateAllJobs(); 1552 verify(l, times(1)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1553 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1554 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1555 anyBoolean()); 1556 1557 verify(l, times(0)).updateAllAlarms(); 1558 verify(l, times(1)).updateAlarmsForUid(eq(UID_10_1)); 1559 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1560 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1561 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1562 verify(l, times(1)).removeAlarmsForUid(UID_10_1); 1563 verify(l, times(0)).handleUidCachedChanged(anyInt(), anyBoolean()); 1564 reset(l); 1565 1566 mIUidObserver.onUidCachedChanged(UID_10_1, true); 1567 1568 waitUntilMainHandlerDrain(); 1569 waitUntilMainHandlerDrain(); 1570 verify(l, times(0)).updateAllJobs(); 1571 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1572 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1573 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1574 anyBoolean()); 1575 1576 verify(l, times(0)).updateAllAlarms(); 1577 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1578 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1579 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1580 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1581 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1582 verify(l, times(1)).handleUidCachedChanged(UID_10_1, true); 1583 reset(l); 1584 1585 mIUidObserver.onUidCachedChanged(UID_10_1, false); 1586 1587 waitUntilMainHandlerDrain(); 1588 waitUntilMainHandlerDrain(); 1589 verify(l, times(0)).updateAllJobs(); 1590 verify(l, times(0)).updateJobsForUid(eq(UID_10_1), anyBoolean()); 1591 verify(l, times(0)).updateJobsForUidPackage(anyInt(), anyString(), anyBoolean()); 1592 verify(l, times(0)).updateBackgroundRestrictedForUidPackage(anyInt(), anyString(), 1593 anyBoolean()); 1594 1595 verify(l, times(0)).updateAllAlarms(); 1596 verify(l, times(0)).updateAlarmsForUid(eq(UID_10_1)); 1597 verify(l, times(0)).unblockAllUnrestrictedAlarms(); 1598 verify(l, times(0)).unblockAlarmsForUid(anyInt()); 1599 verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString()); 1600 verify(l, times(0)).removeAlarmsForUid(anyInt()); 1601 verify(l, times(1)).handleUidCachedChanged(UID_10_1, false); 1602 reset(l); 1603 } 1604 1605 @Test testUserRemoved()1606 public void testUserRemoved() throws Exception { 1607 final AppStateTrackerTestable instance = newInstance(); 1608 callStart(instance); 1609 1610 mIUidObserver.onUidActive(UID_1); 1611 mIUidObserver.onUidActive(UID_10_1); 1612 1613 waitUntilMainHandlerDrain(); 1614 waitUntilMainHandlerDrain(); 1615 1616 setAppOps(UID_2, PACKAGE_2, true); 1617 setAppOps(UID_10_2, PACKAGE_2, true); 1618 1619 assertTrue(instance.isUidActive(UID_1)); 1620 assertTrue(instance.isUidActive(UID_10_1)); 1621 1622 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 1623 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 1624 1625 final Intent intent = new Intent(Intent.ACTION_USER_REMOVED); 1626 intent.putExtra(Intent.EXTRA_USER_HANDLE, 10); 1627 mReceiver.onReceive(mMockContext, intent); 1628 1629 waitUntilMainHandlerDrain(); 1630 1631 assertTrue(instance.isUidActive(UID_1)); 1632 assertFalse(instance.isUidActive(UID_10_1)); 1633 1634 assertFalse(instance.isRunAnyInBackgroundAppOpsAllowed(UID_2, PACKAGE_2)); 1635 assertTrue(instance.isRunAnyInBackgroundAppOpsAllowed(UID_10_2, PACKAGE_2)); 1636 } 1637 1638 @Test testSmallBatteryAndPluggedIn()1639 public void testSmallBatteryAndPluggedIn() throws Exception { 1640 // This is a small battery device 1641 mIsSmallBatteryDevice = true; 1642 1643 final AppStateTrackerTestable instance = newInstance(); 1644 callStart(instance); 1645 assertFalse(instance.isForceAllAppsStandbyEnabled()); 1646 1647 // Setting/experiment for all app standby for small battery is enabled 1648 mGlobalSettings.put(Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED, 1); 1649 instance.mFlagsObserver.onChange(true, 1650 Global.getUriFor(Global.FORCED_APP_STANDBY_FOR_SMALL_BATTERY_ENABLED)); 1651 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1652 1653 // When battery is plugged in, force app standby is disabled 1654 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 1655 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB); 1656 mReceiver.onReceive(mMockContext, intent); 1657 assertFalse(instance.isForceAllAppsStandbyEnabled()); 1658 1659 // When battery stops plugged in, force app standby is enabled 1660 mReceiver.onReceive(mMockContext, new Intent(Intent.ACTION_BATTERY_CHANGED)); 1661 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1662 } 1663 1664 @Test testNotSmallBatteryAndPluggedIn()1665 public void testNotSmallBatteryAndPluggedIn() throws Exception { 1666 // Not a small battery device, so plugged in status should not affect forced app standby 1667 mIsSmallBatteryDevice = false; 1668 1669 final AppStateTrackerTestable instance = newInstance(); 1670 callStart(instance); 1671 assertFalse(instance.isForceAllAppsStandbyEnabled()); 1672 1673 mPowerSaveMode = true; 1674 mPowerSaveObserver.accept(getPowerSaveState()); 1675 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1676 1677 // When battery is plugged in, force app standby is unaffected 1678 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 1679 intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_USB); 1680 mReceiver.onReceive(mMockContext, intent); 1681 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1682 1683 // When battery stops plugged in, force app standby is unaffected 1684 mReceiver.onReceive(mMockContext, new Intent(Intent.ACTION_BATTERY_CHANGED)); 1685 assertTrue(instance.isForceAllAppsStandbyEnabled()); 1686 } 1687 1688 @Test testStateClearedOnPackageRemoved()1689 public void testStateClearedOnPackageRemoved() throws Exception { 1690 final AppStateTrackerTestable instance = newInstance(); 1691 callStart(instance); 1692 1693 instance.mActiveUids.put(UID_1, true); 1694 instance.mRunAnyRestrictedPackages.add(Pair.create(UID_1, PACKAGE_1)); 1695 instance.mExemptedBucketPackages.add(UserHandle.getUserId(UID_2), PACKAGE_2); 1696 1697 // Replace PACKAGE_1, nothing should change 1698 Intent packageRemoved = new Intent(Intent.ACTION_PACKAGE_REMOVED) 1699 .putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(UID_1)) 1700 .putExtra(Intent.EXTRA_UID, UID_1) 1701 .putExtra(Intent.EXTRA_REPLACING, true) 1702 .setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, PACKAGE_1, null)); 1703 mReceiver.onReceive(mMockContext, packageRemoved); 1704 1705 assertEquals(1, instance.mActiveUids.size()); 1706 assertEquals(1, instance.mRunAnyRestrictedPackages.size()); 1707 assertEquals(1, instance.mExemptedBucketPackages.size()); 1708 1709 // Replace PACKAGE_2, nothing should change 1710 packageRemoved = new Intent(Intent.ACTION_PACKAGE_REMOVED) 1711 .putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(UID_2)) 1712 .putExtra(Intent.EXTRA_UID, UID_2) 1713 .putExtra(Intent.EXTRA_REPLACING, true) 1714 .setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, PACKAGE_2, null)); 1715 mReceiver.onReceive(mMockContext, packageRemoved); 1716 1717 assertEquals(1, instance.mActiveUids.size()); 1718 assertEquals(1, instance.mRunAnyRestrictedPackages.size()); 1719 assertEquals(1, instance.mExemptedBucketPackages.size()); 1720 1721 // Remove PACKAGE_1 1722 packageRemoved = new Intent(Intent.ACTION_PACKAGE_REMOVED) 1723 .putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(UID_1)) 1724 .putExtra(Intent.EXTRA_UID, UID_1) 1725 .setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, PACKAGE_1, null)); 1726 mReceiver.onReceive(mMockContext, packageRemoved); 1727 1728 assertEquals(0, instance.mActiveUids.size()); 1729 assertEquals(0, instance.mRunAnyRestrictedPackages.size()); 1730 assertEquals(1, instance.mExemptedBucketPackages.size()); 1731 1732 // Remove PACKAGE_2 1733 packageRemoved = new Intent(Intent.ACTION_PACKAGE_REMOVED) 1734 .putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(UID_2)) 1735 .putExtra(Intent.EXTRA_UID, UID_2) 1736 .setData(Uri.fromParts(IntentFilter.SCHEME_PACKAGE, PACKAGE_2, null)); 1737 mReceiver.onReceive(mMockContext, packageRemoved); 1738 1739 assertEquals(0, instance.mActiveUids.size()); 1740 assertEquals(0, instance.mRunAnyRestrictedPackages.size()); 1741 assertEquals(0, instance.mExemptedBucketPackages.size()); 1742 } 1743 array(int... appIds)1744 static int[] array(int... appIds) { 1745 Arrays.sort(appIds); 1746 return appIds; 1747 } 1748 1749 private final Random mRandom = new Random(); 1750 makeRandomArray()1751 int[] makeRandomArray() { 1752 final ArrayList<Integer> list = new ArrayList<>(); 1753 for (int i = 0; i < 5; i++) { 1754 if (mRandom.nextDouble() < 0.5) { 1755 list.add(i); 1756 } 1757 } 1758 return Arrays.stream(list.toArray(new Integer[list.size()])) 1759 .mapToInt(Integer::intValue).toArray(); 1760 } 1761 isAnyAppIdUnexemptSlow(int[] prevArray, int[] newArray)1762 static boolean isAnyAppIdUnexemptSlow(int[] prevArray, int[] newArray) { 1763 Arrays.sort(newArray); // Just in case... 1764 for (int p : prevArray) { 1765 if (Arrays.binarySearch(newArray, p) < 0) { 1766 return true; 1767 } 1768 } 1769 return false; 1770 } 1771 checkAnyAppIdUnexempt(int[] prevArray, int[] newArray, boolean expected)1772 private void checkAnyAppIdUnexempt(int[] prevArray, int[] newArray, boolean expected) { 1773 assertEquals("Input: " + Arrays.toString(prevArray) + " " + Arrays.toString(newArray), 1774 expected, AppStateTrackerImpl.isAnyAppIdUnexempt(prevArray, newArray)); 1775 1776 // Also test isAnyAppIdUnexempt. 1777 assertEquals("Input: " + Arrays.toString(prevArray) + " " + Arrays.toString(newArray), 1778 expected, isAnyAppIdUnexemptSlow(prevArray, newArray)); 1779 } 1780 1781 @Test isAnyAppIdUnexempt()1782 public void isAnyAppIdUnexempt() { 1783 checkAnyAppIdUnexempt(array(), array(), false); 1784 1785 checkAnyAppIdUnexempt(array(1), array(), true); 1786 checkAnyAppIdUnexempt(array(1), array(1), false); 1787 checkAnyAppIdUnexempt(array(1), array(0, 1), false); 1788 checkAnyAppIdUnexempt(array(1), array(0, 1, 2), false); 1789 checkAnyAppIdUnexempt(array(1), array(0, 1, 2), false); 1790 1791 checkAnyAppIdUnexempt(array(1, 2, 10), array(), true); 1792 checkAnyAppIdUnexempt(array(1, 2, 10), array(1, 2), true); 1793 checkAnyAppIdUnexempt(array(1, 2, 10), array(1, 2, 10), false); 1794 checkAnyAppIdUnexempt(array(1, 2, 10), array(2, 10), true); 1795 checkAnyAppIdUnexempt(array(1, 2, 10), array(0, 1, 2, 4, 3, 10), false); 1796 checkAnyAppIdUnexempt(array(1, 2, 10), array(0, 0, 1, 2, 10), false); 1797 1798 // Random test 1799 int trueCount = 0; 1800 final int count = 10000; 1801 for (int i = 0; i < count; i++) { 1802 final int[] array1 = makeRandomArray(); 1803 final int[] array2 = makeRandomArray(); 1804 1805 final boolean expected = isAnyAppIdUnexemptSlow(array1, array2); 1806 final boolean actual = AppStateTrackerImpl.isAnyAppIdUnexempt(array1, array2); 1807 1808 assertEquals("Input: " + Arrays.toString(array1) + " " + Arrays.toString(array2), 1809 expected, actual); 1810 if (expected) { 1811 trueCount++; 1812 } 1813 } 1814 1815 // Make sure makeRandomArray() didn't generate all same arrays by accident. 1816 assertTrue(trueCount > 0); 1817 assertTrue(trueCount < count); 1818 } 1819 } 1820