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