1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.pm; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import static org.junit.Assert.fail; 23 import static org.junit.Assume.assumeTrue; 24 import static org.testng.Assert.assertThrows; 25 26 import android.annotation.UserIdInt; 27 import android.app.ActivityManager; 28 import android.content.Context; 29 import android.content.pm.PackageManager; 30 import android.content.pm.UserInfo; 31 import android.content.pm.UserProperties; 32 import android.content.res.Resources; 33 import android.os.Bundle; 34 import android.os.UserHandle; 35 import android.os.UserManager; 36 import android.platform.test.annotations.Postsubmit; 37 import android.provider.Settings; 38 import android.test.suitebuilder.annotation.LargeTest; 39 import android.test.suitebuilder.annotation.MediumTest; 40 import android.util.ArraySet; 41 import android.util.Slog; 42 43 import androidx.annotation.Nullable; 44 import androidx.test.InstrumentationRegistry; 45 import androidx.test.filters.SmallTest; 46 import androidx.test.runner.AndroidJUnit4; 47 48 import com.google.common.collect.ImmutableList; 49 import com.google.common.collect.Range; 50 51 import org.junit.After; 52 import org.junit.Before; 53 import org.junit.Test; 54 import org.junit.runner.RunWith; 55 56 import java.util.ArrayList; 57 import java.util.Arrays; 58 import java.util.List; 59 import java.util.concurrent.ExecutorService; 60 import java.util.concurrent.Executors; 61 import java.util.concurrent.TimeUnit; 62 import java.util.concurrent.atomic.AtomicInteger; 63 import java.util.stream.Collectors; 64 65 /** 66 * Test {@link UserManager} functionality. 67 * 68 * atest com.android.server.pm.UserManagerTest 69 */ 70 @Postsubmit 71 @RunWith(AndroidJUnit4.class) 72 public final class UserManagerTest { 73 // Taken from UserManagerService 74 private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // 30 years 75 76 private static final int SWITCH_USER_TIMEOUT_SECONDS = 180; // 180 seconds 77 private static final int REMOVE_USER_TIMEOUT_SECONDS = 180; // 180 seconds 78 79 // Packages which are used during tests. 80 private static final String[] PACKAGES = new String[] { 81 "com.android.egg", 82 "com.google.android.webview" 83 }; 84 private static final String TAG = UserManagerTest.class.getSimpleName(); 85 86 private final Context mContext = InstrumentationRegistry.getInstrumentation().getContext(); 87 88 private UserManager mUserManager = null; 89 private ActivityManager mActivityManager; 90 private PackageManager mPackageManager; 91 private ArraySet<Integer> mUsersToRemove; 92 private UserSwitchWaiter mUserSwitchWaiter; 93 private UserRemovalWaiter mUserRemovalWaiter; 94 private int mOriginalCurrentUserId; 95 96 @Before setUp()97 public void setUp() throws Exception { 98 mOriginalCurrentUserId = ActivityManager.getCurrentUser(); 99 mUserManager = UserManager.get(mContext); 100 mActivityManager = mContext.getSystemService(ActivityManager.class); 101 mPackageManager = mContext.getPackageManager(); 102 mUserSwitchWaiter = new UserSwitchWaiter(TAG, SWITCH_USER_TIMEOUT_SECONDS); 103 mUserRemovalWaiter = new UserRemovalWaiter(mContext, TAG, REMOVE_USER_TIMEOUT_SECONDS); 104 105 mUsersToRemove = new ArraySet<>(); 106 removeExistingUsers(); 107 } 108 109 @After tearDown()110 public void tearDown() throws Exception { 111 if (mOriginalCurrentUserId != ActivityManager.getCurrentUser()) { 112 switchUser(mOriginalCurrentUserId); 113 } 114 mUserSwitchWaiter.close(); 115 116 // Making a copy of mUsersToRemove to avoid ConcurrentModificationException 117 mUsersToRemove.stream().toList().forEach(this::removeUser); 118 mUserRemovalWaiter.close(); 119 } 120 removeExistingUsers()121 private void removeExistingUsers() { 122 int currentUser = ActivityManager.getCurrentUser(); 123 List<UserInfo> list = mUserManager.getUsers(); 124 for (UserInfo user : list) { 125 // Keep system and current user 126 if (user.id != UserHandle.USER_SYSTEM && user.id != currentUser) { 127 removeUser(user.id); 128 } 129 } 130 } 131 132 @SmallTest 133 @Test testHasSystemUser()134 public void testHasSystemUser() throws Exception { 135 assertThat(hasUser(UserHandle.USER_SYSTEM)).isTrue(); 136 } 137 138 @MediumTest 139 @Test testAddGuest()140 public void testAddGuest() throws Exception { 141 UserInfo userInfo = createUser("Guest 1", UserInfo.FLAG_GUEST); 142 assertThat(userInfo).isNotNull(); 143 144 List<UserInfo> list = mUserManager.getUsers(); 145 for (UserInfo user : list) { 146 if (user.id == userInfo.id && user.name.equals("Guest 1") 147 && user.isGuest() 148 && !user.isAdmin() 149 && !user.isPrimary()) { 150 return; 151 } 152 } 153 fail("Didn't find a guest: " + list); 154 } 155 156 @Test testCloneUser()157 public void testCloneUser() throws Exception { 158 assumeCloneEnabled(); 159 UserHandle mainUser = mUserManager.getMainUser(); 160 assumeTrue("Main user is null", mainUser != null); 161 // Get the default properties for clone user type. 162 final UserTypeDetails userTypeDetails = 163 UserTypeFactory.getUserTypes().get(UserManager.USER_TYPE_PROFILE_CLONE); 164 assertWithMessage("No %s type on device", UserManager.USER_TYPE_PROFILE_CLONE) 165 .that(userTypeDetails).isNotNull(); 166 final UserProperties typeProps = userTypeDetails.getDefaultUserPropertiesReference(); 167 168 // Test that only one clone user can be created 169 final int mainUserId = mainUser.getIdentifier(); 170 UserInfo userInfo = createProfileForUser("Clone user1", 171 UserManager.USER_TYPE_PROFILE_CLONE, 172 mainUserId); 173 assertThat(userInfo).isNotNull(); 174 UserInfo userInfo2 = createProfileForUser("Clone user2", 175 UserManager.USER_TYPE_PROFILE_CLONE, 176 mainUserId); 177 assertThat(userInfo2).isNull(); 178 179 final Context userContext = mContext.createPackageContextAsUser("system", 0, 180 UserHandle.of(userInfo.id)); 181 assertThat(userContext.getSystemService( 182 UserManager.class).isMediaSharedWithParent()).isTrue(); 183 assertThat(Settings.Secure.getInt(userContext.getContentResolver(), 184 Settings.Secure.USER_SETUP_COMPLETE, 0)).isEqualTo(1); 185 186 List<UserInfo> list = mUserManager.getUsers(); 187 List<UserInfo> cloneUsers = list.stream().filter( 188 user -> (user.id == userInfo.id && user.name.equals("Clone user1") 189 && user.isCloneProfile())) 190 .collect(Collectors.toList()); 191 assertThat(cloneUsers.size()).isEqualTo(1); 192 193 // Check that the new clone user has the expected properties (relative to the defaults) 194 // provided that the test caller has the necessary permissions. 195 UserProperties cloneUserProperties = 196 mUserManager.getUserProperties(UserHandle.of(userInfo.id)); 197 assertThat(typeProps.getUseParentsContacts()) 198 .isEqualTo(cloneUserProperties.getUseParentsContacts()); 199 assertThat(typeProps.getShowInLauncher()) 200 .isEqualTo(cloneUserProperties.getShowInLauncher()); 201 assertThrows(SecurityException.class, cloneUserProperties::getStartWithParent); 202 assertThrows(SecurityException.class, 203 cloneUserProperties::getCrossProfileIntentFilterAccessControl); 204 assertThrows(SecurityException.class, 205 cloneUserProperties::getCrossProfileIntentResolutionStrategy); 206 assertThat(typeProps.isMediaSharedWithParent()) 207 .isEqualTo(cloneUserProperties.isMediaSharedWithParent()); 208 assertThat(typeProps.isCredentialShareableWithParent()) 209 .isEqualTo(cloneUserProperties.isCredentialShareableWithParent()); 210 assertThrows(SecurityException.class, cloneUserProperties::getDeleteAppWithParent); 211 212 // Verify clone user parent 213 assertThat(mUserManager.getProfileParent(mainUserId)).isNull(); 214 UserInfo parentProfileInfo = mUserManager.getProfileParent(userInfo.id); 215 assertThat(parentProfileInfo).isNotNull(); 216 assertThat(mainUserId).isEqualTo(parentProfileInfo.id); 217 removeUser(userInfo.id); 218 assertThat(mUserManager.getProfileParent(mainUserId)).isNull(); 219 } 220 221 @MediumTest 222 @Test testAdd2Users()223 public void testAdd2Users() throws Exception { 224 UserInfo user1 = createUser("Guest 1", UserInfo.FLAG_GUEST); 225 UserInfo user2 = createUser("User 2", UserInfo.FLAG_ADMIN); 226 227 assertThat(user1).isNotNull(); 228 assertThat(user2).isNotNull(); 229 230 assertThat(hasUser(UserHandle.USER_SYSTEM)).isTrue(); 231 assertThat(hasUser(user1.id)).isTrue(); 232 assertThat(hasUser(user2.id)).isTrue(); 233 } 234 235 /** 236 * Tests that UserManager knows how many users can be created. 237 * 238 * We can only test this with regular secondary users, since some other user types have weird 239 * rules about when or if they count towards the max. 240 */ 241 @MediumTest 242 @Test testAddTooManyUsers()243 public void testAddTooManyUsers() throws Exception { 244 final String userType = UserManager.USER_TYPE_FULL_SECONDARY; 245 final UserTypeDetails userTypeDetails = UserTypeFactory.getUserTypes().get(userType); 246 247 final int maxUsersForType = userTypeDetails.getMaxAllowed(); 248 final int maxUsersOverall = UserManager.getMaxSupportedUsers(); 249 250 int currentUsersOfType = 0; 251 int currentUsersOverall = 0; 252 final List<UserInfo> userList = mUserManager.getAliveUsers(); 253 for (UserInfo user : userList) { 254 currentUsersOverall++; 255 if (userType.equals(user.userType)) { 256 currentUsersOfType++; 257 } 258 } 259 260 final int remainingUserType = maxUsersForType == UserTypeDetails.UNLIMITED_NUMBER_OF_USERS ? 261 Integer.MAX_VALUE : maxUsersForType - currentUsersOfType; 262 final int remainingOverall = maxUsersOverall - currentUsersOverall; 263 final int remaining = Math.min(remainingUserType, remainingOverall); 264 265 Slog.v(TAG, "maxUsersForType=" + maxUsersForType 266 + ", maxUsersOverall=" + maxUsersOverall 267 + ", currentUsersOfType=" + currentUsersOfType 268 + ", currentUsersOverall=" + currentUsersOverall 269 + ", remaining=" + remaining); 270 271 assumeTrue("Device supports too many users for this test to be practical", remaining < 20); 272 273 int usersAdded; 274 for (usersAdded = 0; usersAdded < remaining; usersAdded++) { 275 Slog.v(TAG, "Adding user " + usersAdded); 276 assertThat(mUserManager.canAddMoreUsers()).isTrue(); 277 assertThat(mUserManager.canAddMoreUsers(userType)).isTrue(); 278 279 final UserInfo user = createUser("User " + usersAdded, userType, 0); 280 assertThat(user).isNotNull(); 281 assertThat(hasUser(user.id)).isTrue(); 282 } 283 Slog.v(TAG, "Added " + usersAdded + " users."); 284 285 assertWithMessage("Still thinks more users of that type can be added") 286 .that(mUserManager.canAddMoreUsers(userType)).isFalse(); 287 if (currentUsersOverall + usersAdded >= maxUsersOverall) { 288 assertThat(mUserManager.canAddMoreUsers()).isFalse(); 289 } 290 291 assertThat(createUser("User beyond", userType, 0)).isNull(); 292 293 assertThat(mUserManager.canAddMoreUsers(mUserManager.USER_TYPE_FULL_GUEST)).isTrue(); 294 } 295 296 @MediumTest 297 @Test testRemoveUser()298 public void testRemoveUser() throws Exception { 299 UserInfo userInfo = createUser("Guest 1", UserInfo.FLAG_GUEST); 300 removeUser(userInfo.id); 301 302 assertThat(hasUser(userInfo.id)).isFalse(); 303 } 304 305 @MediumTest 306 @Test testRemoveUserByHandle()307 public void testRemoveUserByHandle() { 308 UserInfo userInfo = createUser("Guest 1", UserInfo.FLAG_GUEST); 309 310 removeUser(userInfo.getUserHandle()); 311 312 assertThat(hasUser(userInfo.id)).isFalse(); 313 } 314 315 @MediumTest 316 @Test testRemoveUserByHandle_ThrowsException()317 public void testRemoveUserByHandle_ThrowsException() { 318 assertThrows(IllegalArgumentException.class, () -> mUserManager.removeUser(null)); 319 } 320 321 @MediumTest 322 @Test testRemoveUserShouldNotRemoveCurrentUser()323 public void testRemoveUserShouldNotRemoveCurrentUser() { 324 final int startUser = ActivityManager.getCurrentUser(); 325 final UserInfo testUser = createUser("TestUser", /* flags= */ 0); 326 // Switch to the user just created. 327 switchUser(testUser.id); 328 329 assertWithMessage("Current user should not be removed") 330 .that(mUserManager.removeUser(testUser.id)) 331 .isFalse(); 332 333 // Switch back to the starting user. 334 switchUser(startUser); 335 336 // Now we can remove the user 337 removeUser(testUser.id); 338 } 339 340 @MediumTest 341 @Test testRemoveUserShouldNotRemoveCurrentUser_DuringUserSwitch()342 public void testRemoveUserShouldNotRemoveCurrentUser_DuringUserSwitch() { 343 final int startUser = ActivityManager.getCurrentUser(); 344 final UserInfo testUser = createUser("TestUser", /* flags= */ 0); 345 // Switch to the user just created. 346 switchUser(testUser.id); 347 348 switchUserThenRun(startUser, () -> { 349 // While the user switch is happening, call removeUser for the current user. 350 assertWithMessage("Current user should not be removed during user switch") 351 .that(mUserManager.removeUser(testUser.id)) 352 .isFalse(); 353 }); 354 assertThat(hasUser(testUser.id)).isTrue(); 355 356 // Now we can remove the user 357 removeUser(testUser.id); 358 } 359 360 @MediumTest 361 @Test testRemoveUserShouldNotRemoveTargetUser_DuringUserSwitch()362 public void testRemoveUserShouldNotRemoveTargetUser_DuringUserSwitch() { 363 final int startUser = ActivityManager.getCurrentUser(); 364 final UserInfo testUser = createUser("TestUser", /* flags= */ 0); 365 366 switchUserThenRun(testUser.id, () -> { 367 // While the user switch is happening, call removeUser for the target user. 368 assertWithMessage("Target user should not be removed during user switch") 369 .that(mUserManager.removeUser(testUser.id)) 370 .isFalse(); 371 }); 372 assertThat(hasUser(testUser.id)).isTrue(); 373 374 // Switch back to the starting user. 375 switchUser(startUser); 376 377 // Now we can remove the user 378 removeUser(testUser.id); 379 } 380 381 @MediumTest 382 @Test testRemoveUserWhenPossible_restrictedReturnsError()383 public void testRemoveUserWhenPossible_restrictedReturnsError() throws Exception { 384 final int currentUser = ActivityManager.getCurrentUser(); 385 final UserInfo user1 = createUser("User 1", /* flags= */ 0); 386 mUserManager.setUserRestriction(UserManager.DISALLOW_REMOVE_USER, /* value= */ true, 387 asHandle(currentUser)); 388 try { 389 assertThat(mUserManager.removeUserWhenPossible(user1.getUserHandle(), 390 /* overrideDevicePolicy= */ false)) 391 .isEqualTo(UserManager.REMOVE_RESULT_ERROR_USER_RESTRICTION); 392 } finally { 393 mUserManager.setUserRestriction(UserManager.DISALLOW_REMOVE_USER, /* value= */ false, 394 asHandle(currentUser)); 395 } 396 397 assertThat(hasUser(user1.id)).isTrue(); 398 assertThat(getUser(user1.id).isEphemeral()).isFalse(); 399 } 400 401 @MediumTest 402 @Test testRemoveUserWhenPossible_evenWhenRestricted()403 public void testRemoveUserWhenPossible_evenWhenRestricted() throws Exception { 404 final int currentUser = ActivityManager.getCurrentUser(); 405 final UserInfo user1 = createUser("User 1", /* flags= */ 0); 406 mUserManager.setUserRestriction(UserManager.DISALLOW_REMOVE_USER, /* value= */ true, 407 asHandle(currentUser)); 408 try { 409 assertThat(mUserManager.removeUserWhenPossible(user1.getUserHandle(), 410 /* overrideDevicePolicy= */ true)) 411 .isEqualTo(UserManager.REMOVE_RESULT_REMOVED); 412 waitForUserRemoval(user1.id); 413 } finally { 414 mUserManager.setUserRestriction(UserManager.DISALLOW_REMOVE_USER, /* value= */ false, 415 asHandle(currentUser)); 416 } 417 418 assertThat(hasUser(user1.id)).isFalse(); 419 } 420 421 @MediumTest 422 @Test testRemoveUserWhenPossible_systemUserReturnsError()423 public void testRemoveUserWhenPossible_systemUserReturnsError() throws Exception { 424 assertThat(mUserManager.removeUserWhenPossible(UserHandle.SYSTEM, 425 /* overrideDevicePolicy= */ false)) 426 .isEqualTo(UserManager.REMOVE_RESULT_ERROR_SYSTEM_USER); 427 428 assertThat(hasUser(UserHandle.USER_SYSTEM)).isTrue(); 429 } 430 431 @MediumTest 432 @Test testRemoveUserWhenPossible_permanentAdminMainUserReturnsError()433 public void testRemoveUserWhenPossible_permanentAdminMainUserReturnsError() throws Exception { 434 assumeHeadlessModeEnabled(); 435 assumeTrue("Main user is not permanent admin", isMainUserPermanentAdmin()); 436 437 int currentUser = ActivityManager.getCurrentUser(); 438 final UserInfo otherUser = createUser("User 1", /* flags= */ UserInfo.FLAG_ADMIN); 439 UserHandle mainUser = mUserManager.getMainUser(); 440 441 switchUser(otherUser.id); 442 443 assertThat(mUserManager.removeUserWhenPossible(mainUser, 444 /* overrideDevicePolicy= */ false)) 445 .isEqualTo(UserManager.REMOVE_RESULT_ERROR_MAIN_USER_PERMANENT_ADMIN); 446 447 448 assertThat(hasUser(mainUser.getIdentifier())).isTrue(); 449 450 // Switch back to the starting user. 451 switchUser(currentUser); 452 } 453 454 @MediumTest 455 @Test testRemoveUserWhenPossible_invalidUserReturnsError()456 public void testRemoveUserWhenPossible_invalidUserReturnsError() throws Exception { 457 assertThat(hasUser(Integer.MAX_VALUE)).isFalse(); 458 assertThat(mUserManager.removeUserWhenPossible(UserHandle.of(Integer.MAX_VALUE), 459 /* overrideDevicePolicy= */ false)) 460 .isEqualTo(UserManager.REMOVE_RESULT_ERROR_USER_NOT_FOUND); 461 } 462 463 @MediumTest 464 @Test testRemoveUserWhenPossible_currentUserSetEphemeral()465 public void testRemoveUserWhenPossible_currentUserSetEphemeral() throws Exception { 466 final int startUser = ActivityManager.getCurrentUser(); 467 final UserInfo user1 = createUser("User 1", /* flags= */ 0); 468 // Switch to the user just created. 469 switchUser(user1.id); 470 471 assertThat(mUserManager.removeUserWhenPossible(user1.getUserHandle(), 472 /* overrideDevicePolicy= */ false)).isEqualTo(UserManager.REMOVE_RESULT_DEFERRED); 473 474 assertThat(hasUser(user1.id)).isTrue(); 475 assertThat(getUser(user1.id).isEphemeral()).isTrue(); 476 477 // Switch back to the starting user. 478 switchUser(startUser); 479 // User will be removed once switch is complete 480 waitForUserRemoval(user1.id); 481 482 assertThat(hasUser(user1.id)).isFalse(); 483 } 484 485 @MediumTest 486 @Test testRemoveUserWhenPossible_currentUserSetEphemeral_duringUserSwitch()487 public void testRemoveUserWhenPossible_currentUserSetEphemeral_duringUserSwitch() { 488 final int startUser = ActivityManager.getCurrentUser(); 489 final UserInfo testUser = createUser("TestUser", /* flags= */ 0); 490 // Switch to the user just created. 491 switchUser(testUser.id); 492 493 switchUserThenRun(startUser, () -> { 494 // While the switch is happening, call removeUserWhenPossible for the current user. 495 assertThat(mUserManager.removeUserWhenPossible(testUser.getUserHandle(), 496 /* overrideDevicePolicy= */ false)) 497 .isEqualTo(UserManager.REMOVE_RESULT_DEFERRED); 498 499 assertThat(hasUser(testUser.id)).isTrue(); 500 assertThat(getUser(testUser.id).isEphemeral()).isTrue(); 501 }); // wait for user switch - startUser 502 // User will be removed once switch is complete 503 waitForUserRemoval(testUser.id); 504 505 assertThat(hasUser(testUser.id)).isFalse(); 506 } 507 508 @MediumTest 509 @Test testRemoveUserWhenPossible_targetUserSetEphemeral_duringUserSwitch()510 public void testRemoveUserWhenPossible_targetUserSetEphemeral_duringUserSwitch() { 511 final int startUser = ActivityManager.getCurrentUser(); 512 final UserInfo testUser = createUser("TestUser", /* flags= */ 0); 513 514 switchUserThenRun(testUser.id, () -> { 515 // While the user switch is happening, call removeUserWhenPossible for the target user. 516 assertThat(mUserManager.removeUserWhenPossible(testUser.getUserHandle(), 517 /* overrideDevicePolicy= */ false)) 518 .isEqualTo(UserManager.REMOVE_RESULT_DEFERRED); 519 520 assertThat(hasUser(testUser.id)).isTrue(); 521 assertThat(getUser(testUser.id).isEphemeral()).isTrue(); 522 }); // wait for user switch - testUser 523 524 // Switch back to the starting user. 525 switchUser(startUser); 526 // User will be removed once switch is complete 527 waitForUserRemoval(testUser.id); 528 529 assertThat(hasUser(testUser.id)).isFalse(); 530 } 531 532 @MediumTest 533 @Test testRemoveUserWhenPossible_nonCurrentUserRemoved()534 public void testRemoveUserWhenPossible_nonCurrentUserRemoved() throws Exception { 535 final UserInfo user1 = createUser("User 1", /* flags= */ 0); 536 537 assertThat(mUserManager.removeUserWhenPossible(user1.getUserHandle(), 538 /* overrideDevicePolicy= */ false)) 539 .isEqualTo(UserManager.REMOVE_RESULT_REMOVED); 540 waitForUserRemoval(user1.id); 541 542 assertThat(hasUser(user1.id)).isFalse(); 543 } 544 545 @MediumTest 546 @Test testRemoveUserWhenPossible_withProfiles()547 public void testRemoveUserWhenPossible_withProfiles() throws Exception { 548 assumeHeadlessModeEnabled(); 549 assumeCloneEnabled(); 550 final List<String> profileTypesToCreate = Arrays.asList( 551 UserManager.USER_TYPE_PROFILE_CLONE, 552 UserManager.USER_TYPE_PROFILE_MANAGED 553 ); 554 555 final UserInfo parentUser = createUser("Human User", /* flags= */ 0); 556 assertWithMessage("Could not create parent user") 557 .that(parentUser).isNotNull(); 558 559 final List<Integer> profileIds = new ArrayList<>(); 560 for (String profileType : profileTypesToCreate) { 561 final String name = profileType.substring(profileType.lastIndexOf('.') + 1); 562 if (mUserManager.canAddMoreProfilesToUser(profileType, parentUser.id)) { 563 final UserInfo profile = createProfileForUser(name, profileType, parentUser.id); 564 assertWithMessage("Could not create " + name) 565 .that(profile).isNotNull(); 566 profileIds.add(profile.id); 567 } else { 568 Slog.w(TAG, "Can not add " + name + " to user #" + parentUser.id); 569 } 570 } 571 572 // Test shouldn't pass or fail unless it's allowed to add profiles to secondary users. 573 assumeTrue("Not possible to create any profiles to user #" + parentUser.id, 574 profileIds.size() > 0); 575 576 assertThat(mUserManager.removeUserWhenPossible(parentUser.getUserHandle(), 577 /* overrideDevicePolicy= */ false)) 578 .isEqualTo(UserManager.REMOVE_RESULT_REMOVED); 579 waitForUserRemoval(parentUser.id); 580 581 assertWithMessage("Parent user still exists") 582 .that(hasUser(parentUser.id)).isFalse(); 583 profileIds.forEach(id -> 584 assertWithMessage("Profile still exists") 585 .that(hasUser(id)).isFalse()); 586 } 587 588 /** Tests creating a FULL user via specifying userType. */ 589 @MediumTest 590 @Test testCreateUserViaTypes()591 public void testCreateUserViaTypes() throws Exception { 592 createUserWithTypeAndCheckFlags(UserManager.USER_TYPE_FULL_GUEST, 593 UserInfo.FLAG_GUEST | UserInfo.FLAG_FULL); 594 595 createUserWithTypeAndCheckFlags(UserManager.USER_TYPE_FULL_DEMO, 596 UserInfo.FLAG_DEMO | UserInfo.FLAG_FULL); 597 598 createUserWithTypeAndCheckFlags(UserManager.USER_TYPE_FULL_SECONDARY, 599 UserInfo.FLAG_FULL); 600 } 601 602 /** Tests creating a FULL user via specifying user flags. */ 603 @MediumTest 604 @Test testCreateUserViaFlags()605 public void testCreateUserViaFlags() throws Exception { 606 createUserWithFlagsAndCheckType(UserInfo.FLAG_GUEST, UserManager.USER_TYPE_FULL_GUEST, 607 UserInfo.FLAG_FULL); 608 609 createUserWithFlagsAndCheckType(0, UserManager.USER_TYPE_FULL_SECONDARY, 610 UserInfo.FLAG_FULL); 611 612 createUserWithFlagsAndCheckType(UserInfo.FLAG_FULL, UserManager.USER_TYPE_FULL_SECONDARY, 613 0); 614 615 createUserWithFlagsAndCheckType(UserInfo.FLAG_DEMO, UserManager.USER_TYPE_FULL_DEMO, 616 UserInfo.FLAG_FULL); 617 } 618 619 /** Creates a user of the given user type and checks that the result has the requiredFlags. */ createUserWithTypeAndCheckFlags(String userType, @UserIdInt int requiredFlags)620 private void createUserWithTypeAndCheckFlags(String userType, 621 @UserIdInt int requiredFlags) { 622 final UserInfo userInfo = createUser("Name", userType, 0); 623 assertWithMessage("Wrong user type").that(userInfo.userType).isEqualTo(userType); 624 assertWithMessage("Flags %s did not contain expected %s", userInfo.flags, requiredFlags) 625 .that(userInfo.flags & requiredFlags).isEqualTo(requiredFlags); 626 removeUser(userInfo.id); 627 } 628 629 /** 630 * Creates a user of the given flags and checks that the result is of the expectedUserType type 631 * and that it has the expected flags (including both flags and any additionalRequiredFlags). 632 */ createUserWithFlagsAndCheckType(@serIdInt int flags, String expectedUserType, @UserIdInt int additionalRequiredFlags)633 private void createUserWithFlagsAndCheckType(@UserIdInt int flags, String expectedUserType, 634 @UserIdInt int additionalRequiredFlags) { 635 final UserInfo userInfo = createUser("Name", flags); 636 assertWithMessage("Wrong user type").that(userInfo.userType).isEqualTo(expectedUserType); 637 additionalRequiredFlags |= flags; 638 assertWithMessage("Flags %s did not contain expected %s", userInfo.flags, 639 additionalRequiredFlags).that(userInfo.flags & additionalRequiredFlags) 640 .isEqualTo(additionalRequiredFlags); 641 removeUser(userInfo.id); 642 } 643 requireSingleGuest()644 private void requireSingleGuest() throws Exception { 645 assumeTrue("device supports single guest", 646 UserTypeFactory.getUserTypes().get(UserManager.USER_TYPE_FULL_GUEST) 647 .getMaxAllowed() == 1); 648 } 649 requireMultipleGuests()650 private void requireMultipleGuests() throws Exception { 651 assumeTrue("device supports multiple guests", 652 UserTypeFactory.getUserTypes().get(UserManager.USER_TYPE_FULL_GUEST) 653 .getMaxAllowed() > 1); 654 } 655 656 @MediumTest 657 @Test testThereCanBeOnlyOneGuest_singleGuest()658 public void testThereCanBeOnlyOneGuest_singleGuest() throws Exception { 659 requireSingleGuest(); 660 assertThat(mUserManager.canAddMoreUsers(mUserManager.USER_TYPE_FULL_GUEST)).isTrue(); 661 UserInfo userInfo1 = createUser("Guest 1", UserInfo.FLAG_GUEST); 662 assertThat(userInfo1).isNotNull(); 663 assertThat(mUserManager.canAddMoreUsers(mUserManager.USER_TYPE_FULL_GUEST)).isFalse(); 664 UserInfo userInfo2 = createUser("Guest 2", UserInfo.FLAG_GUEST); 665 assertThat(userInfo2).isNull(); 666 } 667 668 @MediumTest 669 @Test testThereCanBeMultipleGuests_multipleGuests()670 public void testThereCanBeMultipleGuests_multipleGuests() throws Exception { 671 requireMultipleGuests(); 672 assertThat(mUserManager.canAddMoreUsers(mUserManager.USER_TYPE_FULL_GUEST)).isTrue(); 673 UserInfo userInfo1 = createUser("Guest 1", UserInfo.FLAG_GUEST); 674 assertThat(userInfo1).isNotNull(); 675 assertThat(mUserManager.canAddMoreUsers(mUserManager.USER_TYPE_FULL_GUEST)).isTrue(); 676 UserInfo userInfo2 = createUser("Guest 2", UserInfo.FLAG_GUEST); 677 assertThat(userInfo2).isNotNull(); 678 } 679 680 @MediumTest 681 @Test testFindExistingGuest_guestExists()682 public void testFindExistingGuest_guestExists() throws Exception { 683 UserInfo userInfo1 = createUser("Guest", UserInfo.FLAG_GUEST); 684 assertThat(userInfo1).isNotNull(); 685 UserInfo foundGuest = mUserManager.findCurrentGuestUser(); 686 assertThat(foundGuest).isNotNull(); 687 } 688 689 @MediumTest 690 @Test testGetGuestUsers_singleGuest()691 public void testGetGuestUsers_singleGuest() throws Exception { 692 requireSingleGuest(); 693 UserInfo userInfo1 = createUser("Guest1", UserInfo.FLAG_GUEST); 694 assertThat(userInfo1).isNotNull(); 695 List<UserInfo> guestsFound = mUserManager.getGuestUsers(); 696 assertThat(guestsFound).hasSize(1); 697 assertThat(guestsFound.get(0).name).isEqualTo("Guest1"); 698 } 699 700 @MediumTest 701 @Test testGetGuestUsers_multipleGuests()702 public void testGetGuestUsers_multipleGuests() throws Exception { 703 requireMultipleGuests(); 704 UserInfo userInfo1 = createUser("Guest1", UserInfo.FLAG_GUEST); 705 assertThat(userInfo1).isNotNull(); 706 UserInfo userInfo2 = createUser("Guest2", UserInfo.FLAG_GUEST); 707 assertThat(userInfo2).isNotNull(); 708 709 List<UserInfo> guestsFound = mUserManager.getGuestUsers(); 710 assertThat(guestsFound).hasSize(2); 711 assertThat(ImmutableList.of(guestsFound.get(0).name, guestsFound.get(1).name)) 712 .containsExactly("Guest1", "Guest2"); 713 } 714 715 @MediumTest 716 @Test testGetGuestUsers_markGuestForDeletion()717 public void testGetGuestUsers_markGuestForDeletion() throws Exception { 718 requireMultipleGuests(); 719 UserInfo userInfo1 = createUser("Guest1", UserInfo.FLAG_GUEST); 720 assertThat(userInfo1).isNotNull(); 721 UserInfo userInfo2 = createUser("Guest2", UserInfo.FLAG_GUEST); 722 assertThat(userInfo2).isNotNull(); 723 724 boolean markedForDeletion1 = mUserManager.markGuestForDeletion(userInfo1.id); 725 assertThat(markedForDeletion1).isTrue(); 726 727 List<UserInfo> guestsFound = mUserManager.getGuestUsers(); 728 assertThat(guestsFound.size()).isEqualTo(1); 729 730 boolean markedForDeletion2 = mUserManager.markGuestForDeletion(userInfo2.id); 731 assertThat(markedForDeletion2).isTrue(); 732 733 guestsFound = mUserManager.getGuestUsers(); 734 assertThat(guestsFound).isEmpty(); 735 } 736 737 @SmallTest 738 @Test testFindExistingGuest_guestDoesNotExist()739 public void testFindExistingGuest_guestDoesNotExist() throws Exception { 740 UserInfo foundGuest = mUserManager.findCurrentGuestUser(); 741 assertThat(foundGuest).isNull(); 742 } 743 744 @SmallTest 745 @Test testGetGuestUsers_guestDoesNotExist()746 public void testGetGuestUsers_guestDoesNotExist() throws Exception { 747 List<UserInfo> guestsFound = mUserManager.getGuestUsers(); 748 assertThat(guestsFound).isEmpty(); 749 } 750 751 @MediumTest 752 @Test testSetUserAdmin()753 public void testSetUserAdmin() throws Exception { 754 UserInfo userInfo = createUser("SecondaryUser", /*flags=*/ 0); 755 assertThat(userInfo.isAdmin()).isFalse(); 756 757 mUserManager.setUserAdmin(userInfo.id); 758 759 userInfo = mUserManager.getUserInfo(userInfo.id); 760 assertThat(userInfo.isAdmin()).isTrue(); 761 } 762 763 @MediumTest 764 @Test testRevokeUserAdmin()765 public void testRevokeUserAdmin() throws Exception { 766 UserInfo userInfo = createUser("Admin", /*flags=*/ UserInfo.FLAG_ADMIN); 767 assertThat(userInfo.isAdmin()).isTrue(); 768 769 mUserManager.revokeUserAdmin(userInfo.id); 770 771 userInfo = mUserManager.getUserInfo(userInfo.id); 772 assertThat(userInfo.isAdmin()).isFalse(); 773 } 774 775 @MediumTest 776 @Test testRevokeUserAdminFromNonAdmin()777 public void testRevokeUserAdminFromNonAdmin() throws Exception { 778 UserInfo userInfo = createUser("NonAdmin", /*flags=*/ 0); 779 assertThat(userInfo.isAdmin()).isFalse(); 780 781 mUserManager.revokeUserAdmin(userInfo.id); 782 783 userInfo = mUserManager.getUserInfo(userInfo.id); 784 assertThat(userInfo.isAdmin()).isFalse(); 785 } 786 787 @MediumTest 788 @Test testGetProfileParent()789 public void testGetProfileParent() throws Exception { 790 assumeManagedUsersSupported(); 791 int mainUserId = mUserManager.getMainUser().getIdentifier(); 792 UserInfo userInfo = createProfileForUser("Profile", 793 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 794 assertThat(userInfo).isNotNull(); 795 assertThat(mUserManager.getProfileParent(mainUserId)).isNull(); 796 UserInfo parentProfileInfo = mUserManager.getProfileParent(userInfo.id); 797 assertThat(parentProfileInfo).isNotNull(); 798 assertThat(mainUserId).isEqualTo(parentProfileInfo.id); 799 removeUser(userInfo.id); 800 assertThat(mUserManager.getProfileParent(mainUserId)).isNull(); 801 } 802 803 /** Test that UserManager returns the correct badge information for a managed profile. */ 804 @MediumTest 805 @Test testProfileTypeInformation()806 public void testProfileTypeInformation() throws Exception { 807 assumeManagedUsersSupported(); 808 final UserTypeDetails userTypeDetails = 809 UserTypeFactory.getUserTypes().get(UserManager.USER_TYPE_PROFILE_MANAGED); 810 assertWithMessage("No %s type on device", UserManager.USER_TYPE_PROFILE_MANAGED) 811 .that(userTypeDetails).isNotNull(); 812 assertThat(userTypeDetails.getName()).isEqualTo(UserManager.USER_TYPE_PROFILE_MANAGED); 813 814 int mainUserId = mUserManager.getMainUser().getIdentifier(); 815 UserInfo userInfo = createProfileForUser("Managed", 816 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 817 assertThat(userInfo).isNotNull(); 818 final int userId = userInfo.id; 819 820 assertThat(mUserManager.hasBadge(userId)).isEqualTo(userTypeDetails.hasBadge()); 821 assertThat(mUserManager.getUserIconBadgeResId(userId)) 822 .isEqualTo(userTypeDetails.getIconBadge()); 823 assertThat(mUserManager.getUserBadgeResId(userId)) 824 .isEqualTo(userTypeDetails.getBadgePlain()); 825 assertThat(mUserManager.getUserBadgeNoBackgroundResId(userId)) 826 .isEqualTo(userTypeDetails.getBadgeNoBackground()); 827 828 final int badgeIndex = userInfo.profileBadge; 829 assertThat(mUserManager.getUserBadgeColor(userId)).isEqualTo( 830 Resources.getSystem().getColor(userTypeDetails.getBadgeColor(badgeIndex), null)); 831 assertThat(mUserManager.getUserBadgeDarkColor(userId)).isEqualTo( 832 Resources.getSystem().getColor(userTypeDetails.getDarkThemeBadgeColor(badgeIndex), 833 null)); 834 835 assertThat(mUserManager.getBadgedLabelForUser("Test", asHandle(userId))).isEqualTo( 836 Resources.getSystem().getString(userTypeDetails.getBadgeLabel(badgeIndex), "Test")); 837 838 // Test @UserHandleAware methods 839 final UserManager userManagerForUser = UserManager.get(mContext.createPackageContextAsUser( 840 "android", 0, asHandle(userId))); 841 assertThat(userManagerForUser.isUserOfType(userTypeDetails.getName())).isTrue(); 842 assertThat(userManagerForUser.isProfile()).isEqualTo(userTypeDetails.isProfile()); 843 } 844 845 /** Test that UserManager returns the correct UserProperties for a new managed profile. */ 846 @MediumTest 847 @Test testUserProperties()848 public void testUserProperties() throws Exception { 849 assumeManagedUsersSupported(); 850 851 // Get the default properties for a user type. 852 final UserTypeDetails userTypeDetails = 853 UserTypeFactory.getUserTypes().get(UserManager.USER_TYPE_PROFILE_MANAGED); 854 assertWithMessage("No %s type on device", UserManager.USER_TYPE_PROFILE_MANAGED) 855 .that(userTypeDetails).isNotNull(); 856 final UserProperties typeProps = userTypeDetails.getDefaultUserPropertiesReference(); 857 858 // Create an actual user (of this user type) and get its properties. 859 int mainUserId = mUserManager.getMainUser().getIdentifier(); 860 final UserInfo userInfo = createProfileForUser("Managed", 861 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 862 assertThat(userInfo).isNotNull(); 863 final int userId = userInfo.id; 864 final UserProperties userProps = mUserManager.getUserProperties(UserHandle.of(userId)); 865 866 // Check that this new user has the expected properties (relative to the defaults) 867 // provided that the test caller has the necessary permissions. 868 assertThat(userProps.getShowInLauncher()).isEqualTo(typeProps.getShowInLauncher()); 869 assertThat(userProps.getShowInSettings()).isEqualTo(typeProps.getShowInSettings()); 870 assertThat(userProps.getUseParentsContacts()).isFalse(); 871 assertThrows(SecurityException.class, userProps::getCrossProfileIntentFilterAccessControl); 872 assertThrows(SecurityException.class, userProps::getCrossProfileIntentResolutionStrategy); 873 assertThrows(SecurityException.class, userProps::getStartWithParent); 874 assertThrows(SecurityException.class, userProps::getInheritDevicePolicy); 875 assertThat(userProps.isMediaSharedWithParent()).isFalse(); 876 assertThat(userProps.isCredentialShareableWithParent()).isTrue(); 877 assertThrows(SecurityException.class, userProps::getDeleteAppWithParent); 878 } 879 880 881 // Make sure only max managed profiles can be created 882 @MediumTest 883 @Test testAddManagedProfile()884 public void testAddManagedProfile() throws Exception { 885 assumeManagedUsersSupported(); 886 int mainUserId = mUserManager.getMainUser().getIdentifier(); 887 UserInfo userInfo1 = createProfileForUser("Managed 1", 888 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 889 UserInfo userInfo2 = createProfileForUser("Managed 2", 890 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 891 892 assertThat(userInfo1).isNotNull(); 893 assertThat(userInfo2).isNull(); 894 895 assertThat(userInfo1.userType).isEqualTo(UserManager.USER_TYPE_PROFILE_MANAGED); 896 int requiredFlags = UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_PROFILE; 897 assertWithMessage("Wrong flags %s", userInfo1.flags).that(userInfo1.flags & requiredFlags) 898 .isEqualTo(requiredFlags); 899 900 // Verify that current user is not a managed profile 901 assertThat(mUserManager.isManagedProfile()).isFalse(); 902 } 903 904 // Verify that disallowed packages are not installed in the managed profile. 905 @MediumTest 906 @Test testAddManagedProfile_withDisallowedPackages()907 public void testAddManagedProfile_withDisallowedPackages() throws Exception { 908 assumeManagedUsersSupported(); 909 int mainUserId = mUserManager.getMainUser().getIdentifier(); 910 UserInfo userInfo1 = createProfileForUser("Managed1", 911 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 912 // Verify that the packagesToVerify are installed by default. 913 for (String pkg : PACKAGES) { 914 if (!mPackageManager.isPackageAvailable(pkg)) { 915 Slog.w(TAG, "Package is not available " + pkg); 916 continue; 917 } 918 919 assertWithMessage("Package should be installed in managed profile: %s", pkg) 920 .that(isPackageInstalledForUser(pkg, userInfo1.id)).isTrue(); 921 } 922 removeUser(userInfo1.id); 923 924 UserInfo userInfo2 = createProfileForUser("Managed2", 925 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId, PACKAGES); 926 // Verify that the packagesToVerify are not installed by default. 927 for (String pkg : PACKAGES) { 928 if (!mPackageManager.isPackageAvailable(pkg)) { 929 Slog.w(TAG, "Package is not available " + pkg); 930 continue; 931 } 932 933 assertWithMessage( 934 "Package should not be installed in managed profile when disallowed: %s", pkg) 935 .that(isPackageInstalledForUser(pkg, userInfo2.id)).isFalse(); 936 } 937 } 938 939 // Verify that if any packages are disallowed to install during creation of managed profile can 940 // still be installed later. 941 @MediumTest 942 @Test testAddManagedProfile_disallowedPackagesInstalledLater()943 public void testAddManagedProfile_disallowedPackagesInstalledLater() throws Exception { 944 assumeManagedUsersSupported(); 945 final int mainUserId = mUserManager.getMainUser().getIdentifier(); 946 UserInfo userInfo = createProfileForUser("Managed", 947 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId, PACKAGES); 948 // Verify that the packagesToVerify are not installed by default. 949 for (String pkg : PACKAGES) { 950 if (!mPackageManager.isPackageAvailable(pkg)) { 951 Slog.w(TAG, "Package is not available " + pkg); 952 continue; 953 } 954 955 assertWithMessage("Pkg should not be installed in managed profile when disallowed: %s", 956 pkg).that(isPackageInstalledForUser(pkg, userInfo.id)).isFalse(); 957 } 958 959 // Verify that the disallowed packages during profile creation can be installed now. 960 for (String pkg : PACKAGES) { 961 if (!mPackageManager.isPackageAvailable(pkg)) { 962 Slog.w(TAG, "Package is not available " + pkg); 963 continue; 964 } 965 966 assertWithMessage("Package could not be installed: %s", pkg) 967 .that(mPackageManager.installExistingPackageAsUser(pkg, userInfo.id)) 968 .isEqualTo(PackageManager.INSTALL_SUCCEEDED); 969 } 970 } 971 972 // Make sure createUser would fail if we have DISALLOW_ADD_USER. 973 @MediumTest 974 @Test testCreateUser_disallowAddUser()975 public void testCreateUser_disallowAddUser() throws Exception { 976 final int creatorId = ActivityManager.getCurrentUser(); 977 final UserHandle creatorHandle = asHandle(creatorId); 978 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, true, creatorHandle); 979 try { 980 UserInfo createadInfo = createUser("SecondaryUser", /*flags=*/ 0); 981 assertThat(createadInfo).isNull(); 982 } finally { 983 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, false, 984 creatorHandle); 985 } 986 } 987 988 // Make sure createProfile would fail if we have DISALLOW_ADD_CLONE_PROFILE. 989 @MediumTest 990 @Test testCreateUser_disallowAddClonedUserProfile()991 public void testCreateUser_disallowAddClonedUserProfile() throws Exception { 992 final int mainUserId = ActivityManager.getCurrentUser(); 993 final UserHandle mainUserHandle = asHandle(mainUserId); 994 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_CLONE_PROFILE, 995 true, mainUserHandle); 996 try { 997 UserInfo cloneProfileUserInfo = createProfileForUser("Clone", 998 UserManager.USER_TYPE_PROFILE_CLONE, mainUserId); 999 assertThat(cloneProfileUserInfo).isNull(); 1000 } finally { 1001 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_CLONE_PROFILE, false, 1002 mainUserHandle); 1003 } 1004 } 1005 1006 // Make sure createProfile would fail if we have DISALLOW_ADD_MANAGED_PROFILE. 1007 @MediumTest 1008 @Test testCreateProfileForUser_disallowAddManagedProfile()1009 public void testCreateProfileForUser_disallowAddManagedProfile() throws Exception { 1010 assumeManagedUsersSupported(); 1011 final int mainUserId = mUserManager.getMainUser().getIdentifier(); 1012 final UserHandle mainUserHandle = asHandle(mainUserId); 1013 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, true, 1014 mainUserHandle); 1015 try { 1016 UserInfo userInfo = createProfileForUser("Managed", 1017 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 1018 assertThat(userInfo).isNull(); 1019 } finally { 1020 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, false, 1021 mainUserHandle); 1022 } 1023 } 1024 1025 // Make sure createProfileEvenWhenDisallowedForUser bypass DISALLOW_ADD_MANAGED_PROFILE. 1026 @MediumTest 1027 @Test testCreateProfileForUserEvenWhenDisallowed()1028 public void testCreateProfileForUserEvenWhenDisallowed() throws Exception { 1029 assumeManagedUsersSupported(); 1030 final int mainUserId = mUserManager.getMainUser().getIdentifier(); 1031 final UserHandle mainUserHandle = asHandle(mainUserId); 1032 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, true, 1033 mainUserHandle); 1034 try { 1035 UserInfo userInfo = createProfileEvenWhenDisallowedForUser("Managed", 1036 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 1037 assertThat(userInfo).isNotNull(); 1038 } finally { 1039 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, false, 1040 mainUserHandle); 1041 } 1042 } 1043 1044 // createProfile succeeds even if DISALLOW_ADD_USER is set 1045 @MediumTest 1046 @Test testCreateProfileForUser_disallowAddUser()1047 public void testCreateProfileForUser_disallowAddUser() throws Exception { 1048 assumeManagedUsersSupported(); 1049 final int mainUserId = mUserManager.getMainUser().getIdentifier(); 1050 final UserHandle mainUserHandle = asHandle(mainUserId); 1051 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, true, mainUserHandle); 1052 try { 1053 UserInfo userInfo = createProfileForUser("Managed", 1054 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 1055 assertThat(userInfo).isNotNull(); 1056 } finally { 1057 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, false, 1058 mainUserHandle); 1059 } 1060 } 1061 1062 @MediumTest 1063 @Test testAddRestrictedProfile()1064 public void testAddRestrictedProfile() throws Exception { 1065 if (isAutomotive() || UserManager.isHeadlessSystemUserMode()) return; 1066 assertWithMessage("There should be no associated restricted profiles before the test") 1067 .that(mUserManager.hasRestrictedProfiles()).isFalse(); 1068 UserInfo userInfo = createRestrictedProfile("Profile"); 1069 assertThat(userInfo).isNotNull(); 1070 1071 Bundle restrictions = mUserManager.getUserRestrictions(UserHandle.of(userInfo.id)); 1072 assertWithMessage( 1073 "Restricted profile should have DISALLOW_MODIFY_ACCOUNTS restriction by default") 1074 .that(restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) 1075 .isTrue(); 1076 assertWithMessage( 1077 "Restricted profile should have DISALLOW_SHARE_LOCATION restriction by default") 1078 .that(restrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION)) 1079 .isTrue(); 1080 1081 int locationMode = Settings.Secure.getIntForUser(mContext.getContentResolver(), 1082 Settings.Secure.LOCATION_MODE, 1083 Settings.Secure.LOCATION_MODE_HIGH_ACCURACY, 1084 userInfo.id); 1085 assertWithMessage("Restricted profile should have setting LOCATION_MODE set to " 1086 + "LOCATION_MODE_OFF by default").that(locationMode) 1087 .isEqualTo(Settings.Secure.LOCATION_MODE_OFF); 1088 1089 assertWithMessage("Newly created profile should be associated with the current user") 1090 .that(mUserManager.hasRestrictedProfiles()).isTrue(); 1091 } 1092 1093 @MediumTest 1094 @Test testGetManagedProfileCreationTime()1095 public void testGetManagedProfileCreationTime() throws Exception { 1096 assumeManagedUsersSupported(); 1097 final int mainUserId = mUserManager.getMainUser().getIdentifier(); 1098 final long startTime = System.currentTimeMillis(); 1099 UserInfo profile = createProfileForUser("Managed 1", 1100 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 1101 final long endTime = System.currentTimeMillis(); 1102 assertThat(profile).isNotNull(); 1103 if (System.currentTimeMillis() > EPOCH_PLUS_30_YEARS) { 1104 assertWithMessage("creationTime must be set when the profile is created") 1105 .that(profile.creationTime).isIn(Range.closed(startTime, endTime)); 1106 } else { 1107 assertWithMessage("creationTime must be 0 if the time is not > EPOCH_PLUS_30_years") 1108 .that(profile.creationTime).isEqualTo(0); 1109 } 1110 assertThat(mUserManager.getUserCreationTime(asHandle(profile.id))) 1111 .isEqualTo(profile.creationTime); 1112 1113 long ownerCreationTime = mUserManager.getUserInfo(mainUserId).creationTime; 1114 assertThat(mUserManager.getUserCreationTime(asHandle(mainUserId))) 1115 .isEqualTo(ownerCreationTime); 1116 } 1117 1118 @MediumTest 1119 @Test testGetUserCreationTime()1120 public void testGetUserCreationTime() throws Exception { 1121 long startTime = System.currentTimeMillis(); 1122 UserInfo user = createUser("User", /* flags= */ 0); 1123 long endTime = System.currentTimeMillis(); 1124 assertThat(user).isNotNull(); 1125 assertWithMessage("creationTime must be set when the user is created") 1126 .that(user.creationTime).isIn(Range.closed(startTime, endTime)); 1127 } 1128 1129 @SmallTest 1130 @Test testGetUserCreationTime_nonExistentUser()1131 public void testGetUserCreationTime_nonExistentUser() throws Exception { 1132 int noSuchUserId = 100500; 1133 assertThrows(SecurityException.class, 1134 () -> mUserManager.getUserCreationTime(asHandle(noSuchUserId))); 1135 } 1136 1137 @SmallTest 1138 @Test testGetUserCreationTime_otherUser()1139 public void testGetUserCreationTime_otherUser() throws Exception { 1140 UserInfo user = createUser("User 1", 0); 1141 assertThat(user).isNotNull(); 1142 assertThrows(SecurityException.class, 1143 () -> mUserManager.getUserCreationTime(asHandle(user.id))); 1144 } 1145 1146 @Nullable getUser(int id)1147 private UserInfo getUser(int id) { 1148 List<UserInfo> list = mUserManager.getUsers(); 1149 1150 for (UserInfo user : list) { 1151 if (user.id == id) { 1152 return user; 1153 } 1154 } 1155 return null; 1156 } 1157 hasUser(int id)1158 private boolean hasUser(int id) { 1159 return getUser(id) != null; 1160 } 1161 1162 @MediumTest 1163 @Test testSerialNumber()1164 public void testSerialNumber() { 1165 UserInfo user1 = createUser("User 1", 0); 1166 int serialNumber1 = user1.serialNumber; 1167 assertThat(mUserManager.getUserSerialNumber(user1.id)).isEqualTo(serialNumber1); 1168 assertThat(mUserManager.getUserHandle(serialNumber1)).isEqualTo(user1.id); 1169 UserInfo user2 = createUser("User 2", 0); 1170 int serialNumber2 = user2.serialNumber; 1171 assertThat(serialNumber1 == serialNumber2).isFalse(); 1172 assertThat(mUserManager.getUserSerialNumber(user2.id)).isEqualTo(serialNumber2); 1173 assertThat(mUserManager.getUserHandle(serialNumber2)).isEqualTo(user2.id); 1174 } 1175 1176 @MediumTest 1177 @Test testGetSerialNumbersOfUsers()1178 public void testGetSerialNumbersOfUsers() { 1179 UserInfo user1 = createUser("User 1", 0); 1180 UserInfo user2 = createUser("User 2", 0); 1181 long[] serialNumbersOfUsers = mUserManager.getSerialNumbersOfUsers(false); 1182 assertThat(serialNumbersOfUsers).asList().containsAtLeast( 1183 (long) user1.serialNumber, (long) user2.serialNumber); 1184 } 1185 1186 @MediumTest 1187 @Test testMaxUsers()1188 public void testMaxUsers() { 1189 int N = UserManager.getMaxSupportedUsers(); 1190 int count = mUserManager.getUsers().size(); 1191 // Create as many users as permitted and make sure creation passes 1192 while (count < N) { 1193 UserInfo ui = createUser("User " + count, 0); 1194 assertThat(ui).isNotNull(); 1195 count++; 1196 } 1197 // Try to create one more user and make sure it fails 1198 UserInfo extra = createUser("One more", 0); 1199 assertThat(extra).isNull(); 1200 } 1201 1202 @MediumTest 1203 @Test testGetUserCount()1204 public void testGetUserCount() { 1205 int count = mUserManager.getUsers().size(); 1206 UserInfo user1 = createUser("User 1", 0); 1207 assertThat(user1).isNotNull(); 1208 UserInfo user2 = createUser("User 2", 0); 1209 assertThat(user2).isNotNull(); 1210 assertThat(mUserManager.getUserCount()).isEqualTo(count + 2); 1211 } 1212 1213 @MediumTest 1214 @Test testRestrictions()1215 public void testRestrictions() { 1216 UserInfo testUser = createUser("User 1", 0); 1217 1218 mUserManager.setUserRestriction( 1219 UserManager.DISALLOW_INSTALL_APPS, true, asHandle(testUser.id)); 1220 mUserManager.setUserRestriction( 1221 UserManager.DISALLOW_CONFIG_WIFI, false, asHandle(testUser.id)); 1222 1223 Bundle stored = mUserManager.getUserRestrictions(asHandle(testUser.id)); 1224 // Note this will fail if DO already sets those restrictions. 1225 assertThat(stored.getBoolean(UserManager.DISALLOW_CONFIG_WIFI)).isFalse(); 1226 assertThat(stored.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS)).isFalse(); 1227 assertThat(stored.getBoolean(UserManager.DISALLOW_INSTALL_APPS)).isTrue(); 1228 } 1229 1230 @MediumTest 1231 @Test testDefaultRestrictionsApplied()1232 public void testDefaultRestrictionsApplied() throws Exception { 1233 final UserInfo userInfo = createUser("Useroid", UserManager.USER_TYPE_FULL_SECONDARY, 0); 1234 final UserTypeDetails userTypeDetails = 1235 UserTypeFactory.getUserTypes().get(UserManager.USER_TYPE_FULL_SECONDARY); 1236 final Bundle expectedRestrictions = userTypeDetails.getDefaultRestrictions(); 1237 // Note this can fail if DO unset those restrictions. 1238 for (String restriction : expectedRestrictions.keySet()) { 1239 if (expectedRestrictions.getBoolean(restriction)) { 1240 assertThat(mUserManager.hasUserRestriction(restriction, UserHandle.of(userInfo.id))) 1241 .isTrue(); 1242 } 1243 } 1244 } 1245 1246 @MediumTest 1247 @Test testSetDefaultGuestRestrictions()1248 public void testSetDefaultGuestRestrictions() { 1249 final Bundle origGuestRestrictions = mUserManager.getDefaultGuestRestrictions(); 1250 Bundle restrictions = new Bundle(); 1251 restrictions.putBoolean(UserManager.DISALLOW_FUN, true); 1252 mUserManager.setDefaultGuestRestrictions(restrictions); 1253 1254 try { 1255 UserInfo guest = createUser("Guest", UserInfo.FLAG_GUEST); 1256 assertThat(guest).isNotNull(); 1257 assertThat(mUserManager.hasUserRestriction(UserManager.DISALLOW_FUN, 1258 guest.getUserHandle())).isTrue(); 1259 } finally { 1260 mUserManager.setDefaultGuestRestrictions(origGuestRestrictions); 1261 } 1262 } 1263 1264 @Test testGetUserSwitchability()1265 public void testGetUserSwitchability() { 1266 int userSwitchable = mUserManager.getUserSwitchability(); 1267 assertWithMessage("Expected users to be switchable").that(userSwitchable) 1268 .isEqualTo(UserManager.SWITCHABILITY_STATUS_OK); 1269 } 1270 1271 @LargeTest 1272 @Test testSwitchUser()1273 public void testSwitchUser() { 1274 final int startUser = ActivityManager.getCurrentUser(); 1275 UserInfo user = createUser("User", 0); 1276 assertThat(user).isNotNull(); 1277 // Switch to the user just created. 1278 switchUser(user.id); 1279 // Switch back to the starting user. 1280 switchUser(startUser); 1281 } 1282 1283 @LargeTest 1284 @Test testSwitchUserByHandle()1285 public void testSwitchUserByHandle() { 1286 final int startUser = ActivityManager.getCurrentUser(); 1287 UserInfo user = createUser("User", 0); 1288 assertThat(user).isNotNull(); 1289 // Switch to the user just created. 1290 switchUser(user.getUserHandle()); 1291 // Switch back to the starting user. 1292 switchUser(UserHandle.of(startUser)); 1293 } 1294 1295 @Test testSwitchUserByHandle_ThrowsException()1296 public void testSwitchUserByHandle_ThrowsException() { 1297 assertThrows(IllegalArgumentException.class, () -> mActivityManager.switchUser(null)); 1298 } 1299 1300 @MediumTest 1301 @Test testConcurrentUserCreate()1302 public void testConcurrentUserCreate() throws Exception { 1303 int userCount = mUserManager.getUsers().size(); 1304 int maxSupportedUsers = UserManager.getMaxSupportedUsers(); 1305 int canBeCreatedCount = maxSupportedUsers - userCount; 1306 // Test exceeding the limit while running in parallel 1307 int createUsersCount = canBeCreatedCount + 5; 1308 ExecutorService es = Executors.newCachedThreadPool(); 1309 AtomicInteger created = new AtomicInteger(); 1310 for (int i = 0; i < createUsersCount; i++) { 1311 final String userName = "testConcUser" + i; 1312 es.submit(() -> { 1313 UserInfo user = mUserManager.createUser(userName, 0); 1314 if (user != null) { 1315 created.incrementAndGet(); 1316 mUsersToRemove.add(user.id); 1317 } 1318 }); 1319 } 1320 es.shutdown(); 1321 int timeout = createUsersCount * 20; 1322 assertWithMessage( 1323 "Could not create " + createUsersCount + " users in " + timeout + " seconds") 1324 .that(es.awaitTermination(timeout, TimeUnit.SECONDS)) 1325 .isTrue(); 1326 assertThat(mUserManager.getUsers().size()).isEqualTo(maxSupportedUsers); 1327 assertThat(created.get()).isEqualTo(canBeCreatedCount); 1328 } 1329 1330 @MediumTest 1331 @Test testGetUserHandles_createNewUser_shouldFindNewUser()1332 public void testGetUserHandles_createNewUser_shouldFindNewUser() { 1333 UserInfo user = createUser("Guest 1", UserManager.USER_TYPE_FULL_GUEST, /*flags*/ 0); 1334 1335 boolean found = false; 1336 List<UserHandle> userHandles = mUserManager.getUserHandles(/* excludeDying= */ true); 1337 for (UserHandle userHandle: userHandles) { 1338 if (userHandle.getIdentifier() == user.id) { 1339 found = true; 1340 } 1341 } 1342 1343 assertThat(found).isTrue(); 1344 } 1345 1346 @Test testCreateProfile_withContextUserId()1347 public void testCreateProfile_withContextUserId() throws Exception { 1348 assumeManagedUsersSupported(); 1349 final int mainUserId = mUserManager.getMainUser().getIdentifier(); 1350 1351 UserInfo userProfile = createProfileForUser("Managed 1", 1352 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 1353 assertThat(userProfile).isNotNull(); 1354 1355 UserManager um = (UserManager) mContext.createPackageContextAsUser( 1356 "android", 0, mUserManager.getMainUser()) 1357 .getSystemService(Context.USER_SERVICE); 1358 1359 List<UserHandle> profiles = um.getAllProfiles(); 1360 assertThat(profiles.size()).isEqualTo(2); 1361 assertThat(profiles.get(0).equals(userProfile.getUserHandle()) 1362 || profiles.get(1).equals(userProfile.getUserHandle())).isTrue(); 1363 } 1364 1365 @Test testSetUserName_withContextUserId()1366 public void testSetUserName_withContextUserId() throws Exception { 1367 assumeManagedUsersSupported(); 1368 final int mainUserId = mUserManager.getMainUser().getIdentifier(); 1369 1370 UserInfo userInfo1 = createProfileForUser("Managed 1", 1371 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 1372 assertThat(userInfo1).isNotNull(); 1373 1374 UserManager um = (UserManager) mContext.createPackageContextAsUser( 1375 "android", 0, userInfo1.getUserHandle()) 1376 .getSystemService(Context.USER_SERVICE); 1377 1378 final String newName = "Managed_user 1"; 1379 um.setUserName(newName); 1380 1381 UserInfo userInfo = mUserManager.getUserInfo(userInfo1.id); 1382 assertThat(userInfo.name).isEqualTo(newName); 1383 1384 // get user name from getUserName using context.getUserId 1385 assertThat(um.getUserName()).isEqualTo(newName); 1386 } 1387 1388 @Test testGetUserName_withContextUserId()1389 public void testGetUserName_withContextUserId() throws Exception { 1390 final String userName = "User 2"; 1391 UserInfo user2 = createUser(userName, 0); 1392 assertThat(user2).isNotNull(); 1393 1394 UserManager um = (UserManager) mContext.createPackageContextAsUser( 1395 "android", 0, user2.getUserHandle()) 1396 .getSystemService(Context.USER_SERVICE); 1397 1398 assertThat(um.getUserName()).isEqualTo(userName); 1399 } 1400 1401 @Test testGetUserName_shouldReturnTranslatedTextForNullNamedGuestUser()1402 public void testGetUserName_shouldReturnTranslatedTextForNullNamedGuestUser() throws Exception { 1403 UserInfo guestWithNullName = createUser(null, UserManager.USER_TYPE_FULL_GUEST, 0); 1404 assertThat(guestWithNullName).isNotNull(); 1405 1406 UserManager um = (UserManager) mContext.createPackageContextAsUser( 1407 "android", 0, guestWithNullName.getUserHandle()) 1408 .getSystemService(Context.USER_SERVICE); 1409 1410 assertThat(um.getUserName()).isEqualTo( 1411 mContext.getString(com.android.internal.R.string.guest_name)); 1412 } 1413 1414 @Test testGetUserIcon_withContextUserId()1415 public void testGetUserIcon_withContextUserId() throws Exception { 1416 assumeManagedUsersSupported(); 1417 final int mainUserId = mUserManager.getMainUser().getIdentifier(); 1418 1419 UserInfo userInfo1 = createProfileForUser("Managed 1", 1420 UserManager.USER_TYPE_PROFILE_MANAGED, mainUserId); 1421 assertThat(userInfo1).isNotNull(); 1422 1423 UserManager um = (UserManager) mContext.createPackageContextAsUser( 1424 "android", 0, userInfo1.getUserHandle()) 1425 .getSystemService(Context.USER_SERVICE); 1426 1427 final String newName = "Managed_user 1"; 1428 um.setUserName(newName); 1429 1430 UserInfo userInfo = mUserManager.getUserInfo(userInfo1.id); 1431 assertThat(userInfo.name).isEqualTo(newName); 1432 } 1433 isPackageInstalledForUser(String packageName, int userId)1434 private boolean isPackageInstalledForUser(String packageName, int userId) { 1435 try { 1436 return mPackageManager.getPackageInfoAsUser(packageName, 0, userId) != null; 1437 } catch (PackageManager.NameNotFoundException e) { 1438 return false; 1439 } 1440 } 1441 1442 /** 1443 * Starts the given user in the foreground. And waits for the user switch to be complete. 1444 **/ switchUser(UserHandle user)1445 private void switchUser(UserHandle user) { 1446 final int userId = user.getIdentifier(); 1447 Slog.d(TAG, "Switching to user " + userId); 1448 1449 mUserSwitchWaiter.runThenWaitUntilSwitchCompleted(userId, () -> { 1450 assertWithMessage("Could not start switching to user " + userId) 1451 .that(mActivityManager.switchUser(user)).isTrue(); 1452 }, /* onFail= */ () -> { 1453 throw new AssertionError("Could not complete switching to user " + userId); 1454 }); 1455 } 1456 1457 /** 1458 * Starts the given user in the foreground. And waits for the user switch to be complete. 1459 **/ switchUser(int userId)1460 private void switchUser(int userId) { 1461 switchUserThenRun(userId, null); 1462 } 1463 1464 /** 1465 * Starts the given user in the foreground. And runs the given Runnable right after 1466 * am.switchUser call, before waiting for the actual user switch to be complete. 1467 **/ switchUserThenRun(int userId, Runnable runAfterSwitchBeforeWait)1468 private void switchUserThenRun(int userId, Runnable runAfterSwitchBeforeWait) { 1469 Slog.d(TAG, "Switching to user " + userId); 1470 mUserSwitchWaiter.runThenWaitUntilSwitchCompleted(userId, () -> { 1471 // Start switching to user 1472 assertWithMessage("Could not start switching to user " + userId) 1473 .that(mActivityManager.switchUser(userId)).isTrue(); 1474 1475 // While the user switch is happening, call runAfterSwitchBeforeWait. 1476 if (runAfterSwitchBeforeWait != null) { 1477 runAfterSwitchBeforeWait.run(); 1478 } 1479 }, () -> fail("Could not complete switching to user " + userId)); 1480 } 1481 removeUser(UserHandle userHandle)1482 private void removeUser(UserHandle userHandle) { 1483 mUserManager.removeUser(userHandle); 1484 waitForUserRemoval(userHandle.getIdentifier()); 1485 } 1486 removeUser(int userId)1487 private void removeUser(int userId) { 1488 mUserManager.removeUser(userId); 1489 waitForUserRemoval(userId); 1490 } 1491 waitForUserRemoval(int userId)1492 private void waitForUserRemoval(int userId) { 1493 mUserRemovalWaiter.waitFor(userId); 1494 mUsersToRemove.remove(userId); 1495 } 1496 createUser(String name, int flags)1497 private UserInfo createUser(String name, int flags) { 1498 UserInfo user = mUserManager.createUser(name, flags); 1499 if (user != null) { 1500 mUsersToRemove.add(user.id); 1501 } 1502 return user; 1503 } 1504 createUser(String name, String userType, int flags)1505 private UserInfo createUser(String name, String userType, int flags) { 1506 UserInfo user = mUserManager.createUser(name, userType, flags); 1507 if (user != null) { 1508 mUsersToRemove.add(user.id); 1509 } 1510 return user; 1511 } 1512 createProfileForUser(String name, String userType, int userHandle)1513 private UserInfo createProfileForUser(String name, String userType, int userHandle) { 1514 return createProfileForUser(name, userType, userHandle, null); 1515 } 1516 createProfileForUser(String name, String userType, int userHandle, String[] disallowedPackages)1517 private UserInfo createProfileForUser(String name, String userType, int userHandle, 1518 String[] disallowedPackages) { 1519 UserInfo profile = mUserManager.createProfileForUser( 1520 name, userType, 0, userHandle, disallowedPackages); 1521 if (profile != null) { 1522 mUsersToRemove.add(profile.id); 1523 } 1524 return profile; 1525 } 1526 createProfileEvenWhenDisallowedForUser(String name, String userType, int userHandle)1527 private UserInfo createProfileEvenWhenDisallowedForUser(String name, String userType, 1528 int userHandle) { 1529 UserInfo profile = mUserManager.createProfileForUserEvenWhenDisallowed( 1530 name, userType, 0, userHandle, null); 1531 if (profile != null) { 1532 mUsersToRemove.add(profile.id); 1533 } 1534 return profile; 1535 } 1536 createRestrictedProfile(String name)1537 private UserInfo createRestrictedProfile(String name) { 1538 UserInfo profile = mUserManager.createRestrictedProfile(name); 1539 if (profile != null) { 1540 mUsersToRemove.add(profile.id); 1541 } 1542 return profile; 1543 } 1544 assumeManagedUsersSupported()1545 private void assumeManagedUsersSupported() { 1546 // In Automotive, if headless system user is enabled, a managed user cannot be created 1547 // under a primary user. 1548 assumeTrue("device doesn't support managed users", 1549 mPackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS) 1550 && (!isAutomotive() || !UserManager.isHeadlessSystemUserMode())); 1551 } 1552 assumeHeadlessModeEnabled()1553 private void assumeHeadlessModeEnabled() { 1554 // assume headless mode is enabled 1555 assumeTrue("Device doesn't have headless mode enabled", 1556 UserManager.isHeadlessSystemUserMode()); 1557 } 1558 assumeCloneEnabled()1559 private void assumeCloneEnabled() { 1560 // assume clone profile is supported on the device 1561 assumeTrue("Device doesn't support clone profiles ", 1562 mUserManager.isUserTypeEnabled(UserManager.USER_TYPE_PROFILE_CLONE)); 1563 } 1564 isAutomotive()1565 private boolean isAutomotive() { 1566 return mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); 1567 } 1568 asHandle(int userId)1569 private static UserHandle asHandle(int userId) { 1570 return new UserHandle(userId); 1571 } 1572 isMainUserPermanentAdmin()1573 private boolean isMainUserPermanentAdmin() { 1574 return Resources.getSystem() 1575 .getBoolean(com.android.internal.R.bool.config_isMainUserPermanentAdmin); 1576 } 1577 1578 } 1579