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.user.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 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; 27 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; 28 29 import com.android.car.settings.R; 30 31 /** 32 * Simple class for providing icons for users in Settings. 33 */ 34 public class UserIconProvider { 35 private final CarUserManagerHelper mCarUserManagerHelper; 36 UserIconProvider(CarUserManagerHelper userManagerHelper)37 public UserIconProvider(CarUserManagerHelper userManagerHelper) { 38 mCarUserManagerHelper = userManagerHelper; 39 } 40 41 /** 42 * Gets the icon for the given user to use in settings. 43 * If icon has not been assigned to this user, it defaults to a generic user icon. 44 * 45 * @param userInfo User for which the icon is requested. 46 * 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.car_primary_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 * Scales passed in bitmap to the appropriate user icon size. 68 * 69 * @param bitmap Bitmap to scale. 70 * 71 * @return Drawable scaled to the user icon size. 72 */ scaleUserIcon(Bitmap bitmap, CarUserManagerHelper userManagerHelper, Context context)73 public static Drawable scaleUserIcon(Bitmap bitmap, CarUserManagerHelper userManagerHelper, 74 Context context) { 75 return userManagerHelper.scaleUserIcon(bitmap, context.getResources() 76 .getDimensionPixelSize(R.dimen.car_primary_icon_size)); 77 } 78 } 79