1 /*
2  * Copyright (C) 2022 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 package com.android.car.internal.user;
17 
18 import android.annotation.ColorInt;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.RequiresPermission;
22 import android.content.Context;
23 import android.content.pm.UserInfo;
24 import android.graphics.Bitmap;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.util.Log;
28 
29 import com.android.internal.annotations.VisibleForTesting;
30 import com.android.internal.util.Preconditions;
31 
32 import java.util.Arrays;
33 import java.util.HashSet;
34 import java.util.Set;
35 
36 /**
37  * Provides utility methods for generic user-related functionalities that don't require a manager.
38  *
39  * @hide
40  */
41 public final class UserHelper {
42     @VisibleForTesting
43     static final String TAG = UserHelper.class.getSimpleName();
44 
45     /**
46      * Default set of restrictions for Non-Admin users.
47      */
48     @VisibleForTesting
49     public static final Set<String> DEFAULT_NON_ADMIN_RESTRICTIONS = new HashSet<>(Arrays.asList(
50             UserManager.DISALLOW_FACTORY_RESET
51     ));
52     /**
53      * Additional optional set of restrictions for Non-Admin users. These are the restrictions
54      * configurable via Settings.
55      */
56     @VisibleForTesting
57     public static final Set<String> OPTIONAL_NON_ADMIN_RESTRICTIONS = new HashSet<>(Arrays.asList(
58             UserManager.DISALLOW_ADD_USER,
59             UserManager.DISALLOW_OUTGOING_CALLS,
60             UserManager.DISALLOW_SMS,
61             UserManager.DISALLOW_INSTALL_APPS,
62             UserManager.DISALLOW_UNINSTALL_APPS
63     ));
UserHelper()64     private UserHelper() {
65         throw new UnsupportedOperationException("contains only static methods");
66     }
67     /**
68      * Grants admin permissions to the user.
69      *
70      * @hide
71      */
72     @RequiresPermission(allOf = {
73             android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
74             android.Manifest.permission.MANAGE_USERS
75     })
grantAdminPermissions(@onNull Context context, @NonNull UserHandle user)76     public static void grantAdminPermissions(@NonNull Context context, @NonNull UserHandle user) {
77         Preconditions.checkArgument(context != null, "Context cannot be null");
78         Preconditions.checkArgument(user != null, "User cannot be null");
79         UserManager userManager = context.getSystemService(UserManager.class);
80         if (!userManager.isAdminUser()) {
81             Log.w(TAG, "Only admin users can assign admin permissions.");
82             return;
83         }
84         userManager.setUserAdmin(user.getIdentifier());
85         // Remove restrictions imposed on non-admins.
86         for (String restriction : DEFAULT_NON_ADMIN_RESTRICTIONS) {
87             userManager.setUserRestriction(restriction, /* enable= */ false, user);
88         }
89         for (String restriction : OPTIONAL_NON_ADMIN_RESTRICTIONS) {
90             userManager.setUserRestriction(restriction, /* enable= */ false, user);
91         }
92     }
93     /**
94      * Sets the values of default Non-Admin restrictions to the passed in value.
95      *
96      * @param context Current application context
97      * @param user User to set restrictions on.
98      * @param enable If true, restriction is ON, If false, restriction is OFF.
99      *
100      * @hide
101      */
setDefaultNonAdminRestrictions(@onNull Context context, @NonNull UserHandle user, boolean enable)102     public static void setDefaultNonAdminRestrictions(@NonNull Context context,
103             @NonNull UserHandle user, boolean enable) {
104         Preconditions.checkArgument(context != null, "Context cannot be null");
105         Preconditions.checkArgument(user != null, "User cannot be null");
106         UserManager userManager = context.getSystemService(UserManager.class);
107         for (String restriction : DEFAULT_NON_ADMIN_RESTRICTIONS) {
108             userManager.setUserRestriction(restriction, enable, user);
109         }
110     }
111     /**
112      * Assigns a default icon to a user according to the user's id.
113      *
114      * @param context Current application context
115      * @param user User whose avatar is set to default icon.
116      * @return Bitmap of the user icon.
117      *
118      * @hide
119      */
120     @Nullable
assignDefaultIcon(@onNull Context context, @NonNull UserHandle user)121     public static Bitmap assignDefaultIcon(@NonNull Context context, @NonNull UserHandle user) {
122         Preconditions.checkArgument(context != null, "Context cannot be null");
123         Preconditions.checkArgument(user != null, "User cannot be null");
124         UserManager userManager = context.getSystemService(UserManager.class);
125         UserInfo userInfo = userManager.getUserInfo(user.getIdentifier());
126         if (userInfo == null) {
127             return null;
128         }
129         Bitmap bitmap = CarUserIconProvider.getDefaultUserIcon(context, userInfo);
130         userManager.setUserIcon(user.getIdentifier(), bitmap);
131         return bitmap;
132     }
133     /**
134      * Returns the default user icon for guest users.
135      *
136      * @param context Current application context
137      * @return Bitmap of the user icon.
138      *
139      * @hide
140      */
141     @NonNull
getGuestDefaultIcon(@onNull Context context)142     public static Bitmap getGuestDefaultIcon(@NonNull Context context) {
143         Preconditions.checkArgument(context != null, "Context cannot be null");
144         return CarUserIconProvider.getGuestDefaultUserIcon(context.getResources());
145     }
146     /**
147      * Get the user icon color for a given user id. This should not be the guest user.
148      *
149      * @param context Current application context
150      * @param user Non-guest user to get the user icon color
151      * @return ColorInt of the icon.
152      *
153      * @hide
154      */
155     @ColorInt
getUserNameIconColor(@onNull Context context, @NonNull UserHandle user)156     public static int getUserNameIconColor(@NonNull Context context, @NonNull UserHandle user) {
157         Preconditions.checkArgument(context != null, "Context cannot be null");
158         Preconditions.checkArgument(user != null, "User cannot be null");
159         UserManager userManager = context.getSystemService(UserManager.class);
160         UserInfo userInfo = userManager.getUserInfo(user.getIdentifier());
161         if (userInfo == null) {
162             throw new IllegalArgumentException("Invalid UserHandle - could not get UserInfo");
163         }
164         return CarUserIconProvider.getUserNameIconColor(context, userInfo);
165     }
166 }
167