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

View file

@ -261,11 +261,23 @@ 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")
for (listener in contactsUpdatedListeners) { for (listener in contactsUpdatedListeners) {
listener.onContactUpdated(contact) 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)) { if (!sipContacts.contains(contact)) {
sipContacts.add(contact) sipContacts.add(contact)
return true 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") 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) val address: Address? = coreContext.core.interpretUrl(data1)
if (address == null) { if (address == null) {
Log.e("[Native Contact] Couldn't parse address $data1 !") Log.e("[Native Contact] Couldn't parse address $data1 !")

View file

@ -180,17 +180,17 @@ class NativeContactEditor(val contact: NativeContact) {
phoneNumber.currentValue.isEmpty() -> { phoneNumber.currentValue.isEmpty() -> {
// New phone number to add // New phone number to add
addCount++ addCount++
addNumber(phoneNumber) addNumber(phoneNumber.newValue.value.orEmpty())
} }
phoneNumber.toRemove.value == true -> { phoneNumber.toRemove.value == true -> {
// Existing number to remove // Existing number to remove
removeCount++ removeCount++
removeNumber(phoneNumber) removeNumber(phoneNumber.currentValue)
} }
phoneNumber.currentValue != phoneNumber.newValue.value -> { phoneNumber.currentValue != phoneNumber.newValue.value -> {
// Existing number to update // Existing number to update
editCount++ editCount++
updateNumber(phoneNumber) updateNumber(phoneNumber.currentValue, phoneNumber.newValue.value.orEmpty())
} }
} }
} }
@ -210,20 +210,20 @@ class NativeContactEditor(val contact: NativeContact) {
// New address to add // New address to add
addCount++ addCount++
if (useLinphoneSyncAccount) { if (useLinphoneSyncAccount) {
addAddress(sipAddress) addAddress(sipAddress.newValue.value.orEmpty())
} else { } else {
addSipAddress(sipAddress) addSipAddress(sipAddress.newValue.value.orEmpty())
} }
} }
sipAddress.toRemove.value == true -> { sipAddress.toRemove.value == true -> {
// Existing address to remove // Existing address to remove
removeCount++ removeCount++
removeAddress(sipAddress) removeAddress(sipAddress.currentValue)
} }
sipAddress.currentValue != sipAddress.newValue.value -> { sipAddress.currentValue != sipAddress.newValue.value -> {
// Existing address to update // Existing address to update
editCount++ editCount++
updateAddress(sipAddress) updateAddress(sipAddress.currentValue, sipAddress.newValue.value.orEmpty())
} }
} }
} }
@ -238,6 +238,18 @@ class NativeContactEditor(val contact: NativeContact) {
return this 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() { fun commit() {
if (PermissionHelper.get().hasWriteContactsPermission()) { if (PermissionHelper.get().hasWriteContactsPermission()) {
try { try {
@ -269,14 +281,14 @@ class NativeContactEditor(val contact: NativeContact) {
changes.add(operation) changes.add(operation)
} }
private fun addNumber(number: NumberOrAddressEditorViewModel) { private fun addNumber(number: 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.newValue.value) .withValue(CommonDataKinds.Phone.NUMBER, number)
.withValue( .withValue(
CommonDataKinds.Phone.TYPE, CommonDataKinds.Phone.TYPE,
CommonDataKinds.Phone.TYPE_MOBILE CommonDataKinds.Phone.TYPE_MOBILE
@ -285,19 +297,19 @@ class NativeContactEditor(val contact: NativeContact) {
addChanges(insert) addChanges(insert)
} }
private fun updateNumber(number: NumberOrAddressEditorViewModel) { private fun updateNumber(currentValue: String, newValue: String) {
val update = ContentProviderOperation.newUpdate(contactUri) val update = ContentProviderOperation.newUpdate(contactUri)
.withSelection( .withSelection(
phoneNumberSelection, phoneNumberSelection,
arrayOf( arrayOf(
contact.nativeId, contact.nativeId,
CommonDataKinds.Phone.CONTENT_ITEM_TYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
number.currentValue, currentValue,
number.currentValue currentValue
) )
) )
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Phone.NUMBER, number.newValue.value) .withValue(CommonDataKinds.Phone.NUMBER, newValue)
.withValue( .withValue(
CommonDataKinds.Phone.TYPE, CommonDataKinds.Phone.TYPE,
CommonDataKinds.Phone.TYPE_MOBILE CommonDataKinds.Phone.TYPE_MOBILE
@ -306,48 +318,48 @@ class NativeContactEditor(val contact: NativeContact) {
addChanges(update) addChanges(update)
} }
private fun removeNumber(number: NumberOrAddressEditorViewModel) { private fun removeNumber(number: 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.currentValue, number,
number.currentValue number
) )
) )
.build() .build()
addChanges(delete) addChanges(delete)
} }
private fun addAddress(address: NumberOrAddressEditorViewModel) { private fun addAddress(address: 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(
ContactsContract.Data.MIMETYPE, ContactsContract.Data.MIMETYPE,
AppUtils.getString(R.string.linphone_address_mime_type) 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("data2", AppUtils.getString(R.string.app_name)) // summary
.withValue("data3", address.newValue.value) // detail .withValue("data3", address) // detail
.build() .build()
addChanges(insert) addChanges(insert)
} }
private fun addSipAddress(address: NumberOrAddressEditorViewModel) { private fun addSipAddress(address: 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.SipAddress.CONTENT_ITEM_TYPE CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE
) )
.withValue("data1", address.newValue.value) // value .withValue("data1", address) // value
.build() .build()
addChanges(insert) addChanges(insert)
} }
private fun updateAddress(address: NumberOrAddressEditorViewModel) { private fun updateAddress(currentValue: String, newValue: String) {
val update = ContentProviderOperation.newUpdate(contactUri) val update = ContentProviderOperation.newUpdate(contactUri)
.withSelection( .withSelection(
sipAddressSelection, sipAddressSelection,
@ -355,21 +367,21 @@ class NativeContactEditor(val contact: NativeContact) {
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.currentValue currentValue
) )
) )
.withValue( .withValue(
ContactsContract.Data.MIMETYPE, ContactsContract.Data.MIMETYPE,
AppUtils.getString(R.string.linphone_address_mime_type) 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("data2", AppUtils.getString(R.string.app_name)) // summary
.withValue("data3", address.newValue.value) // detail .withValue("data3", newValue) // detail
.build() .build()
addChanges(update) addChanges(update)
} }
private fun removeAddress(address: NumberOrAddressEditorViewModel) { private fun removeAddress(address: String) {
val delete = ContentProviderOperation.newDelete(contactUri) val delete = ContentProviderOperation.newDelete(contactUri)
.withSelection( .withSelection(
sipAddressSelection, sipAddressSelection,
@ -377,13 +389,53 @@ class NativeContactEditor(val contact: NativeContact) {
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.currentValue address
) )
) )
.build() .build()
addChanges(delete) 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() { private fun updatePicture() {
val value = pictureByteArray val value = pictureByteArray
val id = rawId val id = rawId

View file

@ -118,7 +118,6 @@ class CorePreferences constructor(private val context: Context) {
/* Contacts */ /* Contacts */
// TODO: use it
var storePresenceInNativeContact: Boolean var storePresenceInNativeContact: Boolean
get() = config.getBool("app", "store_presence_in_native_contact", false) get() = config.getBool("app", "store_presence_in_native_contact", false)
set(value) { set(value) {
@ -199,7 +198,7 @@ class CorePreferences constructor(private val context: Context) {
config.setString("app", "voice_mail", value) 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 val defaultDomain: String
get() = config.getString("app", "default_domain", "sip.linphone.org") get() = config.getString("app", "default_domain", "sip.linphone.org")