Refined avatar generation a bit

This commit is contained in:
Daniel Gultsch 2017-12-05 14:15:10 +01:00
parent 614372dcbb
commit b5fe8789e9
5 changed files with 165 additions and 102 deletions

View File

@ -82,7 +82,7 @@ public class Bookmark extends Element implements ListItem {
for (Element element : getChildren()) {
if (element.getName().equals("group") && element.getContent() != null) {
String group = element.getContent();
tags.add(new Tag(group, UIHelper.getColorForName(group)));
tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
}
}
return tags;

View File

@ -30,6 +30,7 @@ import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.jid.Jid;
public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
@ -67,7 +68,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
avatar = mXmppConnectionService.getFileBackend().getAvatar(contact.getAvatar(), size);
}
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);
return avatar;
@ -114,7 +115,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
if (contact != null) {
avatar = get(contact, size, cachedOnly);
} 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);
@ -165,10 +167,12 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
if (bookmark.getConversation() != null) {
return get(bookmark.getConversation(), size, cachedOnly);
} 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 {
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);
if (users.size() == 0) {
bitmap = getImpl(mucOptions.getConversation().getName(),size);
Conversation c = mucOptions.getConversation();
bitmap = getImpl(c.getName(),c.getJid().toBareJid().toString(),size);
} else {
bitmap = getImpl(users,size);
}
@ -319,7 +324,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
}
avatar = mXmppConnectionService.getFileBackend().getAvatar(account.getAvatar(), size);
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);
return avatar;
@ -338,8 +343,12 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
if (user != null) {
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 {
return get(conversation.getAccount(), size, cachedOnly);
}
@ -371,26 +380,26 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
+ String.valueOf(size);
}
public Bitmap get(String name, int size) {
return get(name,size,false);
}
/*public Bitmap get(String name, int size) {
return get(name,null, size,false);
}*/
public Bitmap get(final String name, final int size, boolean cachedOnly) {
final String KEY = key(name, size);
public Bitmap get(final String name, String seed, final int size, boolean cachedOnly) {
final String KEY = key(seed == null ? name : seed, size);
Bitmap bitmap = mXmppConnectionService.getBitmapCache().get(KEY);
if (bitmap != null || cachedOnly) {
return bitmap;
}
bitmap = getImpl(name, size);
bitmap = getImpl(name, seed, size);
mXmppConnectionService.getBitmapCache().put(KEY, 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);
Canvas canvas = new Canvas(bitmap);
final String trimmedName = name == null ? "" : name.trim();
drawTile(canvas, trimmedName, 0, 0, size, size);
drawTile(canvas, trimmedName, seed, 0, 0, size, size);
return bitmap;
}
@ -403,8 +412,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size);
}
private boolean drawTile(Canvas canvas, String letter, int tileColor,
int left, int top, int right, int bottom) {
private boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) {
letter = letter.toUpperCase(Locale.getDefault());
Paint tilePaint = new Paint(), textPaint = new Paint();
tilePaint.setColor(tileColor);
@ -443,8 +451,13 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
return true;
}
}
String name = contact != null ? contact.getDisplayName() : user.getName();
drawTile(canvas, name, left, top, right, bottom);
if (contact != null) {
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;
}
@ -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) {
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);
return true;
}

View File

@ -112,7 +112,7 @@ public class AccountAdapter extends ArrayAdapter<Account> {
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setBackgroundColor(UIHelper.getColorForName(account.getDisplayName()));
imageView.setBackgroundColor(UIHelper.getColorForName(account.getJid().toBareJid().toString()));
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);

View File

@ -127,7 +127,8 @@ public class ListItemAdapter extends ArrayAdapter<ListItem> {
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} 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);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);

View File

@ -30,35 +30,78 @@ import eu.siacs.conversations.xmpp.jid.Jid;
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
0xFFF4511E, //deep orange 600
0xFFE64A19, //deep orange 700
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,
//0xFF4E342E, //brown 800
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(
"where are you", //en
"where are you now", //en
@ -162,8 +205,7 @@ public class UIHelper {
} else if (difference < 60 * 2) {
return context.getString(R.string.last_seen_min);
} else if (difference < 60 * 60) {
return context.getString(R.string.last_seen_mins,
Math.round(difference / 60.0));
return context.getString(R.string.last_seen_mins, Math.round(difference / 60.0));
} else if (difference < 60 * 60 * 2) {
return context.getString(R.string.last_seen_hour);
} else if (difference < 60 * 60 * 24) {
@ -178,18 +220,24 @@ public class UIHelper {
}
public static int getColorForName(String name) {
return getColorForName(name, false);
}
public static int getColorForName(String name, boolean safe) {
if (name == null || name.isEmpty()) {
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 {
final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(name.getBytes());
byte[] bytes = messageDigest.digest();
return Math.abs(new BigInteger(bytes).intValue());
return Math.abs(new BigInteger(messageDigest.digest(name.getBytes())).longValue());
} catch (Exception e) {
return 0;
}