Don't send bare line feeds to EAS 2.5

* EAS 2.5 doesn't like seeing bare LF's in Calendar location
  and description, and Events including them won't sync back to
  the server
* Create a utility to replace bare LF's with CRLF and write
  unit test for the utility
* Fix the bug by using this utility

Bug: 2542220
Change-Id: I2c72d23f15e3a922ebe3585e063abe9fa9e2366f
This commit is contained in:
Marc Blank 2010-03-25 12:12:44 -07:00
parent fe61f358ab
commit eba33f8b5a
3 changed files with 24 additions and 2 deletions

View File

@ -502,4 +502,8 @@ public class Utility {
sb.append("0123456789ABCDEF".charAt(b & 0xF));
return sb;
}
public static String replaceBareLfWithCrlf(String str) {
return str.replace("\r", "").replace("\n", "\r\n");
}
}

View File

@ -51,6 +51,7 @@ import android.provider.Calendar.ExtendedProperties;
import android.provider.Calendar.Reminders;
import android.provider.Calendar.SyncState;
import android.provider.ContactsContract.RawContacts;
import android.text.TextUtils;
import android.util.Log;
import java.io.IOException;
@ -1191,7 +1192,14 @@ public class CalendarSyncAdapter extends AbstractSyncAdapter {
s.data(Tags.CALENDAR_DTSTAMP,
CalendarUtilities.millisToEasDateTime(System.currentTimeMillis()));
s.writeStringValue(entityValues, Events.EVENT_LOCATION, Tags.CALENDAR_LOCATION);
String loc = entityValues.getAsString(Events.EVENT_LOCATION);
if (!TextUtils.isEmpty(loc)) {
if (mService.mProtocolVersionDouble < Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE) {
// EAS 2.5 doesn't like bare line feeds
loc = Utility.replaceBareLfWithCrlf(loc);
}
s.data(Tags.CALENDAR_LOCATION, loc);
}
s.writeStringValue(entityValues, Events.TITLE, Tags.CALENDAR_SUBJECT);
Integer visibility = entityValues.getAsInteger(Events.VISIBILITY);
@ -1211,7 +1219,8 @@ public class CalendarSyncAdapter extends AbstractSyncAdapter {
s.data(Tags.BASE_DATA, desc);
s.end();
} else {
s.data(Tags.CALENDAR_BODY, desc);
// EAS 2.5 doesn't like bare line feeds
s.data(Tags.CALENDAR_BODY, Utility.replaceBareLfWithCrlf(desc));
}
}

View File

@ -161,4 +161,13 @@ public class UtilityUnitTests extends AndroidTestCase {
assertEquals("val=" + i, i, Integer.parseInt(hex, 16));
}
}
public void testReplaceBareLfWithCrlf() {
assertEquals("", Utility.replaceBareLfWithCrlf(""));
assertEquals("", Utility.replaceBareLfWithCrlf("\r"));
assertEquals("\r\n", Utility.replaceBareLfWithCrlf("\r\n"));
assertEquals("\r\n", Utility.replaceBareLfWithCrlf("\n"));
assertEquals("\r\n\r\n\r\n", Utility.replaceBareLfWithCrlf("\n\n\n"));
assertEquals("A\r\nB\r\nC\r\nD", Utility.replaceBareLfWithCrlf("A\nB\r\nC\nD"));
}
}