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