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.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.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.internal.util.UserIcons;
32 
33 /**
34  * Simple class for providing icons for users in Settings.
35  */
36 public class UserIconProvider {
37     /**
38      * Gets a scaled rounded icon for the given user to use in settings.  If a user does
39      * not have an icon saved, this method will default to a generic icon and update UserManager to
40      * use that icon.
41      *
42      * @param userInfo User for which the icon is requested.
43      * @param context Context to use for resources
44      * @return {@link RoundedBitmapDrawable} representing the icon for the user.
45      */
getRoundedUserIcon(UserInfo userInfo, Context context)46     public RoundedBitmapDrawable getRoundedUserIcon(UserInfo userInfo, Context context) {
47         UserManager userManager = UserManager.get(context);
48         Resources res = context.getResources();
49         Bitmap icon = userManager.getUserIcon(userInfo.id);
50 
51         if (icon == null) {
52             icon = assignDefaultIcon(userManager, res, userInfo);
53         }
54 
55         return createScaledRoundIcon(res, icon);
56     }
57 
58     /** Returns a scaled, rounded, default icon for the Guest user */
getRoundedGuestDefaultIcon(Resources resources)59     public RoundedBitmapDrawable getRoundedGuestDefaultIcon(Resources resources) {
60         return createScaledRoundIcon(resources, getGuestUserDefaultIcon(resources));
61     }
62 
createScaledRoundIcon(Resources resources, Bitmap icon)63     private RoundedBitmapDrawable createScaledRoundIcon(Resources resources, Bitmap icon) {
64         BitmapDrawable scaledIcon = UserUtils.scaleUserIcon(resources, icon);
65         RoundedBitmapDrawable circleIcon =
66                 RoundedBitmapDrawableFactory.create(resources, scaledIcon.getBitmap());
67         circleIcon.setCircular(true);
68         return circleIcon;
69     }
70 
71     /**
72      * Assigns a default icon to a user according to the user's id. Handles Guest icon and non-guest
73      * user icons.
74      *
75      * @param userManager {@link UserManager} to set user icon
76      * @param resources {@link Resources} to grab icons from
77      * @param userInfo User whose avatar is set to default icon.
78      * @return Bitmap of the user icon.
79      */
assignDefaultIcon( UserManager userManager, Resources resources, UserInfo userInfo)80     public Bitmap assignDefaultIcon(
81             UserManager userManager, Resources resources, UserInfo userInfo) {
82         Bitmap bitmap = userInfo.isGuest()
83                 ? getGuestUserDefaultIcon(resources)
84                 : getUserDefaultIcon(resources, userInfo.id);
85         userManager.setUserIcon(userInfo.id, bitmap);
86         return bitmap;
87     }
88 
89     /**
90      * Gets a bitmap representing the user's default avatar.
91      *
92      * @param resources The resources to pull from
93      * @param id The id of the user to get the icon for.  Pass {@link UserHandle#USER_NULL} for
94      *           Guest user.
95      * @return Default user icon
96      */
getUserDefaultIcon(Resources resources, @UserIdInt int id)97     private Bitmap getUserDefaultIcon(Resources resources, @UserIdInt int id) {
98         return UserIcons.convertToBitmap(
99                 UserIcons.getDefaultUserIcon(resources, id, /* light= */ false));
100     }
101 
getGuestUserDefaultIcon(Resources resources)102     private Bitmap getGuestUserDefaultIcon(Resources resources) {
103         return getUserDefaultIcon(resources, UserHandle.USER_NULL);
104     }
105 }
106