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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/*
* 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.contacts.common.lettertiles;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.telecom.TelecomManager;
import android.text.TextUtils;
import com.android.contacts.common.R;
import com.android.contacts.common.lettertiles.LetterTileDrawable.ContactType;
import com.android.dialer.common.Assert;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* A drawable that encapsulates all the functionality needed to display a letter tile to represent a
* contact image.
*/
public class LetterTileDrawable extends Drawable {
/**
* ContactType indicates the avatar type of the contact. For a person or for the default when no
* name is provided, it is {@link #TYPE_DEFAULT}, otherwise, for a business it is {@link
* #TYPE_BUSINESS}, and voicemail contacts should use {@link #TYPE_VOICEMAIL}.
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef({TYPE_PERSON, TYPE_BUSINESS, TYPE_VOICEMAIL, TYPE_GENERIC_AVATAR, TYPE_SPAM})
public @interface ContactType {}
/** Contact type constants */
public static final int TYPE_PERSON = 1;
public static final int TYPE_BUSINESS = 2;
public static final int TYPE_VOICEMAIL = 3;
/**
* A generic avatar that features the default icon, default color, and no letter. Useful for
* situations where a contact is anonymous.
*/
public static final int TYPE_GENERIC_AVATAR = 4;
public static final int TYPE_SPAM = 5;
public static final int TYPE_CONFERENCE = 6;
@ContactType public static final int TYPE_DEFAULT = TYPE_PERSON;
/**
* Shape indicates the letter tile shape. It can be either a {@link #SHAPE_CIRCLE}, otherwise, it
* is a {@link #SHAPE_RECTANGLE}.
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef({SHAPE_CIRCLE, SHAPE_RECTANGLE})
public @interface Shape {}
/** Shape constants */
public static final int SHAPE_CIRCLE = 1;
public static final int SHAPE_RECTANGLE = 2;
/** 54% opacity */
private static final int ALPHA = 138;
/** 100% opacity */
private static final int SPAM_ALPHA = 255;
/** Default icon scale for vector drawable. */
private static final float VECTOR_ICON_SCALE = 0.7f;
private static Paint sPaint;
private static final Rect sRect = new Rect();
private static final char[] sFirstChar = new char[1];
private static volatile Integer sSpamColor;
private static volatile Integer sDefaultColor;
private static volatile Integer sTileFontColor;
private static volatile Float sLetterToTileRatio;
private static volatile TypedArray sColors;
private static volatile Drawable sDefaultPersonAvatar;
private static volatile Drawable sDefaultBusinessAvatar;
private static volatile Drawable sDefaultVoicemailAvatar;
private static volatile Drawable sDefaultSpamAvatar;
private static volatile Drawable sDefaultConferenceAvatar;
private final Paint mPaint;
@ContactType private int mContactType = TYPE_DEFAULT;
private float mScale = 1.0f;
private float mOffset = 0.0f;
private boolean mIsCircle = false;
private int mColor;
private Character mLetter = null;
private String mDisplayName;
public LetterTileDrawable(final Resources res) {
// Guard static members with null checks to insulate against concurrent calls to the
// constructor.
if (sSpamColor == null) {
sSpamColor = res.getColor(R.color.spam_contact_background);
}
if (sDefaultColor == null) {
sDefaultColor = res.getColor(R.color.letter_tile_default_color);
}
if (sTileFontColor == null) {
sTileFontColor = res.getColor(R.color.letter_tile_font_color);
}
if (sTileFontColor == null) {
sLetterToTileRatio = res.getFraction(R.dimen.letter_to_tile_ratio, 1, 1);
}
if (sColors == null) {
sColors = res.obtainTypedArray(R.array.letter_tile_colors);
}
if (sDefaultPersonAvatar == null) {
sDefaultPersonAvatar =
res.getDrawable(R.drawable.product_logo_avatar_anonymous_white_color_120, null);
}
if (sDefaultBusinessAvatar == null) {
sDefaultBusinessAvatar = res.getDrawable(R.drawable.quantum_ic_business_vd_theme_24, null);
}
if (sDefaultVoicemailAvatar == null) {
sDefaultVoicemailAvatar = res.getDrawable(R.drawable.quantum_ic_voicemail_vd_theme_24, null);
}
if (sDefaultSpamAvatar == null) {
sDefaultSpamAvatar = res.getDrawable(R.drawable.quantum_ic_report_vd_theme_24, null);
}
if (sDefaultConferenceAvatar == null) {
sDefaultConferenceAvatar = res.getDrawable(R.drawable.quantum_ic_group_vd_theme_24, null);
}
// Calls in this method are potentially not idempotent, and need to be set after
// the member is initilaized.
this.initializeDefaultPaintOptions(res);
// These values should be reset on every call to the constructor.
mPaint = new Paint();
mPaint.setFilterBitmap(true);
mPaint.setDither(true);
mColor = sDefaultColor;
}
private static void initializeDefaultPaintOptions(final Resources res) {
if (sPaint == null) {
synchronized (LetterTileDrawable.class) {
sPaint = new Paint();
sPaint.setTypeface(
Typeface.create(
res.getString(R.string.letter_tile_letter_font_family), Typeface.NORMAL));
sPaint.setTextAlign(Align.CENTER);
sPaint.setAntiAlias(true);
}
}
}
private Rect getScaledBounds(float scale, float offset) {
// The drawable should be drawn in the middle of the canvas without changing its width to
// height ratio.
final Rect destRect = copyBounds();
// Crop the destination bounds into a square, scaled and offset as appropriate
final int halfLength = (int) (scale * Math.min(destRect.width(), destRect.height()) / 2);
destRect.set(
destRect.centerX() - halfLength,
(int) (destRect.centerY() - halfLength + offset * destRect.height()),
destRect.centerX() + halfLength,
(int) (destRect.centerY() + halfLength + offset * destRect.height()));
return destRect;
}
private Drawable getDrawableForContactType(int contactType) {
switch (contactType) {
case TYPE_BUSINESS:
mScale = VECTOR_ICON_SCALE;
return sDefaultBusinessAvatar;
case TYPE_VOICEMAIL:
mScale = VECTOR_ICON_SCALE;
return sDefaultVoicemailAvatar;
case TYPE_SPAM:
mScale = VECTOR_ICON_SCALE;
return sDefaultSpamAvatar;
case TYPE_CONFERENCE:
mScale = VECTOR_ICON_SCALE;
return sDefaultConferenceAvatar;
case TYPE_PERSON:
case TYPE_GENERIC_AVATAR:
default:
return sDefaultPersonAvatar;
}
}
private static boolean isEnglishLetter(final char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}
@Override
public void draw(@NonNull final Canvas canvas) {
final Rect bounds = getBounds();
if (!isVisible() || bounds.isEmpty()) {
return;
}
// Draw letter tile.
drawLetterTile(canvas);
}
public Bitmap getBitmap(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
this.setBounds(0, 0, width, height);
Canvas canvas = new Canvas(bitmap);
this.draw(canvas);
return bitmap;
}
private void drawLetterTile(final Canvas canvas) {
// Draw background color.
sPaint.setColor(mColor);
sPaint.setAlpha(mPaint.getAlpha());
final Rect bounds = getBounds();
final int minDimension = Math.min(bounds.width(), bounds.height());
if (mIsCircle) {
canvas.drawCircle(bounds.centerX(), bounds.centerY(), minDimension / 2, sPaint);
} else {
canvas.drawRect(bounds, sPaint);
}
// Draw letter/digit only if the first character is an english letter or there's a override
if (mLetter != null) {
// Draw letter or digit.
sFirstChar[0] = mLetter;
// Scale text by canvas bounds and user selected scaling factor
sPaint.setTextSize(mScale * sLetterToTileRatio * minDimension);
sPaint.getTextBounds(sFirstChar, 0, 1, sRect);
sPaint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
sPaint.setColor(sTileFontColor);
sPaint.setAlpha(ALPHA);
// Draw the letter in the canvas, vertically shifted up or down by the user-defined
// offset
canvas.drawText(
sFirstChar,
0,
1,
bounds.centerX(),
bounds.centerY() + mOffset * bounds.height() - sRect.exactCenterY(),
sPaint);
} else {
// Draw the default image if there is no letter/digit to be drawn
Drawable drawable = getDrawableForContactType(mContactType);
if (drawable == null) {
throw Assert.createIllegalStateFailException(
"Unable to find drawable for contact type " + mContactType);
}
drawable.setBounds(getScaledBounds(mScale, mOffset));
drawable.setAlpha(drawable == sDefaultSpamAvatar ? SPAM_ALPHA : ALPHA);
drawable.draw(canvas);
}
}
public int getColor() {
return mColor;
}
public LetterTileDrawable setColor(int color) {
mColor = color;
return this;
}
/** Returns a deterministic color based on the provided contact identifier string. */
private int pickColor(final String identifier) {
if (mContactType == TYPE_SPAM) {
return sSpamColor;
}
if (mContactType == TYPE_VOICEMAIL
|| mContactType == TYPE_BUSINESS
|| TextUtils.isEmpty(identifier)) {
return sDefaultColor;
}
// String.hashCode() implementation is not supposed to change across java versions, so
// this should guarantee the same email address always maps to the same color.
// The email should already have been normalized by the ContactRequest.
final int color = Math.abs(identifier.hashCode()) % sColors.length();
return sColors.getColor(color, sDefaultColor);
}
@Override
public void setAlpha(final int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(final ColorFilter cf) {
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return android.graphics.PixelFormat.OPAQUE;
}
@Override
public void getOutline(Outline outline) {
if (mIsCircle) {
outline.setOval(getBounds());
} else {
outline.setRect(getBounds());
}
outline.setAlpha(1);
}
/**
* Scale the drawn letter tile to a ratio of its default size
*
* @param scale The ratio the letter tile should be scaled to as a percentage of its default size,
* from a scale of 0 to 2.0f. The default is 1.0f.
*/
public LetterTileDrawable setScale(float scale) {
mScale = scale;
return this;
}
/**
* Assigns the vertical offset of the position of the letter tile to the ContactDrawable
*
* @param offset The provided offset must be within the range of -0.5f to 0.5f. If set to -0.5f,
* the letter will be shifted upwards by 0.5 times the height of the canvas it is being drawn
* on, which means it will be drawn with the center of the letter starting at the top edge of
* the canvas. If set to 0.5f, the letter will be shifted downwards by 0.5 times the height of
* the canvas it is being drawn on, which means it will be drawn with the center of the letter
* starting at the bottom edge of the canvas. The default is 0.0f.
*/
public LetterTileDrawable setOffset(float offset) {
Assert.checkArgument(offset >= -0.5f && offset <= 0.5f);
mOffset = offset;
return this;
}
public LetterTileDrawable setLetter(Character letter) {
mLetter = letter;
return this;
}
public Character getLetter() {
return this.mLetter;
}
private LetterTileDrawable setLetterAndColorFromContactDetails(
final String displayName, final String identifier) {
if (!TextUtils.isEmpty(displayName) && isEnglishLetter(displayName.charAt(0))) {
mLetter = Character.toUpperCase(displayName.charAt(0));
} else {
mLetter = null;
}
mColor = pickColor(identifier);
return this;
}
public LetterTileDrawable setContactType(@ContactType int contactType) {
mContactType = contactType;
return this;
}
@ContactType
public int getContactType() {
return this.mContactType;
}
public LetterTileDrawable setIsCircular(boolean isCircle) {
mIsCircle = isCircle;
return this;
}
public boolean tileIsCircular() {
return this.mIsCircle;
}
/**
* Creates a canonical letter tile for use across dialer fragments.
*
* @param displayName The display name to produce the letter in the tile. Null values or numbers
* yield no letter.
* @param identifierForTileColor The string used to produce the tile color.
* @param shape The shape of the tile.
* @param contactType The type of contact, e.g. TYPE_VOICEMAIL.
* @return this
*/
public LetterTileDrawable setCanonicalDialerLetterTileDetails(
@Nullable final String displayName,
@Nullable final String identifierForTileColor,
@Shape final int shape,
final int contactType) {
this.setIsCircular(shape == SHAPE_CIRCLE);
/**
* We return quickly under the following conditions: 1. We are asked to draw a default tile, and
* no coloring information is provided, meaning no further initialization is necessary OR 2.
* We've already invoked this method before, set mDisplayName, and found that it has not
* changed. This is useful during events like hangup, when we lose the call state for special
* types of contacts, like voicemail. We keep track of the special case until we encounter a new
* display name.
*/
if (contactType == TYPE_DEFAULT
&& ((displayName == null && identifierForTileColor == null)
|| (displayName != null && displayName.equals(mDisplayName)))) {
return this;
}
this.mDisplayName = displayName;
setContactType(contactType);
// Special contact types receive default color and no letter tile, but special iconography.
if (contactType != TYPE_PERSON) {
this.setLetterAndColorFromContactDetails(null, null);
} else {
if (identifierForTileColor != null) {
this.setLetterAndColorFromContactDetails(displayName, identifierForTileColor);
} else {
this.setLetterAndColorFromContactDetails(displayName, displayName);
}
}
return this;
}
/**
* Returns the appropriate LetterTileDrawable.TYPE_ based on the given primitive conditions.
*
* <p>If no special state is detected, yields TYPE_DEFAULT
*/
public static @ContactType int getContactTypeFromPrimitives(
boolean isVoicemailNumber,
boolean isSpam,
boolean isBusiness,
int numberPresentation,
boolean isConference) {
if (isVoicemailNumber) {
return LetterTileDrawable.TYPE_VOICEMAIL;
} else if (isSpam) {
return LetterTileDrawable.TYPE_SPAM;
} else if (isBusiness) {
return LetterTileDrawable.TYPE_BUSINESS;
} else if (numberPresentation == TelecomManager.PRESENTATION_RESTRICTED) {
return LetterTileDrawable.TYPE_GENERIC_AVATAR;
} else if (isConference) {
return LetterTileDrawable.TYPE_CONFERENCE;
} else {
return LetterTileDrawable.TYPE_DEFAULT;
}
}
}
|