From 21028250a56dbe3f35652f0a57a78ff2facbbf8a Mon Sep 17 00:00:00 2001 From: Andrii Chubko Date: Sun, 29 Aug 2021 16:07:42 +0300 Subject: [PATCH] Export partial dates to vCard 3.0 with year 1900 At the same time, adjust import to be both 3.0 and 4.0 compatible. --- .../contacts/pro/helpers/VcfExporter.kt | 34 ++++++++----------- .../contacts/pro/helpers/VcfImporter.kt | 25 +++++++++++--- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/VcfExporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/VcfExporter.kt index dcb4907b..205755e2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/VcfExporter.kt @@ -71,27 +71,23 @@ class VcfExporter { card.addEmail(email) } - contact.events.forEach { - if (it.type == Event.TYPE_ANNIVERSARY || it.type == Event.TYPE_BIRTHDAY) { - val dateTime = it.value.getDateTimeFromDateString(false) - if (it.value.startsWith("--")) { - val partialDate = PartialDate.builder().year(null).month(dateTime.monthOfYear).date(dateTime.dayOfMonth).build() - if (it.type == Event.TYPE_BIRTHDAY) { - card.birthdays.add(Birthday(partialDate)) + contact.events.forEach { event -> + if (event.type == Event.TYPE_ANNIVERSARY || event.type == Event.TYPE_BIRTHDAY) { + val dateTime = event.value.getDateTimeFromDateString(false) + Calendar.getInstance().apply { + clear() + if (event.value.startsWith("--")) { + set(Calendar.YEAR, 1900) } else { - card.anniversaries.add(Anniversary(partialDate)) - } - } else { - Calendar.getInstance().apply { - clear() set(Calendar.YEAR, dateTime.year) - set(Calendar.MONTH, dateTime.monthOfYear - 1) - set(Calendar.DAY_OF_MONTH, dateTime.dayOfMonth) - if (it.type == Event.TYPE_BIRTHDAY) { - card.birthdays.add(Birthday(time)) - } else { - card.anniversaries.add(Anniversary(time)) - } + + } + set(Calendar.MONTH, dateTime.monthOfYear - 1) + set(Calendar.DAY_OF_MONTH, dateTime.dayOfMonth) + if (event.type == Event.TYPE_BIRTHDAY) { + card.birthdays.add(Birthday(time)) + } else { + card.anniversaries.add(Anniversary(time)) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/VcfImporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/VcfImporter.kt index f7b7bab6..0ce4c94b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/VcfImporter.kt @@ -18,6 +18,7 @@ import com.simplemobiletools.contacts.pro.models.Event import com.simplemobiletools.contacts.pro.models.Organization import ezvcard.Ezvcard import ezvcard.VCard +import ezvcard.util.PartialDate import java.io.File import java.io.FileOutputStream import java.net.URLDecoder @@ -94,13 +95,21 @@ class VcfImporter(val activity: SimpleActivity) { } val events = ArrayList() - ezContact.anniversaries.forEach { - val event = Event(formatDateToDayCode(it.date), CommonDataKinds.Event.TYPE_ANNIVERSARY) + ezContact.anniversaries.forEach { anniversary -> + val event = if (anniversary.date != null) { + Event(formatDateToDayCode(anniversary.date), CommonDataKinds.Event.TYPE_ANNIVERSARY) + } else { + Event(formatPartialDateToDayCode(anniversary.partialDate), CommonDataKinds.Event.TYPE_ANNIVERSARY) + } events.add(event) } - ezContact.birthdays.forEach { - val event = Event(formatDateToDayCode(it.date), CommonDataKinds.Event.TYPE_BIRTHDAY) + ezContact.birthdays.forEach { birthday -> + val event = if (birthday.date != null) { + Event(formatDateToDayCode(birthday.date), CommonDataKinds.Event.TYPE_BIRTHDAY) + } else { + Event(formatPartialDateToDayCode(birthday.partialDate), CommonDataKinds.Event.TYPE_BIRTHDAY) + } events.add(event) } @@ -168,12 +177,18 @@ class VcfImporter(val activity: SimpleActivity) { } private fun formatDateToDayCode(date: Date): String { - val year = 1900 + date.year + val year = if (date.year == 0) "-" else "${1900 + date.year}" val month = String.format("%02d", date.month + 1) val day = String.format("%02d", date.date) return "$year-$month-$day" } + private fun formatPartialDateToDayCode(partialDate: PartialDate): String { + val month = String.format("%02d", partialDate.month) + val day = String.format("%02d", partialDate.date) + return "--$month-$day" + } + private fun getContactGroups(ezContact: VCard): ArrayList { val groups = ArrayList() if (ezContact.categories != null) {