From f3b07250ddabb612e9cd9fc7985c7d97a9b844d8 Mon Sep 17 00:00:00 2001 From: andy Date: Sat, 12 Apr 2014 03:24:30 +0200 Subject: [PATCH] Added tiled MUC icons Now, MUCs have icons with up to 4 tiles representing MUC members, similar to the gmail app. Some caveats: As of now, no ordering is imposed on the members, we simply pick the first (up to) 4 returned by the MucOptions object. This could be done better, e.g. by picking more important members first (based on affil/role), or even going so far as to order them by how recently they sent a message in the MUC. Also, the code generating the tiles is really messy right now (tons of copy and paste). It seems to work though. I will clean this up soon. --- .../siacs/conversations/utils/UIHelper.java | 180 ++++++++++++++++-- 1 file changed, 164 insertions(+), 16 deletions(-) diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 7654b33f..3a4c2378 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -12,6 +12,8 @@ import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.entities.MucOptions; +import eu.siacs.conversations.entities.MucOptions.User; import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.ManageAccountActivity; @@ -65,33 +67,178 @@ public class UIHelper { } } - private static Bitmap getUnknownContactPicture(String name, int size) { - String firstLetter = (name.length() > 0) ? name.substring(0, 1).toUpperCase(Locale.US) : " "; - + private static int getNameColor(String name) { int holoColors[] = { 0xFF1da9da, 0xFFb368d9, 0xFF83b600, 0xFFffa713, 0xFFe92727 }; - int color = holoColors[Math.abs(name.toLowerCase(Locale.getDefault()).hashCode()) % holoColors.length]; + return color; + } + private static Bitmap getUnknownContactPicture(String[] names, int size) { + final int fgColor = 0xffe5e5e5; + int tiles = (names.length > 4)? 4 : names.length; Bitmap bitmap = Bitmap .createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); - bitmap.eraseColor(color); + String[] letters = new String[tiles]; + int[] colors = new int[tiles]; + for(int i = 0; i < tiles; ++i) { + letters[i] = (names[i].length() > 0) ? + names[i].substring(0, 1).toUpperCase(Locale.US) : " "; + colors[i] = getNameColor(names[i]); + } + Paint textPaint = new Paint(), tilePaint = new Paint(); + textPaint.setColor(fgColor); + Rect rect, left, right, topLeft, bottomLeft, topRight, bottomRight; + float width; - Paint paint = new Paint(); - paint.setColor(0xffe5e5e5); - paint.setTextSize((float) (size * 0.9)); - paint.setAntiAlias(true); - Rect rect = new Rect(); - paint.getTextBounds(firstLetter, 0, 1, rect); - float width = paint.measureText(firstLetter); - canvas.drawText(firstLetter, (size / 2) - (width / 2), (size / 2) - + (rect.height() / 2), paint); + switch(tiles) { + case 1: + bitmap.eraseColor(colors[0]); + textPaint.setTextSize((float) (size * 0.9)); + textPaint.setAntiAlias(true); + rect = new Rect(); + textPaint.getTextBounds(letters[0], 0, 1, rect); + width = textPaint.measureText(letters[0]); + canvas.drawText(letters[0], (size / 2) - (width / 2), (size / 2) + + (rect.height() / 2), textPaint); + break; + + case 2: + bitmap.eraseColor(fgColor); + + tilePaint.setColor(colors[0]); + left = new Rect(0, 0, (size/2)-2, size); + canvas.drawRect(left, tilePaint); + + tilePaint.setColor(colors[1]); + right = new Rect((size/2)+2, 0, size, size); + canvas.drawRect(right, tilePaint); + + textPaint.setTextSize((float) (size * 0.9*0.5)); + textPaint.setAntiAlias(true); + rect = new Rect(); + textPaint.getTextBounds(letters[0], 0, 1, rect); + width = textPaint.measureText(letters[0]); + canvas.drawText(letters[0], (size / 4) - (width / 2), (size / 2) + + (rect.height() / 2), textPaint); + textPaint.getTextBounds(letters[1], 0, 1, rect); + width = textPaint.measureText(letters[1]); + canvas.drawText(letters[1], (3 * size / 4) - (width / 2), (size / 2) + + (rect.height() / 2), textPaint); + break; + + case 3: + bitmap.eraseColor(fgColor); + + tilePaint.setColor(colors[0]); + left = new Rect(0, 0, (size/2)-2, size); + canvas.drawRect(left, tilePaint); + + tilePaint.setColor(colors[1]); + topRight = new Rect((size/2)+2, 0, size, (size/2 - 2)); + canvas.drawRect(topRight, tilePaint); + + tilePaint.setColor(colors[2]); + bottomRight = new Rect((size/2)+2, (size/2 + 2), size, size); + canvas.drawRect(bottomRight, tilePaint); + + textPaint.setTextSize((float) (size * 0.9*0.5)); + textPaint.setAntiAlias(true); + rect = new Rect(); + + textPaint.getTextBounds(letters[0], 0, 1, rect); + width = textPaint.measureText(letters[0]); + canvas.drawText(letters[0], (size / 4) - (width / 2), (size / 2) + + (rect.height() / 2), textPaint); + + textPaint.getTextBounds(letters[1], 0, 1, rect); + width = textPaint.measureText(letters[1]); + canvas.drawText(letters[1], (3 * size / 4) - (width / 2), (size / 4) + + (rect.height() / 2), textPaint); + + textPaint.getTextBounds(letters[2], 0, 1, rect); + width = textPaint.measureText(letters[2]); + canvas.drawText(letters[2], (3 * size / 4) - (width / 2), (3* size / 4) + + (rect.height() / 2), textPaint); + break; + + case 4: + bitmap.eraseColor(fgColor); + + tilePaint.setColor(colors[0]); + topLeft = new Rect(0, 0, (size/2)-2, (size/2)-2); + canvas.drawRect(topLeft, tilePaint); + + tilePaint.setColor(colors[1]); + bottomLeft = new Rect(0, (size/2)+2, (size/2)-2, size); + canvas.drawRect(bottomLeft, tilePaint); + + tilePaint.setColor(colors[2]); + topRight = new Rect((size/2)+2, 0, size, (size/2 - 2)); + canvas.drawRect(topRight, tilePaint); + + tilePaint.setColor(colors[3]); + bottomRight = new Rect((size/2)+2, (size/2 + 2), size, size); + canvas.drawRect(bottomRight, tilePaint); + + textPaint.setTextSize((float) (size * 0.9*0.5)); + textPaint.setAntiAlias(true); + rect = new Rect(); + + textPaint.getTextBounds(letters[0], 0, 1, rect); + width = textPaint.measureText(letters[0]); + canvas.drawText(letters[0], (size / 4) - (width / 2), (size / 4) + + (rect.height() / 2), textPaint); + + textPaint.getTextBounds(letters[1], 0, 1, rect); + width = textPaint.measureText(letters[1]); + canvas.drawText(letters[1], (size / 4) - (width / 2), (3* size / 4) + + (rect.height() / 2), textPaint); + + textPaint.getTextBounds(letters[2], 0, 1, rect); + width = textPaint.measureText(letters[2]); + canvas.drawText(letters[2], (3 * size / 4) - (width / 2), (size / 4) + + (rect.height() / 2), textPaint); + + textPaint.getTextBounds(letters[3], 0, 1, rect); + width = textPaint.measureText(letters[3]); + canvas.drawText(letters[3], (3 * size / 4) - (width / 2), (3* size / 4) + + (rect.height() / 2), textPaint); + break; + + default: + bitmap.eraseColor(colors[0]); + + textPaint.setTextSize((float) (size * 0.9)); + textPaint.setAntiAlias(true); + rect = new Rect(); + textPaint.getTextBounds(letters[0], 0, 1, rect); + width = textPaint.measureText(letters[0]); + canvas.drawText(letters[0], (size / 2) - (width / 2), (size / 2) + + (rect.height() / 2), textPaint); + break; + + } return bitmap; } + private static Bitmap getMucContactPicture(Conversation conversation, int size) { + List members = conversation.getMucOptions().getUsers(); + if (members.size() == 0) { + return getUnknownContactPicture(new String[]{conversation.getName(false)}, size); + } + String[] names = new String[members.size()+1]; + names[0] = conversation.getMucOptions().getNick(); + for(int i = 0; i < members.size(); ++i) { + names[i+1] = members.get(i).getName(); + } + + return getUnknownContactPicture(names, size); + } + public static Bitmap getContactPicture(Conversation conversation, int size, Context context) { if(conversation.getMode() == Conversation.MODE_SINGLE) { if (conversation.getContact() != null){ @@ -100,7 +247,7 @@ public class UIHelper { return getContactPicture(conversation.getName(false), size); } } else{ - return getContactPicture(conversation.getName(false), size); + return getMucContactPicture(conversation, size); } } @@ -122,8 +269,9 @@ public class UIHelper { } public static Bitmap getContactPicture(String name, int size) { - return getUnknownContactPicture(name, size); + return getUnknownContactPicture(new String[]{name}, size); } + public static Bitmap getErrorPicture(int size) { Bitmap bitmap = Bitmap .createBitmap(size, size, Bitmap.Config.ARGB_8888);