Support GEO, NICKNAME from vCard (add as a 'note' in Android)

git-svn-id: https://zxing.googlecode.com/svn/trunk@2551 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2013-01-09 21:32:05 +00:00
parent 2d135af9c9
commit b89f3d5e35
7 changed files with 85 additions and 13 deletions

View file

@ -123,6 +123,7 @@ public final class AddressBookResultHandler extends ResultHandler {
switch (action) { switch (action) {
case 0: case 0:
addContact(addressResult.getNames(), addContact(addressResult.getNames(),
addressResult.getNicknames(),
addressResult.getPronunciation(), addressResult.getPronunciation(),
addressResult.getPhoneNumbers(), addressResult.getPhoneNumbers(),
addressResult.getPhoneTypes(), addressResult.getPhoneTypes(),
@ -135,7 +136,8 @@ public final class AddressBookResultHandler extends ResultHandler {
addressResult.getOrg(), addressResult.getOrg(),
addressResult.getTitle(), addressResult.getTitle(),
addressResult.getURL(), addressResult.getURL(),
addressResult.getBirthday()); addressResult.getBirthday(),
addressResult.getGeo());
break; break;
case 1: case 1:
String[] names = addressResult.getNames(); String[] names = addressResult.getNames();

View file

@ -203,14 +203,15 @@ public abstract class ResultHandler {
} }
final void addPhoneOnlyContact(String[] phoneNumbers,String[] phoneTypes) { final void addPhoneOnlyContact(String[] phoneNumbers,String[] phoneTypes) {
addContact(null, null, phoneNumbers, phoneTypes, null, null, null, null, null, null, null, null, null, null); addContact(null, null, null, phoneNumbers, phoneTypes, null, null, null, null, null, null, null, null, null, null, null);
} }
final void addEmailOnlyContact(String[] emails, String[] emailTypes) { final void addEmailOnlyContact(String[] emails, String[] emailTypes) {
addContact(null, null, null, null, emails, emailTypes, null, null, null, null, null, null, null, null); addContact(null, null, null, null, null, emails, emailTypes, null, null, null, null, null, null, null, null, null);
} }
final void addContact(String[] names, final void addContact(String[] names,
String[] nicknames,
String pronunciation, String pronunciation,
String[] phoneNumbers, String[] phoneNumbers,
String[] phoneTypes, String[] phoneTypes,
@ -223,7 +224,8 @@ public abstract class ResultHandler {
String org, String org,
String title, String title,
String url, String url,
String birthday) { String birthday,
String[] geo) {
// Only use the first name in the array, if present. // Only use the first name in the array, if present.
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT, ContactsContract.Contacts.CONTENT_URI); Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT, ContactsContract.Contacts.CONTENT_URI);
@ -258,14 +260,21 @@ public abstract class ResultHandler {
StringBuilder aggregatedNotes = new StringBuilder(); StringBuilder aggregatedNotes = new StringBuilder();
for (String aNote : new String[] { url, birthday, note }) { for (String aNote : new String[] { url, birthday, note }) {
if (aNote != null) { if (aNote != null) {
if (aggregatedNotes.length() > 0) { aggregatedNotes.append('\n').append(aNote);
aggregatedNotes.append('\n');
}
aggregatedNotes.append(aNote);
} }
} }
if (nicknames != null) {
for (String nickname : nicknames) {
aggregatedNotes.append('\n').append(nickname);
}
}
if (geo != null) {
aggregatedNotes.append('\n').append(geo[0]).append(',').append(geo[1]);
}
if (aggregatedNotes.length() > 0) { if (aggregatedNotes.length() > 0) {
putExtra(intent, ContactsContract.Intents.Insert.NOTES, aggregatedNotes.toString()); // Remove extra leading '\n'
putExtra(intent, ContactsContract.Intents.Insert.NOTES, aggregatedNotes.substring(1));
} }
putExtra(intent, ContactsContract.Intents.Insert.IM_HANDLE, instantMessenger); putExtra(intent, ContactsContract.Intents.Insert.IM_HANDLE, instantMessenger);

View file

@ -50,6 +50,7 @@ public final class AddressBookAUResultParser extends ResultParser {
String address = matchSinglePrefixedField("ADD:", rawText, '\r', true); String address = matchSinglePrefixedField("ADD:", rawText, '\r', true);
String[] addresses = address == null ? null : new String[] {address}; String[] addresses = address == null ? null : new String[] {address};
return new AddressBookParsedResult(maybeWrap(name), return new AddressBookParsedResult(maybeWrap(name),
null,
pronunciation, pronunciation,
phoneNumbers, phoneNumbers,
null, null,
@ -62,6 +63,7 @@ public final class AddressBookAUResultParser extends ResultParser {
null, null,
null, null,
null, null,
null,
null); null);
} }

View file

@ -63,6 +63,7 @@ public final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultPar
String org = matchSingleDoCoMoPrefixedField("ORG:", rawText, true); String org = matchSingleDoCoMoPrefixedField("ORG:", rawText, true);
return new AddressBookParsedResult(maybeWrap(name), return new AddressBookParsedResult(maybeWrap(name),
null,
pronunciation, pronunciation,
phoneNumbers, phoneNumbers,
null, null,
@ -75,7 +76,8 @@ public final class AddressBookDoCoMoResultParser extends AbstractDoCoMoResultPar
org, org,
birthday, birthday,
null, null,
url); url,
null);
} }
private static String parseName(String name) { private static String parseName(String name) {

View file

@ -22,6 +22,7 @@ package com.google.zxing.client.result;
public final class AddressBookParsedResult extends ParsedResult { public final class AddressBookParsedResult extends ParsedResult {
private final String[] names; private final String[] names;
private final String[] nicknames;
private final String pronunciation; private final String pronunciation;
private final String[] phoneNumbers; private final String[] phoneNumbers;
private final String[] phoneTypes; private final String[] phoneTypes;
@ -35,8 +36,35 @@ public final class AddressBookParsedResult extends ParsedResult {
private final String birthday; private final String birthday;
private final String title; private final String title;
private final String url; private final String url;
private final String[] geo;
public AddressBookParsedResult(String[] names, public AddressBookParsedResult(String[] names,
String[] phoneNumbers,
String[] phoneTypes,
String[] emails,
String[] emailTypes,
String[] addresses,
String[] addressTypes) {
this(names,
null,
null,
phoneNumbers,
phoneTypes,
emails,
emailTypes,
null,
null,
addresses,
addressTypes,
null,
null,
null,
null,
null);
}
public AddressBookParsedResult(String[] names,
String[] nicknames,
String pronunciation, String pronunciation,
String[] phoneNumbers, String[] phoneNumbers,
String[] phoneTypes, String[] phoneTypes,
@ -49,9 +77,11 @@ public final class AddressBookParsedResult extends ParsedResult {
String org, String org,
String birthday, String birthday,
String title, String title,
String url) { String url,
String[] geo) {
super(ParsedResultType.ADDRESSBOOK); super(ParsedResultType.ADDRESSBOOK);
this.names = names; this.names = names;
this.nicknames = nicknames;
this.pronunciation = pronunciation; this.pronunciation = pronunciation;
this.phoneNumbers = phoneNumbers; this.phoneNumbers = phoneNumbers;
this.phoneTypes = phoneTypes; this.phoneTypes = phoneTypes;
@ -65,12 +95,17 @@ public final class AddressBookParsedResult extends ParsedResult {
this.birthday = birthday; this.birthday = birthday;
this.title = title; this.title = title;
this.url = url; this.url = url;
this.geo = geo;
} }
public String[] getNames() { public String[] getNames() {
return names; return names;
} }
public String[] getNicknames() {
return nicknames;
}
/** /**
* In Japanese, the name is written in kanji, which can have multiple readings. Therefore a hint * In Japanese, the name is written in kanji, which can have multiple readings. Therefore a hint
* is often provided, called furigana, which spells the name phonetically. * is often provided, called furigana, which spells the name phonetically.
@ -144,10 +179,18 @@ public final class AddressBookParsedResult extends ParsedResult {
return birthday; return birthday;
} }
/**
* @return a location as a latitude/longitude pair
*/
public String[] getGeo() {
return geo;
}
@Override @Override
public String getDisplayResult() { public String getDisplayResult() {
StringBuilder result = new StringBuilder(100); StringBuilder result = new StringBuilder(100);
maybeAppend(names, result); maybeAppend(names, result);
maybeAppend(nicknames, result);
maybeAppend(pronunciation, result); maybeAppend(pronunciation, result);
maybeAppend(title, result); maybeAppend(title, result);
maybeAppend(org, result); maybeAppend(org, result);
@ -157,6 +200,7 @@ public final class AddressBookParsedResult extends ParsedResult {
maybeAppend(instantMessenger, result); maybeAppend(instantMessenger, result);
maybeAppend(url, result); maybeAppend(url, result);
maybeAppend(birthday, result); maybeAppend(birthday, result);
maybeAppend(geo, result);
maybeAppend(note, result); maybeAppend(note, result);
return result.toString(); return result.toString();
} }

View file

@ -52,6 +52,7 @@ public final class BizcardResultParser extends AbstractDoCoMoResultParser {
String email = matchSingleDoCoMoPrefixedField("E:", rawText, true); String email = matchSingleDoCoMoPrefixedField("E:", rawText, true);
return new AddressBookParsedResult(maybeWrap(fullName), return new AddressBookParsedResult(maybeWrap(fullName),
null,
null, null,
buildPhoneNumbers(phoneNumber1, phoneNumber2, phoneNumber3), buildPhoneNumbers(phoneNumber1, phoneNumber2, phoneNumber3),
null, null,
@ -64,6 +65,7 @@ public final class BizcardResultParser extends AbstractDoCoMoResultParser {
org, org,
null, null,
title, title,
null,
null); null);
} }

View file

@ -42,6 +42,8 @@ public final class VCardResultParser extends ResultParser {
private static final Pattern EQUALS = Pattern.compile("="); private static final Pattern EQUALS = Pattern.compile("=");
private static final Pattern SEMICOLON = Pattern.compile(";"); private static final Pattern SEMICOLON = Pattern.compile(";");
private static final Pattern UNESCAPED_SEMICOLONS = Pattern.compile("(?<!\\\\);+"); private static final Pattern UNESCAPED_SEMICOLONS = Pattern.compile("(?<!\\\\);+");
private static final Pattern COMMA = Pattern.compile(",");
private static final Pattern SEMICOLON_OR_COMMA = Pattern.compile("[;,]");
@Override @Override
public AddressBookParsedResult parse(Result result) { public AddressBookParsedResult parse(Result result) {
@ -59,6 +61,8 @@ public final class VCardResultParser extends ResultParser {
names = matchVCardPrefixedField("N", rawText, true, false); names = matchVCardPrefixedField("N", rawText, true, false);
formatNames(names); formatNames(names);
} }
List<String> nicknameString = matchSingleVCardPrefixedField("NICKNAME", rawText, true, false);
String[] nicknames = nicknameString == null ? null : COMMA.split(nicknameString.get(0));
List<List<String>> phoneNumbers = matchVCardPrefixedField("TEL", rawText, true, false); List<List<String>> phoneNumbers = matchVCardPrefixedField("TEL", rawText, true, false);
List<List<String>> emails = matchVCardPrefixedField("EMAIL", rawText, true, false); List<List<String>> emails = matchVCardPrefixedField("EMAIL", rawText, true, false);
List<String> note = matchSingleVCardPrefixedField("NOTE", rawText, false, false); List<String> note = matchSingleVCardPrefixedField("NOTE", rawText, false, false);
@ -71,7 +75,13 @@ public final class VCardResultParser extends ResultParser {
List<String> title = matchSingleVCardPrefixedField("TITLE", rawText, true, false); List<String> title = matchSingleVCardPrefixedField("TITLE", rawText, true, false);
List<String> url = matchSingleVCardPrefixedField("URL", rawText, true, false); List<String> url = matchSingleVCardPrefixedField("URL", rawText, true, false);
List<String> instantMessenger = matchSingleVCardPrefixedField("IMPP", rawText, true, false); List<String> instantMessenger = matchSingleVCardPrefixedField("IMPP", rawText, true, false);
return new AddressBookParsedResult(toPrimaryValues(names), List<String> geoString = matchSingleVCardPrefixedField("GEO", rawText, true, false);
String[] geo = geoString == null ? null : SEMICOLON_OR_COMMA.split(geoString.get(0));
if (geo != null && geo.length != 2) {
geo = null;
}
return new AddressBookParsedResult(toPrimaryValues(names),
nicknames,
null, null,
toPrimaryValues(phoneNumbers), toPrimaryValues(phoneNumbers),
toTypes(phoneNumbers), toTypes(phoneNumbers),
@ -84,7 +94,8 @@ public final class VCardResultParser extends ResultParser {
toPrimaryValue(org), toPrimaryValue(org),
toPrimaryValue(birthday), toPrimaryValue(birthday),
toPrimaryValue(title), toPrimaryValue(title),
toPrimaryValue(url)); toPrimaryValue(url),
geo);
} }
static List<List<String>> matchVCardPrefixedField(CharSequence prefix, static List<List<String>> matchVCardPrefixedField(CharSequence prefix,