Use address object as contact picture instead of email address only.

Fixes assignment problems for emails sent by some issue tracking
systems, which send out mails with a fixed mail address on behalf of
different people.
This commit is contained in:
Danny Baumann 2013-09-27 08:53:14 +02:00
parent f964cf40a4
commit 8777441d92
2 changed files with 22 additions and 16 deletions

View File

@ -51,7 +51,7 @@ public class ContactPictureLoader {
/** /**
* LRU cache of contact pictures. * LRU cache of contact pictures.
*/ */
private final LruCache<String, Bitmap> mBitmapCache; private final LruCache<Address, Bitmap> mBitmapCache;
/** /**
* @see <a href="http://developer.android.com/design/style/color.html">Color palette used</a> * @see <a href="http://developer.android.com/design/style/color.html">Color palette used</a>
@ -96,10 +96,10 @@ public class ContactPictureLoader {
// Use 1/16th of the available memory for this memory cache. // Use 1/16th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 16; final int cacheSize = 1024 * 1024 * memClass / 16;
mBitmapCache = new LruCache<String, Bitmap>(cacheSize) { mBitmapCache = new LruCache<Address, Bitmap>(cacheSize) {
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override @Override
protected int sizeOf(String key, Bitmap bitmap) { protected int sizeOf(Address key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number of items. // The cache size will be measured in bytes rather than number of items.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
return bitmap.getByteCount(); return bitmap.getByteCount();
@ -130,12 +130,11 @@ public class ContactPictureLoader {
* @see #calculateFallbackBitmap(Address) * @see #calculateFallbackBitmap(Address)
*/ */
public void loadContactPicture(Address address, QuickContactBadge badge) { public void loadContactPicture(Address address, QuickContactBadge badge) {
String email = address.getPersonal() != null ? address.getPersonal() : address.getAddress(); Bitmap bitmap = getBitmapFromCache(address);
Bitmap bitmap = getBitmapFromCache(email);
if (bitmap != null) { if (bitmap != null) {
// The picture was found in the bitmap cache // The picture was found in the bitmap cache
badge.setImageBitmap(bitmap); badge.setImageBitmap(bitmap);
} else if (cancelPotentialWork(email, badge)) { } else if (cancelPotentialWork(address, badge)) {
// Query the contacts database in a background thread and try to load the contact // Query the contacts database in a background thread and try to load the contact
// picture, if there is one. // picture, if there is one.
ContactPictureRetrievalTask task = new ContactPictureRetrievalTask(badge, address); ContactPictureRetrievalTask task = new ContactPictureRetrievalTask(badge, address);
@ -202,13 +201,13 @@ public class ContactPictureLoader {
return result; return result;
} }
private void addBitmapToCache(String key, Bitmap bitmap) { private void addBitmapToCache(Address key, Bitmap bitmap) {
if (getBitmapFromCache(key) == null) { if (getBitmapFromCache(key) == null) {
mBitmapCache.put(key, bitmap); mBitmapCache.put(key, bitmap);
} }
} }
private Bitmap getBitmapFromCache(String key) { private Bitmap getBitmapFromCache(Address key) {
return mBitmapCache.get(key); return mBitmapCache.get(key);
} }
@ -216,7 +215,7 @@ public class ContactPictureLoader {
* Checks if a {@code ContactPictureRetrievalTask} was already created to load the contact * Checks if a {@code ContactPictureRetrievalTask} was already created to load the contact
* picture for the supplied email address. * picture for the supplied email address.
* *
* @param email * @param address
* The email address to check the contacts database for. * The email address to check the contacts database for.
* @param badge * @param badge
* The {@code QuickContactBadge} instance that will receive the picture. * The {@code QuickContactBadge} instance that will receive the picture.
@ -225,12 +224,11 @@ public class ContactPictureLoader {
* {@code false}, if another {@link ContactPictureRetrievalTask} was already scheduled * {@code false}, if another {@link ContactPictureRetrievalTask} was already scheduled
* to load that contact picture. * to load that contact picture.
*/ */
private boolean cancelPotentialWork(String email, QuickContactBadge badge) { private boolean cancelPotentialWork(Address address, QuickContactBadge badge) {
final ContactPictureRetrievalTask task = getContactPictureRetrievalTask(badge); final ContactPictureRetrievalTask task = getContactPictureRetrievalTask(badge);
if (task != null && email != null) { if (task != null && address != null) {
String emailFromTask = task.getAddress().getAddress(); if (!address.equals(task.getAddress())) {
if (!email.equals(emailFromTask)) {
// Cancel previous task // Cancel previous task
task.cancel(true); task.cancel(true);
} else { } else {
@ -314,7 +312,7 @@ public class ContactPictureLoader {
} }
// Save the picture of the contact with that email address in the bitmap cache // Save the picture of the contact with that email address in the bitmap cache
addBitmapToCache(email, bitmap); addBitmapToCache(mAddress, bitmap);
return bitmap; return bitmap;
} }

View File

@ -167,14 +167,22 @@ public class Address {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o instanceof Address) { if (o instanceof Address) {
return getAddress().equals(((Address) o).getAddress()); Address other = (Address) o;
if (mPersonal != null && other.mPersonal != null && !mPersonal.equals(other.mPersonal)) {
return false;
}
return mAddress.equals(other.mAddress);
} }
return super.equals(o); return super.equals(o);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return getAddress().hashCode(); int hash = mAddress.hashCode();
if (mPersonal != null) {
hash += 3 * mPersonal.hashCode();
}
return hash;
} }
@Override @Override