package de.thedevstack.conversationsplus.services.avatar; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Typeface; import android.net.Uri; import java.util.List; import java.util.Locale; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Contact; import de.thedevstack.conversationsplus.entities.MucOptions; import de.thedevstack.conversationsplus.utils.AvatarUtil; import de.thedevstack.conversationsplus.utils.ImageUtil; import de.thedevstack.conversationsplus.utils.UIHelper; /** */ public final class AvatarBitmapUtil { private static final int FG_COLOR = 0xFFFAFAFA; private static final int TRANSPARENT = 0x00000000; private static final int PLACEHOLDER_COLOR = 0xFF202020; public static Bitmap createAvatarBitmap(String name, int size) { Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawTile(canvas, name, 0, 0, size, size); return bitmap; } public static Bitmap createAvatarBitmap(MucOptions mucOptions, int size) { final List users = mucOptions.getUsers(); int count = users.size(); Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); bitmap.eraseColor(TRANSPARENT); if (count == 0) { String name = mucOptions.getConversation().getName(); drawTile(canvas, name, 0, 0, size, size); } else if (count == 1) { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size); drawTile(canvas, mucOptions.getConversation().getAccount(), size / 2 + 1, 0, size, size); } else if (count == 2) { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size); drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size); } else if (count == 3) { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size); drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1); drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size, size); } else if (count == 4) { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1); drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size); drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1); drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size, size); } else { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1); drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size); drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1); drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1, size, size); } return bitmap; } private static 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); textPaint.setFlags(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(FG_COLOR); textPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); textPaint.setTextSize((float) ((right - left) * 0.8)); Rect rect = new Rect(); canvas.drawRect(new Rect(left, top, right, bottom), tilePaint); textPaint.getTextBounds(letter, 0, 1, rect); float width = textPaint.measureText(letter); canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom) / 2 + rect.height() / 2, textPaint); return true; } private static boolean drawTile(Canvas canvas, MucOptions.User user, int left, int top, int right, int bottom) { Contact contact = user.getContact(); if (contact != null) { Uri uri = null; if (contact.getProfilePhoto() != null) { uri = Uri.parse(contact.getProfilePhoto()); } else if (contact.getAvatar() != null) { uri = AvatarUtil.getAvatarUri(contact.getAvatar()); } if (drawTile(canvas, uri, left, top, right, bottom)) { return true; } } else if (user.getAvatar() != null) { Uri uri = AvatarUtil.getAvatarUri(user.getAvatar()); if (drawTile(canvas, uri, left, top, right, bottom)) { return true; } } String name = contact != null ? contact.getDisplayName() : user.getName(); drawTile(canvas, name, left, top, right, bottom); return true; } private static boolean drawTile(Canvas canvas, Account account, int left, int top, int right, int bottom) { String avatar = account.getAvatar(); if (avatar != null) { Uri uri = AvatarUtil.getAvatarUri(avatar); if (uri != null) { if (drawTile(canvas, uri, left, top, right, bottom)) { return true; } } } return drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom); } private static boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) { if (name != null) { String trimmedName = name.trim(); final String letter = trimmedName.isEmpty() ? "X" : trimmedName.substring(0, 1); final int color = UIHelper.getColorForName(name); drawTile(canvas, letter, color, left, top, right, bottom); return true; } return false; } private static boolean drawTile(Canvas canvas, Uri uri, int left, int top, int right, int bottom) { if (uri != null) { Bitmap bitmap = ImageUtil.cropCenter(uri, bottom - top, right - left); if (bitmap != null) { drawTile(canvas, bitmap, left, top, right, bottom); return true; } } return false; } private static boolean drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, int dstright, int dstbottom) { Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom); canvas.drawBitmap(bm, null, dst, null); return true; } }