use the helper function queryCursor at looping cursors

This commit is contained in:
tibbi 2020-04-15 14:20:51 +02:00
parent f8501d7cf2
commit ce5bf02bf7
1 changed files with 135 additions and 264 deletions

View File

@ -113,25 +113,16 @@ class ContactsHelper(val context: Context) {
RawContacts.ACCOUNT_TYPE RawContacts.ACCOUNT_TYPE
) )
var cursor: Cursor? = null context.queryCursor(uri, projection) { cursor ->
try { val name = cursor.getStringValue(RawContacts.ACCOUNT_NAME) ?: ""
cursor = context.contentResolver.query(uri, projection, null, null, null) val type = cursor.getStringValue(RawContacts.ACCOUNT_TYPE) ?: ""
if (cursor?.moveToFirst() == true) { var publicName = name
do { if (type == TELEGRAM_PACKAGE) {
val name = cursor.getStringValue(RawContacts.ACCOUNT_NAME) ?: "" publicName += " (${context.getString(R.string.telegram)})"
val type = cursor.getStringValue(RawContacts.ACCOUNT_TYPE) ?: ""
var publicName = name
if (type == TELEGRAM_PACKAGE) {
publicName += " (${context.getString(R.string.telegram)})"
}
val source = ContactSource(name, type, publicName)
sources.add(source)
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
} finally { val source = ContactSource(name, type, publicName)
cursor?.close() sources.add(source)
} }
} }
@ -148,57 +139,47 @@ class ContactsHelper(val context: Context) {
val selectionArgs = arrayOf(StructuredName.CONTENT_ITEM_TYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE) val selectionArgs = arrayOf(StructuredName.CONTENT_ITEM_TYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
val sortOrder = getSortString() val sortOrder = getSortString()
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, sortOrder, true) { cursor ->
try { val accountName = cursor.getStringValue(RawContacts.ACCOUNT_NAME) ?: ""
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder) val accountType = cursor.getStringValue(RawContacts.ACCOUNT_TYPE) ?: ""
if (cursor?.moveToFirst() == true) { if (ignoredSources.contains("$accountName:$accountType")) {
do { return@queryCursor
val accountName = cursor.getStringValue(RawContacts.ACCOUNT_NAME) ?: ""
val accountType = cursor.getStringValue(RawContacts.ACCOUNT_TYPE) ?: ""
if (ignoredSources.contains("$accountName:$accountType")) {
continue
}
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
var prefix = ""
var firstName = ""
var middleName = ""
var surname = ""
var suffix = ""
// ignore names at Organization type contacts
if (cursor.getStringValue(Data.MIMETYPE) == StructuredName.CONTENT_ITEM_TYPE) {
prefix = cursor.getStringValue(StructuredName.PREFIX) ?: ""
firstName = cursor.getStringValue(StructuredName.GIVEN_NAME) ?: ""
middleName = cursor.getStringValue(StructuredName.MIDDLE_NAME) ?: ""
surname = cursor.getStringValue(StructuredName.FAMILY_NAME) ?: ""
suffix = cursor.getStringValue(StructuredName.SUFFIX) ?: ""
}
val nickname = ""
val photoUri = cursor.getStringValue(StructuredName.PHOTO_URI) ?: ""
val numbers = ArrayList<PhoneNumber>() // proper value is obtained below
val emails = ArrayList<Email>()
val addresses = ArrayList<Address>()
val events = ArrayList<Event>()
val starred = cursor.getIntValue(StructuredName.STARRED)
val contactId = cursor.getIntValue(Data.CONTACT_ID)
val thumbnailUri = cursor.getStringValue(StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
val notes = ""
val groups = ArrayList<Group>()
val organization = Organization("", "")
val websites = ArrayList<String>()
val ims = ArrayList<IM>()
val contact = Contact(id, prefix, firstName, middleName, surname, suffix, nickname, photoUri, numbers, emails, addresses,
events, accountName, starred, contactId, thumbnailUri, null, notes, groups, organization, websites, ims)
contacts.put(id, contact)
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
} finally { var prefix = ""
cursor?.close() var firstName = ""
var middleName = ""
var surname = ""
var suffix = ""
// ignore names at Organization type contacts
if (cursor.getStringValue(Data.MIMETYPE) == StructuredName.CONTENT_ITEM_TYPE) {
prefix = cursor.getStringValue(StructuredName.PREFIX) ?: ""
firstName = cursor.getStringValue(StructuredName.GIVEN_NAME) ?: ""
middleName = cursor.getStringValue(StructuredName.MIDDLE_NAME) ?: ""
surname = cursor.getStringValue(StructuredName.FAMILY_NAME) ?: ""
suffix = cursor.getStringValue(StructuredName.SUFFIX) ?: ""
}
val nickname = ""
val photoUri = cursor.getStringValue(StructuredName.PHOTO_URI) ?: ""
val numbers = ArrayList<PhoneNumber>() // proper value is obtained below
val emails = ArrayList<Email>()
val addresses = ArrayList<Address>()
val events = ArrayList<Event>()
val starred = cursor.getIntValue(StructuredName.STARRED)
val contactId = cursor.getIntValue(Data.CONTACT_ID)
val thumbnailUri = cursor.getStringValue(StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
val notes = ""
val groups = ArrayList<Group>()
val organization = Organization("", "")
val websites = ArrayList<String>()
val ims = ArrayList<IM>()
val contact = Contact(id, prefix, firstName, middleName, surname, suffix, nickname, photoUri, numbers, emails, addresses,
events, accountName, starred, contactId, thumbnailUri, null, notes, groups, organization, websites, ims)
contacts.put(id, contact)
} }
val phoneNumbers = getPhoneNumbers(null) val phoneNumbers = getPhoneNumbers(null)
@ -282,29 +263,19 @@ class ContactsHelper(val context: Context) {
val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?" val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?"
val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString()) val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString())
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val number = cursor.getStringValue(Phone.NUMBER) ?: return@queryCursor
if (cursor?.moveToFirst() == true) { val normalizedNumber = cursor.getStringValue(Phone.NORMALIZED_NUMBER) ?: number.normalizeNumber()
do { val type = cursor.getIntValue(Phone.TYPE)
val id = cursor.getIntValue(Data.RAW_CONTACT_ID) val label = cursor.getStringValue(Phone.LABEL) ?: ""
val number = cursor.getStringValue(Phone.NUMBER) ?: continue
val normalizedNumber = cursor.getStringValue(Phone.NORMALIZED_NUMBER) ?: number.normalizeNumber()
val type = cursor.getIntValue(Phone.TYPE)
val label = cursor.getStringValue(Phone.LABEL) ?: ""
if (phoneNumbers[id] == null) { if (phoneNumbers[id] == null) {
phoneNumbers.put(id, ArrayList()) phoneNumbers.put(id, ArrayList())
}
val phoneNumber = PhoneNumber(number, type, label, normalizedNumber)
phoneNumbers[id].add(phoneNumber)
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) val phoneNumber = PhoneNumber(number, type, label, normalizedNumber)
} finally { phoneNumbers[id].add(phoneNumber)
cursor?.close()
} }
return phoneNumbers return phoneNumbers
@ -321,20 +292,10 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null) val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(Nickname.CONTENT_ITEM_TYPE, contactId) val selectionArgs = getSourcesSelectionArgs(Nickname.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val nickname = cursor.getStringValue(Nickname.NAME) ?: return@queryCursor
if (cursor?.moveToFirst() == true) { nicknames.put(id, nickname)
do {
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val nickname = cursor.getStringValue(Nickname.NAME) ?: continue
nicknames.put(id, nickname)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
} }
return nicknames return nicknames
@ -353,27 +314,17 @@ class ContactsHelper(val context: Context) {
val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?" val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?"
val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString()) val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString())
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val email = cursor.getStringValue(CommonDataKinds.Email.DATA) ?: return@queryCursor
if (cursor?.moveToFirst() == true) { val type = cursor.getIntValue(CommonDataKinds.Email.TYPE)
do { val label = cursor.getStringValue(CommonDataKinds.Email.LABEL) ?: ""
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val email = cursor.getStringValue(CommonDataKinds.Email.DATA) ?: continue
val type = cursor.getIntValue(CommonDataKinds.Email.TYPE)
val label = cursor.getStringValue(CommonDataKinds.Email.LABEL) ?: ""
if (emails[id] == null) { if (emails[id] == null) {
emails.put(id, ArrayList()) emails.put(id, ArrayList())
}
emails[id]!!.add(Email(email, type, label))
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) emails[id]!!.add(Email(email, type, label))
} finally {
cursor?.close()
} }
return emails return emails
@ -392,27 +343,17 @@ class ContactsHelper(val context: Context) {
val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?" val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?"
val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString()) val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString())
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val address = cursor.getStringValue(StructuredPostal.FORMATTED_ADDRESS) ?: return@queryCursor
if (cursor?.moveToFirst() == true) { val type = cursor.getIntValue(StructuredPostal.TYPE)
do { val label = cursor.getStringValue(StructuredPostal.LABEL) ?: ""
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val address = cursor.getStringValue(StructuredPostal.FORMATTED_ADDRESS) ?: continue
val type = cursor.getIntValue(StructuredPostal.TYPE)
val label = cursor.getStringValue(StructuredPostal.LABEL) ?: ""
if (addresses[id] == null) { if (addresses[id] == null) {
addresses.put(id, ArrayList()) addresses.put(id, ArrayList())
}
addresses[id]!!.add(Address(address, type, label))
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) addresses[id]!!.add(Address(address, type, label))
} finally {
cursor?.close()
} }
return addresses return addresses
@ -431,27 +372,17 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null) val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(Im.CONTENT_ITEM_TYPE, contactId) val selectionArgs = getSourcesSelectionArgs(Im.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val IM = cursor.getStringValue(Im.DATA) ?: return@queryCursor
if (cursor?.moveToFirst() == true) { val type = cursor.getIntValue(Im.PROTOCOL)
do { val label = cursor.getStringValue(Im.CUSTOM_PROTOCOL) ?: ""
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val IM = cursor.getStringValue(Im.DATA) ?: continue
val type = cursor.getIntValue(Im.PROTOCOL)
val label = cursor.getStringValue(Im.CUSTOM_PROTOCOL) ?: ""
if (IMs[id] == null) { if (IMs[id] == null) {
IMs.put(id, ArrayList()) IMs.put(id, ArrayList())
}
IMs[id]!!.add(IM(IM, type, label))
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) IMs[id]!!.add(IM(IM, type, label))
} finally {
cursor?.close()
} }
return IMs return IMs
@ -469,26 +400,16 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null) val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.Event.CONTENT_ITEM_TYPE, contactId) val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.Event.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) ?: return@queryCursor
if (cursor?.moveToFirst() == true) { val type = cursor.getIntValue(CommonDataKinds.Event.TYPE)
do {
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) ?: continue
val type = cursor.getIntValue(CommonDataKinds.Event.TYPE)
if (events[id] == null) { if (events[id] == null) {
events.put(id, ArrayList()) events.put(id, ArrayList())
}
events[id]!!.add(Event(startDate, type))
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) events[id]!!.add(Event(startDate, type))
} finally {
cursor?.close()
} }
return events return events
@ -505,20 +426,10 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null) val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(Note.CONTENT_ITEM_TYPE, contactId) val selectionArgs = getSourcesSelectionArgs(Note.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val note = cursor.getStringValue(Note.NOTE) ?: return@queryCursor
if (cursor?.moveToFirst() == true) { notes.put(id, note)
do {
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val note = cursor.getStringValue(Note.NOTE) ?: continue
notes.put(id, note)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
} }
return notes return notes
@ -536,26 +447,16 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null) val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.Organization.CONTENT_ITEM_TYPE, contactId) val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.Organization.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val company = cursor.getStringValue(CommonDataKinds.Organization.COMPANY) ?: ""
if (cursor?.moveToFirst() == true) { val title = cursor.getStringValue(CommonDataKinds.Organization.TITLE) ?: ""
do { if (company.isEmpty() && title.isEmpty()) {
val id = cursor.getIntValue(Data.RAW_CONTACT_ID) return@queryCursor
val company = cursor.getStringValue(CommonDataKinds.Organization.COMPANY) ?: ""
val title = cursor.getStringValue(CommonDataKinds.Organization.TITLE) ?: ""
if (company.isEmpty() && title.isEmpty()) {
continue
}
val organization = Organization(company, title)
organizations.put(id, organization)
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) val organization = Organization(company, title)
} finally { organizations.put(id, organization)
cursor?.close()
} }
return organizations return organizations
@ -572,25 +473,15 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null) val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(Website.CONTENT_ITEM_TYPE, contactId) val selectionArgs = getSourcesSelectionArgs(Website.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val url = cursor.getStringValue(Website.URL) ?: return@queryCursor
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val url = cursor.getStringValue(Website.URL) ?: continue
if (websites[id] == null) { if (websites[id] == null) {
websites.put(id, ArrayList()) websites.put(id, ArrayList())
}
websites[id]!!.add(url)
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) websites[id]!!.add(url)
} finally {
cursor?.close()
} }
return websites return websites
@ -611,26 +502,16 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null, false) val selection = getSourcesSelection(true, contactId != null, false)
val selectionArgs = getSourcesSelectionArgs(GroupMembership.CONTENT_ITEM_TYPE, contactId) val selectionArgs = getSourcesSelectionArgs(GroupMembership.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getIntValue(Data.CONTACT_ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val newRowId = cursor.getLongValue(Data.DATA1)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(Data.CONTACT_ID)
val newRowId = cursor.getLongValue(Data.DATA1)
val groupTitle = storedGroups.firstOrNull { it.id == newRowId }?.title ?: continue val groupTitle = storedGroups.firstOrNull { it.id == newRowId }?.title ?: return@queryCursor
val group = Group(newRowId, groupTitle) val group = Group(newRowId, groupTitle)
if (groups[id] == null) { if (groups[id] == null) {
groups.put(id, ArrayList()) groups.put(id, ArrayList())
}
groups[id]!!.add(group)
} while (cursor.moveToNext())
} }
} catch (e: Exception) { groups[id]!!.add(group)
context.showErrorToast(e)
} finally {
cursor?.close()
} }
return groups return groups
@ -709,26 +590,16 @@ class ContactsHelper(val context: Context) {
val selection = "${Groups.AUTO_ADD} = ? AND ${Groups.FAVORITES} = ?" val selection = "${Groups.AUTO_ADD} = ? AND ${Groups.FAVORITES} = ?"
val selectionArgs = arrayOf("0", "0") val selectionArgs = arrayOf("0", "0")
var cursor: Cursor? = null context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
try { val id = cursor.getLongValue(Groups._ID)
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) val title = cursor.getStringValue(Groups.TITLE) ?: return@queryCursor
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getLongValue(Groups._ID)
val title = cursor.getStringValue(Groups.TITLE) ?: continue
val systemId = cursor.getStringValue(Groups.SYSTEM_ID) val systemId = cursor.getStringValue(Groups.SYSTEM_ID)
if (groups.map { it.title }.contains(title) && systemId != null) { if (groups.map { it.title }.contains(title) && systemId != null) {
continue return@queryCursor
}
groups.add(Group(id, title))
} while (cursor.moveToNext())
} }
} catch (e: Exception) {
context.showErrorToast(e) groups.add(Group(id, title))
} finally {
cursor?.close()
} }
return groups return groups
} }