fix #81, improve contact handling by lookup key

This commit is contained in:
tibbi 2018-03-16 13:11:47 +01:00
parent 54396d4bd3
commit 1165550ef4
3 changed files with 20 additions and 4 deletions

View File

@ -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'

View File

@ -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)

View File

@ -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<String>): 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) ?: ""