I try to implement a live search over the users contacts, and I want to get the name, thumbnail and address (if there is one) of each matching contact.
The live search is running while the user is typing.
So he types ma and will get 'martin', 'matthews'...
He'll continue with mat and will only see 'matthews'
I try to achieve this with a single query like the following, but I always get the contact number in the FORMATTED_ADRESS field. I guess I have a JOIN problem, because I'm using ContactsContract.CommonDataKinds and ContactsContract.Contacts in the same query?
public static List<ContactModel> getContactsForQuery(Context context, String query) {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS
};
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String selection = ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%" + query + "%'";
Cursor cursor = context.getContentResolver().query(uri, projection, selection, null,null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(0);
String thumbail = cursor.getString(1);
String formattedADress = cursor.getString(2);
}
while (cursor.moveToNext());
}
I actually solved my issue, with
querying for Contacts._ID, Contacts.DISPLAY_NAME
start a second query with the Contacts._ID like the following
Cursor detailCursor = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{
CommonDataKinds.StructuredPostal.STREET,
CommonDataKinds.StructuredPostal.CITY,
CommonDataKinds.StructuredPostal.POSTCODE
},
ContactsContract.Data.CONTACT_ID + "=? AND "
+ CommonDataKinds.StructuredPostal.MIMETYPE + "=?",
new String[]{
String.valueOf(contactID),
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
},
null);
but this will start a second query for every contact, which might not be the best approach.
So my final question is: is it possible to get this work with the first query?
Mmmh, very sad, that no one was able to answer my question and grab the bounty points ;-(
For the record, here's my working example. It solves the issue but I still think it's producing a big overload. On every user entry (afterTextchange) I call the getContactsDetailsQuery which first gets all users with their ID containing the query in their name (cursor) and afterwards I start another query (detailCursor) for every user to get the adress. To prevent the overload, i added an limit..
public static List<SearchModel> getContactDetailsForQuery(Context context, String query, int limit) {
final int CONTACT_ID_INDEX = 0;
final int CONTACT_NAME_INDEX = 1;
final int CONTACT_THUMBNAIL_INDEX = 2;
//my custom model to hold my results
List<SearchModel> results = new ArrayList<SearchModel>();
final String[] selectUser = new String[]{
Contacts._ID,
Contacts.DISPLAY_NAME,
Contacts.PHOTO_THUMBNAIL_URI};
String selection = Contacts.DISPLAY_NAME + " LIKE ?";
String[] selectionArgs = new String[]{"%" + query + "%"};
String sortOrder = Contacts.DISPLAY_NAME + " ASC";
Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, selectUser, selection, selectionArgs, sortOrder, null);
int contactCounter = 0;
if (cursor != null && cursor.moveToFirst()) {
do {
String contactID = cursor.getString(CONTACT_ID_INDEX);
String displayName = cursor.getString(CONTACT_NAME_INDEX);
String thumbnail = cursor.getString(CONTACT_THUMBNAIL_INDEX);
//get user details with user id (this is the query i wanted to change in my question!!)
Cursor detailCursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS},
ContactsContract.Data.CONTACT_ID + "=? AND " +
CommonDataKinds.StructuredPostal.MIMETYPE + "=?",
new String[]{String.valueOf(contactID), CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE},
null);
if (detailCursor != null && detailCursor.moveToFirst()) {
//special case: user has several address, query all of them
do {
String formattedAddress = detailCursor.getString(detailCursor.getColumnIndex(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
//user has serveral adress -> init model for each adress
SearchModel contact = new SearchModel();
results.add(contact);
contactCounter++;
} while (detailCursor.moveToNext() && contactCounter < limit);
} else {
//user has no adress -> init model
SearchModel contact = new SearchModel();
results.add(contact);
contactCounter++;
}
detailCursor.close();
} while (cursor.moveToNext() && contactCounter < limit);
}
cursor.close();
return results;
}
Related
I want to fetch id,name,birthday,addess from android contacts. I changed the uri (ContactsContract.CommonDataKinds, ContactsContract.Contacts etc.) and was able to fetch the address, but then the birthday disappeared. Anyway I tried, I can not find a solution to fetch both at once.
public void getBirthdays() {
// get data from contacts
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS};
String selection =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[]{ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
int columnCount = cursor.getColumnCount();
cursor.moveToFirst();
for (int i = 0; i < columnCount; i++) {
Common.echo(cursor.getColumnName(i) + ", " + cursor.getString(i));
}
cursor.close();
}
the output of the above code is:
I: contact_id, 14046
I: display_name, Magret XXXX
I: data1, XXXX-05-31
I: data1, XXXX-05-31
The "data1"-field is listed twice, because START_DATE (birthday) and FORMATTED_ADDRESS are both stored in same virtual column "data1".
Is there a way to define a virtual column-names in a resolver like the SQL-equivalent "SELECT col1 AS name1, col2 AS name2, col3,col4 WHERE..."?
two issues: you're limiting your query to EVENTs only & you're only logging the first row found in the query.
Try this:
private class Contact {
public Long contactId;
public String name;
public String birthday;
public String address;
}
public void getBirthdaysAndAddress() {
// mapping from contactId to a contact object
Map<Long, Contact> contacts = new HashMap<>();
Uri uri = Data.CONTENT_URI;
String[] projection = new String[]{
Data.CONTACT_ID,
Data.DISPLAY_NAME,
Data.MIMETYPE,
Data.DATA1,
Event.TYPE};
// get ALL rows that represent either a birthday or a postal address
String selection = Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?";
String[] selectionArgs = new String[]{Event.CONTENT_ITEM_TYPE, StructuredPostal.CONTENT_ITEM_TYPE};
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
while (cursor.moveToNext()) {
Long id = cursor.getLong(0);
// get an existing contact object, or create a new one
Contact contact = contacts.get(id);
if (contact == null) {
contact = new Contact();
contact.contactId = id;
contact.name = cursor.getString(1);
contacts.put(id, contact);
}
String mimetype = cursor.getString(2);
if (mimetype.equals(Event.CONTENT_ITEM_TYPE)) {
// this is an event row, check that it is a birthday
int type = cursor.getInt(4);
if (type == Event.TYPE_BIRTHDAY) {
contact.birthday = cursor.getString(3);
}
} else {
// this is an address row
contact.address = cursor.getString(3);
}
}
cursor.close();
// print all contact objects containing a birthday
Iterator it = mp.values().iterator();
while (it.hasNext()) {
Contact contact = (Contact) it.next();
if (!TextUtils.isEmpty(contact.birthday)) {
Log.i("Birthday contacts", contact.contactId + " - " + contact.name + " - " + contact.birthday + " - " + contact.address);
}
}
}
I'm iterating through Contacts table and each iteration do iteration on Phone table so it takes appx. 4 seconds to do contact list for 243 contacts (without iterating phone numbers it loads instantly). I'm sure there is a way to make it faster.
Already made projections to fetch only needed columns, but haven't improved that much. Maybe I should improve SQLite query or iterate in some other manner:
Contact realmContact = new Contact();
Uri uri = Contacts.CONTENT_URI;
String[] projection = {
Contacts.LOOKUP_KEY,
Contacts.DISPLAY_NAME_PRIMARY,
Contacts.LAST_TIME_CONTACTED,
Contacts.HAS_PHONE_NUMBER
};
String selection = "((" + CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " != '' ))";
Cursor phones = getActivity()
.getContentResolver()
.query(uri, projection, selection, null, null);
while (phones.moveToNext()) {
String id = phones.getString(phones.getColumnIndex(Contacts.LOOKUP_KEY));
String name = phones.getString(phones.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY));
String lastTimeContacted = phones.getString(phones.getColumnIndex(Contacts.LAST_TIME_CONTACTED));
long data = Long.parseLong(lastTimeContacted);
String date = new SimpleDateFormat("dd/MM/yyyy | HH:mm").format(new Date(data));
if (Integer.parseInt(phones.getString(phones.getColumnIndex(Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getActivity().getContentResolver().query(
CommonDataKinds.Phone.CONTENT_URI,
new String[]{CommonDataKinds.Phone.NUMBER},
CommonDataKinds.Phone.LOOKUP_KEY + " = ?",
new String[]{id}, null);
int iterationCounter = 0;
String phoneNumber = "";
while (pCur.moveToNext()) {
phoneNumber += iterationCounter == 0 ?
pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER))
: "," + pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
iterationCounter += 1;
}
realmContact.setNumber(phoneNumber);
Log.i("asd-number", phoneNumber);
pCur.close();
}
realmContact.setId(id);
realmContact.setName(name);
realmContact.setLastTimeContacted(
getActivity().getResources()
.getString(R.string.contacts_list_fragment_last_call)
+ date);
realmContact.setIsBeingSaved(true);
_realm.insertOrUpdate(realmContact);
As #pskink mentioned in the comments, you need to query over the Phone table.
To get the list of phones by contact, you can maintain a HashMap from contactId to List of phones.
Here's a little snippet that might help (you'd probably want to tweak the collection a bit to store the display-name only once per contact):
HashMap<Long, ArrayList<String>> phones = new HashMap<Long, ArrayList<String>>();
String[] projection = new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor c = cr.query(Phone.CONTENT_URI, projection, null, null, null);
while (c != null && c.moveToNext()) {
long contactId = c.getLong(0);
ArrayList<String> list = phones.get(contactId);
if (list == null) {
ArrayList<String> list = new ArrayList<String>();
phones.put(contactId, list);
}
list.add(c.getString(1) + " " + c.getString(2));
}
i'm doing some tests to figure out how the provider works with events etc.
I'm trying to get all contacts of specific accounts, then read details of each contact to get the birthday if present.
I firstly tried to do it in 1 go but I didn't have enough knowledge of the provider so I'm trying to do it in 2 steps for know just to see if I can get to it.
I get Invalid column on this line:
Cursor cursor = cr.query(uri, projection, [...]
The contact_id is the problem here. How can I improve the second query to get the data I want? I tried various mods on the code getting nowhere.
Thanks.
The code I use:
private void readContacts(){
Cursor contByAccCursor = getContactsByAccounts();
getContactsBirthdays(contByAccCursor);
}
private Cursor getContactsByAccounts() {
Uri uri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.RawContacts.CONTACT_ID,
ContactsContract.RawContacts.ACCOUNT_TYPE
};
String where = ContactsContract.RawContacts.ACCOUNT_TYPE + " IN ('com.google', 'vnd.sec.contact.phone', 'com.skype.contacts.sync')";
return activity.getContentResolver().query(uri,
projection,
where,
null,
null);
}
private void getContactsBirthdays(Cursor filteredContacts) {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
//ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE
};
String where =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + " AND " +
ContactsContract.CommonDataKinds.Event.CONTACT_ID + "= ?";
String[] selectionArgs;
String sortOrder = null;
StringBuffer sb = new StringBuffer();
ContentResolver cr = activity.getContentResolver();
int accTypeCol = filteredContacts.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE);
while(filteredContacts.moveToNext()){
selectionArgs = new String[] {
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
filteredContacts.getString(filteredContacts.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID))
};
Cursor cursor = cr.query(uri,
projection,
where,
selectionArgs,
sortOrder);
if(cursor.moveToNext()){
String dName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
String bDay = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
String accType = filteredContacts.getString(accTypeCol);
sb.append(dName + "("+accType+")" + " - " + bDay);
sb.append("\n");
}
}
Log.d(TAG, sb.toString());
}
Ok I feel so stupid now. The error is the second content_uri, I was getting the wrong one, must be tired...
This is the correct uri to get the right data:
private void getContactsBirthdays(Cursor filteredContacts) {
Uri uri = ContactsContract.Data.CONTENT_URI;
For a given number from my address book, I need to look-up if the number has whatsapp enabled.
(The idea is to choose SMS/WhatsApp for initiating a text intent)
Lets say, I have two numbers under a contact, And I need to know which one has whatsapp enabled.
The "People" app on the Nexus 4 shows both contact numbers,
And also a little below has a CONNECTIONS section, which shows only the WhatsApp possible contact.
Is there a way to look up(like how People app does) ?
If you want to know if this contact has WhatsApp:
String[] projection = new String[] { RawContacts._ID };
String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
String[] selectionArgs = new String[] { "THE_CONTACT_DEVICE_ID", "com.whatsapp" };
Cursor cursor = activity.getContentResolver().query(RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
boolean hasWhatsApp = cursor.moveToNext();
if (hasWhatsApp){
String rowContactId = cursor.getString(0);
}
And to find to which number of this contact has WhatsApp
projection = new String[] { ContactsContract.Data.DATA3 };
selection = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? ";
selectionArgs = new String[] { "vnd.android.cursor.item/vnd.com.whatsapp.profile", rawContactId };
cursor = CallAppApplication.get().getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, "1 LIMIT 1");
String phoneNumber = null;
if (cursor.moveToNext()) {
phoneNumber = cursor.getString(0);
}
Using #idog's method, I improved code to work easier. contactID is a string variable to be passed. If contact hasn't WhatsApp returns null, otherwise returns with contactID which has been passed as variable.
public String hasWhatsapp(String contactID) {
String rowContactId = null;
boolean hasWhatsApp;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
String[] selectionArgs = new String[]{contactID, "com.whatsapp"};
Cursor cursor = getActivity().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
if (cursor != null) {
hasWhatsApp = cursor.moveToNext();
if (hasWhatsApp) {
rowContactId = cursor.getString(0);
}
cursor.close();
}
return rowContactId;
}
public int hasWhatsApp(String contactID) {
int whatsAppExists = 0;
boolean hasWhatsApp;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)";
String[] selectionArgs = new String[]{contactID, "com.whatsapp"};
Cursor cursor = getActivity().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
if (cursor != null) {
hasWhatsApp = cursor.moveToNext();
if (hasWhatsApp) {
whatsAppExists = 1;
}
cursor.close();
}
return whatsAppExists;
}
Friends I want Contacts which have email and also sort in ascending order..
any one know how to get this list and sort..
Please help me and thanks in advance.
I am using this code.
MatrixCursor matCur = new MatrixCursor(new String[] { Contacts._ID,
Contacts.DISPLAY_NAME, "photo_id", "starred" });
Cursor cEmail = WP7Main.this.managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cEmail.moveToFirst();
if (cEmail.moveToFirst())
{
// String name =
// cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
String contactId = cEmail.getString(cEmail.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emails = WP7Main.this.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + contactId, null, null);
String emailAddress = "";
while (emails.moveToNext())
{
// This would allow you get several email addresses
if (emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)) != null)
{
String[] columnValues = {
cEmail.getString(cEmail
.getColumnIndex("_id")),
cEmail.getString(cEmail
.getColumnIndex("display_name")),
cEmail.getString(cEmail
.getColumnIndex("photo_id")),
cEmail.getString(cEmail
.getColumnIndex("starred")) };
matCur.addRow(columnValues);
}
}
emails.close();
}
Try this:
/**
* #return A managed cursor of email contacts for the given activity.
*/
public static Cursor buildFilteredEmailCursor(Activity activity) {
final String my_sort_order = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
String my_selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] eproj = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA};
Uri uri = android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_URI;
return activity.managedQuery(uri, eproj, my_selection, null, my_sort_order);
}
Use this query :
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[]{Data.CONTACT_ID, Data.DISPLAY_NAME, Email.ADDRESS},
Data.MIMETYPE + "=?", new String[] {Email.CONTENT_TYPE}, Data.DISPLAY_NAME /* use Email.ADDRESS if you want to sort it using that*/);