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.car.userlib.CarUserManagerHelper; 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 27 import androidx.core.graphics.drawable.RoundedBitmapDrawable; 28 import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; 29 30 import com.android.car.settings.R; 31 32 /** 33 * Simple class for providing icons for users in Settings. 34 */ 35 public class UserIconProvider { 36 private final CarUserManagerHelper mCarUserManagerHelper; 37 UserIconProvider(CarUserManagerHelper userManagerHelper)38 public UserIconProvider(CarUserManagerHelper userManagerHelper) { 39 mCarUserManagerHelper = userManagerHelper; 40 } 41 42 /** 43 * Gets the icon for the given user to use in settings. 44 * If icon has not been assigned to this user, it defaults to a generic user icon. 45 * 46 * @param userInfo User for which the icon is requested. 47 * @return Drawable representing the icon for the user. 48 */ getUserIcon(UserInfo userInfo, Context context)49 public Drawable getUserIcon(UserInfo userInfo, Context context) { 50 Bitmap icon = mCarUserManagerHelper.getUserIcon(userInfo); 51 if (icon == null) { 52 // Return default user icon. 53 return context.getDrawable(R.drawable.ic_user); 54 } 55 Resources res = context.getResources(); 56 BitmapDrawable scaledIcon = (BitmapDrawable) mCarUserManagerHelper.scaleUserIcon(icon, res 57 .getDimensionPixelSize(R.dimen.icon_size)); 58 59 // Enforce that the icon is circular 60 RoundedBitmapDrawable circleIcon = RoundedBitmapDrawableFactory 61 .create(res, scaledIcon.getBitmap()); 62 circleIcon.setCircular(true); 63 return circleIcon; 64 } 65 66 /** 67 * Gets the default icon for guest user. 68 * 69 * @return Drawable representing the default guest icon. 70 */ getDefaultGuestIcon(Context context)71 public Drawable getDefaultGuestIcon(Context context) { 72 return UserIconProvider.scaleUserIcon(mCarUserManagerHelper.getGuestDefaultIcon(), 73 mCarUserManagerHelper, context); 74 } 75 76 /** 77 * Scales passed in bitmap to the appropriate user icon size. 78 * 79 * @param bitmap Bitmap to scale. 80 * @return Drawable scaled to the user icon size. 81 */ scaleUserIcon(Bitmap bitmap, CarUserManagerHelper userManagerHelper, Context context)82 public static Drawable scaleUserIcon(Bitmap bitmap, CarUserManagerHelper userManagerHelper, 83 Context context) { 84 return userManagerHelper.scaleUserIcon(bitmap, context.getResources() 85 .getDimensionPixelSize(R.dimen.icon_size)); 86 } 87 } 88