I'm writing a class that handles Android contact information and after some struggle, I've written a function that retrieves all information about the user's contacts, including all phone numbers, email addresses, postal addresses, etc. However, I still don't understand how this information is stored in the phone, so I'm hoping someone can give me some insight on this.
So, a user can have multiple phone numbers, multiple email addresses, etc. For contacts you have ContactsContract.Contacts.CONTENT_URI for the table containing all contacts and for phone numbers you have ContactsContract.CommonDataKinds.Phone.CONTENT_URI. But then there is also ContactsContract.RawContacts.CONTENT_URI, which is supposed to contain all the data for your contacts, I think. My assumption is that in the Contacts table, contact ID is the unique identifier for separate rows, whereas for the Phone table, phone number is the unique row identifier. If RawContacts contains all columns of the ContactsContract classes, then there would be no unique identifier for each row, since each contact might be assigned multiple phone numbers, multiple email addresses, etc. In other words, I don't understand how such a table is structured.
So my question is this: Are the various tables containing contact information--Contacts, Phone, Email, StructuredPostal, etc.--completely separate or is the information for each of those tables extracted from the larger RawContacts table? Or am I misunderstand what RawContacts is? Since the class I am writing will help move contacts from the phone contact list into a separate database, knowing this information will help me understand whether I should store the information in multiple databases or just one (similar to how I described the RawContacts database above).
The android API Guide on Contacts Provider gives a nice description of how the Contacts tables are stored (it's a bit convoluted, allowing the merge of many contacts)
picture of data hierarchy
your Q:
So my question is this: Are the various tables containing contact
information--Contacts, Phone, Email, StructuredPostal,
etc.--completely separate or is the information for each of those
tables extracted from the larger RawContacts table?
Looking over the docs again (I've not coded against these tables, only read docs), the Phone, Email... tables appear to be separate. However, the information looks to be stored in both tables, the contact and also in the RawContacts.
Related
I was just going through the ContactsContract API in Android and I am stuck with the overview part of it.
Then I got this link https://developer.android.com/guide/topics/providers/contacts-provider but I am still having difficulty understanding Contacts in android.
Can any of you provide me with links or explanation what is a Contact is, in Android perspective, for me it is just the number we save on our phone but I now know it is some thing more. Please explain to me (or provide link to a simpler or clearer explanation which is not the android documentation itself) what these three tables contain as I am unable to understand it from the docs.
ContactsContract.Contacts table
ContactsContract.RawContacts table
ContactsContract.Data table
Thanks in advance.
As I wrote in many answers before in various phrasing:
The Contacts DB is organized in three main tables:
Contacts - each entry represents one contact, and groups together one or more RawContacts
RawContacts - each entry represents data about a contact that was synced in by some SyncAdapter (e.g. Whatsapp, Google, Facebook, Viber), this groups multiple Data entries
Data - The actual data about a contact, emails, phones, etc. each line is a single piece of data that belongs to a single RawContact
Usually what happens is that an app (e.g. Google, Whatsapp, Linkedin) that wishes to create a new contact will create a new row in the RawContacts table that will usually contain just a name, and then use that row's _ID to add rows into the Data table for phones, emails, addresses, photos, etc.
Android will then either create a new row in Contacts to assign to that new raw-contact (i.e. a new contact was created), or if it decides that raw-contact contain similar enough information to an existing contact, will have an existing contact row assigned to that new raw-contact (i.e. the new information will be added to an existing contact).
The "assigning" part is done like this - each row in Data has a column RAW_CONTACT_ID which tells the contacts app which raw-contact does this info belong to, and each row in RawContacts has a column CONTACT_ID which tells the contacts app which row in Contacts this raw-contact belong-to.
So to get information on contact with _ID = 1234, you could first query info from Contacts where _ID = 1234, then query more info from RawContacts where CONTACT_ID = 1234, then query for more info from Data where RAW_CONTACT_ID IN (X) where X is the list of raw-contact ids you found previously.
The Data table also has a CONTACT_ID column, so you can basically skip the RawContact query, and get all the data (phones, emails, etc.) directly from the contact-id.
Hope that's clear.
Let me try and clarify my intentions.
I'm developing an app that accesses to the Android contacts provider. I have already implemented a mechanism for pulling contacts from the contacts provider and storing the results in an SQLite table. Currently, when I query for the results of a contact's _ID, I can retrieve all the data for that contact, phone numbers, email addresses, etc.
However, in order to specify which of those my app should use on future occasions, I have to store the resulting contact data (e.g. CommonDataKinds.Phone.NUMBER, CommonDataKinds.Phone.TYPE etc) in the SQL table.
This presents a problem if the data in the Android contacts provider has changed. One solution I have considered is to re-query the _ID and store the data that has changed. However, implementing such a solution requires gathering all data for that contact, making it difficult to determine the correct contact data to use.
My question is thus:
Is there a unique record key used in the Android Provider's contact data, in the same way as there is in the Provider's contact entry itself? A phone number or email address equivalent of Contact._ID?
Failing that, does the Android contacts provider store the last modified date and time? I'm hoping that if I can't reference the contact data in the provider, I can at least run a check to see if anything has changed since the contact was selected for use in the app, allowing my app to alert the user that the data has changed.
Yoi may use ContentObserver with the help of a service to monitor for contacts change or update.
for example-
extend contentObserver-
public class Contact_change extends ContentObserver
register contentobserver-
Contact_change changeObserver = new Contact_change();
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,chageobserver);
By using this you can monitor for contact changes and update your database.
So while looking to see what was possible, I've come across some interesting findings by doing a localised record dump to my tablet.
I incorrectly referred to the phone _ID when asking my question. It's there, but it references individual records in any given contact. This might be useful, it might be exactly what I hoped, but it might also be a dead end.
So far, I've retrieved a contact using the Android Contacts Provider, which returns the contact's data including the fields that follow
ContactsContract.Contacts._ID
ContactsContract.Contacts.DISPLAY_NAME
After assigning the _ID to a String and using it in a query, I initially searched for the _ID in the ContactsContract.Data._ID field, which did not work as intended.
That's when I realised I was doing it wrong. I needed to apply the search to ContactsContract.Data.CONTACT_ID, which then retrieved all related data records for my selected contact.
Having corrected the error and re-ran my query, I found a series of records for the CONTACT_ID, each of which had its' own unique _ID (with the exception of photos and binary data, which all had a null reference to _ID instead.
Now the question is: Does this data reference change even if a phone number, email address or IM entry is modified, as opposed to deleted and re-created? I'll probably find out through further testing, but I wanted to ask if anyone had already tried this first.
Ultimately, I am hoping my internal SQL table can store only the CONTACT_ID and the _ID for each entry, relying on callbacks to the Android Provider to pull the relevant values. That way, I can be sure the data displayed in my app is up to date between the app and the stored contact data on the device.
Combined with the change notifier code provided here, I should be able to alert the user to any changes that may require action on their part.
Update:
More investigation reveals that the UID associated with a given contact entry is preserved through entry edits.
The UID is lost obviously on deletion, and in my brief testing, I have not found UID's being recycled. This solves a referential integrity issue for me at least, as that UID won't suddenly reference another record out of nowhere.
The upshot for anyone else wanting to reference individual entries in the Contacts Provider is that it's possible, and you can store only the _ID and still retrieve individual entries in the provider.
My task is to retreive name, phone, email, company and note from all contacts on android device. I have never worked with content providers before. I read content provider documentation, tutorials, saw code samples, but I'm still not quite sure: does android stores information about name, phone, email, company and note in different tables?
So do I have to query name, phone, email, company and note separately for each contact? 5 queries per contact * number of contact = most efficient way? Or do I have alternative with fewer number of queries? Why not to store all contacts and all contact fields in the same table so that it would be possible to get all data with single SELECT query (its analog in android content providers framework)?
So my main question again: do I have to make 5 queries per contact or there is more effient way?
does android stores information about name, phone, email, company and
note in different tables?
Almost. Name is stored in Contacts.DISPLAY_NAME.
phone and emails are in Data table but to interpret column names you need to use
ContactsContract.CommonDataKinds.Phone class.
Update: The original article I referred to was removed.
Here are the relevant links:
Android documentation overall
The columns reference
If you are interested in what goes on behind the curtain, google implemented their contact database (as of 4.2) so that phone, email are stored all in the same table called data. This table has columns like data1, data2 .... data15, which allows storing diverse data in that table as long as attribute of that data item do not exceed 15.
My guess it is for performance reasons - much faster to select all the data about one contact in one query() call.
Name is another story. It is stored in two tables, "raw_contacts"(column display_name) and also "data" (column "data1")
How do I query all contacts matching a certain criteria on Android?
Let´s say, I want to have all contacts, which have a name, a phone number, but no profile picture.
As I understand the SQL thing, I need some kind of selection, which is passed to my query, but how would it exactly look in the example above?
I´m asking, because I´m currently running an app which first queries all contacts and after that I iterate through it and filter it, which seems much less performant then querying directly the right contacts
You are correct in the sense that contacts are stored in a SQLite database, but they are accessed via the Contacts Content Provider.
The Contacts Content Provider is organized in a three table fashion. At the bottom of the data structure lies the Data table. Here you will find all data that pertains to a specific RawContact (will discuss shortly). The Data table stores information like phone numbers, emails, addresses,etc. It does so by using MIME name value pairs defined on the mimetypes database. Up one level is the RawContacts database. Here all the information that pertains to the same account (i.e. Contact John Doe may have a twitter and Facebook account). In short, the RawContacts table keeps track of Data MIME types that pertain to the same person so there is no need to store the same information multiple times for different accounts. At the top of the data structure you have the Contacts table that groups all the information for a single person. It is basically a way of unifying all account information into one contact. Think of this contacts as you regularly would.
You should take a look at the Loader class which will allow you to perform queries on a background Thread. If you use this class once you implement the Loader callback for the Cursor, you will create a CursorLoader. One of its constructors parameter is
A typical MySQL where clause where you can specify the constraints you want to include as part of your query.
Here you would find a guide to Loaders to query a database.
I've been reading for a couple hours now, trying to figure out how to maintain a reliable list of contacts in an Android application, but still cannot find one clear successful case.
My situation is this: I let users create Groups in my application, and in each Group, the user can select, from their contact list on the phone, which users they'd like to add to that group. I then need to be able to have a reliable way to compare a call from an incoming contact with my contacts database in my application, to see if they are in specific groups.
The easy parts are to add specific contacts to my database, and also to look up a contact based on their phone number. Thanks to this forum they are easy anyway! :) I'm storing the contacts in my database by Contact Name, Lookup Key, and Contact Id. The hard part for me, and this is what I cannot find a clear answer on, is how do I know that a month down the road, Contact X is going to have the same Lookup Key or Contact Id as when they were added? Couldn't they all change by then? Obviously the name can easily change, but can't the lookup_key and Contact ID also change? I've read about the lookup_key changing if contacts are manually aggregated. In other words, I am looking for the identifiable information for a contact that CANNOT change once they are entered?
I have read about using a ContentObserver to register for changes to the Contacts database, but I don't see that this helps me at all, since if I have Contact X with Lookup Key Y and Contact ID of Z, even if I get updated that the Contacts have changed, I still need to match Contact X in my application with Contact X in the Contacts database to update my info, which I still cannot do if the identifying information has been changed.
For example, I have a contact with Name, Lookup_Key, ContactID of Ted, 230ff392, 3209482. A month later, could it happen that what used to be Ted is now T-bone, 458ee247, 5502981? If this were the case, I cannot use these 3 identifiers as a means to look up the contact.
Thanks so much for the help on this!
Paul
I don't know where you read that the lookup key might change, but the documentation states that they are permanent in contrary to contact ids.
Contacts Provider / Contacts:
The ContactsContract.Contacts table also has the column LOOKUP_KEY
that is a "permanent" link to the contact row. Because the Contacts
Provider maintains contacts automatically, it may change a contact
row's _ID value in response to an aggregation or sync. Even If this
happens, the content URI CONTENT_LOOKUP_URI combined with contact's
LOOKUP_KEY will still point to the contact row, so you can use
LOOKUP_KEY to maintain links to "favorite" contacts, and so forth.
This column has its own format that is unrelated to the format of the
_ID column.