Revert "avoid unnecessarily fetching all contact emails and addresses for the main screen"

This reverts commit 8edab98cb3.
This commit is contained in:
tibbi 2018-02-22 16:14:33 +01:00
parent 2fa449d60e
commit f81700c120
1 changed files with 53 additions and 16 deletions

View File

@ -75,6 +75,22 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
contacts[key]?.phoneNumbers = phoneNumbers.valueAt(i) contacts[key]?.phoneNumbers = phoneNumbers.valueAt(i)
} }
val emails = getEmails()
size = emails.size()
for (i in 0 until size) {
val key = emails.keyAt(i)
contacts[key]?.emails = emails.valueAt(i)
}
val addresses = getAddresses()
size = addresses.size()
for (i in 0 until size) {
val key = addresses.keyAt(i)
contacts[key]?.addresses = addresses.valueAt(i)
}
//getNotes()
activity.dbHelper.getContacts().forEach { activity.dbHelper.getContacts().forEach {
contacts.put(it.id, it) contacts.put(it.id, it)
} }
@ -124,27 +140,35 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return phoneNumbers return phoneNumbers
} }
private fun getEmails(contactId: Int): ArrayList<Email> { private fun getEmails(contactId: Int? = null): SparseArray<ArrayList<Email>> {
val emails = ArrayList<Email>() val emails = SparseArray<ArrayList<Email>>()
val uri = CommonDataKinds.Email.CONTENT_URI val uri = CommonDataKinds.Email.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID,
CommonDataKinds.Email.DATA, CommonDataKinds.Email.DATA,
CommonDataKinds.Email.TYPE CommonDataKinds.Email.TYPE
) )
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" val selection = if (contactId == null) null else "${ContactsContract.Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(contactId.toString()) val selectionArgs = if (contactId == null) null else arrayOf(contactId.toString())
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) { if (cursor?.moveToFirst() == true) {
do { do {
val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID)
val email = cursor.getStringValue(CommonDataKinds.Email.DATA) ?: continue val email = cursor.getStringValue(CommonDataKinds.Email.DATA) ?: continue
val type = cursor.getIntValue(CommonDataKinds.Email.TYPE) val type = cursor.getIntValue(CommonDataKinds.Email.TYPE)
emails.add(Email(email, type))
if (emails[id] == null) {
emails.put(id, ArrayList())
}
emails[id]!!.add(Email(email, type))
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} catch (e: Exception) { } catch (e: Exception) {
activity.showErrorToast(e) activity.showErrorToast(e)
} finally { } finally {
@ -154,27 +178,35 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return emails return emails
} }
private fun getAddresses(contactId: Int): ArrayList<Address> { private fun getAddresses(contactId: Int? = null): SparseArray<ArrayList<Address>> {
val addresses = ArrayList<Address>() val addresses = SparseArray<ArrayList<Address>>()
val uri = CommonDataKinds.StructuredPostal.CONTENT_URI val uri = CommonDataKinds.StructuredPostal.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID,
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
CommonDataKinds.StructuredPostal.TYPE CommonDataKinds.StructuredPostal.TYPE
) )
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" val selection = if (contactId == null) null else "${ContactsContract.Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(contactId.toString()) val selectionArgs = if (contactId == null) null else arrayOf(contactId.toString())
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) { if (cursor?.moveToFirst() == true) {
do { do {
val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID)
val address = cursor.getStringValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS) val address = cursor.getStringValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)
val type = cursor.getIntValue(CommonDataKinds.StructuredPostal.TYPE) val type = cursor.getIntValue(CommonDataKinds.StructuredPostal.TYPE)
addresses.add(Address(address, type))
if (addresses[id] == null) {
addresses.put(id, ArrayList())
}
addresses[id]!!.add(Address(address, type))
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} catch (e: Exception) { } catch (e: Exception) {
activity.showErrorToast(e) activity.showErrorToast(e)
} finally { } finally {
@ -184,8 +216,8 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return addresses return addresses
} }
private fun getEvents(contactId: Int): ArrayList<Event> { private fun getEvents(contactId: Int): SparseArray<ArrayList<Event>> {
val events = ArrayList<Event>() val events = SparseArray<ArrayList<Event>>()
val uri = ContactsContract.Data.CONTENT_URI val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
CommonDataKinds.Event.START_DATE, CommonDataKinds.Event.START_DATE,
@ -200,7 +232,12 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
do { do {
val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE)
val type = cursor.getIntValue(CommonDataKinds.Event.TYPE) val type = cursor.getIntValue(CommonDataKinds.Event.TYPE)
events.add(Event(startDate, type))
if (events[contactId] == null) {
events.put(contactId, ArrayList())
}
events[contactId]!!.add(Event(startDate, type))
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -253,9 +290,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val surname = cursor.getStringValue(CommonDataKinds.StructuredName.FAMILY_NAME) ?: "" val surname = cursor.getStringValue(CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
val photoUri = cursor.getStringValue(CommonDataKinds.Phone.PHOTO_URI) ?: "" val photoUri = cursor.getStringValue(CommonDataKinds.Phone.PHOTO_URI) ?: ""
val number = getPhoneNumbers(id)[id] ?: ArrayList() val number = getPhoneNumbers(id)[id] ?: ArrayList()
val emails = getEmails(id) val emails = getEmails(id)[id] ?: ArrayList()
val addresses = getAddresses(id) val addresses = getAddresses(id)[id] ?: ArrayList()
val events = getEvents(id) val events = getEvents(id)[id] ?: ArrayList()
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: "" val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: ""
val starred = cursor.getIntValue(CommonDataKinds.StructuredName.STARRED) val starred = cursor.getIntValue(CommonDataKinds.StructuredName.STARRED)
val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID) val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)