Commit d6ad012f authored by Ricki Hirner's avatar Ricki Hirner

Birthdays without years, again

parent 6c8b4aa8
Pipeline #8775880 passed with stage
in 9 minutes and 48 seconds
......@@ -24,6 +24,7 @@ import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.Date;
import java.util.Arrays;
import ezvcard.VCardVersion;
......@@ -71,7 +72,7 @@ public class AndroidContactTest {
vcard.phoneticGivenName = "Först";
vcard.phoneticMiddleName = "Mittelerde";
vcard.phoneticFamilyName = "Fämilie";
vcard.birthDay = new Birthday(PartialDate.parse("1980-04-16"));
vcard.birthDay = new Birthday(Date.valueOf("1980-04-16"));
AndroidContact contact = new AndroidContact(addressBook, vcard, null, null);
contact.create();
......
......@@ -46,8 +46,10 @@ import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
......@@ -540,20 +542,29 @@ public class AndroidContact {
}
protected void populateEvent(ContentValues row) {
String dateStr = row.getAsString(CommonDataKinds.Event.START_DATE);
Date full = null;
PartialDate partial = null;
SimpleDateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
try {
PartialDate date = PartialDate.parse(row.getAsString(CommonDataKinds.Event.START_DATE));
if (row.containsKey(Event.TYPE))
full = fullFormat.parse(dateStr);
} catch(ParseException ignored) {
try {
partial = PartialDate.parse(dateStr);
} catch (IllegalArgumentException e) {
Constants.log.log(Level.WARNING, "Couldn't parse birthday/anniversary date from database", e);
}
}
if ((full != null || partial != null) && row.containsKey(Event.TYPE))
switch (row.getAsInteger(Event.TYPE)) {
case Event.TYPE_ANNIVERSARY:
contact.anniversary = new Anniversary(date);
contact.anniversary = full != null ? new Anniversary(full) : new Anniversary(partial);
break;
case Event.TYPE_BIRTHDAY:
contact.birthDay = new Birthday(date);
contact.birthDay = full != null ? new Birthday(full) : new Birthday(partial);
break;
}
} catch (IllegalArgumentException e) {
Constants.log.log(Level.WARNING, "Couldn't parse birthday/anniversary date from database", e);
}
}
protected void populateRelation(ContentValues row) {
......@@ -1262,7 +1273,7 @@ public class AndroidContact {
if (TextUtils.isEmpty(related.getText()))
return;
int typeCode = Event.TYPE_CUSTOM;
int typeCode = Relation.TYPE_CUSTOM;
List<String> labels = new LinkedList<>();
for (RelatedType type : related.getTypes()) {
......
......@@ -515,12 +515,18 @@ public class Contact {
else if (anniversary.getPartialDate() != null) {
// VCard 3: partial date with month and day, but without year
PartialDate partial = anniversary.getPartialDate();
if (partial.getDate() != null && partial.getMonth() != null && partial.getYear() == null) {
Calendar fakeCal = GregorianCalendar.getInstance();
fakeCal.set(DATE_PARAMETER_OMIT_YEAR_DEFAULT, partial.getMonth()-1, partial.getDate());
Anniversary fakeAnniversary = new Anniversary(fakeCal.getTime(), false);
fakeAnniversary.addParameter(DATE_PARAMETER_OMIT_YEAR, String.valueOf(DATE_PARAMETER_OMIT_YEAR_DEFAULT));
vCard.setAnniversary(fakeAnniversary);
if (partial.getDate() != null && partial.getMonth() != null) {
if (partial.getYear() != null)
// partial date is a complete date
vCard.setAnniversary(anniversary);
else {
// VCard 3: partial date with month and day, but without year
Calendar fakeCal = GregorianCalendar.getInstance();
fakeCal.set(DATE_PARAMETER_OMIT_YEAR_DEFAULT, partial.getMonth()-1, partial.getDate());
Anniversary fakeAnniversary = new Anniversary(fakeCal.getTime(), false);
fakeAnniversary.addParameter(DATE_PARAMETER_OMIT_YEAR, String.valueOf(DATE_PARAMETER_OMIT_YEAR_DEFAULT));
vCard.setAnniversary(fakeAnniversary);
}
}
}
}
......@@ -529,14 +535,19 @@ public class Contact {
if (vCardVersion == VCardVersion.V4_0 || birthDay.getDate() != null)
vCard.setBirthday(birthDay);
else if (birthDay.getPartialDate() != null) {
// VCard 3: partial date with month and day, but without year
PartialDate partial = birthDay.getPartialDate();
if (partial.getDate() != null && partial.getMonth() != null && partial.getYear() == null) {
Calendar fakeCal = GregorianCalendar.getInstance();
fakeCal.set(DATE_PARAMETER_OMIT_YEAR_DEFAULT, partial.getMonth()-1, partial.getDate());
Birthday fakeBirthday = new Birthday(fakeCal.getTime(), false);
fakeBirthday.addParameter(DATE_PARAMETER_OMIT_YEAR, String.valueOf(DATE_PARAMETER_OMIT_YEAR_DEFAULT));
vCard.setBirthday(fakeBirthday);
if (partial.getDate() != null && partial.getMonth() != null) {
if (partial.getYear() != null)
// partial date is a complete date
vCard.setBirthday(birthDay);
else {
// VCard 3: partial date with month and day, but without year
Calendar fakeCal = GregorianCalendar.getInstance();
fakeCal.set(DATE_PARAMETER_OMIT_YEAR_DEFAULT, partial.getMonth()-1, partial.getDate());
Birthday fakeBirthday = new Birthday(fakeCal.getTime(), false);
fakeBirthday.addParameter(DATE_PARAMETER_OMIT_YEAR, String.valueOf(DATE_PARAMETER_OMIT_YEAR_DEFAULT));
vCard.setBirthday(fakeBirthday);
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment