AvatarService.java

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