Commit 0cb821c7 authored by Ricki Hirner's avatar Ricki Hirner

Support partial dates for BDAY/ANNIVERSARY

parent 1bb0b253
Pipeline #8507262 passed with stage
in 11 minutes and 29 seconds
......@@ -14,8 +14,6 @@ import android.content.ContentProviderClient;
import android.provider.ContactsContract;
import android.support.annotation.RequiresPermission;
import junit.framework.Assert;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.junit.After;
......@@ -30,7 +28,9 @@ import java.util.Arrays;
import ezvcard.VCardVersion;
import ezvcard.property.Address;
import ezvcard.property.Birthday;
import ezvcard.property.Email;
import ezvcard.util.PartialDate;
import lombok.Cleanup;
import static android.support.test.InstrumentationRegistry.getContext;
......@@ -71,20 +71,22 @@ public class AndroidContactTest {
vcard.phoneticGivenName = "Först";
vcard.phoneticMiddleName = "Mittelerde";
vcard.phoneticFamilyName = "Fämilie";
vcard.birthDay = new Birthday(PartialDate.parse("1980-04-16"));
AndroidContact contact = new AndroidContact(addressBook, vcard, null, null);
contact.create();
@Cleanup("delete") AndroidContact contact2 = new AndroidContact(addressBook, contact.id, null, null);
Contact vcard2 = contact2.getContact();
assertEquals(vcard2.displayName, vcard.displayName);
assertEquals(vcard2.prefix, vcard.prefix);
assertEquals(vcard2.givenName, vcard.givenName);
assertEquals(vcard2.familyName, vcard.familyName);
assertEquals(vcard2.suffix, vcard.suffix);
assertEquals(vcard2.phoneticGivenName, vcard.phoneticGivenName);
assertEquals(vcard2.phoneticMiddleName, vcard.phoneticMiddleName);
assertEquals(vcard2.phoneticFamilyName, vcard.phoneticFamilyName);
assertEquals(vcard.displayName, vcard2.displayName);
assertEquals(vcard.prefix, vcard2.prefix);
assertEquals(vcard.givenName, vcard2.givenName);
assertEquals(vcard.familyName, vcard2.familyName);
assertEquals(vcard.suffix, vcard2.suffix);
assertEquals(vcard.phoneticGivenName, vcard2.phoneticGivenName);
assertEquals(vcard.phoneticMiddleName, vcard2.phoneticMiddleName);
assertEquals(vcard.phoneticFamilyName, vcard2.phoneticFamilyName);
assertEquals(vcard.birthDay, vcard2.birthDay);
}
@Test
......@@ -165,6 +167,21 @@ public class AndroidContactTest {
assertTrue(os.toString().contains("ADR;LABEL=My ^'Label^'\\nLine 2:;;Street \"Address\";;;;"));
}
@Test
public void testBirthdayWithoutYear() throws ContactsStorageException, FileNotFoundException {
Contact vcard = new Contact();
vcard.displayName = "Mya Contact";
vcard.birthDay = new Birthday(PartialDate.parse("-04-16"));
AndroidContact contact = new AndroidContact(addressBook, vcard, null, null);
contact.create();
@Cleanup("delete") AndroidContact contact2 = new AndroidContact(addressBook, contact.id, null, null);
Contact vcard2 = contact2.getContact();
assertEquals(vcard.displayName, vcard2.displayName);
assertEquals(vcard.birthDay, vcard2.birthDay);
}
@Test
public void testLabelToXName() {
......
......@@ -46,10 +46,7 @@ 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;
......@@ -69,6 +66,7 @@ import ezvcard.property.Impp;
import ezvcard.property.Related;
import ezvcard.property.Telephone;
import ezvcard.property.Url;
import ezvcard.util.PartialDate;
import lombok.Cleanup;
import lombok.Getter;
import lombok.NonNull;
......@@ -541,9 +539,8 @@ public class AndroidContact {
}
protected void populateEvent(ContentValues row) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
try {
Date date = formatter.parse(row.getAsString(CommonDataKinds.Event.START_DATE));
PartialDate date = PartialDate.parse(row.getAsString(CommonDataKinds.Event.START_DATE));
if (row.containsKey(Event.TYPE))
switch (row.getAsInteger(Event.TYPE)) {
case Event.TYPE_ANNIVERSARY:
......@@ -553,7 +550,7 @@ public class AndroidContact {
contact.birthDay = new Birthday(date);
break;
}
} catch (ParseException e) {
} catch (IllegalArgumentException e) {
Constants.log.log(Level.WARNING, "Couldn't parse birthday/anniversary date from database", e);
}
}
......@@ -1234,12 +1231,6 @@ public class AndroidContact {
}
protected void insertEvent(BatchOperation batch, int type, DateOrTimeProperty dateOrTime) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
if (dateOrTime.getDate() == null) {
Constants.log.warning("Ignoring contact event (birthday/anniversary) without date");
return;
}
final BatchOperation.Operation op;
final ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(dataSyncURI());
if (id == null)
......@@ -1250,7 +1241,7 @@ public class AndroidContact {
}
builder .withValue(Event.MIMETYPE, Event.CONTENT_ITEM_TYPE)
.withValue(Event.TYPE, type)
.withValue(Event.START_DATE, formatter.format(dateOrTime.getDate()));
.withValue(Event.START_DATE, dateOrTime.getPartialDate().toString());
batch.enqueue(op);
Constants.log.log(Level.FINER, "Built Event data row", builder.build());
}
......
......@@ -28,6 +28,7 @@ import ezvcard.parameter.ImppType;
import ezvcard.parameter.RelatedType;
import ezvcard.parameter.TelephoneType;
import ezvcard.property.Address;
import ezvcard.property.Birthday;
import ezvcard.property.Email;
import ezvcard.property.Impp;
import ezvcard.property.Nickname;
......@@ -35,6 +36,7 @@ import ezvcard.property.Organization;
import ezvcard.property.Related;
import ezvcard.property.Telephone;
import ezvcard.property.Url;
import ezvcard.util.PartialDate;
import lombok.Cleanup;
import static org.junit.Assert.assertArrayEquals;
......@@ -360,6 +362,18 @@ public class ContactTest {
assertNotNull(addr.getPref());
}
@Test
public void testVCard4FieldsAsVCard3() throws IOException {
Contact c = regenerate(parseContact("vcard4.vcf", null), VCardVersion.V3_0);
assertNull(c.birthDay);
}
@Test
public void testVCard4FieldsAsVCard4() throws IOException {
Contact c = regenerate(parseContact("vcard4.vcf", null), VCardVersion.V4_0);
assertEquals(new Birthday(PartialDate.parse("--04-16")), c.birthDay);
}
@Test
public void testStrangeREV() throws IOException {
......
BEGIN:VCARD
VERSION:4.0
UID:vcard4test
FN:VCard 4 Test
BDAY:--04-16
END:VCARD
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