Question

I need to find out if a particular contact is a member of a given group. I have been working with the code below and have proved that it works using a handset running Gingerbread. However I have been trying to run it on my HTC One S running Android version 4.0.3 and it is failing. Has the API spec changed? My code is below.

public boolean checkGroupMembership(String groupID, String contactID, Context ctx) {
            ContentResolver groupContentResolver = ctx.getContentResolver();
                        String select = "contact_id=" + contactID + 
                    " AND " + GroupMembership.GROUP_ROW_ID + " = " + groupID +
                    " AND mimetype='vnd.android.cursor.item/group_membership'";

            Cursor contactGroupCursor =  
                    groupContentResolver.query(
                        Data.CONTENT_URI, 
                        new String[] { GroupMembership.CONTACT_ID,  GroupMembership.GROUP_ROW_ID, GroupMembership.IN_VISIBLE_GROUP }, 
                        select,
                        null, 
                        null);
            int records = contactGroupCursor.getCount();
            try {
            while(contactGroupCursor.moveToNext()){
                String ContactID = contactGroupCursor.getString(contactGroupCursor.getColumnIndexOrThrow(GroupMembership.CONTACT_ID));
                String groupRowId = contactGroupCursor.getString(contactGroupCursor.getColumnIndexOrThrow(GroupMembership.GROUP_ROW_ID));
                String InVisiblegroup = contactGroupCursor.getString(contactGroupCursor.getColumnIndexOrThrow(GroupMembership.IN_VISIBLE_GROUP));

                Log.d("DEBUG", "groupSourceId in checkGroupMembership: " + groupRowId + "  InVisiblegroup = " + InVisiblegroup);
                Log.d("DEBUG", "ContactID in checkGroupMembership: " + ContactID);          

                }       
            } 
            finally 
            {
                contactGroupCursor.close();
            }

            // see if the contact is in this group
            if (records > 0) {
                return true;
            }
            else {


            return false;
            }
        }

    }

In addition changing the query to purely find out what groups the contact is a member of:

String select = "contact_id=" + contactID;

results in the following debug output. In some cases groupRowID looks like a group ID, in others it is the contact number or the name of the contact. Note that I am using my home landline to call test and this is appearing. Also - I only have 10 groups defined on the handset and an id of 11, 12 and 13 is printed in the output... It could be that I'm missing something? I definitely know that the contact_id I'm passing in is a member of the Coworkers group (group_ID 5 on my phone) but this is never returned in ICS. I have independently verified the group membership for HOME using contact manager apps on both handsets.

11-04 11:48:21.980: D/DEBUG(10145): select in checkGroupMembership : contact_id=133 11-04 11:48:21.990: D/DEBUG(10145): count of records in checkGroupMembership: 13 11-04 11:48:22.000: D/DEBUG(10145): groupRowId in checkGroupMembership: null InVisiblegroup = 1 11-04 11:48:22.000: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.000: D/DEBUG(10145): groupRowId in checkGroupMembership: InVisiblegroup = 1 11-04 11:48:22.000: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.000: D/DEBUG(10145): groupRowId in checkGroupMembership: 004-414-83326995 InVisiblegroup = 1 11-04 11:48:22.000: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.000: D/DEBUG(10145): groupRowId in checkGroupMembership: 014-833-26995 InVisiblegroup = 1 11-04 11:48:22.000: D/MYOB(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/MYOB(10145): groupRowId in checkGroupMembership: 01483326995 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 10 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 11 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 12 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 13 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 7 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 8 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: 9 InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133 11-04 11:48:22.030: D/DEBUG(10145): groupRowId in checkGroupMembership: HOME InVisiblegroup = 1 11-04 11:48:22.030: D/DEBUG(10145): ContactID in checkGroupMembership: 133

P.S. please could someone with sufficient privileges please add "GroupMembership" as a tag to the site.

Was it helpful?

Solution 2

The image below shows a dump of the groups from Gingerbread (Cyanogen) and ICS (HTC) produced using exactly the same code. As you can see they are quite different in terms of how the data is presented. For some reason the groups are duplicated to some extent in ICS. So the question is how do I manage this across different versions if I'm planning to use the group _ID to perform lookups. Note that in the contacts app on ICS each group appears only once so how do I know which ID this refers to?

OTHER TIPS

Nothing's changed in group membership between Honeycomb and 4.0.3.

It would be easier to read all this if you used LogCat or even did a ListView and screenshot or something. It's hard to understand what your problem is. You can do a dump of ContactsContract.Groups to see what groups are defined for the database. Unless you're absolutely certain that both handsets have exactly the same data, you're gonna find differences. The only way to ensure they have exactly the same data is to wipe the contacts data off them and sync them both to a small test dataset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top