Added setting to choose wether to save new contact to default or app sync account + fixed contact sip address update

This commit is contained in:
Sylvain Berfini 2020-08-18 16:45:59 +02:00
parent 88befd40ac
commit c28f2373d9
8 changed files with 51 additions and 12 deletions

View file

@ -29,8 +29,10 @@ import java.io.ByteArrayOutputStream
import java.io.IOException
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
import org.linphone.contact.*
import org.linphone.core.tools.Log
import org.linphone.utils.AppUtils
import org.linphone.utils.ImageUtils
import org.linphone.utils.PermissionHelper
@ -82,7 +84,11 @@ class ContactEditorViewModel(val c: Contact?) : ViewModel(), ContactViewModelInt
created = true
contact = if (PermissionHelper.get().hasWriteContactsPermission()) {
// Store native contact in default sync account
NativeContact(NativeContactEditor.createAndroidContact().toString())
if (corePreferences.storeCreatedContactsInAppSyncAccount) {
NativeContact(NativeContactEditor.createAndroidContact(AppUtils.getString(R.string.sync_account_name), AppUtils.getString(R.string.sync_account_type)).toString())
} else {
NativeContact(NativeContactEditor.createAndroidContact(null, null).toString())
}
} else {
Contact()
}

View file

@ -74,7 +74,7 @@ class ContactViewModel(private val c: Contact) : ErrorReportingViewModel(), Cont
private val contactsUpdatedListener = object : ContactsUpdatedListenerStub() {
override fun onContactUpdated(contact: Contact) {
if (c is NativeContact && contact is NativeContact && c.nativeId == contact.nativeId) {
Log.i("[Contact] $contact has changed")
Log.d("[Contact] $contact has changed")
updateNumbersAndAddresses()
}
}

View file

@ -38,6 +38,13 @@ class ContactsSettingsViewModel : GenericSettingsViewModel() {
}
val friendListSubscribe = MutableLiveData<Boolean>()
val contactStorageAppSyncAccountListener = object : SettingListenerStub() {
override fun onBoolValueChanged(newValue: Boolean) {
prefs.storeCreatedContactsInAppSyncAccount = newValue
}
}
val contactStorageAppSyncAccount = MutableLiveData<Boolean>()
val nativePresenceListener = object : SettingListenerStub() {
override fun onBoolValueChanged(newValue: Boolean) {
if (newValue) {
@ -73,6 +80,7 @@ class ContactsSettingsViewModel : GenericSettingsViewModel() {
readContactsPermissionGranted.value = PermissionHelper.get().hasReadContactsPermission()
friendListSubscribe.value = core.isFriendListSubscriptionEnabled
contactStorageAppSyncAccount.value = prefs.storeCreatedContactsInAppSyncAccount
nativePresence.value = prefs.storePresenceInNativeContact
showOrganization.value = prefs.displayOrganization
launcherShortcuts.value = prefs.contactsShortcuts

View file

@ -133,7 +133,7 @@ class NativeContact(val nativeId: String, private val lookupKey: String? = null)
val stringAddress = address.asStringUriOnly()
if (!rawSipAddresses.contains(stringAddress)) {
sipAddresses.add(address)
rawSipAddresses.add(stringAddress)
rawSipAddresses.add(data1)
}
}
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE -> {

View file

@ -39,7 +39,7 @@ class NativeContactEditor(
private var syncAccountType: String? = null
) {
companion object {
fun createAndroidContact(accountName: String? = null, accountType: String? = null): Long {
fun createAndroidContact(accountName: String?, accountType: String?): Long {
val values = ContentValues()
values.put(RawContacts.ACCOUNT_NAME, accountName)
values.put(RawContacts.ACCOUNT_TYPE, accountType)
@ -371,31 +371,40 @@ class NativeContactEditor(
}
private fun updateLinphoneOrSipAddress(currentValue: String, sipAddress: String) {
val update = ContentProviderOperation.newUpdate(contactUri)
val updateLegacy = ContentProviderOperation.newUpdate(contactUri)
.withSelection(
sipAddressSelection,
"${ContactsContract.Data.CONTACT_ID} =? AND ${ContactsContract.Data.MIMETYPE} =? AND data1=?",
arrayOf(
contact.nativeId,
CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE,
AppUtils.getString(R.string.linphone_address_mime_type),
currentValue
)
)
.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", sipAddress) // detail
.build()
val update = ContentProviderOperation.newUpdate(contactUri)
.withSelection(
"${ContactsContract.Data.CONTACT_ID} =? AND ${ContactsContract.Data.MIMETYPE} =? AND data1=?",
arrayOf(
contact.nativeId,
CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE,
currentValue
)
)
.withValue("data1", sipAddress) // value
.build()
addChanges(updateLegacy)
addChanges(update)
}
private fun removeLinphoneOrSipAddress(sipAddress: String) {
val delete = ContentProviderOperation.newDelete(contactUri)
.withSelection(
sipAddressSelection,
"${ContactsContract.Data.CONTACT_ID} =? AND (${ContactsContract.Data.MIMETYPE} =? OR ${ContactsContract.Data.MIMETYPE} =?) AND data1=?",
arrayOf(
contact.nativeId,
CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE,

View file

@ -119,6 +119,12 @@ class CorePreferences constructor(private val context: Context) {
/* Contacts */
var storeCreatedContactsInAppSyncAccount: Boolean
get() = config.getBool("app", "store_contacts_in_app_sync_account", false)
set(value) {
config.setBool("app", "store_contacts_in_app_sync_account", value)
}
var storePresenceInNativeContact: Boolean
get() = config.getBool("app", "store_presence_in_native_contact", false)
set(value) {

View file

@ -72,6 +72,14 @@
linphone:checked="@={viewModel.friendListSubscribe}"
linphone:enabled="@{viewModel.readContactsPermissionGranted}"/>
<include
layout="@layout/settings_widget_switch"
linphone:title="@{@string/contacts_settings_contact_storage_title}"
linphone:subtitle="@{@string/contacts_settings_contact_storage_summary}"
linphone:listener="@{viewModel.contactStorageAppSyncAccountListener}"
linphone:checked="@={viewModel.contactStorageAppSyncAccount}"
linphone:enabled="@{viewModel.readContactsPermissionGranted}"/>
<include
layout="@layout/settings_widget_switch"
linphone:title="@{@string/contacts_settings_native_presence_title}"

View file

@ -430,6 +430,8 @@
<!-- Contacts settings -->
<string name="contacts_settings_friendlist_subscribe_title">Friendlist subscribe</string>
<string name="contacts_settings_friendlist_subscribe_summary"></string>
<string name="contacts_settings_contact_storage_title">Store new contacts in the app sync account</string>
<string name="contacts_settings_contact_storage_summary">If enabled, contacts created via the app will be removed when the app will be uninstalled.</string>
<string name="contacts_settings_native_presence_title">Presence information in native contact</string>
<string name="contacts_settings_native_presence_summary">Inserting information shortcuts from the &appName; contact into native Android contacts</string>
<string name="contacts_settings_show_organization_title">Display contact organization</string>