mirror of
https://github.com/etesync/android
synced 2024-11-22 16:08:13 +00:00
Ignore invalid IM addresses (fixes #105), Lombok 1.12.2
This commit is contained in:
parent
eaa080cfe8
commit
33b8aa8e05
Binary file not shown.
Binary file not shown.
@ -425,6 +425,8 @@ public class LocalAddressBook extends LocalCollection<Contact> {
|
||||
}
|
||||
|
||||
protected String labelToXName(String label) {
|
||||
if (label == null)
|
||||
return null;
|
||||
String xName = "X-" + label.replaceAll(" ","_").replaceAll("[^\\p{L}\\p{Nd}\\-_]", "").toUpperCase(Locale.US);
|
||||
return xName;
|
||||
}
|
||||
@ -434,6 +436,8 @@ public class LocalAddressBook extends LocalCollection<Contact> {
|
||||
}
|
||||
|
||||
protected String xNameToLabel(String xname) {
|
||||
if (xname == null)
|
||||
return null;
|
||||
// "x-my_property"
|
||||
// 1. ensure lower case -> "x-my_property"
|
||||
// 2. remove x- from beginning -> "my_property"
|
||||
@ -466,41 +470,39 @@ public class LocalAddressBook extends LocalCollection<Contact> {
|
||||
pendingOperations.add(buildStructuredName(newDataInsertBuilder(localID, backrefIdx), contact).build());
|
||||
|
||||
for (Telephone number : contact.getPhoneNumbers())
|
||||
pendingOperations.add(buildPhoneNumber(newDataInsertBuilder(localID, backrefIdx), number).build());
|
||||
queueOperation(buildPhoneNumber(newDataInsertBuilder(localID, backrefIdx), number));
|
||||
|
||||
for (ezvcard.property.Email email : contact.getEmails())
|
||||
pendingOperations.add(buildEmail(newDataInsertBuilder(localID, backrefIdx), email).build());
|
||||
queueOperation(buildEmail(newDataInsertBuilder(localID, backrefIdx), email));
|
||||
|
||||
if (contact.getPhoto() != null)
|
||||
pendingOperations.add(buildPhoto(newDataInsertBuilder(localID, backrefIdx), contact.getPhoto()).build());
|
||||
queueOperation(buildPhoto(newDataInsertBuilder(localID, backrefIdx), contact.getPhoto()));
|
||||
|
||||
if (contact.getOrganization() != null || contact.getRole() != null)
|
||||
pendingOperations.add(buildOrganization(newDataInsertBuilder(localID, backrefIdx), contact.getOrganization(), contact.getRole()).build());
|
||||
queueOperation(buildOrganization(newDataInsertBuilder(localID, backrefIdx), contact.getOrganization(), contact.getRole()));
|
||||
|
||||
for (Impp impp : contact.getImpps())
|
||||
pendingOperations.add(buildIMPP(newDataInsertBuilder(localID, backrefIdx), impp).build());
|
||||
queueOperation(buildIMPP(newDataInsertBuilder(localID, backrefIdx), impp));
|
||||
|
||||
if (contact.getNickName() != null)
|
||||
pendingOperations.add(buildNickName(newDataInsertBuilder(localID, backrefIdx), contact.getNickName()).build());
|
||||
queueOperation(buildNickName(newDataInsertBuilder(localID, backrefIdx), contact.getNickName()));
|
||||
|
||||
if (contact.getNote() != null)
|
||||
pendingOperations.add(buildNote(newDataInsertBuilder(localID, backrefIdx), contact.getNote()).build());
|
||||
queueOperation(buildNote(newDataInsertBuilder(localID, backrefIdx), contact.getNote()));
|
||||
|
||||
for (Address address : contact.getAddresses())
|
||||
pendingOperations.add(buildAddress(newDataInsertBuilder(localID, backrefIdx), address).build());
|
||||
queueOperation(buildAddress(newDataInsertBuilder(localID, backrefIdx), address));
|
||||
|
||||
// TODO group membership
|
||||
|
||||
if (contact.getURL() != null)
|
||||
pendingOperations.add(buildURL(newDataInsertBuilder(localID, backrefIdx), contact.getURL()).build());
|
||||
queueOperation(buildURL(newDataInsertBuilder(localID, backrefIdx), contact.getURL()));
|
||||
|
||||
// events
|
||||
if (contact.getAnniversary() != null)
|
||||
pendingOperations.add(buildEvent(newDataInsertBuilder(localID, backrefIdx),
|
||||
contact.getAnniversary(), CommonDataKinds.Event.TYPE_ANNIVERSARY).build());
|
||||
queueOperation(buildEvent(newDataInsertBuilder(localID, backrefIdx), contact.getAnniversary(), CommonDataKinds.Event.TYPE_ANNIVERSARY));
|
||||
if (contact.getBirthDay() != null)
|
||||
pendingOperations.add(buildEvent(newDataInsertBuilder(localID, backrefIdx),
|
||||
contact.getBirthDay(), CommonDataKinds.Event.TYPE_BIRTHDAY).build());
|
||||
queueOperation(buildEvent(newDataInsertBuilder(localID, backrefIdx), contact.getBirthDay(), CommonDataKinds.Event.TYPE_BIRTHDAY));
|
||||
|
||||
// TODO relation
|
||||
// TODO SIP address
|
||||
@ -650,6 +652,13 @@ public class LocalAddressBook extends LocalCollection<Contact> {
|
||||
|
||||
int protocolCode;
|
||||
String protocolLabel = null;
|
||||
|
||||
String protocol = impp.getProtocol();
|
||||
if (protocol == null) {
|
||||
Log.w(TAG, "Ignoring IMPP address without protocol");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (impp.isAim())
|
||||
protocolCode = Im.PROTOCOL_AIM;
|
||||
else if (impp.isMsn())
|
||||
@ -658,20 +667,17 @@ public class LocalAddressBook extends LocalCollection<Contact> {
|
||||
protocolCode = Im.PROTOCOL_YAHOO;
|
||||
else if (impp.isSkype())
|
||||
protocolCode = Im.PROTOCOL_SKYPE;
|
||||
else if (impp.getProtocol().equalsIgnoreCase("qq"))
|
||||
else if (protocol.equalsIgnoreCase("qq"))
|
||||
protocolCode = Im.PROTOCOL_QQ;
|
||||
else if (impp.getProtocol().equalsIgnoreCase("google-talk"))
|
||||
else if (protocol.equalsIgnoreCase("google-talk"))
|
||||
protocolCode = Im.PROTOCOL_GOOGLE_TALK;
|
||||
else if (impp.isIcq())
|
||||
protocolCode = Im.PROTOCOL_ICQ;
|
||||
else if (impp.isXmpp())
|
||||
else if (impp.isXmpp() || protocol.equalsIgnoreCase("jabber"))
|
||||
protocolCode = Im.PROTOCOL_JABBER;
|
||||
else if (impp.getProtocol().equalsIgnoreCase("netmeeting"))
|
||||
else if (protocol.equalsIgnoreCase("netmeeting"))
|
||||
protocolCode = Im.PROTOCOL_NETMEETING;
|
||||
else {
|
||||
String protocol = impp.getProtocol();
|
||||
if (protocol == null)
|
||||
protocol = "unknown";
|
||||
protocolCode = Im.PROTOCOL_CUSTOM;
|
||||
protocolLabel = protocol;
|
||||
}
|
||||
@ -761,12 +767,13 @@ public class LocalAddressBook extends LocalCollection<Contact> {
|
||||
|
||||
protected Builder buildEvent(Builder builder, DateOrTimeProperty date, int type) {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
|
||||
if (date.getDate() != null)
|
||||
return builder
|
||||
.withValue(Data.MIMETYPE, CommonDataKinds.Event.CONTENT_ITEM_TYPE)
|
||||
.withValue(CommonDataKinds.Event.TYPE, type)
|
||||
.withValue(CommonDataKinds.Event.START_DATE, formatter.format(date.getDate()));
|
||||
else
|
||||
return builder;
|
||||
if (date.getDate() == null) {
|
||||
Log.i(TAG, "Ignoring contact event without date");
|
||||
return null;
|
||||
}
|
||||
return builder
|
||||
.withValue(Data.MIMETYPE, CommonDataKinds.Event.CONTENT_ITEM_TYPE)
|
||||
.withValue(CommonDataKinds.Event.TYPE, type)
|
||||
.withValue(CommonDataKinds.Event.START_DATE, formatter.format(date.getDate()));
|
||||
}
|
||||
}
|
||||
|
@ -23,13 +23,6 @@ import android.os.RemoteException;
|
||||
import android.provider.CalendarContract;
|
||||
import android.util.Log;
|
||||
|
||||
/* TODO: move Android <-> iCal/VCard code to adapter class for better maintenance / testing
|
||||
* RemoteCollection<> LocalCollection<>
|
||||
* RemoteResource <-- ResourceAdapter ----> LocalResource
|
||||
* - RemoteContact <-- - ContactAdapter --> - LocalContact
|
||||
* - RemoteEvent <-- - EventAdapter ----> - LocalEvent
|
||||
*/
|
||||
|
||||
public abstract class LocalCollection<T extends Resource> {
|
||||
private static final String TAG = "davdroid.LocalCollection";
|
||||
|
||||
@ -151,6 +144,11 @@ public abstract class LocalCollection<T extends Resource> {
|
||||
|
||||
|
||||
public abstract void populate(Resource record) throws RemoteException;
|
||||
|
||||
protected void queueOperation(Builder builder) {
|
||||
if (builder != null)
|
||||
pendingOperations.add(builder.build());
|
||||
}
|
||||
|
||||
|
||||
// create/update/delete
|
||||
|
9
test/assets/impp.vcf
Normal file
9
test/assets/impp.vcf
Normal file
@ -0,0 +1,9 @@
|
||||
BEGIN:VCARD
|
||||
VERSION:3.0
|
||||
UID:2de59c6cc9
|
||||
PRODID:-//ownCloud//NONSGML Contacts 0.2.5//EN
|
||||
REV:2013-12-08T00:04:30+00:00
|
||||
FN:test mctest
|
||||
N:mctest;test;;;
|
||||
IMPP;TYPE=WORK;X-SERVICE-TYPE=jabber:test-without-valid-scheme@test.tld
|
||||
END:VCARD
|
@ -10,11 +10,13 @@ package at.bitfire.davdroid.test;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import lombok.Cleanup;
|
||||
import net.fortuna.ical4j.data.ParserException;
|
||||
import junit.framework.Assert;
|
||||
import android.content.res.AssetManager;
|
||||
import android.test.InstrumentationTestCase;
|
||||
import at.bitfire.davdroid.resource.Contact;
|
||||
import ezvcard.VCardException;
|
||||
import ezvcard.property.Impp;
|
||||
|
||||
public class ContactTest extends InstrumentationTestCase {
|
||||
AssetManager assetMgr;
|
||||
@ -22,26 +24,40 @@ public class ContactTest extends InstrumentationTestCase {
|
||||
public void setUp() {
|
||||
assetMgr = getInstrumentation().getContext().getResources().getAssets();
|
||||
}
|
||||
|
||||
public void testIMPP() throws VCardException, IOException {
|
||||
Contact c = parseVCard("impp.vcf");
|
||||
assertEquals("test mctest", c.getDisplayName());
|
||||
|
||||
Impp jabber = c.getImpps().get(0);
|
||||
assertNull(jabber.getProtocol());
|
||||
assertEquals("test-without-valid-scheme@test.tld", jabber.getHandle());
|
||||
}
|
||||
|
||||
|
||||
public void testParseVcard3() throws IOException, ParserException {
|
||||
String fname = "vcard3-sample1.vcf";
|
||||
InputStream in = assetMgr.open(fname, AssetManager.ACCESS_STREAMING);
|
||||
Contact c = parseVCard("vcard3-sample1.vcf");
|
||||
|
||||
Contact c = new Contact(fname, null);
|
||||
c.parseEntity(in);
|
||||
assertEquals("Forrest Gump", c.getDisplayName());
|
||||
assertEquals("Forrest", c.getGivenName());
|
||||
assertEquals("Gump", c.getFamilyName());
|
||||
|
||||
Assert.assertEquals("Forrest Gump", c.getDisplayName());
|
||||
Assert.assertEquals("Forrest", c.getGivenName());
|
||||
Assert.assertEquals("Gump", c.getFamilyName());
|
||||
assertEquals(2, c.getPhoneNumbers().size());
|
||||
assertEquals("(111) 555-1212", c.getPhoneNumbers().get(0).getText());
|
||||
|
||||
Assert.assertEquals(2, c.getPhoneNumbers().size());
|
||||
Assert.assertEquals("(111) 555-1212", c.getPhoneNumbers().get(0).getText());
|
||||
assertEquals(1, c.getEmails().size());
|
||||
assertEquals("forrestgump@example.com", c.getEmails().get(0).getValue());
|
||||
|
||||
Assert.assertEquals(1, c.getEmails().size());
|
||||
Assert.assertEquals("forrestgump@example.com", c.getEmails().get(0).getValue());
|
||||
|
||||
Assert.assertFalse(c.isStarred());
|
||||
assertFalse(c.isStarred());
|
||||
}
|
||||
|
||||
|
||||
private Contact parseVCard(String fileName) throws VCardException, IOException {
|
||||
@Cleanup InputStream in = assetMgr.open(fileName, AssetManager.ACCESS_STREAMING);
|
||||
|
||||
Contact c = new Contact(fileName, null);
|
||||
c.parseEntity(in);
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
19
test/src/at/bitfire/davdroid/test/LombokTest.java
Normal file
19
test/src/at/bitfire/davdroid/test/LombokTest.java
Normal file
@ -0,0 +1,19 @@
|
||||
package at.bitfire.davdroid.test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import lombok.NonNull;
|
||||
|
||||
/* Lombok has some strange issues under Android:
|
||||
* @NonNull always crashes on Android – http://code.google.com/p/projectlombok/issues/detail?id=612
|
||||
*/
|
||||
|
||||
public class LombokTest extends TestCase {
|
||||
private String appendSlash(@NonNull String s) {
|
||||
return s + "/";
|
||||
}
|
||||
|
||||
public void testNonNull() {
|
||||
assertNull(appendSlash(null));
|
||||
//assertEquals("1/", appendSlash("1"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user