1package eu.siacs.conversations.services;
2
3import android.content.Context;
4import android.content.res.Resources;
5import android.graphics.Bitmap;
6import android.graphics.BitmapFactory;
7import android.graphics.Canvas;
8import android.graphics.Paint;
9import android.graphics.PorterDuff;
10import android.graphics.PorterDuffXfermode;
11import android.graphics.Rect;
12import android.graphics.Typeface;
13import android.graphics.drawable.BitmapDrawable;
14import android.graphics.drawable.Drawable;
15import android.net.Uri;
16import android.support.annotation.ColorInt;
17import android.support.annotation.DrawableRes;
18import android.support.annotation.Nullable;
19import android.support.v4.content.res.ResourcesCompat;
20import android.text.TextUtils;
21import android.util.DisplayMetrics;
22import android.util.Log;
23import android.util.LruCache;
24
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.List;
29import java.util.Locale;
30import java.util.Set;
31
32import eu.siacs.conversations.Config;
33import eu.siacs.conversations.R;
34import eu.siacs.conversations.entities.Account;
35import eu.siacs.conversations.entities.Bookmark;
36import eu.siacs.conversations.entities.Contact;
37import eu.siacs.conversations.entities.Conversation;
38import eu.siacs.conversations.entities.Conversational;
39import eu.siacs.conversations.entities.ListItem;
40import eu.siacs.conversations.entities.Message;
41import eu.siacs.conversations.entities.MucOptions;
42import eu.siacs.conversations.utils.UIHelper;
43import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
44import eu.siacs.conversations.xmpp.XmppConnection;
45import eu.siacs.conversations.xmpp.pep.Avatar;
46import rocks.xmpp.addr.Jid;
47
48public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
49
50 private static final int FG_COLOR = 0xFFFAFAFA;
51 private static final int TRANSPARENT = 0x00000000;
52 private static final int PLACEHOLDER_COLOR = 0xFF202020;
53
54 public static final int SYSTEM_UI_AVATAR_SIZE = 48;
55
56 private static final String PREFIX_CONTACT = "contact";
57 private static final String PREFIX_CONVERSATION = "conversation";
58 private static final String PREFIX_ACCOUNT = "account";
59 private static final String PREFIX_GENERIC = "generic";
60
61 final private ArrayList<Integer> sizes = new ArrayList<>();
62 final private HashMap<String, Set<String>> conversationDependentKeys = new HashMap<>();
63
64 protected XmppConnectionService mXmppConnectionService = null;
65
66 AvatarService(XmppConnectionService service) {
67 this.mXmppConnectionService = service;
68 }
69
70 public static int getSystemUiAvatarSize(final Context context) {
71 return (int) (SYSTEM_UI_AVATAR_SIZE * context.getResources().getDisplayMetrics().density);
72 }
73
74 public Bitmap get(final Avatarable avatarable, final int size, final boolean cachedOnly) {
75 if (avatarable instanceof Account) {
76 return get((Account) avatarable,size,cachedOnly);
77 } else if (avatarable instanceof Conversation) {
78 return get((Conversation) avatarable, size, cachedOnly);
79 } else if (avatarable instanceof Message) {
80 return get((Message) avatarable, size, cachedOnly);
81 } else if (avatarable instanceof ListItem) {
82 return get((ListItem) avatarable, size, cachedOnly);
83 } else if (avatarable instanceof MucOptions.User) {
84 return get((MucOptions.User) avatarable, size, cachedOnly);
85 }
86 throw new AssertionError("AvatarService does not know how to generate avatar from "+avatarable.getClass().getName());
87
88 }
89
90 private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
91 if (contact.isSelf()) {
92 return get(contact.getAccount(), size, cachedOnly);
93 }
94 final String KEY = key(contact, size);
95 Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
96 if (avatar != null || cachedOnly) {
97 return avatar;
98 }
99 if (contact.getAvatarFilename() != null && QuickConversationsService.isQuicksy()) {
100 avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatarFilename(), size);
101 }
102 if (avatar == null && contact.getProfilePhoto() != null) {
103 avatar = mXmppConnectionService.getFileBackend().cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
104 }
105 if (avatar == null && contact.getAvatarFilename() != null) {
106 avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatarFilename(), size);
107 }
108 if (avatar == null) {
109 avatar = get(contact.getDisplayName(), contact.getJid().asBareJid().toString(), size, false);
110 }
111 this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
112 return avatar;
113 }
114
115 public Bitmap getRoundedShortcut(final Contact contact) {
116 return getRoundedShortcut(contact, false);
117 }
118
119 public Bitmap getRoundedShortcutWithIcon(final Contact contact) {
120 return getRoundedShortcut(contact, true);
121 }
122
123 private Bitmap getRoundedShortcut(final Contact contact, boolean withIcon) {
124 DisplayMetrics metrics = mXmppConnectionService.getResources().getDisplayMetrics();
125 int size = Math.round(metrics.density * 48);
126 Bitmap bitmap = get(contact, size);
127 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
128 Canvas canvas = new Canvas(output);
129 final Paint paint = new Paint();
130
131 drawAvatar(bitmap, canvas, paint);
132 if (withIcon) {
133 drawIcon(canvas, paint);
134 }
135 return output;
136 }
137
138 private void drawAvatar(Bitmap bitmap, Canvas canvas, Paint paint) {
139 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
140 paint.setAntiAlias(true);
141 canvas.drawARGB(0, 0, 0, 0);
142 canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
143 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
144 canvas.drawBitmap(bitmap, rect, rect, paint);
145 }
146
147 private void drawIcon(Canvas canvas, Paint paint) {
148 final Resources resources = mXmppConnectionService.getResources();
149 final Bitmap icon = getRoundLauncherIcon(resources);
150 if (icon == null) {
151 return;
152 }
153 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
154
155 int iconSize = Math.round(canvas.getHeight() / 2.6f);
156
157 int left = canvas.getWidth() - iconSize;
158 int top = canvas.getHeight() - iconSize;
159 final Rect rect = new Rect(left, top, left + iconSize, top + iconSize);
160 canvas.drawBitmap(icon, null, rect, paint);
161 }
162
163 private static Bitmap getRoundLauncherIcon(Resources resources) {
164
165 final Drawable drawable = ResourcesCompat.getDrawable(resources, R.mipmap.new_launcher_round,null);
166 if (drawable == null) {
167 return null;
168 }
169
170 if (drawable instanceof BitmapDrawable) {
171 return ((BitmapDrawable)drawable).getBitmap();
172 }
173
174 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
175 Canvas canvas = new Canvas(bitmap);
176 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
177 drawable.draw(canvas);
178
179 return bitmap;
180 }
181
182 public Bitmap get(final MucOptions.User user, final int size, boolean cachedOnly) {
183 Contact c = user.getContact();
184 if (c != null && (c.getProfilePhoto() != null || c.getAvatarFilename() != null || user.getAvatar() == null)) {
185 return get(c, size, cachedOnly);
186 } else {
187 return getImpl(user, size, cachedOnly);
188 }
189 }
190
191 private Bitmap getImpl(final MucOptions.User user, final int size, boolean cachedOnly) {
192 final String KEY = key(user, size);
193 Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
194 if (avatar != null || cachedOnly) {
195 return avatar;
196 }
197 if (user.getAvatar() != null) {
198 avatar = mXmppConnectionService.getFileBackend().getAvatar(user.getAvatar(), size);
199 }
200 if (avatar == null) {
201 Contact contact = user.getContact();
202 if (contact != null) {
203 avatar = get(contact, size, false);
204 } else {
205 String seed = user.getRealJid() != null ? user.getRealJid().asBareJid().toString() : null;
206 avatar = get(user.getName(), seed, size, false);
207 }
208 }
209 this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
210 return avatar;
211 }
212
213 public void clear(Contact contact) {
214 synchronized (this.sizes) {
215 for (Integer size : sizes) {
216 this.mXmppConnectionService.getBitmapCache().remove(
217 key(contact, size));
218 }
219 }
220 for (Conversation conversation : mXmppConnectionService.findAllConferencesWith(contact)) {
221 MucOptions.User user = conversation.getMucOptions().findUserByRealJid(contact.getJid().asBareJid());
222 if (user != null) {
223 clear(user);
224 }
225 clear(conversation);
226 }
227 }
228
229 private String key(Contact contact, int size) {
230 synchronized (this.sizes) {
231 if (!this.sizes.contains(size)) {
232 this.sizes.add(size);
233 }
234 }
235 return PREFIX_CONTACT +
236 '\0' +
237 contact.getAccount().getJid().asBareJid() +
238 '\0' +
239 emptyOnNull(contact.getJid()) +
240 '\0' +
241 size;
242 }
243
244 private String key(MucOptions.User user, int size) {
245 synchronized (this.sizes) {
246 if (!this.sizes.contains(size)) {
247 this.sizes.add(size);
248 }
249 }
250 return PREFIX_CONTACT +
251 '\0' +
252 user.getAccount().getJid().asBareJid() +
253 '\0' +
254 emptyOnNull(user.getFullJid()) +
255 '\0' +
256 emptyOnNull(user.getRealJid()) +
257 '\0' +
258 size;
259 }
260
261 public Bitmap get(ListItem item, int size) {
262 return get(item, size, false);
263 }
264
265 public Bitmap get(ListItem item, int size, boolean cachedOnly) {
266 if (item instanceof Contact) {
267 return get((Contact) item, size, cachedOnly);
268 } else if (item instanceof Bookmark) {
269 Bookmark bookmark = (Bookmark) item;
270 if (bookmark.getConversation() != null) {
271 return get(bookmark.getConversation(), size, cachedOnly);
272 } else {
273 Jid jid = bookmark.getJid();
274 Account account = bookmark.getAccount();
275 Contact contact = jid == null ? null : account.getRoster().getContact(jid);
276 if (contact != null && contact.getAvatarFilename() != null) {
277 return get(contact, size, cachedOnly);
278 }
279 String seed = jid != null ? jid.asBareJid().toString() : null;
280 return get(bookmark.getDisplayName(), seed, size, cachedOnly);
281 }
282 } else {
283 String seed = item.getJid() != null ? item.getJid().asBareJid().toString() : null;
284 return get(item.getDisplayName(), seed, size, cachedOnly);
285 }
286 }
287
288 public Bitmap get(Conversation conversation, int size) {
289 return get(conversation, size, false);
290 }
291
292 public Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
293 if (conversation.getMode() == Conversation.MODE_SINGLE) {
294 return get(conversation.getContact(), size, cachedOnly);
295 } else {
296 return get(conversation.getMucOptions(), size, cachedOnly);
297 }
298 }
299
300 public void clear(Conversation conversation) {
301 if (conversation.getMode() == Conversation.MODE_SINGLE) {
302 clear(conversation.getContact());
303 } else {
304 clear(conversation.getMucOptions());
305 synchronized (this.conversationDependentKeys) {
306 Set<String> keys = this.conversationDependentKeys.get(conversation.getUuid());
307 if (keys == null) {
308 return;
309 }
310 LruCache<String, Bitmap> cache = this.mXmppConnectionService.getBitmapCache();
311 for (String key : keys) {
312 cache.remove(key);
313 }
314 keys.clear();
315 }
316 }
317 }
318
319 private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) {
320 final String KEY = key(mucOptions, size);
321 Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
322 if (bitmap != null || cachedOnly) {
323 return bitmap;
324 }
325
326 bitmap = mXmppConnectionService.getFileBackend().getAvatar(mucOptions.getAvatar(), size);
327
328 if (bitmap == null) {
329 final List<MucOptions.User> users = mucOptions.getUsersRelevantForNameAndAvatar();
330 if (users.size() == 0) {
331 Conversation c = mucOptions.getConversation();
332 bitmap = getImpl(c.getName().toString(), c.getJid().asBareJid().toString(), size);
333 } else {
334 bitmap = getImpl(users, size);
335 }
336 }
337
338 this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
339
340 return bitmap;
341 }
342
343 private Bitmap get(List<MucOptions.User> users, int size, boolean cachedOnly) {
344 final String KEY = key(users, size);
345 Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
346 if (bitmap != null || cachedOnly) {
347 return bitmap;
348 }
349 bitmap = getImpl(users, size);
350 this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
351 return bitmap;
352 }
353
354 private Bitmap getImpl(List<MucOptions.User> users, int size) {
355 int count = users.size();
356 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
357 Canvas canvas = new Canvas(bitmap);
358 bitmap.eraseColor(TRANSPARENT);
359 if (count == 0) {
360 throw new AssertionError("Unable to draw tiles for 0 users");
361 } else if (count == 1) {
362 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
363 drawTile(canvas, users.get(0).getAccount(), size / 2 + 1, 0, size, size);
364 } else if (count == 2) {
365 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
366 drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
367 } else if (count == 3) {
368 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
369 drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
370 drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
371 size);
372 } else if (count == 4) {
373 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
374 drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
375 drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
376 drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
377 size);
378 } else {
379 drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
380 drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
381 drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
382 drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
383 size, size);
384 }
385 return bitmap;
386 }
387
388 public void clear(final MucOptions options) {
389 if (options == null) {
390 return;
391 }
392 synchronized (this.sizes) {
393 for (Integer size : sizes) {
394 this.mXmppConnectionService.getBitmapCache().remove(key(options, size));
395 }
396 }
397 }
398
399 private String key(final MucOptions options, int size) {
400 synchronized (this.sizes) {
401 if (!this.sizes.contains(size)) {
402 this.sizes.add(size);
403 }
404 }
405 return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
406 + "_" + String.valueOf(size);
407 }
408
409 private String key(List<MucOptions.User> users, int size) {
410 final Conversation conversation = users.get(0).getConversation();
411 StringBuilder builder = new StringBuilder("TILE_");
412 builder.append(conversation.getUuid());
413
414 for (MucOptions.User user : users) {
415 builder.append("\0");
416 builder.append(emptyOnNull(user.getRealJid()));
417 builder.append("\0");
418 builder.append(emptyOnNull(user.getFullJid()));
419 }
420 builder.append('\0');
421 builder.append(size);
422 final String key = builder.toString();
423 synchronized (this.conversationDependentKeys) {
424 Set<String> keys;
425 if (this.conversationDependentKeys.containsKey(conversation.getUuid())) {
426 keys = this.conversationDependentKeys.get(conversation.getUuid());
427 } else {
428 keys = new HashSet<>();
429 this.conversationDependentKeys.put(conversation.getUuid(), keys);
430 }
431 keys.add(key);
432 }
433 return key;
434 }
435
436 public Bitmap get(Account account, int size) {
437 return get(account, size, false);
438 }
439
440 public Bitmap get(Account account, int size, boolean cachedOnly) {
441 final String KEY = key(account, size);
442 Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
443 if (avatar != null || cachedOnly) {
444 return avatar;
445 }
446 avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
447 if (avatar == null) {
448 final String displayName = account.getDisplayName();
449 final String jid = account.getJid().asBareJid().toEscapedString();
450 if (QuickConversationsService.isQuicksy() && !TextUtils.isEmpty(displayName)) {
451 avatar = get(displayName, jid, size, false);
452 } else {
453 avatar = get(jid, null, size, false);
454 }
455 }
456 mXmppConnectionService.getBitmapCache().put(KEY, avatar);
457 return avatar;
458 }
459
460 public Bitmap get(Message message, int size, boolean cachedOnly) {
461 final Conversational conversation = message.getConversation();
462 if (message.getType() == Message.TYPE_STATUS && message.getCounterparts() != null && message.getCounterparts().size() > 1) {
463 return get(message.getCounterparts(), size, cachedOnly);
464 } else if (message.getStatus() == Message.STATUS_RECEIVED) {
465 Contact c = message.getContact();
466 if (c != null && (c.getProfilePhoto() != null || c.getAvatarFilename() != null)) {
467 return get(c, size, cachedOnly);
468 } else if (conversation instanceof Conversation && message.getConversation().getMode() == Conversation.MODE_MULTI) {
469 final Jid trueCounterpart = message.getTrueCounterpart();
470 final MucOptions mucOptions = ((Conversation) conversation).getMucOptions();
471 MucOptions.User user;
472 if (trueCounterpart != null) {
473 user = mucOptions.findOrCreateUserByRealJid(trueCounterpart, message.getCounterpart());
474 } else {
475 user = mucOptions.findUserByFullJid(message.getCounterpart());
476 }
477 if (user != null) {
478 return getImpl(user, size, cachedOnly);
479 }
480 } else if (c != null) {
481 return get(c, size, cachedOnly);
482 }
483 Jid tcp = message.getTrueCounterpart();
484 String seed = tcp != null ? tcp.asBareJid().toString() : null;
485 return get(UIHelper.getMessageDisplayName(message), seed, size, cachedOnly);
486 } else {
487 return get(conversation.getAccount(), size, cachedOnly);
488 }
489 }
490
491 public void clear(Account account) {
492 synchronized (this.sizes) {
493 for (Integer size : sizes) {
494 this.mXmppConnectionService.getBitmapCache().remove(key(account, size));
495 }
496 }
497 }
498
499 public void clear(MucOptions.User user) {
500 synchronized (this.sizes) {
501 for (Integer size : sizes) {
502 this.mXmppConnectionService.getBitmapCache().remove(key(user, size));
503 }
504 }
505 }
506
507 private String key(Account account, int size) {
508 synchronized (this.sizes) {
509 if (!this.sizes.contains(size)) {
510 this.sizes.add(size);
511 }
512 }
513 return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
514 + String.valueOf(size);
515 }
516
517 /*public Bitmap get(String name, int size) {
518 return get(name,null, size,false);
519 }*/
520
521 public Bitmap get(final String name, String seed, final int size, boolean cachedOnly) {
522 final String KEY = key(seed == null ? name : name+"\0"+seed, size);
523 Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
524 if (bitmap != null || cachedOnly) {
525 return bitmap;
526 }
527 bitmap = getImpl(name, seed, size);
528 mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
529 return bitmap;
530 }
531
532 public static Bitmap get(final Jid jid, final int size) {
533 return getImpl(jid.asBareJid().toEscapedString(), null, size);
534 }
535
536 private static Bitmap getImpl(final String name, final String seed, final int size) {
537 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
538 Canvas canvas = new Canvas(bitmap);
539 final String trimmedName = name == null ? "" : name.trim();
540 drawTile(canvas, trimmedName, seed, 0, 0, size, size);
541 return bitmap;
542 }
543
544 private String key(String name, int size) {
545 synchronized (this.sizes) {
546 if (!this.sizes.contains(size)) {
547 this.sizes.add(size);
548 }
549 }
550 return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
551 }
552
553 private static boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) {
554 letter = letter.toUpperCase(Locale.getDefault());
555 Paint tilePaint = new Paint(), textPaint = new Paint();
556 tilePaint.setColor(tileColor);
557 textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
558 textPaint.setColor(FG_COLOR);
559 textPaint.setTypeface(Typeface.create("sans-serif-light",
560 Typeface.NORMAL));
561 textPaint.setTextSize((float) ((right - left) * 0.8));
562 Rect rect = new Rect();
563
564 canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
565 textPaint.getTextBounds(letter, 0, 1, rect);
566 float width = textPaint.measureText(letter);
567 canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
568 / 2 + rect.height() / 2, textPaint);
569 return true;
570 }
571
572 private boolean drawTile(Canvas canvas, MucOptions.User user, int left, int top, int right, int bottom) {
573 Contact contact = user.getContact();
574 if (contact != null) {
575 Uri uri = null;
576 if (contact.getAvatarFilename() != null && QuickConversationsService.isQuicksy()) {
577 uri = mXmppConnectionService.getFileBackend().getAvatarUri(contact.getAvatarFilename());
578 } else if (contact.getProfilePhoto() != null) {
579 uri = Uri.parse(contact.getProfilePhoto());
580 } else if (contact.getAvatarFilename() != null) {
581 uri = mXmppConnectionService.getFileBackend().getAvatarUri(contact.getAvatarFilename());
582 }
583 if (drawTile(canvas, uri, left, top, right, bottom)) {
584 return true;
585 }
586 } else if (user.getAvatar() != null) {
587 Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(user.getAvatar());
588 if (drawTile(canvas, uri, left, top, right, bottom)) {
589 return true;
590 }
591 }
592 if (contact != null) {
593 String seed = contact.getJid().asBareJid().toString();
594 drawTile(canvas, contact.getDisplayName(), seed, left, top, right, bottom);
595 } else {
596 String seed = user.getRealJid() == null ? null : user.getRealJid().asBareJid().toString();
597 drawTile(canvas, user.getName(), seed, left, top, right, bottom);
598 }
599 return true;
600 }
601
602 private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
603 String avatar = account.getAvatar();
604 if (avatar != null) {
605 Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
606 if (uri != null) {
607 if (drawTile(canvas, uri, left, top, right, bottom)) {
608 return true;
609 }
610 }
611 }
612 String name = account.getJid().asBareJid().toString();
613 return drawTile(canvas, name, name, left, top, right, bottom);
614 }
615
616 private static boolean drawTile(Canvas canvas, String name, String seed, int left, int top, int right, int bottom) {
617 if (name != null) {
618 final String letter = getFirstLetter(name);
619 final int color = UIHelper.getColorForName(seed == null ? name : seed);
620 drawTile(canvas, letter, color, left, top, right, bottom);
621 return true;
622 }
623 return false;
624 }
625
626 private static String getFirstLetter(String name) {
627 for (Character c : name.toCharArray()) {
628 if (Character.isLetterOrDigit(c)) {
629 return c.toString();
630 }
631 }
632 return "X";
633 }
634
635 private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
636 if (uri != null) {
637 Bitmap bitmap = mXmppConnectionService.getFileBackend()
638 .cropCenter(uri, bottom - top, right - left);
639 if (bitmap != null) {
640 drawTile(canvas, bitmap, left, top, right, bottom);
641 return true;
642 }
643 }
644 return false;
645 }
646
647 private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, int dstright, int dstbottom) {
648 Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
649 canvas.drawBitmap(bm, null, dst, null);
650 return true;
651 }
652
653 @Override
654 public void onAdvancedStreamFeaturesAvailable(Account account) {
655 XmppConnection.Features features = account.getXmppConnection().getFeatures();
656 if (features.pep() && !features.pepPersistent()) {
657 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has pep but is not persistent");
658 if (account.getAvatar() != null) {
659 mXmppConnectionService.republishAvatarIfNeeded(account);
660 }
661 }
662 }
663
664 private static String emptyOnNull(@Nullable Jid value) {
665 return value == null ? "" : value.toString();
666 }
667
668 public interface Avatarable {
669 @ColorInt int getAvatarBackgroundColor();
670 }
671}