Refined avatar generation a bit

This commit is contained in:
Christian Schneppe 2017-12-10 21:33:48 +01:00
parent 213672da66
commit 8031e1e521
5 changed files with 114 additions and 52 deletions

View file

@ -87,7 +87,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), 0));
tags.add(new Tag(group, UIHelper.getColorForName(group, true), 0));
}
}
return tags;

View file

@ -30,6 +30,7 @@ import de.pixart.messenger.entities.MucOptions;
import de.pixart.messenger.utils.UIHelper;
import de.pixart.messenger.xmpp.OnAdvancedStreamFeaturesLoaded;
import de.pixart.messenger.xmpp.XmppConnection;
import de.pixart.messenger.xmpp.jid.Jid;
public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
@ -71,7 +72,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
}
}
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;
@ -118,7 +119,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);
@ -169,10 +171,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);
}
}
@ -215,7 +219,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);
}
@ -323,7 +328,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;
@ -342,8 +347,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);
}
@ -375,26 +384,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;
}
@ -407,8 +416,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);
@ -457,8 +465,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;
}
@ -472,13 +485,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

@ -98,7 +98,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

@ -160,7 +160,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,34 +30,75 @@ import de.pixart.messenger.xmpp.jid.Jid;
public class UIHelper {
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
private static int[] UNSAFE_COLORS = {
0xFFF44336, //red 500
0xFFE53935, //red 600
0xFFD32F2F, //red 700
0xFFC62828, //red 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
@ -78,7 +119,7 @@ public class UIHelper {
"donde estas" //es
);
private static final List<Character> PUNCTIONATION = Arrays.asList('.',',','?','!',';',':');
private static final List<Character> PUNCTIONATION = Arrays.asList('.', ',', '?', '!', ';', ':');
private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
@ -177,18 +218,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;
}