1 /* 2 * Copyright (C) 2019 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.testing.shadows; 18 19 import android.annotation.NonNull; 20 import android.annotation.UserIdInt; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 24 import org.robolectric.annotation.Implementation; 25 import org.robolectric.annotation.Implements; 26 27 import java.util.HashMap; 28 import java.util.HashSet; 29 import java.util.Map; 30 import java.util.Set; 31 32 /** Shadow for {@link UserManager}. */ 33 @Implements(UserManager.class) 34 public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager { 35 private final Map<Integer, Set<Integer>> profileIds = new HashMap<>(); 36 37 /** @see UserManager#isUserUnlocked() */ 38 @Implementation isUserUnlocked(@serIdInt int userId)39 public boolean isUserUnlocked(@UserIdInt int userId) { 40 return false; 41 } 42 43 /** @see UserManager#getProfileIds(int, boolean) () */ 44 @Implementation 45 @NonNull getProfileIds(@serIdInt int userId, boolean enabledOnly)46 public int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) { 47 // Currently, enabledOnly is ignored. 48 if (!profileIds.containsKey(userId)) { 49 return new int[] {userId}; 50 } 51 return profileIds.get(userId).stream().mapToInt(Number::intValue).toArray(); 52 } 53 54 /** @see UserManager#getMainUser() */ 55 @Implementation getMainUser()56 public UserHandle getMainUser() { 57 return null; 58 } 59 60 /** Add a collection of profile IDs, all within the same profile group. */ addProfileIds(@serIdInt int... userIds)61 public void addProfileIds(@UserIdInt int... userIds) { 62 final Set<Integer> profileGroup = new HashSet<>(); 63 for (int userId : userIds) { 64 profileGroup.add(userId); 65 profileIds.put(userId, profileGroup); 66 } 67 } 68 } 69