Fix contact lookup from number (old + new).

This commit is contained in:
Guillaume Beraudo 2011-10-05 10:22:23 +02:00
parent 99807340f5
commit 534af8c62c
2 changed files with 21 additions and 6 deletions

View file

@ -31,6 +31,7 @@ import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
@ -266,9 +267,23 @@ public class ContactPickerActivityNew extends AbstractContactPickerActivity {
}
// Finally using phone number
String normalizedNumber = PhoneNumberUtils.getStrippedReversed(username);
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(username));
projection = new String[]{PhoneLookup._ID};
projection = new String[]{PhoneLookup._ID, PhoneLookup.NUMBER};
c = resolver.query(lookupUri, projection, null, null, null);
return retrievePhotoUri(resolver, c, PhoneLookup._ID);
while (c.moveToNext()) {
long id = c.getLong(c.getColumnIndex(PhoneLookup._ID));
String enteredNumber = c.getString(c.getColumnIndex(PhoneLookup.NUMBER));
if (!normalizedNumber.equals(PhoneNumberUtils.getStrippedReversed(enteredNumber))) {
continue;
}
Uri picture = retrievePhotoUri(resolver, id);
if (picture != null) {
c.close();
return picture;
}
}
c.close();
return null;
}
}

View file

@ -28,6 +28,7 @@ import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.Contacts.Photos;
import android.telephony.PhoneNumberUtils;
@SuppressWarnings("deprecation")
public class ContactPickerActivityOld extends Activity {
@ -118,11 +119,10 @@ public class ContactPickerActivityOld extends Activity {
}
public static Uri findUriPictureOfContact(ContentResolver resolver, String username, String domain) {
// A direct qery on the number column doesn't work as the number is stored
// with hyphens inside it.
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode("0952636505"));
String normalizedNumber = PhoneNumberUtils.getStrippedReversed(username);
String[] projection = {Contacts.Phones.PERSON_ID};
Cursor c = resolver.query(contactUri, projection, null, null, null);
String selection = Contacts.Phones.NUMBER_KEY + "=" + normalizedNumber;
Cursor c = resolver.query(Contacts.Phones.CONTENT_URI, projection, selection, null, null);
return retrievePhotoUriAndCloseC(resolver, c, Contacts.Phones.PERSON_ID);
}