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 090641cb..e4e67839 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 @@ -56,11 +56,16 @@ class CallActivity : SimpleActivity() { initButtons() audioManager.mode = AudioManager.MODE_IN_CALL - callContact = CallManager.getCallContact(applicationContext) - callContactAvatar = getCallContactAvatar() + CallManager.getCallContact(applicationContext) { contact -> + callContact = contact + callContactAvatar = getCallContactAvatar() + setupNotification() + runOnUiThread { + updateOtherPersonsInfo() + } + } + addLockScreenFlags() - setupNotification() - updateOtherPersonsInfo() initProximitySensor() CallManager.registerCallback(callCallback) @@ -176,8 +181,7 @@ class CallActivity : SimpleActivity() { return } - val callContact = CallManager.getCallContact(applicationContext) ?: return - caller_name_label.text = if (callContact.name.isNotEmpty()) callContact.name else getString(R.string.unknown_caller) + caller_name_label.text = if (callContact!!.name.isNotEmpty()) callContact!!.name else getString(R.string.unknown_caller) if (callContactAvatar != null) { caller_avatar.setImageBitmap(callContactAvatar) 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 102e0c40..0661cdc1 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 @@ -7,6 +7,8 @@ import android.telecom.Call import android.telecom.VideoProfile import com.simplemobiletools.commons.extensions.getNameFromPhoneNumber import com.simplemobiletools.commons.extensions.getPhotoUriFromPhoneNumber +import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import com.simplemobiletools.contacts.pro.extensions.contactsDB import com.simplemobiletools.contacts.pro.models.CallContact // inspired by https://github.com/Chooloo/call_manage @@ -50,10 +52,11 @@ class CallManager { call?.stopDtmfTone() } - fun getCallContact(context: Context): CallContact? { + fun getCallContact(context: Context, callback: (CallContact?) -> Unit) { val callContact = CallContact("", "", "") if (call == null) { - return callContact + callback(callContact) + return } val uri = Uri.decode(call!!.details.handle.toString()) @@ -62,9 +65,23 @@ class CallManager { callContact.number = number callContact.name = context.getNameFromPhoneNumber(number) callContact.photoUri = context.getPhotoUriFromPhoneNumber(number) - } - return callContact + if (callContact.name == callContact.number) { + ensureBackgroundThread { + val localContact = context.contactsDB.getContactWithNumber("%$number%") + if (localContact != null) { + val storedGroups = ContactsHelper(context).getStoredGroupsSync() + val newContact = LocalContactsHelper(context).convertLocalContactToContact(localContact, storedGroups) + callContact.name = newContact!!.getNameToDisplay() + callContact.photoUri = newContact.photoUri + } + + callback(callContact) + } + } else { + callback(callContact) + } + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/LocalContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/LocalContactsHelper.kt index ce5007ea..d4bf5d95 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/LocalContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/LocalContactsHelper.kt @@ -81,7 +81,7 @@ class LocalContactsHelper(val context: Context) { return scaledSizePhotoData } - private fun convertLocalContactToContact(localContact: LocalContact?, storedGroups: ArrayList): Contact? { + fun convertLocalContactToContact(localContact: LocalContact?, storedGroups: ArrayList): Contact? { if (localContact == null) { return null } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/interfaces/ContactsDao.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/interfaces/ContactsDao.kt index cc869362..42621e76 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/interfaces/ContactsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/interfaces/ContactsDao.kt @@ -14,6 +14,9 @@ interface ContactsDao { @Query("SELECT * FROM contacts WHERE id = :id") fun getContactWithId(id: Int): LocalContact? + @Query("SELECT * FROM contacts WHERE phone_numbers LIKE :number") + fun getContactWithNumber(number: String): LocalContact? + @Query("UPDATE contacts SET starred = :isStarred WHERE id = :id") fun updateStarred(isStarred: Int, id: Int)