• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.internal.util;
18 
19 import android.annotation.ColorInt;
20 import android.content.res.Resources;
21 import android.graphics.Bitmap;
22 import android.graphics.Canvas;
23 import android.graphics.PorterDuff.Mode;
24 import android.graphics.drawable.Drawable;
25 import android.os.UserHandle;
26 
27 import com.android.internal.R;
28 
29 /**
30  * Helper class that generates default user icons.
31  */
32 public class UserIcons {
33 
34     private static final int[] USER_ICON_COLORS = {
35         R.color.user_icon_1,
36         R.color.user_icon_2,
37         R.color.user_icon_3,
38         R.color.user_icon_4,
39         R.color.user_icon_5,
40         R.color.user_icon_6,
41         R.color.user_icon_7,
42         R.color.user_icon_8
43     };
44 
45     /**
46      * Converts a given drawable to a bitmap.
47      */
convertToBitmap(Drawable icon)48     public static Bitmap convertToBitmap(Drawable icon) {
49         return convertToBitmapAtSize(icon, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
50     }
51 
52     /**
53      * Converts a given drawable to a bitmap, with width and height equal to the default icon size.
54      */
convertToBitmapAtUserIconSize(Resources res, Drawable icon)55     public static Bitmap convertToBitmapAtUserIconSize(Resources res, Drawable icon) {
56         int size = res.getDimensionPixelSize(R.dimen.user_icon_size);
57         return convertToBitmapAtSize(icon, size, size);
58     }
59 
convertToBitmapAtSize(Drawable icon, int width, int height)60     private static Bitmap convertToBitmapAtSize(Drawable icon, int width, int height) {
61         if (icon == null) {
62             return null;
63         }
64         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
65         Canvas canvas = new Canvas(bitmap);
66         icon.setBounds(0, 0, width, height);
67         icon.draw(canvas);
68         return bitmap;
69     }
70 
71     /**
72      * Returns a default user icon for the given user.
73      *
74      * Note that for guest users, you should pass in {@code UserHandle.USER_NULL}.
75      *
76      * @param resources resources object to fetch user icon / color.
77      * @param userId the user id or {@code UserHandle.USER_NULL} for a non-user specific icon
78      * @param light whether we want a light icon (suitable for a dark background)
79      */
getDefaultUserIcon(Resources resources, int userId, boolean light)80     public static Drawable getDefaultUserIcon(Resources resources, int userId, boolean light) {
81         int colorResId = light ? R.color.user_icon_default_white : R.color.user_icon_default_gray;
82         if (userId != UserHandle.USER_NULL) {
83             // Return colored icon instead
84             colorResId = USER_ICON_COLORS[userId % USER_ICON_COLORS.length];
85         }
86         return getDefaultUserIconInColor(resources, resources.getColor(colorResId, null));
87     }
88 
89     /**
90      * Returns a default user icon in a particular color.
91      *
92      * @param resources resources object to fetch the user icon
93      * @param color the color used for the icon
94      */
getDefaultUserIconInColor(Resources resources, @ColorInt int color)95     public static Drawable getDefaultUserIconInColor(Resources resources, @ColorInt int color) {
96         Drawable icon = resources.getDrawable(R.drawable.ic_account_circle, null).mutate();
97         icon.setColorFilter(color, Mode.SRC_IN);
98         icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
99         return icon;
100     }
101 
102     /**
103      * Returns an array containing colors to be used for default user icons.
104      */
getUserIconColors(Resources resources)105     public static int[] getUserIconColors(Resources resources) {
106         int[] result = new int[USER_ICON_COLORS.length];
107         for (int i = 0; i < result.length; i++) {
108             result[i] = resources.getColor(USER_ICON_COLORS[i], null);
109         }
110         return result;
111     }
112 }
113