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

View File

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