diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt index 87aefa8e..421a808c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt @@ -386,7 +386,7 @@ class EditContactActivity : ContactActivity() { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) supportActionBar?.title = resources.getString(R.string.new_contact) val contactSource = if (hasContactPermissions()) config.lastUsedContactSource else SMT_PRIVATE - contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), contactSource, 0, 0, "", null, "", ArrayList()) + contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), contactSource, 0, 0, "", null, "", ArrayList(), "") contact_source.text = getPublicContactSource(contact!!.source) contact_source.setOnClickListener { showContactSourcePicker(contact!!.source) { 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 cb0ad9e6..a7d6ad06 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -83,8 +83,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: "" val notes = "" val groups = ArrayList() + val organization = "" val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, addresses, events, accountName, - starred, contactId, thumbnailUri, null, notes, groups) + starred, contactId, thumbnailUri, null, notes, groups, organization) contacts.put(id, contact) } while (cursor.moveToNext()) } @@ -128,6 +129,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val key = notes.keyAt(i) contacts[key]?.notes = notes.valueAt(i) } + + val organizations = getOrganizations() + size = organizations.size() + for (i in 0 until size) { + val key = organizations.keyAt(i) + contacts[key]?.organization = organizations.valueAt(i) + } } private fun getPhoneNumbers(contactId: Int? = null): SparseArray> { @@ -320,6 +328,41 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return notes } + private fun getOrganizations(contactId: Int? = null): SparseArray { + val organizations = SparseArray() + val uri = ContactsContract.Data.CONTENT_URI + val projection = arrayOf( + ContactsContract.Data.RAW_CONTACT_ID, + CommonDataKinds.Organization.COMPANY + ) + + var selection = "${ContactsContract.Data.MIMETYPE} = ?" + var selectionArgs = arrayOf(CommonDataKinds.Organization.CONTENT_ITEM_TYPE) + + if (contactId != null) { + selection += " AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?" + selectionArgs = arrayOf(CommonDataKinds.Organization.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 organization = cursor.getStringValue(CommonDataKinds.Organization.COMPANY) ?: continue + organizations.put(id, organization) + } while (cursor.moveToNext()) + } + } catch (e: Exception) { + activity.showErrorToast(e) + } finally { + cursor?.close() + } + + return organizations + } + private fun getContactGroups(storedGroups: ArrayList, contactId: Int? = null): SparseArray> { val groups = SparseArray>() if (!activity.hasContactPermissions()) { @@ -508,8 +551,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID) val groups = getContactGroups(storedGroups, contactId)[contactId] ?: ArrayList() val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: "" + val organization = getOrganizations(id)[id] ?: "" return Contact(id, firstName, middleName, surname, photoUri, number, emails, addresses, events, accountName, starred, contactId, - thumbnailUri, null, notes, groups) + thumbnailUri, null, notes, groups, organization) } } finally { cursor?.close() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt index 0067b33e..d3ca9181 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -267,7 +267,10 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont val groupIds = Gson().fromJson>(groupIdsJson, groupIdsToken) ?: ArrayList(1) val groups = storedGroups.filter { groupIds.contains(it.id) } as ArrayList - val contact = Contact(id, firstName, middleName, surname, "", phoneNumbers, emails, addresses, events, SMT_PRIVATE, starred, id, "", photo, notes, groups) + val organization = "" + + val contact = Contact(id, firstName, middleName, surname, "", phoneNumbers, emails, addresses, events, SMT_PRIVATE, starred, + id, "", photo, notes, groups, organization) contacts.add(contact) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt index 602e5160..1341ead5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt @@ -24,6 +24,7 @@ class VcfImporter(val activity: SimpleActivity) { private var curSurname = "" private var curPhotoUri = "" private var curNotes = "" + private var curOrganization = "" private var curPhoneNumbers = ArrayList() private var curEmails = ArrayList() private var curEvents = ArrayList() @@ -236,7 +237,7 @@ 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, curGroups) + source, 0, 0, "", null, curNotes, curGroups, curOrganization) if (ContactsHelper(activity).insertContact(contact)) { contactsImported++ } @@ -248,6 +249,7 @@ class VcfImporter(val activity: SimpleActivity) { curSurname = "" curPhotoUri = "" curNotes = "" + curOrganization = "" curPhoneNumbers = ArrayList() curEmails = ArrayList() curEvents = ArrayList() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt index 488f4a14..cbb34453 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -8,7 +8,7 @@ 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, var emails: ArrayList, var addresses: ArrayList
, var events: ArrayList, var source: String, var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?, var notes: String, - var groups: ArrayList) : Comparable { + var groups: ArrayList, var organization: String) : Comparable { companion object { var sorting = 0 }