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

Commit 39c990b8 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Increase EMAIL TYPE compatibility; library update

- EMAIL: drop TYPE=internet and TYPE=x400 because Android only knows Internet email addresses
- EMAIL: treat TYPE=other (which is not specified in vCard RFCs) as "other" email address for better compatibility
- upgrade Apache Commons text library
parent 17b47491
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -72,7 +72,7 @@ android {
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"

    implementation 'org.apache.commons:commons-text:1.7'
    implementation 'org.apache.commons:commons-text:1.8'
    implementation 'commons-io:commons-io:2.6'

    // ez-vcard to parse/generate VCards
+45 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import at.bitfire.vcard4android.impl.TestAddressBook
import ezvcard.VCardVersion
import ezvcard.parameter.EmailType
import ezvcard.property.Address
import ezvcard.property.Birthday
import ezvcard.property.Email
@@ -202,6 +203,50 @@ class AndroidContactTest {
        }
    }

    @Test
    @SmallTest
    fun testEmailTypes() {
        val vCard = "BEGIN:VCARD\r\n" +
                "VERSION:4.0\r\n" +
                "FN:Test\r\n" +
                "EMAIL;TYPE=internet;TYPE=work:work@example.com\r\n" +
                "EMAIL;TYPE=home:home@example.com\r\n" +
                "EMAIL;TYPE=internet,pref:other1@example.com\r\n" +
                "EMAIL;TYPE=x400,other:other2@example.com\r\n" +
                "EMAIL;TYPE=x-mobile:mobile@example.com\r\n" +
                "END:VCARD\r\n"
        val contacts = Contact.fromReader(StringReader(vCard), null)

        val dbContact = AndroidContact(addressBook, contacts.first(), null, null)
        dbContact.add()

        val dbContact2 = addressBook.findContactByID(dbContact.id!!)
        try {
            val contact2 = dbContact2.contact!!
            assertEquals("work@example.com", contact2.emails[0].property.value)
            assertArrayEquals(arrayOf(EmailType.WORK), contact2.emails[0].property.types.toTypedArray())
            assertNull(contact2.emails[0].property.pref)

            assertEquals("home@example.com", contact2.emails[1].property.value)
            assertArrayEquals(arrayOf(EmailType.HOME), contact2.emails[1].property.types.toTypedArray())
            assertNull(contact2.emails[1].property.pref)

            assertEquals("other1@example.com", contact2.emails[2].property.value)
            assertTrue(contact2.emails[2].property.types.isEmpty())
            assertNotEquals(0, contact2.emails[2].property.pref)

            assertEquals("other2@example.com", contact2.emails[3].property.value)
            assertTrue(contact2.emails[3].property.types.isEmpty())
            assertNull(contact2.emails[3].property.pref)

            assertEquals("mobile@example.com", contact2.emails[4].property.value)
            assertArrayEquals(arrayOf(Contact.EMAIL_TYPE_MOBILE), contact2.emails[4].property.types.toTypedArray())
            assertNull(contact2.emails[4].property.pref)
        } finally {
            dbContact2.delete()
        }
    }


    @Test
    fun testLabelToXName() {
+4 −1
Original line number Diff line number Diff line
@@ -768,7 +768,9 @@ open class AndroidContact(
    protected open fun insertEmail(batch: BatchOperation, labeledEmail: LabeledProperty<ezvcard.property.Email>) {
        val email = labeledEmail.property

        // drop TYPE=internet and TYPE=x400 because Android only knows Internet email addresses
        val types = email.types
        types.removeAll(arrayOf(EmailType.INTERNET, EmailType.X400))

        // preferred email address?
        var pref: Int? = null
@@ -794,8 +796,9 @@ open class AndroidContact(
                    EmailType.HOME -> typeCode = Email.TYPE_HOME
                    EmailType.WORK -> typeCode = Email.TYPE_WORK
                    Contact.EMAIL_TYPE_MOBILE -> typeCode = Email.TYPE_MOBILE
                    Contact.EMAIL_TYPE_OTHER -> typeCode = Email.TYPE_OTHER
                }
            if (typeCode == 0) {
            if (typeCode == 0) {    // we still didn't find a known type
                if (email.types.isEmpty())
                    typeCode = Email.TYPE_OTHER
                else {
+3 −0
Original line number Diff line number Diff line
@@ -91,7 +91,10 @@ class Contact {
        val PHONE_TYPE_ASSISTANT = TelephoneType.get("X-assistant")!!
        val PHONE_TYPE_MMS = TelephoneType.get("x-mms")!!

        /** Custom email type to denote "mobile" email addresses. */
        val EMAIL_TYPE_MOBILE = EmailType.get("x-mobile")!!
        /** Sometimes used to denote an "other" email address. Only for compatibility – don't use it yourself! */
        val EMAIL_TYPE_OTHER = EmailType.get("other")!!

        const val NICKNAME_TYPE_MAIDEN_NAME = "x-maiden-name"
        const val NICKNAME_TYPE_SHORT_NAME = "x-short-name"