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