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:
- Declare the People Directory
ContentProviderin your manifest. - Implement query handling for persons and, optionally, groups.
- Synchronize data changes with the People Provider database.
- Synchronize full state transitions.
- 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.
- In your
AndroidManifest.xmlfile, declare yourContentProvider. - Inside the
<provider>element, add a<property>tag withandroid:name="android.content.PeopleDirectory"set totrue. - 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.
- In your provider's
querymethod, inspect the incomingUripath (/personsor/groups). - Extract the sub-query argument list from the
Bundle. - Iterate through each sub-query, and extract
QUERY_ARG_SELECTION_TYPEandQUERY_ARG_SELECTION_VALUE. - Search your app's user database for matches to the selection criteria.
- Populate a
Cursorwith standard columns. For example, for persons, includeLOOKUP_KEY,DISPLAY_NAME,PHOTO_URI,CAPABILITY,IS_KNOWN,QUERY_ARG_SELECTION_TYPE, andQUERY_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:
- In your user management service, call
PeopleContract.Directory.notifyChangewhen a contact updates their status or profile.
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.
- In
AndroidManifest.xml, declare an exportedActivitywith an intent filter forandroid.provider.action.START_PEOPLE_INTERACTION. - In your
Activity.onCreatemethod, extractEXTRA_PEOPLE_INTERACTION_TYPEandEXTRA_PEOPLE_QUERY_ARGS. - 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
- About People Directory
- Plan your People Directory integration
- Create a content provider
- Build a calling app