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