1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-01-30 23:00:09 -05:00

ContactPictureLoader code cleanup

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

View File

@ -27,6 +27,7 @@ import android.os.Build;
import android.support.v4.util.LruCache;
import android.widget.QuickContactBadge;
import com.fsck.k9.helper.Contacts;
import com.fsck.k9.helper.StringUtils;
import com.fsck.k9.mail.Address;
public class ContactPictureLoader {
@ -40,6 +41,11 @@ public class ContactPictureLoader {
*/
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 Resources mResources;
@ -160,16 +166,18 @@ public class ContactPictureLoader {
return rgb;
}
private char calcUnknownContactLetter(Address address) {
String letter = "";
String str = address.getPersonal() != null ? address.getPersonal() : address.getAddress();
private String calcUnknownContactLetter(Address address) {
String letter = null;
String personal = address.getPersonal();
String str = (personal != null) ? personal : address.getAddress();
Matcher m = EXTRACT_LETTER_PATTERN.matcher(str);
if (m.find()) {
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);
result.eraseColor(rgb);
String letter = Character.toString(calcUnknownContactLetter(address));
String letter = calcUnknownContactLetter(address);
Paint paint = new Paint();
paint.setAntiAlias(true);
@ -195,8 +203,8 @@ public class ContactPictureLoader {
paint.getTextBounds(letter, 0, 1, rect);
float width = paint.measureText(letter);
canvas.drawText(letter,
mPictureSizeInPx/2f-width/2f,
mPictureSizeInPx/2f+rect.height()/2f, paint);
(mPictureSizeInPx / 2f) - (width / 2f),
(mPictureSizeInPx / 2f) + (rect.height() / 2f), paint);
return result;
}
@ -260,7 +268,7 @@ public class ContactPictureLoader {
*/
class ContactPictureRetrievalTask extends AsyncTask<Void, Void, Bitmap> {
private final WeakReference<QuickContactBadge> mQuickContactBadgeReference;
private Address mAddress;
private final Address mAddress;
ContactPictureRetrievalTask(QuickContactBadge badge, Address address) {
mQuickContactBadgeReference = new WeakReference<QuickContactBadge>(badge);
@ -283,11 +291,11 @@ public class ContactPictureLoader {
@Override
protected Bitmap doInBackground(Void... args) {
final String email = mAddress.getAddress();
final Uri x = mContactsHelper.getPhotoUri(email);
final Uri photoUri = mContactsHelper.getPhotoUri(email);
Bitmap bitmap = null;
if (x != null) {
if (photoUri != null) {
try {
InputStream stream = mContentResolver.openInputStream(x);
InputStream stream = mContentResolver.openInputStream(photoUri);
if (stream != null) {
try {
Bitmap tempBitmap = BitmapFactory.decodeStream(stream);