Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RelationBuilder.kt « contactrow « vcard4android « bitfire « at « java « main « src - github.com/bitfireAT/vcard4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 87d9dbda0424f9824d5da3e5620400a015f56359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/***************************************************************************************************
 * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
 **************************************************************************************************/

package at.bitfire.vcard4android.contactrow

import android.net.Uri
import android.provider.ContactsContract.CommonDataKinds.Relation
import at.bitfire.vcard4android.BatchOperation
import at.bitfire.vcard4android.Contact
import at.bitfire.vcard4android.property.CustomType
import ezvcard.parameter.RelatedType
import org.apache.commons.lang3.StringUtils
import org.apache.commons.text.WordUtils
import java.util.*

class RelationBuilder(dataRowUri: Uri, rawContactId: Long?, contact: Contact)
    : DataRowBuilder(Factory.mimeType(), dataRowUri, rawContactId, contact) {

    override fun build(): List<BatchOperation.CpoBuilder> {
        val result = LinkedList<BatchOperation.CpoBuilder>()
        for (related in contact.relations) {
            val name = StringUtils.trimToNull(related.text) ?: StringUtils.trimToNull(related.uri)
            if (name.isNullOrBlank())
                continue

            val typeCode = when {
                // specific Android types (not defined in RFC 6350)
                related.types.contains(CustomType.Related.ASSISTANT) -> Relation.TYPE_ASSISTANT
                related.types.contains(CustomType.Related.BROTHER) -> Relation.TYPE_BROTHER
                related.types.contains(CustomType.Related.DOMESTIC_PARTNER) -> Relation.TYPE_DOMESTIC_PARTNER
                related.types.contains(CustomType.Related.FATHER) -> Relation.TYPE_FATHER
                related.types.contains(CustomType.Related.MANAGER) -> Relation.TYPE_MANAGER
                related.types.contains(CustomType.Related.MOTHER) -> Relation.TYPE_MOTHER
                related.types.contains(CustomType.Related.PARTNER) -> Relation.TYPE_PARTNER
                related.types.contains(CustomType.Related.REFERRED_BY) -> Relation.TYPE_REFERRED_BY
                related.types.contains(CustomType.Related.SISTER) -> Relation.TYPE_SISTER

                // standard types (defined in RFC 6350) supported by Android
                related.types.contains(RelatedType.CHILD) -> Relation.TYPE_CHILD
                related.types.contains(RelatedType.FRIEND) -> Relation.TYPE_FRIEND
                related.types.contains(RelatedType.KIN) -> Relation.TYPE_RELATIVE
                related.types.contains(RelatedType.PARENT) -> Relation.TYPE_PARENT
                related.types.contains(RelatedType.SPOUSE) -> Relation.TYPE_SPOUSE

                // other standard types are set as TYPE_CUSTOM
                else -> Relation.TYPE_CUSTOM
            }

            val builder = newDataRow()
                .withValue(Relation.NAME, name)
                .withValue(Relation.TYPE, typeCode)

            if (typeCode == Relation.TYPE_CUSTOM) {
                if (related.types.isEmpty())
                    related.types += CustomType.Related.OTHER

                val types = related.types.map { type -> WordUtils.capitalize(type.value) }
                val typesStr = types.joinToString(", ")
                builder.withValue(Relation.LABEL, typesStr)
            }

            result += builder
        }
        return result
    }


    object Factory: DataRowBuilder.Factory<RelationBuilder> {
        override fun mimeType() = Relation.CONTENT_ITEM_TYPE
        override fun newInstance(dataRowUri: Uri, rawContactId: Long?, contact: Contact) =
            RelationBuilder(dataRowUri, rawContactId, contact)
    }

}