mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-24 21:15:48 +00:00
Refactor more ContactDiscovery code.
This commit is contained in:
committed by
Cody Henthorne
parent
d409278dd5
commit
a32d5bef20
@@ -27,7 +27,7 @@ object SystemContactsRepository {
|
||||
|
||||
private val TAG = Log.tag(SystemContactsRepository::class.java)
|
||||
|
||||
private const val FIELD_FORMATTED_PHONE = ContactsContract.RawContacts.SYNC1
|
||||
private const val FIELD_DISPLAY_PHONE = ContactsContract.RawContacts.SYNC1
|
||||
private const val FIELD_TAG = ContactsContract.Data.SYNC2
|
||||
private const val FIELD_SUPPORTS_VOICE = ContactsContract.RawContacts.SYNC4
|
||||
|
||||
@@ -36,7 +36,7 @@ object SystemContactsRepository {
|
||||
* structured name data.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getAllSystemContacts(context: Context, rewrites: Map<String, String>, e164Formatter: (String) -> String): ContactIterator {
|
||||
fun getAllSystemContacts(context: Context, e164Formatter: (String) -> String): ContactIterator {
|
||||
val uri = ContactsContract.Data.CONTENT_URI
|
||||
val projection = SqlUtil.buildArgs(
|
||||
ContactsContract.Data.MIMETYPE,
|
||||
@@ -56,9 +56,12 @@ object SystemContactsRepository {
|
||||
|
||||
val cursor: Cursor = context.contentResolver.query(uri, projection, where, args, orderBy) ?: return EmptyContactIterator()
|
||||
|
||||
return CursorContactIterator(cursor, rewrites, e164Formatter)
|
||||
return CursorContactIterator(cursor, e164Formatter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all unique display numbers in the system contacts. (By display, we mean not-E164-formatted)
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getAllDisplayNumbers(context: Context): Set<String> {
|
||||
val results: MutableSet<String> = mutableSetOf()
|
||||
@@ -116,14 +119,14 @@ object SystemContactsRepository {
|
||||
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
|
||||
.build()
|
||||
|
||||
val projection = arrayOf(BaseColumns._ID, FIELD_FORMATTED_PHONE)
|
||||
val projection = arrayOf(BaseColumns._ID, FIELD_DISPLAY_PHONE)
|
||||
|
||||
// TODO Could we write this as a single delete(DELETED = true)?
|
||||
context.contentResolver.query(currentContactsUri, projection, "${ContactsContract.RawContacts.DELETED} = ?", SqlUtil.buildArgs(1), null)?.use { cursor ->
|
||||
while (cursor.moveToNext()) {
|
||||
val rawContactId = cursor.requireLong(BaseColumns._ID)
|
||||
|
||||
Log.i(TAG, "Deleting raw contact: ${cursor.requireString(FIELD_FORMATTED_PHONE)}, $rawContactId")
|
||||
Log.i(TAG, "Deleting raw contact: ${cursor.requireString(FIELD_DISPLAY_PHONE)}, $rawContactId")
|
||||
context.contentResolver.delete(currentContactsUri, "${ContactsContract.RawContacts._ID} = ?", SqlUtil.buildArgs(rawContactId))
|
||||
}
|
||||
}
|
||||
@@ -375,7 +378,7 @@ object SystemContactsRepository {
|
||||
ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
|
||||
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, linkConfig.account.name)
|
||||
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, linkConfig.account.type)
|
||||
.withValue(FIELD_FORMATTED_PHONE, systemContactInfo.formattedPhone)
|
||||
.withValue(FIELD_DISPLAY_PHONE, systemContactInfo.displayPhone)
|
||||
.withValue(FIELD_SUPPORTS_VOICE, true.toString())
|
||||
.build(),
|
||||
|
||||
@@ -388,7 +391,7 @@ object SystemContactsRepository {
|
||||
ContentProviderOperation.newInsert(dataUri)
|
||||
.withValueBackReference(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, operationIndex)
|
||||
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
|
||||
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, systemContactInfo.formattedPhone)
|
||||
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, systemContactInfo.displayPhone)
|
||||
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, systemContactInfo.type)
|
||||
.withValue(FIELD_TAG, linkConfig.syncTag)
|
||||
.build(),
|
||||
@@ -396,18 +399,18 @@ object SystemContactsRepository {
|
||||
ContentProviderOperation.newInsert(dataUri)
|
||||
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, operationIndex)
|
||||
.withValue(ContactsContract.Data.MIMETYPE, linkConfig.messageMimetype)
|
||||
.withValue(ContactsContract.Data.DATA1, systemContactInfo.formattedPhone)
|
||||
.withValue(ContactsContract.Data.DATA1, systemContactInfo.displayPhone)
|
||||
.withValue(ContactsContract.Data.DATA2, linkConfig.appName)
|
||||
.withValue(ContactsContract.Data.DATA3, linkConfig.messagePrompt(systemContactInfo.formattedPhone))
|
||||
.withValue(ContactsContract.Data.DATA3, linkConfig.messagePrompt(systemContactInfo.displayPhone))
|
||||
.withYieldAllowed(true)
|
||||
.build(),
|
||||
|
||||
ContentProviderOperation.newInsert(dataUri)
|
||||
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, operationIndex)
|
||||
.withValue(ContactsContract.Data.MIMETYPE, linkConfig.callMimetype)
|
||||
.withValue(ContactsContract.Data.DATA1, systemContactInfo.formattedPhone)
|
||||
.withValue(ContactsContract.Data.DATA1, systemContactInfo.displayPhone)
|
||||
.withValue(ContactsContract.Data.DATA2, linkConfig.appName)
|
||||
.withValue(ContactsContract.Data.DATA3, linkConfig.callPrompt(systemContactInfo.formattedPhone))
|
||||
.withValue(ContactsContract.Data.DATA3, linkConfig.callPrompt(systemContactInfo.displayPhone))
|
||||
.withYieldAllowed(true)
|
||||
.build(),
|
||||
|
||||
@@ -440,7 +443,7 @@ object SystemContactsRepository {
|
||||
.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type).build()
|
||||
val projection = arrayOf(
|
||||
BaseColumns._ID,
|
||||
FIELD_FORMATTED_PHONE,
|
||||
FIELD_DISPLAY_PHONE,
|
||||
FIELD_SUPPORTS_VOICE,
|
||||
ContactsContract.RawContacts.CONTACT_ID,
|
||||
ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY,
|
||||
@@ -451,10 +454,10 @@ object SystemContactsRepository {
|
||||
|
||||
context.contentResolver.query(currentContactsUri, projection, null, null, null)?.use { cursor ->
|
||||
while (cursor.moveToNext()) {
|
||||
val formattedPhone = cursor.requireString(FIELD_FORMATTED_PHONE)
|
||||
val displayPhone = cursor.requireString(FIELD_DISPLAY_PHONE)
|
||||
|
||||
if (formattedPhone != null) {
|
||||
val e164 = e164Formatter(formattedPhone)
|
||||
if (displayPhone != null) {
|
||||
val e164 = e164Formatter(displayPhone)
|
||||
|
||||
contactsDetails[e164] = LinkedContactDetails(
|
||||
id = cursor.requireLong(BaseColumns._ID),
|
||||
@@ -489,7 +492,7 @@ object SystemContactsRepository {
|
||||
if (idCursor.moveToNext()) {
|
||||
return SystemContactInfo(
|
||||
displayName = contactCursor.requireString(ContactsContract.PhoneLookup.DISPLAY_NAME),
|
||||
formattedPhone = systemNumber,
|
||||
displayPhone = systemNumber,
|
||||
rawContactId = idCursor.requireLong(ContactsContract.RawContacts._ID),
|
||||
type = contactCursor.requireInt(ContactsContract.PhoneLookup.TYPE)
|
||||
)
|
||||
@@ -559,7 +562,6 @@ object SystemContactsRepository {
|
||||
*/
|
||||
private class CursorContactIterator(
|
||||
private val cursor: Cursor,
|
||||
private val e164Rewrites: Map<String, String>,
|
||||
private val e164Formatter: (String) -> String
|
||||
) : ContactIterator {
|
||||
|
||||
@@ -599,17 +601,14 @@ object SystemContactsRepository {
|
||||
val phoneDetails: MutableList<ContactPhoneDetails> = mutableListOf()
|
||||
|
||||
while (!cursor.isAfterLast && lookupKey == cursor.getLookupKey() && cursor.isPhoneMimeType()) {
|
||||
val formattedNumber: String? = cursor.requireString(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
||||
|
||||
if (formattedNumber != null && formattedNumber.isNotEmpty()) {
|
||||
val e164: String = e164Formatter(formattedNumber)
|
||||
val realE164: String = firstNonEmpty(e164Rewrites[e164], e164)
|
||||
val displayNumber: String? = cursor.requireString(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
||||
|
||||
if (displayNumber != null && displayNumber.isNotEmpty()) {
|
||||
phoneDetails += ContactPhoneDetails(
|
||||
contactUri = ContactsContract.Contacts.getLookupUri(cursor.requireLong(ContactsContract.CommonDataKinds.Phone._ID), lookupKey),
|
||||
displayName = cursor.requireString(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME),
|
||||
photoUri = cursor.requireString(ContactsContract.CommonDataKinds.Phone.PHOTO_URI),
|
||||
number = realE164,
|
||||
number = e164Formatter(displayNumber),
|
||||
type = cursor.requireInt(ContactsContract.CommonDataKinds.Phone.TYPE),
|
||||
label = cursor.requireString(ContactsContract.CommonDataKinds.Phone.LABEL),
|
||||
)
|
||||
@@ -717,7 +716,7 @@ object SystemContactsRepository {
|
||||
|
||||
private data class SystemContactInfo(
|
||||
val displayName: String?,
|
||||
val formattedPhone: String,
|
||||
val displayPhone: String,
|
||||
val rawContactId: Long,
|
||||
val type: Int
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user