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 
17 package com.android.systemui.car.userswitcher;
18 
19 import android.annotation.UserIdInt;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.content.res.Resources;
23 import android.graphics.Bitmap;
24 import android.graphics.drawable.BitmapDrawable;
25 import android.graphics.drawable.Drawable;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 
29 import androidx.core.graphics.drawable.RoundedBitmapDrawable;
30 
31 import com.android.car.admin.ui.UserAvatarView;
32 import com.android.car.internal.user.UserHelper;
33 import com.android.systemui.R;
34 
35 /**
36  * Simple class for providing icons for users.
37  */
38 public class UserIconProvider {
39     /**
40      * Sets a rounded icon with the first letter of the given user name.
41      * This method will update UserManager to use that icon.
42      *
43      * @param userInfo User for which the icon is requested.
44      * @param context Context to use for resources
45      */
setRoundedUserIcon(UserInfo userInfo, Context context)46     public void setRoundedUserIcon(UserInfo userInfo, Context context) {
47         UserHelper.assignDefaultIcon(context, userInfo.getUserHandle());
48     }
49 
50     /**
51      * Gets a scaled rounded icon for the given user.  If a user does not have an icon saved, this
52      * method will default to a generic icon and update UserManager to use that icon.
53      *
54      * @param userInfo User for which the icon is requested.
55      * @param context Context to use for resources
56      * @return {@link RoundedBitmapDrawable} representing the icon for the user.
57      */
getRoundedUserIcon(UserInfo userInfo, Context context)58     public Drawable getRoundedUserIcon(UserInfo userInfo, Context context) {
59         UserManager userManager = context.getSystemService(UserManager.class);
60         Resources res = context.getResources();
61         Bitmap icon = userManager.getUserIcon(userInfo.id);
62 
63         if (icon == null) {
64             icon = UserHelper.assignDefaultIcon(context, userInfo.getUserHandle());
65         }
66 
67         return new BitmapDrawable(res, icon);
68     }
69 
70     /**
71      * Gets a user icon with badge if the user profile is managed.
72      *
73      * @param context to use for the avatar view
74      * @param userInfo User for which the icon is requested and badge is set
75      * @return {@link Drawable} with badge
76      */
getDrawableWithBadge(Context context, UserInfo userInfo)77     public Drawable getDrawableWithBadge(Context context, UserInfo userInfo) {
78         return addBadge(context, getRoundedUserIcon(userInfo, context), userInfo.id);
79     }
80 
81     /**
82      * Gets an icon with badge if the device is managed.
83      *
84      * @param context context
85      * @param drawable icon without badge
86      * @return {@link Drawable} with badge
87      */
getDrawableWithBadge(Context context, Drawable drawable)88     public Drawable getDrawableWithBadge(Context context, Drawable drawable) {
89         return addBadge(context, drawable, UserHandle.USER_NULL);
90     }
91 
addBadge(Context context, Drawable drawable, @UserIdInt int userId)92     private static Drawable addBadge(Context context, Drawable drawable, @UserIdInt int userId) {
93         int iconSize = drawable.getIntrinsicWidth();
94         UserAvatarView userAvatarView = new UserAvatarView(context);
95         float badgeToIconSizeRatio =
96                 context.getResources().getDimension(R.dimen.car_user_switcher_managed_badge_size)
97                         / context.getResources().getDimension(
98                         R.dimen.car_user_switcher_image_avatar_size);
99         userAvatarView.setBadgeDiameter(iconSize * badgeToIconSizeRatio);
100         float badgePadding = context.getResources().getDimension(
101                 R.dimen.car_user_switcher_managed_badge_margin);
102         userAvatarView.setBadgeMargin(badgePadding);
103         if (userId != UserHandle.USER_NULL) {
104             // When the userId is valid, add badge if the user is managed.
105             userAvatarView.setDrawableWithBadge(drawable, userId);
106         } else {
107             // When the userId is not valid, add badge if the device is managed.
108             userAvatarView.setDrawableWithBadge(drawable);
109         }
110         Drawable badgedIcon = userAvatarView.getUserIconDrawable();
111         badgedIcon.setBounds(0, 0, iconSize, iconSize);
112         return badgedIcon;
113     }
114 
115     /** Returns a scaled, rounded, default icon for the Guest user */
getRoundedGuestDefaultIcon(Context context)116     public Drawable getRoundedGuestDefaultIcon(Context context) {
117         Bitmap icon = UserHelper.getGuestDefaultIcon(context);
118         return new BitmapDrawable(context.getResources(), icon);
119     }
120 }
121