• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.util.SparseArray;
25 
26 import org.robolectric.RuntimeEnvironment;
27 import org.robolectric.annotation.Implementation;
28 import org.robolectric.annotation.Implements;
29 import org.robolectric.annotation.Resetter;
30 import org.robolectric.shadow.api.Shadow;
31 
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 
39 @Implements(value = UserManager.class, inheritImplementationMethods = true)
40 public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager {
41 
42     private SparseArray<UserInfo> mUserInfos = new SparseArray<>();
43     private final List<String> mRestrictions = new ArrayList<>();
44     private final Map<String, List<EnforcingUser>> mRestrictionSources = new HashMap<>();
45     private final List<UserInfo> mUserProfileInfos = new ArrayList<>();
46     private final Set<Integer> mManagedProfiles = new HashSet<>();
47     private boolean mIsQuietModeEnabled = false;
48 
49     @Resetter
reset()50     public void reset() {
51         mUserInfos.clear();
52         mRestrictions.clear();
53         mUserProfileInfos.clear();
54         mRestrictionSources.clear();
55         mManagedProfiles.clear();
56         mIsQuietModeEnabled = false;
57     }
58 
setUserInfo(int userHandle, UserInfo userInfo)59     public void setUserInfo(int userHandle, UserInfo userInfo) {
60         mUserInfos.put(userHandle, userInfo);
61     }
62 
63     @Implementation
getUserInfo(int userHandle)64     public UserInfo getUserInfo(int userHandle) {
65         return mUserInfos.get(userHandle);
66     }
67 
addProfile(UserInfo userInfo)68     public void addProfile(UserInfo userInfo) {
69         mUserProfileInfos.add(userInfo);
70     }
71 
72     @Implementation
getProfiles(@serIdInt int userHandle)73     public List<UserInfo> getProfiles(@UserIdInt int userHandle) {
74         return mUserProfileInfos;
75     }
76 
77     @Implementation
getCredentialOwnerProfile(@serIdInt int userHandle)78     public int getCredentialOwnerProfile(@UserIdInt int userHandle) {
79         return userHandle;
80     }
81 
82     @Implementation
hasBaseUserRestriction(String restrictionKey, UserHandle userHandle)83     public boolean hasBaseUserRestriction(String restrictionKey, UserHandle userHandle) {
84         return mRestrictions.contains(restrictionKey);
85     }
86 
addBaseUserRestriction(String restriction)87     public void addBaseUserRestriction(String restriction) {
88         mRestrictions.add(restriction);
89     }
90 
getShadow()91     public static ShadowUserManager getShadow() {
92         return (ShadowUserManager) Shadow.extract(
93                 RuntimeEnvironment.application.getSystemService(UserManager.class));
94     }
95 
96     @Implementation
getUserRestrictionSources( String restrictionKey, UserHandle userHandle)97     public List<EnforcingUser> getUserRestrictionSources(
98             String restrictionKey, UserHandle userHandle) {
99         return mRestrictionSources.get(restrictionKey + userHandle.getIdentifier());
100     }
101 
setUserRestrictionSources( String restrictionKey, UserHandle userHandle, List<EnforcingUser> enforcers)102     public void setUserRestrictionSources(
103             String restrictionKey, UserHandle userHandle, List<EnforcingUser> enforcers) {
104         mRestrictionSources.put(restrictionKey + userHandle.getIdentifier(), enforcers);
105     }
106 
107     @Implementation
isManagedProfile(@serIdInt int userId)108     public boolean isManagedProfile(@UserIdInt int userId) {
109         return mManagedProfiles.contains(userId);
110     }
111 
addManagedProfile(int userId)112     public void addManagedProfile(int userId) {
113         mManagedProfiles.add(userId);
114     }
115 
116     @Implementation
isQuietModeEnabled(UserHandle userHandle)117     public boolean isQuietModeEnabled(UserHandle userHandle) {
118         return mIsQuietModeEnabled;
119     }
120 
setQuietModeEnabled(boolean enabled)121     public void setQuietModeEnabled(boolean enabled) {
122         mIsQuietModeEnabled = enabled;
123     }
124 }
125