Search code examples
androidcontacts

How to identify a local phone contact and an SYNC phone contact in android?


Can anybody guide me to find the solution for the following problem.

  1. I have to identify whether the phone contact saved in locally or from Email ?(programatically)

i have read fromgoogle doc that ContactsContract.Groups, which contains information about raw contact groups such as Gmail contact groups. The current API does not support the notion of groups spanning multiple accounts.

Based on that i have tried the following code.

 StringBuffer output = new StringBuffer();
   final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, 
            ContactsContract.Groups.TITLE,
            ContactsContract.Groups.SUMMARY_WITH_PHONES
            };

Cursor c =  getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
        null, ContactsContract.Groups.TITLE);

  int IDX_ID = c.getColumnIndex(ContactsContract.Groups._ID);
  int IDX_TITLE = c.getColumnIndex(ContactsContract.Groups.TITLE);
  output.append("title"+IDX_TITLE+"\n");
    Map<String,GroupInfo> m = new HashMap<String, GroupInfo>();

while (c.moveToNext()) {
    output.append("test...\n");
    GroupInfo g = new GroupInfo();
    g.id = c.getString(IDX_ID);
    g.title = c.getString(IDX_TITLE);
    output.append("title"+c.getString(IDX_TITLE)+"\n");
    int users = c.getInt(c.getColumnIndex(ContactsContract.Groups.SUMMARY_WITH_PHONES));
    if (users>0) {
        // group with duplicate name?
        GroupInfo g2 = m.get(g.title);
        if (g2==null) {
            m.put(g.title, g);
            output.append("title"+g.title+"\n");
            groups.add(g);
        } else {
            g2.id+=","+g.id;
        }
    }
}
outputText.setText(output);
c.close();

but no hope.


Solution

  • I am posting this answer for future use. We can differentiate the local phone contacts and the sync contacts by using the field called RawContacts.SOURCE_ID

    It is described here

    SOURCE_ID
    read/write
    String that uniquely identifies this row to its source account. Typically it is set at the time the raw contact is inserted and never changed afterwards. The one notable exception is a new raw contact: it will have an account name and type (and possibly a data set), but no source id. This indicates to the sync adapter that a new contact needs to be created server-side and its ID stored in the corresponding SOURCE_ID field on the phone.

    The sample code is follows, it gives the id for sync contacts and null for others.

    private void testContact() {
            StringBuffer output = new StringBuffer();
            ContentResolver resolver = getContentResolver();
            Cursor contacts = resolver.query(Contacts.CONTENT_URI, null,
                    Contacts.HAS_PHONE_NUMBER + " != 0", null, Contacts._ID
                            + " ASC");
            Cursor data = resolver.query(Data.CONTENT_URI, null, Data.MIMETYPE
                    + "=? OR " + Data.MIMETYPE + "=?", new String[]{
                    Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
                    Data.CONTACT_ID + " ASC");
    
            int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID);
            int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME);
            int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID);
            int data1Index = data.getColumnIndexOrThrow(Data.DATA1);
            boolean hasData = data.moveToNext();
    
            while (contacts.moveToNext()) {
                long id = contacts.getLong(idIndex);
                Uri rawContactUri = 
                          ContentUris.withAppendedId(RawContacts.CONTENT_URI, id);
    
                        Uri entityUri =
                          Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
    
                        Cursor c =
                          getContentResolver().query(
                            entityUri,
                            new String[] {
                                      RawContacts.ACCOUNT_NAME,
                              RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
                            null, null, null);
    
                        try {
                             while (c.moveToNext()) {
                                 String sourceId = c.getString(0);
                                 if (!c.isNull(1)) {
    
                                     String source_id = c.getString(1);
                                 try {
                                         output.append(c.getString(4)+sourceId+" "+source_id+"\n");
    
                                         //output.append(datas+ "Sync1 "+  c.getString(4)+" Sync2 "+  c.getString(5)+" Sync3"+  c.getString(6)+" Sync4 "+  c.getString(7)+"\n");
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                     //decide here based on mimeType, see comment later
                                 }
                             }
                        } finally {
                             c.close();
                        }
    
            }
    
            outputText.setText(output);
        }