use of ezvcard.property.StructuredName in project android by nextcloud.
the class ContactOperations method convertName.
private void convertName(List<NonEmptyContentValues> contentValues, VCard vcard) {
NonEmptyContentValues values = new NonEmptyContentValues(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
String firstName = null, lastName = null, namePrefix = null, nameSuffix = null;
StructuredName n = vcard.getStructuredName();
if (n != null) {
firstName = n.getGiven();
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
lastName = n.getFamily();
values.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
List<String> prefixes = n.getPrefixes();
if (!prefixes.isEmpty()) {
namePrefix = prefixes.get(0);
values.put(ContactsContract.CommonDataKinds.StructuredName.PREFIX, namePrefix);
}
List<String> suffixes = n.getSuffixes();
if (!suffixes.isEmpty()) {
nameSuffix = suffixes.get(0);
values.put(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, nameSuffix);
}
}
FormattedName fn = vcard.getFormattedName();
String formattedName = (fn == null) ? null : fn.getValue();
String displayName;
if (isEmpty(formattedName)) {
StringBuilder sb = new StringBuilder();
if (!isEmpty(namePrefix)) {
sb.append(namePrefix).append(' ');
}
if (!isEmpty(firstName)) {
sb.append(firstName).append(' ');
}
if (!isEmpty(lastName)) {
sb.append(lastName).append(' ');
}
if (!isEmpty(nameSuffix)) {
if (sb.length() > 0) {
// delete space character
sb.deleteCharAt(sb.length() - 1);
sb.append(", ");
}
sb.append(nameSuffix);
}
displayName = sb.toString().trim();
} else {
displayName = formattedName;
}
values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
RawProperty xPhoneticFirstName = vcard.getExtendedProperty("X-PHONETIC-FIRST-NAME");
String firstPhoneticName = (xPhoneticFirstName == null) ? null : xPhoneticFirstName.getValue();
values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_GIVEN_NAME, firstPhoneticName);
RawProperty xPhoneticLastName = vcard.getExtendedProperty("X-PHONETIC-LAST-NAME");
String lastPhoneticName = (xPhoneticLastName == null) ? null : xPhoneticLastName.getValue();
values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME, lastPhoneticName);
contentValues.add(values);
}
use of ezvcard.property.StructuredName in project qksms by moezbhatti.
the class ContactOperations method convertName.
private void convertName(List<NonEmptyContentValues> contentValues, VCard vcard) {
NonEmptyContentValues values = new NonEmptyContentValues(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
String firstName = null, lastName = null, namePrefix = null, nameSuffix = null;
StructuredName n = vcard.getStructuredName();
if (n != null) {
firstName = n.getGiven();
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
lastName = n.getFamily();
values.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
List<String> prefixes = n.getPrefixes();
if (!prefixes.isEmpty()) {
namePrefix = prefixes.get(0);
values.put(ContactsContract.CommonDataKinds.StructuredName.PREFIX, namePrefix);
}
List<String> suffixes = n.getSuffixes();
if (!suffixes.isEmpty()) {
nameSuffix = suffixes.get(0);
values.put(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, nameSuffix);
}
}
FormattedName fn = vcard.getFormattedName();
String formattedName = (fn == null) ? null : fn.getValue();
String displayName;
if (isEmpty(formattedName)) {
StringBuilder sb = new StringBuilder();
if (!isEmpty(namePrefix)) {
sb.append(namePrefix).append(' ');
}
if (!isEmpty(firstName)) {
sb.append(firstName).append(' ');
}
if (!isEmpty(lastName)) {
sb.append(lastName).append(' ');
}
if (!isEmpty(nameSuffix)) {
if (sb.length() > 0) {
// delete space character
sb.deleteCharAt(sb.length() - 1);
sb.append(", ");
}
sb.append(nameSuffix);
}
displayName = sb.toString().trim();
} else {
displayName = formattedName;
}
values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
RawProperty xPhoneticFirstName = vcard.getExtendedProperty("X-PHONETIC-FIRST-NAME");
String firstPhoneticName = (xPhoneticFirstName == null) ? null : xPhoneticFirstName.getValue();
values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_GIVEN_NAME, firstPhoneticName);
RawProperty xPhoneticLastName = vcard.getExtendedProperty("X-PHONETIC-LAST-NAME");
String lastPhoneticName = (xPhoneticLastName == null) ? null : xPhoneticLastName.getValue();
values.put(ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME, lastPhoneticName);
contentValues.add(values);
}
use of ezvcard.property.StructuredName in project data-transfer-project by google.
the class GoogleContactsImportConversionTest method makeStructuredName.
private static StructuredName makeStructuredName(String givenName, String familyName, @Nullable String sourceType) {
StructuredName structuredName = new StructuredName();
structuredName.setGiven(givenName);
structuredName.setFamily(familyName);
if (sourceType != null) {
structuredName.setParameter(SOURCE_PARAM_NAME_TYPE, sourceType);
}
return structuredName;
}
use of ezvcard.property.StructuredName in project data-transfer-project by google.
the class GoogleContactsImportConversionTest method testConversionToGoogleNames.
@Test
public void testConversionToGoogleNames() {
// Set up vCard with a primary name and one secondary name
String primaryGivenName = "Mark";
String primaryFamilyName = "Twain";
String primarySourceType = "CONTACT";
StructuredName primaryName = makeStructuredName(primaryGivenName, primaryFamilyName, primarySourceType);
String altGivenName = "Samuel";
String altFamilyName = "Clemens";
String altSourceType = "PROFILE";
StructuredName altName = makeStructuredName(altGivenName, altFamilyName, altSourceType);
VCard vCard = new VCard();
vCard.addProperty(primaryName);
vCard.addPropertyAlt(StructuredName.class, Collections.singleton(altName));
// Run test
Person person = GoogleContactsImporter.convert(vCard);
// Check results
// Correct number of names
assertThat(person.getNames().size()).isEqualTo(1);
// Check primary names
List<Name> actualPrimaryNames = person.getNames().stream().filter(a -> a.getMetadata().getPrimary()).collect(Collectors.toList());
List<Pair<String, String>> actualPrimaryNameValues = actualPrimaryNames.stream().map(GoogleContactsImportConversionTest::getGivenAndFamilyValues).collect(Collectors.toList());
assertThat(actualPrimaryNameValues).containsExactly(Pair.of(primaryGivenName, primaryFamilyName));
List<String> actualPrimaryNameSourceValues = actualPrimaryNames.stream().map(a -> a.getMetadata().getSource().getType()).collect(Collectors.toList());
assertThat(actualPrimaryNameSourceValues).containsExactly(primarySourceType);
// Check secondary names - there shouldn't be any
List<Name> actualSecondaryNames = person.getNames().stream().filter(a -> !a.getMetadata().getPrimary()).collect(Collectors.toList());
assertThat(actualSecondaryNames).isEmpty();
}
use of ezvcard.property.StructuredName in project data-transfer-project by google.
the class GoogleContactsImporterTest method importFirstResources.
@Test
public void importFirstResources() throws IOException {
// Set up: small number of VCards to be imported
int numberOfVCards = 5;
List<VCard> vCardList = new LinkedList<>();
for (int i = 0; i < numberOfVCards; i++) {
StructuredName structuredName = new StructuredName();
structuredName.setFamily("Family" + i);
structuredName.setParameter(SOURCE_PARAM_NAME_TYPE, CONTACT_SOURCE_TYPE);
VCard vCard = new VCard();
vCard.setStructuredName(structuredName);
vCardList.add(vCard);
}
String vCardString = GoogleContactsExporter.makeVCardString(vCardList);
ContactsModelWrapper wrapper = new ContactsModelWrapper(vCardString);
// Run test
contactsService.importItem(UUID.randomUUID(), null, wrapper);
// Check that the right methods were called
verify(people, times(numberOfVCards)).createContact(any(Person.class));
verify(createContact, times(numberOfVCards)).execute();
}
Aggregations