Refined avatar generation a bit
This commit is contained in:
parent
614372dcbb
commit
b5fe8789e9
|
@ -82,7 +82,7 @@ public class Bookmark extends Element implements ListItem {
|
||||||
for (Element element : getChildren()) {
|
for (Element element : getChildren()) {
|
||||||
if (element.getName().equals("group") && element.getContent() != null) {
|
if (element.getName().equals("group") && element.getContent() != null) {
|
||||||
String group = element.getContent();
|
String group = element.getContent();
|
||||||
tags.add(new Tag(group, UIHelper.getColorForName(group)));
|
tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tags;
|
return tags;
|
||||||
|
|
|
@ -30,6 +30,7 @@ import eu.siacs.conversations.entities.MucOptions;
|
||||||
import eu.siacs.conversations.utils.UIHelper;
|
import eu.siacs.conversations.utils.UIHelper;
|
||||||
import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
|
import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
|
||||||
import eu.siacs.conversations.xmpp.XmppConnection;
|
import eu.siacs.conversations.xmpp.XmppConnection;
|
||||||
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
||||||
|
|
||||||
public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
|
|
||||||
|
@ -67,7 +68,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatar(), size);
|
avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatar(), size);
|
||||||
}
|
}
|
||||||
if (avatar == null) {
|
if (avatar == null) {
|
||||||
avatar = get(contact.getDisplayName(), size, cachedOnly);
|
avatar = get(contact.getDisplayName(), contact.getJid().toBareJid().toString(), size, cachedOnly);
|
||||||
}
|
}
|
||||||
this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
||||||
return avatar;
|
return avatar;
|
||||||
|
@ -114,7 +115,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
if (contact != null) {
|
if (contact != null) {
|
||||||
avatar = get(contact, size, cachedOnly);
|
avatar = get(contact, size, cachedOnly);
|
||||||
} else {
|
} else {
|
||||||
avatar = get(user.getName(), size, cachedOnly);
|
String seed = user.getRealJid() != null ? user.getRealJid().toBareJid().toString() : null;
|
||||||
|
avatar = get(user.getName(), seed, size, cachedOnly);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
this.mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
||||||
|
@ -165,10 +167,12 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
if (bookmark.getConversation() != null) {
|
if (bookmark.getConversation() != null) {
|
||||||
return get(bookmark.getConversation(), size, cachedOnly);
|
return get(bookmark.getConversation(), size, cachedOnly);
|
||||||
} else {
|
} else {
|
||||||
return get(bookmark.getDisplayName(), size, cachedOnly);
|
String seed = bookmark.getJid() != null ? bookmark.getJid().toBareJid().toString() : null;
|
||||||
|
return get(bookmark.getDisplayName(), seed, size, cachedOnly);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return get(item.getDisplayName(), size, cachedOnly);
|
String seed = item.getJid() != null ? item.getJid().toBareJid().toString() : null;
|
||||||
|
return get(item.getDisplayName(), seed, size, cachedOnly);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +215,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
}
|
}
|
||||||
final List<MucOptions.User> users = mucOptions.getUsers(5);
|
final List<MucOptions.User> users = mucOptions.getUsers(5);
|
||||||
if (users.size() == 0) {
|
if (users.size() == 0) {
|
||||||
bitmap = getImpl(mucOptions.getConversation().getName(),size);
|
Conversation c = mucOptions.getConversation();
|
||||||
|
bitmap = getImpl(c.getName(),c.getJid().toBareJid().toString(),size);
|
||||||
} else {
|
} else {
|
||||||
bitmap = getImpl(users,size);
|
bitmap = getImpl(users,size);
|
||||||
}
|
}
|
||||||
|
@ -319,7 +324,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
}
|
}
|
||||||
avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
|
avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
|
||||||
if (avatar == null) {
|
if (avatar == null) {
|
||||||
avatar = get(account.getJid().toBareJid().toString(), size,false);
|
avatar = get(account.getJid().toBareJid().toString(), null, size,false);
|
||||||
}
|
}
|
||||||
mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
mXmppConnectionService.getBitmapCache().put(KEY, avatar);
|
||||||
return avatar;
|
return avatar;
|
||||||
|
@ -338,8 +343,12 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
return getImpl(user,size,cachedOnly);
|
return getImpl(user,size,cachedOnly);
|
||||||
}
|
}
|
||||||
|
} else if (c != null) {
|
||||||
|
return get(c, size, cachedOnly);
|
||||||
}
|
}
|
||||||
return get(UIHelper.getMessageDisplayName(message), size, cachedOnly);
|
Jid tcp = message.getTrueCounterpart();
|
||||||
|
String seed = tcp != null ? tcp.toBareJid().toString() :null;
|
||||||
|
return get(UIHelper.getMessageDisplayName(message), seed, size, cachedOnly);
|
||||||
} else {
|
} else {
|
||||||
return get(conversation.getAccount(), size, cachedOnly);
|
return get(conversation.getAccount(), size, cachedOnly);
|
||||||
}
|
}
|
||||||
|
@ -371,26 +380,26 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
+ String.valueOf(size);
|
+ String.valueOf(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap get(String name, int size) {
|
/*public Bitmap get(String name, int size) {
|
||||||
return get(name,size,false);
|
return get(name,null, size,false);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public Bitmap get(final String name, final int size, boolean cachedOnly) {
|
public Bitmap get(final String name, String seed, final int size, boolean cachedOnly) {
|
||||||
final String KEY = key(name, size);
|
final String KEY = key(seed == null ? name : seed, size);
|
||||||
Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
|
Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
|
||||||
if (bitmap != null || cachedOnly) {
|
if (bitmap != null || cachedOnly) {
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
bitmap = getImpl(name, size);
|
bitmap = getImpl(name, seed, size);
|
||||||
mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
|
mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Bitmap getImpl(final String name, final int size) {
|
private Bitmap getImpl(final String name, final String seed, final int size) {
|
||||||
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
final String trimmedName = name == null ? "" : name.trim();
|
final String trimmedName = name == null ? "" : name.trim();
|
||||||
drawTile(canvas, trimmedName, 0, 0, size, size);
|
drawTile(canvas, trimmedName, seed, 0, 0, size, size);
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,8 +412,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
|
return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean drawTile(Canvas canvas, String letter, int tileColor,
|
private boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) {
|
||||||
int left, int top, int right, int bottom) {
|
|
||||||
letter = letter.toUpperCase(Locale.getDefault());
|
letter = letter.toUpperCase(Locale.getDefault());
|
||||||
Paint tilePaint = new Paint(), textPaint = new Paint();
|
Paint tilePaint = new Paint(), textPaint = new Paint();
|
||||||
tilePaint.setColor(tileColor);
|
tilePaint.setColor(tileColor);
|
||||||
|
@ -443,8 +451,13 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String name = contact != null ? contact.getDisplayName() : user.getName();
|
if (contact != null) {
|
||||||
drawTile(canvas, name, left, top, right, bottom);
|
String seed = contact.getJid().toBareJid().toString();
|
||||||
|
drawTile(canvas, contact.getDisplayName(), seed, left, top, right, bottom);
|
||||||
|
} else {
|
||||||
|
String seed = user.getRealJid() == null ? null : user.getRealJid().toBareJid().toString();
|
||||||
|
drawTile(canvas, user.getName(), seed, left, top, right, bottom);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -458,13 +471,14 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom);
|
String name = account.getJid().toBareJid().toString();
|
||||||
|
return drawTile(canvas, name, name, left, top, right, bottom);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {
|
private boolean drawTile(Canvas canvas, String name, String seed, int left, int top, int right, int bottom) {
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
final String letter = getFirstLetter(name);
|
final String letter = getFirstLetter(name);
|
||||||
final int color = UIHelper.getColorForName(name);
|
final int color = UIHelper.getColorForName(seed == null ? name : seed);
|
||||||
drawTile(canvas, letter, color, left, top, right, bottom);
|
drawTile(canvas, letter, color, left, top, right, bottom);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,7 +112,7 @@ public class AccountAdapter extends ArrayAdapter<Account> {
|
||||||
imageView.setImageBitmap(bm);
|
imageView.setImageBitmap(bm);
|
||||||
imageView.setBackgroundColor(0x00000000);
|
imageView.setBackgroundColor(0x00000000);
|
||||||
} else {
|
} else {
|
||||||
imageView.setBackgroundColor(UIHelper.getColorForName(account.getDisplayName()));
|
imageView.setBackgroundColor(UIHelper.getColorForName(account.getJid().toBareJid().toString()));
|
||||||
imageView.setImageDrawable(null);
|
imageView.setImageDrawable(null);
|
||||||
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
|
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
|
||||||
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
|
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
|
||||||
|
|
|
@ -127,7 +127,8 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
|
||||||
imageView.setImageBitmap(bm);
|
imageView.setImageBitmap(bm);
|
||||||
imageView.setBackgroundColor(0x00000000);
|
imageView.setBackgroundColor(0x00000000);
|
||||||
} else {
|
} else {
|
||||||
imageView.setBackgroundColor(UIHelper.getColorForName(item.getDisplayName()));
|
String seed = item.getJid() != null ? item.getJid().toBareJid().toString() : item.getDisplayName();
|
||||||
|
imageView.setBackgroundColor(UIHelper.getColorForName(seed));
|
||||||
imageView.setImageDrawable(null);
|
imageView.setImageDrawable(null);
|
||||||
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
|
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
|
||||||
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
|
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
|
||||||
|
|
|
@ -30,35 +30,78 @@ import eu.siacs.conversations.xmpp.jid.Jid;
|
||||||
|
|
||||||
public class UIHelper {
|
public class UIHelper {
|
||||||
|
|
||||||
|
private static int[] UNSAFE_COLORS = {
|
||||||
|
0xFFF44336, //red 500
|
||||||
|
0xFFE53935, //red 600
|
||||||
|
0xFFD32F2F, //red 700
|
||||||
|
0xFFC62828, //red 800
|
||||||
|
|
||||||
private static int COLORS[] = {
|
|
||||||
0xFFE91E63, //pink 500
|
|
||||||
0xFFAD1457, //pink 800
|
|
||||||
0xFF9C27B0, //purple 500
|
|
||||||
0xFF6A1B9A, //purple 800
|
|
||||||
0xFF673AB7, //deep purple 500,
|
|
||||||
0xFF4527A0, //deep purple 800,
|
|
||||||
0xFF3F51B5, //indigo 500,
|
|
||||||
0xFF283593, //indigo 800
|
|
||||||
0xFF2196F3, //blue 500
|
|
||||||
0xFF1565C0, //blue 800
|
|
||||||
0xFF03A9F4, //light blue 500
|
|
||||||
0xFF0277BD, //light blue 800
|
|
||||||
0xFF00BCD4, //cyan 500
|
|
||||||
0xFF00838F, //cyan 800
|
|
||||||
0xFF009688, //teal 500,
|
|
||||||
0xFF00695C, //teal 800,
|
|
||||||
//0xFF558B2F, //light green 800
|
|
||||||
0xFFC0CA33, //lime 600
|
|
||||||
0xFF9E9D24, //lime 800
|
|
||||||
0xFFEF6C00, //orange 800
|
0xFFEF6C00, //orange 800
|
||||||
|
|
||||||
|
0xFFF4511E, //deep orange 600
|
||||||
|
0xFFE64A19, //deep orange 700
|
||||||
0xFFD84315, //deep orange 800,
|
0xFFD84315, //deep orange 800,
|
||||||
|
};
|
||||||
|
|
||||||
|
private static int[] SAFE_COLORS = {
|
||||||
|
0xFFE91E63, //pink 500
|
||||||
|
0xFFD81B60, //pink 600
|
||||||
|
0xFFC2185B, //pink 700
|
||||||
|
0xFFAD1457, //pink 800
|
||||||
|
|
||||||
|
0xFF9C27B0, //purple 500
|
||||||
|
0xFF8E24AA, //purple 600
|
||||||
|
0xFF7B1FA2, //purple 700
|
||||||
|
0xFF6A1B9A, //purple 800
|
||||||
|
|
||||||
|
0xFF673AB7, //deep purple 500,
|
||||||
|
0xFF5E35B1, //deep purple 600
|
||||||
|
0xFF512DA8, //deep purple 700
|
||||||
|
0xFF4527A0, //deep purple 800,
|
||||||
|
|
||||||
|
0xFF3F51B5, //indigo 500,
|
||||||
|
0xFF3949AB,//indigo 600
|
||||||
|
0xFF303F9F,//indigo 700
|
||||||
|
0xFF283593, //indigo 800
|
||||||
|
|
||||||
|
0xFF2196F3, //blue 500
|
||||||
|
0xFF1E88E5, //blue 600
|
||||||
|
0xFF1976D2, //blue 700
|
||||||
|
0xFF1565C0, //blue 800
|
||||||
|
|
||||||
|
0xFF03A9F4, //light blue 500
|
||||||
|
0xFF039BE5, //light blue 600
|
||||||
|
0xFF0288D1, //light blue 700
|
||||||
|
0xFF0277BD, //light blue 800
|
||||||
|
|
||||||
|
0xFF00BCD4, //cyan 500
|
||||||
|
0xFF00ACC1, //cyan 600
|
||||||
|
0xFF0097A7, //cyan 700
|
||||||
|
0xFF00838F, //cyan 800
|
||||||
|
|
||||||
|
0xFF009688, //teal 500,
|
||||||
|
0xFF00897B, //teal 600
|
||||||
|
0xFF00796B, //teal 700
|
||||||
|
0xFF00695C, //teal 800,
|
||||||
|
|
||||||
|
//0xFF558B2F, //light green 800
|
||||||
|
|
||||||
|
//0xFFC0CA33, //lime 600
|
||||||
|
0xFF9E9D24, //lime 800
|
||||||
|
|
||||||
0xFF795548, //brown 500,
|
0xFF795548, //brown 500,
|
||||||
//0xFF4E342E, //brown 800
|
//0xFF4E342E, //brown 800
|
||||||
0xFF607D8B, //blue grey 500,
|
0xFF607D8B, //blue grey 500,
|
||||||
0xFF37474F //blue grey 800
|
//0xFF37474F //blue grey 800
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static final int[] COLORS;
|
||||||
|
|
||||||
|
static {
|
||||||
|
COLORS = Arrays.copyOf(SAFE_COLORS, SAFE_COLORS.length + UNSAFE_COLORS.length);
|
||||||
|
System.arraycopy(UNSAFE_COLORS, 0, COLORS, SAFE_COLORS.length, UNSAFE_COLORS.length);
|
||||||
|
}
|
||||||
|
|
||||||
private static final List<String> LOCATION_QUESTIONS = Arrays.asList(
|
private static final List<String> LOCATION_QUESTIONS = Arrays.asList(
|
||||||
"where are you", //en
|
"where are you", //en
|
||||||
"where are you now", //en
|
"where are you now", //en
|
||||||
|
@ -162,8 +205,7 @@ public class UIHelper {
|
||||||
} else if (difference < 60 * 2) {
|
} else if (difference < 60 * 2) {
|
||||||
return context.getString(R.string.last_seen_min);
|
return context.getString(R.string.last_seen_min);
|
||||||
} else if (difference < 60 * 60) {
|
} else if (difference < 60 * 60) {
|
||||||
return context.getString(R.string.last_seen_mins,
|
return context.getString(R.string.last_seen_mins, Math.round(difference / 60.0));
|
||||||
Math.round(difference / 60.0));
|
|
||||||
} else if (difference < 60 * 60 * 2) {
|
} else if (difference < 60 * 60 * 2) {
|
||||||
return context.getString(R.string.last_seen_hour);
|
return context.getString(R.string.last_seen_hour);
|
||||||
} else if (difference < 60 * 60 * 24) {
|
} else if (difference < 60 * 60 * 24) {
|
||||||
|
@ -178,18 +220,24 @@ public class UIHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getColorForName(String name) {
|
public static int getColorForName(String name) {
|
||||||
|
return getColorForName(name, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getColorForName(String name, boolean safe) {
|
||||||
if (name == null || name.isEmpty()) {
|
if (name == null || name.isEmpty()) {
|
||||||
return 0xFF202020;
|
return 0xFF202020;
|
||||||
}
|
}
|
||||||
return COLORS[getIntForName(name) % COLORS.length];
|
if (safe) {
|
||||||
|
return SAFE_COLORS[(int) (getLongForName(name) % SAFE_COLORS.length)];
|
||||||
|
} else {
|
||||||
|
return COLORS[(int) (getLongForName(name) % COLORS.length)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getIntForName(String name) {
|
private static long getLongForName(String name) {
|
||||||
try {
|
try {
|
||||||
final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
||||||
messageDigest.update(name.getBytes());
|
return Math.abs(new BigInteger(messageDigest.digest(name.getBytes())).longValue());
|
||||||
byte[] bytes = messageDigest.digest();
|
|
||||||
return Math.abs(new BigInteger(bytes).intValue());
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue