Expose contacts as a People Directory

Implementing a People Directory in your VoIP or communication app lets user identities and reachability capabilities synchronize with the People Provider and appear in system surfaces like dialers. To expose your contacts to the People Provider ecosystem, complete the following tasks:

  1. Declare the People Directory ContentProvider in your manifest.
  2. Implement query handling for persons and, optionally, groups.
  3. Synchronize data changes with the People Provider database.
  4. Synchronize full state transitions.
  5. Handle incoming interaction intents.

Declare the directory in your manifest

To let the system discover your directory during package scanning, declare an exported ContentProvider with the PeopleDirectory property.

  1. In your AndroidManifest.xml file, declare your ContentProvider.
  2. Inside the <provider> element, add a <property> tag with android:name="android.content.PeopleDirectory" set to true.
  3. Add the android:readPermission="android.permission.BIND_DIRECTORY_SEARCH" attribute to restrict access exclusively to the system.
    • If you need to allow access from non-system apps, for example for backward compatibility, you can omit this permission declaration and control the permission programmatically.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.voipapp">
    <application>
        <provider
            android:name=".provider.VoipPeopleDirectoryProvider"
            android:authorities="com.example.voipapp.people"
            android:exported="true"
            android:readPermission="android.permission.BIND_DIRECTORY_SEARCH">
            <property
                android:name="android.content.PeopleDirectory"
                android:value="true" />
        </provider>
    </application>
</manifest>

Implement sub-query handling

When the system queries your directory to synchronize records into its internal database, it passes structured selection criteria in the query Bundle under PeopleContract.QUERY_ARG_QUERIES.

  1. In your provider's query method, inspect the incoming Uri path (/persons or /groups).
  2. Extract the sub-query argument list from the Bundle.
  3. Iterate through each sub-query, and extract QUERY_ARG_SELECTION_TYPE and QUERY_ARG_SELECTION_VALUE.
  4. Search your app's user database for matches to the selection criteria.
  5. Populate a Cursor with standard columns. For example, for persons, include LOOKUP_KEY, DISPLAY_NAME, PHOTO_URI, CAPABILITY, IS_KNOWN, QUERY_ARG_SELECTION_TYPE, and QUERY_ARG_SELECTION_VALUE.

The following example shows how to implement a People Directory ContentProvider in Kotlin:

import android.content.ContentProvider
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import android.os.Bundle
import android.os.CancellationSignal
import android.provider.PeopleContract

class VoipPeopleDirectoryProvider : ContentProvider() {
    override fun query(
        uri: Uri,
        projection: Array<out String>?,
        queryArgs: Bundle?,
        cancellationSignal: CancellationSignal?
    ): Cursor? {
        val cursor = MatrixCursor(
            arrayOf(
                PeopleContract.Persons.LOOKUP_KEY,
                PeopleContract.Persons.DISPLAY_NAME,
                PeopleContract.Persons.PHOTO_URI,
                PeopleContract.Persons.CAPABILITY,
                PeopleContract.Persons.IS_KNOWN,
                PeopleContract.QUERY_ARG_SELECTION_TYPE,
                PeopleContract.QUERY_ARG_SELECTION_VALUE
            )
        )
        val subQueries = queryArgs?.getParcelableArrayList(
            PeopleContract.QUERY_ARG_QUERIES,
            Bundle::class.java
        ) ?: return cursor
        for (request in subQueries) {
            val selectionType = request.getString(PeopleContract.QUERY_ARG_SELECTION_TYPE) ?: continue
            val values = request.getStringArray(PeopleContract.QUERY_ARG_SELECTION_VALUE) ?: continue
            for (targetValue in values) {
                val matchedUser = findUserByCriterion(selectionType, targetValue) ?: continue
                cursor.addRow(
                    arrayOf(
                        matchedUser.lookupKey,
                        matchedUser.displayName,
                        matchedUser.photoUrl,
                        PeopleContract.Capability.VOICE_REACHABILITY or PeopleContract.Capability.VIDEO_REACHABILITY,
                        if (matchedUser.isContact) 1 else 0,
                        selectionType,
                        targetValue
                    )
                )
            }
        }
        return cursor
    }

    private fun findUserByCriterion(selectionType: String, value: String): UserRecord? {
        // Query your app's local user database
        return null
    }

    override fun onCreate(): Boolean = true
    override fun getType(uri: Uri): String? = null
    override fun insert(uri: Uri, values: android.content.ContentValues?): Uri? = null
    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
    override fun update(uri: Uri, values: android.content.ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int = 0
}

data class UserRecord(
    val lookupKey: String,
    val displayName: String,
    val photoUrl: String,
    val isContact: Boolean
)

Synchronize incremental data changes

Notify the system whenever user records or reachabilities change so the People Provider schedules a lightweight targeted synchronization:

fun notifyUserUpdated(context: Context, lookupKey: String, phone: String) {
    val queryArgs = PeopleContract.Persons.Query.Builder()
        .addPhoneQuery(context.packageName, listOf(phone))
        .build()
    PeopleContract.Directory.notifyChange(
        context,
        PeopleContract.Persons.CONTENT_URI,
        queryArgs,
        null
    )
}

Synchronize full state transitions

Request a full database refresh only after major events, such as initial account setup or a cloud database restoration. In your sync or setup service, call PeopleContract.Directory.requestRefresh:

fun onAccountSyncCompleted(context: Context) {
    PeopleContract.Directory.requestRefresh(context, null)
}

Handle incoming interaction intents

When a user initiates a call or message from a client app, the system invokes your interaction Activity.

  1. In AndroidManifest.xml, declare an exported Activity with an intent filter for android.provider.action.START_PEOPLE_INTERACTION.
  2. In your Activity.onCreate method, extract EXTRA_PEOPLE_INTERACTION_TYPE and EXTRA_PEOPLE_QUERY_ARGS.
  3. Route the user into the voice, video, or chat interface.

Manifest declaration

Add the interaction activity declaration to your AndroidManifest.xml file and protect it with android:permission="android.permission.BIND_DIRECTORY_SEARCH":

<activity
    android:name=".VoipInteractionActivity"
    android:exported="true"
    android:permission="android.permission.BIND_DIRECTORY_SEARCH">
    <intent-filter>
        <action android:name="android.provider.action.START_PEOPLE_INTERACTION" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Activity implementation

The following example shows how to extract the interaction type and target query arguments in your interaction Activity:

import android.app.Activity
import android.os.Bundle
import android.provider.PeopleContract

class VoipInteractionActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val interactionType = intent.getStringExtra(PeopleContract.Intents.EXTRA_PEOPLE_INTERACTION_TYPE)
        val queryArgs = intent.getBundleExtra(PeopleContract.Intents.EXTRA_PEOPLE_QUERY_ARGS)
        val subQueries = queryArgs?.getParcelableArrayList(
            PeopleContract.QUERY_ARG_QUERIES,
            Bundle::class.java
        )
        val targetQuery = subQueries?.firstOrNull()
        val selectionType = targetQuery?.getString(PeopleContract.QUERY_ARG_SELECTION_TYPE)
        val selectionValues = targetQuery?.getStringArray(PeopleContract.QUERY_ARG_SELECTION_VALUE)
        val targetIdentity = selectionValues?.firstOrNull()
        when (interactionType) {
            PeopleContract.Intents.PEOPLE_INTERACTION_TYPE_VOICE -> startAudioCall(targetIdentity)
            PeopleContract.Intents.PEOPLE_INTERACTION_TYPE_VIDEO -> startVideoCall(targetIdentity)
            PeopleContract.Intents.PEOPLE_INTERACTION_TYPE_MESSAGE -> startChatSession(targetIdentity)
        }
        finish()
    }

    private fun startAudioCall(target: String?) { /* ... */ }
    private fun startVideoCall(target: String?) { /* ... */ }
    private fun startChatSession(target: String?) { /* ... */ }
}

Additional resources