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.
This commit is contained in:
Andrii Chubko 2021-08-29 16:07:42 +03:00
parent a80aa5ffc5
commit 21028250a5
2 changed files with 35 additions and 24 deletions

View File

@ -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))
}
}
}

View File

@ -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<Event>()
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<Group> {
val groups = ArrayList<Group>()
if (ezContact.categories != null) {