1 /* 2 * Copyright (C) 2023 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.internal.user; 18 19 import android.annotation.ColorInt; 20 import android.annotation.NonNull; 21 import android.content.Context; 22 import android.content.pm.UserInfo; 23 import android.content.res.Resources; 24 import android.graphics.Bitmap; 25 import android.graphics.BlendMode; 26 import android.graphics.BlendModeColorFilter; 27 import android.graphics.Canvas; 28 import android.graphics.Paint; 29 import android.graphics.Rect; 30 import android.graphics.drawable.Drawable; 31 32 import android.text.TextUtils; 33 import android.util.Log; 34 import com.android.internal.util.UserIcons; 35 36 /** 37 * Helper class for providing Car user icons. 38 * 39 * @hide 40 */ 41 class CarUserIconProvider { 42 private static final String TAG = "CarUserIconProvider"; 43 44 private static final int[] USER_NAME_ICON_COLORS = { 45 R.color.car_internal_user_name_icon_1, 46 R.color.car_internal_user_name_icon_2, 47 R.color.car_internal_user_name_icon_3, 48 R.color.car_internal_user_name_icon_4, 49 R.color.car_internal_user_name_icon_5, 50 R.color.car_internal_user_name_icon_6, 51 R.color.car_internal_user_name_icon_7, 52 R.color.car_internal_user_name_icon_8 53 }; 54 55 private static final int[] USER_BACKGROUND_ICON_COLORS = { 56 R.color.car_internal_user_background_icon_1, 57 R.color.car_internal_user_background_icon_2, 58 R.color.car_internal_user_background_icon_3, 59 R.color.car_internal_user_background_icon_4, 60 R.color.car_internal_user_background_icon_5, 61 R.color.car_internal_user_background_icon_6, 62 R.color.car_internal_user_background_icon_7, 63 R.color.car_internal_user_background_icon_8 64 }; 65 getDefaultUserIcon(@onNull Context context, @NonNull UserInfo userInfo)66 static Bitmap getDefaultUserIcon(@NonNull Context context, @NonNull UserInfo userInfo) { 67 Resources resources = context.getResources(); 68 if (userInfo.isGuest()) { 69 return getGuestDefaultUserIcon(resources); 70 } 71 72 Drawable icon = resources.getDrawable( 73 R.drawable.car_internal_user_icon_circle_background, /* theme= */ null) 74 .mutate(); 75 icon.setBounds(/* left= */ 0, /* top= */ 0, 76 icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); 77 // Set color for the background of the user icon. 78 int backgroundColor = getUserBackgroundIconColor(context, userInfo); 79 icon.setColorFilter(new BlendModeColorFilter(backgroundColor, BlendMode.SRC_IN)); 80 81 Bitmap userIconBitmap = UserIcons.convertToBitmap(icon); 82 83 String name = userInfo.name; 84 if (TextUtils.isEmpty(name)) { 85 Log.w(TAG, "User name is empty."); 86 return userIconBitmap; 87 } 88 89 // Set the first letter of user name as user icon. 90 String firstLetter = name.substring(/* beginIndex= */ 0, /* endIndex= */ 1); 91 Paint paint = new Paint(); 92 paint.setStyle(Paint.Style.FILL); 93 paint.setColor(getUserNameIconColor(context, userInfo)); 94 paint.setTextSize(resources.getDimension(R.dimen.car_internal_user_icon_text_size)); 95 paint.setTextAlign(Paint.Align.LEFT); 96 97 // Draw text in center of the canvas. 98 Canvas canvas = new Canvas(userIconBitmap); 99 Rect textBounds = new Rect(); 100 paint.getTextBounds(firstLetter, /*start=*/0, /*end=*/1, textBounds); 101 float x = canvas.getWidth() * 0.5f - textBounds.exactCenterX(); 102 float y = canvas.getHeight() * 0.5f - textBounds.exactCenterY(); 103 canvas.drawText(firstLetter, x, y, paint); 104 105 return userIconBitmap; 106 } 107 getGuestDefaultUserIcon(Resources resources)108 static Bitmap getGuestDefaultUserIcon(Resources resources) { 109 return UserIcons.convertToBitmap( 110 resources.getDrawable(R.drawable.car_internal_guest_user_icon, null)); 111 } 112 113 @ColorInt getUserNameIconColor(@onNull Context context, @NonNull UserInfo userInfo)114 static int getUserNameIconColor(@NonNull Context context, @NonNull UserInfo userInfo) { 115 if (userInfo.isGuest()) { 116 return context.getColor(R.color.car_internal_guest_user_avatar_color); 117 } 118 return context.getColor(USER_NAME_ICON_COLORS[userInfo.id % USER_NAME_ICON_COLORS.length]); 119 } 120 121 @ColorInt getUserBackgroundIconColor(@onNull Context context, @NonNull UserInfo userInfo)122 static int getUserBackgroundIconColor(@NonNull Context context, @NonNull UserInfo userInfo) { 123 if (userInfo.isGuest()) { 124 return context.getColor(R.color.car_internal_guest_user_background_color); 125 } 126 return context.getColor( 127 USER_BACKGROUND_ICON_COLORS[userInfo.id % USER_BACKGROUND_ICON_COLORS.length]); 128 } 129 } 130