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