Fixed same SIP address visible more than once in a contact + fixed presence not stored immediatly after setting enabled

This commit is contained in:
Sylvain Berfini 2020-06-08 16:38:47 +02:00
parent c15ecd54ac
commit 31f70f2ca4
6 changed files with 59 additions and 22 deletions

View file

@ -206,6 +206,7 @@ class MasterContactsFragment : MasterFragment() {
val granted = grantResults[0] == PackageManager.PERMISSION_GRANTED val granted = grantResults[0] == PackageManager.PERMISSION_GRANTED
if (granted) { if (granted) {
Log.i("[Contacts] READ_CONTACTS permission granted") Log.i("[Contacts] READ_CONTACTS permission granted")
coreContext.contactsManager.onReadContactsPermissionGranted()
coreContext.contactsManager.fetchContactsAsync() coreContext.contactsManager.fetchContactsAsync()
} else { } else {
Log.w("[Contacts] READ_CONTACTS permission denied") Log.w("[Contacts] READ_CONTACTS permission denied")

View file

@ -98,6 +98,7 @@ class ContactsSettingsFragment : Fragment() {
if (granted) { if (granted) {
Log.i("[Contacts Settings] READ_CONTACTS permission granted") Log.i("[Contacts Settings] READ_CONTACTS permission granted")
viewModel.readContactsPermissionGranted.value = true viewModel.readContactsPermissionGranted.value = true
coreContext.contactsManager.onReadContactsPermissionGranted()
coreContext.contactsManager.fetchContactsAsync() coreContext.contactsManager.fetchContactsAsync()
} else { } else {
Log.w("[Contacts Settings] READ_CONTACTS permission denied") Log.w("[Contacts Settings] READ_CONTACTS permission denied")
@ -108,13 +109,7 @@ class ContactsSettingsFragment : Fragment() {
if (granted) { if (granted) {
Log.i("[Contacts Settings] WRITE_CONTACTS permission granted") Log.i("[Contacts Settings] WRITE_CONTACTS permission granted")
corePreferences.storePresenceInNativeContact = true corePreferences.storePresenceInNativeContact = true
coreContext.contactsManager.storePresenceInformationForAllContacts()
if (coreContext.core.isFriendListSubscriptionEnabled) {
Log.i("[Contacts Settings] Updating subscription")
for (list in coreContext.core.friendsLists) {
list.updateSubscriptions()
}
}
} else { } else {
Log.w("[Contacts Settings] WRITE_CONTACTS permission denied") Log.w("[Contacts Settings] WRITE_CONTACTS permission denied")
} }

View file

@ -88,12 +88,14 @@ open class Contact : Comparable<Contact> {
for (number in friend.phoneNumbers) { for (number in friend.phoneNumbers) {
if (!phoneNumbers.contains(number)) phoneNumbers.add(number) if (!phoneNumbers.contains(number)) phoneNumbers.add(number)
} }
sipAddresses.clear() sipAddresses.clear()
rawSipAddresses.clear() rawSipAddresses.clear()
for (address in friend.addresses) { for (address in friend.addresses) {
if (!sipAddresses.contains(address)) { val stringAddress = address.asStringUriOnly()
if (!rawSipAddresses.contains(stringAddress)) {
sipAddresses.add(address) sipAddresses.add(address)
rawSipAddresses.add(address.asStringUriOnly()) rawSipAddresses.add(stringAddress)
} }
} }

View file

@ -106,8 +106,9 @@ class ContactsManager(private val context: Context) {
} }
init { init {
if (PermissionHelper.required(context).hasReadContactsPermission()) if (PermissionHelper.required(context).hasReadContactsPermission()) {
context.contentResolver.registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactsObserver) onReadContactsPermissionGranted()
}
initSyncAccount() initSyncAccount()
@ -118,6 +119,15 @@ class ContactsManager(private val context: Context) {
Log.i("[Contacts Manager] Created") Log.i("[Contacts Manager] Created")
} }
fun onReadContactsPermissionGranted() {
Log.i("[Contacts Manager] Register contacts observer")
context.contentResolver.registerContentObserver(
ContactsContract.Contacts.CONTENT_URI,
true,
contactsObserver
)
}
@Synchronized @Synchronized
fun fetchContactsAsync() { fun fetchContactsAsync() {
if (loadContactsTask != null) { if (loadContactsTask != null) {
@ -282,20 +292,14 @@ class ContactsManager(private val context: Context) {
if (friend.userData == null) return false if (friend.userData == null) return false
val contact: Contact = friend.userData as Contact val contact: Contact = friend.userData as Contact
Log.i("[Contacts Manager] Received presence information for contact $contact") Log.d("[Contacts Manager] Received presence information for contact $contact")
for (listener in contactsUpdatedListeners) { for (listener in contactsUpdatedListeners) {
listener.onContactUpdated(contact) listener.onContactUpdated(contact)
} }
if (corePreferences.storePresenceInNativeContact && PermissionHelper.get().hasWriteContactsPermission()) { if (corePreferences.storePresenceInNativeContact && PermissionHelper.get().hasWriteContactsPermission()) {
if (contact is NativeContact) { if (contact is NativeContact) {
for (phoneNumber in contact.phoneNumbers) { storePresenceInNativeContact(contact)
val sipAddress = contact.getContactForPhoneNumberOrAddress(phoneNumber)
if (sipAddress != null) {
Log.i("[Contacts Manager] Found presence information to store in native contact $contact")
NativeContactEditor(contact, null, null).setPresenceInformation(phoneNumber, sipAddress).commit()
}
}
} }
} }
@ -306,4 +310,38 @@ class ContactsManager(private val context: Context) {
return false return false
} }
@Synchronized
fun storePresenceInformationForAllContacts() {
if (corePreferences.storePresenceInNativeContact && PermissionHelper.get().hasWriteContactsPermission()) {
for (list in coreContext.core.friendsLists) {
for (friend in list.friends) {
if (friend.userData == null) continue
val contact: Contact = friend.userData as Contact
if (contact is NativeContact) {
storePresenceInNativeContact(contact)
}
}
}
}
}
private fun storePresenceInNativeContact(contact: NativeContact) {
for (phoneNumber in contact.phoneNumbers) {
val sipAddress = contact.getContactForPhoneNumberOrAddress(phoneNumber)
if (sipAddress != null) {
Log.d("[Contacts Manager] Found presence information to store in native contact $contact")
val contactEditor = NativeContactEditor(contact, null, null)
// TODO: could be great to do in a coroutine
// launch {
// withContext(Dispatchers.IO) {
contactEditor.setPresenceInformation(phoneNumber, sipAddress).commit()
// }
// }
for (listener in contactsUpdatedListeners) {
listener.onContactUpdated(contact)
}
}
}
}
} }

View file

@ -130,9 +130,10 @@ class NativeContact(val nativeId: String, private val lookupKey: String? = null)
return return
} }
if (!sipAddresses.contains(address)) { val stringAddress = address.asStringUriOnly()
if (!rawSipAddresses.contains(stringAddress)) {
sipAddresses.add(address) sipAddresses.add(address)
rawSipAddresses.add(data1) rawSipAddresses.add(stringAddress)
} }
} }
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE -> { ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE -> {

View file

@ -266,7 +266,7 @@ class NativeContactEditor(
} }
fun setPresenceInformation(phoneNumber: String, sipAddress: String): NativeContactEditor { fun setPresenceInformation(phoneNumber: String, sipAddress: String): NativeContactEditor {
Log.i("[Native Contact Editor] Trying to add presence information to contact as ${if (useLinphoneSyncAccount) "phone number" else "SIP address"}") Log.d("[Native Contact Editor] Trying to add presence information to contact as ${if (useLinphoneSyncAccount) "phone number" else "SIP address"}")
if (useLinphoneSyncAccount) { if (useLinphoneSyncAccount) {
setPresence(sipAddress, phoneNumber) setPresence(sipAddress, phoneNumber)