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