Fixed presence storage in native contacts + prevent duplicated SIP addresses in same contact + improved contact fetching used memory

This commit is contained in:
Sylvain Berfini 2022-06-08 15:39:00 +02:00
parent 6222758126
commit ed03a721a5
4 changed files with 38 additions and 49 deletions

View file

@ -119,7 +119,6 @@ class ContactsSettingsFragment : GenericSettingFragment<SettingsContactsFragment
if (granted) {
Log.i("[Contacts Settings] WRITE_CONTACTS permission granted")
corePreferences.storePresenceInNativeContact = true
coreContext.contactsManager.storePresenceInformationForAllContacts()
} else {
Log.w("[Contacts Settings] WRITE_CONTACTS permission denied")
}

View file

@ -38,10 +38,7 @@ import kotlinx.coroutines.withContext
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
import org.linphone.core.Factory
import org.linphone.core.Friend
import org.linphone.core.GlobalState
import org.linphone.core.SubscribePolicy
import org.linphone.core.*
import org.linphone.core.tools.Log
import org.linphone.utils.PhoneNumberUtils
@ -76,7 +73,7 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
projection,
selection,
null,
null
ContactsContract.Data.CONTACT_ID + " ASC"
)
}
@ -101,7 +98,9 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
withContext(Dispatchers.IO) {
try {
// Cursor can be null now that we are on a different dispatcher according to Crashlytics
val friendsPhoneNumbers = HashMap<String, List<String>>()
val friendsPhoneNumbers = arrayListOf<String>()
val friendsAddresses = arrayListOf<Address>()
var previousId = ""
while (cursor != null && !cursor.isClosed && cursor.moveToNext()) {
try {
val id: String =
@ -123,6 +122,12 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
val lookupKey =
cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY))
if (previousId.isEmpty() || previousId != id) {
friendsPhoneNumbers.clear()
friendsAddresses.clear()
previousId = id
}
val friend = friends[id] ?: core.createFriend()
friend.refKey = id
if (friend.name.isNullOrEmpty()) {
@ -143,9 +148,6 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
friend.incSubscribePolicy = SubscribePolicy.SPDeny
}
if (!friendsPhoneNumbers.containsKey(id)) {
friendsPhoneNumbers[id] = arrayListOf()
}
when (mime) {
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE -> {
val label = PhoneNumberUtils.addressBookLabelTypeToVcardParamString(
@ -165,26 +167,32 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
if (number != null) {
if (
friendsPhoneNumbers[id].orEmpty().find {
friendsPhoneNumbers.find {
PhoneNumberUtils.arePhoneNumberWeakEqual(it, number)
} == null
) {
val phoneNumber = Factory.instance()
.createFriendPhoneNumber(number, label)
friend.addPhoneNumberWithLabel(phoneNumber)
friendsPhoneNumbers[id] = friendsPhoneNumbers[id].orEmpty().plus(number)
friendsPhoneNumbers.add(number)
}
}
}
linphoneMime, ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE -> {
if (data1 == null) continue
val address = core.interpretUrl(data1) ?: continue
friend.addAddress(address)
if (
friendsAddresses.find {
it.weakEqual(address)
} == null
) {
friend.addAddress(address)
friendsAddresses.add(address)
}
}
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE -> {
if (data1 == null) continue
val vCard = friend.vcard
vCard?.organization = data1
friend.organization = data1
}
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE -> {
if (data2 == null && data3 == null) continue
@ -199,7 +207,6 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
Log.e("[Contacts Loader] Exception: $e")
}
}
friendsPhoneNumbers.clear()
withContext(Dispatchers.Main) {
if (core.globalState == GlobalState.Shutdown || core.globalState == GlobalState.Off) {

View file

@ -275,40 +275,19 @@ class ContactsManager(private val context: Context) {
notifyListeners(friend)
}
@Synchronized
fun storePresenceInformationForAllContacts() {
if (corePreferences.storePresenceInNativeContact && PermissionHelper.get().hasWriteContactsPermission()) {
for (list in coreContext.core.friendsLists) {
for (friend in list.friends) {
val id = friend.refKey
if (id != null) {
storePresenceInNativeContact(friend)
}
}
}
}
}
private fun storePresenceInNativeContact(friend: Friend) {
val contactEditor = NativeContactEditor(friend)
for (phoneNumber in friend.phoneNumbers) {
val sipAddress = friend.getContactForPhoneNumberOrAddress(phoneNumber)
if (sipAddress != null) {
Log.d("[Contacts Manager] Found presence information to store in native contact $friend under Linphone sync account")
val contactEditor = NativeContactEditor(friend)
val coroutineScope = CoroutineScope(Dispatchers.Main)
coroutineScope.launch {
val deferred = async {
withContext(Dispatchers.IO) {
contactEditor.setPresenceInformation(
phoneNumber,
sipAddress
).commit()
}
}
deferred.await()
}
contactEditor.setPresenceInformation(
phoneNumber,
sipAddress
)
}
}
contactEditor.commit()
}
fun createFriendFromSearchResult(searchResult: SearchResult): Friend {

View file

@ -103,7 +103,7 @@ class NativeContactEditor(val friend: Friend) {
if (rawId == null) {
try {
rawId = cursor.getString(cursor.getColumnIndexOrThrow(RawContacts._ID))
Log.i("[Native Contact Editor] Found raw id $rawId for native contact with id ${friend.refKey}")
Log.d("[Native Contact Editor] Found raw id $rawId for native contact with id ${friend.refKey}")
} catch (iae: IllegalArgumentException) {
Log.e("[Native Contact Editor] Exception: $iae")
}
@ -484,15 +484,19 @@ class NativeContactEditor(val friend: Friend) {
} else null
cursor?.close()
val address = if (sipAddress.endsWith(";user=phone")) {
sipAddress.substring(0, sipAddress.length - ";user=phone".length)
} else sipAddress
if (count == 0) {
Log.i("[Native Contact Editor] No existing presence information found for this phone number & SIP address, let's add it")
addPresenceLinphoneSipAddressForPhoneNumber(sipAddress, phoneNumber)
Log.i("[Native Contact Editor] No existing presence information found for this phone number ($phoneNumber) & SIP address ($address), let's add it")
addPresenceLinphoneSipAddressForPhoneNumber(address, phoneNumber)
} else {
if (data1 != null && data1 == sipAddress) {
if (data1 != null && data1 == address) {
Log.d("[Native Contact Editor] There is already an entry for this phone number and SIP address, skipping")
} else {
Log.w("[Native Contact Editor] There is already an entry for this phone number but not for the same SIP address")
updatePresenceLinphoneSipAddressForPhoneNumber(sipAddress, phoneNumber)
Log.w("[Native Contact Editor] There is already an entry for this phone number ($phoneNumber) but not for the same SIP address ($data1 != $address)")
updatePresenceLinphoneSipAddressForPhoneNumber(address, phoneNumber)
}
}
}