page.title=Contacts Provider @jd:body
The Contacts Provider is a powerful and flexible Android component that manages the device's central repository of data about people. The Contacts Provider is the source of data you see in the device's contacts application, and you can also access its data in your own application and transfer data between the device and online services. The provider accommodates a wide range of data sources and tries to manage as much data as possible for each person, with the result that its organization is complex. Because of this, the provider's API includes an extensive set of contract classes and interfaces that facilitate both data retrieval and modification.
This guide describes the following:
This guide assumes that you know the basics of Android content providers. To learn more about Android content providers, read the Content Provider Basics guide. The Sample Sync Adapter sample app is an example of using a sync adapter to transfer data between the Contacts Provider and a sample application hosted by Google Web Services.
The Contacts Provider is an Android content provider component. It maintains three types of data about a person, each of which corresponds to a table offered by the provider, as illustrated in figure 1:
The three tables are commonly referred to by the names of their contract classes. The classes define constants for content URIs, column names, and column values used by the tables:
The other tables represented by contract classes in {@link android.provider.ContactsContract} are auxiliary tables that the Contacts Provider uses to manage its operations or support specific functions in the device's contacts or telephony applications.
A raw contact represents a person's data coming from a single account type and account name. Because the Contacts Provider allows more than one online service as the source of data for a person, the Contacts Provider allows multiple raw contacts for the same person. Multiple raw contacts also allow a user to combine a person's data from more than one account from the same account type.
Most of the data for a raw contact isn't stored in the {@link android.provider.ContactsContract.RawContacts} table. Instead, it's stored in one or more rows in the {@link android.provider.ContactsContract.Data} table. Each data row has a column {@link android.provider.ContactsContract.DataColumns#RAW_CONTACT_ID Data.RAW_CONTACT_ID} that contains the {@link android.provider.BaseColumns#_ID RawContacts._ID} value of its parent {@link android.provider.ContactsContract.RawContacts} row.
The important columns in the {@link android.provider.ContactsContract.RawContacts} table are listed in table 1. Please read the notes that follow after the table:
Column name | Use | Notes |
---|---|---|
{@link android.provider.ContactsContract.SyncColumns#ACCOUNT_NAME} | The account name for the account type that's the source of this raw contact. For example, the account name of a Google account is one of the device owner's Gmail addresses. See the next entry for {@link android.provider.ContactsContract.SyncColumns#ACCOUNT_TYPE} for more information. | The format of this name is specific to its account type. It is not necessarily an email address. |
{@link android.provider.ContactsContract.SyncColumns#ACCOUNT_TYPE} |
The account type that's the source of this raw contact. For example, the account
type of a Google account is com.google . Always qualify your account type
with a domain identifier for a domain you own or control. This will ensure that your
account type is unique.
|
An account type that offers contacts data usually has an associated sync adapter that synchronizes with the Contacts Provider. |
{@link android.provider.ContactsContract.RawContactsColumns#DELETED} | The "deleted" flag for a raw contact. | This flag allows the Contacts Provider to maintain the row internally until sync adapters are able to delete the row from their servers and then finally delete the row from the repository. |
The following are important notes about the {@link android.provider.ContactsContract.RawContacts} table:
For example, if you want your app to maintain contacts data for your web-based service with the domain {@code com.example.dataservice}, and the user's account for your service is {@code becky.sharp@dataservice.example.com}, the user must first add the account "type" ({@code com.example.dataservice}) and account "name" ({@code becky.smart@dataservice.example.com}) before your app can add raw contact rows. You can explain this requirement to the user in documentation, or you can prompt the user to add the type and name, or both. Account types and account names are described in more detail in the next section.
To understand how raw contacts work, consider the user "Emily Dickinson" who has the following three user accounts defined on her device:
emily.dickinson@gmail.com
emilyd@gmail.com
This user has enabled Sync Contacts for all three of these accounts in the Accounts settings.
Suppose Emily Dickinson opens a browser window, logs into Gmail as
emily.dickinson@gmail.com
, opens
Contacts, and adds "Thomas Higginson". Later on, she logs into Gmail as
emilyd@gmail.com
and sends an email to "Thomas Higginson", which automatically
adds him as a contact. She also follows "colonel_tom" (Thomas Higginson's Twitter ID) on
Twitter.
The Contacts Provider creates three raw contacts as a result of this work:
emily.dickinson@gmail.com
.
The user account type is Google.
emilyd@gmail.com
.
The user account type is also Google. There is a second raw contact even
though the name is identical to a previous name, because the person was added for a
different user account.
As noted previously, the data for a raw contact is stored in a
{@link android.provider.ContactsContract.Data} row that is linked to the raw contact's
_ID
value. This allows a single raw contact to have multiple instances of the same
type of data such as email addresses or phone numbers. For example, if
"Thomas Higginson" for {@code emilyd@gmail.com} (the raw contact row for Thomas Higginson
associated with the Google account emilyd@gmail.com
) has a home email address of
thigg@gmail.com
and a work email address of
thomas.higginson@gmail.com
, the Contacts Provider stores the two email address
rows and links them both to the raw contact.
Notice that different types of data are stored in this single table. Display name, phone number, email, postal address, photo, and website detail rows are all found in the {@link android.provider.ContactsContract.Data} table. To help manage this, the {@link android.provider.ContactsContract.Data} table has some columns with descriptive names, and others with generic names. The contents of a descriptive-name column have the same meaning regardless of the type of data in the row, while the contents of a generic-name column have different meanings depending on the type of data.
Some examples of descriptive column names are:
_ID
column of the raw contact for this data.
There are 15 generic columns named DATA1
through
DATA15
that are generally available and an additional four generic
columns SYNC1
through SYNC4
that should only be used by sync
adapters. The generic column name constants always work, regardless of the type of
data the row contains.
The DATA1
column is indexed. The Contacts Provider always uses this column for
the data that the provider expects will be the most frequent target of a query. For example,
in an email row, this column contains the actual email address.
By convention, the column DATA15
is reserved for storing Binary Large Object
(BLOB) data such as photo thumbnails.
To facilitate working with the columns for a particular type of row, the Contacts Provider also provides type-specific column name constants, defined in subclasses of {@link android.provider.ContactsContract.CommonDataKinds}. The constants simply give a different constant name to the same column name, which helps you access data in a row of a particular type.
For example, the {@link android.provider.ContactsContract.CommonDataKinds.Email} class defines type-specific column name constants for a {@link android.provider.ContactsContract.Data} row that has the MIME type {@link android.provider.ContactsContract.CommonDataKinds.Email#CONTENT_ITEM_TYPE Email.CONTENT_ITEM_TYPE}. The class contains the constant {@link android.provider.ContactsContract.CommonDataKinds.Email#ADDRESS} for the email address column. The actual value of {@link android.provider.ContactsContract.CommonDataKinds.Email#ADDRESS} is "data1", which is the same as the column's generic name.
Caution: Don't add your own custom data to the
{@link android.provider.ContactsContract.Data} table using a row that has one of the
provider's pre-defined MIME types. If you do, you may lose the data or cause the provider to
malfunction. For example, you should not add a row with the MIME type
{@link android.provider.ContactsContract.CommonDataKinds.Email#CONTENT_ITEM_TYPE
Email.CONTENT_ITEM_TYPE} that contains a user name instead of an email address in the
column DATA1
. If you use your own custom MIME type for the row, then you are free
to define your own type-specific column names and use the columns however you wish.
Figure 2 shows how descriptive columns and data columns appear in a {@link android.provider.ContactsContract.Data} row, and how type-specific column names "overlay" the generic column names
Table 2 lists the most commonly-used type-specific column name classes:
Mapping class | Type of data | Notes |
---|---|---|
{@link android.provider.ContactsContract.CommonDataKinds.StructuredName} | The name data for the raw contact associated with this data row. | A raw contact has only one of these rows. |
{@link android.provider.ContactsContract.CommonDataKinds.Photo} | The main photo for the raw contact associated with this data row. | A raw contact has only one of these rows. |
{@link android.provider.ContactsContract.CommonDataKinds.Email} | An email address for the raw contact associated with this data row. | A raw contact can have multiple email addresses. |
{@link android.provider.ContactsContract.CommonDataKinds.StructuredPostal} | A postal address for the raw contact associated with this data row. | A raw contact can have multiple postal addresses. |
{@link android.provider.ContactsContract.CommonDataKinds.GroupMembership} | An identifier that links the raw contact to one of the groups in the Contacts Provider. | Groups are an optional feature of an account type and account name. They're described in more detail in the section Contact groups. |
The Contacts Provider combines the raw contact rows across all account types and account names to form a contact. This facilitates displaying and modifying all the data a user has collected for a person. The Contacts Provider manages the creation of new contact rows, and the aggregation of raw contacts with an existing contact row. Neither applications nor sync adapters are allowed to add contacts, and some columns in a contact row are read-only.
Note: If you try to add a contact to the Contacts Provider with an {@link android.content.ContentResolver#insert(Uri,ContentValues) insert()}, you'll get an {@link java.lang.UnsupportedOperationException} exception. If you try to update a column that's listed as "read-only," the update is ignored.
The Contacts Provider creates a new contact in response to the addition of a new raw contact that doesn't match any existing contacts. The provider also does this if an existing raw contact's data changes in such a way that it no longer matches the contact to which it was previously attached. If an application or sync adapter creates a new raw contact that does match an existing contact, the new raw contact is aggregated to the existing contact.
The Contacts Provider links a contact row to its raw contact rows with the contact row's
_ID
column in the {@link android.provider.ContactsContract.Contacts Contacts}
table. The CONTACT_ID
column of the raw contacts table
{@link android.provider.ContactsContract.RawContacts} contains _ID
values for
the contacts row associated with each raw contacts row.
The {@link android.provider.ContactsContract.Contacts} table also has the column {@link android.provider.ContactsContract.ContactsColumns#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 {@link android.provider.BaseColumns#_ID} value in response to an aggregation or sync. Even If this happens, the content URI {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI} combined with contact's {@link android.provider.ContactsContract.ContactsColumns#LOOKUP_KEY} will still point to the contact row, so you can use {@link android.provider.ContactsContract.ContactsColumns#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 {@link android.provider.BaseColumns#_ID} column.
Figure 3 shows how the three main tables relate to each other.
Users enter contacts data directly into the device, but data also flows into the Contacts Provider from web services via sync adapters, which automate the transfer of data between the device and services. Sync adapters run in the background under the control of the system, and they call {@link android.content.ContentResolver} methods to manage data.
In Android, the web service that a sync adapter works with is identified by an account type. Each sync adapter works with one account type, but it can support multiple account names for that type. Account types and account names are described briefly in the section Sources of raw contacts data. The following definitions offer more detail, and describe how account type and name relate to sync adapters and services.
google.com
. This value corresponds to the account type used by
{@link android.accounts.AccountManager}.
Account types don't have to be unique. A user can configure multiple Google Contacts accounts and download their data to the Contacts Provider; this may happen if the user has one set of personal contacts for a personal account name, and another set for work. Account names are usually unique. Together, they identify a specific data flow between the Contacts Provider and an external service.
If you want to transfer your service's data to the Contacts Provider, you need to write your own sync adapter. This is described in more detail in the section Contacts Provider Sync Adapters.
Figure 4 shows how the Contacts Provider fits into the flow of data about people. In the box marked "sync adapters," each adapter is labeled by its account type.
Applications that want to access the Contacts Provider must request the following permissions:
AndroidManifest.xml
with the
<uses-permission>
element as
<uses-permission android:name="android.permission.READ_CONTACTS">
.
AndroidManifest.xml
with the
<uses-permission>
element as
<uses-permission android:name="android.permission.WRITE_CONTACTS">
.
These permissions do not extend to the user profile data. The user profile and its required permissions are discussed in the following section, The User Profile.
Remember that the user's contacts data is personal and sensitive. Users are concerned about their privacy, so they don't want applications collecting data about them or their contacts. If it's not obvious why you need permission to access their contacts data, they may give your application low ratings or simply refuse to install it.
The {@link android.provider.ContactsContract.Contacts} table has a single row containing
profile data for the device's user. This data describes the device's user
rather
than one of the user's contacts. The profile contacts row is linked to a raw
contacts row for each system that uses a profile.
Each profile raw contact row can have multiple data rows. Constants for accessing the user
profile are available in the {@link android.provider.ContactsContract.Profile} class.
Access to the user profile requires special permissions. In addition to the {@link android.Manifest.permission#READ_CONTACTS} and {@link android.Manifest.permission#WRITE_CONTACTS} permissions needed to read and write, access to the user profile requires the {@link android.Manifest.permission#READ_PROFILE} and {@link android.Manifest.permission#WRITE_PROFILE} permissions for read and write access, respectively.
Remember that you should consider a user's profile to be sensitive. The permission {@link android.Manifest.permission#READ_PROFILE} allows you to access the device user's personally-identifying data. Make sure to tell the user why you need user profile access permissions in the description of your application.
To retrieve the contact row that contains the user's profile, call {@link android.content.ContentResolver#query(Uri,String[], String, String[], String) ContentResolver.query()}. Set the content URI to {@link android.provider.ContactsContract.Profile#CONTENT_URI} and don't provide any selection criteria. You can also use this content URI as the base URI for retrieving raw contacts or data for the profile. For example, this snippet retrieves data for the profile:
// Sets the columns to retrieve for the user profile mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY, Profile.PHOTO_THUMBNAIL_URI }; // Retrieves the profile from the Contacts Provider mProfileCursor = getContentResolver().query( Profile.CONTENT_URI, mProjection , null, null, null);
Note: If you retrieve multiple contact rows, and you want to determine if one of them is the user profile, test the row's {@link android.provider.ContactsContract.ContactsColumns#IS_USER_PROFILE} column. This column is set to "1" if the contact is the user profile.
The Contacts Provider manages data that keeps track of the state of contacts data in the repository. This metadata about the repository is stored in various places, including the Raw Contacts, Data, and Contacts table rows, the {@link android.provider.ContactsContract.Settings} table, and the {@link android.provider.ContactsContract.SyncState} table. The following table shows the effect of each of these pieces of metadata:
Table | Column | Values | Meaning |
---|---|---|---|
{@link android.provider.ContactsContract.RawContacts} | {@link android.provider.ContactsContract.SyncColumns#DIRTY} | "0" - not changed since the last sync. |
Marks raw contacts that were changed on the device and have to be synced back to the
server. The value is set automatically by the Contacts Provider when Android
applications update a row.
Sync adapters that modify the raw contact or data tables should always append the string {@link android.provider.ContactsContract#CALLER_IS_SYNCADAPTER} to the content URI they use. This prevents the provider from marking rows as dirty. Otherwise, sync adapter modifications appear to be local modifications and are sent to the server, even though the server was the source of the modification. |
"1" - changed since last sync, needs to be synced back to the server. | |||
{@link android.provider.ContactsContract.RawContacts} | {@link android.provider.ContactsContract.SyncColumns#VERSION} | The version number of this row. | The Contacts Provider automatically increments this value whenever the row or its related data changes. |
{@link android.provider.ContactsContract.Data} | {@link android.provider.ContactsContract.DataColumns#DATA_VERSION} | The version number of this row. | The Contacts Provider automatically increments this value whenever the data row is changed. |
{@link android.provider.ContactsContract.RawContacts} | {@link android.provider.ContactsContract.SyncColumns#SOURCE_ID} | A string value that uniquely identifies this raw contact to the account in which it was created. |
When a sync adapter creates a new raw contact, this column should be set to the
server's unique ID for the raw contact. When an Android application creates a new
raw contact, the application should leave this column empty. This signals the sync
adapter that it should create a new raw contact on the server, and get a
value for the {@link android.provider.ContactsContract.SyncColumns#SOURCE_ID}.
In particular, the source id must be unique for each account type and should be stable across syncs:
|
{@link android.provider.ContactsContract.Groups} | {@link android.provider.ContactsContract.GroupsColumns#GROUP_VISIBLE} | "0" - Contacts in this group should not be visible in Android application UIs. | This column is for compatibility with servers that allow a user to hide contacts in certain groups. |
"1" - Contacts in this group are allowed to be visible in application UIs. | |||
{@link android.provider.ContactsContract.Settings} | {@link android.provider.ContactsContract.SettingsColumns#UNGROUPED_VISIBLE} | "0" - For this account and account type, contacts that don't belong to a group are invisible to Android application UIs. | By default, contacts are invisible if none of their raw contacts belongs to a group (Group membership for a raw contact is indicated by one or more {@link android.provider.ContactsContract.CommonDataKinds.GroupMembership} rows in the {@link android.provider.ContactsContract.Data} table). By setting this flag in the {@link android.provider.ContactsContract.Settings} table row for an account type and account, you can force contacts without groups to be visible. One use of this flag is to show contacts from servers that don't use groups. |
"1" - For this account and account type, contacts that don't belong to a group are visible to application UIs. | |||
{@link android.provider.ContactsContract.SyncState} | (all) | Use this table to store metadata for your sync adapter. | With this table you can store sync state and other sync-related data persistently on the device. |
This section describes guidelines for accessing data from the Contacts Provider, focusing on the following:
Making modifications from a sync adapter is also covered in more detail in the section Contacts Provider Sync Adapters.
Because the Contacts Provider tables are organized in a hierarchy, it's often useful to retrieve a row and all of the "child" rows that are linked to it. For example, to display all the information for a person, you may want to retrieve all the {@link android.provider.ContactsContract.RawContacts} rows for a single {@link android.provider.ContactsContract.Contacts} row, or all the {@link android.provider.ContactsContract.CommonDataKinds.Email} rows for a single {@link android.provider.ContactsContract.RawContacts} row. To facilitate this, the Contacts Provider offers entity constructs, which act like database joins between tables.
An entity is like a table composed of selected columns from a parent table and its child table. When you query an entity, you supply a projection and search criteria based on the columns available from the entity. The result is a {@link android.database.Cursor} that contains contains one row for each child table row that was retrieved. For example, if you query {@link android.provider.ContactsContract.Contacts.Entity} for a contact name and all the {@link android.provider.ContactsContract.CommonDataKinds.Email} rows for all the raw contacts for that name, you get back a {@link android.database.Cursor} containing one row for each {@link android.provider.ContactsContract.CommonDataKinds.Email} row.
Entities simplify queries. Using an entity, you can retrieve all of the contacts data for a contact or raw contact at once, instead of having to query the parent table first to get an ID, and then having to query the child table with that ID. Also, the Contacts Provider processes a query against an entity in a single transaction, which ensures that the retrieved data is internally consistent.
Note: An entity usually doesn't contain all the columns of the parent and child table. If you attempt to work with a column name that isn't in the list of column name constants for the entity, you'll get an {@link java.lang.Exception}.
The following snippet shows how to retrieve all the raw contact rows for a contact. The snippet is part of a larger application that has two activities, "main" and "detail". The main activity shows a list of contact rows; when the user select one, the activity sends its ID to the detail activity. The detail activity uses the {@link android.provider.ContactsContract.Contacts.Entity} to display all of the data rows from all of the raw contacts associated with the selected contact.
This snippet is taken from the "detail" activity:
... /* * Appends the entity path to the URI. In the case of the Contacts Provider, the * expected URI is content://com.google.contacts/#/entity (# is the ID value). */ mContactUri = Uri.withAppendedPath( mContactUri, ContactsContract.Contacts.Entity.CONTENT_DIRECTORY); // Initializes the loader identified by LOADER_ID. getLoaderManager().initLoader( LOADER_ID, // The identifier of the loader to initialize null, // Arguments for the loader (in this case, none) this); // The context of the activity // Creates a new cursor adapter to attach to the list view mCursorAdapter = new SimpleCursorAdapter( this, // the context of the activity R.layout.detail_list_item, // the view item containing the detail widgets mCursor, // the backing cursor mFromColumns, // the columns in the cursor that provide the data mToViews, // the views in the view item that display the data 0); // flags // Sets the ListView's backing adapter. mRawContactList.setAdapter(mCursorAdapter); ... @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { /* * Sets the columns to retrieve. * RAW_CONTACT_ID is included to identify the raw contact associated with the data row. * DATA1 contains the first column in the data row (usually the most important one). * MIMETYPE indicates the type of data in the data row. */ String[] projection = { ContactsContract.Contacts.Entity.RAW_CONTACT_ID, ContactsContract.Contacts.Entity.DATA1, ContactsContract.Contacts.Entity.MIMETYPE }; /* * Sorts the retrieved cursor by raw contact id, to keep all data rows for a single raw * contact collated together. */ String sortOrder = ContactsContract.Contacts.Entity.RAW_CONTACT_ID + " ASC"; /* * Returns a new CursorLoader. The arguments are similar to * ContentResolver.query(), except for the Context argument, which supplies the location of * the ContentResolver to use. */ return new CursorLoader( getApplicationContext(), // The activity's context mContactUri, // The entity content URI for a single contact projection, // The columns to retrieve null, // Retrieve all the raw contacts and their data rows. null, // sortOrder); // Sort by the raw contact ID. }
When the load is finished, {@link android.app.LoaderManager} invokes a callback to {@link android.app.LoaderManager.LoaderCallbacks#onLoadFinished(Loader, D) onLoadFinished()}. One of the incoming arguments to this method is a {@link android.database.Cursor} with the results of the query. In your own app, you can get the data from this {@link android.database.Cursor} to display it or work with it further.
Whenever possible, you should insert, update, and delete data in the Contacts Provider in "batch mode", by creating an {@link java.util.ArrayList} of {@link android.content.ContentProviderOperation} objects and calling {@link android.content.ContentResolver#applyBatch(String, ArrayList) applyBatch()}. Because the Contacts Provider performs all of the operations in an {@link android.content.ContentResolver#applyBatch(String, ArrayList) applyBatch()} in a single transaction, your modifications will never leave the contacts repository in an inconsistent state. A batch modification also facilitates inserting a raw contact and its detail data at the same time.
Note: To modify a single raw contact, consider sending an intent to the device's contacts application rather than handling the modification in your app. Doing this is described in more detail in the section Retrieval and modification with intents.
A batch modification containing a large number of operations can block other processes,
resulting in a bad overall user experience. To organize all the modifications you want to
perform in as few separate lists as possible, and at the same time prevent them from
blocking the system, you should set yield points for one or more operations.
A yield point is a {@link android.content.ContentProviderOperation} object that has its
{@link android.content.ContentProviderOperation#isYieldAllowed()} value set to
true
. When the Contacts Provider encounters a yield point, it pauses its work to
let other processes run and closes the current transaction. When the provider starts again, it
continues with the next operation in the {@link java.util.ArrayList} and starts a new
transaction.
Yield points do result in more than one transaction per call to {@link android.content.ContentResolver#applyBatch(String, ArrayList) applyBatch()}. Because of this, you should set a yield point for the last operation for a set of related rows. For example, you should set a yield point for the last operation in a set that adds a raw contact rows and its associated data rows, or the last operation for a set of rows related to a single contact.
Yield points are also a unit of atomic operation. All accesses between two yield points will either succeed or fail as a single unit. If you don't set any yield points, the smallest atomic operation is the entire batch of operations. If you do use yield points, you prevent operations from degrading system performance, while at the same time ensuring that a subset of operations is atomic.
When you're inserting a new raw contact row and its associated data rows as a set of {@link android.content.ContentProviderOperation} objects, you have to link the data rows to the raw contact row by inserting the raw contact's {@link android.provider.BaseColumns#_ID} value as the {@link android.provider.ContactsContract.DataColumns#RAW_CONTACT_ID} value. However, this value isn't available when you're creating the {@link android.content.ContentProviderOperation} for the data row, because you haven't yet applied the {@link android.content.ContentProviderOperation} for the raw contact row. To work around this, the {@link android.content.ContentProviderOperation.Builder} class has the method {@link android.content.ContentProviderOperation.Builder#withValueBackReference(String, int) withValueBackReference()}. This method allows you to insert or modify a column with the result of a previous operation.
The {@link android.content.ContentProviderOperation.Builder#withValueBackReference(String, int) withValueBackReference()} method has two arguments:
key
previousResult
previousResult
value is the index
of one of these results, which is retrieved and stored with the key
value. This allows you to insert a new raw contact record and get back its
{@link android.provider.BaseColumns#_ID} value, then make a "back reference" to the
value when you add a {@link android.provider.ContactsContract.Data} row.
The entire result array is created when you first call
{@link android.content.ContentResolver#applyBatch(String, ArrayList) applyBatch()},
with a size equal to the size of the {@link java.util.ArrayList} of
{@link android.content.ContentProviderOperation} objects you provide. However, all
the elements in the result array are set to null
, and if you try
to do a back reference to a result for an operation that hasn't yet been applied,
{@link android.content.ContentProviderOperation.Builder#withValueBackReference(String, int) withValueBackReference()}
throws an {@link java.lang.Exception}.
The following snippets show how to insert a new raw contact and data in batch. They
includes code that establishes a yield point and uses a back reference. The snippets are an
expanded version of the createContacEntry()
method, which is part of the
ContactAdder
class in the
Contact Manager
sample application.
The first snippet retrieves contact data from the UI. At this point, the user has already selected the account for which the new raw contact should be added.
// Creates a contact entry from the current UI values, using the currently-selected account. protected void createContactEntry() { /* * Gets values from the UI */ String name = mContactNameEditText.getText().toString(); String phone = mContactPhoneEditText.getText().toString(); String email = mContactEmailEditText.getText().toString(); int phoneType = mContactPhoneTypes.get( mContactPhoneTypeSpinner.getSelectedItemPosition()); int emailType = mContactEmailTypes.get( mContactEmailTypeSpinner.getSelectedItemPosition());
The next snippet creates an operation to insert the raw contact row into the {@link android.provider.ContactsContract.RawContacts} table:
/* * Prepares the batch operation for inserting a new raw contact and its data. Even if * the Contacts Provider does not have any data for this person, you can't add a Contact, * only a raw contact. The Contacts Provider will then add a Contact automatically. */ // Creates a new array of ContentProviderOperation objects. ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); /* * Creates a new raw contact with its account type (server type) and account name * (user's account). Remember that the display name is not stored in this row, but in a * StructuredName data row. No other data is required. */ ContentProviderOperation.Builder op = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType()) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName()); // Builds the operation and adds it to the array of operations ops.add(op.build());
Next, the code creates data rows for the display name, phone, and email rows.
Each operation builder object uses {@link android.content.ContentProviderOperation.Builder#withValueBackReference(String, int) withValueBackReference()} to get the {@link android.provider.ContactsContract.DataColumns#RAW_CONTACT_ID}. The reference points back to the {@link android.content.ContentProviderResult} object from the first operation, which adds the raw contact row and returns its new {@link android.provider.BaseColumns#_ID} value. As a result, each data row is automatically linked by its {@link android.provider.ContactsContract.DataColumns#RAW_CONTACT_ID} to the new {@link android.provider.ContactsContract.RawContacts} row to which it belongs.
The {@link android.content.ContentProviderOperation.Builder} object that adds the email row is flagged with {@link android.content.ContentProviderOperation.Builder#withYieldAllowed(boolean) withYieldAllowed()}, which sets a yield point:
// Creates the display name for the new raw contact, as a StructuredName data row. op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) /* * withValueBackReference sets the value of the first argument to the value of * the ContentProviderResult indexed by the second argument. In this particular * call, the raw contact ID column of the StructuredName data row is set to the * value of the result returned by the first operation, which is the one that * actually adds the raw contact row. */ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) // Sets the data row's MIME type to StructuredName .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) // Sets the data row's display name to the name in the UI. .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name); // Builds the operation and adds it to the array of operations ops.add(op.build()); // Inserts the specified phone number and type as a Phone data row op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) /* * Sets the value of the raw contact id column to the new raw contact ID returned * by the first operation in the batch. */ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) // Sets the data row's MIME type to Phone .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) // Sets the phone number and type .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phoneType); // Builds the operation and adds it to the array of operations ops.add(op.build()); // Inserts the specified email and type as a Phone data row op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) /* * Sets the value of the raw contact id column to the new raw contact ID returned * by the first operation in the batch. */ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) // Sets the data row's MIME type to Email .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) // Sets the email address and type .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email) .withValue(ContactsContract.CommonDataKinds.Email.TYPE, emailType); /* * Demonstrates a yield point. At the end of this insert, the batch operation's thread * will yield priority to other threads. Use after every set of operations that affect a * single contact, to avoid degrading performance. */ op.withYieldAllowed(true); // Builds the operation and adds it to the array of operations ops.add(op.build());
The last snippet shows the call to {@link android.content.ContentResolver#applyBatch(String, ArrayList) applyBatch()} that inserts the new raw contact and data rows.
// Ask the Contacts Provider to create a new contact Log.d(TAG,"Selected account: " + mSelectedAccount.getName() + " (" + mSelectedAccount.getType() + ")"); Log.d(TAG,"Creating contact: " + name); /* * Applies the array of ContentProviderOperation objects in batch. The results are * discarded. */ try { getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); } catch (Exception e) { // Display a warning Context ctx = getApplicationContext(); CharSequence txt = getString(R.string.contactCreationFailure); int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(ctx, txt, duration); toast.show(); // Log exception Log.e(TAG, "Exception encountered while inserting contact: " + e); } }
Batch operations also allow you to implement optimistic concurrency control, a method of applying modification transactions without having to lock the underlying repository. To use this method, you apply the transaction and then check for other modifications that may have been made at the same time. If you find an inconsistent modification has occurred, you roll back your transaction and retry it.
Optimistic concurrency control is useful for a mobile device, where there's only one user at a time, and simultaneous accesses to a data repository are rare. Because locking isn't used, no time is wasted on setting locks or waiting for other transactions to release their locks.
To use optimistic concurrency control while updating a single {@link android.provider.ContactsContract.RawContacts} row, follow these steps:
If the raw contact row is updated by another operation between the time you read the row and the time you attempt to modify it, the "assert" {@link android.content.ContentProviderOperation} will fail, and the entire batch of operations will be backed out. You can then choose to retry the batch or take some other action.
The following snippet demonstrates how to create an "assert" {@link android.content.ContentProviderOperation} after querying for a single raw contact using a {@link android.content.CursorLoader}:
/* * The application uses CursorLoader to query the raw contacts table. The system calls this method * when the load is finished. */ public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { // Gets the raw contact's _ID and VERSION values mRawContactID = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); mVersion = cursor.getInt(cursor.getColumnIndex(SyncColumns.VERSION)); } ... // Sets up a Uri for the assert operation Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, mRawContactID); // Creates a builder for the assert operation ContentProviderOperation.Builder assertOp = ContentProviderOperation.netAssertQuery(rawContactUri); // Adds the assertions to the assert operation: checks the version and count of rows tested assertOp.withValue(SyncColumns.VERSION, mVersion); assertOp.withExpectedCount(1); // Creates an ArrayList to hold the ContentProviderOperation objects ArrayList ops = new ArrayList<ContentProviderOperationg>; ops.add(assertOp.build()); // You would add the rest of your batch operations to "ops" here ... // Applies the batch. If the assert fails, an Exception is thrown try { ContentProviderResult[] results = getContentResolver().applyBatch(AUTHORITY, ops); } catch (OperationApplicationException e) { // Actions you want to take if the assert operation fails go here }
Sending an intent to the device's contacts application allows you to access the Contacts Provider indirectly. The intent starts the device's contacts application UI, in which users can do contacts-related work. With this type of access, users can:
If the user is inserting or updating data, you can collect the data first and send it as part of the intent.
When you use intents to access the Contacts Provider via the device's contacts application, you don't have to write your own UI or code for accessing the provider. You also don't have to request permission to read or write to the provider. The device's contacts application can delegate read permission for a contact to you, and because you're making modifications to the provider through another application, you don't have to have write permissions.
The general process of sending an intent to access a provider is described in detail in the Content Provider Basics guide in the section "Data access via intents." The action, MIME type, and data values you use for the available tasks are summarized in Table 4, while the extras values you can use with {@link android.content.Intent#putExtra(String, String) putExtra()} are listed in the reference documentation for {@link android.provider.ContactsContract.Intents.Insert}:
Task | Action | Data | MIME type | Notes |
---|---|---|---|---|
Pick a contact from a list | {@link android.content.Intent#ACTION_PICK} |
One of:
|
Not used |
Displays a list of raw contacts or a list of data from a raw contact, depending on the
content URI type you supply.
Call
{@link android.app.Activity#startActivityForResult(Intent, int) startActivityForResult()},
which returns the content URI of the selected row. The form of the URI is the
table's content URI with the row's |
Insert a new raw contact | {@link android.provider.ContactsContract.Intents.Insert#ACTION Insert.ACTION} | N/A | {@link android.provider.ContactsContract.RawContacts#CONTENT_TYPE RawContacts.CONTENT_TYPE}, MIME type for a set of raw contacts. | Displays the device's contacts application's Add Contact screen. The extras values you add to the intent are displayed. If sent with {@link android.app.Activity#startActivityForResult(Intent, int) startActivityForResult()}, the content URI of the newly-added raw contact is passed back to your activity's {@link android.app.Activity#onActivityResult(int, int, Intent) onActivityResult()} callback method in the {@link android.content.Intent} argument, in the "data" field. To get the value, call {@link android.content.Intent#getData()}. |
Edit a contact | {@link android.content.Intent#ACTION_EDIT} | {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI} for the contact. The editor activity will allow the user to edit any of the data associated with this contact. | {@link android.provider.ContactsContract.Contacts#CONTENT_ITEM_TYPE Contacts.CONTENT_ITEM_TYPE}, a single contact. | Displays the Edit Contact screen in the contacts application. The extras values you add to the intent are displayed. When the user clicks Done to save the edits, your activity returns to the foreground. |
Display a picker that can also add data. | {@link android.content.Intent#ACTION_INSERT_OR_EDIT} | N/A | {@link android.provider.ContactsContract.Contacts#CONTENT_ITEM_TYPE} |
This intent always displays the contacts app's picker screen. The user can either
pick a contact to edit, or add a new contact. Either the edit or the add screen
appears, depending on the user's choice, and the extras data you pass in the intent
is displayed. If your app displays contact data such as an email or phone number, use
this intent to allow the user to add the data to an existing contact.
contact,
Note: There's no need to send a name value in this intent's extras, because the user always picks an existing name or adds a new one. Moreover, if you send a name, and the user chooses to do an edit, the contacts app will display the name you send, overwriting the previous value. If the user doesn't notice this and saves the edit, the old value is lost. |
The device's contacts app doesn't allow you to delete a raw contact or any of its data with an intent. Instead, to delete a raw contact, use {@link android.content.ContentResolver#delete(Uri, String, String[]) ContentResolver.delete()} or {@link android.content.ContentProviderOperation#newDelete(Uri) ContentProviderOperation.newDelete()}.
The following snippet shows how to construct and send an intent that inserts a new raw contact and data:
// Gets values from the UI String name = mContactNameEditText.getText().toString(); String phone = mContactPhoneEditText.getText().toString(); String email = mContactEmailEditText.getText().toString(); String company = mCompanyName.getText().toString(); String jobtitle = mJobTitle.getText().toString(); // Creates a new intent for sending to the device's contacts application Intent insertIntent = new Intent(ContactsContract.Intents.Insert.ACTION); // Sets the MIME type to the one expected by the insertion activity insertIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE); // Sets the new contact name insertIntent.putExtra(ContactsContract.Intents.Insert.NAME, name); // Sets the new company and job title insertIntent.putExtra(ContactsContract.Intents.Insert.COMPANY, company); insertIntent.putExtra(ContactsContract.Intents.Insert.JOB_TITLE, jobtitle); /* * Demonstrates adding data rows as an array list associated with the DATA key */ // Defines an array list to contain the ContentValues objects for each row ArrayList<ContentValues> contactData = new ArrayList<ContentValues>(); /* * Defines the raw contact row */ // Sets up the row as a ContentValues object ContentValues rawContactRow = new ContentValues(); // Adds the account type and name to the row rawContactRow.put(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType()); rawContactRow.put(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName()); // Adds the row to the array contactData.add(rawContactRow); /* * Sets up the phone number data row */ // Sets up the row as a ContentValues object ContentValues phoneRow = new ContentValues(); // Specifies the MIME type for this data row (all data rows must be marked by their type) phoneRow.put( ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE ); // Adds the phone number and its type to the row phoneRow.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone); // Adds the row to the array contactData.add(phoneRow); /* * Sets up the email data row */ // Sets up the row as a ContentValues object ContentValues emailRow = new ContentValues(); // Specifies the MIME type for this data row (all data rows must be marked by their type) emailRow.put( ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ); // Adds the email address and its type to the row emailRow.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email); // Adds the row to the array contactData.add(emailRow); /* * Adds the array to the intent's extras. It must be a parcelable object in order to * travel between processes. The device's contacts app expects its key to be * Intents.Insert.DATA */ insertIntent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, contactData); // Send out the intent to start the device's contacts app in its add contact activity. startActivity(insertIntent);
Because the contacts repository contains important and sensitive data that users expect to be correct and up-to-date, the Contacts Provider has well-defined rules for data integrity. It's your responsibility to conform to these rules when you modify contacts data. The important rules are listed here:
By creating and using your own custom MIME types, you can insert, edit, delete, and retrieve your own data rows in the {@link android.provider.ContactsContract.Data} table. Your rows are limited to using the column defined in {@link android.provider.ContactsContract.DataColumns}, although you can map your own type-specific column names to the default column names. In the device's contacts application, the data for your rows is displayed but can't be edited or deleted, and users can't add additional data. To allow users to modify your custom data rows, you must provide an editor activity in your own application.
To display your custom data, provide a contacts.xml
file containing a
<ContactsAccountType>
element and one or more of its
<ContactsDataKind>
child elements. This is described in more detail in the
section <ContactsDataKind> element
.
To learn more about custom MIME types, read the Creating a Content Provider guide.
The Contacts Provider is specifically designed for handling synchronization of contacts data between a device and an online service. This allows users to download existing data to a new device and upload existing data to a new account. Synchronization also ensures that users have the latest data at hand, regardless of the source of additions and changes. Another advantage of synchronization is that it makes contacts data available even when the device is not connected to the network.
Although you can implement synchronization in a variety of ways, the Android system provides a plug-in synchronization framework that automates the following tasks:
To use this framework, you supply a sync adapter plug-in. Each sync adapter is unique to a service and content provider, but can handle multiple account names for the same service. The framework also allows multiple sync adapters for the same service and provider.
You implement a sync adapter as a subclass of {@link android.content.AbstractThreadedSyncAdapter} and install it as part of an Android application. The system learns about the sync adapter from elements in your application manifest, and from a special XML file pointed to by the manifest. The XML file defines the account type for the online service and the authority for the content provider, which together uniquely identify the adapter. The sync adapter does not become active until the user adds an account for the sync adapter's account type and enables synchronization for the content provider the sync adapter syncs with. At that point, the system starts managing the adapter, calling it as necessary to synchronize between the content provider and the server.
Note: Using an account type as part of the sync adapter's identification allows
the system to detect and group together sync adapters that access different services from the
same organization. For example, sync adapters for Google online services all have the same
account type com.google
. When users add a Google account to their devices, all
of the installed sync adapters for Google services are listed together; each sync adapter
listed syncs with a different content provider on the device.
Because most services require users to verify their identity before accessing data, the Android system offers an authentication framework that is similar to, and often used in conjunction with, the sync adapter framework. The authentication framework uses plug-in authenticators that are subclasses of {@link android.accounts.AbstractAccountAuthenticator}. An authenticator verifies the user's identity in the following steps:
If the service accepts the credentials, the authenticator can store the credentials for later use. Because of the plug-in authenticator framework, the {@link android.accounts.AccountManager} can provide access to any authtokens an authenticator supports and chooses to expose, such as OAuth2 authtokens.
Although authentication is not required, most contacts services use it. However, you're not required to use the Android authentication framework to do authentication.
To implement a sync adapter for the Contacts Provider, you start by creating an Android application that contains the following:
In the
Sample Sync Adapter sample app, the class name of this service is
com.example.android.samplesync.syncadapter.SyncService
.
In the
Sample Sync Adapter sample app, the sync adapter is defined in the class
com.example.android.samplesync.syncadapter.SyncAdapter
.
In the
Sample Sync Adapter sample app, the class name of this service is
com.example.android.samplesync.authenticator.AuthenticationService
.
In the
Sample Sync Adapter sample app, the authenticator is defined in the class
com.example.android.samplesync.authenticator.Authenticator
.
<service>
elements in the application manifest. These elements
contain
<meta-data>
child elements that provide specific data to the
system:
<meta-data>
element for the sync adapter service points to the
XML file res/xml/syncadapter.xml
. In turn, this file specifies
a URI for the web service that will be synchronized with the Contacts Provider,
and an account type for the web service.
<meta-data>
element for the authenticator points to the XML file
res/xml/authenticator.xml
. In turn, this file specifies the
account type that this authenticator supports, as well as UI resources that
appear during the authentication process. The account type specified in this
element must be the same as the account type specified for the sync
adapter.
The {@link android.provider.ContactsContract.StreamItems} and {@link android.provider.ContactsContract.StreamItemPhotos} tables manage incoming data from social networks. You can write a sync adapter that adds stream data from your own network to these tables, or you can read stream data from these tables and display it in your own application, or both. With these features, your social networking services and applications can be integrated into Android's social networking experience.
Stream items are always associated with a raw contact. The
{@link android.provider.ContactsContract.StreamItemsColumns#RAW_CONTACT_ID} links to the
_ID
value for the raw contact. The account type and account name of the raw
contact are also stored in the stream item row.
Store the data from your stream in the following columns:
To display identifying information for your stream items, use the {@link android.provider.ContactsContract.StreamItemsColumns#RES_ICON}, {@link android.provider.ContactsContract.StreamItemsColumns#RES_LABEL}, and {@link android.provider.ContactsContract.StreamItemsColumns#RES_PACKAGE} to link to resources in your application.
The {@link android.provider.ContactsContract.StreamItems} table also contains the columns {@link android.provider.ContactsContract.StreamItemsColumns#SYNC1} through {@link android.provider.ContactsContract.StreamItemsColumns#SYNC4} for the exclusive use of sync adapters.
The {@link android.provider.ContactsContract.StreamItemPhotos} table stores photos associated with a stream item. The table's {@link android.provider.ContactsContract.StreamItemPhotosColumns#STREAM_ITEM_ID} column links to values in the {@link android.provider.BaseColumns#_ID} column of {@link android.provider.ContactsContract.StreamItems} table. Photo references are stored in the table in these columns:
These tables work the same as the other main tables in the Contacts Provider, except that:
null
. The query
returns a Cursor containing a single row, with the single column
{@link android.provider.ContactsContract.StreamItems#MAX_ITEMS}.
The class {@link android.provider.ContactsContract.StreamItems.StreamItemPhotos} defines a sub-table of {@link android.provider.ContactsContract.StreamItemPhotos} containing the photo rows for a single stream item.
The social stream data managed by the Contacts Provider, in conjunction with the device's contacts application, offers a powerful way to connect your social networking system with existing contacts. The following features are available:
Regular synchronization of stream items with the Contacts Provider is the same as other synchronizations. To learn more about synchronization, see the section Contacts Provider Sync Adapters. Registering notifications and inviting contacts are covered in the next two sections.
To register your sync adapter to receive notifications when the user views a contact that's managed by your sync adapter:
contacts.xml
in your project's res/xml/
directory. If you already have this file, you can skip this step.
<ContactsAccountType xmlns:android="http://schemas.android.com/apk/res/android">
.
If this element already exists, you can skip this step.
viewContactNotifyService="serviceclass"
to the element, where
serviceclass
is the fully-qualified classname of the service
that should receive the intent from the device's contacts application. For the notifier
service, use a class that extends {@link android.app.IntentService}, to allow the service to
receive intents. The data in the incoming intent contains the content URI of the raw
contact the user clicked. From the notifier service, you can bind to and then call your
sync adapter to update the data for the raw contact.
To register an activity to be called when the user clicks on a stream item or photo or both:
contacts.xml
in your project's res/xml/
directory. If you already have this file, you can skip this step.
<ContactsAccountType xmlns:android="http://schemas.android.com/apk/res/android">
.
If this element already exists, you can skip this step.
viewStreamItemActivity="activityclass"
to the element, where
activityclass
is the fully-qualified classname of the activity
that should receive the intent from the device's contacts application.
viewStreamItemPhotoActivity="activityclass"
to the element, where
activityclass
is the fully-qualified classname of the activity
that should receive the intent from the device's contacts application.
The <ContactsAccountType>
element is described in more detail in the
section <ContactsAccountType> element.
The incoming intent contains the content URI of the item or photo that the user clicked. To have separate activities for text items and for photos, use both attributes in the same file.
Users don't have to leave the device's contacts application to invite a contact to your social networking site. Instead, you can have the device's contacts app send an intent for inviting the contact to one of your activities. To set this up:
contacts.xml
in your project's res/xml/
directory. If you already have this file, you can skip this step.
<ContactsAccountType xmlns:android="http://schemas.android.com/apk/res/android">
.
If this element already exists, you can skip this step.
inviteContactActivity="activityclass"
inviteContactActionLabel="@string/invite_action_label"
activityclass
value is the fully-qualified classname of the
activity that should receive the intent. The invite_action_label
value is a text string that's displayed in the Add Connection menu in the
device's contacts application.
Note: ContactsSource
is a deprecated tag name for
ContactsAccountType
.
The file contacts.xml
contains XML elements that control the interaction of your
sync adapter and application with the contacts application and the Contacts Provider. These
elements are described in the following sections.
The <ContactsAccountType>
element controls the interaction of your
application with the contacts application. It has the following syntax:
<ContactsAccountType xmlns:android="http://schemas.android.com/apk/res/android" inviteContactActivity="activity_name" inviteContactActionLabel="invite_command_text" viewContactNotifyService="view_notify_service" viewGroupActivity="group_view_activity" viewGroupActionLabel="group_action_text" viewStreamItemActivity="viewstream_activity_name" viewStreamItemPhotoActivity="viewphotostream_activity_name">
contained in:
res/xml/contacts.xml
can contain:
<ContactsDataKind>
Description:
Declares Android components and UI labels that allow users to invite one of their contacts to a social network, notify users when one of their social networking streams is updated, and so forth.
Notice that the attribute prefix android:
is not necessary for the attributes
of <ContactsAccountType>
.
Attributes:
NotifierService.java
file in the
SampleSyncAdapter
sample app.
For example, if you install the Google+ application on your device and you sync Google+ with the contacts application, you'll see Google+ circles listed as groups in your contacts application's Groups tab. If you click on a Google+ circle, you'll see people in that circle listed as a "group". At the top of the display, you'll see a Google+ icon; if you click it, control switches to the Google+ app. The contacts application does this with the {@code viewGroupActivity}, using the Google+ icon as the value of {@code viewGroupActionLabel}.
A string resource identifier is allowed for this attribute.
The <ContactsDataKind>
element controls the display of your application's
custom data rows in the contacts application's UI. It has the following syntax:
<ContactsDataKind android:mimeType="MIMEtype" android:icon="icon_resources" android:summaryColumn="column_name" android:detailColumn="column_name">
contained in:
<ContactsAccountType>
Description:
Use this element to have the contacts application display the contents of a custom data row as
part of the details of a raw contact. Each <ContactsDataKind>
child element
of <ContactsAccountType>
represents a type of custom data row that your sync
adapter adds to the {@link android.provider.ContactsContract.Data} table. Add one
<ContactsDataKind>
element for each custom MIME type you use. You don't have
to add the element if you have a custom data row for which you don't want to display data.
Attributes:
vnd.android.cursor.item/vnd.example.locationstatus
could be a custom
MIME type for a data row that records a contact's last known location.
Besides the main features described in previous sections, the Contacts Provider offers these useful features for working with contacts data:
The Contacts Provider can optionally label collections of related contacts with group data. If the server associated with a user account wants to maintain groups, the sync adapter for the account's account type should transfer groups data between the Contacts Provider and the server. When users add a new contact to the server and then put this contact in a new group, the sync adapter must add the new group to the {@link android.provider.ContactsContract.Groups} table. The group or groups a raw contact belongs to are stored in the {@link android.provider.ContactsContract.Data} table, using the {@link android.provider.ContactsContract.CommonDataKinds.GroupMembership} MIME type.
If you're designing a sync adapter that will add raw contact data from server to the Contacts Provider, and you aren't using groups, then you need to tell the Provider to make your data visible. In the code that is executed when a user adds an account to the device, update the {@link android.provider.ContactsContract.Settings} row that the Contacts Provider adds for the account. In this row, set the value of the {@link android.provider.ContactsContract.SettingsColumns#UNGROUPED_VISIBLE Settings.UNGROUPED_VISIBLE} column to 1. When you do this, the Contacts Provider will always make your contacts data visible, even if you don't use groups.
The {@link android.provider.ContactsContract.Data} table stores photos as rows with MIME type {@link android.provider.ContactsContract.CommonDataKinds.Photo#CONTENT_ITEM_TYPE Photo.CONTENT_ITEM_TYPE}. The row's {@link android.provider.ContactsContract.RawContactsColumns#CONTACT_ID} column is linked to the {@link android.provider.BaseColumns#_ID} column of the raw contact to which it belongs. The class {@link android.provider.ContactsContract.Contacts.Photo} defines a sub-table of {@link android.provider.ContactsContract.Contacts} containing photo information for a contact's primary photo, which is the primary photo of the contact's primary raw contact. Similarly, the class {@link android.provider.ContactsContract.RawContacts.DisplayPhoto} defines a sub-table of {@link android.provider.ContactsContract.RawContacts} containing photo information for a raw contact's primary photo.
The reference documentation for {@link android.provider.ContactsContract.Contacts.Photo} and {@link android.provider.ContactsContract.RawContacts.DisplayPhoto} contain examples of retrieving photo information. There is no convenience class for retrieving the primary thumbnail for a raw contact, but you can send a query to the {@link android.provider.ContactsContract.Data} table, selecting on the raw contact's {@link android.provider.BaseColumns#_ID}, the {@link android.provider.ContactsContract.CommonDataKinds.Photo#CONTENT_ITEM_TYPE Photo.CONTENT_ITEM_TYPE}, and the {@link android.provider.ContactsContract.Data#IS_PRIMARY} column to find the raw contact's primary photo row.
Social stream data for a person may also include photos. These are stored in the {@link android.provider.ContactsContract.StreamItemPhotos} table, which is described in more detail in the section Social stream photos.