Search code examples
androidandroid-contactscontactscontract

Retriving SIP adress from contact


I need to get this field:

enter image description here

But I am struggling with my code:

private void getContactList() {

    ContentResolver cr = getContentResolver();
    Cursor  cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);


    if ((cur != null ? cur.getCount() : 0) > 0) {
        while (cur != null && cur.moveToNext()) {

            Contact contact = new Contact();

            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String photoURI = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_URI));
            String photoThumbURI = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));


            //Phones
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);


            while (pCur.moveToNext()) {

                String phoneNum = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

            }

            ///Emails
            Cursor pCur2 = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);

            while (pCur2.moveToNext())
            {

                String em = pCur2.getString(pCur2.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.d("DTAG","em: "+em);

            }


            ///Sip
            Cursor pCur3 = cr.query(/*What goes here?*/,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id}, null);

            while (pCur3.moveToNext())
            {
                String em = pCur3.getString(pCur3.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.DATA));
            }

            contact.setId(id);
            contact.setName(name);
            contact.setImageURI(photoURI);
            contact.setThumbnailURI(photoThumbURI);

            pCur.close();

        }
    }
    if (cur != null) {
        cur.close();
    }

}

How to address the SIP query to find this number?


Solution

  • All the data from Phone/Email/etc CONTENT_URIs is contained in a single big table called Data. Querying on that table allows for implicit join with the Contacts table, so you can get the CONTACT_ID, DISPLAY_NAME, etc. as well.

    You're currently doing way too many queries just to get all the phones/email/sips from the Data table, you can do it all in one simple query, and use selection to specify which MIMETYPEs you're interested in.

    Here's sample code, consider switching the contacts Map from a list of strings to some custom POJO object:

    Map<Long, List<String>> contacts = new HashMap<Long, List<String>>();
    
    String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.PHOTO_URI, Data.PHOTO_THUMBNAIL_URI};
    
    // query only emails/phones/sip
    String selection = Data.MIMETYPE + " IN ('" + 
        Phone.CONTENT_ITEM_TYPE + "', '" + 
        Email.CONTENT_ITEM_TYPE + "', '" + 
        SipAddress.CONTENT_ITEM_TYPE + "')";
    
    Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
    
    while (cur != null && cur.moveToNext()) {
        long id = cur.getLong(0);
        String name = cur.getString(1); // full name
        String mime = cur.getString(2); // type of data (phone / email / sip)
        String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
        String photo = cur.getString(4);
        String thumb = cur.getString(5);
    
        String kind = "unknown";
    
        switch (mime) {
            case Phone.CONTENT_ITEM_TYPE: 
                kind = "phone"; 
                break;
            case Email.CONTENT_ITEM_TYPE: 
                Event = "email";
                break;
            case SipAddress.CONTENT_ITEM_TYPE: 
                Event = "sip";
                break;
        }
        Log.d(TAG, "got " + id + ", " + name + ", " + kind + " - " + data);
    
        // add info to existing list if this contact-id was already found, or create a new list in case it's new
        List<String> infos;
        if (contacts.containsKey(id)) {
            infos = contacts.get(id);
        } else {
            // create a new contact object
            infos = new ArrayList<String>();
            infos.add("name = " + name);
            infos.add("photo = " + photo);
            infos.add("thumb = " + thumb);
            contacts.put(id, infos);
        }
        infos.add(kind + " = " + data);
    }