mirror of
https://github.com/zxing/zxing.git
synced 2024-11-10 04:54:04 -08:00
Add description support, as well as handle VEVENT line continuation. Get rid of unused 'title' property
git-svn-id: https://zxing.googlecode.com/svn/trunk@1304 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
fe1004f378
commit
2e9afb3673
|
@ -35,6 +35,7 @@ import java.util.GregorianCalendar;
|
|||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
public final class CalendarResultHandler extends ResultHandler {
|
||||
|
||||
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
|
||||
private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
|
||||
|
||||
|
@ -63,7 +64,8 @@ public final class CalendarResultHandler extends ResultHandler {
|
|||
addCalendarEvent(calendarResult.getSummary(),
|
||||
calendarResult.getStart(),
|
||||
calendarResult.getEnd(),
|
||||
calendarResult.getLocation());
|
||||
calendarResult.getLocation(),
|
||||
calendarResult.getDescription());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,7 +85,7 @@ public final class CalendarResultHandler extends ResultHandler {
|
|||
|
||||
ParsedResult.maybeAppend(calResult.getLocation(), result);
|
||||
ParsedResult.maybeAppend(calResult.getAttendee(), result);
|
||||
ParsedResult.maybeAppend(calResult.getTitle(), result);
|
||||
ParsedResult.maybeAppend(calResult.getDescription(), result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -144,8 +144,13 @@ public abstract class ResultHandler {
|
|||
* @param start The start time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
|
||||
* @param end The end time as yyyyMMdd or yyyyMMdd'T'HHmmss or yyyyMMdd'T'HHmmss'Z'
|
||||
* @param location a text description of the event location
|
||||
* @param description a text description of the event itself
|
||||
*/
|
||||
final void addCalendarEvent(String summary, String start, String end, String location) {
|
||||
final void addCalendarEvent(String summary,
|
||||
String start,
|
||||
String end,
|
||||
String location,
|
||||
String description) {
|
||||
Intent intent = new Intent(Intent.ACTION_EDIT);
|
||||
intent.setType("vnd.android.cursor.item/event");
|
||||
intent.putExtra("beginTime", calculateMilliseconds(start));
|
||||
|
@ -158,6 +163,7 @@ public abstract class ResultHandler {
|
|||
intent.putExtra("endTime", calculateMilliseconds(end));
|
||||
intent.putExtra("title", summary);
|
||||
intent.putExtra("eventLocation", location);
|
||||
intent.putExtra("description", description);
|
||||
launchIntent(intent);
|
||||
}
|
||||
|
||||
|
|
|
@ -26,27 +26,31 @@ public final class CalendarParsedResult extends ParsedResult {
|
|||
private final String end;
|
||||
private final String location;
|
||||
private final String attendee;
|
||||
private final String title;
|
||||
private final String description;
|
||||
|
||||
public CalendarParsedResult(String summary,
|
||||
String start,
|
||||
String end,
|
||||
String location,
|
||||
String attendee,
|
||||
String title) {
|
||||
String description) {
|
||||
super(ParsedResultType.CALENDAR);
|
||||
// Start is required, end is not
|
||||
if (start == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
validateDate(start);
|
||||
validateDate(end);
|
||||
if (end == null) {
|
||||
end = start;
|
||||
} else {
|
||||
validateDate(end);
|
||||
}
|
||||
this.summary = summary;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.location = location;
|
||||
this.attendee = attendee;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getSummary() {
|
||||
|
@ -79,8 +83,8 @@ public final class CalendarParsedResult extends ParsedResult {
|
|||
return attendee;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getDisplayResult() {
|
||||
|
@ -90,7 +94,7 @@ public final class CalendarParsedResult extends ParsedResult {
|
|||
maybeAppend(end, result);
|
||||
maybeAppend(location, result);
|
||||
maybeAppend(attendee, result);
|
||||
maybeAppend(title, result);
|
||||
maybeAppend(description, result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -88,7 +88,12 @@ final class VCardResultParser extends ResultParser {
|
|||
}
|
||||
i++; // skip colon
|
||||
int start = i; // Found the start of a match here
|
||||
i = rawText.indexOf((int) '\n', i); // Really, ends in \r\n
|
||||
while ((i = rawText.indexOf((int) '\n', i)) >= 0 && // Really, ends in \r\n
|
||||
i < rawText.length() - 1 && // But if followed by tab or space,
|
||||
(rawText.charAt(i+1) == ' ' || // this is only a continuation
|
||||
rawText.charAt(i+1) == '\t')) {
|
||||
i += 2;
|
||||
}
|
||||
if (i < 0) {
|
||||
// No terminating end character? uh, done. Set i such that loop terminates and break
|
||||
i = max;
|
||||
|
@ -97,10 +102,14 @@ final class VCardResultParser extends ResultParser {
|
|||
if (matches == null) {
|
||||
matches = new Vector(3); // lazy init
|
||||
}
|
||||
if (rawText.charAt(i-1) == '\r') {
|
||||
i--; // Back up over \r, which really should be there
|
||||
}
|
||||
String element = rawText.substring(start, i);
|
||||
if (trim) {
|
||||
element = element.trim();
|
||||
}
|
||||
element = stripContinuationCRLF(element);
|
||||
matches.addElement(element);
|
||||
i++;
|
||||
} else {
|
||||
|
@ -113,6 +122,30 @@ final class VCardResultParser extends ResultParser {
|
|||
return toStringArray(matches);
|
||||
}
|
||||
|
||||
private static String stripContinuationCRLF(String value) {
|
||||
int length = value.length();
|
||||
StringBuffer result = new StringBuffer(length);
|
||||
boolean lastWasLF = false;
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (lastWasLF) {
|
||||
lastWasLF = false;
|
||||
continue;
|
||||
}
|
||||
char c = value.charAt(i);
|
||||
lastWasLF = false;
|
||||
switch (c) {
|
||||
case '\n':
|
||||
lastWasLF = true;
|
||||
break;
|
||||
case '\r':
|
||||
break;
|
||||
default:
|
||||
result.append(c);
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
static String matchSingleVCardPrefixedField(String prefix, String rawText, boolean trim) {
|
||||
String[] values = matchVCardPrefixedField(prefix, rawText, trim);
|
||||
return values == null ? null : values[0];
|
||||
|
|
|
@ -47,8 +47,9 @@ final class VEventResultParser extends ResultParser {
|
|||
String start = VCardResultParser.matchSingleVCardPrefixedField("DTSTART", rawText, true);
|
||||
String end = VCardResultParser.matchSingleVCardPrefixedField("DTEND", rawText, true);
|
||||
String location = VCardResultParser.matchSingleVCardPrefixedField("LOCATION", rawText, true);
|
||||
String description = VCardResultParser.matchSingleVCardPrefixedField("DESCRIPTION", rawText, true);
|
||||
try {
|
||||
return new CalendarParsedResult(summary, start, end, location, null, null);
|
||||
return new CalendarParsedResult(summary, start, end, location, null, description);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -27,16 +27,58 @@ import junit.framework.TestCase;
|
|||
*/
|
||||
public final class CalendarParsedResultTestCase extends TestCase {
|
||||
|
||||
public void testVEvent() {
|
||||
public void testStartEnd() {
|
||||
doTest(
|
||||
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nSUMMARY:foo\r\nDTSTART:20080504T123456Z\r\n" +
|
||||
"DTEND:20080505T234555Z\r\nLOCATION:Miami\r\n" +
|
||||
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
|
||||
"DTSTART:20080504T123456Z\r\n" +
|
||||
"DTEND:20080505T234555Z\r\n" +
|
||||
"END:VEVENT\r\nEND:VCALENDAR",
|
||||
null, "foo", "Miami", "20080504T123456Z", "20080505T234555Z", null);
|
||||
null, null, null, "20080504T123456Z", "20080505T234555Z", null);
|
||||
}
|
||||
|
||||
public void testStart() {
|
||||
doTest(
|
||||
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
|
||||
"DTSTART:20080504T123456Z\r\n" +
|
||||
"END:VEVENT\r\nEND:VCALENDAR",
|
||||
null, null, null, "20080504T123456Z", "20080504T123456Z", null);
|
||||
}
|
||||
|
||||
public void testSummary() {
|
||||
doTest(
|
||||
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
|
||||
"SUMMARY:foo\r\n" +
|
||||
"DTSTART:20080504T123456Z\r\n" +
|
||||
"END:VEVENT\r\nEND:VCALENDAR",
|
||||
null, "foo", null, "20080504T123456Z", "20080504T123456Z", null);
|
||||
}
|
||||
|
||||
public void testLocation() {
|
||||
doTest(
|
||||
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
|
||||
"LOCATION:Miami\r\n" +
|
||||
"DTSTART:20080504T123456Z\r\n" +
|
||||
"END:VEVENT\r\nEND:VCALENDAR",
|
||||
null, null, "Miami", "20080504T123456Z", "20080504T123456Z", null);
|
||||
}
|
||||
|
||||
public void testDescription() {
|
||||
doTest(
|
||||
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
|
||||
"DTSTART:20080504T123456Z\r\n" +
|
||||
"DESCRIPTION:This is a test\r\n" +
|
||||
"END:VEVENT\r\nEND:VCALENDAR",
|
||||
"This is a test", null, null, "20080504T123456Z", "20080504T123456Z", null);
|
||||
doTest(
|
||||
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\n" +
|
||||
"DTSTART:20080504T123456Z\r\n" +
|
||||
"DESCRIPTION:This is a test\r\n\t with a continuation\r\n" +
|
||||
"END:VEVENT\r\nEND:VCALENDAR",
|
||||
"This is a test with a continuation", null, null, "20080504T123456Z", "20080504T123456Z", null);
|
||||
}
|
||||
|
||||
private static void doTest(String contents,
|
||||
String title,
|
||||
String description,
|
||||
String summary,
|
||||
String location,
|
||||
String start,
|
||||
|
@ -46,7 +88,7 @@ public final class CalendarParsedResultTestCase extends TestCase {
|
|||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertSame(ParsedResultType.CALENDAR, result.getType());
|
||||
CalendarParsedResult calResult = (CalendarParsedResult) result;
|
||||
assertEquals(title, calResult.getTitle());
|
||||
assertEquals(description, calResult.getDescription());
|
||||
assertEquals(summary, calResult.getSummary());
|
||||
assertEquals(location, calResult.getLocation());
|
||||
assertEquals(start, calResult.getStart());
|
||||
|
|
Loading…
Reference in a new issue