• 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.Nullable;
20 import android.annotation.UserIdInt;
21 import android.app.admin.DevicePolicyManager;
22 import android.content.Context;
23 import android.content.pm.UserInfo;
24 import android.content.res.Resources;
25 import android.graphics.Bitmap;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.os.UserHandle;
29 import android.os.UserManager;
30 
31 import androidx.core.graphics.drawable.RoundedBitmapDrawable;
32 import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
33 
34 import com.android.internal.util.UserIcons;
35 import com.android.settingslib.Utils;
36 import com.android.settingslib.drawable.UserIconDrawable;
37 import com.android.systemui.R;
38 
39 /**
40  * Simple class for providing icons for users.
41  */
42 public class UserIconProvider {
43     /**
44      * Gets a scaled rounded icon for the given user.  If a user does not have an icon saved, this
45      * method will default to a generic icon and update UserManager to use that icon.
46      *
47      * @param userInfo User for which the icon is requested.
48      * @param context Context to use for resources
49      * @return {@link RoundedBitmapDrawable} representing the icon for the user.
50      */
getRoundedUserIcon(UserInfo userInfo, Context context)51     public Drawable getRoundedUserIcon(UserInfo userInfo, Context context) {
52         UserManager userManager = UserManager.get(context);
53         Resources res = context.getResources();
54         Bitmap icon = userManager.getUserIcon(userInfo.id);
55 
56         if (icon == null) {
57             icon = assignDefaultIcon(userManager, res, userInfo);
58         }
59 
60         return new BitmapDrawable(res, icon);
61     }
62 
63     /** Returns a scaled, rounded, default icon for the Guest user */
getRoundedGuestDefaultIcon(Resources resources)64     public Drawable getRoundedGuestDefaultIcon(Resources resources) {
65         Bitmap icon = getGuestUserDefaultIcon(resources);
66         return new BitmapDrawable(resources, icon);
67     }
68 
69     /**
70      * Returns a {@link Drawable} for the given {@code icon} scaled to the appropriate size.
71      */
scaleUserIcon(Resources res, Bitmap icon)72     private static BitmapDrawable scaleUserIcon(Resources res, Bitmap icon) {
73         int desiredSize = res.getDimensionPixelSize(R.dimen.car_primary_icon_size);
74         Bitmap scaledIcon =
75                 Bitmap.createScaledBitmap(icon, desiredSize, desiredSize, /*filter=*/ true);
76         return new BitmapDrawable(res, scaledIcon);
77     }
78 
79     /**
80      * Assigns a default icon to a user according to the user's id. Handles Guest icon and non-guest
81      * user icons.
82      *
83      * @param userManager {@link UserManager} to set user icon
84      * @param resources {@link Resources} to grab icons from
85      * @param userInfo User whose avatar is set to default icon.
86      * @return Bitmap of the user icon.
87      */
assignDefaultIcon( UserManager userManager, Resources resources, UserInfo userInfo)88     public Bitmap assignDefaultIcon(
89             UserManager userManager, Resources resources, UserInfo userInfo) {
90         Bitmap bitmap = userInfo.isGuest()
91                 ? getGuestUserDefaultIcon(resources)
92                 : getUserDefaultIcon(resources, userInfo.id);
93         userManager.setUserIcon(userInfo.id, bitmap);
94         return bitmap;
95     }
96 
97     /**
98      * Gets a bitmap representing the user's default avatar.
99      *
100      * @param resources The resources to pull from
101      * @param id The id of the user to get the icon for.  Pass {@link UserHandle#USER_NULL} for
102      *           Guest user.
103      * @return Default user icon
104      */
getUserDefaultIcon(Resources resources, @UserIdInt int id)105     private Bitmap getUserDefaultIcon(Resources resources, @UserIdInt int id) {
106         return UserIcons.convertToBitmap(
107                 UserIcons.getDefaultUserIcon(resources, id, /* light= */ false));
108     }
109 
getGuestUserDefaultIcon(Resources resources)110     private Bitmap getGuestUserDefaultIcon(Resources resources) {
111         return getUserDefaultIcon(resources, UserHandle.USER_NULL);
112     }
113 }
114