From 901ce085a7df579b9c8c402b8af59a7ce7233044 Mon Sep 17 00:00:00 2001 From: Andreas Straub Date: Sun, 13 Apr 2014 20:41:06 +0200 Subject: [PATCH] Major Contact Picture code cleanup Notifications now have pseudotransparent icons, icons are generated in the correct size. --- .../conversations/ui/ContactsActivity.java | 2 +- .../ui/ConversationActivity.java | 2 +- .../ui/ConversationFragment.java | 6 +- .../conversations/ui/MucDetailsActivity.java | 2 +- .../conversations/ui/ShareWithActivity.java | 5 +- .../conversations/utils/PhoneHelper.java | 4 +- .../siacs/conversations/utils/UIHelper.java | 105 ++++++++++-------- 7 files changed, 71 insertions(+), 55 deletions(-) diff --git a/src/eu/siacs/conversations/ui/ContactsActivity.java b/src/eu/siacs/conversations/ui/ContactsActivity.java index 835e0a615..e403450a0 100644 --- a/src/eu/siacs/conversations/ui/ContactsActivity.java +++ b/src/eu/siacs/conversations/ui/ContactsActivity.java @@ -381,7 +381,7 @@ public class ContactsActivity extends XmppActivity { contactJid.setText(contact.getJid()); ImageView imageView = (ImageView) view .findViewById(R.id.contact_photo); - imageView.setImageBitmap(UIHelper.getContactPicture(contact, 90, this.getContext())); + imageView.setImageBitmap(UIHelper.getContactPicture(contact, 48, this.getContext(), false)); return view; } }; diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index 20d839614..6ce31302e 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -164,7 +164,7 @@ public class ConversationActivity extends XmppActivity { ImageView imageView = (ImageView) view .findViewById(R.id.conversation_image); imageView.setImageBitmap(UIHelper.getContactPicture( - conv, 200, activity.getApplicationContext())); + conv, 56, activity.getApplicationContext(), false)); return view; } diff --git a/src/eu/siacs/conversations/ui/ConversationFragment.java b/src/eu/siacs/conversations/ui/ConversationFragment.java index 3796f83e1..61843b820 100644 --- a/src/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/eu/siacs/conversations/ui/ConversationFragment.java @@ -333,7 +333,7 @@ public class ConversationFragment extends Fragment { boolean showPhoneSelfContactPicture = sharedPref.getBoolean( "show_phone_selfcontact_picture", true); - return UIHelper.getSelfContactPicture(conversation.getAccount(), 200, + return UIHelper.getSelfContactPicture(conversation.getAccount(), 48, showPhoneSelfContactPicture, getActivity()); } @@ -616,9 +616,9 @@ public class ConversationFragment extends Fragment { } else { Bitmap bm; if (contact != null){ - bm = UIHelper.getContactPicture(contact, 200, context); + bm = UIHelper.getContactPicture(contact, 48, context, false); } else { - bm = UIHelper.getContactPicture(name, 200, context); + bm = UIHelper.getContactPicture(name, 48, context, false); } 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 02bc8ddb6..c6807c612 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(contact.getName(), 90,this.getApplicationContext())); + imageView.setImageBitmap(UIHelper.getContactPicture(contact.getName(), 48,this.getApplicationContext(), false)); membersView.addView(view); } } diff --git a/src/eu/siacs/conversations/ui/ShareWithActivity.java b/src/eu/siacs/conversations/ui/ShareWithActivity.java index 3a6304854..1bc9fc460 100644 --- a/src/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/eu/siacs/conversations/ui/ShareWithActivity.java @@ -83,7 +83,8 @@ public class ShareWithActivity extends XmppActivity { for(final Conversation conversation : convList) { View view = createContactView(conversation.getName(useSubject), conversation.getLatestMessage().getBody().trim(), - UIHelper.getContactPicture(conversation, 90,this.getApplicationContext())); + UIHelper.getContactPicture(conversation, 48, + this.getApplicationContext(), false)); view.setOnClickListener(new OnClickListener() { @Override @@ -118,7 +119,7 @@ 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, 90, this.getApplicationContext())); + UIHelper.getContactPicture(con, 48, this.getApplicationContext(), false)); view.setOnClickListener(new OnClickListener() { @Override diff --git a/src/eu/siacs/conversations/utils/PhoneHelper.java b/src/eu/siacs/conversations/utils/PhoneHelper.java index dd3e0e19c..6355e3786 100644 --- a/src/eu/siacs/conversations/utils/PhoneHelper.java +++ b/src/eu/siacs/conversations/utils/PhoneHelper.java @@ -72,10 +72,10 @@ public class PhoneHelper { mCursorLoader.startLoading(); } - public static Uri getSefliUri(Activity activity) { + public static Uri getSefliUri(Context context) { String[] mProjection = new String[] { Profile._ID, Profile.PHOTO_THUMBNAIL_URI }; - Cursor mProfileCursor = activity.getContentResolver().query( + Cursor mProfileCursor = context.getContentResolver().query( Profile.CONTENT_URI, mProjection, null, null, null); if (mProfileCursor.getCount() == 0) { diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index 312f42b35..c794db5e1 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -40,6 +40,7 @@ import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.InboxStyle; import android.support.v4.app.TaskStackBuilder; import android.text.Html; +import android.util.DisplayMetrics; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -48,6 +49,10 @@ import android.widget.QuickContactBadge; import android.widget.TextView; public class UIHelper { + private static final int BG_COLOR = 0xFF181818; + private static final int FG_COLOR = 0xFFE5E5E5; + private static final int TRANSPARENT = 0x00000000; + public static String readableTimeDifference(long time) { if (time == 0) { return "just now"; @@ -67,6 +72,11 @@ public class UIHelper { } } + public static int getRealPx(int dp, Context context) { + final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); + return ((int) (dp * metrics.density)); + } + private static int getNameColor(String name) { int holoColors[] = { 0xFF1da9da, 0xFFb368d9, 0xFF83b600, 0xFFffa713, 0xFFe92727 }; @@ -74,9 +84,8 @@ public class UIHelper { return color; } - private static Bitmap getUnknownContactPicture(String[] names, int size) { - final int fgColor = 0xffe5e5e5; int tiles = (names.length > 4)? 4 : names.length; + private static Bitmap getUnknownContactPicture(String[] names, int size, int bgColor, int fgColor) { Bitmap bitmap = Bitmap .createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); @@ -107,7 +116,7 @@ public class UIHelper { break; case 2: - bitmap.eraseColor(fgColor); + bitmap.eraseColor(bgColor); tilePaint.setColor(colors[0]); left = new Rect(0, 0, (size/2)-2, size); @@ -131,7 +140,7 @@ public class UIHelper { break; case 3: - bitmap.eraseColor(fgColor); + bitmap.eraseColor(bgColor); tilePaint.setColor(colors[0]); left = new Rect(0, 0, (size/2)-2, size); @@ -166,7 +175,7 @@ public class UIHelper { break; case 4: - bitmap.eraseColor(fgColor); + bitmap.eraseColor(bgColor); tilePaint.setColor(colors[0]); topLeft = new Rect(0, 0, (size/2)-2, (size/2)-2); @@ -208,27 +217,18 @@ public class UIHelper { 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) { + private static Bitmap getUnknownContactPicture(String[] names, int size) { + return getUnknownContactPicture(names, size, UIHelper.BG_COLOR, UIHelper.FG_COLOR); + } + + private static Bitmap getMucContactPicture(Conversation conversation, int size, int bgColor, int fgColor) { List members = conversation.getMucOptions().getUsers(); if (members.size() == 0) { - return getUnknownContactPicture(new String[]{conversation.getName(false)}, size); + return getUnknownContactPicture(new String[]{conversation.getName(false)}, size, bgColor, fgColor); } String[] names = new String[members.size()+1]; names[0] = conversation.getMucOptions().getNick(); @@ -236,40 +236,56 @@ public class UIHelper { names[i+1] = members.get(i).getName(); } - return getUnknownContactPicture(names, size); + return getUnknownContactPicture(names, size, bgColor, fgColor); } - public static Bitmap getContactPicture(Conversation conversation, int size, Context context) { + public static Bitmap getContactPicture(Conversation conversation, int dpSize, Context context, boolean notification) { if(conversation.getMode() == Conversation.MODE_SINGLE) { if (conversation.getContact() != null){ - return getContactPicture(conversation.getContact(), size, context); + return getContactPicture(conversation.getContact(), dpSize, + context, notification); } else { - return getContactPicture(conversation.getName(false), size); + return getContactPicture(conversation.getName(false), dpSize, + context, notification); } } else{ - return getMucContactPicture(conversation, size); + int fgColor = UIHelper.FG_COLOR, + bgColor = (notification) ? + UIHelper.BG_COLOR : UIHelper.TRANSPARENT; + + return getMucContactPicture(conversation, getRealPx(dpSize, context), + bgColor, fgColor); } } - public static Bitmap getContactPicture(Contact contact, int size, Context context) { + public static Bitmap getContactPicture(Contact contact, int dpSize, Context context, boolean notification) { + int fgColor = UIHelper.FG_COLOR, + bgColor = (notification) ? + UIHelper.BG_COLOR : UIHelper.TRANSPARENT; + String uri = contact.getProfilePhoto(); if (uri==null) { - return getContactPicture(contact.getDisplayName(), size); + return getContactPicture(contact.getDisplayName(), dpSize, + context, notification); } try { - Bitmap bm = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(Uri.parse(uri))); - return Bitmap.createScaledBitmap(bm, size, size, false); + Bitmap bm = BitmapFactory.decodeStream(context.getContentResolver() + .openInputStream(Uri.parse(uri))); + return Bitmap.createScaledBitmap(bm, getRealPx(dpSize, context), + getRealPx(dpSize, context), false); } catch (FileNotFoundException e) { - return getContactPicture(contact.getDisplayName(), size); + return getContactPicture(contact.getDisplayName(), dpSize, + context, notification); } } - public static Bitmap getContactPicture(String name, int size, Context context) { - return getContactPicture(name, size); - } + public static Bitmap getContactPicture(String name, int dpSize, Context context, boolean notification) { + int fgColor = UIHelper.FG_COLOR, + bgColor = (notification) ? + UIHelper.BG_COLOR : UIHelper.TRANSPARENT; - public static Bitmap getContactPicture(String name, int size) { - return getUnknownContactPicture(new String[]{name}, size); + return getUnknownContactPicture(new String[]{name}, getRealPx(dpSize, context), + bgColor, fgColor); } public static Bitmap getErrorPicture(int size) { @@ -367,7 +383,6 @@ public class UIHelper { } String ringtone = preferences.getString("notification_ringtone", null); - Resources res = context.getResources(); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( context); if (unread.size() == 0) { @@ -376,8 +391,8 @@ public class UIHelper { } else if (unread.size() == 1) { Conversation conversation = unread.get(0); targetUuid = conversation.getUuid(); - mBuilder.setLargeIcon(UIHelper.getContactPicture(conversation, (int) res - .getDimension(android.R.dimen.notification_large_icon_width), context)); + mBuilder.setLargeIcon(UIHelper.getContactPicture(conversation, 64, + context, true)); mBuilder.setContentTitle(conversation.getName(useSubject)); if (notify) { mBuilder.setTicker(conversation.getLatestMessage().getBody().trim()); @@ -480,7 +495,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, 72, context, false)); } public static AlertDialog getVerifyFingerprintDialog( @@ -516,20 +531,20 @@ public class UIHelper { return builder.create(); } - public static Bitmap getSelfContactPicture(Account account, int size, boolean showPhoneSelfContactPicture, Activity activity) { + public static Bitmap getSelfContactPicture(Account account, int size, boolean showPhoneSelfContactPicture, Context context) { if (showPhoneSelfContactPicture) { - Uri selfiUri = PhoneHelper.getSefliUri(activity); + Uri selfiUri = PhoneHelper.getSefliUri(context); if (selfiUri != null) { try { - return BitmapFactory.decodeStream(activity + return BitmapFactory.decodeStream(context .getContentResolver().openInputStream(selfiUri)); } catch (FileNotFoundException e) { - return getContactPicture(account.getJid(), size); + return getContactPicture(account.getJid(), size, context, false); } } - return getContactPicture(account.getJid(), size); + return getContactPicture(account.getJid(), size, context, false); } else { - return getContactPicture(account.getJid(), size); + return getContactPicture(account.getJid(), size, context, false); } } }