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.profiles; 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 com.android.car.admin.ui.UserAvatarView; 30 import com.android.car.settings.R; 31 import com.android.internal.util.UserIcons; 32 33 /** 34 * Simple class for providing icons for profiles in Settings. 35 */ 36 public class ProfileIconProvider { 37 38 /** 39 * Gets a scaled rounded icon for the given profile to use in settings. If a profile does 40 * not have an icon saved, this method will default to a generic icon and update UserManager to 41 * use that icon. 42 * 43 * @param userInfo User for which the icon is requested. 44 * @param context Context to use for resources 45 * @return {@link Drawable} representing the icon for the user. 46 */ getRoundedProfileIcon(UserInfo userInfo, Context context)47 public Drawable getRoundedProfileIcon(UserInfo userInfo, Context context) { 48 UserManager userManager = UserManager.get(context); 49 Resources res = context.getResources(); 50 Bitmap icon = userManager.getUserIcon(userInfo.id); 51 52 if (icon == null) { 53 icon = assignDefaultIcon(userManager, res, userInfo); 54 } 55 56 return new BitmapDrawable(res, icon); 57 } 58 59 /** Returns a scaled, rounded, default icon for the Guest profile */ getRoundedGuestDefaultIcon(Resources resources)60 public Drawable getRoundedGuestDefaultIcon(Resources resources) { 61 Bitmap icon = getGuestProfileDefaultIcon(resources); 62 return new BitmapDrawable(resources, icon); 63 } 64 65 /** 66 * Assigns a default icon to a profile according to the user's id. Handles Guest icon and 67 * non-guest profile icons. 68 * 69 * @param userManager {@link UserManager} to set user icon 70 * @param resources {@link Resources} to grab icons from 71 * @param userInfo User whose avatar is set to default icon. 72 * @return Bitmap of the profile icon. 73 */ assignDefaultIcon( UserManager userManager, Resources resources, UserInfo userInfo)74 public Bitmap assignDefaultIcon( 75 UserManager userManager, Resources resources, UserInfo userInfo) { 76 Bitmap bitmap = userInfo.isGuest() 77 ? getGuestProfileDefaultIcon(resources) 78 : getProfileDefaultIcon(resources, userInfo.id); 79 userManager.setUserIcon(userInfo.id, bitmap); 80 return bitmap; 81 } 82 83 // TODO (b/179802719): refactor this method into getRoundedUserIcon(). 84 /** 85 * Gets badge to profile icon Drawable if the profile is managed. 86 * 87 * @param context to use for the avatar view 88 * @param userInfo User for which the icon is requested and badge is set 89 * @return {@link Drawable} with badge 90 */ getDrawableWithBadge(Context context, UserInfo userInfo)91 public Drawable getDrawableWithBadge(Context context, UserInfo userInfo) { 92 Drawable userIcon = getRoundedProfileIcon(userInfo, context); 93 int iconSize = userIcon.getIntrinsicWidth(); 94 UserAvatarView userAvatarView = new UserAvatarView(context); 95 float badgeToIconSizeRatio = 96 context.getResources().getDimension(R.dimen.profile_switcher_managed_badge_size) 97 / context.getResources().getDimension( 98 R.dimen.profile_switcher_image_avatar_size); 99 userAvatarView.setBadgeDiameter(iconSize * badgeToIconSizeRatio); 100 float badgePadding = context.getResources().getDimension( 101 R.dimen.profile_switcher_managed_badge_margin); 102 userAvatarView.setBadgeMargin(badgePadding); 103 userAvatarView.setDrawableWithBadge(userIcon, userInfo.id); 104 Drawable badgedIcon = userAvatarView.getUserIconDrawable(); 105 badgedIcon.setBounds(0, 0, iconSize, iconSize); 106 return badgedIcon; 107 } 108 109 /** 110 * Gets a bitmap representing the profile's default avatar. 111 * 112 * @param resources The resources to pull from 113 * @param id The id of the user to get the icon for. Pass {@link UserHandle#USER_NULL} for 114 * Guest user. 115 * @return Default profile icon 116 */ getProfileDefaultIcon(Resources resources, @UserIdInt int id)117 private Bitmap getProfileDefaultIcon(Resources resources, @UserIdInt int id) { 118 return UserIcons.convertToBitmap( 119 UserIcons.getDefaultUserIcon(resources, id, /* light= */ false)); 120 } 121 getGuestProfileDefaultIcon(Resources resources)122 private Bitmap getGuestProfileDefaultIcon(Resources resources) { 123 return getProfileDefaultIcon(resources, UserHandle.USER_NULL); 124 } 125 } 126