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