1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mail.bitmap;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.Typeface;
import com.android.mail.R;
/**
* A contact drawable with the default avatar as a letter tile.
*/
public class ContactDrawable extends AbstractAvatarDrawable {
/** Letter tile */
private ColorPicker mTileColorPicker;
/** Reusable components to avoid new allocations */
private static int sTileLetterFontSize;
private static int sTileFontColor;
private static Bitmap DEFAULT_AVATAR;
private static final Paint sPaint = new Paint();
private static final Rect sRect = new Rect();
private static final char[] sFirstChar = new char[1];
public ContactDrawable(final Resources res) {
super(res);
if (sTileLetterFontSize == 0) {
sTileLetterFontSize = res.getDimensionPixelSize(R.dimen.tile_letter_font_size_small);
sTileFontColor = res.getColor(R.color.letter_tile_font_color);
DEFAULT_AVATAR = BitmapFactory.decodeResource(res, R.drawable.ic_anonymous_avatar_40dp);
sPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
sPaint.setTextAlign(Align.CENTER);
sPaint.setAntiAlias(true);
}
}
/**
* Sets the {@link ColorPicker} for the background tile used in letter avatars.
* @param colorPicker
*/
public void setTileColorPicker(ColorPicker colorPicker) {
mTileColorPicker = colorPicker;
}
/**
* Returns the color picker for the background tile used in the letter avatars.
* If none was set, initializes a simple {@link ColorPicker.PaletteColorPicker} first.
* @return non-null color picker.
*/
public ColorPicker getTileColorPicker() {
if (mTileColorPicker == null) {
mTileColorPicker = new ColorPicker.PaletteColorPicker(mResources);
}
return mTileColorPicker;
}
@Override
protected void drawDefaultAvatar(Canvas canvas) {
// Draw letter tile as default
drawLetterTile(canvas);
}
private void drawLetterTile(final Canvas canvas) {
if (mContactRequest == null) {
return;
}
final Rect bounds = getBounds();
// Draw background color.
final String email = mContactRequest.getEmail();
// The email should already have been normalized by the ContactRequest.
sPaint.setColor(getTileColorPicker().pickColor(email));
sPaint.setAlpha(mBitmapPaint.getAlpha());
drawCircle(canvas, bounds, sPaint);
// Draw letter/digit or generic avatar.
final String displayName = mContactRequest.getDisplayName();
final char firstChar = displayName.charAt(0);
if (isEnglishLetterOrDigit(firstChar)) {
// Draw letter or digit.
sFirstChar[0] = Character.toUpperCase(firstChar);
sPaint.setTextSize(sTileLetterFontSize);
sPaint.getTextBounds(sFirstChar, 0, 1, sRect);
sPaint.setColor(sTileFontColor);
canvas.drawText(sFirstChar, 0, 1, bounds.centerX(),
bounds.centerY() + sRect.height() / 2, sPaint);
} else {
drawBitmap(DEFAULT_AVATAR, DEFAULT_AVATAR.getWidth(), DEFAULT_AVATAR.getHeight(),
canvas);
}
}
private static boolean isEnglishLetterOrDigit(final char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9');
}
}
|