diff --git a/app/src/main/java/eu/faircode/email/ContactInfo.java b/app/src/main/java/eu/faircode/email/ContactInfo.java index 7c7737c7f7..c69524b1ae 100644 --- a/app/src/main/java/eu/faircode/email/ContactInfo.java +++ b/app/src/main/java/eu/faircode/email/ContactInfo.java @@ -133,13 +133,16 @@ public class ContactInfo { } if (info.bitmap == null) { + TypedValue tv = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.themeName, tv, true); + int dp = Helper.dp2pixels(context, 48); + + boolean dark = !"light".equals(tv.string); boolean identicons = prefs.getBoolean("identicons", false); - if (identicons) { - TypedValue tv = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.themeName, tv, true); - int dp = Helper.dp2pixels(context, 48); - info.bitmap = Identicon.generate(key, dp, 5, !"light".equals(tv.string)); - } + if (identicons) + info.bitmap = Identicon.icon(key, dp, 5, dark); + else + info.bitmap = Identicon.letter(key, dp, dark); } if (info.displayName == null) diff --git a/app/src/main/java/eu/faircode/email/Identicon.java b/app/src/main/java/eu/faircode/email/Identicon.java index 4189038706..a5247567ef 100644 --- a/app/src/main/java/eu/faircode/email/Identicon.java +++ b/app/src/main/java/eu/faircode/email/Identicon.java @@ -31,13 +31,8 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; class Identicon { - static Bitmap generate(String email, int size, int pixels, boolean dark) { - byte[] hash; - try { - hash = MessageDigest.getInstance("MD5").digest(email.getBytes()); - } catch (NoSuchAlgorithmException ignored) { - hash = email.getBytes(); - } + static Bitmap icon(String email, int size, int pixels, boolean dark) { + byte[] hash = getHash(email); int color = Color.argb(255, hash[0], hash[1], hash[2]); color = ColorUtils.blendARGB(color, dark ? Color.WHITE : Color.BLACK, 0.2f); @@ -63,4 +58,40 @@ class Identicon { return bitmap; } + + static Bitmap letter(String email, int size, boolean dark) { + byte[] hash = getHash(email); + + int color = Color.argb(255, hash[0], hash[1], hash[2]); + color = ColorUtils.blendARGB(color, dark ? Color.WHITE : Color.BLACK, 0.2f); + + Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + canvas.drawColor(color); + + Paint paint = new Paint(); + paint.setColor(Color.argb( + Color.alpha(color), + (~Color.red(color)) & 0xff, + (~Color.green(color)) & 0xff, + (~Color.blue(color)) & 0xff)); + paint.setTextSize(size / 2f); + paint.setFakeBoldText(true); + + String text = email.substring(0, 1).toUpperCase(); + canvas.drawText( + text, + size / 2f - paint.measureText(text) / 2, + size / 2f - ((paint.descent() + paint.ascent()) / 2), paint); + + return bitmap; + } + + private static byte[] getHash(String email) { + try { + return MessageDigest.getInstance("MD5").digest(email.getBytes()); + } catch (NoSuchAlgorithmException ignored) { + return email.getBytes(); + } + } }