1 /* 2 * Copyright (C) 2018 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.car.testutils.shadow; 18 19 import android.content.Context; 20 import android.content.pm.UserInfo; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 24 import java.util.ArrayList; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 29 import org.robolectric.RuntimeEnvironment; 30 import org.robolectric.annotation.HiddenApi; 31 import org.robolectric.annotation.Implementation; 32 import org.robolectric.annotation.Implements; 33 import org.robolectric.shadow.api.Shadow; 34 35 @Implements(UserManager.class) 36 public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager { 37 private final Map<Integer, UserInfo> mUserInfos = new HashMap<>(); 38 private int mCurrentUser = UserHandle.USER_SYSTEM; 39 public boolean isSystemUser = true; 40 public boolean isPrimaryUser = true; 41 public boolean isLinkedUser = false; 42 public boolean isDemoUser = false; 43 public boolean isManagedProfile = false; 44 public boolean isGuestUser = false; 45 public boolean canSwitchUser = true; 46 47 { addUserInfo(new UserInfo(UserHandle.USER_SYSTEM, "system_user", 0))48 addUserInfo(new UserInfo(UserHandle.USER_SYSTEM, "system_user", 0)); 49 } 50 51 /** 52 * Used by BaseActivity when creating intents. 53 */ 54 @Implementation 55 @HiddenApi getUsers()56 public List<UserInfo> getUsers() { 57 return new ArrayList<>(mUserInfos.values()); 58 } 59 60 @Implementation isSystemUser()61 public boolean isSystemUser() { 62 return isSystemUser; 63 } 64 65 @Implementation isPrimaryUser()66 public boolean isPrimaryUser() { 67 return isPrimaryUser; 68 } 69 70 @Implementation isLinkedUser()71 public boolean isLinkedUser() { 72 return isLinkedUser; 73 } 74 75 @Implementation isDemoUser()76 public boolean isDemoUser() { 77 return isDemoUser; 78 } 79 80 @Implementation isGuestUser()81 public boolean isGuestUser() { 82 return isGuestUser; 83 } 84 85 @Implementation isManagedProfile(int userId)86 public boolean isManagedProfile(int userId) { 87 return isManagedProfile; 88 } 89 90 @Implementation isSplitSystemUser()91 public static boolean isSplitSystemUser() { 92 return false; 93 } 94 95 @Implementation getUserInfo(int id)96 public UserInfo getUserInfo(int id) { 97 return mUserInfos.get(id); 98 } 99 100 @Implementation isUserUnlockingOrUnlocked(int userId)101 public boolean isUserUnlockingOrUnlocked(int userId) { 102 return isUserUnlocked(); 103 } 104 105 @Implementation canSwitchUsers()106 public boolean canSwitchUsers() { 107 return canSwitchUser; 108 } 109 110 @Implementation removeUser(int userId)111 public boolean removeUser(int userId) { 112 mUserInfos.remove(userId); 113 return true; 114 } 115 116 @Implementation createGuest(Context context, String name)117 public UserInfo createGuest(Context context, String name) { 118 UserInfo guest = new UserInfo(12, name, UserInfo.FLAG_GUEST); 119 120 addUserInfo(guest); 121 return guest; 122 } 123 switchUser(int userId)124 public void switchUser(int userId) { 125 if (!mUserInfos.containsKey(userId)) { 126 throw new UnsupportedOperationException("Must add user before switching to it"); 127 } 128 mCurrentUser = userId; 129 } 130 getCurrentUser()131 public int getCurrentUser() { 132 return mCurrentUser; 133 } 134 setCurrentUser(int userId)135 public void setCurrentUser(int userId) { 136 mCurrentUser = userId; 137 } 138 addUserInfo(UserInfo userInfo)139 public void addUserInfo(UserInfo userInfo) { 140 mUserInfos.put(userInfo.id, userInfo); 141 } 142 getShadow()143 public static ShadowUserManager getShadow() { 144 return (ShadowUserManager) Shadow.extract( 145 RuntimeEnvironment.application.getSystemService(UserManager.class)); 146 } 147 } 148