Filter out duplicate phone number entries that come from multiple contacts providers

Closes #1285
This commit is contained in:
moezbhatti
2019-10-31 01:19:47 -04:00
committed by Moez Bhatti
parent b4ef834df0
commit 414a5775bc
4 changed files with 31 additions and 8 deletions
@@ -35,6 +35,7 @@ class CursorToContactImpl @Inject constructor(
val URI = Phone.CONTENT_URI val URI = Phone.CONTENT_URI
val PROJECTION = arrayOf( val PROJECTION = arrayOf(
Phone.LOOKUP_KEY, Phone.LOOKUP_KEY,
Phone.ACCOUNT_TYPE_AND_DATA_SET,
Phone.NUMBER, Phone.NUMBER,
Phone.TYPE, Phone.TYPE,
Phone.LABEL, Phone.LABEL,
@@ -44,18 +45,20 @@ class CursorToContactImpl @Inject constructor(
) )
const val COLUMN_LOOKUP_KEY = 0 const val COLUMN_LOOKUP_KEY = 0
const val COLUMN_NUMBER = 1 const val COLUMN_ACCOUNT_TYPE = 1
const val COLUMN_TYPE = 2 const val COLUMN_NUMBER = 2
const val COLUMN_LABEL = 3 const val COLUMN_TYPE = 3
const val COLUMN_DISPLAY_NAME = 4 const val COLUMN_LABEL = 4
const val COLUMN_STARRED = 5 const val COLUMN_DISPLAY_NAME = 5
const val CONTACT_LAST_UPDATED = 6 const val COLUMN_STARRED = 6
const val CONTACT_LAST_UPDATED = 7
} }
override fun map(from: Cursor) = Contact().apply { override fun map(from: Cursor) = Contact().apply {
lookupKey = from.getString(COLUMN_LOOKUP_KEY) lookupKey = from.getString(COLUMN_LOOKUP_KEY)
name = from.getString(COLUMN_DISPLAY_NAME) ?: "" name = from.getString(COLUMN_DISPLAY_NAME) ?: ""
numbers.add(PhoneNumber( numbers.add(PhoneNumber(
accountType = from.getString(COLUMN_ACCOUNT_TYPE),
address = from.getString(COLUMN_NUMBER) ?: "", address = from.getString(COLUMN_NUMBER) ?: "",
type = Phone.getTypeLabel(context.resources, from.getInt(COLUMN_TYPE), type = Phone.getTypeLabel(context.resources, from.getInt(COLUMN_TYPE),
from.getString(COLUMN_LABEL)).toString() from.getString(COLUMN_LABEL)).toString()
@@ -127,6 +127,9 @@ class QkRealmMigration : RealmMigration {
realm.schema.get("Contact") realm.schema.get("Contact")
?.addField("starred", Boolean::class.java, FieldAttribute.REQUIRED) ?.addField("starred", Boolean::class.java, FieldAttribute.REQUIRED)
realm.schema.get("PhoneNumber")
?.addField("accountType", String::class.java, FieldAttribute.REQUIRED)
version++ version++
} }
@@ -37,6 +37,7 @@ import com.moez.QKSMS.model.ContactGroup
import com.moez.QKSMS.model.Conversation import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message import com.moez.QKSMS.model.Message
import com.moez.QKSMS.model.MmsPart import com.moez.QKSMS.model.MmsPart
import com.moez.QKSMS.model.PhoneNumber
import com.moez.QKSMS.model.Recipient import com.moez.QKSMS.model.Recipient
import com.moez.QKSMS.model.SyncLog import com.moez.QKSMS.model.SyncLog
import com.moez.QKSMS.util.PhoneNumberUtils import com.moez.QKSMS.util.PhoneNumberUtils
@@ -289,10 +290,25 @@ class SyncRepositoryImpl @Inject constructor(
?.map { cursor -> cursorToContact.map(cursor) } ?.map { cursor -> cursorToContact.map(cursor) }
?.groupBy { contact -> contact.lookupKey } ?.groupBy { contact -> contact.lookupKey }
?.map { contacts -> ?.map { contacts ->
val allNumbers = contacts.value.map { it.numbers }.flatten() // Sometimes, contacts providers on the phone will create duplicate phone number entries. This
// commonly happens with Whatsapp. Let's try to detect these duplicate entries and filter them out
val uniqueNumbers = mutableListOf<PhoneNumber>()
contacts.value
.flatMap { it.numbers }
.sortedBy { it.accountType }
.forEach { number ->
val duplicate = uniqueNumbers.any { other ->
number.accountType != other.accountType
&& phoneNumberUtils.compare(number.address, other.address)
}
if (!duplicate) {
uniqueNumbers += number
}
}
contacts.value.first().apply { contacts.value.first().apply {
numbers.clear() numbers.clear()
numbers.addAll(allNumbers) numbers.addAll(uniqueNumbers)
} }
} ?: listOf() } ?: listOf()
} }
@@ -21,6 +21,7 @@ package com.moez.QKSMS.model
import io.realm.RealmObject import io.realm.RealmObject
open class PhoneNumber( open class PhoneNumber(
var accountType: String = "",
var address: String = "", var address: String = "",
var type: String = "" var type: String = ""
) : RealmObject() ) : RealmObject()