From 45e868fc3f9e6670dc226580ae062b8fdb1e4927 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 13 May 2020 12:18:46 +0200 Subject: [PATCH] Added presence information storage in native contact + fixed task(s) issue --- app/src/main/AndroidManifest.xml | 3 +- .../org/linphone/contact/ContactsManager.kt | 14 ++- .../org/linphone/contact/NativeContact.kt | 5 + .../linphone/contact/NativeContactEditor.kt | 106 +++++++++++++----- .../java/org/linphone/core/CorePreferences.kt | 3 +- 5 files changed, 100 insertions(+), 31 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4bd4eb171..8fbe86829 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -47,6 +47,7 @@ @@ -86,7 +87,7 @@ android:windowSoftInputMode="adjustResize"/> diff --git a/app/src/main/java/org/linphone/contact/ContactsManager.kt b/app/src/main/java/org/linphone/contact/ContactsManager.kt index e39c39fc7..17e0ee821 100644 --- a/app/src/main/java/org/linphone/contact/ContactsManager.kt +++ b/app/src/main/java/org/linphone/contact/ContactsManager.kt @@ -261,11 +261,23 @@ 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") for (listener in contactsUpdatedListeners) { listener.onContactUpdated(contact) } - Log.i("[Contacts Manager] Received presence information for contact $contact") + if (corePreferences.storePresenceInNativeContact) { + 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).setPresenceInformation(phoneNumber, sipAddress).commit() + } + } + } + } + if (!sipContacts.contains(contact)) { sipContacts.add(contact) return true diff --git a/app/src/main/java/org/linphone/contact/NativeContact.kt b/app/src/main/java/org/linphone/contact/NativeContact.kt index c48c5b502..8f1841fd4 100644 --- a/app/src/main/java/org/linphone/contact/NativeContact.kt +++ b/app/src/main/java/org/linphone/contact/NativeContact.kt @@ -119,6 +119,11 @@ class NativeContact(val nativeId: String, private val lookupKey: String? = null) } Log.d("[Native Contact] Found SIP address $data1") + if (phoneNumbers.contains(data1)) { + Log.d("[Native Contact] SIP address value already exists in phone numbers list, skipping") + return + } + val address: Address? = coreContext.core.interpretUrl(data1) if (address == null) { Log.e("[Native Contact] Couldn't parse address $data1 !") diff --git a/app/src/main/java/org/linphone/contact/NativeContactEditor.kt b/app/src/main/java/org/linphone/contact/NativeContactEditor.kt index ef7da8bfb..c07ff88f3 100644 --- a/app/src/main/java/org/linphone/contact/NativeContactEditor.kt +++ b/app/src/main/java/org/linphone/contact/NativeContactEditor.kt @@ -180,17 +180,17 @@ class NativeContactEditor(val contact: NativeContact) { phoneNumber.currentValue.isEmpty() -> { // New phone number to add addCount++ - addNumber(phoneNumber) + addNumber(phoneNumber.newValue.value.orEmpty()) } phoneNumber.toRemove.value == true -> { // Existing number to remove removeCount++ - removeNumber(phoneNumber) + removeNumber(phoneNumber.currentValue) } phoneNumber.currentValue != phoneNumber.newValue.value -> { // Existing number to update editCount++ - updateNumber(phoneNumber) + updateNumber(phoneNumber.currentValue, phoneNumber.newValue.value.orEmpty()) } } } @@ -210,20 +210,20 @@ class NativeContactEditor(val contact: NativeContact) { // New address to add addCount++ if (useLinphoneSyncAccount) { - addAddress(sipAddress) + addAddress(sipAddress.newValue.value.orEmpty()) } else { - addSipAddress(sipAddress) + addSipAddress(sipAddress.newValue.value.orEmpty()) } } sipAddress.toRemove.value == true -> { // Existing address to remove removeCount++ - removeAddress(sipAddress) + removeAddress(sipAddress.currentValue) } sipAddress.currentValue != sipAddress.newValue.value -> { // Existing address to update editCount++ - updateAddress(sipAddress) + updateAddress(sipAddress.currentValue, sipAddress.newValue.value.orEmpty()) } } } @@ -238,6 +238,18 @@ class NativeContactEditor(val contact: NativeContact) { return this } + 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"}") + + if (useLinphoneSyncAccount) { + setPresencePhoneNumber(phoneNumber) + } else { + setPresenceSipAddress(sipAddress) + } + + return this + } + fun commit() { if (PermissionHelper.get().hasWriteContactsPermission()) { try { @@ -269,14 +281,14 @@ class NativeContactEditor(val contact: NativeContact) { changes.add(operation) } - private fun addNumber(number: NumberOrAddressEditorViewModel) { + private fun addNumber(number: String) { val insert = ContentProviderOperation.newInsert(contactUri) .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawId) .withValue( ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE ) - .withValue(CommonDataKinds.Phone.NUMBER, number.newValue.value) + .withValue(CommonDataKinds.Phone.NUMBER, number) .withValue( CommonDataKinds.Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE @@ -285,19 +297,19 @@ class NativeContactEditor(val contact: NativeContact) { addChanges(insert) } - private fun updateNumber(number: NumberOrAddressEditorViewModel) { + private fun updateNumber(currentValue: String, newValue: String) { val update = ContentProviderOperation.newUpdate(contactUri) .withSelection( phoneNumberSelection, arrayOf( contact.nativeId, CommonDataKinds.Phone.CONTENT_ITEM_TYPE, - number.currentValue, - number.currentValue + currentValue, + currentValue ) ) .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE) - .withValue(CommonDataKinds.Phone.NUMBER, number.newValue.value) + .withValue(CommonDataKinds.Phone.NUMBER, newValue) .withValue( CommonDataKinds.Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE @@ -306,48 +318,48 @@ class NativeContactEditor(val contact: NativeContact) { addChanges(update) } - private fun removeNumber(number: NumberOrAddressEditorViewModel) { + private fun removeNumber(number: String) { val delete = ContentProviderOperation.newDelete(contactUri) .withSelection( phoneNumberSelection, arrayOf( contact.nativeId, CommonDataKinds.Phone.CONTENT_ITEM_TYPE, - number.currentValue, - number.currentValue + number, + number ) ) .build() addChanges(delete) } - private fun addAddress(address: NumberOrAddressEditorViewModel) { + private fun addAddress(address: String) { val insert = ContentProviderOperation.newInsert(contactUri) .withValue(ContactsContract.Data.RAW_CONTACT_ID, linphoneRawId) .withValue( ContactsContract.Data.MIMETYPE, AppUtils.getString(R.string.linphone_address_mime_type) ) - .withValue("data1", address.newValue.value) // value + .withValue("data1", address) // value .withValue("data2", AppUtils.getString(R.string.app_name)) // summary - .withValue("data3", address.newValue.value) // detail + .withValue("data3", address) // detail .build() addChanges(insert) } - private fun addSipAddress(address: NumberOrAddressEditorViewModel) { + private fun addSipAddress(address: String) { val insert = ContentProviderOperation.newInsert(contactUri) .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawId) .withValue( ContactsContract.Data.MIMETYPE, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE ) - .withValue("data1", address.newValue.value) // value + .withValue("data1", address) // value .build() addChanges(insert) } - private fun updateAddress(address: NumberOrAddressEditorViewModel) { + private fun updateAddress(currentValue: String, newValue: String) { val update = ContentProviderOperation.newUpdate(contactUri) .withSelection( sipAddressSelection, @@ -355,21 +367,21 @@ class NativeContactEditor(val contact: NativeContact) { contact.nativeId, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE, AppUtils.getString(R.string.linphone_address_mime_type), - address.currentValue + currentValue ) ) .withValue( ContactsContract.Data.MIMETYPE, AppUtils.getString(R.string.linphone_address_mime_type) ) - .withValue("data1", address.newValue.value) // value + .withValue("data1", newValue) // value .withValue("data2", AppUtils.getString(R.string.app_name)) // summary - .withValue("data3", address.newValue.value) // detail + .withValue("data3", newValue) // detail .build() addChanges(update) } - private fun removeAddress(address: NumberOrAddressEditorViewModel) { + private fun removeAddress(address: String) { val delete = ContentProviderOperation.newDelete(contactUri) .withSelection( sipAddressSelection, @@ -377,13 +389,53 @@ class NativeContactEditor(val contact: NativeContact) { contact.nativeId, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE, AppUtils.getString(R.string.linphone_address_mime_type), - address.currentValue + address ) ) .build() addChanges(delete) } + private fun setPresencePhoneNumber(phoneNumber: String) { + val contentResolver = coreContext.context.contentResolver + val cursor = contentResolver.query( + ContactsContract.Data.CONTENT_URI, + arrayOf("data1"), + "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? AND data1 = ?", + arrayOf(linphoneRawId, AppUtils.getString(R.string.linphone_address_mime_type), phoneNumber), + null + ) + val count = cursor?.count ?: 0 + cursor?.close() + + if (count == 0) { + Log.i("[Native Contact Editor] No existing presence information found for this phone number, let's add it") + addAddress(phoneNumber) + } else { + Log.i("[Native Contact Editor] There is already an entry for this, skipping") + } + } + + private fun setPresenceSipAddress(sipAddress: String) { + val contentResolver = coreContext.context.contentResolver + val cursor = contentResolver.query( + ContactsContract.Data.CONTENT_URI, + arrayOf("data1"), + "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? AND data1 = ?", + arrayOf(rawId, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE, sipAddress), + null + ) + val count = cursor?.count ?: 0 + cursor?.close() + + if (count == 0) { + Log.i("[Native Contact Editor] No existing presence information found for this phone number, let's add it") + addSipAddress(sipAddress) + } else { + Log.i("[Native Contact Editor] There is already an entry for this, skipping") + } + } + private fun updatePicture() { val value = pictureByteArray val id = rawId diff --git a/app/src/main/java/org/linphone/core/CorePreferences.kt b/app/src/main/java/org/linphone/core/CorePreferences.kt index 6100bf002..f7c79e753 100644 --- a/app/src/main/java/org/linphone/core/CorePreferences.kt +++ b/app/src/main/java/org/linphone/core/CorePreferences.kt @@ -118,7 +118,6 @@ class CorePreferences constructor(private val context: Context) { /* Contacts */ - // TODO: use it var storePresenceInNativeContact: Boolean get() = config.getBool("app", "store_presence_in_native_contact", false) set(value) { @@ -199,7 +198,7 @@ class CorePreferences constructor(private val context: Context) { config.setString("app", "voice_mail", value) } - /* App settings previously in non_localizable_custom */ + /* Read only application settings previously in non_localizable_custom */ val defaultDomain: String get() = config.getString("app", "default_domain", "sip.linphone.org")