Added presence information storage in native contact + fixed task(s) issue

This commit is contained in:
Sylvain Berfini 2020-05-13 12:18:46 +02:00
parent f23c28415a
commit 45e868fc3f
5 changed files with 100 additions and 31 deletions

View file

@ -47,6 +47,7 @@
</activity>
<activity android:name=".activities.main.MainActivity"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<nav-graph android:value="@navigation/main_nav_graph" />
@ -86,7 +87,7 @@
android:windowSoftInputMode="adjustResize"/>
<activity android:name=".activities.call.CallActivity"
android:launchMode="singleInstance"
android:launchMode="singleTop"
android:showWhenLocked="true"
android:supportsPictureInPicture="true" />

View file

@ -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

View file

@ -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 !")

View file

@ -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

View file

@ -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")