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