From 5caa304b7f1c8134abe64c2c2a83e4ef6cebc780 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 11 Jun 2020 10:46:15 +0200 Subject: [PATCH] Using coroutine for presence storage in native contact + changed verbosity of some recurring logs + switched to java 8 --- app/build.gradle | 14 +++++++++---- .../org/linphone/contact/ContactsManager.kt | 21 ++++++++++++------- .../linphone/contact/NativeContactEditor.kt | 8 +++---- .../org/linphone/utils/PermissionHelper.kt | 9 ++++++-- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 72ddf8cc5..3a2ef5b0a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -126,6 +126,15 @@ android { buildFeatures { dataBinding = true } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + + kotlinOptions { + jvmTarget = "1.8" + } } repositories { @@ -182,7 +191,4 @@ ktlint { ignoreFailures = true } -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 +project.tasks['preBuild'].dependsOn 'ktlintFormat' \ No newline at end of file diff --git a/app/src/main/java/org/linphone/contact/ContactsManager.kt b/app/src/main/java/org/linphone/contact/ContactsManager.kt index 990fbe461..190d66313 100644 --- a/app/src/main/java/org/linphone/contact/ContactsManager.kt +++ b/app/src/main/java/org/linphone/contact/ContactsManager.kt @@ -28,6 +28,8 @@ import android.net.Uri import android.os.AsyncTask.THREAD_POOL_EXECUTOR import android.provider.ContactsContract import android.util.Patterns +import com.google.android.gms.tasks.Tasks.await +import kotlinx.coroutines.* import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.R @@ -332,14 +334,17 @@ class ContactsManager(private val context: Context) { if (sipAddress != null) { Log.d("[Contacts Manager] Found presence information to store in native contact $contact") val contactEditor = NativeContactEditor(contact, null, null) - // TODO: could be great to do in a coroutine - // launch { - // withContext(Dispatchers.IO) { - contactEditor.setPresenceInformation(phoneNumber, sipAddress).commit() - // } - // } - for (listener in contactsUpdatedListeners) { - listener.onContactUpdated(contact) + val coroutineScope = CoroutineScope(Dispatchers.Main) + coroutineScope.launch { + val deferred = async { + withContext(Dispatchers.IO) { + contactEditor.setPresenceInformation(phoneNumber, sipAddress).commit() + } + } + deferred.await() + for (listener in contactsUpdatedListeners) { + listener.onContactUpdated(contact) + } } } } diff --git a/app/src/main/java/org/linphone/contact/NativeContactEditor.kt b/app/src/main/java/org/linphone/contact/NativeContactEditor.kt index 840acccd9..8d7660623 100644 --- a/app/src/main/java/org/linphone/contact/NativeContactEditor.kt +++ b/app/src/main/java/org/linphone/contact/NativeContactEditor.kt @@ -92,13 +92,13 @@ class NativeContactEditor( do { if (rawId == null) { rawId = cursor.getString(cursor.getColumnIndex(RawContacts._ID)) - Log.i("[Native Contact Editor] Found raw id $rawId for native contact with id ${contact.nativeId}") + Log.d("[Native Contact Editor] Found raw id $rawId for native contact with id ${contact.nativeId}") } val accountType = cursor.getString(cursor.getColumnIndex(RawContacts.ACCOUNT_TYPE)) if (accountType == syncAccountType && linphoneRawId == null) { linphoneRawId = cursor.getString(cursor.getColumnIndex(RawContacts._ID)) - Log.i("[Native Contact Editor] Found linphone raw id $linphoneRawId for native contact with id ${contact.nativeId}") + Log.d("[Native Contact Editor] Found linphone raw id $linphoneRawId for native contact with id ${contact.nativeId}") } } while (cursor.moveToNext() && linphoneRawId == null) } @@ -447,7 +447,7 @@ class NativeContactEditor( addLinphoneAddress(sipAddress, phoneNumber) } else { if (data1 != null && data1 == sipAddress) { - Log.i("[Native Contact Editor] There is already an entry for this phone number and SIP address, skipping") + Log.d("[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) @@ -495,7 +495,7 @@ class NativeContactEditor( Log.i("[Native Contact Editor] SIP address not found, let's add it") addSipAddress(sipAddress) } else { - Log.i("[Native Contact Editor] There is already an entry for this SIP address, skipping") + Log.d("[Native Contact Editor] There is already an entry for this SIP address, skipping") } } diff --git a/app/src/main/java/org/linphone/utils/PermissionHelper.kt b/app/src/main/java/org/linphone/utils/PermissionHelper.kt index 14575d499..9f1798501 100644 --- a/app/src/main/java/org/linphone/utils/PermissionHelper.kt +++ b/app/src/main/java/org/linphone/utils/PermissionHelper.kt @@ -32,8 +32,13 @@ class PermissionHelper private constructor(private val context: Context) { private fun hasPermission(permission: String): Boolean { val granted = Compatibility.hasPermission(context, permission) - val result = if (granted) "granted" else "denied" - Log.i("[Permission Helper] Permission $permission is $result") + + if (granted) { + Log.d("[Permission Helper] Permission $permission is granted") + } else { + Log.w("[Permission Helper] Permission $permission is denied") + } + return granted }