1 /* 2 * Copyright (C) 2021 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.car.user; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.annotation.UserIdInt; 21 import android.car.builtin.os.UserManagerHelper; 22 import android.content.Context; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 26 import java.util.List; 27 28 /** 29 * Helper class to get User properties using UserHandle 30 */ 31 //TODO(b/214340360) : Add unit test 32 public final class UserHandleHelper { 33 34 private final Context mContext; 35 private final UserManager mUserManager; 36 UserHandleHelper(@onNull Context context, @NonNull UserManager userManager)37 public UserHandleHelper(@NonNull Context context, @NonNull UserManager userManager) { 38 mContext = context; 39 mUserManager = userManager; 40 } 41 42 /** 43 * Gets user handle if user exists 44 */ 45 @Nullable getExistingUserHandle(@serIdInt int userId)46 public UserHandle getExistingUserHandle(@UserIdInt int userId) { 47 List<UserHandle> users = UserManagerHelper.getUserHandles(mUserManager, 48 /* excludeDying= */ false); 49 50 for (UserHandle user : users) { 51 if (user.getIdentifier() == userId) return user; 52 } 53 return null; 54 } 55 56 /** 57 * Get enabled profiles 58 */ 59 @NonNull getEnabledProfiles(@serIdInt int userId)60 public List<UserHandle> getEnabledProfiles(@UserIdInt int userId) { 61 return getUserContextAwareUserManager(userId).getEnabledProfiles(); 62 } 63 64 /** 65 * Is User a guest user? 66 */ isGuestUser(UserHandle user)67 public boolean isGuestUser(UserHandle user) { 68 return getUserContextAwareUserManager(user.getIdentifier()).isGuestUser(); 69 } 70 71 /** 72 * Is User an admin user? 73 */ isAdminUser(UserHandle user)74 public boolean isAdminUser(UserHandle user) { 75 return getUserContextAwareUserManager(user.getIdentifier()).isAdminUser(); 76 } 77 78 /** 79 * Is User an ephemeral user? 80 */ isEphemeralUser(UserHandle user)81 public boolean isEphemeralUser(UserHandle user) { 82 return UserManagerHelper.isEphemeralUser(mUserManager, user); 83 } 84 85 /** 86 * Is User enabled? 87 */ isEnabledUser(UserHandle user)88 public boolean isEnabledUser(UserHandle user) { 89 return UserManagerHelper.isEnabledUser(mUserManager, user); 90 } 91 92 /** 93 * Is User a managed profile? 94 */ isManagedProfile(UserHandle user)95 public boolean isManagedProfile(UserHandle user) { 96 return mUserManager.isManagedProfile(user.getIdentifier()); 97 } 98 99 /** 100 * Is user for a profile? 101 */ isProfileUser(UserHandle user)102 public boolean isProfileUser(UserHandle user) { 103 return getUserContextAwareUserManager(user.getIdentifier()).isProfile(); 104 } 105 106 /** 107 * Is User Initialized? 108 */ isInitializedUser(UserHandle user)109 public boolean isInitializedUser(UserHandle user) { 110 return UserManagerHelper.isInitializedUser(mUserManager, user); 111 } 112 getUserContextAwareUserManager(@serIdInt int userId)113 private UserManager getUserContextAwareUserManager(@UserIdInt int userId) { 114 Context userContext = mContext.createContextAsUser(UserHandle.of(userId), /* flags= */ 0); 115 return userContext.getSystemService(UserManager.class); 116 } 117 } 118