From f8ac74ee974f2cb5cd84751dbfc10f349cb0ff88 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 1 Jun 2020 15:05:58 +0200 Subject: [PATCH] Try to prevent databinding build issue + few changes for contact editor to be able to choose in which sync account create the contact --- app/build.gradle | 3 ++ .../viewmodels/ContactEditorViewModel.kt | 5 ++- .../org/linphone/contact/ContactsManager.kt | 23 +++++++++++- .../linphone/contact/NativeContactEditor.kt | 36 ++++++++++++++++--- .../java/org/linphone/core/CorePreferences.kt | 2 +- .../main/java/org/linphone/utils/AppUtils.kt | 20 ----------- 6 files changed, 59 insertions(+), 30 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a719efc91..0556c59b0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -181,3 +181,6 @@ ktlint { } project.tasks['preBuild'].dependsOn 'ktlintFormat' + +// Always clean on pre-build, prevents build issues caused by data binding +project.tasks['preBuild'].dependsOn 'clean' \ No newline at end of file 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 9aaec73ed..439b4c355 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 @@ -31,7 +31,6 @@ import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.corePreferences 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,14 +81,14 @@ class ContactEditorViewModel(val c: Contact?) : ViewModel(), ContactViewModelInt if (contact == null) { created = true contact = if (PermissionHelper.get().hasWriteContactsPermission()) { - NativeContact(AppUtils.createAndroidContact().toString()) + NativeContact(NativeContactEditor.createAndroidContact(null, null).toString()) } else { Contact() } } if (contact is NativeContact) { - NativeContactEditor(contact) + NativeContactEditor(contact, null, null) .setFirstAndLastNames(firstName.value.orEmpty(), lastName.value.orEmpty()) .setOrganization(organization.value.orEmpty()) .setPhoneNumbers(numbers.value.orEmpty()) diff --git a/app/src/main/java/org/linphone/contact/ContactsManager.kt b/app/src/main/java/org/linphone/contact/ContactsManager.kt index 17e0ee821..6b58e2a82 100644 --- a/app/src/main/java/org/linphone/contact/ContactsManager.kt +++ b/app/src/main/java/org/linphone/contact/ContactsManager.kt @@ -21,6 +21,7 @@ package org.linphone.contact import android.accounts.Account import android.accounts.AccountManager +import android.content.ContentResolver import android.content.Context import android.database.ContentObserver import android.net.Uri @@ -256,6 +257,26 @@ class ContactsManager(private val context: Context) { } } + fun getAvailableSyncAccounts(): List> { + val accountManager = context.getSystemService(Context.ACCOUNT_SERVICE) as AccountManager + val syncAdapters = ContentResolver.getSyncAdapterTypes() + val available = arrayListOf>() + + for (syncAdapter in syncAdapters) { + if (syncAdapter.authority == "com.android.contacts" && syncAdapter.isUserVisible) { + Log.i("[Contacts Manager] Found sync adapter for com.android.contacts authority: ${syncAdapter.accountType}") + val accounts = accountManager.getAccountsByType(syncAdapter.accountType) + for (account in accounts) { + Log.i("[Contacts Manager] Found account for account type ${syncAdapter.accountType}: ${account.name}") + val pair = Pair(account.name, account.type) + available.add(pair) + } + } + } + + return available + } + @Synchronized private fun refreshContactOnPresenceReceived(friend: Friend): Boolean { if (friend.userData == null) return false @@ -272,7 +293,7 @@ class ContactsManager(private val context: Context) { 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() + NativeContactEditor(contact, null, null).setPresenceInformation(phoneNumber, sipAddress).commit() } } } diff --git a/app/src/main/java/org/linphone/contact/NativeContactEditor.kt b/app/src/main/java/org/linphone/contact/NativeContactEditor.kt index c07ff88f3..cd96de763 100644 --- a/app/src/main/java/org/linphone/contact/NativeContactEditor.kt +++ b/app/src/main/java/org/linphone/contact/NativeContactEditor.kt @@ -21,6 +21,7 @@ package org.linphone.contact import android.content.ContentProviderOperation import android.content.ContentUris +import android.content.ContentValues import android.net.Uri import android.provider.ContactsContract import android.provider.ContactsContract.CommonDataKinds @@ -33,7 +34,29 @@ import org.linphone.core.tools.Log import org.linphone.utils.AppUtils import org.linphone.utils.PermissionHelper -class NativeContactEditor(val contact: NativeContact) { +class NativeContactEditor( + val contact: NativeContact, + private var syncAccountName: String?, + private var syncAccountType: String? +) { + companion object { + fun createAndroidContact(accountName: String?, accountType: String?): Long { + val values = ContentValues() + + if (accountName == null && accountType == null && corePreferences.useLinphoneSyncAccount) { + values.put(RawContacts.ACCOUNT_NAME, AppUtils.getString(R.string.sync_account_name)) + values.put(RawContacts.ACCOUNT_TYPE, AppUtils.getString(R.string.sync_account_type)) + } else { + values.put(RawContacts.ACCOUNT_NAME, accountName) + values.put(RawContacts.ACCOUNT_TYPE, accountType) + } + + val rawContactUri = coreContext.context.contentResolver + .insert(RawContacts.CONTENT_URI, values) + return ContentUris.parseId(rawContactUri) + } + } + private val changes = arrayListOf() private val selection = "${ContactsContract.Data.CONTACT_ID} =? AND ${ContactsContract.Data.MIMETYPE} =?" @@ -49,10 +72,13 @@ class NativeContactEditor(val contact: NativeContact) { private var pictureByteArray: ByteArray? = null init { - val contentResolver = coreContext.context.contentResolver - val syncAccountType = AppUtils.getString(R.string.sync_account_type) - val syncAccountName = AppUtils.getString(R.string.sync_account_name) + if (syncAccountName == null && syncAccountType == null && useLinphoneSyncAccount) { + syncAccountName = AppUtils.getString(R.string.sync_account_name) + syncAccountType = AppUtils.getString(R.string.sync_account_type) + } + Log.i("[Native Contact Editor] Using sync account $syncAccountName with type $syncAccountType") + val contentResolver = coreContext.context.contentResolver val cursor = contentResolver.query( RawContacts.CONTENT_URI, arrayOf(RawContacts._ID, RawContacts.ACCOUNT_TYPE), @@ -76,7 +102,7 @@ class NativeContactEditor(val contact: NativeContact) { } cursor?.close() - // When contact has been created with AppUtils.createAndroidContact this is required + // When contact has been created with NativeContactEditor.createAndroidContact this is required if (rawId == null) rawId = contact.nativeId if (linphoneRawId == null && useLinphoneSyncAccount) { diff --git a/app/src/main/java/org/linphone/core/CorePreferences.kt b/app/src/main/java/org/linphone/core/CorePreferences.kt index ce6df4055..74d5bca61 100644 --- a/app/src/main/java/org/linphone/core/CorePreferences.kt +++ b/app/src/main/java/org/linphone/core/CorePreferences.kt @@ -228,7 +228,7 @@ class CorePreferences constructor(private val context: Context) { get() = config.getBool("app", "display_contact_organization", true) // If enabled, SIP addresses will be stored in a different raw id than the contact and with a custom MIME type - // If disabled, account won't be created + // If disabled, contacts will be created in default account & addresses will be stored in native SIP field val useLinphoneSyncAccount: Boolean get() = config.getBool("app", "use_linphone_tag", true) diff --git a/app/src/main/java/org/linphone/utils/AppUtils.kt b/app/src/main/java/org/linphone/utils/AppUtils.kt index 13affca54..2403ad7c7 100644 --- a/app/src/main/java/org/linphone/utils/AppUtils.kt +++ b/app/src/main/java/org/linphone/utils/AppUtils.kt @@ -21,14 +21,12 @@ package org.linphone.utils import android.app.Activity import android.content.* -import android.provider.ContactsContract import android.text.Spanned import android.util.TypedValue import androidx.core.text.HtmlCompat import java.util.* import java.util.regex.Pattern import org.linphone.LinphoneApplication.Companion.coreContext -import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.R import org.linphone.core.tools.Log @@ -102,24 +100,6 @@ class AppUtils { } } - fun createAndroidContact(): Long { - var accountType: String? = null - var accountName: String? = null - - if (corePreferences.useLinphoneSyncAccount) { - accountType = getString(org.linphone.R.string.sync_account_type) - accountName = getString(org.linphone.R.string.sync_account_name) - } - - val values = ContentValues() - values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType) - values.put(ContactsContract.RawContacts.ACCOUNT_NAME, accountName) - - val rawContactUri = coreContext.context.contentResolver - .insert(ContactsContract.RawContacts.CONTENT_URI, values) - return ContentUris.parseId(rawContactUri) - } - fun pixelsToDp(pixels: Float): Float { return TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP,