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