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
@ -79,7 +122,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;
@ -106,7 +149,7 @@ public class UIHelper {
} else if (difference < 60 * 2) {
return context.getString(R.string.minute_ago);
} else if (difference < 60 * 15) {
return context.getString(R.string.minutes_ago,Math.round(difference / 60.0));
return context.getString(R.string.minutes_ago, Math.round(difference / 60.0));
} else if (today(date)) {
java.text.DateFormat df = DateFormat.getTimeFormat(context);
return df.format(date);
@ -122,17 +165,17 @@ public class UIHelper {
}
private static boolean today(Date date) {
return sameDay(date,new Date(System.currentTimeMillis()));
return sameDay(date, new Date(System.currentTimeMillis()));
}
public static boolean today(long date) {
return sameDay(date,System.currentTimeMillis());
return sameDay(date, System.currentTimeMillis());
}
public static boolean yesterday(long date) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.add(Calendar.DAY_OF_YEAR,-1);
cal1.add(Calendar.DAY_OF_YEAR, -1);
cal2.setTime(new Date(date));
return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
&& cal1.get(Calendar.DAY_OF_YEAR) == cal2
@ -140,7 +183,7 @@ public class UIHelper {
}
public static boolean sameDay(long a, long b) {
return sameDay(new Date(a),new Date(b));
return sameDay(new Date(a), new Date(b));
}
private static boolean sameDay(Date a, Date b) {
@ -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,42 +220,48 @@ 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;
}
}
public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
public static Pair<String, Boolean> getMessagePreview(final Context context, final Message message) {
final Transferable d = message.getTransferable();
if (d != null ) {
if (d != null) {
switch (d.getStatus()) {
case Transferable.STATUS_CHECKING:
return new Pair<>(context.getString(R.string.checking_x,
getFileDescriptionString(context,message)),true);
getFileDescriptionString(context, message)), true);
case Transferable.STATUS_DOWNLOADING:
return new Pair<>(context.getString(R.string.receiving_x_file,
getFileDescriptionString(context,message),
d.getProgress()),true);
getFileDescriptionString(context, message),
d.getProgress()), true);
case Transferable.STATUS_OFFER:
case Transferable.STATUS_OFFER_CHECK_FILESIZE:
return new Pair<>(context.getString(R.string.x_file_offered_for_download,
getFileDescriptionString(context,message)),true);
getFileDescriptionString(context, message)), true);
case Transferable.STATUS_DELETED:
return new Pair<>(context.getString(R.string.file_deleted),true);
return new Pair<>(context.getString(R.string.file_deleted), true);
case Transferable.STATUS_FAILED:
return new Pair<>(context.getString(R.string.file_transmission_failed),true);
return new Pair<>(context.getString(R.string.file_transmission_failed), true);
case Transferable.STATUS_UPLOADING:
if (message.getStatus() == Message.STATUS_OFFERED) {
return new Pair<>(context.getString(R.string.offering_x_file,
@ -223,10 +271,10 @@ public class UIHelper {
getFileDescriptionString(context, message)), true);
}
default:
return new Pair<>("",false);
return new Pair<>("", false);
}
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
return new Pair<>(context.getString(R.string.pgp_message),true);
return new Pair<>(context.getString(R.string.pgp_message), true);
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
return new Pair<>(context.getString(R.string.decryption_failed), true);
} else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
@ -234,7 +282,7 @@ public class UIHelper {
return new Pair<>(context.getString(R.string.received_x_file,
getFileDescriptionString(context, message)), true);
} else {
return new Pair<>(getFileDescriptionString(context,message),true);
return new Pair<>(getFileDescriptionString(context, message), true);
}
} else {
final String body = message.getBody();
@ -249,19 +297,19 @@ public class UIHelper {
}
} else if (message.treatAsDownloadable()) {
return new Pair<>(context.getString(R.string.x_file_offered_for_download,
getFileDescriptionString(context,message)),true);
getFileDescriptionString(context, message)), true);
} else {
String[] lines = body.split("\n");
StringBuilder builder = new StringBuilder();
for(String l : lines) {
for (String l : lines) {
if (l.length() > 0) {
char first = l.charAt(0);
if ((first != '>' || !isPositionFollowedByQuoteableCharacter(l,0)) && first != '\u00bb') {
if ((first != '>' || !isPositionFollowedByQuoteableCharacter(l, 0)) && first != '\u00bb') {
String line = l.trim();
if (line.isEmpty()) {
continue;
}
char last = line.charAt(line.length()-1);
char last = line.charAt(line.length() - 1);
if (builder.length() != 0) {
builder.append(' ');
}
@ -275,20 +323,20 @@ public class UIHelper {
if (builder.length() == 0) {
builder.append(body.trim());
}
return new Pair<>(builder.length() > 256 ? builder.substring(0,256) : builder.toString(), false);
return new Pair<>(builder.length() > 256 ? builder.substring(0, 256) : builder.toString(), false);
}
}
}
public static boolean isPositionFollowedByQuoteableCharacter(CharSequence body, int pos) {
return !isPositionFollowedByNumber(body, pos)
&& !isPositionFollowedByEmoticon(body,pos)
&& !isPositionFollowedByEquals(body,pos);
&& !isPositionFollowedByEmoticon(body, pos)
&& !isPositionFollowedByEquals(body, pos);
}
private static boolean isPositionFollowedByNumber(CharSequence body, int pos) {
boolean previousWasNumber = false;
for (int i = pos +1; i < body.length(); i++) {
for (int i = pos + 1; i < body.length(); i++) {
char c = body.charAt(i);
if (Character.isDigit(body.charAt(i))) {
previousWasNumber = true;
@ -302,22 +350,22 @@ public class UIHelper {
}
private static boolean isPositionFollowedByEquals(CharSequence body, int pos) {
return body.length() > pos + 1 && body.charAt(pos+1) == '=';
return body.length() > pos + 1 && body.charAt(pos + 1) == '=';
}
private static boolean isPositionFollowedByEmoticon(CharSequence body, int pos) {
if (body.length() <= pos +1) {
if (body.length() <= pos + 1) {
return false;
} else {
final char first = body.charAt(pos +1);
final char first = body.charAt(pos + 1);
return first == ';'
|| first == ':'
|| closingBeforeWhitespace(body,pos+1);
|| closingBeforeWhitespace(body, pos + 1);
}
}
private static boolean closingBeforeWhitespace(CharSequence body, int pos) {
for(int i = pos; i < body.length(); ++i) {
for (int i = pos; i < body.length(); ++i) {
final char c = body.charAt(i);
if (Character.isWhitespace(c)) {
return false;
@ -329,11 +377,11 @@ public class UIHelper {
}
public static boolean isPositionFollowedByQuote(CharSequence body, int pos) {
if (body.length() <= pos + 1 || Character.isWhitespace(body.charAt(pos+1))) {
if (body.length() <= pos + 1 || Character.isWhitespace(body.charAt(pos + 1))) {
return false;
}
boolean previousWasWhitespace = false;
for (int i = pos +1; i < body.length(); i++) {
for (int i = pos + 1; i < body.length(); i++) {
char c = body.charAt(i);
if (c == '\n' || c == '»') {
return false;
@ -364,13 +412,13 @@ public class UIHelper {
}
public static String concatNames(List<MucOptions.User> users) {
return concatNames(users,users.size());
return concatNames(users, users.size());
}
public static String concatNames(List<MucOptions.User> users, int max) {
StringBuilder builder = new StringBuilder();
final boolean shortNames = users.size() >= 3;
for(int i = 0; i < Math.max(users.size(),max); ++i) {
for (int i = 0; i < Math.max(users.size(), max); ++i) {
if (builder.length() != 0) {
builder.append(", ");
}
@ -389,16 +437,16 @@ public class UIHelper {
return context.getString(R.string.file);
} else if (mime.startsWith("audio/")) {
return context.getString(R.string.audio);
} else if(mime.startsWith("video/")) {
} else if (mime.startsWith("video/")) {
return context.getString(R.string.video);
} else if (mime.startsWith("image/")) {
return context.getString(R.string.image);
} else if (mime.contains("pdf")) {
return context.getString(R.string.pdf_document) ;
return context.getString(R.string.pdf_document);
} else if (mime.contains("application/vnd.android.package-archive")) {
return context.getString(R.string.apk) ;
return context.getString(R.string.apk);
} else if (mime.contains("vcard")) {
return context.getString(R.string.vcard) ;
return context.getString(R.string.vcard);
} else {
return mime;
}
@ -433,7 +481,7 @@ public class UIHelper {
if (Config.multipleEncryptionChoices()) {
return context.getString(R.string.send_unencrypted_message);
} else {
return context.getString(R.string.send_message_to_x,conversation.getName());
return context.getString(R.string.send_message_to_x, conversation.getName());
}
case Message.ENCRYPTION_OTR:
return context.getString(R.string.send_otr_message);
@ -452,7 +500,7 @@ public class UIHelper {
}
public static String getDisplayedMucCounterpart(final Jid counterpart) {
if (counterpart==null) {
if (counterpart == null) {
return "";
} else if (!counterpart.isBareJid()) {
return counterpart.getResourcepart().trim();
@ -468,7 +516,7 @@ public class UIHelper {
return false;
}
String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
body = body.replace("?","").replace("¿","");
body = body.replace("?", "").replace("¿", "");
return LOCATION_QUESTIONS.contains(body);
}