allow SimpleContact contain multiple phone numbers

This commit is contained in:
tibbi 2020-07-12 15:46:31 +02:00
parent 1ee2d9d1cf
commit 3d7142fb5c
4 changed files with 25 additions and 15 deletions

View file

@ -7,7 +7,7 @@ buildscript {
propMinSdkVersion = 21
propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1
propVersionName = '5.29.15'
propVersionName = '5.29.16'
kotlin_version = '1.3.72'
}

View file

@ -3,6 +3,8 @@ package com.simplemobiletools.commons.helpers
import android.content.Context
import android.database.Cursor
import android.net.Uri
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.models.SimpleContact
@ -18,7 +20,7 @@ class MyContactsContentProvider {
const val COL_CONTACT_ID = "contact_id"
const val COL_NAME = "name"
const val COL_PHOTO_URI = "photo_uri"
const val COL_PHONE_NUMBER = "phone_number"
const val COL_PHONE_NUMBERS = "phone_numbers"
fun getSimpleContacts(context: Context, cursor: Cursor?): ArrayList<SimpleContact> {
val contacts = ArrayList<SimpleContact>()
@ -35,8 +37,11 @@ class MyContactsContentProvider {
val contactId = cursor.getIntValue(COL_CONTACT_ID)
val name = cursor.getStringValue(COL_NAME)
val photoUri = cursor.getStringValue(COL_PHOTO_URI)
val phoneNumber = cursor.getStringValue(COL_PHONE_NUMBER)
val contact = SimpleContact(rawId, contactId, name, photoUri, phoneNumber)
val phoneNumbersJson = cursor.getStringValue(COL_PHONE_NUMBERS)
val token = object : TypeToken<ArrayList<String>>() {}.type
val phoneNumbers = Gson().fromJson<ArrayList<String>>(phoneNumbersJson, token) ?: ArrayList()
val contact = SimpleContact(rawId, contactId, name, photoUri, phoneNumbers)
contacts.add(contact)
} while (cursor.moveToNext())
}

View file

@ -43,8 +43,8 @@ class SimpleContactsHelper(val context: Context) {
}
allContacts = allContacts.filter { it.name.isNotEmpty() }.distinctBy {
val startIndex = Math.max(0, it.phoneNumber.length - 9)
it.phoneNumber.substring(startIndex)
val startIndex = Math.max(0, it.phoneNumbers.first().length - 9)
it.phoneNumbers.first().substring(startIndex)
}.distinctBy { it.rawId }.toMutableList() as ArrayList<SimpleContact>
allContacts.sort()
@ -101,7 +101,7 @@ class SimpleContactsHelper(val context: Context) {
}
val fullName = TextUtils.join(" ", names)
val contact = SimpleContact(rawId, contactId, fullName, photoUri, "")
val contact = SimpleContact(rawId, contactId, fullName, photoUri, ArrayList())
contacts.add(contact)
}
}
@ -112,7 +112,7 @@ class SimpleContactsHelper(val context: Context) {
val jobTitle = cursor.getStringValue(Organization.TITLE) ?: ""
if (company.isNotEmpty() || jobTitle.isNotEmpty()) {
val fullName = "$company $jobTitle".trim()
val contact = SimpleContact(rawId, contactId, fullName, photoUri, "")
val contact = SimpleContact(rawId, contactId, fullName, photoUri, ArrayList())
contacts.add(contact)
}
}
@ -138,8 +138,11 @@ class SimpleContactsHelper(val context: Context) {
val rawId = cursor.getIntValue(Data.RAW_CONTACT_ID)
val contactId = cursor.getIntValue(Data.CONTACT_ID)
val contact = SimpleContact(rawId, contactId, "", "", phoneNumber)
contacts.add(contact)
if (contacts.firstOrNull { it.rawId == rawId } == null) {
contacts.add(SimpleContact(rawId, contactId, "", "", ArrayList()))
}
mydebug("number $phoneNumber, $rawId, $contactId")
contacts.firstOrNull { it.rawId == rawId }?.phoneNumbers?.add(phoneNumber)
}
return contacts
}

View file

@ -4,7 +4,7 @@ import android.telephony.PhoneNumberUtils
import com.simplemobiletools.commons.extensions.normalizePhoneNumber
import com.simplemobiletools.commons.extensions.normalizeString
data class SimpleContact(val rawId: Int, val contactId: Int, var name: String, var photoUri: String, var phoneNumber: String) : Comparable<SimpleContact> {
data class SimpleContact(val rawId: Int, val contactId: Int, var name: String, var photoUri: String, var phoneNumbers: ArrayList<String>) : Comparable<SimpleContact> {
override fun compareTo(other: SimpleContact): Int {
val firstString = name.normalizeString()
val secondString = other.name.normalizeString()
@ -27,10 +27,12 @@ data class SimpleContact(val rawId: Int, val contactId: Int, var name: String, v
fun doesContainPhoneNumber(text: String): Boolean {
return if (text.isNotEmpty()) {
val normalizedText = text.normalizePhoneNumber()
PhoneNumberUtils.compare(phoneNumber.normalizePhoneNumber(), normalizedText) ||
phoneNumber.contains(text) ||
phoneNumber.normalizePhoneNumber().contains(normalizedText) ||
phoneNumber.contains(normalizedText)
phoneNumbers.any { phoneNumber ->
PhoneNumberUtils.compare(phoneNumber.normalizePhoneNumber(), normalizedText) ||
phoneNumber.contains(text) ||
phoneNumber.normalizePhoneNumber().contains(normalizedText) ||
phoneNumber.contains(normalizedText)
}
} else {
false
}