2014-10-20 19:08:33 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
2018-04-04 07:33:07 +00:00
|
|
|
import android.content.res.Resources;
|
2014-11-04 23:06:07 +00:00
|
|
|
import android.graphics.Bitmap;
|
2018-04-04 07:33:07 +00:00
|
|
|
import android.graphics.BitmapFactory;
|
2014-11-04 23:06:07 +00:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
2017-05-31 14:45:51 +00:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.PorterDuffXfermode;
|
2014-11-04 23:06:07 +00:00
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.net.Uri;
|
2018-03-22 14:10:29 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2017-05-31 14:45:51 +00:00
|
|
|
import android.util.DisplayMetrics;
|
2016-04-11 20:20:32 +00:00
|
|
|
import android.util.Log;
|
2017-11-19 00:53:04 +00:00
|
|
|
import android.util.LruCache;
|
2014-11-04 23:06:07 +00:00
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
import java.util.ArrayList;
|
2017-11-19 00:53:04 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
2014-10-20 19:08:33 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2017-11-19 00:53:04 +00:00
|
|
|
import java.util.Set;
|
2014-10-20 19:08:33 +00:00
|
|
|
|
2016-04-11 20:20:32 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2018-04-04 07:33:07 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2014-10-20 19:08:33 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Bookmark;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2018-04-26 11:22:31 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversational;
|
2014-10-20 19:08:33 +00:00
|
|
|
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;
|
2016-04-11 20:20:32 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
|
|
|
|
import eu.siacs.conversations.xmpp.XmppConnection;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2014-10-20 19:08:33 +00:00
|
|
|
|
2016-04-11 20:20:32 +00:00
|
|
|
public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
2014-10-20 19:08:33 +00:00
|
|
|
|
|
|
|
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<>();
|
2018-04-04 07:33:31 +00:00
|
|
|
final private HashMap<String, Set<String>> conversationDependentKeys = new HashMap<>();
|
2014-10-21 12:57:16 +00:00
|
|
|
|
2014-10-20 19:08:33 +00:00
|
|
|
protected XmppConnectionService mXmppConnectionService = null;
|
|
|
|
|
2018-06-30 11:35:17 +00:00
|
|
|
AvatarService(XmppConnectionService service) {
|
2014-10-20 19:08:33 +00:00
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
2015-02-15 17:48:05 +00:00
|
|
|
private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
|
2016-11-22 11:34:16 +00:00
|
|
|
if (contact.isSelf()) {
|
2018-04-04 07:33:31 +00:00
|
|
|
return get(contact.getAccount(), size, cachedOnly);
|
2016-11-22 11:34:16 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2018-11-07 12:42:01 +00:00
|
|
|
if (contact.getAvatarFilename() != null) {
|
2018-09-21 10:27:58 +00:00
|
|
|
avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatarFilename(), size);
|
2014-11-04 23:06:07 +00:00
|
|
|
}
|
2018-11-07 12:42:01 +00:00
|
|
|
if (avatar == null && contact.getProfilePhoto() != null) {
|
|
|
|
avatar = mXmppConnectionService.getFileBackend().cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size);
|
|
|
|
}
|
2014-10-20 19:08:33 +00:00
|
|
|
if (avatar == null) {
|
2018-11-07 12:42:01 +00:00
|
|
|
avatar = get(contact.getDisplayName(), contact.getJid().asBareJid().toString(), size, false);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-31 14:45:51 +00:00
|
|
|
public Bitmap getRoundedShortcut(final Contact contact) {
|
2018-04-04 07:33:31 +00:00
|
|
|
return getRoundedShortcut(contact, false);
|
2018-04-04 07:33:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 07:33:31 +00:00
|
|
|
public Bitmap getRoundedShortcutWithIcon(final Contact contact) {
|
|
|
|
return getRoundedShortcut(contact, true);
|
2018-04-04 07:33:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 07:33:31 +00:00
|
|
|
private Bitmap getRoundedShortcut(final Contact contact, boolean withIcon) {
|
2017-05-31 14:45:51 +00:00
|
|
|
DisplayMetrics metrics = mXmppConnectionService.getResources().getDisplayMetrics();
|
|
|
|
int size = Math.round(metrics.density * 48);
|
2018-04-04 07:33:31 +00:00
|
|
|
Bitmap bitmap = get(contact, size);
|
2017-05-31 14:45:51 +00:00
|
|
|
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas canvas = new Canvas(output);
|
|
|
|
final Paint paint = new Paint();
|
|
|
|
|
2018-04-04 07:33:07 +00:00
|
|
|
drawAvatar(bitmap, canvas, paint);
|
2018-04-04 07:33:31 +00:00
|
|
|
if (withIcon) {
|
2018-04-04 07:33:07 +00:00
|
|
|
drawIcon(canvas, paint);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawAvatar(Bitmap bitmap, Canvas canvas, Paint paint) {
|
|
|
|
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
|
2017-05-31 14:45:51 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas.drawARGB(0, 0, 0, 0);
|
|
|
|
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
|
|
|
|
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
|
|
|
canvas.drawBitmap(bitmap, rect, rect, paint);
|
2018-04-04 07:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void drawIcon(Canvas canvas, Paint paint) {
|
|
|
|
BitmapFactory.Options opts = new BitmapFactory.Options();
|
2018-04-04 07:33:31 +00:00
|
|
|
opts.inSampleSize = 2;
|
2018-04-04 07:33:07 +00:00
|
|
|
Resources resources = mXmppConnectionService.getResources();
|
2018-09-05 20:24:58 +00:00
|
|
|
Bitmap icon = BitmapFactory.decodeResource(resources, R.mipmap.new_launcher_round, opts);
|
2018-04-04 07:33:07 +00:00
|
|
|
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
|
|
|
|
|
2018-04-04 07:33:31 +00:00
|
|
|
int iconSize = Math.round(canvas.getHeight() / 2.6f);
|
|
|
|
|
|
|
|
int left = canvas.getWidth() - iconSize;
|
|
|
|
int top = canvas.getHeight() - iconSize;
|
|
|
|
final Rect rect = new Rect(left, top, left + iconSize, top + iconSize);
|
2018-04-04 07:33:07 +00:00
|
|
|
canvas.drawBitmap(icon, null, rect, paint);
|
2017-05-31 14:45:51 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2018-09-21 10:27:58 +00:00
|
|
|
if (c != null && (c.getProfilePhoto() != null || c.getAvatarFilename() != null || user.getAvatar() == null)) {
|
2015-12-04 17:39:09 +00:00
|
|
|
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) {
|
2018-11-07 12:42:01 +00:00
|
|
|
avatar = get(contact, size, false);
|
2015-12-09 09:30:26 +00:00
|
|
|
} else {
|
2018-03-05 17:30:40 +00:00
|
|
|
String seed = user.getRealJid() != null ? user.getRealJid().asBareJid().toString() : null;
|
2018-11-07 12:42:01 +00:00
|
|
|
avatar = get(user.getName(), seed, size, false);
|
2015-12-09 09:30:26 +00:00
|
|
|
}
|
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
|
|
|
}
|
2018-04-04 07:33:31 +00:00
|
|
|
for (Conversation conversation : mXmppConnectionService.findAllConferencesWith(contact)) {
|
2018-09-21 10:27:58 +00:00
|
|
|
MucOptions.User user = conversation.getMucOptions().findUserByRealJid(contact.getJid().asBareJid());
|
|
|
|
if (user != null) {
|
|
|
|
clear(user);
|
|
|
|
}
|
2016-06-14 15:11:31 +00:00
|
|
|
clear(conversation);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 14:10:29 +00:00
|
|
|
return PREFIX_CONTACT +
|
|
|
|
'\0' +
|
|
|
|
contact.getAccount().getJid().asBareJid() +
|
|
|
|
'\0' +
|
|
|
|
emptyOnNull(contact.getJid()) +
|
|
|
|
'\0' +
|
|
|
|
size;
|
2014-10-21 12:57:16 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 14:10:29 +00:00
|
|
|
return PREFIX_CONTACT +
|
|
|
|
'\0' +
|
|
|
|
user.getAccount().getJid().asBareJid() +
|
|
|
|
'\0' +
|
|
|
|
emptyOnNull(user.getFullJid()) +
|
|
|
|
'\0' +
|
|
|
|
emptyOnNull(user.getRealJid()) +
|
|
|
|
'\0' +
|
|
|
|
size;
|
2015-12-03 17:18:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
public Bitmap get(ListItem item, int size) {
|
2018-04-04 07:33:31 +00:00
|
|
|
return get(item, size, false);
|
2015-02-15 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap get(ListItem item, int size, boolean cachedOnly) {
|
2014-10-20 19:08:33 +00:00
|
|
|
if (item instanceof Contact) {
|
2018-04-04 07:33:31 +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 {
|
2018-06-30 11:35:17 +00:00
|
|
|
Jid jid = bookmark.getJid();
|
|
|
|
Account account = bookmark.getAccount();
|
|
|
|
Contact contact = jid == null ? null : account.getRoster().getContact(jid);
|
2018-09-21 10:27:58 +00:00
|
|
|
if (contact != null && contact.getAvatarFilename() != null) {
|
2018-06-30 11:35:17 +00:00
|
|
|
return get(contact, size, cachedOnly);
|
|
|
|
}
|
|
|
|
String seed = jid != null ? jid.asBareJid().toString() : null;
|
2017-12-05 13:15:10 +00:00
|
|
|
return get(bookmark.getDisplayName(), seed, size, cachedOnly);
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-05 17:30:40 +00:00
|
|
|
String seed = item.getJid() != null ? item.getJid().asBareJid().toString() : null;
|
2017-12-05 13:15:10 +00:00
|
|
|
return get(item.getDisplayName(), seed, 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) {
|
2018-04-04 07:33:31 +00:00
|
|
|
return get(conversation, size, false);
|
2015-02-15 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2017-11-19 00:53:04 +00:00
|
|
|
synchronized (this.conversationDependentKeys) {
|
|
|
|
Set<String> keys = this.conversationDependentKeys.get(conversation.getUuid());
|
|
|
|
if (keys == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LruCache<String, Bitmap> cache = this.mXmppConnectionService.getBitmapCache();
|
2018-04-04 07:33:31 +00:00
|
|
|
for (String key : keys) {
|
2017-11-19 00:53:04 +00:00
|
|
|
cache.remove(key);
|
|
|
|
}
|
|
|
|
keys.clear();
|
|
|
|
}
|
2014-10-21 12:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 07:33:31 +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;
|
|
|
|
}
|
2018-04-14 15:54:04 +00:00
|
|
|
|
|
|
|
bitmap = mXmppConnectionService.getFileBackend().getAvatar(mucOptions.getAvatar(), size);
|
|
|
|
|
|
|
|
if (bitmap == null) {
|
|
|
|
final List<MucOptions.User> users = mucOptions.getUsersRelevantForNameAndAvatar();
|
|
|
|
if (users.size() == 0) {
|
|
|
|
Conversation c = mucOptions.getConversation();
|
|
|
|
bitmap = getImpl(c.getName().toString(), c.getJid().asBareJid().toString(), size);
|
|
|
|
} else {
|
|
|
|
bitmap = getImpl(users, size);
|
|
|
|
}
|
2017-11-19 00:53:04 +00:00
|
|
|
}
|
2018-04-14 15:54:04 +00:00
|
|
|
|
2017-11-19 00:53:04 +00:00
|
|
|
this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
|
2018-04-14 15:54:04 +00:00
|
|
|
|
2017-11-19 00:53:04 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap get(List<MucOptions.User> users, int size, boolean cachedOnly) {
|
|
|
|
final String KEY = key(users, size);
|
|
|
|
Bitmap bitmap = this.mXmppConnectionService.getBitmapCache().get(KEY);
|
|
|
|
if (bitmap != null || cachedOnly) {
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
bitmap = getImpl(users, size);
|
2018-04-04 07:33:31 +00:00
|
|
|
this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
|
2017-11-19 00:53:04 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap getImpl(List<MucOptions.User> users, int size) {
|
2014-10-20 19:08:33 +00:00
|
|
|
int count = users.size();
|
2017-11-19 00:53:04 +00:00
|
|
|
Bitmap 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) {
|
2017-11-19 00:53:04 +00:00
|
|
|
throw new AssertionError("Unable to draw tiles for 0 users");
|
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);
|
2017-11-19 00:53:04 +00:00
|
|
|
drawTile(canvas, users.get(0).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);
|
|
|
|
}
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2018-04-25 12:41:33 +00:00
|
|
|
public void clear(final MucOptions options) {
|
|
|
|
if (options == null) {
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 12:41:33 +00:00
|
|
|
private String key(final MucOptions options, int size) {
|
2014-10-21 12:57:16 +00:00
|
|
|
synchronized (this.sizes) {
|
|
|
|
if (!this.sizes.contains(size)) {
|
|
|
|
this.sizes.add(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid()
|
|
|
|
+ "_" + String.valueOf(size);
|
|
|
|
}
|
|
|
|
|
2017-11-19 00:53:04 +00:00
|
|
|
private String key(List<MucOptions.User> users, int size) {
|
|
|
|
final Conversation conversation = users.get(0).getConversation();
|
|
|
|
StringBuilder builder = new StringBuilder("TILE_");
|
|
|
|
builder.append(conversation.getUuid());
|
|
|
|
|
2018-04-04 07:33:31 +00:00
|
|
|
for (MucOptions.User user : users) {
|
2017-11-19 00:53:04 +00:00
|
|
|
builder.append("\0");
|
2018-03-22 14:10:29 +00:00
|
|
|
builder.append(emptyOnNull(user.getRealJid()));
|
2017-11-19 00:53:04 +00:00
|
|
|
builder.append("\0");
|
2018-03-22 14:10:29 +00:00
|
|
|
builder.append(emptyOnNull(user.getFullJid()));
|
2017-11-19 00:53:04 +00:00
|
|
|
}
|
2018-03-22 14:10:29 +00:00
|
|
|
builder.append('\0');
|
|
|
|
builder.append(size);
|
2017-11-19 00:53:04 +00:00
|
|
|
final String key = builder.toString();
|
|
|
|
synchronized (this.conversationDependentKeys) {
|
|
|
|
Set<String> keys;
|
|
|
|
if (this.conversationDependentKeys.containsKey(conversation.getUuid())) {
|
|
|
|
keys = this.conversationDependentKeys.get(conversation.getUuid());
|
|
|
|
} else {
|
|
|
|
keys = new HashSet<>();
|
2018-04-04 07:33:31 +00:00
|
|
|
this.conversationDependentKeys.put(conversation.getUuid(), keys);
|
2017-11-19 00:53:04 +00:00
|
|
|
}
|
|
|
|
keys.add(key);
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:57:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-04-11 20:20:32 +00:00
|
|
|
avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
|
2014-10-20 19:08:33 +00:00
|
|
|
if (avatar == null) {
|
2018-04-04 07:33:31 +00:00
|
|
|
avatar = get(account.getJid().asBareJid().toString(), null, 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) {
|
2018-04-26 11:22:31 +00:00
|
|
|
final Conversational conversation = message.getConversation();
|
2017-11-19 00:53:04 +00:00
|
|
|
if (message.getType() == Message.TYPE_STATUS && message.getCounterparts() != null && message.getCounterparts().size() > 1) {
|
2018-04-04 07:33:31 +00:00
|
|
|
return get(message.getCounterparts(), size, cachedOnly);
|
2017-11-19 00:53:04 +00:00
|
|
|
} else if (message.getStatus() == Message.STATUS_RECEIVED) {
|
2015-12-03 17:18:34 +00:00
|
|
|
Contact c = message.getContact();
|
2018-09-21 10:27:58 +00:00
|
|
|
if (c != null && (c.getProfilePhoto() != null || c.getAvatarFilename() != null)) {
|
2015-12-03 17:18:34 +00:00
|
|
|
return get(c, size, cachedOnly);
|
2018-04-26 11:22:31 +00:00
|
|
|
} else if (conversation instanceof Conversation && message.getConversation().getMode() == Conversation.MODE_MULTI) {
|
2018-01-09 09:13:34 +00:00
|
|
|
final Jid trueCounterpart = message.getTrueCounterpart();
|
2018-04-26 11:22:31 +00:00
|
|
|
final MucOptions mucOptions = ((Conversation) conversation).getMucOptions();
|
2018-01-09 09:13:34 +00:00
|
|
|
MucOptions.User user;
|
|
|
|
if (trueCounterpart != null) {
|
2018-09-21 10:27:58 +00:00
|
|
|
user = mucOptions.findOrCreateUserByRealJid(trueCounterpart, message.getCounterpart());
|
2018-01-09 09:13:34 +00:00
|
|
|
} else {
|
2018-04-26 11:22:31 +00:00
|
|
|
user = mucOptions.findUserByFullJid(message.getCounterpart());
|
2018-01-09 09:13:34 +00:00
|
|
|
}
|
2015-12-03 17:18:34 +00:00
|
|
|
if (user != null) {
|
2018-04-04 07:33:31 +00:00
|
|
|
return getImpl(user, size, cachedOnly);
|
2015-12-03 17:18:34 +00:00
|
|
|
}
|
2017-12-05 13:15:10 +00:00
|
|
|
} else if (c != null) {
|
|
|
|
return get(c, size, cachedOnly);
|
2015-10-29 11:08:15 +00:00
|
|
|
}
|
2017-12-05 13:15:10 +00:00
|
|
|
Jid tcp = message.getTrueCounterpart();
|
2018-04-04 07:33:31 +00:00
|
|
|
String seed = tcp != null ? tcp.asBareJid().toString() : null;
|
2017-12-05 13:15:10 +00:00
|
|
|
return get(UIHelper.getMessageDisplayName(message), seed, size, cachedOnly);
|
2018-04-04 07:33:31 +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);
|
|
|
|
}
|
|
|
|
|
2017-12-05 13:15:10 +00:00
|
|
|
/*public Bitmap get(String name, int size) {
|
|
|
|
return get(name,null, size,false);
|
|
|
|
}*/
|
2015-02-15 17:48:05 +00:00
|
|
|
|
2017-12-05 13:15:10 +00:00
|
|
|
public Bitmap get(final String name, String seed, final int size, boolean cachedOnly) {
|
|
|
|
final String KEY = key(seed == null ? name : seed, size);
|
2014-10-21 12:57:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-12-05 13:15:10 +00:00
|
|
|
bitmap = getImpl(name, seed, size);
|
2017-11-19 00:53:04 +00:00
|
|
|
mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2017-12-05 13:15:10 +00:00
|
|
|
private Bitmap getImpl(final String name, final String seed, final int size) {
|
2017-11-19 00:53:04 +00:00
|
|
|
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
2014-10-20 19:08:33 +00:00
|
|
|
Canvas canvas = new Canvas(bitmap);
|
2016-07-27 18:11:22 +00:00
|
|
|
final String trimmedName = name == null ? "" : name.trim();
|
2017-12-05 13:15:10 +00:00
|
|
|
drawTile(canvas, trimmedName, seed, 0, 0, size, size);
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-05 13:15:10 +00:00
|
|
|
private boolean drawTile(Canvas canvas, String letter, int tileColor, 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
|
|
|
}
|
|
|
|
|
2018-03-22 14:10:29 +00:00
|
|
|
private boolean drawTile(Canvas canvas, MucOptions.User user, int left, int top, int right, int bottom) {
|
2014-10-20 19:08:33 +00:00
|
|
|
Contact contact = user.getContact();
|
|
|
|
if (contact != null) {
|
|
|
|
Uri uri = null;
|
2018-11-07 12:42:01 +00:00
|
|
|
if (contact.getAvatarFilename() != null) {
|
|
|
|
uri = mXmppConnectionService.getFileBackend().getAvatarUri(contact.getAvatarFilename());
|
|
|
|
} else if (contact.getProfilePhoto() != null) {
|
2014-11-04 23:06:07 +00:00
|
|
|
uri = Uri.parse(contact.getProfilePhoto());
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|
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
|
|
|
}
|
2017-12-05 13:15:10 +00:00
|
|
|
if (contact != null) {
|
2018-03-05 17:30:40 +00:00
|
|
|
String seed = contact.getJid().asBareJid().toString();
|
2017-12-05 13:15:10 +00:00
|
|
|
drawTile(canvas, contact.getDisplayName(), seed, left, top, right, bottom);
|
|
|
|
} else {
|
2018-03-05 17:30:40 +00:00
|
|
|
String seed = user.getRealJid() == null ? null : user.getRealJid().asBareJid().toString();
|
2017-12-05 13:15:10 +00:00
|
|
|
drawTile(canvas, user.getName(), seed, left, top, right, bottom);
|
|
|
|
}
|
2015-11-10 18:20:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-03-05 17:30:40 +00:00
|
|
|
String name = account.getJid().asBareJid().toString();
|
2017-12-05 13:15:10 +00:00
|
|
|
return drawTile(canvas, name, name, left, top, right, bottom);
|
2015-11-10 18:20:35 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 13:15:10 +00:00
|
|
|
private boolean drawTile(Canvas canvas, String name, String seed, int left, int top, int right, int bottom) {
|
2015-11-10 18:20:35 +00:00
|
|
|
if (name != null) {
|
2016-05-27 09:34:12 +00:00
|
|
|
final String letter = getFirstLetter(name);
|
2017-12-05 13:15:10 +00:00
|
|
|
final int color = UIHelper.getColorForName(seed == null ? name : seed);
|
2015-11-10 18:20:35 +00:00
|
|
|
drawTile(canvas, letter, color, left, top, right, bottom);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-27 09:34:12 +00:00
|
|
|
private static String getFirstLetter(String name) {
|
2018-04-04 07:33:31 +00:00
|
|
|
for (Character c : name.toCharArray()) {
|
2016-05-27 09:34:12 +00:00
|
|
|
if (Character.isLetterOrDigit(c)) {
|
|
|
|
return c.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "X";
|
|
|
|
}
|
|
|
|
|
2015-11-10 18:20:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-11 20:20:32 +00:00
|
|
|
private boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, 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
|
|
|
}
|
2016-04-11 20:20:32 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdvancedStreamFeaturesAvailable(Account account) {
|
|
|
|
XmppConnection.Features features = account.getXmppConnection().getFeatures();
|
|
|
|
if (features.pep() && !features.pepPersistent()) {
|
2018-04-04 07:33:31 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has pep but is not persistent");
|
2016-04-11 20:20:32 +00:00
|
|
|
if (account.getAvatar() != null) {
|
|
|
|
mXmppConnectionService.republishAvatarIfNeeded(account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 14:10:29 +00:00
|
|
|
|
|
|
|
private static String emptyOnNull(@Nullable Jid value) {
|
|
|
|
return value == null ? "" : value.toString();
|
|
|
|
}
|
2014-10-20 19:08:33 +00:00
|
|
|
}
|