From c28f2373d9d7b7d47ba8aacedfe27cc1e9d0c70f Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 18 Aug 2020 16:45:59 +0200 Subject: [PATCH] Added setting to choose wether to save new contact to default or app sync account + fixed contact sip address update --- .../viewmodels/ContactEditorViewModel.kt | 8 +++++- .../contact/viewmodels/ContactViewModel.kt | 2 +- .../viewmodels/ContactsSettingsViewModel.kt | 8 ++++++ .../org/linphone/contact/NativeContact.kt | 2 +- .../linphone/contact/NativeContactEditor.kt | 27 ++++++++++++------- .../java/org/linphone/core/CorePreferences.kt | 6 +++++ .../res/layout/settings_contacts_fragment.xml | 8 ++++++ app/src/main/res/values/strings.xml | 2 ++ 8 files changed, 51 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/linphone/activities/main/contact/viewmodels/ContactEditorViewModel.kt b/app/src/main/java/org/linphone/activities/main/contact/viewmodels/ContactEditorViewModel.kt index 052e3707c..988dd36bd 100644 --- a/app/src/main/java/org/linphone/activities/main/contact/viewmodels/ContactEditorViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/contact/viewmodels/ContactEditorViewModel.kt @@ -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() } diff --git a/app/src/main/java/org/linphone/activities/main/contact/viewmodels/ContactViewModel.kt b/app/src/main/java/org/linphone/activities/main/contact/viewmodels/ContactViewModel.kt index f1c58d1fe..e9cf86ac7 100644 --- a/app/src/main/java/org/linphone/activities/main/contact/viewmodels/ContactViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/contact/viewmodels/ContactViewModel.kt @@ -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() } } diff --git a/app/src/main/java/org/linphone/activities/main/settings/viewmodels/ContactsSettingsViewModel.kt b/app/src/main/java/org/linphone/activities/main/settings/viewmodels/ContactsSettingsViewModel.kt index be6a160ae..2f214e1b3 100644 --- a/app/src/main/java/org/linphone/activities/main/settings/viewmodels/ContactsSettingsViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/settings/viewmodels/ContactsSettingsViewModel.kt @@ -38,6 +38,13 @@ class ContactsSettingsViewModel : GenericSettingsViewModel() { } val friendListSubscribe = MutableLiveData() + val contactStorageAppSyncAccountListener = object : SettingListenerStub() { + override fun onBoolValueChanged(newValue: Boolean) { + prefs.storeCreatedContactsInAppSyncAccount = newValue + } + } + val contactStorageAppSyncAccount = MutableLiveData() + 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 diff --git a/app/src/main/java/org/linphone/contact/NativeContact.kt b/app/src/main/java/org/linphone/contact/NativeContact.kt index fc0f280d1..713ba175e 100644 --- a/app/src/main/java/org/linphone/contact/NativeContact.kt +++ b/app/src/main/java/org/linphone/contact/NativeContact.kt @@ -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 -> { diff --git a/app/src/main/java/org/linphone/contact/NativeContactEditor.kt b/app/src/main/java/org/linphone/contact/NativeContactEditor.kt index 3da2d7b91..b07f63d57 100644 --- a/app/src/main/java/org/linphone/contact/NativeContactEditor.kt +++ b/app/src/main/java/org/linphone/contact/NativeContactEditor.kt @@ -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, diff --git a/app/src/main/java/org/linphone/core/CorePreferences.kt b/app/src/main/java/org/linphone/core/CorePreferences.kt index 25e6a7aa3..715c986bf 100644 --- a/app/src/main/java/org/linphone/core/CorePreferences.kt +++ b/app/src/main/java/org/linphone/core/CorePreferences.kt @@ -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) { diff --git a/app/src/main/res/layout/settings_contacts_fragment.xml b/app/src/main/res/layout/settings_contacts_fragment.xml index 9c1b6810b..5d21c62d9 100644 --- a/app/src/main/res/layout/settings_contacts_fragment.xml +++ b/app/src/main/res/layout/settings_contacts_fragment.xml @@ -72,6 +72,14 @@ linphone:checked="@={viewModel.friendListSubscribe}" linphone:enabled="@{viewModel.readContactsPermissionGranted}"/> + + Friendlist subscribe + Store new contacts in the app sync account + If enabled, contacts created via the app will be removed when the app will be uninstalled. Presence information in native contact Inserting information shortcuts from the &appName; contact into native Android contacts Display contact organization