1 /*
2  * Copyright (C) 2018 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.car.settings.users;
18 
19 import android.content.Context;
20 import android.content.pm.UserInfo;
21 import android.content.res.Resources;
22 import android.graphics.Bitmap;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.os.UserManager;
26 
27 import com.android.car.settings.R;
28 
29 /**
30  * Util class for providing basic, universally needed user-related methods.
31  */
32 public class UserUtils {
UserUtils()33     private UserUtils() {
34     }
35 
36     /**
37      * Fetches the {@link UserInfo} from UserManager system service for the user ID.
38      *
39      * @param userId ID that corresponds to the returned UserInfo.
40      * @return {@link UserInfo} for the user ID.
41      */
getUserInfo(Context context, int userId)42     public static UserInfo getUserInfo(Context context, int userId) {
43         UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
44         return userManager.getUserInfo(userId);
45     }
46 
47     /**
48      * Returns the user name that should be displayed. The caller shouldn't use userInfo.name
49      * directly, because the display name is modified for the current process user.
50      */
getUserDisplayName(Context context, UserInfo userInfo)51     public static String getUserDisplayName(Context context, UserInfo userInfo) {
52         return UserHelper.getInstance(context).isCurrentProcessUser(userInfo) ? context.getString(
53                 R.string.current_user_name, userInfo.name) : userInfo.name;
54     }
55 
56     /**
57      * Returns whether or not the current user is an admin and whether the user info they are
58      * viewing is of a non-admin.
59      */
isAdminViewingNonAdmin(UserManager userManager, UserInfo userInfo)60     public static boolean isAdminViewingNonAdmin(UserManager userManager, UserInfo userInfo) {
61         return userManager.isAdminUser() && !userInfo.isAdmin();
62     }
63 
64     /**
65      * Returns a {@link Drawable} for the given {@code icon} scaled to the appropriate size.
66      */
scaleUserIcon(Resources res, Bitmap icon)67     public static BitmapDrawable scaleUserIcon(Resources res, Bitmap icon) {
68         int desiredSize = res.getDimensionPixelSize(R.dimen.icon_size);
69         Bitmap scaledIcon =
70                 Bitmap.createScaledBitmap(icon, desiredSize, desiredSize, /*filter=*/true);
71         return new BitmapDrawable(res, scaledIcon);
72     }
73 }
74