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