diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt index a17d4f70..998a7f80 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt @@ -159,4 +159,10 @@ abstract class ContactActivity : SimpleActivity() { ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY -> R.string.anniversary else -> R.string.other } + + fun getAddressTextId(type: Int) = when (type) { + ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME -> R.string.home + ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK -> R.string.work + else -> R.string.other + } } 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 bb10ac81..06d90f0e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/EditContactActivity.kt @@ -292,7 +292,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(), config.lastUsedContactSource, 0, 0, "", null) + contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), config.lastUsedContactSource, 0, 0, "", null) contact_source.text = getPublicContactSource(contact!!.source) contact_source.setOnClickListener { showContactSourcePicker(contact!!.source) { @@ -651,4 +651,10 @@ class EditContactActivity : ContactActivity() { getString(R.string.anniversary) -> CommonDataKinds.Event.TYPE_ANNIVERSARY else -> CommonDataKinds.Event.TYPE_OTHER } + + private fun getAddressTypeId(value: String) = when (value) { + getString(R.string.home) -> CommonDataKinds.StructuredPostal.TYPE_HOME + getString(R.string.work) -> CommonDataKinds.StructuredPostal.TYPE_WORK + else -> CommonDataKinds.StructuredPostal.TYPE_OTHER + } } 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 4ba069b1..1468f82b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -28,6 +28,7 @@ import com.simplemobiletools.contacts.extensions.getPhotoThumbnailSize import com.simplemobiletools.contacts.models.* import java.io.ByteArrayOutputStream import java.util.* +import kotlin.collections.ArrayList class ContactsHelper(val activity: BaseSimpleActivity) { fun getContacts(callback: (ArrayList) -> Unit) { @@ -52,11 +53,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val number = ArrayList() // proper value is obtained below val emails = ArrayList() val events = ArrayList() + val addresses = ArrayList
() val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: "" val starred = cursor.getIntValue(CommonDataKinds.StructuredName.STARRED) val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID) val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: "" - val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, events, accountName, starred, contactId, thumbnailUri, null) + val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, events, addresses, accountName, + starred, contactId, thumbnailUri, null) contacts.put(id, contact) } while (cursor.moveToNext()) } @@ -80,6 +83,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) { contacts[key]?.emails = emails.valueAt(i) } + val addresses = getAddresses() + size = addresses.size() + for (i in 0 until size) { + val key = addresses.keyAt(i) + contacts[key]?.addresses = addresses.valueAt(i) + } + activity.dbHelper.getContacts().forEach { contacts.put(it.id, it) } @@ -200,6 +210,44 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return events } + private fun getAddresses(contactId: Int? = null): SparseArray> { + val addresses = SparseArray>() + val uri = CommonDataKinds.StructuredPostal.CONTENT_URI + val projection = arrayOf( + ContactsContract.Data.RAW_CONTACT_ID, + CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, + CommonDataKinds.StructuredPostal.TYPE + ) + + val selection = if (contactId == null) null else "${ContactsContract.Data.RAW_CONTACT_ID} = ?" + val selectionArgs = if (contactId == null) null else arrayOf(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 address = cursor.getStringValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS) + val type = cursor.getIntValue(CommonDataKinds.StructuredPostal.TYPE) + + if (addresses[id] == null) { + addresses.put(id, ArrayList()) + } + + addresses[id]!!.add(Address(address, type)) + } while (cursor.moveToNext()) + } + + } catch (e: Exception) { + activity.showErrorToast(e) + } finally { + cursor?.close() + } + + return addresses + } + fun getContactWithId(id: Int, isLocalPrivate: Boolean): Contact? { if (id == 0) { return null @@ -222,11 +270,12 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val number = getPhoneNumbers(id)[id] ?: ArrayList() val emails = getEmails(id)[id] ?: ArrayList() val events = getEvents(id)[id] ?: ArrayList() + val addresses = getAddresses(id)[id] ?: ArrayList() val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: "" val starred = cursor.getIntValue(CommonDataKinds.StructuredName.STARRED) val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID) val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: "" - return Contact(id, firstName, middleName, surname, photoUri, number, emails, events, accountName, starred, contactId, thumbnailUri, null) + return Contact(id, firstName, middleName, surname, photoUri, number, emails, events, addresses, accountName, starred, contactId, thumbnailUri, null) } } 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 f598e576..615dfad9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -16,10 +16,7 @@ import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.contacts.extensions.getByteArray import com.simplemobiletools.contacts.extensions.getPhotoThumbnailSize -import com.simplemobiletools.contacts.models.Contact -import com.simplemobiletools.contacts.models.Email -import com.simplemobiletools.contacts.models.Event -import com.simplemobiletools.contacts.models.PhoneNumber +import com.simplemobiletools.contacts.models.* class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { private val CONTACTS_TABLE_NAME = "contacts" @@ -144,6 +141,8 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont val eventsToken = object : TypeToken>() {}.type val events = Gson().fromJson>(eventsJson, eventsToken) ?: ArrayList(1) + val addresses = ArrayList
() + val photoByteArray = cursor.getBlobValue(COL_PHOTO) ?: null val photo = if (photoByteArray?.isNotEmpty() == true) { BitmapFactory.decodeByteArray(photoByteArray, 0, photoByteArray.size) @@ -152,7 +151,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } val starred = cursor.getIntValue(COL_STARRED) - val contact = Contact(id, firstName, middleName, surname, "", phoneNumbers, emails, events, SMT_PRIVATE, starred, id, "", photo) + val contact = Contact(id, firstName, middleName, surname, "", phoneNumbers, emails, events, addresses, SMT_PRIVATE, starred, id, "", photo) 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 c69a5402..7244d247 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt @@ -10,10 +10,7 @@ import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.extensions.getCachePhoto import com.simplemobiletools.contacts.extensions.getCachePhotoUri import com.simplemobiletools.contacts.helpers.VcfImporter.ImportResult.* -import com.simplemobiletools.contacts.models.Contact -import com.simplemobiletools.contacts.models.Email -import com.simplemobiletools.contacts.models.Event -import com.simplemobiletools.contacts.models.PhoneNumber +import com.simplemobiletools.contacts.models.* import java.io.File import java.io.FileOutputStream @@ -29,6 +26,7 @@ class VcfImporter(val activity: SimpleActivity) { private var curPhoneNumbers = ArrayList() private var curEmails = ArrayList() private var curEvents = ArrayList() + private var curAddresses = ArrayList
() private var isGettingPhoto = false private var currentPhotoString = StringBuilder() @@ -199,7 +197,7 @@ class VcfImporter(val activity: SimpleActivity) { } private fun saveContact(source: String) { - val contact = Contact(0, curFirstName, curMiddleName, curSurname, curPhotoUri, curPhoneNumbers, curEmails, curEvents, source, 0, 0, "", null) + val contact = Contact(0, curFirstName, curMiddleName, curSurname, curPhotoUri, curPhoneNumbers, curEmails, curEvents, curAddresses, source, 0, 0, "", null) if (ContactsHelper(activity).insertContact(contact)) { contactsImported++ } @@ -213,6 +211,7 @@ class VcfImporter(val activity: SimpleActivity) { curPhoneNumbers = ArrayList() curEmails = ArrayList() curEvents = ArrayList() + curAddresses = ArrayList() isGettingPhoto = false currentPhotoString = StringBuilder() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Address.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Address.kt new file mode 100644 index 00000000..7571a228 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Address.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.contacts.models + +data class Address(var value: String, var type: Int) 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 f98b9770..4bf94011 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -6,8 +6,8 @@ import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME 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 events: ArrayList, var source: String, - var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?) : Comparable { + var phoneNumbers: ArrayList, var emails: ArrayList, var events: ArrayList, var addresses: ArrayList
, + var source: String, var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?) : Comparable { companion object { var sorting = 0 }