From e5f3da5434faf3876c3b0734a05ed7a8841b0da5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 1 May 2020 11:10:38 +0200 Subject: [PATCH] show the callers name, number and photo, if available --- app/build.gradle | 2 +- .../contacts/pro/activities/CallActivity.kt | 24 +++++++++++++++++ .../contacts/pro/extensions/Context.kt | 6 ++--- .../contacts/pro/helpers/CallManager.kt | 27 +++++++++++++++++++ .../contacts/pro/models/CallContact.kt | 4 +++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/CallContact.kt diff --git a/app/build.gradle b/app/build.gradle index b2fb0158..fa341d70 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -57,7 +57,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.26.33' + implementation 'com.simplemobiletools:commons:5.27.0' implementation 'joda-time:joda-time:2.10.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4' implementation 'com.googlecode.ez-vcard:ez-vcard:0.10.5' diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/CallActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/CallActivity.kt index 3327a40b..c41b5c86 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/CallActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/CallActivity.kt @@ -10,6 +10,10 @@ import android.os.Bundle import android.telecom.Call import android.widget.RemoteViews import androidx.core.app.NotificationCompat +import com.bumptech.glide.Glide +import com.bumptech.glide.load.engine.DiskCacheStrategy +import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions +import com.bumptech.glide.request.RequestOptions import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.isOreoPlus import com.simplemobiletools.contacts.pro.R @@ -33,8 +37,10 @@ class CallActivity : SimpleActivity() { updateTextColors(call_holder) initButtons() showNotification() + CallManager.registerCallback(getCallCallback()) updateCallState(CallManager.getState()) + updateOtherPersonsInfo() } override fun onDestroy() { @@ -77,6 +83,24 @@ class CallActivity : SimpleActivity() { call_toggle_microphone.setImageDrawable(getDrawable(drawable)) } + private fun updateOtherPersonsInfo() { + val callContact = CallManager.getCallContact(applicationContext) ?: return + caller_name_label.text = callContact.name + caller_number_label.text = callContact.number + caller_number_label.beVisibleIf(callContact.number.isNotEmpty()) + + val options = RequestOptions() + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .centerCrop() + + Glide.with(this) + .load(callContact.photoUri) + .transition(DrawableTransitionOptions.withCrossFade()) + .apply(options) + .apply(RequestOptions.circleCropTransform()) + .into(caller_avatar) + } + private fun updateCallState(state: Int) { when (state) { Call.STATE_ACTIVE -> callStarted() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt index 655d087b..7429ddae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt @@ -36,7 +36,7 @@ fun Context.getEmptyContact(): Contact { val originalContactSource = if (hasContactPermissions()) config.lastUsedContactSource else SMT_PRIVATE val organization = Organization("", "") return Contact(0, "", "", "", "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), originalContactSource, 0, 0, "", - null, "", ArrayList(), organization, ArrayList(), ArrayList()) + null, "", ArrayList(), organization, ArrayList(), ArrayList()) } fun Context.viewContact(contact: Contact) { @@ -226,7 +226,7 @@ fun Context.sendSMSToContacts(contacts: ArrayList) { val numbers = StringBuilder() contacts.forEach { val number = it.phoneNumbers.firstOrNull { it.type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE } - ?: it.phoneNumbers.firstOrNull() + ?: it.phoneNumbers.firstOrNull() if (number != null) { numbers.append("${number.value};") } @@ -312,7 +312,7 @@ fun Context.getVisibleContactSources(): ArrayList { val sources = getAllContactSources() val ignoredContactSources = config.ignoredContactSources return ArrayList(sources).filter { !ignoredContactSources.contains(it.getFullIdentifier()) } - .map { it.name }.toMutableList() as ArrayList + .map { it.name }.toMutableList() as ArrayList } fun Context.getAllContactSources(): ArrayList { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/CallManager.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/CallManager.kt index a1b70832..7680ec20 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/CallManager.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/CallManager.kt @@ -1,8 +1,13 @@ package com.simplemobiletools.contacts.pro.helpers import android.annotation.SuppressLint +import android.content.Context +import android.net.Uri import android.telecom.Call import android.telecom.VideoProfile +import com.simplemobiletools.commons.extensions.getNameFromPhoneNumber +import com.simplemobiletools.commons.extensions.getPhotoUriFromPhoneNumber +import com.simplemobiletools.contacts.pro.models.CallContact // inspired by https://github.com/Chooloo/call_manage @SuppressLint("NewApi") @@ -35,5 +40,27 @@ class CallManager { } else { call!!.state } + + fun getCallContact(context: Context): CallContact? { + val callContact = CallContact("", "", "") + if (call == null) { + return callContact + } + + val uri = Uri.decode(call!!.details.handle.toString()) + if (uri.startsWith("tel:")) { + val number = uri.substringAfter("tel:") + callContact.number = number + callContact.name = context.getNameFromPhoneNumber(number) + callContact.photoUri = context.getPhotoUriFromPhoneNumber(number) + } + + if (callContact.name.isEmpty() && callContact.number.isNotEmpty()) { + callContact.name = callContact.number + callContact.number = "" + } + + return callContact + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/CallContact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/CallContact.kt new file mode 100644 index 00000000..d40a4e34 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/CallContact.kt @@ -0,0 +1,4 @@ +package com.simplemobiletools.contacts.pro.models + +// a simpler Contact model containing just info needed at the call screen +data class CallContact(var name: String, var number: String, var photoUri: String)