@cxm-2016
2016-08-15T16:49:28.000000Z
字数 2797
阅读 1409
android
no
fun Cursor.getString(columnName: String): String = this.getString(this.getColumnIndex(columnName))
fun Cursor.getLong(columnName: String): Long = this.getLong(this.getColumnIndex(columnName))
/**
* 陈小默 16/8/13.
*/
class ContactsUtils {
companion object {
private val RAW_CONTACTS_URI = ContactsContract.RawContacts.CONTENT_URI
private val DATA_URI = ContactsContract.Data.CONTENT_URI
private val RAW_CONTACT_ID = ContactsContract.RawContactsEntity.RAW_CONTACT_ID
private val MIME_TYPE_ID = "mimetype_id"
private val TYPE_PHONE = 5
private val TYPE_NAME = 7
private val TYPE_EMALL = 1
private val DISPLAY_NAME = "display_name"
private val ITEM = "vnd.android.cursor.item/"
private val CONDITION = "$RAW_CONTACT_ID=? and $MIME_TYPE_ID=?"
private val MIME_TYPE = ContactsContract.Data.MIMETYPE
private val MIME_TYPES: Array<String> = arrayOf(
"",
"${ITEM}email_v2",
"${ITEM}im",
"${ITEM}nickname",
"${ITEM}organization",
"${ITEM}phone_v2",
"${ITEM}sip_address",
"${ITEM}name",
"${ITEM}postal-address_v2",
"${ITEM}identity",
"${ITEM}photo",
"${ITEM}group_membership"
)
fun insertContacts(context: Context, contactsData: ContactsData): Long {
val resolver = context.contentResolver
val raw = resolver.insert(RAW_CONTACTS_URI, ContentValues())
val id = ContentUris.parseId(raw)
val (i, name, phone, email) = contactsData
val nameData = getDataValues(id, MIME_TYPES[TYPE_NAME], name, name)
resolver.insert(DATA_URI, nameData)
val phoneData = getDataValues(id, MIME_TYPES[TYPE_PHONE], phone, "2")
resolver.insert(DATA_URI, phoneData)
val emailData = getDataValues(id, MIME_TYPES[TYPE_EMALL], email, "2")
resolver.insert(DATA_URI, emailData)
return id
}
fun deleteContacts(context: Context, id: Long): Boolean {
val resolver = context.contentResolver
val count = resolver.delete(RAW_CONTACTS_URI, "_id=$id", null)
return count > 0
}
fun query(context: Context): List<ContactsData> {
val list = ArrayList<ContactsData>()
val resolver = context.contentResolver
val cursor = resolver.query(RAW_CONTACTS_URI, null, null, null, null)
while (cursor.moveToNext()) {
val id = cursor.getLong("_id")
val name = cursor.getString(DISPLAY_NAME)
val phone = getCursor(id, TYPE_PHONE, resolver)?.getString("data1")
val email = getCursor(id, TYPE_EMALL, resolver)?.getString("data1")
list.add(ContactsData(id, name, "$phone", "$email"))//TODO
}
return list
}
//创建时构造
private fun getDataValues(id: Long, mimeType: String, data1: String, data2: String): ContentValues {
val values = ContentValues()
values.put(RAW_CONTACT_ID, id)
values.put(MIME_TYPE, mimeType)
values.put("data1", data1)
values.put("data2", data2)
return values
}
//更新时构造
private fun getDataValues(data1: String, data2: String): ContentValues {
val values = ContentValues()
values.put("data1", data1)
values.put("data2", data2)
return values
}
private fun getCursor(id: Long, mimeType: Int, resolver: ContentResolver): Cursor? {
val c = resolver.query(DATA_URI, null, CONDITION, arrayOf("$id", "$mimeType"), null)
return if (c.moveToNext()) c else null
}
}
}
data class ContactsData(var id: Long = 0, val name: String, val phone: String, val email: String)