add a Groups field to contacts

This commit is contained in:
tibbi 2018-03-16 22:56:13 +01:00
parent 32e7eadbb3
commit 20a333071e
5 changed files with 32 additions and 14 deletions

View File

@ -321,7 +321,7 @@ class EditContactActivity : ContactActivity() {
private fun setupNewContact() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
supportActionBar?.title = resources.getString(R.string.new_contact)
contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), config.lastUsedContactSource, 0, 0, "", null, "")
contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), config.lastUsedContactSource, 0, 0, "", null, "", ArrayList())
contact_source.text = getPublicContactSource(contact!!.source)
contact_source.setOnClickListener {
showContactSourcePicker(contact!!.source) {

View File

@ -57,8 +57,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
val notes = ""
val groups = ArrayList<Group>()
val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, addresses, events, accountName,
starred, contactId, thumbnailUri, null, notes)
starred, contactId, thumbnailUri, null, notes, groups)
contacts.put(id, contact)
} while (cursor.moveToNext())
}
@ -103,6 +104,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
contacts[key]?.notes = notes.valueAt(i)
}
val groups = getContactGroups(getStoredGroups())
size = groups.size()
for (i in 0 until size) {
val key = groups.keyAt(i)
contacts[key]?.groups = groups.valueAt(i)
}
activity.dbHelper.getContacts().forEach {
contacts.put(it.id, it)
}
@ -305,19 +313,19 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return notes
}
private fun getContactGroups(contactId: Int? = null): SparseArray<ArrayList<Group>> {
private fun getContactGroups(storedGroups: ArrayList<Group>, contactId: Int? = null): SparseArray<ArrayList<Group>> {
val groups = SparseArray<ArrayList<Group>>()
val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID,
CommonDataKinds.GroupMembership.GROUP_ROW_ID
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DATA1
)
var selection = "${ContactsContract.Data.MIMETYPE} = ?"
var selectionArgs = arrayOf(CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
if (contactId != null) {
selection += " AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?"
selection += " AND ${ContactsContract.Data.CONTACT_ID} = ?"
selectionArgs = arrayOf(CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, contactId.toString())
}
@ -326,14 +334,16 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
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)
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val newRowId = cursor.getIntValue(ContactsContract.Data.DATA1)
if (groups[id] == null) {
groups.put(id, ArrayList())
}
groups[id]!!.add(Group(rowId, ""))
val groupTitle = storedGroups.firstOrNull { it.id == newRowId }?.title ?: ""
val group = Group(newRowId, groupTitle)
groups[id]!!.add(group)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
@ -345,7 +355,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return groups
}
fun getGroups(): ArrayList<Group> {
fun getStoredGroups(): ArrayList<Group> {
val groups = ArrayList<Group>()
val uri = ContactsContract.Groups.CONTENT_URI
val projection = arrayOf(
@ -391,6 +401,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
private fun parseContactCursor(selection: String, selectionArgs: Array<String>): Contact? {
val storedGroups = getStoredGroups()
val uri = ContactsContract.Data.CONTENT_URI
val projection = getContactProjection()
var cursor: Cursor? = null
@ -410,8 +421,10 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: ""
val starred = cursor.getIntValue(CommonDataKinds.StructuredName.STARRED)
val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val groups = getContactGroups(storedGroups, contactId)[contactId] ?: ArrayList()
val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
return Contact(id, firstName, middleName, surname, photoUri, number, emails, addresses, events, accountName, starred, contactId, thumbnailUri, null, notes)
return Contact(id, firstName, middleName, surname, photoUri, number, emails, addresses, events, accountName, starred, contactId,
thumbnailUri, null, notes, groups)
}
} finally {
cursor?.close()

View File

@ -163,8 +163,9 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val notes = cursor.getStringValue(COL_NOTES)
val starred = cursor.getIntValue(COL_STARRED)
val groups = ArrayList<Group>()
val contact = Contact(id, firstName, middleName, surname, "", phoneNumbers, emails, addresses, events, SMT_PRIVATE, starred, id, "", photo, notes)
val contact = Contact(id, firstName, middleName, surname, "", phoneNumbers, emails, addresses, events, SMT_PRIVATE, starred, id, "", photo, notes, groups)
contacts.add(contact)
}
}

View File

@ -28,6 +28,7 @@ class VcfImporter(val activity: SimpleActivity) {
private var curEmails = ArrayList<Email>()
private var curEvents = ArrayList<Event>()
private var curAddresses = ArrayList<Address>()
private var curGroups = ArrayList<Group>()
private var isGettingPhoto = false
private var currentPhotoString = StringBuilder()
@ -234,7 +235,8 @@ class VcfImporter(val activity: SimpleActivity) {
}
private fun saveContact(source: String) {
val contact = Contact(0, curFirstName, curMiddleName, curSurname, curPhotoUri, curPhoneNumbers, curEmails, curAddresses, curEvents, source, 0, 0, "", null, curNotes)
val contact = Contact(0, curFirstName, curMiddleName, curSurname, curPhotoUri, curPhoneNumbers, curEmails, curAddresses, curEvents,
source, 0, 0, "", null, curNotes, curGroups)
if (ContactsHelper(activity).insertContact(contact)) {
contactsImported++
}
@ -250,6 +252,7 @@ class VcfImporter(val activity: SimpleActivity) {
curEmails = ArrayList()
curEvents = ArrayList()
curAddresses = ArrayList()
curGroups = ArrayList()
isGettingPhoto = false
currentPhotoString = StringBuilder()

View File

@ -7,7 +7,8 @@ import com.simplemobiletools.commons.helpers.SORT_DESCENDING
data class Contact(val id: Int, var firstName: String, var middleName: String, var surname: String, var photoUri: String,
var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var addresses: ArrayList<Address>, var events: ArrayList<Event>,
var source: String, var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?, var notes: String) : Comparable<Contact> {
var source: String, var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?, var notes: String,
var groups: ArrayList<Group>) : Comparable<Contact> {
companion object {
var sorting = 0
}