From 3d6f03e859cfc4727c68bfda424b58228f4ffa61 Mon Sep 17 00:00:00 2001 From: andy Date: Fri, 11 Apr 2014 22:30:50 +0200 Subject: [PATCH 1/3] Refactor UIHelper to use getContactPicture Normalize the interface, reduce code duplication --- .../conversations/ui/ContactDetailsActivity.java | 2 +- src/eu/siacs/conversations/utils/UIHelper.java | 16 +++------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/eu/siacs/conversations/ui/ContactDetailsActivity.java b/src/eu/siacs/conversations/ui/ContactDetailsActivity.java index 5dc6eb3b7..86535ba1a 100644 --- a/src/eu/siacs/conversations/ui/ContactDetailsActivity.java +++ b/src/eu/siacs/conversations/ui/ContactDetailsActivity.java @@ -230,7 +230,7 @@ public class ContactDetailsActivity extends XmppActivity { contactJid.setText(contact.getJid()); accountJid.setText(contact.getAccount().getJid()); - UIHelper.prepareContactBadge(this, badge, contact); + UIHelper.prepareContactBadge(this, badge, contact, getApplicationContext()); if (contact.getSystemAccount() == null) { badge.setOnClickListener(onBadgeClick); diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 73a0494b0..0d0e7b915 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -260,7 +260,7 @@ public class UIHelper { if (notify) { if (vibrate) { int dat = 70; - long[] pattern = {0,3*dat,dat,dat,dat,3*dat,dat,dat}; + long[] pattern = {0,3*dat,dat,dat}; mBuilder.setVibrate(pattern); } mBuilder.setLights(0xffffffff, 2000, 4000); @@ -307,23 +307,13 @@ public class UIHelper { } public static void prepareContactBadge(final Activity activity, - QuickContactBadge badge, final Contact contact) { + QuickContactBadge badge, final Contact contact, Context context) { if (contact.getSystemAccount() != null) { String[] systemAccount = contact.getSystemAccount().split("#"); long id = Long.parseLong(systemAccount[0]); badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1])); - - if (contact.getProfilePhoto() != null) { - badge.setImageURI(Uri.parse(contact.getProfilePhoto())); - } else { - badge.setImageBitmap(UIHelper.getUnknownContactPicture( - contact.getDisplayName(), 400)); - } - } else { - badge.setImageBitmap(UIHelper.getUnknownContactPicture( - contact.getDisplayName(), 400)); } - + badge.setImageBitmap(UIHelper.getContactPicture(contact, "", 400, context)); } public static AlertDialog getVerifyFingerprintDialog( From c2307337368e37fc3c7fec7d4b2e5cdf3be47212 Mon Sep 17 00:00:00 2001 From: andy Date: Sat, 12 Apr 2014 01:36:44 +0200 Subject: [PATCH 2/3] More Contact Picture refactors Use polymorphism to avoid dynamic dispatch based on nulled parameters. Next up: Prettier MUC icons --- .../ui/ConversationActivity.java | 3 +- .../ui/ConversationFragment.java | 7 +++- .../conversations/ui/MucDetailsActivity.java | 2 +- .../conversations/ui/ShareWithActivity.java | 7 +++- .../siacs/conversations/utils/UIHelper.java | 38 +++++++++++++------ 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index 06a726899..20d839614 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -164,8 +164,7 @@ public class ConversationActivity extends XmppActivity { ImageView imageView = (ImageView) view .findViewById(R.id.conversation_image); imageView.setImageBitmap(UIHelper.getContactPicture( - conv.getContact(), conv.getName(useSubject), 200, - activity.getApplicationContext())); + conv, 200, activity.getApplicationContext())); return view; } diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index 04fb6f923..3796f83e1 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -614,7 +614,12 @@ public class ConversationFragment extends Fragment { if (bitmaps.containsKey(name)) { return bitmaps.get(name); } else { - Bitmap bm = UIHelper.getContactPicture(contact, name, 200, context); + Bitmap bm; + if (contact != null){ + bm = UIHelper.getContactPicture(contact, 200, context); + } else { + bm = UIHelper.getContactPicture(name, 200, context); + } bitmaps.put(name, bm); return bm; } diff --git a/src/eu/siacs/conversations/ui/MucDetailsActivity.java b/src/eu/siacs/conversations/ui/MucDetailsActivity.java index bf36f156e..b5ecd7962 100644 --- a/src/eu/siacs/conversations/ui/MucDetailsActivity.java +++ b/src/eu/siacs/conversations/ui/MucDetailsActivity.java @@ -179,7 +179,7 @@ public class MucDetailsActivity extends XmppActivity { role.setText(getReadableRole(contact.getRole())); ImageView imageView = (ImageView) view .findViewById(R.id.contact_photo); - imageView.setImageBitmap(UIHelper.getContactPicture(null,contact.getName(), 90,this.getApplicationContext())); + imageView.setImageBitmap(UIHelper.getContactPicture(contact.getName(), 89,this.getApplicationContext())); membersView.addView(view); } } diff --git a/src/eu/siacs/conversations/ui/ShareWithActivity.java b/src/eu/siacs/conversations/ui/ShareWithActivity.java index 51bad721d..3a6304854 100644 --- a/src/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/eu/siacs/conversations/ui/ShareWithActivity.java @@ -81,7 +81,9 @@ public class ShareWithActivity extends XmppActivity { } }); for(final Conversation conversation : convList) { - View view = createContactView(conversation.getName(useSubject), conversation.getLatestMessage().getBody().trim(), UIHelper.getContactPicture(conversation.getContact(),conversation.getName(useSubject), 90,this.getApplicationContext())); + View view = createContactView(conversation.getName(useSubject), + conversation.getLatestMessage().getBody().trim(), + UIHelper.getContactPicture(conversation, 90,this.getApplicationContext())); view.setOnClickListener(new OnClickListener() { @Override @@ -115,7 +117,8 @@ public class ShareWithActivity extends XmppActivity { for(int i = 0; i < contactsList.size(); ++i) { final Contact con = contactsList.get(i); - View view = createContactView(con.getDisplayName(), con.getJid(), UIHelper.getContactPicture(con,null, 90,this.getApplicationContext())); + View view = createContactView(con.getDisplayName(), con.getJid(), + UIHelper.getContactPicture(con, 90, this.getApplicationContext())); view.setOnClickListener(new OnClickListener() { @Override diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 0d0e7b915..7654b33f3 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -91,23 +91,39 @@ public class UIHelper { return bitmap; } - - public static Bitmap getContactPicture(Contact contact, String fallback, int size, Context context) { - if (contact==null) { - return getUnknownContactPicture(fallback, size); + + public static Bitmap getContactPicture(Conversation conversation, int size, Context context) { + if(conversation.getMode() == Conversation.MODE_SINGLE) { + if (conversation.getContact() != null){ + return getContactPicture(conversation.getContact(), size, context); + } else { + return getContactPicture(conversation.getName(false), size); + } + } else{ + return getContactPicture(conversation.getName(false), size); } + } + + public static Bitmap getContactPicture(Contact contact, int size, Context context) { String uri = contact.getProfilePhoto(); if (uri==null) { - return getUnknownContactPicture(contact.getDisplayName(), size); + return getContactPicture(contact.getDisplayName(), size); } try { Bitmap bm = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(Uri.parse(uri))); return Bitmap.createScaledBitmap(bm, size, size, false); } catch (FileNotFoundException e) { - return getUnknownContactPicture(contact.getDisplayName(), size); + return getContactPicture(contact.getDisplayName(), size); } } + public static Bitmap getContactPicture(String name, int size, Context context) { + return getContactPicture(name, size); + } + + public static Bitmap getContactPicture(String name, int size) { + return getUnknownContactPicture(name, size); + } public static Bitmap getErrorPicture(int size) { Bitmap bitmap = Bitmap .createBitmap(size, size, Bitmap.Config.ARGB_8888); @@ -212,7 +228,7 @@ public class UIHelper { } else if (unread.size() == 1) { Conversation conversation = unread.get(0); targetUuid = conversation.getUuid(); - mBuilder.setLargeIcon(UIHelper.getContactPicture(conversation.getContact(), conversation.getName(useSubject), (int) res + mBuilder.setLargeIcon(UIHelper.getContactPicture(conversation, (int) res .getDimension(android.R.dimen.notification_large_icon_width), context)); mBuilder.setContentTitle(conversation.getName(useSubject)); if (notify) { @@ -313,7 +329,7 @@ public class UIHelper { long id = Long.parseLong(systemAccount[0]); badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1])); } - badge.setImageBitmap(UIHelper.getContactPicture(contact, "", 400, context)); + badge.setImageBitmap(UIHelper.getContactPicture(contact, 400, context)); } public static AlertDialog getVerifyFingerprintDialog( @@ -357,12 +373,12 @@ public class UIHelper { return BitmapFactory.decodeStream(activity .getContentResolver().openInputStream(selfiUri)); } catch (FileNotFoundException e) { - return getUnknownContactPicture(account.getJid(), size); + return getContactPicture(account.getJid(), size); } } - return getUnknownContactPicture(account.getJid(), size); + return getContactPicture(account.getJid(), size); } else { - return getUnknownContactPicture(account.getJid(), size); + return getContactPicture(account.getJid(), size); } } } From f3b07250ddabb612e9cd9fc7985c7d97a9b844d8 Mon Sep 17 00:00:00 2001 From: andy Date: Sat, 12 Apr 2014 03:24:30 +0200 Subject: [PATCH 3/3] 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 7654b33f3..3a4c23785 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);