• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.res.Resources;
22 import android.graphics.Bitmap;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 
28 import androidx.core.graphics.drawable.RoundedBitmapDrawable;
29 import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
30 
31 import com.android.car.admin.ui.UserAvatarView;
32 import com.android.car.internal.user.UserHelper;
33 import com.android.internal.util.UserIcons;
34 import com.android.systemui.R;
35 import com.android.systemui.dagger.SysUISingleton;
36 
37 import javax.inject.Inject;
38 
39 /**
40  * Simple class for providing icons for users.
41  */
42 @SysUISingleton
43 public class UserIconProvider {
44     private final Context mContext;
45     private final UserManager mUserManager;
46 
47     private final float mBadgeToIconSizeRatio;
48     private final float mBadgePadding;
49 
50     @Inject
UserIconProvider(Context context, UserManager userManager)51     public UserIconProvider(Context context, UserManager userManager) {
52         mContext = context;
53         mUserManager = userManager;
54 
55         mBadgeToIconSizeRatio =
56                 mContext.getResources().getDimension(R.dimen.car_user_switcher_managed_badge_size)
57                         / mContext.getResources().getDimension(
58                         R.dimen.car_user_switcher_image_avatar_size);
59         mBadgePadding = mContext.getResources().getDimension(
60                 R.dimen.car_user_switcher_managed_badge_margin);
61     }
62 
63     /**
64      * Sets a rounded icon with the first letter of the given user name.
65      * This method will update UserManager to use that icon.
66      *
67      * @param userId User for which the icon is requested.
68      */
setRoundedUserIcon(@serIdInt int userId)69     public void setRoundedUserIcon(@UserIdInt int userId) {
70         UserHelper.assignDefaultIcon(mContext, UserHandle.of(userId));
71     }
72 
73     /**
74      * Gets a scaled rounded icon for the given user.  If a user does not have an icon saved, this
75      * method will default to a generic icon and update UserManager to use that icon.
76      *
77      * @param userId User for which the icon is requested.
78      * @return {@link RoundedBitmapDrawable} representing the icon for the user.
79      */
getRoundedUserIcon(@serIdInt int userId)80     public Drawable getRoundedUserIcon(@UserIdInt int userId) {
81         Resources res = mContext.getResources();
82         Bitmap icon = mUserManager.getUserIcon(userId);
83 
84         if (icon == null) {
85             icon = UserHelper.assignDefaultIcon(mContext, UserHandle.of(userId));
86         }
87 
88         return new BitmapDrawable(res, icon);
89     }
90 
91     /**
92      * Gets a user icon with badge if the user profile is managed.
93      *
94      * @param userId User for which the icon is requested and badge is set
95      * @return {@link Drawable} with badge
96      */
getDrawableWithBadge(@serIdInt int userId)97     public Drawable getDrawableWithBadge(@UserIdInt int userId) {
98         return addBadge(getRoundedUserIcon(userId), userId);
99     }
100 
101     /**
102      * Gets an icon with badge if the device is managed.
103      *
104      * @param drawable icon without badge
105      * @return {@link Drawable} with badge
106      */
getDrawableWithBadge(Drawable drawable)107     public Drawable getDrawableWithBadge(Drawable drawable) {
108         return addBadge(drawable, UserHandle.USER_NULL);
109     }
110 
addBadge(Drawable drawable, @UserIdInt int userId)111     private Drawable addBadge(Drawable drawable, @UserIdInt int userId) {
112         int iconSize = drawable.getIntrinsicWidth();
113         UserAvatarView userAvatarView = new UserAvatarView(mContext);
114         userAvatarView.setBadgeDiameter(iconSize * mBadgeToIconSizeRatio);
115         userAvatarView.setBadgeMargin(mBadgePadding);
116         if (userId != UserHandle.USER_NULL) {
117             // When the userId is valid, add badge if the user is managed.
118             userAvatarView.setDrawableWithBadge(drawable, userId);
119         } else {
120             // When the userId is not valid, add badge if the device is managed.
121             userAvatarView.setDrawableWithBadge(drawable);
122         }
123         Drawable badgedIcon = userAvatarView.getUserIconDrawable();
124         badgedIcon.setBounds(0, 0, iconSize, iconSize);
125         return badgedIcon;
126     }
127 
128     /** Returns a scaled, rounded, default icon for the Guest user */
getRoundedGuestDefaultIcon()129     public Drawable getRoundedGuestDefaultIcon() {
130         Bitmap icon = UserHelper.getGuestDefaultIcon(mContext);
131         return new BitmapDrawable(mContext.getResources(), icon);
132     }
133 
134     /** Returns a scaled, rounded, default icon for the add user entry. */
getRoundedAddUserIcon()135     public Drawable getRoundedAddUserIcon() {
136         RoundedBitmapDrawable roundedIcon = RoundedBitmapDrawableFactory.create(
137                 mContext.getResources(),
138                 UserIcons.convertToBitmap(mContext.getDrawable(R.drawable.car_add_circle_round)));
139         roundedIcon.setCircular(true);
140         return roundedIcon;
141     }
142 }
143