1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00

Avoid race condition that can lead to a NullPointerException

This commit is contained in:
cketti 2013-08-21 12:33:27 +02:00
parent f11f0fcc9b
commit ce56475a4f
2 changed files with 15 additions and 10 deletions

View File

@ -129,12 +129,12 @@ public class ContactPictureLoader {
} else if (cancelPotentialWork(email, badge)) {
// Query the contacts database in a background thread and try to load the contact
// picture, if there is one.
ContactPictureRetrievalTask task = new ContactPictureRetrievalTask(badge);
ContactPictureRetrievalTask task = new ContactPictureRetrievalTask(badge, address);
AsyncDrawable asyncDrawable = new AsyncDrawable(mResources,
calculateFallbackBitmap(address), task);
badge.setImageDrawable(asyncDrawable);
try {
task.exec(address.getAddress(), address.getPersonal());
task.exec();
} catch (RejectedExecutionException e) {
// We flooded the thread pool queue... use a fallback picture
badge.setImageBitmap(calculateFallbackBitmap(address));
@ -250,15 +250,16 @@ public class ContactPictureLoader {
/**
* Load a contact picture in a background thread.
*/
class ContactPictureRetrievalTask extends AsyncTask<String, Void, Bitmap> {
class ContactPictureRetrievalTask extends AsyncTask<Void, Void, Bitmap> {
private final WeakReference<QuickContactBadge> mQuickContactBadgeReference;
private Address mAddress;
ContactPictureRetrievalTask(QuickContactBadge badge) {
ContactPictureRetrievalTask(QuickContactBadge badge, Address address) {
mQuickContactBadgeReference = new WeakReference<QuickContactBadge>(badge);
mAddress = new Address(address);
}
public void exec(String... args) {
public void exec(Void... args) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args);
} else {
@ -271,10 +272,8 @@ public class ContactPictureLoader {
}
@Override
protected Bitmap doInBackground(String... args) {
String email = args[0];
String personal = args[1];
mAddress = new Address(email, personal);
protected Bitmap doInBackground(Void... args) {
final String email = mAddress.getAddress();
final Uri x = mContactsHelper.getPhotoUri(email);
Bitmap bitmap = null;
if (x != null) {

View File

@ -47,12 +47,18 @@ public class Address {
String mPersonal;
public Address(Address address) {
mAddress = address.mAddress;
mPersonal = address.mPersonal;
}
public Address(String address, String personal) {
this(address, personal, true);
}
public Address(String address) {
this(address, null);
this(address, null, true);
}
private Address(String address, String personal, boolean parse) {