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