Fixed same SIP address visible more than once in a contact + fixed presence not stored immediatly after setting enabled
This commit is contained in:
parent
c15ecd54ac
commit
31f70f2ca4
6 changed files with 59 additions and 22 deletions
|
@ -206,6 +206,7 @@ class MasterContactsFragment : MasterFragment() {
|
|||
val granted = grantResults[0] == PackageManager.PERMISSION_GRANTED
|
||||
if (granted) {
|
||||
Log.i("[Contacts] READ_CONTACTS permission granted")
|
||||
coreContext.contactsManager.onReadContactsPermissionGranted()
|
||||
coreContext.contactsManager.fetchContactsAsync()
|
||||
} else {
|
||||
Log.w("[Contacts] READ_CONTACTS permission denied")
|
||||
|
|
|
@ -98,6 +98,7 @@ class ContactsSettingsFragment : Fragment() {
|
|||
if (granted) {
|
||||
Log.i("[Contacts Settings] READ_CONTACTS permission granted")
|
||||
viewModel.readContactsPermissionGranted.value = true
|
||||
coreContext.contactsManager.onReadContactsPermissionGranted()
|
||||
coreContext.contactsManager.fetchContactsAsync()
|
||||
} else {
|
||||
Log.w("[Contacts Settings] READ_CONTACTS permission denied")
|
||||
|
@ -108,13 +109,7 @@ class ContactsSettingsFragment : Fragment() {
|
|||
if (granted) {
|
||||
Log.i("[Contacts Settings] WRITE_CONTACTS permission granted")
|
||||
corePreferences.storePresenceInNativeContact = true
|
||||
|
||||
if (coreContext.core.isFriendListSubscriptionEnabled) {
|
||||
Log.i("[Contacts Settings] Updating subscription")
|
||||
for (list in coreContext.core.friendsLists) {
|
||||
list.updateSubscriptions()
|
||||
}
|
||||
}
|
||||
coreContext.contactsManager.storePresenceInformationForAllContacts()
|
||||
} else {
|
||||
Log.w("[Contacts Settings] WRITE_CONTACTS permission denied")
|
||||
}
|
||||
|
|
|
@ -88,12 +88,14 @@ open class Contact : Comparable<Contact> {
|
|||
for (number in friend.phoneNumbers) {
|
||||
if (!phoneNumbers.contains(number)) phoneNumbers.add(number)
|
||||
}
|
||||
|
||||
sipAddresses.clear()
|
||||
rawSipAddresses.clear()
|
||||
for (address in friend.addresses) {
|
||||
if (!sipAddresses.contains(address)) {
|
||||
val stringAddress = address.asStringUriOnly()
|
||||
if (!rawSipAddresses.contains(stringAddress)) {
|
||||
sipAddresses.add(address)
|
||||
rawSipAddresses.add(address.asStringUriOnly())
|
||||
rawSipAddresses.add(stringAddress)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -106,8 +106,9 @@ class ContactsManager(private val context: Context) {
|
|||
}
|
||||
|
||||
init {
|
||||
if (PermissionHelper.required(context).hasReadContactsPermission())
|
||||
context.contentResolver.registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactsObserver)
|
||||
if (PermissionHelper.required(context).hasReadContactsPermission()) {
|
||||
onReadContactsPermissionGranted()
|
||||
}
|
||||
|
||||
initSyncAccount()
|
||||
|
||||
|
@ -118,6 +119,15 @@ class ContactsManager(private val context: Context) {
|
|||
Log.i("[Contacts Manager] Created")
|
||||
}
|
||||
|
||||
fun onReadContactsPermissionGranted() {
|
||||
Log.i("[Contacts Manager] Register contacts observer")
|
||||
context.contentResolver.registerContentObserver(
|
||||
ContactsContract.Contacts.CONTENT_URI,
|
||||
true,
|
||||
contactsObserver
|
||||
)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun fetchContactsAsync() {
|
||||
if (loadContactsTask != null) {
|
||||
|
@ -282,20 +292,14 @@ class ContactsManager(private val context: Context) {
|
|||
if (friend.userData == null) return false
|
||||
|
||||
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) {
|
||||
listener.onContactUpdated(contact)
|
||||
}
|
||||
|
||||
if (corePreferences.storePresenceInNativeContact && PermissionHelper.get().hasWriteContactsPermission()) {
|
||||
if (contact is NativeContact) {
|
||||
for (phoneNumber in contact.phoneNumbers) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
storePresenceInNativeContact(contact)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -306,4 +310,38 @@ class ContactsManager(private val context: Context) {
|
|||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -130,9 +130,10 @@ class NativeContact(val nativeId: String, private val lookupKey: String? = null)
|
|||
return
|
||||
}
|
||||
|
||||
if (!sipAddresses.contains(address)) {
|
||||
val stringAddress = address.asStringUriOnly()
|
||||
if (!rawSipAddresses.contains(stringAddress)) {
|
||||
sipAddresses.add(address)
|
||||
rawSipAddresses.add(data1)
|
||||
rawSipAddresses.add(stringAddress)
|
||||
}
|
||||
}
|
||||
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE -> {
|
||||
|
|
|
@ -266,7 +266,7 @@ class 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) {
|
||||
setPresence(sipAddress, phoneNumber)
|
||||
|
|
Loading…
Reference in a new issue