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