class PhoneContactModel { | |
var id: Int = 0 | |
var name: String = "" | |
var number: String = "" | |
var isSelected: Boolean = false | |
var viewType: Int = 1 | |
var headerName = "" | |
} | |
private fun loadContacts() { | |
val contentUri = ContactsContract.Contacts.CONTENT_URI | |
val id = ContactsContract.Contacts._ID | |
val displayName = ContactsContract.Contacts.DISPLAY_NAME | |
val hasPhoneNumber = ContactsContract.Contacts.HAS_PHONE_NUMBER | |
val phoneContentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI | |
val phoneContactId = ContactsContract.CommonDataKinds.Phone.CONTACT_ID | |
val number = ContactsContract.CommonDataKinds.Phone.NUMBER | |
val cursor = contentResolver.query(contentUri, null, null, null, null) | |
if (cursor.count > 0 && cursor.moveToFirst()) { | |
while (!cursor.isAfterLast) { | |
val contactId = cursor.getString(cursor.getColumnIndex(id)) | |
val name = cursor.getString(cursor.getColumnIndex(displayName)) | |
val hasNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(hasPhoneNumber))) | |
if (hasNumber > 0) { | |
val model = PhoneContactModel() | |
model.name = name | |
//This is to read multiple phone numbers associated with the same contact | |
val phoneCursor = contentResolver.query(phoneContentUri, null, phoneContactId + " = ?", arrayOf(contactId), null) | |
while (phoneCursor.moveToNext()) { | |
val phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(number)) | |
model.number = phoneNumber | |
} | |
model.isSelected = false | |
items.add(model) | |
phoneCursor.close() | |
} | |
cursor.moveToNext() | |
} | |
cursor.close() | |
inviteContactAdapter.notifyDataSetChanged() | |
if (items.size > 0) { | |
startActivity(Intent(this, InvitationsActivity::class.java)) | |
finish() | |
} else { | |
Utils.infoDialog(this, "", getString(R.string.contacts_not_found), false) | |
} | |
} else { | |
Utils.infoDialog(this, "", getString(R.string.contacts_not_found), false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment