1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-02-23 22:23:22 -05:00

ContactPictureLoader code cleanup

This commit is contained in:
cketti 2013-10-03 21:20:06 +02:00
parent bea6ba6881
commit 1aca9eb22c

@ -27,6 +27,7 @@ import android.os.Build;
import android.support.v4.util.LruCache; import android.support.v4.util.LruCache;
import android.widget.QuickContactBadge; import android.widget.QuickContactBadge;
import com.fsck.k9.helper.Contacts; import com.fsck.k9.helper.Contacts;
import com.fsck.k9.helper.StringUtils;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
public class ContactPictureLoader { public class ContactPictureLoader {
@ -40,6 +41,11 @@ public class ContactPictureLoader {
*/ */
private static final Pattern EXTRACT_LETTER_PATTERN = Pattern.compile("[a-zA-Z]"); private static final Pattern EXTRACT_LETTER_PATTERN = Pattern.compile("[a-zA-Z]");
/**
* Letter to use when {@link #EXTRACT_LETTER_PATTERN} couldn't find a match.
*/
private static final String FALLBACK_CONTACT_LETTER = "?";
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private Resources mResources; private Resources mResources;
@ -160,16 +166,18 @@ public class ContactPictureLoader {
return rgb; return rgb;
} }
private char calcUnknownContactLetter(Address address) { private String calcUnknownContactLetter(Address address) {
String letter = ""; String letter = null;
String str = address.getPersonal() != null ? address.getPersonal() : address.getAddress(); String personal = address.getPersonal();
String str = (personal != null) ? personal : address.getAddress();
Matcher m = EXTRACT_LETTER_PATTERN.matcher(str); Matcher m = EXTRACT_LETTER_PATTERN.matcher(str);
if (m.find()) { if (m.find()) {
letter = m.group(0).toUpperCase(Locale.US); letter = m.group(0).toUpperCase(Locale.US);
} }
return letter.length() == 0 ? '?' : letter.charAt(0); return (StringUtils.isNullOrEmpty(letter)) ?
FALLBACK_CONTACT_LETTER : letter.substring(0, 1);
} }
/** /**
@ -184,7 +192,7 @@ public class ContactPictureLoader {
int rgb = calcUnknownContactColor(address); int rgb = calcUnknownContactColor(address);
result.eraseColor(rgb); result.eraseColor(rgb);
String letter = Character.toString(calcUnknownContactLetter(address)); String letter = calcUnknownContactLetter(address);
Paint paint = new Paint(); Paint paint = new Paint();
paint.setAntiAlias(true); paint.setAntiAlias(true);
@ -195,8 +203,8 @@ public class ContactPictureLoader {
paint.getTextBounds(letter, 0, 1, rect); paint.getTextBounds(letter, 0, 1, rect);
float width = paint.measureText(letter); float width = paint.measureText(letter);
canvas.drawText(letter, canvas.drawText(letter,
mPictureSizeInPx/2f-width/2f, (mPictureSizeInPx / 2f) - (width / 2f),
mPictureSizeInPx/2f+rect.height()/2f, paint); (mPictureSizeInPx / 2f) + (rect.height() / 2f), paint);
return result; return result;
} }
@ -260,7 +268,7 @@ public class ContactPictureLoader {
*/ */
class ContactPictureRetrievalTask extends AsyncTask<Void, Void, Bitmap> { class ContactPictureRetrievalTask extends AsyncTask<Void, Void, Bitmap> {
private final WeakReference<QuickContactBadge> mQuickContactBadgeReference; private final WeakReference<QuickContactBadge> mQuickContactBadgeReference;
private Address mAddress; private final Address mAddress;
ContactPictureRetrievalTask(QuickContactBadge badge, Address address) { ContactPictureRetrievalTask(QuickContactBadge badge, Address address) {
mQuickContactBadgeReference = new WeakReference<QuickContactBadge>(badge); mQuickContactBadgeReference = new WeakReference<QuickContactBadge>(badge);
@ -283,11 +291,11 @@ public class ContactPictureLoader {
@Override @Override
protected Bitmap doInBackground(Void... args) { protected Bitmap doInBackground(Void... args) {
final String email = mAddress.getAddress(); final String email = mAddress.getAddress();
final Uri x = mContactsHelper.getPhotoUri(email); final Uri photoUri = mContactsHelper.getPhotoUri(email);
Bitmap bitmap = null; Bitmap bitmap = null;
if (x != null) { if (photoUri != null) {
try { try {
InputStream stream = mContentResolver.openInputStream(x); InputStream stream = mContentResolver.openInputStream(photoUri);
if (stream != null) { if (stream != null) {
try { try {
Bitmap tempBitmap = BitmapFactory.decodeStream(stream); Bitmap tempBitmap = BitmapFactory.decodeStream(stream);