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.content.Context; 20 import android.content.pm.UserInfo; 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.UserManager; 26 27 import com.android.car.admin.ui.UserAvatarView; 28 import com.android.car.internal.user.UserHelper; 29 import com.android.car.settings.R; 30 31 /** 32 * Simple class for providing icons for profiles in Settings. 33 */ 34 public class ProfileIconProvider { 35 36 /** 37 * Gets a scaled rounded icon for the given profile to use in settings. If a profile does 38 * not have an icon saved, this method will default to a generic icon and update UserManager to 39 * use that icon. 40 * 41 * @param userInfo User for which the icon is requested. 42 * @param context Context to use for resources 43 * @return {@link Drawable} representing the icon for the user. 44 */ getRoundedProfileIcon(UserInfo userInfo, Context context)45 public Drawable getRoundedProfileIcon(UserInfo userInfo, Context context) { 46 UserManager userManager = UserManager.get(context); 47 Resources res = context.getResources(); 48 Bitmap icon = userManager.getUserIcon(userInfo.id); 49 50 if (icon == null) { 51 icon = UserHelper.assignDefaultIcon(context, userInfo.getUserHandle()); 52 } 53 54 return new BitmapDrawable(res, icon); 55 } 56 57 /** Returns a scaled, rounded, default icon for the Guest profile */ getRoundedGuestDefaultIcon(Context context)58 public Drawable getRoundedGuestDefaultIcon(Context context) { 59 Bitmap icon = UserHelper.getGuestDefaultIcon(context); 60 return new BitmapDrawable(context.getResources(), icon); 61 } 62 63 // TODO (b/179802719): refactor this method into getRoundedUserIcon(). 64 /** 65 * Gets badge to profile icon Drawable if the profile is managed. 66 * 67 * @param context to use for the avatar view 68 * @param userInfo User for which the icon is requested and badge is set 69 * @return {@link Drawable} with badge 70 */ getDrawableWithBadge(Context context, UserInfo userInfo)71 public Drawable getDrawableWithBadge(Context context, UserInfo userInfo) { 72 Drawable userIcon = getRoundedProfileIcon(userInfo, context); 73 int iconSize = userIcon.getIntrinsicWidth(); 74 UserAvatarView userAvatarView = new UserAvatarView(context); 75 float badgeToIconSizeRatio = 76 context.getResources().getDimension(R.dimen.profile_switcher_managed_badge_size) 77 / context.getResources().getDimension( 78 R.dimen.profile_switcher_image_avatar_size); 79 userAvatarView.setBadgeDiameter(iconSize * badgeToIconSizeRatio); 80 float badgePadding = context.getResources().getDimension( 81 R.dimen.profile_switcher_managed_badge_margin); 82 userAvatarView.setBadgeMargin(badgePadding); 83 userAvatarView.setDrawableWithBadge(userIcon, userInfo.id); 84 Drawable badgedIcon = userAvatarView.getUserIconDrawable(); 85 badgedIcon.setBounds(0, 0, iconSize, iconSize); 86 return badgedIcon; 87 } 88 } 89