1 /*
2  * Copyright (C) 2016 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.settings.testutils.shadow;
18 
19 import android.annotation.UserIdInt;
20 import android.content.pm.UserInfo;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 import android.os.UserManager.EnforcingUser;
24 
25 import com.google.android.collect.Maps;
26 
27 import org.robolectric.RuntimeEnvironment;
28 import org.robolectric.annotation.Implementation;
29 import org.robolectric.annotation.Implements;
30 import org.robolectric.shadow.api.Shadow;
31 import org.robolectric.annotation.Resetter;
32 
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40 
41 @Implements(value = UserManager.class)
42 public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager {
43 
44     private static boolean sIsSupportsMultipleUsers;
45 
46     private final List<String> mRestrictions = new ArrayList<>();
47     private final Map<String, List<EnforcingUser>> mRestrictionSources = new HashMap<>();
48     private final List<UserInfo> mUserProfileInfos = new ArrayList<>();
49     private final Set<Integer> mManagedProfiles = new HashSet<>();
50     private boolean mIsQuietModeEnabled = false;
51     private int[] profileIdsForUser = new int[0];
52     private boolean mUserSwitchEnabled;
53     private final Map<Integer, Integer> mSameProfileGroupIds = Maps.newHashMap();
54 
addProfile(UserInfo userInfo)55     public void addProfile(UserInfo userInfo) {
56         mUserProfileInfos.add(userInfo);
57     }
58 
59     @Resetter
reset()60     public static void reset() {
61         sIsSupportsMultipleUsers = false;
62     }
63 
64     @Implementation
getProfiles(@serIdInt int userHandle)65     protected List<UserInfo> getProfiles(@UserIdInt int userHandle) {
66         return mUserProfileInfos;
67     }
68 
69     @Implementation
getProfileIds(@serIdInt int userHandle, boolean enabledOnly)70     protected int[] getProfileIds(@UserIdInt int userHandle, boolean enabledOnly) {
71         int[] ids = new int[mUserProfileInfos.size()];
72         for (int i = 0; i < mUserProfileInfos.size(); i++) {
73             ids[i] = mUserProfileInfos.get(i).id;
74         }
75         return ids;
76     }
77 
78     @Implementation
getCredentialOwnerProfile(@serIdInt int userHandle)79     protected int getCredentialOwnerProfile(@UserIdInt int userHandle) {
80         return userHandle;
81     }
82 
83     @Implementation
hasBaseUserRestriction(String restrictionKey, UserHandle userHandle)84     protected boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
85         return mRestrictions.contains(restrictionKey);
86     }
87 
addBaseUserRestriction(String restriction)88     public void addBaseUserRestriction(String restriction) {
89         mRestrictions.add(restriction);
90     }
91 
getShadow()92     public static ShadowUserManager getShadow() {
93         return (ShadowUserManager) Shadow.extract(
94                 RuntimeEnvironment.application.getSystemService(UserManager.class));
95     }
96 
97     @Implementation
getUserRestrictionSources( String restrictionKey, UserHandle userHandle)98     protected List<EnforcingUser> getUserRestrictionSources(
99             String restrictionKey, UserHandle userHandle) {
100         // Return empty list when there is no enforcing user, otherwise might trigger
101         // NullPointer Exception in RestrictedLockUtils.checkIfRestrictionEnforced.
102         List<EnforcingUser> enforcingUsers =
103                 mRestrictionSources.get(restrictionKey + userHandle.getIdentifier());
104         return enforcingUsers == null ? Collections.emptyList() : enforcingUsers;
105     }
106 
setUserRestrictionSources( String restrictionKey, UserHandle userHandle, List<EnforcingUser> enforcers)107     public void setUserRestrictionSources(
108             String restrictionKey, UserHandle userHandle, List<EnforcingUser> enforcers) {
109         mRestrictionSources.put(restrictionKey + userHandle.getIdentifier(), enforcers);
110     }
111 
112     @Implementation
isQuietModeEnabled(UserHandle userHandle)113     protected boolean isQuietModeEnabled(UserHandle userHandle) {
114         return mIsQuietModeEnabled;
115     }
116 
setQuietModeEnabled(boolean enabled)117     public void setQuietModeEnabled(boolean enabled) {
118         mIsQuietModeEnabled = enabled;
119     }
120 
121     @Implementation
getProfileIdsWithDisabled(@serIdInt int userId)122     protected int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
123         return profileIdsForUser;
124     }
125 
setProfileIdsWithDisabled(int[] profileIds)126     public void setProfileIdsWithDisabled(int[] profileIds) {
127         profileIdsForUser = profileIds;
128     }
129 
130     @Implementation
isUserSwitcherEnabled()131     protected boolean isUserSwitcherEnabled() {
132         return mUserSwitchEnabled;
133     }
134 
setUserSwitcherEnabled(boolean userSwitchEnabled)135     public void setUserSwitcherEnabled(boolean userSwitchEnabled) {
136         mUserSwitchEnabled = userSwitchEnabled;
137     }
138 
139     @Implementation
supportsMultipleUsers()140     protected static boolean supportsMultipleUsers() {
141         return sIsSupportsMultipleUsers;
142     }
143 
144     @Implementation
isSameProfileGroup(@serIdInt int userId, int otherUserId)145     protected boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
146         return mSameProfileGroupIds.containsKey(userId)
147                 && mSameProfileGroupIds.get(userId) == otherUserId
148                 || mSameProfileGroupIds.containsKey(otherUserId)
149                 && mSameProfileGroupIds.get(otherUserId) == userId;
150     }
151 
getSameProfileGroupIds()152     public Map<Integer, Integer> getSameProfileGroupIds() {
153         return mSameProfileGroupIds;
154     }
155 
setSupportsMultipleUsers(boolean supports)156     public void setSupportsMultipleUsers(boolean supports) {
157         sIsSupportsMultipleUsers = supports;
158     }
159 }
160