Fixed native contact editor to update presence information in case of change + renamed some methods for better understanding
This commit is contained in:
parent
31f70f2ca4
commit
11bf573d42
1 changed files with 64 additions and 28 deletions
|
@ -64,6 +64,8 @@ class NativeContactEditor(
|
||||||
"$selection AND (${CommonDataKinds.Phone.NUMBER}=? OR ${CommonDataKinds.Phone.NORMALIZED_NUMBER}=?)"
|
"$selection AND (${CommonDataKinds.Phone.NUMBER}=? OR ${CommonDataKinds.Phone.NORMALIZED_NUMBER}=?)"
|
||||||
private val sipAddressSelection =
|
private val sipAddressSelection =
|
||||||
"${ContactsContract.Data.CONTACT_ID} =? AND (${ContactsContract.Data.MIMETYPE} =? OR ${ContactsContract.Data.MIMETYPE} =?) AND data1=?"
|
"${ContactsContract.Data.CONTACT_ID} =? AND (${ContactsContract.Data.MIMETYPE} =? OR ${ContactsContract.Data.MIMETYPE} =?) AND data1=?"
|
||||||
|
private val presenceUpdateSelection =
|
||||||
|
"${ContactsContract.Data.CONTACT_ID} =? AND ${ContactsContract.Data.MIMETYPE} =? AND data3=?"
|
||||||
private val useLinphoneSyncAccount = corePreferences.useLinphoneSyncAccount
|
private val useLinphoneSyncAccount = corePreferences.useLinphoneSyncAccount
|
||||||
private val contactUri = ContactsContract.Data.CONTENT_URI
|
private val contactUri = ContactsContract.Data.CONTENT_URI
|
||||||
|
|
||||||
|
@ -206,17 +208,17 @@ class NativeContactEditor(
|
||||||
phoneNumber.currentValue.isEmpty() -> {
|
phoneNumber.currentValue.isEmpty() -> {
|
||||||
// New phone number to add
|
// New phone number to add
|
||||||
addCount++
|
addCount++
|
||||||
addNumber(phoneNumber.newValue.value.orEmpty())
|
addPhoneNumber(phoneNumber.newValue.value.orEmpty())
|
||||||
}
|
}
|
||||||
phoneNumber.toRemove.value == true -> {
|
phoneNumber.toRemove.value == true -> {
|
||||||
// Existing number to remove
|
// Existing number to remove
|
||||||
removeCount++
|
removeCount++
|
||||||
removeNumber(phoneNumber.currentValue)
|
removePhoneNumber(phoneNumber.currentValue)
|
||||||
}
|
}
|
||||||
phoneNumber.currentValue != phoneNumber.newValue.value -> {
|
phoneNumber.currentValue != phoneNumber.newValue.value -> {
|
||||||
// Existing number to update
|
// Existing number to update
|
||||||
editCount++
|
editCount++
|
||||||
updateNumber(phoneNumber.currentValue, phoneNumber.newValue.value.orEmpty())
|
updatePhoneNumber(phoneNumber.currentValue, phoneNumber.newValue.value.orEmpty())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,7 +239,7 @@ class NativeContactEditor(
|
||||||
addCount++
|
addCount++
|
||||||
val address = sipAddress.newValue.value.orEmpty()
|
val address = sipAddress.newValue.value.orEmpty()
|
||||||
if (useLinphoneSyncAccount) {
|
if (useLinphoneSyncAccount) {
|
||||||
addAddress(address, address)
|
addLinphoneAddress(address, address)
|
||||||
} else {
|
} else {
|
||||||
addSipAddress(address)
|
addSipAddress(address)
|
||||||
}
|
}
|
||||||
|
@ -245,12 +247,12 @@ class NativeContactEditor(
|
||||||
sipAddress.toRemove.value == true -> {
|
sipAddress.toRemove.value == true -> {
|
||||||
// Existing address to remove
|
// Existing address to remove
|
||||||
removeCount++
|
removeCount++
|
||||||
removeAddress(sipAddress.currentValue)
|
removeLinphoneOrSipAddress(sipAddress.currentValue)
|
||||||
}
|
}
|
||||||
sipAddress.currentValue != sipAddress.newValue.value -> {
|
sipAddress.currentValue != sipAddress.newValue.value -> {
|
||||||
// Existing address to update
|
// Existing address to update
|
||||||
editCount++
|
editCount++
|
||||||
updateAddress(sipAddress.currentValue, sipAddress.newValue.value.orEmpty())
|
updateLinphoneOrSipAddress(sipAddress.currentValue, sipAddress.newValue.value.orEmpty())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -269,7 +271,7 @@ class NativeContactEditor(
|
||||||
Log.d("[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)
|
setPresenceLinphoneSipAddressForPhoneNumber(sipAddress, phoneNumber)
|
||||||
} else {
|
} else {
|
||||||
setPresenceSipAddress(sipAddress)
|
setPresenceSipAddress(sipAddress)
|
||||||
}
|
}
|
||||||
|
@ -308,14 +310,14 @@ class NativeContactEditor(
|
||||||
changes.add(operation)
|
changes.add(operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addNumber(number: String) {
|
private fun addPhoneNumber(phoneNumber: String) {
|
||||||
val insert = ContentProviderOperation.newInsert(contactUri)
|
val insert = ContentProviderOperation.newInsert(contactUri)
|
||||||
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawId)
|
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawId)
|
||||||
.withValue(
|
.withValue(
|
||||||
ContactsContract.Data.MIMETYPE,
|
ContactsContract.Data.MIMETYPE,
|
||||||
CommonDataKinds.Phone.CONTENT_ITEM_TYPE
|
CommonDataKinds.Phone.CONTENT_ITEM_TYPE
|
||||||
)
|
)
|
||||||
.withValue(CommonDataKinds.Phone.NUMBER, number)
|
.withValue(CommonDataKinds.Phone.NUMBER, phoneNumber)
|
||||||
.withValue(
|
.withValue(
|
||||||
CommonDataKinds.Phone.TYPE,
|
CommonDataKinds.Phone.TYPE,
|
||||||
CommonDataKinds.Phone.TYPE_MOBILE
|
CommonDataKinds.Phone.TYPE_MOBILE
|
||||||
|
@ -324,7 +326,7 @@ class NativeContactEditor(
|
||||||
addChanges(insert)
|
addChanges(insert)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateNumber(currentValue: String, newValue: String) {
|
private fun updatePhoneNumber(currentValue: String, phoneNumber: String) {
|
||||||
val update = ContentProviderOperation.newUpdate(contactUri)
|
val update = ContentProviderOperation.newUpdate(contactUri)
|
||||||
.withSelection(
|
.withSelection(
|
||||||
phoneNumberSelection,
|
phoneNumberSelection,
|
||||||
|
@ -336,7 +338,7 @@ class NativeContactEditor(
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
|
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
|
||||||
.withValue(CommonDataKinds.Phone.NUMBER, newValue)
|
.withValue(CommonDataKinds.Phone.NUMBER, phoneNumber)
|
||||||
.withValue(
|
.withValue(
|
||||||
CommonDataKinds.Phone.TYPE,
|
CommonDataKinds.Phone.TYPE,
|
||||||
CommonDataKinds.Phone.TYPE_MOBILE
|
CommonDataKinds.Phone.TYPE_MOBILE
|
||||||
|
@ -345,22 +347,22 @@ class NativeContactEditor(
|
||||||
addChanges(update)
|
addChanges(update)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun removeNumber(number: String) {
|
private fun removePhoneNumber(phoneNumber: String) {
|
||||||
val delete = ContentProviderOperation.newDelete(contactUri)
|
val delete = ContentProviderOperation.newDelete(contactUri)
|
||||||
.withSelection(
|
.withSelection(
|
||||||
phoneNumberSelection,
|
phoneNumberSelection,
|
||||||
arrayOf(
|
arrayOf(
|
||||||
contact.nativeId,
|
contact.nativeId,
|
||||||
CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
|
CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
|
||||||
number,
|
phoneNumber,
|
||||||
number
|
phoneNumber
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
addChanges(delete)
|
addChanges(delete)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addAddress(address: String, detail: String) {
|
private fun addLinphoneAddress(address: String, detail: String) {
|
||||||
val insert = ContentProviderOperation.newInsert(contactUri)
|
val insert = ContentProviderOperation.newInsert(contactUri)
|
||||||
.withValue(ContactsContract.Data.RAW_CONTACT_ID, linphoneRawId)
|
.withValue(ContactsContract.Data.RAW_CONTACT_ID, linphoneRawId)
|
||||||
.withValue(
|
.withValue(
|
||||||
|
@ -386,7 +388,7 @@ class NativeContactEditor(
|
||||||
addChanges(insert)
|
addChanges(insert)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateAddress(currentValue: String, newValue: String) {
|
private fun updateLinphoneOrSipAddress(currentValue: String, sipAddress: String) {
|
||||||
val update = ContentProviderOperation.newUpdate(contactUri)
|
val update = ContentProviderOperation.newUpdate(contactUri)
|
||||||
.withSelection(
|
.withSelection(
|
||||||
sipAddressSelection,
|
sipAddressSelection,
|
||||||
|
@ -401,14 +403,14 @@ class NativeContactEditor(
|
||||||
ContactsContract.Data.MIMETYPE,
|
ContactsContract.Data.MIMETYPE,
|
||||||
AppUtils.getString(R.string.linphone_address_mime_type)
|
AppUtils.getString(R.string.linphone_address_mime_type)
|
||||||
)
|
)
|
||||||
.withValue("data1", newValue) // value
|
.withValue("data1", sipAddress) // value
|
||||||
.withValue("data2", AppUtils.getString(R.string.app_name)) // summary
|
.withValue("data2", AppUtils.getString(R.string.app_name)) // summary
|
||||||
.withValue("data3", newValue) // detail
|
.withValue("data3", sipAddress) // detail
|
||||||
.build()
|
.build()
|
||||||
addChanges(update)
|
addChanges(update)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun removeAddress(address: String) {
|
private fun removeLinphoneOrSipAddress(sipAddress: String) {
|
||||||
val delete = ContentProviderOperation.newDelete(contactUri)
|
val delete = ContentProviderOperation.newDelete(contactUri)
|
||||||
.withSelection(
|
.withSelection(
|
||||||
sipAddressSelection,
|
sipAddressSelection,
|
||||||
|
@ -416,33 +418,67 @@ class NativeContactEditor(
|
||||||
contact.nativeId,
|
contact.nativeId,
|
||||||
CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE,
|
CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE,
|
||||||
AppUtils.getString(R.string.linphone_address_mime_type),
|
AppUtils.getString(R.string.linphone_address_mime_type),
|
||||||
address
|
sipAddress
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
addChanges(delete)
|
addChanges(delete)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setPresence(sipAddress: String, phoneNumber: String) {
|
private fun setPresenceLinphoneSipAddressForPhoneNumber(sipAddress: String, phoneNumber: String) {
|
||||||
val contentResolver = coreContext.context.contentResolver
|
val contentResolver = coreContext.context.contentResolver
|
||||||
val cursor = contentResolver.query(
|
val cursor = contentResolver.query(
|
||||||
ContactsContract.Data.CONTENT_URI,
|
ContactsContract.Data.CONTENT_URI,
|
||||||
arrayOf("data1"),
|
arrayOf("data1"),
|
||||||
"${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? AND data1 = ? AND data3 = ?",
|
"${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? AND data3 = ?",
|
||||||
arrayOf(linphoneRawId, AppUtils.getString(R.string.linphone_address_mime_type), sipAddress, phoneNumber),
|
arrayOf(linphoneRawId, AppUtils.getString(R.string.linphone_address_mime_type), phoneNumber),
|
||||||
null
|
null
|
||||||
)
|
)
|
||||||
val count = cursor?.count ?: 0
|
val count = cursor?.count ?: 0
|
||||||
|
val data1 = if (count > 0) {
|
||||||
|
if (cursor?.moveToFirst() == true) {
|
||||||
|
cursor.getString(cursor.getColumnIndex("data1"))
|
||||||
|
} else null
|
||||||
|
} else null
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
|
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
Log.i("[Native Contact Editor] No existing presence information found for this phone number, let's add it")
|
Log.i("[Native Contact Editor] No existing presence information found for this phone number & SIP address, let's add it")
|
||||||
addAddress(sipAddress, phoneNumber)
|
addLinphoneAddress(sipAddress, phoneNumber)
|
||||||
} else {
|
} else {
|
||||||
Log.i("[Native Contact Editor] There is already an entry for this, skipping")
|
if (data1 != null && data1 == sipAddress) {
|
||||||
|
Log.i("[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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updatePresenceLinphoneSipAddressForPhoneNumber(
|
||||||
|
sipAddress: String,
|
||||||
|
phoneNumber: String
|
||||||
|
) {
|
||||||
|
val update = ContentProviderOperation.newUpdate(contactUri)
|
||||||
|
.withSelection(
|
||||||
|
presenceUpdateSelection,
|
||||||
|
arrayOf(
|
||||||
|
contact.nativeId,
|
||||||
|
AppUtils.getString(R.string.linphone_address_mime_type),
|
||||||
|
phoneNumber
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.withValue(
|
||||||
|
ContactsContract.Data.MIMETYPE,
|
||||||
|
AppUtils.getString(R.string.linphone_address_mime_type)
|
||||||
|
)
|
||||||
|
.withValue("data1", sipAddress) // value
|
||||||
|
.withValue("data2", AppUtils.getString(R.string.app_name)) // summary
|
||||||
|
.withValue("data3", phoneNumber) // detail
|
||||||
|
.build()
|
||||||
|
addChanges(update)
|
||||||
|
}
|
||||||
|
|
||||||
private fun setPresenceSipAddress(sipAddress: String) {
|
private fun setPresenceSipAddress(sipAddress: String) {
|
||||||
val contentResolver = coreContext.context.contentResolver
|
val contentResolver = coreContext.context.contentResolver
|
||||||
val cursor = contentResolver.query(
|
val cursor = contentResolver.query(
|
||||||
|
@ -456,10 +492,10 @@ class NativeContactEditor(
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
|
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
Log.i("[Native Contact Editor] No existing presence information found for this phone number, let's add it")
|
Log.i("[Native Contact Editor] SIP address not found, let's add it")
|
||||||
addSipAddress(sipAddress)
|
addSipAddress(sipAddress)
|
||||||
} else {
|
} else {
|
||||||
Log.i("[Native Contact Editor] There is already an entry for this, skipping")
|
Log.i("[Native Contact Editor] There is already an entry for this SIP address, skipping")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue