Merge change I76588b56 into eclair-mr2

* changes:
  Make vCard importer handle "PAGER" phone number correctly.
This commit is contained in:
Android (Google) Code Review
2009-11-04 19:45:14 -05:00
4 changed files with 71 additions and 21 deletions

View File

@@ -471,20 +471,24 @@ public class ContactStruct {
} }
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
String trimed = data.trim(); String trimed = data.trim();
int length = trimed.length(); final String formattedNumber;
for (int i = 0; i < length; i++) { if (type == Phone.TYPE_PAGER) {
char ch = trimed.charAt(i); formattedNumber = trimed;
if (('0' <= ch && ch <= '9') || (i == 0 && ch == '+')) { } else {
builder.append(ch); final int length = trimed.length();
for (int i = 0; i < length; i++) {
char ch = trimed.charAt(i);
if (('0' <= ch && ch <= '9') || (i == 0 && ch == '+')) {
builder.append(ch);
}
} }
}
// Use NANP in default when there's no information about locale. // Use NANP in default when there's no information about locale.
final int formattingType = (VCardConfig.isJapaneseDevice(mVCardType) ? final int formattingType = (VCardConfig.isJapaneseDevice(mVCardType) ?
PhoneNumberUtils.FORMAT_JAPAN : PhoneNumberUtils.FORMAT_NANP); PhoneNumberUtils.FORMAT_JAPAN : PhoneNumberUtils.FORMAT_NANP);
final String formattedPhoneNumber = formattedNumber = PhoneNumberUtils.formatNumber(builder.toString(), formattingType);
PhoneNumberUtils.formatNumber(builder.toString(), formattingType); }
PhoneData phoneData = new PhoneData(type, formattedPhoneNumber, label, isPrimary); PhoneData phoneData = new PhoneData(type, formattedNumber, label, isPrimary);
mPhoneList.add(phoneData); mPhoneList.add(phoneData);
} }
@@ -856,7 +860,8 @@ public class ContactStruct {
} }
} else if (propName.equals(Constants.PROPERTY_TEL)) { } else if (propName.equals(Constants.PROPERTY_TEL)) {
final Collection<String> typeCollection = paramMap.get(Constants.PARAM_TYPE); final Collection<String> typeCollection = paramMap.get(Constants.PARAM_TYPE);
final Object typeObject = VCardUtils.getPhoneTypeFromStrings(typeCollection); final Object typeObject =
VCardUtils.getPhoneTypeFromStrings(typeCollection, propValue);
final int type; final int type;
final String label; final String label;
if (typeObject instanceof Integer) { if (typeObject instanceof Integer) {

View File

@@ -98,7 +98,11 @@ public class VCardUtils {
* Returns Interger when the given types can be parsed as known type. Returns String object * Returns Interger when the given types can be parsed as known type. Returns String object
* when not, which should be set to label. * when not, which should be set to label.
*/ */
public static Object getPhoneTypeFromStrings(Collection<String> types) { public static Object getPhoneTypeFromStrings(Collection<String> types,
String number) {
if (number == null) {
number = "";
}
int type = -1; int type = -1;
String label = null; String label = null;
boolean isFax = false; boolean isFax = false;
@@ -117,7 +121,20 @@ public class VCardUtils {
} }
Integer tmp = sKnownPhoneTypeMap_StoI.get(typeString); Integer tmp = sKnownPhoneTypeMap_StoI.get(typeString);
if (tmp != null) { if (tmp != null) {
type = tmp; final int typeCandidate = tmp;
// TYPE_PAGER is prefered when the number contains @ surronded by
// a pager number and a domain name.
// e.g.
// o 1111@domain.com
// x @domain.com
// x 1111@
final int indexOfAt = number.indexOf("@");
if ((typeCandidate == Phone.TYPE_PAGER
&& 0 < indexOfAt && indexOfAt < number.length() - 1)
|| type < 0
|| type == Phone.TYPE_CUSTOM) {
type = tmp;
}
} else if (type < 0) { } else if (type < 0) {
type = Phone.TYPE_CUSTOM; type = Phone.TYPE_CUSTOM;
label = typeString; label = typeString;

View File

@@ -0,0 +1,5 @@
BEGIN:VCARD
VERSION:3.0
N:F;G;M;;
TEL;TYPE=PAGER,WORK,MSG:6101231234@pagersample.com
END:VCARD

View File

@@ -18,7 +18,6 @@ package com.android.unit_tests.vcard;
import android.content.ContentValues; import android.content.ContentValues;
import android.pim.vcard.VCardConfig; import android.pim.vcard.VCardConfig;
import android.pim.vcard.VCardParser_V21;
import android.pim.vcard.exception.VCardException; import android.pim.vcard.exception.VCardException;
import android.provider.ContactsContract.Data; import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Email;
@@ -35,7 +34,6 @@ import com.android.unit_tests.R;
import com.android.unit_tests.vcard.PropertyNodesVerifierElem.TypeSet; import com.android.unit_tests.vcard.PropertyNodesVerifierElem.TypeSet;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
public class VCardImporterTests extends VCardTestsBase { public class VCardImporterTests extends VCardTestsBase {
@@ -866,8 +864,9 @@ public class VCardImporterTests extends VCardTestsBase {
} }
public void testV21Japanese2_Type_Generic_Utf8() throws IOException, VCardException { public void testV21Japanese2_Type_Generic_Utf8() throws IOException, VCardException {
ImportVerifierElem verifier = new ImportVerifierElem(); ImportVerifier verifier = new ImportVerifier();
verifier.addExpected(StructuredName.CONTENT_ITEM_TYPE) ImportVerifierElem elem = verifier.addImportVerifierElem();
elem.addExpected(StructuredName.CONTENT_ITEM_TYPE)
.put(StructuredName.FAMILY_NAME, "\u5B89\u85E4") .put(StructuredName.FAMILY_NAME, "\u5B89\u85E4")
.put(StructuredName.GIVEN_NAME, "\u30ED\u30A4\u30C9\u0031") .put(StructuredName.GIVEN_NAME, "\u30ED\u30A4\u30C9\u0031")
.put(StructuredName.DISPLAY_NAME, .put(StructuredName.DISPLAY_NAME,
@@ -877,7 +876,7 @@ public class VCardImporterTests extends VCardTestsBase {
.put(StructuredName.PHONETIC_FAMILY_NAME, "\uFF71\uFF9D\uFF84\uFF9E\uFF73") .put(StructuredName.PHONETIC_FAMILY_NAME, "\uFF71\uFF9D\uFF84\uFF9E\uFF73")
.put(StructuredName.PHONETIC_GIVEN_NAME, "\uFF9B\uFF72\uFF84\uFF9E\u0031"); .put(StructuredName.PHONETIC_GIVEN_NAME, "\uFF9B\uFF72\uFF84\uFF9E\u0031");
verifier.addExpected(StructuredPostal.CONTENT_ITEM_TYPE) elem.addExpected(StructuredPostal.CONTENT_ITEM_TYPE)
.put(StructuredPostal.POSTCODE, "150-8512") .put(StructuredPostal.POSTCODE, "150-8512")
.put(StructuredPostal.NEIGHBORHOOD, .put(StructuredPostal.NEIGHBORHOOD,
"\u6771\u4EAC\u90FD\u6E0B\u8C37\u533A\u685C" + "\u6771\u4EAC\u90FD\u6E0B\u8C37\u533A\u685C" +
@@ -890,7 +889,7 @@ public class VCardImporterTests extends VCardTestsBase {
"\u30EB\u30EA\u30A2\u30F3\u30BF\u30EF\u30FC" + "\u30EB\u30EA\u30A2\u30F3\u30BF\u30EF\u30FC" +
"\u0036\u968E 150-8512") "\u0036\u968E 150-8512")
.put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME); .put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME);
verifier.addExpected(Note.CONTENT_ITEM_TYPE) elem.addExpected(Note.CONTENT_ITEM_TYPE)
.put(Note.NOTE, "\u30E1\u30E2"); .put(Note.NOTE, "\u30E1\u30E2");
verifier.verify(R.raw.v21_japanese_2, VCardConfig.VCARD_TYPE_V21_GENERIC_UTF8); verifier.verify(R.raw.v21_japanese_2, VCardConfig.VCARD_TYPE_V21_GENERIC_UTF8);
} }
@@ -1013,4 +1012,28 @@ public class VCardImporterTests extends VCardTestsBase {
.put(Phone.NUMBER, "20"); .put(Phone.NUMBER, "20");
verifier.verify(R.raw.v21_multiple_entry, VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS); verifier.verify(R.raw.v21_multiple_entry, VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS);
} }
public void testPagerV30_Parse() throws IOException, VCardException {
PropertyNodesVerifier verifier = new PropertyNodesVerifier(this);
verifier.addPropertyNodesVerifierElem()
.addNodeWithOrder("VERSION", "3.0")
.addNodeWithOrder("N", Arrays.asList("F", "G", "M", "", ""))
.addNodeWithOrder("TEL", "6101231234@pagersample.com",
new TypeSet("WORK", "MSG", "PAGER"));
verifier.verify(R.raw.v30_comma_separated, VCardConfig.VCARD_TYPE_V30_GENERIC_UTF8);
}
public void testPagerV30() throws IOException, VCardException {
ImportVerifier verifier = new ImportVerifier();
ImportVerifierElem elem = verifier.addImportVerifierElem();
elem.addExpected(StructuredName.CONTENT_ITEM_TYPE)
.put(StructuredName.FAMILY_NAME, "F")
.put(StructuredName.MIDDLE_NAME, "M")
.put(StructuredName.GIVEN_NAME, "G")
.put(StructuredName.DISPLAY_NAME, "G M F");
elem.addExpected(Phone.CONTENT_ITEM_TYPE)
.put(Phone.TYPE, Phone.TYPE_PAGER)
.put(Phone.NUMBER, "6101231234@pagersample.com");
verifier.verify(R.raw.v30_comma_separated, VCardConfig.VCARD_TYPE_V30_GENERIC_UTF8);
}
} }