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.utils.UIHelper;
 43import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
 44import eu.siacs.conversations.xmpp.XmppConnection;
 45import eu.siacs.conversations.xmpp.Jid;
 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 ArrayList<Integer> sizes = new ArrayList<>();
 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 (Integer size : sizes) {
228				this.mXmppConnectionService.getBitmapCache().remove(
229						key(contact, size));
230			}
231		}
232		for (Conversation conversation : mXmppConnectionService.findAllConferencesWith(contact)) {
233			MucOptions.User user = conversation.getMucOptions().findUserByRealJid(contact.getJid().asBareJid());
234			if (user != null) {
235				clear(user);
236			}
237			clear(conversation);
238		}
239	}
240
241	private String key(Contact contact, int size) {
242		synchronized (this.sizes) {
243			if (!this.sizes.contains(size)) {
244				this.sizes.add(size);
245			}
246		}
247		return PREFIX_CONTACT +
248				'\0' +
249				contact.getAccount().getJid().asBareJid() +
250				'\0' +
251				emptyOnNull(contact.getJid()) +
252				'\0' +
253				size;
254	}
255
256	private String key(MucOptions.User user, int size) {
257		synchronized (this.sizes) {
258			if (!this.sizes.contains(size)) {
259				this.sizes.add(size);
260			}
261		}
262		return PREFIX_CONTACT +
263				'\0' +
264				user.getAccount().getJid().asBareJid() +
265				'\0' +
266				emptyOnNull(user.getFullJid()) +
267				'\0' +
268				emptyOnNull(user.getRealJid()) +
269				'\0' +
270				size;
271	}
272
273	public Bitmap get(ListItem item, int size) {
274		return get(item, size, false);
275	}
276
277	public Bitmap get(ListItem item, int size, boolean cachedOnly) {
278		if (item instanceof RawBlockable) {
279			return get(item.getDisplayName(), item.getJid().toEscapedString(), size, cachedOnly);
280		} else if (item instanceof Contact) {
281			return get((Contact) item, size, cachedOnly);
282		} else if (item instanceof Bookmark) {
283			Bookmark bookmark = (Bookmark) item;
284			if (bookmark.getConversation() != null) {
285				return get(bookmark.getConversation(), size, cachedOnly);
286			} else {
287				Jid jid = bookmark.getJid();
288				Account account = bookmark.getAccount();
289				Contact contact = jid == null ? null : account.getRoster().getContact(jid);
290				if (contact != null && contact.getAvatarFilename() != null) {
291					return get(contact, size, cachedOnly);
292				}
293				String seed = jid != null ? jid.asBareJid().toString() : null;
294				return get(bookmark.getDisplayName(), seed, size, cachedOnly);
295			}
296		} else {
297			String seed = item.getJid() != null ? item.getJid().asBareJid().toString() : null;
298			return get(item.getDisplayName(), seed, size, cachedOnly);
299		}
300	}
301
302	public Bitmap get(Conversation conversation, int size) {
303		return get(conversation, size, false);
304	}
305
306	public Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
307		if (conversation.getMode() == Conversation.MODE_SINGLE) {
308			return get(conversation.getContact(), size, cachedOnly);
309		} else {
310			return get(conversation.getMucOptions(), size, cachedOnly);
311		}
312	}
313
314	public void clear(Conversation conversation) {
315		if (conversation.getMode() == Conversation.MODE_SINGLE) {
316			clear(conversation.getContact());
317		} else {
318			clear(conversation.getMucOptions());
319			synchronized (this.conversationDependentKeys) {
320				Set<String> keys = this.conversationDependentKeys.get(conversation.getUuid());
321				if (keys == null) {
322					return;
323				}
324				LruCache<String, Bitmap> cache = this.mXmppConnectionService.getBitmapCache();
325				for (String key : keys) {
326					cache.remove(key);
327				}
328				keys.clear();
329			}
330		}
331	}
332
333	private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) {
334		final String KEY = key(mucOptions, size);
335		Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
336		if (bitmap != null || cachedOnly) {
337			return bitmap;
338		}
339
340		bitmap = mXmppConnectionService.getFileBackend().getAvatar(mucOptions.getAvatar(), size);
341
342		if (bitmap == null) {
343			Conversation c = mucOptions.getConversation();
344			if (mucOptions.isPrivateAndNonAnonymous()) {
345				final List<MucOptions.User> users = mucOptions.getUsersRelevantForNameAndAvatar();
346				if (users.size() == 0) {
347					bitmap = getImpl(c.getName().toString(), c.getJid().asBareJid().toString(), size);
348				} else {
349					bitmap = getImpl(users, size);
350				}
351			} else {
352				bitmap = getImpl(CHANNEL_SYMBOL, c.getJid().asBareJid().toString(), size);
353			}
354		}
355
356		this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
357
358		return bitmap;
359	}
360
361	private Bitmap get(List<MucOptions.User> users, int size, boolean cachedOnly) {
362		final String KEY = key(users, size);
363		Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
364		if (bitmap != null || cachedOnly) {
365			return bitmap;
366		}
367		bitmap = getImpl(users, size);
368		this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
369		return bitmap;
370	}
371
372	private Bitmap getImpl(List<MucOptions.User> users, int size) {
373		int count = users.size();
374		Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
375		Canvas canvas = new Canvas(bitmap);
376		bitmap.eraseColor(TRANSPARENT);
377		if (count == 0) {
378			throw new AssertionError("Unable to draw tiles for 0 users");
379		} else if (count == 1) {
380			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
381			drawTile(canvas, users.get(0).getAccount(), size / 2 + 1, 0, size, size);
382		} else if (count == 2) {
383			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
384			drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
385		} else if (count == 3) {
386			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
387			drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
388			drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
389					size);
390		} else if (count == 4) {
391			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
392			drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
393			drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
394			drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
395					size);
396		} else {
397			drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
398			drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
399			drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
400			drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
401					size, size);
402		}
403		return bitmap;
404	}
405
406	public void clear(final MucOptions options) {
407		if (options == null) {
408			return;
409		}
410		synchronized (this.sizes) {
411			for (Integer size : sizes) {
412				this.mXmppConnectionService.getBitmapCache().remove(key(options, size));
413			}
414		}
415	}
416
417	private String key(final MucOptions options, int size) {
418		synchronized (this.sizes) {
419			if (!this.sizes.contains(size)) {
420				this.sizes.add(size);
421			}
422		}
423		return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
424				+ "_" + String.valueOf(size);
425	}
426
427	private String key(List<MucOptions.User> users, int size) {
428		final Conversation conversation = users.get(0).getConversation();
429		StringBuilder builder = new StringBuilder("TILE_");
430		builder.append(conversation.getUuid());
431
432		for (MucOptions.User user : users) {
433			builder.append("\0");
434			builder.append(emptyOnNull(user.getRealJid()));
435			builder.append("\0");
436			builder.append(emptyOnNull(user.getFullJid()));
437		}
438		builder.append('\0');
439		builder.append(size);
440		final String key = builder.toString();
441		synchronized (this.conversationDependentKeys) {
442			Set<String> keys;
443			if (this.conversationDependentKeys.containsKey(conversation.getUuid())) {
444				keys = this.conversationDependentKeys.get(conversation.getUuid());
445			} else {
446				keys = new HashSet<>();
447				this.conversationDependentKeys.put(conversation.getUuid(), keys);
448			}
449			keys.add(key);
450		}
451		return key;
452	}
453
454	public Bitmap get(Account account, int size) {
455		return get(account, size, false);
456	}
457
458	public Bitmap get(Account account, int size, boolean cachedOnly) {
459		final String KEY = key(account, size);
460		Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
461		if (avatar != null || cachedOnly) {
462			return avatar;
463		}
464		avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
465		if (avatar == null) {
466			final String displayName = account.getDisplayName();
467			final String jid = account.getJid().asBareJid().toEscapedString();
468			if (QuickConversationsService.isQuicksy() && !TextUtils.isEmpty(displayName)) {
469				avatar = get(displayName, jid, size, false);
470			} else {
471				avatar = get(jid, null, size, false);
472			}
473		}
474		mXmppConnectionService.getBitmapCache().put(KEY, avatar);
475		return avatar;
476	}
477
478	public Bitmap get(Message message, int size, boolean cachedOnly) {
479		final Conversational conversation = message.getConversation();
480		if (message.getType() == Message.TYPE_STATUS && message.getCounterparts() != null && message.getCounterparts().size() > 1) {
481			return get(message.getCounterparts(), size, cachedOnly);
482		} else if (message.getStatus() == Message.STATUS_RECEIVED) {
483			Contact c = message.getContact();
484			if (c != null && (c.getProfilePhoto() != null || c.getAvatarFilename() != null)) {
485				return get(c, size, cachedOnly);
486			} else if (conversation instanceof Conversation && message.getConversation().getMode() == Conversation.MODE_MULTI) {
487				final Jid trueCounterpart = message.getTrueCounterpart();
488				final MucOptions mucOptions = ((Conversation) conversation).getMucOptions();
489				MucOptions.User user;
490				if (trueCounterpart != null) {
491					user = mucOptions.findOrCreateUserByRealJid(trueCounterpart, message.getCounterpart());
492				} else {
493					user = mucOptions.findUserByFullJid(message.getCounterpart());
494				}
495				if (user != null) {
496					return getImpl(user, size, cachedOnly);
497				}
498			} else if (c != null) {
499				return get(c, size, cachedOnly);
500			}
501			Jid tcp = message.getTrueCounterpart();
502			String seed = tcp != null ? tcp.asBareJid().toString() : null;
503			return get(UIHelper.getMessageDisplayName(message), seed, size, cachedOnly);
504		} else {
505			return get(conversation.getAccount(), size, cachedOnly);
506		}
507	}
508
509	public void clear(Account account) {
510		synchronized (this.sizes) {
511			for (Integer size : sizes) {
512				this.mXmppConnectionService.getBitmapCache().remove(key(account, size));
513			}
514		}
515	}
516
517	public void clear(MucOptions.User user) {
518		synchronized (this.sizes) {
519			for (Integer size : sizes) {
520				this.mXmppConnectionService.getBitmapCache().remove(key(user, size));
521			}
522		}
523	}
524
525	private String key(Account account, int size) {
526		synchronized (this.sizes) {
527			if (!this.sizes.contains(size)) {
528				this.sizes.add(size);
529			}
530		}
531		return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
532				+ String.valueOf(size);
533	}
534
535	/*public Bitmap get(String name, int size) {
536		return get(name,null, size,false);
537	}*/
538
539	public Bitmap get(final String name, String seed, final int size, boolean cachedOnly) {
540		final String KEY = key(seed == null ? name : name+"\0"+seed, size);
541		Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
542		if (bitmap != null || cachedOnly) {
543			return bitmap;
544		}
545		bitmap = getImpl(name, seed, size);
546		mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
547		return bitmap;
548	}
549
550	public static Bitmap get(final Jid jid, final int size) {
551		return getImpl(jid.asBareJid().toEscapedString(), null, size);
552	}
553
554	private static Bitmap getImpl(final String name, final String seed, final int size) {
555		Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
556		Canvas canvas = new Canvas(bitmap);
557		final String trimmedName = name == null ? "" : name.trim();
558		drawTile(canvas, trimmedName, seed, 0, 0, size, size);
559		return bitmap;
560	}
561
562	private String key(String name, int size) {
563		synchronized (this.sizes) {
564			if (!this.sizes.contains(size)) {
565				this.sizes.add(size);
566			}
567		}
568		return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
569	}
570
571	private static boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) {
572		letter = letter.toUpperCase(Locale.getDefault());
573		Paint tilePaint = new Paint(), textPaint = new Paint();
574		tilePaint.setColor(tileColor);
575		textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
576		textPaint.setColor(FG_COLOR);
577		textPaint.setTypeface(Typeface.create("sans-serif-light",
578				Typeface.NORMAL));
579		textPaint.setTextSize((float) ((right - left) * 0.8));
580		Rect rect = new Rect();
581
582		canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
583		textPaint.getTextBounds(letter, 0, 1, rect);
584		float width = textPaint.measureText(letter);
585		canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
586				/ 2 + rect.height() / 2, textPaint);
587		return true;
588	}
589
590	private boolean drawTile(Canvas canvas, MucOptions.User user, int left, int top, int right, int bottom) {
591		Contact contact = user.getContact();
592		if (contact != null) {
593			Uri uri = null;
594			if (contact.getAvatarFilename() != null && QuickConversationsService.isQuicksy()) {
595				uri = mXmppConnectionService.getFileBackend().getAvatarUri(contact.getAvatarFilename());
596			} else if (contact.getProfilePhoto() != null) {
597				uri = Uri.parse(contact.getProfilePhoto());
598			} else if (contact.getAvatarFilename() != null) {
599				uri = mXmppConnectionService.getFileBackend().getAvatarUri(contact.getAvatarFilename());
600			}
601			if (drawTile(canvas, uri, left, top, right, bottom)) {
602				return true;
603			}
604		} else if (user.getAvatar() != null) {
605			Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(user.getAvatar());
606			if (drawTile(canvas, uri, left, top, right, bottom)) {
607				return true;
608			}
609		}
610		if (contact != null) {
611			String seed = contact.getJid().asBareJid().toString();
612			drawTile(canvas, contact.getDisplayName(), seed, left, top, right, bottom);
613		} else {
614			String seed = user.getRealJid() == null ? null : user.getRealJid().asBareJid().toString();
615			drawTile(canvas, user.getName(), seed, left, top, right, bottom);
616		}
617		return true;
618	}
619
620	private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
621		String avatar = account.getAvatar();
622		if (avatar != null) {
623			Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
624			if (uri != null) {
625				if (drawTile(canvas, uri, left, top, right, bottom)) {
626					return true;
627				}
628			}
629		}
630		String name = account.getJid().asBareJid().toString();
631		return drawTile(canvas, name, name, left, top, right, bottom);
632	}
633
634	private static boolean drawTile(Canvas canvas, String name, String seed, int left, int top, int right, int bottom) {
635		if (name != null) {
636			final String letter = name.equals(CHANNEL_SYMBOL) ? name : getFirstLetter(name);
637			final int color = UIHelper.getColorForName(seed == null ? name : seed);
638			drawTile(canvas, letter, color, left, top, right, bottom);
639			return true;
640		}
641		return false;
642	}
643
644	private static String getFirstLetter(String name) {
645		for (Character c : name.toCharArray()) {
646			if (Character.isLetterOrDigit(c)) {
647				return c.toString();
648			}
649		}
650		return "X";
651	}
652
653	private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
654		if (uri != null) {
655			Bitmap bitmap = mXmppConnectionService.getFileBackend()
656					.cropCenter(uri, bottom - top, right - left);
657			if (bitmap != null) {
658				drawTile(canvas, bitmap, left, top, right, bottom);
659				return true;
660			}
661		}
662		return false;
663	}
664
665	private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, int dstright, int dstbottom) {
666		Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
667		canvas.drawBitmap(bm, null, dst, null);
668		return true;
669	}
670
671	@Override
672	public void onAdvancedStreamFeaturesAvailable(Account account) {
673		XmppConnection.Features features = account.getXmppConnection().getFeatures();
674		if (features.pep() && !features.pepPersistent()) {
675			Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has pep but is not persistent");
676			if (account.getAvatar() != null) {
677				mXmppConnectionService.republishAvatarIfNeeded(account);
678			}
679		}
680	}
681
682	private static String emptyOnNull(@Nullable Jid value) {
683		return value == null ? "" : value.toString();
684	}
685
686	public interface Avatarable {
687		@ColorInt int getAvatarBackgroundColor();
688		String getAvatarName();
689	}
690}