From 1165550ef4c92c4b0548387baabb0fa1b017d732 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 16 Mar 2018 13:11:47 +0100 Subject: [PATCH] fix #81, improve contact handling by lookup key --- app/build.gradle | 2 +- .../contacts/activities/ViewContactActivity.kt | 7 ++++++- .../contacts/helpers/ContactsHelper.kt | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d942bf59..c6cb783b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,7 +45,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.16.10' + implementation 'com.simplemobiletools:commons:3.16.11' implementation 'joda-time:joda-time:2.9.9' implementation 'com.facebook.stetho:stetho:1.5.0' implementation 'com.google.code.gson:gson:2.8.2' diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ViewContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ViewContactActivity.kt index f27ef8d8..dbd35dd2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ViewContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ViewContactActivity.kt @@ -65,6 +65,11 @@ class ViewContactActivity : ContactActivity() { val data = intent.data if (data != null) { val rawId = if (data.path.contains("lookup")) { + val lookupKey = getLookupKeyFromUri(data) + if (lookupKey != null) { + contact = ContactsHelper(this).getContactWithLookupKey(lookupKey) + } + getLookupUriRawId(data) } else { getContactUriRawId(data) @@ -76,7 +81,7 @@ class ViewContactActivity : ContactActivity() { } } - if (contactId != 0) { + if (contactId != 0 && contact == null) { contact = ContactsHelper(this).getContactWithId(contactId, intent.getBooleanExtra(IS_PRIVATE, false)) if (contact == null) { toast(R.string.unknown_error_occurred) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index c1423506..00e812e3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -312,14 +312,25 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return activity.dbHelper.getContactWithId(id) } - val uri = ContactsContract.Data.CONTENT_URI - val projection = getContactProjection() val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?" val selectionArgs = arrayOf(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, id.toString()) + return parseContactCursor(selection, selectionArgs) + } + + fun getContactWithLookupKey(key: String): Contact? { + val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.Data.LOOKUP_KEY} = ?" + val selectionArgs = arrayOf(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, key) + return parseContactCursor(selection, selectionArgs) + } + + private fun parseContactCursor(selection: String, selectionArgs: Array): Contact? { + val uri = ContactsContract.Data.CONTENT_URI + val projection = getContactProjection() 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) val firstName = cursor.getStringValue(CommonDataKinds.StructuredName.GIVEN_NAME) ?: "" val middleName = cursor.getStringValue(CommonDataKinds.StructuredName.MIDDLE_NAME) ?: "" val surname = cursor.getStringValue(CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""