1 /* 2 * Copyright (C) 2020 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 android.car.userlib; 17 18 import android.annotation.NonNull; 19 import android.annotation.RequiresPermission; 20 import android.content.Context; 21 import android.content.pm.UserInfo; 22 import android.graphics.Bitmap; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 import android.util.Log; 26 27 import com.android.internal.annotations.VisibleForTesting; 28 import com.android.internal.util.Preconditions; 29 import com.android.internal.util.UserIcons; 30 31 import com.google.android.collect.Sets; 32 33 import java.util.Set; 34 35 /** 36 * Provides utility methods for generic user-related functionalities that don't require a manager. 37 */ 38 public final class UserHelper { 39 40 private static final String TAG = UserHelper.class.getSimpleName(); 41 42 /** 43 * Default set of restrictions for Non-Admin users. 44 */ 45 @VisibleForTesting 46 public static final Set<String> DEFAULT_NON_ADMIN_RESTRICTIONS = Sets.newArraySet( 47 UserManager.DISALLOW_FACTORY_RESET 48 ); 49 50 /** 51 * Additional optional set of restrictions for Non-Admin users. These are the restrictions 52 * configurable via Settings. 53 */ 54 @VisibleForTesting 55 public static final Set<String> OPTIONAL_NON_ADMIN_RESTRICTIONS = Sets.newArraySet( 56 UserManager.DISALLOW_ADD_USER, 57 UserManager.DISALLOW_OUTGOING_CALLS, 58 UserManager.DISALLOW_SMS, 59 UserManager.DISALLOW_INSTALL_APPS, 60 UserManager.DISALLOW_UNINSTALL_APPS 61 ); 62 UserHelper()63 private UserHelper() { 64 throw new UnsupportedOperationException("contains only static methods"); 65 } 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 UserInfo user)76 public static void grantAdminPermissions(@NonNull Context context, @NonNull UserInfo user) { 77 Preconditions.checkArgument(context != null, "Context cannot be null"); 78 Preconditions.checkArgument(user != null, "User cannot be null"); 79 80 UserManager userManager = UserManager.get(context); 81 82 if (!userManager.isAdminUser()) { 83 Log.w(TAG, "Only admin users can assign admin permissions."); 84 return; 85 } 86 87 userManager.setUserAdmin(user.id); 88 89 UserHandle userHandle = user.getUserHandle(); 90 // Remove restrictions imposed on non-admins. 91 for (String restriction : DEFAULT_NON_ADMIN_RESTRICTIONS) { 92 userManager.setUserRestriction(restriction, /* enable= */ false, userHandle); 93 } 94 95 for (String restriction : OPTIONAL_NON_ADMIN_RESTRICTIONS) { 96 userManager.setUserRestriction(restriction, /* enable= */ false, userHandle); 97 } 98 } 99 100 /** 101 * Sets the values of default Non-Admin restrictions to the passed in value. 102 * 103 * @param context Current application context 104 * @param user User to set restrictions on. 105 * @param enable If true, restriction is ON, If false, restriction is OFF. 106 */ setDefaultNonAdminRestrictions(@onNull Context context, @NonNull UserInfo user, boolean enable)107 public static void setDefaultNonAdminRestrictions(@NonNull Context context, 108 @NonNull UserInfo user, boolean enable) { 109 Preconditions.checkArgument(context != null, "Context cannot be null"); 110 Preconditions.checkArgument(user != null, "User cannot be null"); 111 112 UserManager userManager = UserManager.get(context); 113 for (String restriction : DEFAULT_NON_ADMIN_RESTRICTIONS) { 114 userManager.setUserRestriction(restriction, enable, user.getUserHandle()); 115 } 116 } 117 118 /** 119 * Assigns a default icon to a user according to the user's id. 120 * 121 * @param context Current application context 122 * @param user User whose avatar is set to default icon. 123 * @return Bitmap of the user icon. 124 */ 125 @NonNull assignDefaultIcon(@onNull Context context, @NonNull UserInfo user)126 public static Bitmap assignDefaultIcon(@NonNull Context context, @NonNull UserInfo user) { 127 Preconditions.checkArgument(context != null, "Context cannot be null"); 128 Preconditions.checkArgument(user != null, "User cannot be null"); 129 130 int idForIcon = user.isGuest() ? UserHandle.USER_NULL : user.id; 131 Bitmap bitmap = UserIcons.convertToBitmap( 132 UserIcons.getDefaultUserIcon(context.getResources(), idForIcon, false)); 133 UserManager.get(context).setUserIcon(user.id, bitmap); 134 return bitmap; 135 } 136 } 137