From cb629354c476dd777d57954b277cb5082d5f26bf Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 20 Nov 2018 11:33:57 +0100 Subject: [PATCH] add a helper function for getting the called contact at the Dialer --- app/src/main/AndroidManifest.xml | 10 +++++++- .../contacts/pro/activities/DialerActivity.kt | 10 ++++++++ .../contacts/pro/helpers/ContactsHelper.kt | 25 +++++++++++++++++++ .../pro/helpers/LocalContactsHelper.kt | 9 +++++++ .../contacts/pro/models/LocalContact.kt | 2 +- 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6a0934d2..bbf6914a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -205,7 +205,15 @@ + android:parentActivityName=".activities.MainActivity"> + + + + + + + + Unit) { + Thread { + val uri = CommonDataKinds.Phone.CONTENT_URI + val projection = arrayOf(ContactsContract.Data.RAW_CONTACT_ID) + val selection = "${CommonDataKinds.Phone.NUMBER} = ?" + val selectionArgs = arrayOf(number) + + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) + if (cursor?.moveToFirst() == true) { + val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID) + callback(getContactWithId(id, false)) + return@Thread + } + } catch (e: Exception) { + activity.showErrorToast(e) + } finally { + cursor?.close() + } + + callback(LocalContactsHelper(activity).getContactWithNumber(number)) + }.start() + } + private fun getContentResolverAccounts(): HashSet { val uri = ContactsContract.Data.CONTENT_URI val projection = arrayOf( 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 d8e52fc3..a15396c2 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 @@ -13,6 +13,15 @@ class LocalContactsHelper(val activity: Activity) { fun getContactWithId(id: Int) = convertLocalContactToContact(activity.contactsDB.getContactWithId(id)) + fun getContactWithNumber(number: String): Contact? { + activity.contactsDB.getContacts().forEach { + if (it.phoneNumbers.map { it.value }.contains(number)) { + return convertLocalContactToContact(it) + } + } + return null + } + fun insertOrUpdateContact(contact: Contact): Boolean { val localContact = convertContactToLocalContact(contact) return activity.contactsDB.insertOrUpdate(localContact) > 0 diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/LocalContact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/LocalContact.kt index 9ff65b9d..a2947246 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/LocalContact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/LocalContact.kt @@ -27,7 +27,7 @@ data class LocalContact( @ColumnInfo(name = "websites") var websites: ArrayList, @ColumnInfo(name = "ims") var IMs: ArrayList) { - override fun equals(other: Any?) = this.id == (other as? LocalContact?)?.id + override fun equals(other: Any?) = id == (other as? LocalContact?)?.id override fun hashCode() = id ?: 0 }