AvatarService.java

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