parse contact addresses
This commit is contained in:
parent
5a0095c22a
commit
d3498f96cb
7 changed files with 77 additions and 15 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Contact>) -> Unit) {
|
||||
|
@ -52,11 +53,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||
val number = ArrayList<PhoneNumber>() // proper value is obtained below
|
||||
val emails = ArrayList<Email>()
|
||||
val events = ArrayList<Event>()
|
||||
val addresses = ArrayList<Address>()
|
||||
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<ArrayList<Address>> {
|
||||
val addresses = SparseArray<ArrayList<Address>>()
|
||||
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()
|
||||
|
|
|
@ -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<List<Event>>() {}.type
|
||||
val events = Gson().fromJson<ArrayList<Event>>(eventsJson, eventsToken) ?: ArrayList(1)
|
||||
|
||||
val addresses = ArrayList<Address>()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<PhoneNumber>()
|
||||
private var curEmails = ArrayList<Email>()
|
||||
private var curEvents = ArrayList<Event>()
|
||||
private var curAddresses = ArrayList<Address>()
|
||||
|
||||
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()
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package com.simplemobiletools.contacts.models
|
||||
|
||||
data class Address(var value: String, var type: Int)
|
|
@ -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<PhoneNumber>, var emails: ArrayList<Email>, var events: ArrayList<Event>, var source: String,
|
||||
var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?) : Comparable<Contact> {
|
||||
var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var events: ArrayList<Event>, var addresses: ArrayList<Address>,
|
||||
var source: String, var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?) : Comparable<Contact> {
|
||||
companion object {
|
||||
var sorting = 0
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue