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,11 +113,7 @@ class ContactsHelper(val context: Context) {
RawContacts.ACCOUNT_TYPE
)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection) { cursor ->
val name = cursor.getStringValue(RawContacts.ACCOUNT_NAME) ?: ""
val type = cursor.getStringValue(RawContacts.ACCOUNT_TYPE) ?: ""
var publicName = name
@ -127,11 +123,6 @@ class ContactsHelper(val context: Context) {
val source = ContactSource(name, type, publicName)
sources.add(source)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
} finally {
cursor?.close()
}
}
@ -148,15 +139,11 @@ class ContactsHelper(val context: Context) {
val selectionArgs = arrayOf(StructuredName.CONTENT_ITEM_TYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
val sortOrder = getSortString()
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, sortOrder, true) { cursor ->
val accountName = cursor.getStringValue(RawContacts.ACCOUNT_NAME) ?: ""
val accountType = cursor.getStringValue(RawContacts.ACCOUNT_TYPE) ?: ""
if (ignoredSources.contains("$accountName:$accountType")) {
continue
return@queryCursor
}
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
@ -193,12 +180,6 @@ class ContactsHelper(val context: Context) {
events, accountName, starred, contactId, thumbnailUri, null, notes, groups, organization, websites, ims)
contacts.put(id, contact)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
val phoneNumbers = getPhoneNumbers(null)
@ -282,13 +263,9 @@ class ContactsHelper(val context: Context) {
val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?"
val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString())
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val number = cursor.getStringValue(Phone.NUMBER) ?: continue
val number = cursor.getStringValue(Phone.NUMBER) ?: return@queryCursor
val normalizedNumber = cursor.getStringValue(Phone.NORMALIZED_NUMBER) ?: number.normalizeNumber()
val type = cursor.getIntValue(Phone.TYPE)
val label = cursor.getStringValue(Phone.LABEL) ?: ""
@ -299,12 +276,6 @@ class ContactsHelper(val context: Context) {
val phoneNumber = PhoneNumber(number, type, label, normalizedNumber)
phoneNumbers[id].add(phoneNumber)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return phoneNumbers
@ -321,20 +292,10 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(Nickname.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val nickname = cursor.getStringValue(Nickname.NAME) ?: continue
val nickname = cursor.getStringValue(Nickname.NAME) ?: return@queryCursor
nicknames.put(id, nickname)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return nicknames
@ -353,13 +314,9 @@ class ContactsHelper(val context: Context) {
val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?"
val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString())
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val email = cursor.getStringValue(CommonDataKinds.Email.DATA) ?: continue
val email = cursor.getStringValue(CommonDataKinds.Email.DATA) ?: return@queryCursor
val type = cursor.getIntValue(CommonDataKinds.Email.TYPE)
val label = cursor.getStringValue(CommonDataKinds.Email.LABEL) ?: ""
@ -368,12 +325,6 @@ class ContactsHelper(val context: Context) {
}
emails[id]!!.add(Email(email, type, label))
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return emails
@ -392,13 +343,9 @@ class ContactsHelper(val context: Context) {
val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?"
val selectionArgs = if (contactId == null) getSourcesSelectionArgs() else arrayOf(contactId.toString())
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val address = cursor.getStringValue(StructuredPostal.FORMATTED_ADDRESS) ?: continue
val address = cursor.getStringValue(StructuredPostal.FORMATTED_ADDRESS) ?: return@queryCursor
val type = cursor.getIntValue(StructuredPostal.TYPE)
val label = cursor.getStringValue(StructuredPostal.LABEL) ?: ""
@ -407,12 +354,6 @@ class ContactsHelper(val context: Context) {
}
addresses[id]!!.add(Address(address, type, label))
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return addresses
@ -431,13 +372,9 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(Im.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val IM = cursor.getStringValue(Im.DATA) ?: continue
val IM = cursor.getStringValue(Im.DATA) ?: return@queryCursor
val type = cursor.getIntValue(Im.PROTOCOL)
val label = cursor.getStringValue(Im.CUSTOM_PROTOCOL) ?: ""
@ -446,12 +383,6 @@ class ContactsHelper(val context: Context) {
}
IMs[id]!!.add(IM(IM, type, label))
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return IMs
@ -469,13 +400,9 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.Event.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) ?: continue
val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) ?: return@queryCursor
val type = cursor.getIntValue(CommonDataKinds.Event.TYPE)
if (events[id] == null) {
@ -483,12 +410,6 @@ class ContactsHelper(val context: Context) {
}
events[id]!!.add(Event(startDate, type))
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return events
@ -505,20 +426,10 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(Note.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val note = cursor.getStringValue(Note.NOTE) ?: continue
val note = cursor.getStringValue(Note.NOTE) ?: return@queryCursor
notes.put(id, note)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return notes
@ -536,26 +447,16 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.Organization.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val company = cursor.getStringValue(CommonDataKinds.Organization.COMPANY) ?: ""
val title = cursor.getStringValue(CommonDataKinds.Organization.TITLE) ?: ""
if (company.isEmpty() && title.isEmpty()) {
continue
return@queryCursor
}
val organization = Organization(company, title)
organizations.put(id, organization)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return organizations
@ -572,25 +473,15 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null)
val selectionArgs = getSourcesSelectionArgs(Website.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val url = cursor.getStringValue(Website.URL) ?: continue
val url = cursor.getStringValue(Website.URL) ?: return@queryCursor
if (websites[id] == null) {
websites.put(id, ArrayList())
}
websites[id]!!.add(url)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return websites
@ -611,26 +502,16 @@ class ContactsHelper(val context: Context) {
val selection = getSourcesSelection(true, contactId != null, false)
val selectionArgs = getSourcesSelectionArgs(GroupMembership.CONTENT_ITEM_TYPE, contactId)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
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)
if (groups[id] == null) {
groups.put(id, ArrayList())
}
groups[id]!!.add(group)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
context.showErrorToast(e)
} finally {
cursor?.close()
}
return groups
@ -709,26 +590,16 @@ class ContactsHelper(val context: Context) {
val selection = "${Groups.AUTO_ADD} = ? AND ${Groups.FAVORITES} = ?"
val selectionArgs = arrayOf("0", "0")
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getLongValue(Groups._ID)
val title = cursor.getStringValue(Groups.TITLE) ?: continue
val title = cursor.getStringValue(Groups.TITLE) ?: return@queryCursor
val systemId = cursor.getStringValue(Groups.SYSTEM_ID)
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)
} finally {
cursor?.close()
}
return groups
}