Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0cda5911 authored by Daisuke Miyakawa's avatar Daisuke Miyakawa
Browse files

Make vCard composer use ContentValues object with non-empty name unless the...

Make vCard composer use ContentValues object with non-empty name unless the object is not marked as IS_SUPER_PRIMARY.

Previous change selected the first ContactValues object even when its name fields are all empty.
This time, vCard composer checks the name fields and skip the object withouth valid name.
One exception is the object with IS_SUPER_PRIMARY flag. If IS_SUPER_PRIMARY flag is set, the object will be
selected even when the object does not have valid name.

Add a unit test for this fix.

Internal issue number: 2252304
parent 6780d8c0
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -676,6 +676,18 @@ public class VCardComposer {
        }
    }

    private boolean containsNonEmptyName(ContentValues contentValues) {
        final String familyName = contentValues.getAsString(StructuredName.FAMILY_NAME);
        final String middleName = contentValues.getAsString(StructuredName.MIDDLE_NAME);
        final String givenName = contentValues.getAsString(StructuredName.GIVEN_NAME);
        final String prefix = contentValues.getAsString(StructuredName.PREFIX);
        final String suffix = contentValues.getAsString(StructuredName.SUFFIX);
        final String displayName = contentValues.getAsString(StructuredName.DISPLAY_NAME);
        return !(TextUtils.isEmpty(familyName) && TextUtils.isEmpty(middleName) &&
                TextUtils.isEmpty(givenName) && TextUtils.isEmpty(prefix) &&
                TextUtils.isEmpty(suffix) && TextUtils.isEmpty(displayName));
    }

    private void appendStructuredNamesInternal(final StringBuilder builder,
            final List<ContentValues> contentValuesList) {
        // For safety, we'll emit just one value around StructuredName, as external importers
@@ -695,12 +707,14 @@ public class VCardComposer {
            } else if (primaryContentValues == null) {
                // We choose the first "primary" ContentValues
                // if "super primary" ContentValues does not exist.
                Integer primary = contentValues.getAsInteger(StructuredName.IS_PRIMARY);
                if (primary != null && primary > 0) {
                Integer isPrimary = contentValues.getAsInteger(StructuredName.IS_PRIMARY);
                if (isPrimary != null && isPrimary > 0 &&
                        containsNonEmptyName(contentValues)) {
                    primaryContentValues = contentValues;
                    // Do not break, since there may be ContentValues with "super primary"
                    // afterword.
                } else if (subprimaryContentValues == null) {
                } else if (subprimaryContentValues == null &&
                        containsNonEmptyName(contentValues)) {
                    subprimaryContentValues = contentValues;
                }
            }
+30 −0
Original line number Diff line number Diff line
@@ -903,4 +903,34 @@ public class VCardExporterTests extends VCardTestsBase {
                .addNodeWithoutOrder("TEL", "777-888-9999", new TypeSet("HOME"));
        verifier.verify();
    }

    private void testPickUpNonEmptyContentValuesCommon(int version) {
        ExportTestResolver resolver = new ExportTestResolver();
        ContactEntry entry = resolver.buildContactEntry();
        entry.buildData(StructuredName.CONTENT_ITEM_TYPE)
                .put(StructuredName.IS_PRIMARY, 1);  // Empty name. Should be ignored.
        entry.buildData(StructuredName.CONTENT_ITEM_TYPE)
                .put(StructuredName.FAMILY_NAME, "family1");  // Not primary. Should be ignored.
        entry.buildData(StructuredName.CONTENT_ITEM_TYPE)
                .put(StructuredName.IS_PRIMARY, 1)
                .put(StructuredName.FAMILY_NAME, "family2");  // This entry is what we want.
        entry.buildData(StructuredName.CONTENT_ITEM_TYPE)
                .put(StructuredName.IS_PRIMARY, 1)
                .put(StructuredName.FAMILY_NAME, "family3");
        entry.buildData(StructuredName.CONTENT_ITEM_TYPE)
                .put(StructuredName.FAMILY_NAME, "family4");
        VCardVerifier verifier = new VCardVerifier(resolver, version);
        verifier.addPropertyNodesVerifierElem()
                .addNodeWithoutOrder("N", Arrays.asList("family2", "", "", "", ""))
                .addNodeWithoutOrder("FN", "family2");
        verifier.verify();
    }

    public void testPickUpNonEmptyContentValuesV21() {
        testPickUpNonEmptyContentValuesCommon(V21);
    }

    public void testPickUpNonEmptyContentValuesV30() {
        testPickUpNonEmptyContentValuesCommon(V30);
    }
}