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

ContactReader.kt « vcard4android « bitfire « at « java « main « src - github.com/bitfireAT/vcard4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d41c4dd32d4944360bcdc93bea4b3450fac67428 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***************************************************************************************************
 * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
 **************************************************************************************************/

package at.bitfire.vcard4android

import at.bitfire.vcard4android.property.*
import at.bitfire.vcard4android.property.CustomScribes.registerCustomScribes
import ezvcard.Ezvcard
import ezvcard.VCard
import ezvcard.parameter.RelatedType
import ezvcard.property.*
import ezvcard.util.PartialDate
import org.apache.commons.lang3.StringUtils
import java.net.URI
import java.net.URISyntaxException
import java.util.*
import java.util.logging.Level

/**
 * Responsible for converting a specific vCard with a specific version to
 * the version-independent data class [Contact].
 *
 * Attention: This class works with the original vCard and modifies it!
 */
class ContactReader internal constructor(val vCard: VCard, val downloader: Contact.Downloader? = null) {

    companion object {

        /**
         * Maximum size for binary data like LOGO and SOUND data URIs.
         *
         * Data larger than this size has unfortunately to be dropped because of Android's IPC limits
         * (max 1 MB per transaction, for all rows together).
         */
        const val MAX_BINARY_DATA_SIZE = 25*1024

        fun fromVCard(vCard: VCard, downloader: Contact.Downloader? = null) =
            ContactReader(vCard, downloader).toContact()

        fun checkPartialDate(prop: DateOrTimeProperty) {
            val date = prop.date
            if (prop.partialDate == null && date != null) {
                prop.getParameter(Contact.DATE_PARAMETER_OMIT_YEAR)?.let { omitYearStr ->
                    val cal = GregorianCalendar.getInstance()
                    cal.time = date
                    if (cal.get(GregorianCalendar.YEAR).toString() == omitYearStr) {
                        val partial = PartialDate.builder()
                            .date(cal.get(GregorianCalendar.DAY_OF_MONTH))
                            .month(cal.get(GregorianCalendar.MONTH) + 1)
                            .build()
                        prop.partialDate = partial
                    }

                    // X-APPLE-OMIT-YEAR not required anymore because we're now working with PartialDate
                    prop.removeParameter(Contact.DATE_PARAMETER_OMIT_YEAR)
                }
            }
        }

        fun uriToUid(uriString: String?): String? {
            if (uriString == null)
                return null

            val uid = try {
                val uri = URI(uriString)
                when {
                    uri.scheme == null ->
                        uri.schemeSpecificPart
                    uri.scheme.equals("urn", true) && uri.schemeSpecificPart.startsWith("uuid:", true) ->
                        uri.schemeSpecificPart.substring(5)
                    else ->
                        uriString
                }
            } catch(e: URISyntaxException) {
                Constants.log.warning("Invalid URI for UID: $uriString")
                uriString
            }

            return StringUtils.trimToNull(uid)
        }

    }


    /**
     * Converts the vCard to a [Contact].
     */
    private fun toContact(): Contact {
        val c = Contact()

        // process standard properties; after processing, only unknown properties will remain
        for (prop in vCard.properties) {
            var remove = true       // assume that this property will be processed and thus shall be removed

            when (prop) {
                is Uid ->
                    c.uid = uriToUid(prop.value)

                is Kind, is XAddressBookServerKind -> {
                    val kindProp = prop as Kind
                    c.group = kindProp.isGroup
                }
                is Member, is XAddressBookServerMember -> {
                    val uriProp = prop as Member
                    uriToUid(uriProp.uri)?.let { c.members += it }
                }

                is FormattedName ->
                    c.displayName = StringUtils.trimToNull(prop.value)
                is StructuredName -> {
                    c.prefix = StringUtils.trimToNull(prop.prefixes.joinToString(" "))
                    c.givenName = StringUtils.trimToNull(prop.given)
                    c.middleName = StringUtils.trimToNull(prop.additionalNames.joinToString(" "))
                    c.familyName = StringUtils.trimToNull(prop.family)
                    c.suffix = StringUtils.trimToNull(prop.suffixes.joinToString(" "))
                }
                is XPhoneticFirstName ->
                    c.phoneticGivenName = StringUtils.trimToNull(prop.value)
                is XPhoneticMiddleName ->
                    c.phoneticMiddleName = StringUtils.trimToNull(prop.value)
                is XPhoneticLastName ->
                    c.phoneticFamilyName = StringUtils.trimToNull(prop.value)
                is Nickname ->
                    c.nickName = LabeledProperty(prop, findAndRemoveLabel(prop.group))

                is Categories ->
                    c.categories.addAll(prop.values)

                is Organization ->
                    c.organization = prop
                is Title ->
                    c.jobTitle = StringUtils.trimToNull(prop.value)
                is Role ->
                    c.jobDescription = StringUtils.trimToNull(prop.value)

                is Telephone ->
                    if (!prop.text.isNullOrBlank())
                        c.phoneNumbers += LabeledProperty(prop, findAndRemoveLabel(prop.group))
                is Email ->
                    if (!prop.value.isNullOrBlank())
                        c.emails += LabeledProperty(prop, findAndRemoveLabel(prop.group))
                is Impp ->
                    c.impps += LabeledProperty(prop, findAndRemoveLabel(prop.group))
                is XSip ->
                    // special case: treat  X-SIP:address  as  IMPP:sip:address
                    c.impps += LabeledProperty(Impp.sip(prop.value))
                is Url ->
                    c.urls += LabeledProperty(prop, findAndRemoveLabel(prop.group))

                is Address ->
                    c.addresses += LabeledProperty(prop, findAndRemoveLabel(prop.group))
                is Label -> { /* drop vCard3 formatted address because it can't be associated to a specific address */ }

                is Anniversary -> {
                    checkPartialDate(prop)
                    c.anniversary = prop
                }
                is Birthday -> {
                    checkPartialDate(prop)
                    c.birthDay = prop
                }
                is XAbDate -> {
                    checkPartialDate(prop)
                    var label = findAndRemoveLabel(prop.group)
                    if (label == XAbLabel.APPLE_OTHER)              // drop Apple "Other" label
                        label = null

                    if (label == XAbLabel.APPLE_ANNIVERSARY) {      // convert custom date with Apple "Anniversary" label to real anniversary
                        if (prop.date != null)
                            c.anniversary = Anniversary(prop.date)
                        else if (prop.partialDate != null)
                            c.anniversary = Anniversary(prop.partialDate)
                    } else
                        c.customDates += LabeledProperty(prop, label)
                }

                is Related ->
                    if (!prop.uri.isNullOrBlank() || !prop.text.isNullOrBlank())
                        c.relations += prop
                is XAbRelatedNames -> {
                    val relation = Related()
                    relation.text = prop.value

                    val labelStr = findAndRemoveLabel(prop.group)
                    when (labelStr) {
                        XAbRelatedNames.APPLE_ASSISTANT -> {
                            relation.types.add(CustomType.Related.ASSISTANT)
                            relation.types.add(RelatedType.CO_WORKER)
                        }
                        XAbRelatedNames.APPLE_BROTHER -> {
                            relation.types.add(CustomType.Related.BROTHER)
                            relation.types.add(RelatedType.SIBLING)
                        }
                        XAbRelatedNames.APPLE_CHILD ->
                            relation.types.add(RelatedType.CHILD)
                        XAbRelatedNames.APPLE_FATHER -> {
                            relation.types.add(CustomType.Related.FATHER)
                            relation.types.add(RelatedType.PARENT)
                        }
                        XAbRelatedNames.APPLE_FRIEND ->
                            relation.types.add(RelatedType.FRIEND)
                        XAbRelatedNames.APPLE_MANAGER -> {
                            relation.types.add(CustomType.Related.MANAGER)
                            relation.types.add(RelatedType.CO_WORKER)
                        }
                        XAbRelatedNames.APPLE_MOTHER -> {
                            relation.types.add(CustomType.Related.MOTHER)
                            relation.types.add(RelatedType.PARENT)
                        }
                        XAbRelatedNames.APPLE_SISTER -> {
                            relation.types.add(CustomType.Related.SISTER)
                            relation.types.add(RelatedType.SIBLING)
                        }
                        XAbRelatedNames.APPLE_PARENT ->
                            relation.types.add(RelatedType.PARENT)
                        XAbRelatedNames.APPLE_PARTNER ->
                            relation.types.add(CustomType.Related.PARTNER)
                        XAbRelatedNames.APPLE_SPOUSE ->
                            relation.types.add(RelatedType.SPOUSE)

                        is String /* label != null */ -> {
                            for (label in labelStr.split(','))
                                relation.types.add(RelatedType.get(label.trim().lowercase()))
                        }
                    }
                    c.relations += relation
                }

                is Note -> {
                    StringUtils.trimToNull(prop.value)?.let { note ->
                        if (c.note == null)
                            c.note = note
                        else
                            c.note += "\n\n\n" + note
                    }
                }

                is Photo ->
                    c.photo = c.photo ?: getPhotoBytes(prop)

                // drop large binary properties because of potential OutOfMemory / TransactionTooLarge exceptions
                is Logo -> {
                    remove = prop.data != null && prop.data.size > MAX_BINARY_DATA_SIZE
                }
                is Sound -> {
                    remove = prop.data != null && prop.data.size > MAX_BINARY_DATA_SIZE
                }

                // remove properties that don't apply anymore
                is ProductId,
                is Revision,
                is SortString,      // not counterpart in Android; remove it because FN/N may be changed, which would cause inconsistency
                is Source -> {      // when we upload a modified contact, the SOURCE would maybe point to a different version, which would cause inconsistency
                    /* remove = true */
                }

                else ->     // unknown property, keep it in vCard in order to retain it
                    remove = false
            }

            if (remove)
                vCard.removeProperty(prop)
        }

        if (c.uid == null) {
            Constants.log.warning("Received vCard without UID, generating new one")
            c.uid = UUID.randomUUID().toString()
        }

        // remove properties which
        // - couldn't be parsed (and thus are treated as extended/unknown properties), and
        // - must not occur more than once
        arrayOf("ANNIVERSARY", "BDAY", "KIND", "FN", "N", "PRODID", "REV", "UID").forEach {
            vCard.removeExtendedProperty(it)
        }

        if (vCard.properties.isNotEmpty() || vCard.extendedProperties.isNotEmpty())
            try {
                c.unknownProperties = Ezvcard
                        .write(vCard)
                        .prodId(false)
                        .version(vCard.version)
                        .registerCustomScribes()
                        .go()
            } catch(e: Exception) {
                Constants.log.log(Level.WARNING, "Couldn't serialize unknown properties, dropping them", e)
            }

        return c
    }


    // helpers

    fun findAndRemoveLabel(group: String?): String? {
        if (group == null)
            return null

        for (label in vCard.getProperties(XAbLabel::class.java)) {
            if (label.group.equals(group, true)) {
                vCard.removeProperty(label)
                return StringUtils.trimToNull(label.value)
            }
        }

        return null
    }

    fun getPhotoBytes(photo: Photo): ByteArray? {
        if (photo.data != null)
            return photo.data

        val url = photo.url
        if (photo.url != null && downloader != null) {
            Constants.log.info("Downloading photo from $url")
            return downloader.download(url, "image/*")
        }

        return null
    }

}