From 32e7eadbb35aaad3e0a3cb098df6272e728bdc1b Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 16 Mar 2018 20:40:57 +0100 Subject: [PATCH] add some helper functions for querying device and contact groups --- .../contacts/helpers/ContactsHelper.kt | 67 +++++++++++++++++++ .../contacts/models/Group.kt | 3 + 2 files changed, 70 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/models/Group.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 00e812e3..474888e5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -305,6 +305,73 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return notes } + private fun getContactGroups(contactId: Int? = null): SparseArray> { + val groups = SparseArray>() + val uri = ContactsContract.Data.CONTENT_URI + val projection = arrayOf( + ContactsContract.Data.RAW_CONTACT_ID, + CommonDataKinds.GroupMembership.GROUP_ROW_ID + ) + + var selection = "${ContactsContract.Data.MIMETYPE} = ?" + var selectionArgs = arrayOf(CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE) + + if (contactId != null) { + selection += " AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?" + selectionArgs = arrayOf(CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, contactId.toString()) + } + + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) + if (cursor?.moveToFirst() == true) { + do { + val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID) + val rowId = cursor.getIntValue(CommonDataKinds.GroupMembership.GROUP_ROW_ID) + + if (groups[id] == null) { + groups.put(id, ArrayList()) + } + + groups[id]!!.add(Group(rowId, "")) + } while (cursor.moveToNext()) + } + } catch (e: Exception) { + activity.showErrorToast(e) + } finally { + cursor?.close() + } + + return groups + } + + fun getGroups(): ArrayList { + val groups = ArrayList() + val uri = ContactsContract.Groups.CONTENT_URI + val projection = arrayOf( + ContactsContract.Groups._ID, + ContactsContract.Groups.TITLE + ) + + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, null, null, null) + if (cursor?.moveToFirst() == true) { + do { + val id = cursor.getIntValue(ContactsContract.Groups._ID) + val title = cursor.getStringValue(ContactsContract.Groups.TITLE) + groups.add(Group(id, title)) + } while (cursor.moveToNext()) + } + } catch (e: Exception) { + activity.showErrorToast(e) + } finally { + cursor?.close() + } + + return groups + } + fun getContactWithId(id: Int, isLocalPrivate: Boolean): Contact? { if (id == 0) { return null diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Group.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Group.kt new file mode 100644 index 00000000..736cd5fc --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Group.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.contacts.models + +data class Group(var id: Int, var title: String)