AvatarService.java

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