Permalink
Browse files

Optimized getEmailFromContactPicker()

  • Loading branch information...
1 parent 2d13b0f commit 3b5492f5fc6d63f2dfb3c49e5fa739e385a5729f @cketti cketti committed Apr 8, 2012
Showing with 20 additions and 14 deletions.
  1. +20 −14 src/com/fsck/k9/helper/ContactsSdk5.java
View
34 src/com/fsck/k9/helper/ContactsSdk5.java
@@ -195,34 +195,41 @@ public Intent contactPickerIntent() {
@Override
public ContactItem getEmailFromContactPicker(final Intent data) {
Cursor cursor = null;
- Cursor cursor2 = null;
- ContactItem item = new ContactItem();
ArrayList<String> email = new ArrayList<String>();
try {
Uri result = data.getData();
String displayName = null;
- cursor = mContentResolver.query(result, null, null, null, null);
- if (cursor.moveToFirst()) {
- displayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
- }
// Get the contact id from the Uri
String id = result.getLastPathSegment();
- cursor2 = mContentResolver.query(Email.CONTENT_URI,
- null, Email.CONTACT_ID + "=?", new String[] { id },
- null);
- if (cursor2 != null) {
- int emailIdx = cursor2.getColumnIndex(Email.DATA);
+ cursor = mContentResolver.query(Email.CONTENT_URI, PROJECTION,
+ Email.CONTACT_ID + "=?", new String[] { id }, null);
+
+ if (cursor != null) {
+ while (cursor.moveToNext()) {
+ String address = cursor.getString(EMAIL_INDEX);
+ if (address != null) {
+ email.add(address);
+ }
- while (cursor2.moveToNext()) {
- email.add(cursor2.getString(emailIdx));
+ if (displayName == null) {
+ displayName = cursor.getString(NAME_INDEX);
+ }
}
+ // Return 'null' if no email addresses have been found
if (email.size() == 0) {
return null;
}
+
+ // Use the first email address found as display name
+ if (displayName == null) {
+ displayName = email.get(0);
+ }
+
+ ContactItem item = new ContactItem();
item.setDisplayName(displayName);
item.setEmailAddresses(email);
return item;
@@ -231,7 +238,6 @@ public ContactItem getEmailFromContactPicker(final Intent data) {
Log.e(K9.LOG_TAG, "Failed to get email data", e);
} finally {
Utility.closeQuietly(cursor);
- Utility.closeQuietly(cursor2);
}
return null;

0 comments on commit 3b5492f

Please sign in to comment.