| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 47 |
|
| 48 |
public static Bitmap convertToBitmap(Drawable icon) { |
| 49 |
return convertToBitmapAtSize(icon, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 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 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 84 |
colorResId = USER_ICON_COLORS[userId % USER_ICON_COLORS.length]; |
| 85 |
} |
| 86 |
return getDefaultUserIconInColor(resources, resources.getColor(colorResId, null)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 104 |
|
| 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 |
} |