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