Skip to content

Commit

Permalink
Add support for structured address representations
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanritscher committed Sep 11, 2024
1 parent 0517240 commit 8953d36
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ open class BaseConfig(val context: Context) {
SHOW_CONTACT_FIELDS,
SHOW_FIRST_NAME_FIELD or SHOW_SURNAME_FIELD or SHOW_PHONE_NUMBERS_FIELD or SHOW_EMAILS_FIELD or
SHOW_ADDRESSES_FIELD or SHOW_EVENTS_FIELD or SHOW_NOTES_FIELD or SHOW_GROUPS_FIELD or SHOW_CONTACT_SOURCE_FIELD
or SHOW_STRUCTURED_ADDRESSES_FIELD
)
set(showContactFields) = prefs.edit().putInt(SHOW_CONTACT_FIELDS, showContactFields).apply()
var showDialpadButton: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ const val SHOW_WEBSITES_FIELD = 8192
const val SHOW_NICKNAME_FIELD = 16384
const val SHOW_IMS_FIELD = 32768
const val SHOW_RINGTONE_FIELD = 65536
const val SHOW_STRUCTURED_ADDRESSES_FIELD = 131072

const val DEFAULT_EMAIL_TYPE = ContactsContract.CommonDataKinds.Email.TYPE_HOME
const val DEFAULT_PHONE_NUMBER_TYPE = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ class ContactsHelper(val context: Context) {
val projection = arrayOf(
Data.RAW_CONTACT_ID,
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
CommonDataKinds.StructuredPostal.COUNTRY,
CommonDataKinds.StructuredPostal.REGION,
CommonDataKinds.StructuredPostal.CITY,
CommonDataKinds.StructuredPostal.POSTCODE,
CommonDataKinds.StructuredPostal.POBOX,
CommonDataKinds.StructuredPostal.STREET,
CommonDataKinds.StructuredPostal.NEIGHBORHOOD,
CommonDataKinds.StructuredPostal.TYPE,
CommonDataKinds.StructuredPostal.LABEL
)
Expand All @@ -371,14 +378,22 @@ class ContactsHelper(val context: Context) {
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val address = cursor.getStringValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS) ?: return@queryCursor
val country = cursor.getStringValue(CommonDataKinds.StructuredPostal.COUNTRY) ?: ""
val region = cursor.getStringValue(CommonDataKinds.StructuredPostal.REGION) ?: ""
val city = cursor.getStringValue(CommonDataKinds.StructuredPostal.CITY) ?: ""
val postcode = cursor.getStringValue(CommonDataKinds.StructuredPostal.POSTCODE) ?: ""
val pobox = cursor.getStringValue(CommonDataKinds.StructuredPostal.POBOX) ?: ""
val street = cursor.getStringValue(CommonDataKinds.StructuredPostal.STREET) ?: ""
val neighborhood = cursor.getStringValue(CommonDataKinds.StructuredPostal.NEIGHBORHOOD) ?: ""
val type = cursor.getIntValue(CommonDataKinds.StructuredPostal.TYPE)
val label = cursor.getStringValue(CommonDataKinds.StructuredPostal.LABEL) ?: ""

if (addresses[id] == null) {
addresses.put(id, ArrayList())
}

addresses[id]!!.add(Address(address, type, label))
addresses[id]!!.add(Address(address, type, label, country, region, city, postcode, pobox, street,
neighborhood))
}

return addresses
Expand Down Expand Up @@ -1006,6 +1021,13 @@ class ContactsHelper(val context: Context) {
withValue(Data.RAW_CONTACT_ID, contact.id)
withValue(Data.MIMETYPE, CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
withValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, it.value)
withValue(CommonDataKinds.StructuredPostal.COUNTRY, it.country)
withValue(CommonDataKinds.StructuredPostal.REGION, it.region)
withValue(CommonDataKinds.StructuredPostal.CITY, it.city)
withValue(CommonDataKinds.StructuredPostal.POSTCODE, it.postcode)
withValue(CommonDataKinds.StructuredPostal.POBOX, it.pobox)
withValue(CommonDataKinds.StructuredPostal.STREET, it.street)
withValue(CommonDataKinds.StructuredPostal.NEIGHBORHOOD, it.neighborhood)
withValue(CommonDataKinds.StructuredPostal.TYPE, it.type)
withValue(CommonDataKinds.StructuredPostal.LABEL, it.label)
operations.add(build())
Expand Down Expand Up @@ -1300,6 +1322,13 @@ class ContactsHelper(val context: Context) {
withValueBackReference(Data.RAW_CONTACT_ID, 0)
withValue(Data.MIMETYPE, CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
withValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, it.value)
withValue(CommonDataKinds.StructuredPostal.COUNTRY, it.country)
withValue(CommonDataKinds.StructuredPostal.REGION, it.region)
withValue(CommonDataKinds.StructuredPostal.CITY, it.city)
withValue(CommonDataKinds.StructuredPostal.POSTCODE, it.postcode)
withValue(CommonDataKinds.StructuredPostal.POBOX, it.pobox)
withValue(CommonDataKinds.StructuredPostal.STREET, it.street)
withValue(CommonDataKinds.StructuredPostal.NEIGHBORHOOD, it.neighborhood)
withValue(CommonDataKinds.StructuredPostal.TYPE, it.type)
withValue(CommonDataKinds.StructuredPostal.LABEL, it.label)
operations.add(build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,18 @@ class VcfExporter {

contact.addresses.forEach {
val address = Address()
address.streetAddress = it.value
if (!it.country.isNullOrEmpty() || !it.region.isNullOrEmpty() || !it.city.isNullOrEmpty() || !it.postcode.isNullOrEmpty() ||
!it.pobox.isNullOrEmpty() || !it.street.isNullOrEmpty() || !it.neighborhood.isNullOrEmpty()) {
address.country = it.country
address.region = it.region
address.locality = it.city
address.postalCode = it.postcode
address.poBox = it.pobox
address.streetAddress = it.street
address.extendedAddress = it.neighborhood
} else {
address.streetAddress = it.value
}
address.parameters.addType(getAddressTypeLabel(it.type, it.label))
card.addAddress(address)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.fossify.commons.models.contacts

import kotlinx.serialization.Serializable

@Serializable
data class Address(var value: String, var type: Int, var label: String)
data class Address(var value: String, var type: Int, var label: String, var country: String, var region: String, var city: String,
var postcode: String, var pobox: String, var street: String, var neighborhood: String)

0 comments on commit 8953d36

Please sign in to comment.