1 /*
2  * Copyright (C) 2021 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 import android.annotation.NonNull;
18 import android.annotation.UserIdInt;
19 import android.content.Context;
20 import android.content.pm.UserInfo;
21 import android.graphics.Bitmap;
22 import android.os.Bundle;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 
26 import java.util.List;
27 
28 public class UserManagerCompat {
29     private final UserManager mUserManager;
30 
UserManagerCompat(UserManager userManager)31     public UserManagerCompat(UserManager userManager) {
32         mUserManager = userManager;
33     }
34 
UserManagerCompat(Context context)35     public UserManagerCompat(Context context) {
36         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
37     }
38 
getProfileIdsWithDisabled(@serIdInt int userId)39     public int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
40         return mUserManager.getProfileIdsWithDisabled(userId);
41     }
42 
getUserInfo(@serIdInt int userId)43     public UserInfoCompat getUserInfo(@UserIdInt int userId) {
44         return new UserInfoCompat(mUserManager.getUserInfo(userId));
45     }
46 
removeUser(@serIdInt int userId)47     public boolean removeUser(@UserIdInt int userId) {
48         return mUserManager.removeUser(userId);
49     }
50 
getUsers()51     public List<UserInfoCompat> getUsers() {
52         return UserInfoCompat.convert(mUserManager.getUsers());
53     }
54 
createProfileForUser(String name, @NonNull String userType, @UserInfo.UserInfoFlag int flags, @UserIdInt int userId)55     public UserInfoCompat createProfileForUser(String name, @NonNull String userType,
56             @UserInfo.UserInfoFlag int flags, @UserIdInt int userId) {
57         return new UserInfoCompat(mUserManager.createProfileForUser(name, userType, flags, userId));
58     }
59 
getCompat(Context context)60     public static UserManagerCompat getCompat(Context context) {
61         return new UserManagerCompat(context);
62     }
63 
get(Context context)64     public static UserManager get(Context context) {
65         return UserManager.get(context);
66     }
67 
isSameProfileGroup(@serIdInt int userId, int otherUserId)68     public boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
69         return mUserManager.isSameProfileGroup(userId, otherUserId);
70     }
71 
72     public @NonNull
getAliveUsers()73     List<UserInfoCompat> getAliveUsers() {
74         return UserInfoCompat.convert(mUserManager.getAliveUsers());
75     }
76 
getProfiles(@serIdInt int userId)77     public List<UserInfoCompat> getProfiles(@UserIdInt int userId) {
78         return UserInfoCompat.convert(mUserManager.getProfiles(userId));
79     }
80 
getProfileParent(@serIdInt int userId)81     public UserInfoCompat getProfileParent(@UserIdInt int userId) {
82         return new UserInfoCompat(mUserManager.getProfileParent(userId));
83     }
84 
isUserAdmin(@serIdInt int userId)85     public boolean isUserAdmin(@UserIdInt int userId) {
86         return mUserManager.isUserAdmin(userId);
87     }
88 
isAdminUser()89     public boolean isAdminUser() {
90         return mUserManager.isAdminUser();
91     }
92 
setUserRestriction(String key, boolean value, UserHandle userHandle)93     public void setUserRestriction(String key, boolean value, UserHandle userHandle) {
94         mUserManager.setUserRestriction(key, value, userHandle);
95     }
96 
hasBaseUserRestriction( @serManager.UserRestrictionKey @onNull String restrictionKey, @NonNull UserHandle userHandle)97     public boolean hasBaseUserRestriction(
98             @UserManager.UserRestrictionKey @NonNull String restrictionKey,
99             @NonNull UserHandle userHandle) {
100         return mUserManager.hasBaseUserRestriction(restrictionKey, userHandle);
101     }
102 
setApplicationRestrictions( String packageName, Bundle restrictions, UserHandle user)103     public void setApplicationRestrictions(
104             String packageName, Bundle restrictions, UserHandle user) {
105         mUserManager.setApplicationRestrictions(packageName, restrictions, user);
106     }
107 
getApplicationRestrictions(String packageName, UserHandle user)108     public Bundle getApplicationRestrictions(String packageName, UserHandle user) {
109         return mUserManager.getApplicationRestrictions(packageName, user);
110     }
111 
setUserIcon(@serIdInt int userId, @NonNull Bitmap icon)112     public void setUserIcon(@UserIdInt int userId, @NonNull Bitmap icon) {
113         mUserManager.setUserIcon(userId, icon);
114     }
115 }
116