From 4f8cfa56ffb47c20bac4bec14b311cf6ae6e276c Mon Sep 17 00:00:00 2001 From: Stephan Ritscher Date: Tue, 23 Jan 2024 21:30:52 +0100 Subject: [PATCH] Add support for structured address representations --- .../org/fossify/commons/helpers/BaseConfig.kt | 1 + .../org/fossify/commons/helpers/Constants.kt | 1 + .../fossify/commons/helpers/ContactsHelper.kt | 31 ++++++++++++++++++- .../fossify/commons/helpers/VcfExporter.kt | 14 ++++++++- .../commons/models/contacts/Address.kt | 5 +-- 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/commons/src/main/kotlin/org/fossify/commons/helpers/BaseConfig.kt b/commons/src/main/kotlin/org/fossify/commons/helpers/BaseConfig.kt index 3722ea46d..bd105b040 100644 --- a/commons/src/main/kotlin/org/fossify/commons/helpers/BaseConfig.kt +++ b/commons/src/main/kotlin/org/fossify/commons/helpers/BaseConfig.kt @@ -535,6 +535,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 diff --git a/commons/src/main/kotlin/org/fossify/commons/helpers/Constants.kt b/commons/src/main/kotlin/org/fossify/commons/helpers/Constants.kt index e0e3ddedd..818e4cb0a 100644 --- a/commons/src/main/kotlin/org/fossify/commons/helpers/Constants.kt +++ b/commons/src/main/kotlin/org/fossify/commons/helpers/Constants.kt @@ -602,6 +602,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 diff --git a/commons/src/main/kotlin/org/fossify/commons/helpers/ContactsHelper.kt b/commons/src/main/kotlin/org/fossify/commons/helpers/ContactsHelper.kt index 8bb62ee99..e1daa8567 100644 --- a/commons/src/main/kotlin/org/fossify/commons/helpers/ContactsHelper.kt +++ b/commons/src/main/kotlin/org/fossify/commons/helpers/ContactsHelper.kt @@ -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 ) @@ -371,6 +378,13 @@ 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) ?: "" @@ -378,7 +392,8 @@ class ContactsHelper(val context: Context) { 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 @@ -1009,6 +1024,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()) @@ -1303,6 +1325,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()) diff --git a/commons/src/main/kotlin/org/fossify/commons/helpers/VcfExporter.kt b/commons/src/main/kotlin/org/fossify/commons/helpers/VcfExporter.kt index 068471646..fcd59cd1a 100644 --- a/commons/src/main/kotlin/org/fossify/commons/helpers/VcfExporter.kt +++ b/commons/src/main/kotlin/org/fossify/commons/helpers/VcfExporter.kt @@ -102,7 +102,19 @@ class VcfExporter { contact.addresses.forEach { val address = Address() - address.streetAddress = it.value + if (listOf(it.country, it.region, it.city, it.postcode, it.pobox, it.street, it.neighborhood) + .map{it.isNullOrEmpty()} + .reduce{a, b -> a || b}) { + 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) } diff --git a/commons/src/main/kotlin/org/fossify/commons/models/contacts/Address.kt b/commons/src/main/kotlin/org/fossify/commons/models/contacts/Address.kt index 1a4d7425a..73bf1d0c3 100644 --- a/commons/src/main/kotlin/org/fossify/commons/models/contacts/Address.kt +++ b/commons/src/main/kotlin/org/fossify/commons/models/contacts/Address.kt @@ -1,6 +1,7 @@ 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)