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.Log;
 20import android.util.LruCache;
 21import androidx.annotation.ColorInt;
 22import androidx.annotation.Nullable;
 23import androidx.core.content.res.ResourcesCompat;
 24import com.google.common.base.Strings;
 25import eu.siacs.conversations.Config;
 26import eu.siacs.conversations.R;
 27import eu.siacs.conversations.entities.Account;
 28import eu.siacs.conversations.entities.Bookmark;
 29import eu.siacs.conversations.entities.Contact;
 30import eu.siacs.conversations.entities.Conversation;
 31import eu.siacs.conversations.entities.Conversational;
 32import eu.siacs.conversations.entities.ListItem;
 33import eu.siacs.conversations.entities.Message;
 34import eu.siacs.conversations.entities.MucOptions;
 35import eu.siacs.conversations.entities.RawBlockable;
 36import eu.siacs.conversations.entities.Room;
 37import eu.siacs.conversations.persistance.FileBackend;
 38import eu.siacs.conversations.utils.UIHelper;
 39import eu.siacs.conversations.xmpp.Jid;
 40import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
 41import eu.siacs.conversations.xmpp.XmppConnection;
 42import java.util.HashMap;
 43import java.util.HashSet;
 44import java.util.List;
 45import java.util.Locale;
 46import java.util.Set;
 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().toString() : 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().toString(), 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().toString();
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		return getImpl(name, seed, size);
549	}
550
551	public static Drawable get(final Jid jid, final int size) {
552		return getImpl(jid.asBareJid().toString(), null, size);
553	}
554
555	private static Drawable getImpl(final String name, final String seed, final int size) {
556		Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
557		Canvas canvas = new Canvas(bitmap);
558		final String trimmedName = name == null ? "" : name.trim();
559		return new TextDrawable(name, seed, size);
560	}
561
562	private String key(String name, int size) {
563		synchronized (this.sizes) {
564			this.sizes.add(size);
565		}
566		return PREFIX_GENERIC + "_" + name + "_" + size;
567	}
568
569	private static boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) {
570		letter = letter.toUpperCase(Locale.getDefault());
571		Paint tilePaint = new Paint(), textPaint = new Paint();
572		tilePaint.setColor(tileColor);
573		textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
574		textPaint.setColor(FG_COLOR);
575		textPaint.setTypeface(Typeface.create("sans-serif-light",
576				Typeface.NORMAL));
577		textPaint.setTextSize((float) ((right - left) * 0.8));
578		Rect rect = new Rect();
579
580		canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
581		textPaint.getTextBounds(letter, 0, 1, rect);
582		float width = textPaint.measureText(letter);
583		canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
584				/ 2 + rect.height() / 2, textPaint);
585		return true;
586	}
587
588	private boolean drawTile(Canvas canvas, MucOptions.User user, int left, int top, int right, int bottom) {
589		Contact contact = user.getContact();
590		if (contact != null) {
591			Uri uri = null;
592			if (contact.getAvatarFilename() != null && QuickConversationsService.isQuicksy()) {
593				uri = mXmppConnectionService.getFileBackend().getAvatarUri(contact.getAvatarFilename());
594			} else if (contact.getProfilePhoto() != null) {
595				uri = Uri.parse(contact.getProfilePhoto());
596			} else if (contact.getAvatarFilename() != null) {
597				uri = mXmppConnectionService.getFileBackend().getAvatarUri(contact.getAvatarFilename());
598			}
599			if (drawTile(canvas, uri, left, top, right, bottom)) {
600				return true;
601			}
602		} else if (user.getAvatar() != null) {
603			Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(user.getAvatar());
604			if (drawTile(canvas, uri, left, top, right, bottom)) {
605				return true;
606			}
607		}
608		if (contact != null) {
609			String seed = contact.getJid().asBareJid().toString();
610			drawTile(canvas, contact.getDisplayName(), seed, left, top, right, bottom);
611		} else {
612			String seed = user.getRealJid() == null ? null : user.getRealJid().asBareJid().toString();
613			drawTile(canvas, user.getName(), seed, left, top, right, bottom);
614		}
615		return true;
616	}
617
618	private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
619		String avatar = account.getAvatar();
620		if (avatar != null) {
621			Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
622			if (uri != null) {
623				if (drawTile(canvas, uri, left, top, right, bottom)) {
624					return true;
625				}
626			}
627		}
628		String name = account.getJid().asBareJid().toString();
629		return drawTile(canvas, name, name, left, top, right, bottom);
630	}
631
632	private static boolean drawTile(Canvas canvas, String name, String seed, int left, int top, int right, int bottom) {
633		if (name != null) {
634			final String letter = name.equals(CHANNEL_SYMBOL) ? name : getFirstLetter(name);
635			final int color = UIHelper.getColorForName(seed == null ? name : seed);
636			drawTile(canvas, letter, color, left, top, right, bottom);
637			return true;
638		}
639		return false;
640	}
641
642	private static String getFirstLetter(String name) {
643		for (Character c : name.toCharArray()) {
644			if (Character.isLetterOrDigit(c)) {
645				return c.toString();
646			}
647		}
648		return "X";
649	}
650
651	private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
652		if (uri != null) {
653			Bitmap bitmap = mXmppConnectionService.getFileBackend()
654					.cropCenter(uri, bottom - top, right - left);
655			if (bitmap != null) {
656				drawTile(canvas, bitmap, left, top, right, bottom);
657				return true;
658			}
659		}
660		return false;
661	}
662
663	private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, int dstright, int dstbottom) {
664		Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
665		canvas.drawBitmap(bm, null, dst, null);
666		return true;
667	}
668
669	@Override
670	public void onAdvancedStreamFeaturesAvailable(Account account) {
671		XmppConnection.Features features = account.getXmppConnection().getFeatures();
672		if (features.pep() && !features.pepPersistent()) {
673			Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has pep but is not persistent");
674			if (account.getAvatar() != null) {
675				mXmppConnectionService.republishAvatarIfNeeded(account);
676			}
677		}
678	}
679
680	private static String emptyOnNull(@Nullable Jid value) {
681		return value == null ? "" : value.toString();
682	}
683
684	public interface Avatarable {
685		@ColorInt int getAvatarBackgroundColor();
686		String getAvatarName();
687	}
688
689	public static class TextDrawable extends Drawable {
690		protected final String name;
691		protected final String seed;
692		protected final int size;
693
694		public TextDrawable(final String name, final String seed, final int size) {
695			this.name = name;
696			this.seed = seed;
697			this.size = size;
698		}
699
700		@Override
701		public void draw(Canvas canvas) {
702			final var r = getBounds();
703			drawTile(canvas, name, seed, r.left, r.top, r.right, r.bottom);
704		}
705
706		@Override
707		public void setAlpha(int alpha) {
708			// TODO?
709		}
710
711		@Override
712		public void setColorFilter(ColorFilter cf) {
713			// TODO?
714		}
715
716		@Override
717		public int getOpacity() {
718			return PixelFormat.TRANSLUCENT;
719		}
720
721		@Override
722		public int getIntrinsicWidth() {
723			return size;
724		}
725
726		@Override
727		public int getIntrinsicHeight() {
728			return size;
729		}
730	}
731}