2014-10-20 19:08:33 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
2014-11-04 23:06:07 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.net.Uri;
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
import java.util.ArrayList;
|
2014-10-20 19:08:33 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Bookmark;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.ListItem;
|
2015-10-29 11:08:15 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2014-10-20 19:08:33 +00:00
|
|
|
import eu.siacs.conversations.entities.MucOptions;
|
2014-11-16 16:21:21 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2014-10-20 19:08:33 +00:00
|
|
|
|
|
|
|
public class AvatarService {
|
|
|
|
|
|
|
|
private static final int FG_COLOR = 0xFFFAFAFA;
|
|
|
|
private static final int TRANSPARENT = 0x00000000;
|
2014-11-09 16:02:39 +00:00
|
|
|
private static final int PLACEHOLDER_COLOR = 0xFF202020;
|
2014-10-20 19:08:33 +00:00
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
private static final String PREFIX_CONTACT = "contact";
|
|
|
|
private static final String PREFIX_CONVERSATION = "conversation";
|
|
|
|
private static final String PREFIX_ACCOUNT = "account";
|
|
|
|
private static final String PREFIX_GENERIC = "generic";
|
|
|
|
|
2014-11-08 03:21:18 +00:00
|
|
|
final private ArrayList<Integer> sizes = new ArrayList<>();
|
2014-10-21 12:57:16 +00:00
|
|
|
|
2014-10-20 19:08:33 +00:00
|
|
|
protected XmppConnectionService mXmppConnectionService = null;
|
|
|
|
|
|
|
|
public AvatarService(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
2015-02-15 17:48:05 +00:00
|
|
|
private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
|
2014-10-21 12:57:16 +00:00
|
|
|
final String KEY = key(contact, size);
|
|
|
|
Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
|
2015-02-15 17:48:05 +00:00
|
|
|
if (avatar != null || cachedOnly) {
|
2014-10-21 12:57:16 +00:00
|
|
|
return avatar;
|
|
|
|
}
|
2014-11-04 23:06:07 +00:00
|
|
|
if (contact.getProfilePhoto() != null) {
|
|
|
|
avatar = mXmppConnectionService.getFileBackend().cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
|
|
|
|
}
|
|
|
|
if (avatar == null && contact.getAvatar() != null) {
|
|
|
|
avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatar(), size);
|
|
|
|
}
|
2014-10-20 19:08:33 +00:00
|
|
|
if (avatar == null) {
|
2015-02-15 17:48:05 +00:00
|
|
|
avatar = get(contact.getDisplayName(), size, cachedOnly);
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
2014-10-21 12:57:16 +00:00
|
|
|
this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
2014-10-20 19:08:33 +00:00
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:18:34 +00:00
|
|
|
public Bitmap get(final MucOptions.User user, final int size, boolean cachedOnly) {
|
2015-12-04 17:39:09 +00:00
|
|
|
Contact c = user.getContact();
|
|
|
|
if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
|
|
|
|
return get(c, size, cachedOnly);
|
|
|
|
} else {
|
|
|
|
return getImpl(user, size, cachedOnly);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap getImpl(final MucOptions.User user, final int size, boolean cachedOnly) {
|
2015-12-03 17:18:34 +00:00
|
|
|
final String KEY = key(user, size);
|
|
|
|
Bitmap avatar = this.mXmppConnectionService.getBitmapCache().get(KEY);
|
|
|
|
if (avatar != null || cachedOnly) {
|
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
if (user.getAvatar() != null) {
|
|
|
|
avatar = mXmppConnectionService.getFileBackend().getAvatar(user.getAvatar(), size);
|
|
|
|
}
|
|
|
|
if (avatar == null) {
|
2015-12-09 09:30:26 +00:00
|
|
|
Contact contact = user.getContact();
|
|
|
|
if (contact != null) {
|
|
|
|
avatar = get(contact, size, cachedOnly);
|
|
|
|
} else {
|
|
|
|
avatar = get(user.getName(), size, cachedOnly);
|
|
|
|
}
|
2015-12-03 17:18:34 +00:00
|
|
|
}
|
|
|
|
this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
public void clear(Contact contact) {
|
2014-11-17 19:01:56 +00:00
|
|
|
synchronized (this.sizes) {
|
|
|
|
for (Integer size : sizes) {
|
|
|
|
this.mXmppConnectionService.getBitmapCache().remove(
|
|
|
|
key(contact, size));
|
|
|
|
}
|
2014-10-21 12:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String key(Contact contact, int size) {
|
|
|
|
synchronized (this.sizes) {
|
|
|
|
if (!this.sizes.contains(size)) {
|
|
|
|
this.sizes.add(size);
|
|
|
|
}
|
|
|
|
}
|
2014-11-09 15:57:22 +00:00
|
|
|
return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_"
|
2014-10-21 12:57:16 +00:00
|
|
|
+ contact.getJid() + "_" + String.valueOf(size);
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:18:34 +00:00
|
|
|
private String key(MucOptions.User user, int size) {
|
|
|
|
synchronized (this.sizes) {
|
|
|
|
if (!this.sizes.contains(size)) {
|
|
|
|
this.sizes.add(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PREFIX_CONTACT + "_" + user.getAccount().getJid().toBareJid() + "_"
|
|
|
|
+ user.getFullJid() + "_" + String.valueOf(size);
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
public Bitmap get(ListItem item, int size) {
|
2015-02-15 17:48:05 +00:00
|
|
|
return get(item,size,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap get(ListItem item, int size, boolean cachedOnly) {
|
2014-10-20 19:08:33 +00:00
|
|
|
if (item instanceof Contact) {
|
2015-02-15 17:48:05 +00:00
|
|
|
return get((Contact) item, size,cachedOnly);
|
2014-10-20 19:08:33 +00:00
|
|
|
} else if (item instanceof Bookmark) {
|
|
|
|
Bookmark bookmark = (Bookmark) item;
|
|
|
|
if (bookmark.getConversation() != null) {
|
2015-02-15 17:48:05 +00:00
|
|
|
return get(bookmark.getConversation(), size, cachedOnly);
|
2014-10-20 19:08:33 +00:00
|
|
|
} else {
|
2015-02-15 17:48:05 +00:00
|
|
|
return get(bookmark.getDisplayName(), size, cachedOnly);
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-02-15 17:48:05 +00:00
|
|
|
return get(item.getDisplayName(), size, cachedOnly);
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
public Bitmap get(Conversation conversation, int size) {
|
2015-02-15 17:48:05 +00:00
|
|
|
return get(conversation,size,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap get(Conversation conversation, int size, boolean cachedOnly) {
|
2014-10-20 19:08:33 +00:00
|
|
|
if (conversation.getMode() == Conversation.MODE_SINGLE) {
|
2015-02-15 17:48:05 +00:00
|
|
|
return get(conversation.getContact(), size, cachedOnly);
|
2014-10-20 19:08:33 +00:00
|
|
|
} else {
|
2015-02-15 17:48:05 +00:00
|
|
|
return get(conversation.getMucOptions(), size, cachedOnly);
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
public void clear(Conversation conversation) {
|
|
|
|
if (conversation.getMode() == Conversation.MODE_SINGLE) {
|
|
|
|
clear(conversation.getContact());
|
|
|
|
} else {
|
|
|
|
clear(conversation.getMucOptions());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 17:48:05 +00:00
|
|
|
private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) {
|
2014-10-21 12:57:16 +00:00
|
|
|
final String KEY = key(mucOptions, size);
|
|
|
|
Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
|
2015-02-15 17:48:05 +00:00
|
|
|
if (bitmap != null || cachedOnly) {
|
2014-10-21 12:57:16 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
2015-12-03 17:18:34 +00:00
|
|
|
final List<MucOptions.User> users = mucOptions.getUsers();
|
2014-10-20 19:08:33 +00:00
|
|
|
int count = users.size();
|
2014-10-21 12:57:16 +00:00
|
|
|
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
2014-10-20 19:08:33 +00:00
|
|
|
Canvas canvas = new Canvas(bitmap);
|
|
|
|
bitmap.eraseColor(TRANSPARENT);
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
String name = mucOptions.getConversation().getName();
|
2015-11-10 18:20:35 +00:00
|
|
|
drawTile(canvas, name, 0, 0, size, size);
|
2014-10-20 19:08:33 +00:00
|
|
|
} else if (count == 1) {
|
2015-11-10 18:20:35 +00:00
|
|
|
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
|
|
|
|
drawTile(canvas, mucOptions.getConversation().getAccount(), size / 2 + 1, 0, size, size);
|
2014-10-20 19:08:33 +00:00
|
|
|
} else if (count == 2) {
|
|
|
|
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
|
|
|
|
drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size);
|
|
|
|
} else if (count == 3) {
|
|
|
|
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size);
|
|
|
|
drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1);
|
|
|
|
drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size,
|
|
|
|
size);
|
|
|
|
} else if (count == 4) {
|
|
|
|
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
|
|
|
|
drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
|
|
|
|
drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
|
|
|
|
drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size,
|
|
|
|
size);
|
|
|
|
} else {
|
|
|
|
drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1);
|
|
|
|
drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size);
|
|
|
|
drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1);
|
2014-11-09 16:02:39 +00:00
|
|
|
drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1,
|
2014-10-20 19:08:33 +00:00
|
|
|
size, size);
|
|
|
|
}
|
2014-10-21 12:57:16 +00:00
|
|
|
this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
|
2014-10-20 19:08:33 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
public void clear(MucOptions options) {
|
2014-11-17 19:01:56 +00:00
|
|
|
synchronized (this.sizes) {
|
|
|
|
for (Integer size : sizes) {
|
2015-12-04 20:36:48 +00:00
|
|
|
this.mXmppConnectionService.getBitmapCache().remove(key(options, size));
|
2014-11-17 19:01:56 +00:00
|
|
|
}
|
2014-10-21 12:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String key(MucOptions options, int size) {
|
|
|
|
synchronized (this.sizes) {
|
|
|
|
if (!this.sizes.contains(size)) {
|
|
|
|
this.sizes.add(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
|
|
|
|
+ "_" + String.valueOf(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap get(Account account, int size) {
|
2015-10-29 11:08:15 +00:00
|
|
|
return get(account, size, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap get(Account account, int size, boolean cachedOnly) {
|
2014-10-21 12:57:16 +00:00
|
|
|
final String KEY = key(account, size);
|
|
|
|
Bitmap avatar = mXmppConnectionService.getBitmapCache().get(KEY);
|
2015-10-29 11:08:15 +00:00
|
|
|
if (avatar != null || cachedOnly) {
|
2014-10-21 12:57:16 +00:00
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
avatar = mXmppConnectionService.getFileBackend().getAvatar(
|
2014-10-20 19:08:33 +00:00
|
|
|
account.getAvatar(), size);
|
|
|
|
if (avatar == null) {
|
2015-02-15 17:48:05 +00:00
|
|
|
avatar = get(account.getJid().toBareJid().toString(), size,false);
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
2014-10-21 12:57:16 +00:00
|
|
|
mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
2014-10-20 19:08:33 +00:00
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
|
2015-10-29 11:08:15 +00:00
|
|
|
public Bitmap get(Message message, int size, boolean cachedOnly) {
|
2015-12-03 17:18:34 +00:00
|
|
|
final Conversation conversation = message.getConversation();
|
2015-10-29 11:08:15 +00:00
|
|
|
if (message.getStatus() == Message.STATUS_RECEIVED) {
|
2015-12-03 17:18:34 +00:00
|
|
|
Contact c = message.getContact();
|
|
|
|
if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) {
|
|
|
|
return get(c, size, cachedOnly);
|
|
|
|
} else if (message.getConversation().getMode() == Conversation.MODE_MULTI){
|
|
|
|
MucOptions.User user = conversation.getMucOptions().findUser(message.getCounterpart().getResourcepart());
|
|
|
|
if (user != null) {
|
2015-12-04 17:39:09 +00:00
|
|
|
return getImpl(user,size,cachedOnly);
|
2015-12-03 17:18:34 +00:00
|
|
|
}
|
2015-10-29 11:08:15 +00:00
|
|
|
}
|
2015-12-03 17:18:34 +00:00
|
|
|
return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
|
2015-10-29 11:08:15 +00:00
|
|
|
} else {
|
2015-12-03 17:18:34 +00:00
|
|
|
return get(conversation.getAccount(), size, cachedOnly);
|
2015-10-29 11:08:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
public void clear(Account account) {
|
2014-11-17 19:01:56 +00:00
|
|
|
synchronized (this.sizes) {
|
|
|
|
for (Integer size : sizes) {
|
2015-12-04 20:36:48 +00:00
|
|
|
this.mXmppConnectionService.getBitmapCache().remove(key(account, size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clear(MucOptions.User user) {
|
|
|
|
synchronized (this.sizes) {
|
|
|
|
for (Integer size : sizes) {
|
|
|
|
this.mXmppConnectionService.getBitmapCache().remove(key(user, size));
|
2014-11-17 19:01:56 +00:00
|
|
|
}
|
2014-10-21 12:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String key(Account account, int size) {
|
|
|
|
synchronized (this.sizes) {
|
|
|
|
if (!this.sizes.contains(size)) {
|
|
|
|
this.sizes.add(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PREFIX_ACCOUNT + "_" + account.getUuid() + "_"
|
|
|
|
+ String.valueOf(size);
|
|
|
|
}
|
|
|
|
|
2015-02-15 17:48:05 +00:00
|
|
|
public Bitmap get(String name, int size) {
|
|
|
|
return get(name,size,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap get(final String name, final int size, boolean cachedOnly) {
|
2014-10-21 12:57:16 +00:00
|
|
|
final String KEY = key(name, size);
|
|
|
|
Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
|
2015-02-15 17:48:05 +00:00
|
|
|
if (bitmap != null || cachedOnly) {
|
2014-10-21 12:57:16 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
2014-10-20 19:08:33 +00:00
|
|
|
Canvas canvas = new Canvas(bitmap);
|
2015-01-14 21:48:14 +00:00
|
|
|
final String trimmedName = name.trim();
|
2015-11-10 18:20:35 +00:00
|
|
|
drawTile(canvas, trimmedName, 0, 0, size, size);
|
2014-10-21 12:57:16 +00:00
|
|
|
mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
|
2014-10-20 19:08:33 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
private String key(String name, int size) {
|
|
|
|
synchronized (this.sizes) {
|
|
|
|
if (!this.sizes.contains(size)) {
|
|
|
|
this.sizes.add(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
|
|
|
|
}
|
|
|
|
|
2015-11-10 18:20:35 +00:00
|
|
|
private boolean drawTile(Canvas canvas, String letter, int tileColor,
|
2014-11-04 23:06:07 +00:00
|
|
|
int left, int top, int right, int bottom) {
|
2014-10-20 19:08:33 +00:00
|
|
|
letter = letter.toUpperCase(Locale.getDefault());
|
|
|
|
Paint tilePaint = new Paint(), textPaint = new Paint();
|
|
|
|
tilePaint.setColor(tileColor);
|
|
|
|
textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
textPaint.setColor(FG_COLOR);
|
|
|
|
textPaint.setTypeface(Typeface.create("sans-serif-light",
|
|
|
|
Typeface.NORMAL));
|
|
|
|
textPaint.setTextSize((float) ((right - left) * 0.8));
|
|
|
|
Rect rect = new Rect();
|
|
|
|
|
|
|
|
canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
|
|
|
|
textPaint.getTextBounds(letter, 0, 1, rect);
|
|
|
|
float width = textPaint.measureText(letter);
|
|
|
|
canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
|
|
|
|
/ 2 + rect.height() / 2, textPaint);
|
2015-11-10 18:20:35 +00:00
|
|
|
return true;
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 18:20:35 +00:00
|
|
|
private boolean drawTile(Canvas canvas, MucOptions.User user, int left,
|
2014-11-04 23:06:07 +00:00
|
|
|
int top, int right, int bottom) {
|
2014-10-20 19:08:33 +00:00
|
|
|
Contact contact = user.getContact();
|
|
|
|
if (contact != null) {
|
|
|
|
Uri uri = null;
|
2014-11-04 23:06:07 +00:00
|
|
|
if (contact.getProfilePhoto() != null) {
|
|
|
|
uri = Uri.parse(contact.getProfilePhoto());
|
|
|
|
} else if (contact.getAvatar() != null) {
|
2014-10-20 19:08:33 +00:00
|
|
|
uri = mXmppConnectionService.getFileBackend().getAvatarUri(
|
|
|
|
contact.getAvatar());
|
|
|
|
}
|
2015-11-10 18:20:35 +00:00
|
|
|
if (drawTile(canvas, uri, left, top, right, bottom)) {
|
|
|
|
return true;
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
2015-12-03 17:18:34 +00:00
|
|
|
} else if (user.getAvatar() != null) {
|
|
|
|
Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(user.getAvatar());
|
|
|
|
if (drawTile(canvas, uri, left, top, right, bottom)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-09 16:02:39 +00:00
|
|
|
}
|
2014-11-20 17:20:42 +00:00
|
|
|
String name = contact != null ? contact.getDisplayName() : user.getName();
|
2015-11-10 18:20:35 +00:00
|
|
|
drawTile(canvas, name, left, top, right, bottom);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) {
|
|
|
|
String avatar = account.getAvatar();
|
|
|
|
if (avatar != null) {
|
|
|
|
Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
|
|
|
|
if (uri != null) {
|
2015-12-04 20:36:48 +00:00
|
|
|
if (drawTile(canvas, uri, left, top, right, bottom)) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-10 18:20:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 20:36:48 +00:00
|
|
|
return drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom);
|
2015-11-10 18:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {
|
|
|
|
if (name != null) {
|
|
|
|
final String letter = name.isEmpty() ? "X" : name.substring(0, 1);
|
|
|
|
final int color = UIHelper.getColorForName(name);
|
|
|
|
drawTile(canvas, letter, color, left, top, right, bottom);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) {
|
|
|
|
if (uri != null) {
|
|
|
|
Bitmap bitmap = mXmppConnectionService.getFileBackend()
|
|
|
|
.cropCenter(uri, bottom - top, right - left);
|
|
|
|
if (bitmap != null) {
|
|
|
|
drawTile(canvas, bitmap, left, top, right, bottom);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 18:20:35 +00:00
|
|
|
private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop,
|
2014-11-04 23:06:07 +00:00
|
|
|
int dstright, int dstbottom) {
|
2014-10-20 19:08:33 +00:00
|
|
|
Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom);
|
|
|
|
canvas.drawBitmap(bm, null, dst, null);
|
2015-11-10 18:20:35 +00:00
|
|
|
return true;
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
|
|
|
}
|