I just barely wrote a tutorial on how to get this working. Check it out here: https://simplenexus.dev/2019/08/27/android-caller-id.html
The basics of how to get this working are:
AndroidManifest.xml
<provider
android:name=".callerid.CallerIDProvider"
android:authorities="@string/callerid_authority"
android:readPermission="android.permission.READ_CONTACTS"
android:enabled="true"
android:exported="true">
<meta-data
android:name="android.content.ContactDirectory"
android:value="true"/></provider>
CallerIDProvider.kt
private var userRepository: UserRepository? = null
private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH)
override fun onCreate(): Boolean {
context?.let {
val userDao = UserDatabase.getDatabase(it).userDao()
userRepository = UserRepository(userDao)
val authority = it.getString(R.string.callerid_authority)
uriMatcher.addURI(authority, "directories", DIRECTORIES)
uriMatcher.addURI(authority, "phone_lookup/*", PHONE_LOOKUP)
}
return true
}
override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? {
when (uriMatcher.match(uri)) {
DIRECTORIES -> {
val label = context?.getString(R.string.app_name) ?: return null
val cursor = MatrixCursor(projection)
projection?.map { column ->
when (column) {
Directory.ACCOUNT_NAME,
Directory.ACCOUNT_TYPE,
Directory.DISPLAY_NAME -> label
Directory.TYPE_RESOURCE_ID -> R.string.app_name
Directory.EXPORT_SUPPORT -> Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY
Directory.SHORTCUT_SUPPORT -> Directory.SHORTCUT_SUPPORT_NONE
else -> null
}
}?.let { cursor.addRow(it) }
return cursor
}
PHONE_LOOKUP -> {
userRepository?.let { userRepo ->
val phoneNumber = uri.pathSegments[1]
val cursor = MatrixCursor(projection)
val user = runBlocking(Dispatchers.IO) { userRepo.getUser(phoneNumber) }
user?.let { u ->
projection?.map { column ->
when (column) {
PhoneLookup._ID -> -1
PhoneLookup.DISPLAY_NAME -> u.fullName
PhoneLookup.LABEL -> u.phoneLabel
else -> null
}
}?.let { cursor.addRow(it) }
}
return cursor
}
}
}
return null
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…