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.Bundle;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.os.UserManager.EnforcingUser;
25 
26 import com.google.android.collect.Maps;
27 
28 import org.robolectric.RuntimeEnvironment;
29 import org.robolectric.annotation.Implementation;
30 import org.robolectric.annotation.Implements;
31 import org.robolectric.annotation.Resetter;
32 import org.robolectric.shadow.api.Shadow;
33 
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41 
42 @Implements(value = UserManager.class)
43 public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager {
44 
45     private static boolean sIsSupportsMultipleUsers;
46 
47     private final List<String> mBaseRestrictions = new ArrayList<>();
48     private final List<String> mGuestRestrictions = new ArrayList<>();
49     private final Map<String, List<EnforcingUser>> mRestrictionSources = new HashMap<>();
50     private final List<UserInfo> mUserProfileInfos = new ArrayList<>();
51     private final Set<Integer> mManagedProfiles = new HashSet<>();
52     private boolean mIsQuietModeEnabled = false;
53     private int[] profileIdsForUser = new int[0];
54     private boolean mUserSwitchEnabled;
55 
56     private @UserManager.UserSwitchabilityResult int mSwitchabilityStatus =
57             UserManager.SWITCHABILITY_STATUS_OK;
58     private final Map<Integer, Integer> mSameProfileGroupIds = Maps.newHashMap();
59 
addProfile(UserInfo userInfo)60     public void addProfile(UserInfo userInfo) {
61         mUserProfileInfos.add(userInfo);
62     }
63 
64     @Resetter
reset()65     public static void reset() {
66         sIsSupportsMultipleUsers = false;
67     }
68 
69     @Implementation
getProfiles(@serIdInt int userHandle)70     protected List<UserInfo> getProfiles(@UserIdInt int userHandle) {
71         return mUserProfileInfos;
72     }
73 
74     @Implementation
getProfileIds(@serIdInt int userHandle, boolean enabledOnly)75     protected int[] getProfileIds(@UserIdInt int userHandle, boolean enabledOnly) {
76         int[] ids = new int[mUserProfileInfos.size()];
77         for (int i = 0; i < mUserProfileInfos.size(); i++) {
78             ids[i] = mUserProfileInfos.get(i).id;
79         }
80         return ids;
81     }
82 
83     @Implementation
getCredentialOwnerProfile(@serIdInt int userHandle)84     protected int getCredentialOwnerProfile(@UserIdInt int userHandle) {
85         return userHandle;
86     }
87 
88     @Implementation
hasBaseUserRestriction(String restrictionKey, UserHandle userHandle)89     protected boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
90         return mBaseRestrictions.contains(restrictionKey);
91     }
92 
addBaseUserRestriction(String restriction)93     public void addBaseUserRestriction(String restriction) {
94         mBaseRestrictions.add(restriction);
95     }
96 
97     @Implementation
getDefaultGuestRestrictions()98     protected Bundle getDefaultGuestRestrictions() {
99         Bundle bundle = new Bundle();
100         mGuestRestrictions.forEach(restriction -> bundle.putBoolean(restriction, true));
101         return bundle;
102     }
103 
addGuestUserRestriction(String restriction)104     public void addGuestUserRestriction(String restriction) {
105         mGuestRestrictions.add(restriction);
106     }
107 
getShadow()108     public static ShadowUserManager getShadow() {
109         return (ShadowUserManager) Shadow.extract(
110                 RuntimeEnvironment.application.getSystemService(UserManager.class));
111     }
112 
113     @Implementation
getUserRestrictionSources( String restrictionKey, UserHandle userHandle)114     protected List<EnforcingUser> getUserRestrictionSources(
115             String restrictionKey, UserHandle userHandle) {
116         // Return empty list when there is no enforcing user, otherwise might trigger
117         // NullPointer Exception in RestrictedLockUtils.checkIfRestrictionEnforced.
118         List<EnforcingUser> enforcingUsers =
119                 mRestrictionSources.get(restrictionKey + userHandle.getIdentifier());
120         return enforcingUsers == null ? Collections.emptyList() : enforcingUsers;
121     }
122 
setUserRestrictionSources( String restrictionKey, UserHandle userHandle, List<EnforcingUser> enforcers)123     public void setUserRestrictionSources(
124             String restrictionKey, UserHandle userHandle, List<EnforcingUser> enforcers) {
125         mRestrictionSources.put(restrictionKey + userHandle.getIdentifier(), enforcers);
126     }
127 
128     @Implementation
isQuietModeEnabled(UserHandle userHandle)129     protected boolean isQuietModeEnabled(UserHandle userHandle) {
130         return mIsQuietModeEnabled;
131     }
132 
setQuietModeEnabled(boolean enabled)133     public void setQuietModeEnabled(boolean enabled) {
134         mIsQuietModeEnabled = enabled;
135     }
136 
137     @Implementation
getProfileIdsWithDisabled(@serIdInt int userId)138     protected int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
139         return profileIdsForUser;
140     }
141 
setProfileIdsWithDisabled(int[] profileIds)142     public void setProfileIdsWithDisabled(int[] profileIds) {
143         profileIdsForUser = profileIds;
144     }
145 
146     @Implementation
isUserSwitcherEnabled()147     protected boolean isUserSwitcherEnabled() {
148         return mUserSwitchEnabled;
149     }
150 
151     @Implementation
isManagedProfile(int userId)152     protected boolean isManagedProfile(int userId) {
153         return mManagedProfiles.contains(userId);
154     }
155 
setManagedProfiles(Set<Integer> profileIds)156     public void setManagedProfiles(Set<Integer> profileIds) {
157         mManagedProfiles.clear();
158         mManagedProfiles.addAll(profileIds);
159     }
160 
setUserSwitcherEnabled(boolean userSwitchEnabled)161     public void setUserSwitcherEnabled(boolean userSwitchEnabled) {
162         mUserSwitchEnabled = userSwitchEnabled;
163     }
164 
165     @Implementation
supportsMultipleUsers()166     protected static boolean supportsMultipleUsers() {
167         return sIsSupportsMultipleUsers;
168     }
169 
170     @Implementation
isSameProfileGroup(@serIdInt int userId, int otherUserId)171     protected boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
172         return mSameProfileGroupIds.containsKey(userId)
173                 && mSameProfileGroupIds.get(userId) == otherUserId
174                 || mSameProfileGroupIds.containsKey(otherUserId)
175                 && mSameProfileGroupIds.get(otherUserId) == userId;
176     }
177 
getSameProfileGroupIds()178     public Map<Integer, Integer> getSameProfileGroupIds() {
179         return mSameProfileGroupIds;
180     }
181 
setSupportsMultipleUsers(boolean supports)182     public void setSupportsMultipleUsers(boolean supports) {
183         sIsSupportsMultipleUsers = supports;
184     }
185 
186     @Implementation
getUserInfo(@serIdInt int userId)187     protected UserInfo getUserInfo(@UserIdInt int userId) {
188         return mUserProfileInfos.stream()
189                 .filter(userInfo -> userInfo.id == userId)
190                 .findFirst()
191                 .orElse(super.getUserInfo(userId));
192     }
193 
194     @Implementation
getUserSwitchability()195     protected @UserManager.UserSwitchabilityResult int getUserSwitchability() {
196         return mSwitchabilityStatus;
197     }
198 
setSwitchabilityStatus(@serManager.UserSwitchabilityResult int newStatus)199     public void setSwitchabilityStatus(@UserManager.UserSwitchabilityResult int newStatus) {
200         mSwitchabilityStatus = newStatus;
201     }
202 }
203