AvatarService.java

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