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.settings.testutils; 18 19 import android.annotation.UserIdInt; 20 import android.content.pm.UserInfo; 21 import android.graphics.Bitmap; 22 import android.os.UserHandle; 23 import android.os.UserManager; 24 import android.util.ArrayMap; 25 26 import org.robolectric.annotation.Implementation; 27 import org.robolectric.annotation.Implements; 28 import org.robolectric.annotation.Resetter; 29 30 import java.util.ArrayList; 31 import java.util.Collections; 32 import java.util.List; 33 import java.util.Map; 34 35 @Implements(UserManager.class) 36 public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager { 37 38 private static boolean sIsHeadlessSystemUserMode = true; 39 private static boolean sCanAddMoreUsers = true; 40 private static Map<Integer, List<UserInfo>> sProfiles = new ArrayMap<>(); 41 private static Map<Integer, Bitmap> sUserIcons = new ArrayMap<>(); 42 43 @Implementation getProfileIdsWithDisabled(int userId)44 protected int[] getProfileIdsWithDisabled(int userId) { 45 if (sProfiles.containsKey(userId)) { 46 return sProfiles.get(userId).stream().mapToInt(userInfo -> userInfo.id).toArray(); 47 } 48 return new int[]{}; 49 } 50 51 @Implementation getProfiles(int userHandle)52 protected List<UserInfo> getProfiles(int userHandle) { 53 if (sProfiles.containsKey(userHandle)) { 54 return new ArrayList<>(sProfiles.get(userHandle)); 55 } 56 return Collections.emptyList(); 57 } 58 59 /** Adds a profile to be returned by {@link #getProfiles(int)}. **/ addProfile( int userHandle, int profileUserHandle, String profileName, int profileFlags)60 public void addProfile( 61 int userHandle, int profileUserHandle, String profileName, int profileFlags) { 62 sProfiles.putIfAbsent(userHandle, new ArrayList<>()); 63 sProfiles.get(userHandle).add(new UserInfo(profileUserHandle, profileName, profileFlags)); 64 } 65 66 @Implementation setUserRestriction(String key, boolean value, UserHandle userHandle)67 protected void setUserRestriction(String key, boolean value, UserHandle userHandle) { 68 setUserRestriction(userHandle, key, value); 69 } 70 71 @Implementation getUsers(boolean excludeDying)72 protected List<UserInfo> getUsers(boolean excludeDying) { 73 return super.getUsers(); 74 } 75 76 @Implementation isHeadlessSystemUserMode()77 protected static boolean isHeadlessSystemUserMode() { 78 return sIsHeadlessSystemUserMode; 79 } 80 setIsHeadlessSystemUserMode(boolean isEnabled)81 public static void setIsHeadlessSystemUserMode(boolean isEnabled) { 82 sIsHeadlessSystemUserMode = isEnabled; 83 } 84 85 @Implementation canAddMoreUsers()86 protected static boolean canAddMoreUsers() { 87 return sCanAddMoreUsers; 88 } 89 setCanAddMoreUsers(boolean isEnabled)90 public static void setCanAddMoreUsers(boolean isEnabled) { 91 sCanAddMoreUsers = isEnabled; 92 } 93 94 @Implementation getUserIcon(@serIdInt int userId)95 public Bitmap getUserIcon(@UserIdInt int userId) { 96 return sUserIcons.get(userId); 97 } 98 setUserIcon(@serIdInt int userId, Bitmap icon)99 public static void setUserIcon(@UserIdInt int userId, Bitmap icon) { 100 sUserIcons.put(userId, icon); 101 } 102 103 @Resetter reset()104 public static void reset() { 105 org.robolectric.shadows.ShadowUserManager.reset(); 106 sIsHeadlessSystemUserMode = true; 107 sCanAddMoreUsers = true; 108 sProfiles.clear(); 109 sUserIcons.clear(); 110 } 111 } 112