How to get the defined Contact Groups on an Android? - android

I would like to get a list of Contacts Groups already defined in the system, how can I do that?

You can query the Contacts.Group table for the available groups.
You can use the Contacts.GroupMembership table to query what group a particular contact is a member of. PERSON_ID is the ID of the contact, GROUP_ID matches the ID of the group.
Note that both these APIs have been deprecated in favour of ContactsContract.Groups and ContactsContract.CommonDataKinds.GroupMembership from 2.0 onwards.

Related

Change of ContactsContract.RawContacts _ID after aggregation

According to Google docs, you should use LOOKUP_KEY to fetch a contact, since its ID in the ContactsContract.Contacts table can change:
An opaque value that contains hints on how to find the contact if its
row id changed as a result of a sync or aggregation.
(source)
Is it also possible that a RawContacts's ID in the ContactsContract.RawContacts change as well?

Which one is better in _ID and LOOKUP_KEY and the reason

I want to search the contact list to get a particular contact using _ID or LOOKUP_KEY. Which one of these two is better and the reason. It will be really helpful if you give the main differences of both.
Android Doc says-
_ID - Row ID. Consider using LOOKUP_KEY instead.
LOOKUP_KEY - An opaque value that contains hints on how to find the contact if its row id changed as a result of a sync or aggregation.
So when in case the contacts RowID change for any reason LOOKUP_KEY is our way to go. Check this link on how to find it as this is not there on Docs I guess.

in Android 2.0 or later, is it possible to identify all the raw contacts from which a single aggregate contact is formed?

as far as you might know, there are contacts (aggregate contacts) which are formed by aggregation of two or more raw contacts in Android V2.x
is it possible to identify all the raw contacts from which a single aggregate contacts is formed through a query on the ContactsContract.Contacts or is there a way to identify these
contacts at all?
i could not find any flag or database field that tells me that this aggregate contacts is linked with these raw contacts.
any suggestions?
You can check AggregationExceptions.CONTENT_URI Table where relationship type are AggregationExceptions.TYPE_KEEP_TOGETHER, AggregationExceptions.TYPE_KEEP_SEPARATE, etc.
and you can find Raw_contact_id1 and raw_contact_id2.
Example of data into database. Lets say 1,2,3,4 are in relation so you can find following pairs.
Raw_contact_id1 raw_contact_id2 Relationship type
1-> 2, 1->3, 1->4, 2->3, 2->4, 3->4
A Contact cannot be created explicitly. When a raw contact is inserted, the provider will first try to find a Contact representing the same person. If one is found, the raw contact's CONTACT_ID column gets the _ID of the aggregate Contact. If no match is found, the provider automatically inserts a new Contact and puts its _ID into the CONTACT_ID column of the newly inserted raw contact.
So, while reading all the contacts one by one we can take its _ID value and can retrieve all the contacts from raw_contacts where _ID matches with raw_contacts.CONTACT_ID.
If the count is greater than 1 then we can conclude that it is linked with those numbers of contacts else it is not linked with any other contact.

Account for an indefinite amount of SQLite column?

I am trying to make a SQLite table for group of contacts, but I do know the number of contacts that are going to be in that table, so how could I account for ALL the contacts? I could make a single TEXT column and have the String URI of each contact with a delimeter. I would like to support the wide range of users running Android 1.5 and (correct me if I'm wrong) the version of SQLite that runs on Android 1.5 doesn't support foreign keys (only Android 2.2 and above). What should I do?
You can simulate foreign key constraints on older versions of android using triggers: http://www.codeproject.com/KB/android/AndroidSQLite.aspx
From there, you can simply have another table for the groups (and whatever metadata you wanted associated to it), and then another for contacts, and have each contact have a group_id, or if you want multiple groups per contact, you could also have another table containing only contact_id and group_id.
So, let's say you want a set of contacts, and you want a set of contact groups. And you want each group to contain a variable amount of contacts, and you want a contact to be in a variable ammoun of groups, you will want three tables, a group table, a contact table, and a groupcontact table. The group table will have all of the columns that a group needs, plus an additional primary key column (note that it has to be called _id because android assumes it will be called _id). The contact table will have all of the columns needed for a contact, plus an additional primary key. Finally, the groupcontact table will have three columns (although you only care about two of them), a primary key column, a group_id column, and a contact_id column.
Finally, if you want to associate all of the elements of a group, including all of the contacts, you could do something like this:
<group_fileds>, <contact_fields> from <group_table>, <contact_table>, <group_contact_table> where <group_table>._id = <groupid> and <contact_table>._id = <contactid>
Where and are from the groupcontact table. (Note that android has methods demonstrated in their notepad example: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NotePadProvider.html , also you can see an example of things similar (although a bit simpler) in a recent provider I wrote (although I haven't implemented triggers yet, so it won't work properly in older versions of android): https://github.com/LeifAndersen/NetCatch/blob/master/src/net/leifandersen/mobile/android/netcatch/providers/ShowsProvider.java

How to retrieve the best given name of a User in Android?

I retrieve the display name of a user via the ContactsContract API in Android.
Now I want to retrieve the given(first) name of this user. There are specific Rows in the data table that contain the name of the user. The problem is that there are multiple rows for every user because of the synchronization and aggregation of contacts.
The Contacts table documentation states that there should be column containing the id of the raw contact that contributes the primary name for this user. But there is no constant name for this column defined and I couldn't find this column inside my data table.
How do I retrieve the Id of the raw contact that contributes the DisplayName to a contact?
If I understood correctly, the "IS_PRIMARY" or the "IS_SUPER_PRIMARY" flag in the RawContactsEntity-table may solve your problem.

Categories

Resources