mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
split the name between first/middle/sur name
This commit is contained in:
@ -41,23 +41,32 @@ class ContactsHelper(val activity: SimpleActivity) {
|
||||
Thread {
|
||||
val sources = activity.config.displayContactSources
|
||||
val questionMarks = ("?," * sources.size).trimEnd(',')
|
||||
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||
val uri = ContactsContract.Data.CONTENT_URI
|
||||
val projection = getContactProjection()
|
||||
val selection = if (activity.config.showAllContacts()) null else "${ContactsContract.RawContacts.ACCOUNT_NAME} IN ($questionMarks)"
|
||||
val selectionArgs = if (activity.config.showAllContacts()) null else sources.toTypedArray()
|
||||
var selection = "${ContactsContract.Data.MIMETYPE} = ?"
|
||||
var selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
||||
if (!activity.config.showAllContacts()) {
|
||||
selection += " AND ${ContactsContract.RawContacts.ACCOUNT_NAME} IN ($questionMarks)"
|
||||
selectionArgs += sources.toTypedArray()
|
||||
}
|
||||
val sortOrder = getSortString()
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
do {
|
||||
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)
|
||||
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: continue
|
||||
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: ""
|
||||
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
|
||||
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID)
|
||||
val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
|
||||
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
|
||||
val familyName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
|
||||
if (firstName.isEmpty() && middleName.isEmpty() && familyName.isEmpty())
|
||||
continue
|
||||
|
||||
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI) ?: ""
|
||||
val number = "" // proper value is obtained below
|
||||
val email = "" // proper value is obtained below
|
||||
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
|
||||
val contact = Contact(id, name, number, photoUri, email, accountName)
|
||||
val contact = Contact(id, firstName, middleName, familyName, photoUri, number, email, accountName)
|
||||
contacts.put(id, contact)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
@ -73,6 +82,12 @@ class ContactsHelper(val activity: SimpleActivity) {
|
||||
}
|
||||
}
|
||||
|
||||
getNumbers().forEach {
|
||||
if (contacts.containsKey(it.first)) {
|
||||
contacts[it.first]!!.number = it.second
|
||||
}
|
||||
}
|
||||
|
||||
callback(ArrayList(contacts.values))
|
||||
}.start()
|
||||
}
|
||||
@ -94,7 +109,10 @@ class ContactsHelper(val activity: SimpleActivity) {
|
||||
private fun getEmails(): ArrayList<Pair<Int, String>> {
|
||||
val pairs = ArrayList<Pair<Int, String>>()
|
||||
val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI
|
||||
val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Email.DATA)
|
||||
val projection = arrayOf(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
|
||||
ContactsContract.CommonDataKinds.Email.DATA
|
||||
)
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = activity.contentResolver.query(uri, projection, null, null, null)
|
||||
@ -111,6 +129,29 @@ class ContactsHelper(val activity: SimpleActivity) {
|
||||
return pairs
|
||||
}
|
||||
|
||||
private fun getNumbers(): ArrayList<Pair<Int, String>> {
|
||||
val pairs = ArrayList<Pair<Int, String>>()
|
||||
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
|
||||
ContactsContract.CommonDataKinds.Phone.NUMBER
|
||||
)
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = activity.contentResolver.query(uri, projection, null, null, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
do {
|
||||
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)
|
||||
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
||||
pairs.add(Pair(id, number))
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
return pairs
|
||||
}
|
||||
|
||||
fun getContactEmail(id: Int): String {
|
||||
val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI
|
||||
val projection = arrayOf(ContactsContract.CommonDataKinds.Email.DATA)
|
||||
@ -129,24 +170,45 @@ class ContactsHelper(val activity: SimpleActivity) {
|
||||
return ""
|
||||
}
|
||||
|
||||
fun getContactWithId(id: Int): Contact? {
|
||||
if (id == 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
fun getContactNumber(id: Int): String {
|
||||
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||
val projection = getContactProjection()
|
||||
val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
||||
val selection = "${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = ?"
|
||||
val selectionArgs = arrayOf(id.toString())
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: return null
|
||||
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: ""
|
||||
return cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
fun getContactWithId(id: Int): Contact? {
|
||||
if (id == 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
val uri = ContactsContract.Data.CONTENT_URI
|
||||
val projection = getContactProjection()
|
||||
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID} = ?"
|
||||
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, id.toString())
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
|
||||
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
|
||||
val familyName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
|
||||
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
|
||||
val number = getContactNumber(id)
|
||||
val email = getContactEmail(id)
|
||||
return Contact(id, name, number, photoUri, email, "")
|
||||
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
|
||||
return Contact(id, firstName, middleName, familyName, photoUri, number, email, accountName)
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
@ -156,10 +218,11 @@ class ContactsHelper(val activity: SimpleActivity) {
|
||||
}
|
||||
|
||||
private fun getContactProjection() = arrayOf(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
|
||||
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
|
||||
ContactsContract.CommonDataKinds.Phone.NUMBER,
|
||||
ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
|
||||
ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID,
|
||||
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
|
||||
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
|
||||
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
|
||||
ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI,
|
||||
ContactsContract.RawContacts.ACCOUNT_NAME
|
||||
)
|
||||
|
||||
|
Reference in New Issue
Block a user