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