Skip to content

Commit e4718ff

Browse files
committed
properly parse encoded names stretching to multiple lines too
1 parent 563c6c1 commit e4718ff

File tree

1 file changed

+28
-7
lines changed
  • app/src/main/kotlin/com/simplemobiletools/contacts/helpers

1 file changed

+28
-7
lines changed

app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class VcfImporter(val activity: SimpleActivity) {
3434
private var currentPhotoString = StringBuilder()
3535
private var currentPhotoCompressionFormat = Bitmap.CompressFormat.JPEG
3636

37+
private var isGettingName = false
38+
private var currentNameIsANSI = false
39+
private var currentNameString = StringBuilder()
40+
3741
private var contactsImported = 0
3842
private var contactsFailed = 0
3943

@@ -54,11 +58,15 @@ class VcfImporter(val activity: SimpleActivity) {
5458
isGettingPhoto = false
5559
}
5660
continue
61+
} else if (line.startsWith('\t') && isGettingName) {
62+
currentNameString.append(line.trimStart('\t'))
63+
isGettingName = false
64+
parseNames()
5765
}
5866

5967
when {
6068
line.toUpperCase() == BEGIN_VCARD -> resetValues()
61-
line.toUpperCase().startsWith(N) -> parseNames(line.substring(N.length))
69+
line.toUpperCase().startsWith(N) -> addNames(line.substring(N.length))
6270
line.toUpperCase().startsWith(TEL) -> addPhoneNumber(line.substring(TEL.length))
6371
line.toUpperCase().startsWith(EMAIL) -> addEmail(line.substring(EMAIL.length))
6472
line.toUpperCase().startsWith(BDAY) -> addBirthday(line.substring(BDAY.length))
@@ -81,14 +89,23 @@ class VcfImporter(val activity: SimpleActivity) {
8189
}
8290
}
8391

84-
private fun parseNames(names: String) {
92+
private fun addNames(names: String) {
8593
val parts = names.split(":")
86-
val isANSI = parts.first().toUpperCase().contains("QUOTED-PRINTABLE")
87-
val nameParts = parts[1].split(";")
88-
curSurname = if (isANSI) QuotedPrintable.decode(nameParts[0]) else nameParts[0]
89-
curFirstName = if (isANSI) QuotedPrintable.decode(nameParts[1]) else nameParts[1]
94+
currentNameIsANSI = parts.first().toUpperCase().contains("QUOTED-PRINTABLE")
95+
currentNameString.append(parts[1].trimEnd('='))
96+
if (!isGettingName && currentNameIsANSI && names.endsWith('=')) {
97+
isGettingName = true
98+
} else {
99+
parseNames()
100+
}
101+
}
102+
103+
private fun parseNames() {
104+
val nameParts = currentNameString.split(";")
105+
curSurname = if (currentNameIsANSI) QuotedPrintable.decode(nameParts[0]) else nameParts[0]
106+
curFirstName = if (currentNameIsANSI) QuotedPrintable.decode(nameParts[1]) else nameParts[1]
90107
if (nameParts.size > 2) {
91-
curMiddleName = if (isANSI) QuotedPrintable.decode(nameParts[2]) else nameParts[2]
108+
curMiddleName = if (currentNameIsANSI) QuotedPrintable.decode(nameParts[2]) else nameParts[2]
92109
}
93110
}
94111

@@ -200,5 +217,9 @@ class VcfImporter(val activity: SimpleActivity) {
200217
isGettingPhoto = false
201218
currentPhotoString = StringBuilder()
202219
currentPhotoCompressionFormat = Bitmap.CompressFormat.JPEG
220+
221+
isGettingName = false
222+
currentNameIsANSI = false
223+
currentNameString = StringBuilder()
203224
}
204225
}

0 commit comments

Comments
 (0)