Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 414a5775 authored by moezbhatti's avatar moezbhatti Committed by Moez Bhatti
Browse files

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

Closes #1285
parent b4ef834d
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ class CursorToContactImpl @Inject constructor(
        val URI = Phone.CONTENT_URI
        val PROJECTION = arrayOf(
                Phone.LOOKUP_KEY,
                Phone.ACCOUNT_TYPE_AND_DATA_SET,
                Phone.NUMBER,
                Phone.TYPE,
                Phone.LABEL,
@@ -44,18 +45,20 @@ class CursorToContactImpl @Inject constructor(
        )

        const val COLUMN_LOOKUP_KEY = 0
        const val COLUMN_NUMBER = 1
        const val COLUMN_TYPE = 2
        const val COLUMN_LABEL = 3
        const val COLUMN_DISPLAY_NAME = 4
        const val COLUMN_STARRED = 5
        const val CONTACT_LAST_UPDATED = 6
        const val COLUMN_ACCOUNT_TYPE = 1
        const val COLUMN_NUMBER = 2
        const val COLUMN_TYPE = 3
        const val COLUMN_LABEL = 4
        const val COLUMN_DISPLAY_NAME = 5
        const val COLUMN_STARRED = 6
        const val CONTACT_LAST_UPDATED = 7
    }

    override fun map(from: Cursor) = Contact().apply {
        lookupKey = from.getString(COLUMN_LOOKUP_KEY)
        name = from.getString(COLUMN_DISPLAY_NAME) ?: ""
        numbers.add(PhoneNumber(
                accountType = from.getString(COLUMN_ACCOUNT_TYPE),
                address = from.getString(COLUMN_NUMBER) ?: "",
                type = Phone.getTypeLabel(context.resources, from.getInt(COLUMN_TYPE),
                        from.getString(COLUMN_LABEL)).toString()
+3 −0
Original line number Diff line number Diff line
@@ -127,6 +127,9 @@ class QkRealmMigration : RealmMigration {
            realm.schema.get("Contact")
                    ?.addField("starred", Boolean::class.java, FieldAttribute.REQUIRED)

            realm.schema.get("PhoneNumber")
                    ?.addField("accountType", String::class.java, FieldAttribute.REQUIRED)

            version++
        }

+18 −2
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import com.moez.QKSMS.model.ContactGroup
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message
import com.moez.QKSMS.model.MmsPart
import com.moez.QKSMS.model.PhoneNumber
import com.moez.QKSMS.model.Recipient
import com.moez.QKSMS.model.SyncLog
import com.moez.QKSMS.util.PhoneNumberUtils
@@ -289,10 +290,25 @@ class SyncRepositoryImpl @Inject constructor(
                ?.map { cursor -> cursorToContact.map(cursor) }
                ?.groupBy { contact -> contact.lookupKey }
                ?.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 {
                        numbers.clear()
                        numbers.addAll(allNumbers)
                        numbers.addAll(uniqueNumbers)
                    }
                } ?: listOf()
    }
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ package com.moez.QKSMS.model
import io.realm.RealmObject

open class PhoneNumber(
    var accountType: String = "",
    var address: String = "",
    var type: String = ""
) : RealmObject()
 No newline at end of file