Skip to content

Commit

Permalink
[contacts] Handle BLOB data
Browse files Browse the repository at this point in the history
Contacts data can include BLOBs (binary data), which must be handled
differently from ordinary string data (or other data convertible to
string form), and base64 encoded / decoded for use in JSON.

Addresses: #50
  • Loading branch information
tmo1 committed Aug 31, 2022
1 parent 8e06f6b commit a1b81b4
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions app/src/main/java/com/github/tmo1/sms_ie/ImportExportContacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ package com.github.tmo1.sms_ie

import android.content.ContentProviderOperation
import android.content.Context
import android.database.Cursor.FIELD_TYPE_BLOB
import android.net.Uri
import android.provider.BaseColumns
import android.provider.ContactsContract
import android.util.Base64
import android.util.JsonReader
import android.util.JsonWriter
import android.util.Log
Expand Down Expand Up @@ -132,9 +134,20 @@ private suspend fun contactsToJSON(
do {
jsonWriter.beginObject()
data.columnNames.forEachIndexed { i, columnName ->
val value = data.getString(i)
if (value != null) jsonWriter.name(columnName)
.value(value)
if (data.getType(i) != FIELD_TYPE_BLOB) {
val value = data.getString(i)
if (value != null) jsonWriter.name(columnName)
.value(value)
} else {
val value = data.getBlob(i)
if (value != null) jsonWriter.name("${columnName}__base64__")
.value(
Base64.encodeToString(
value,
Base64.NO_WRAP
)
)
}
}
jsonWriter.endObject()
} while (data.moveToNext())
Expand Down Expand Up @@ -221,8 +234,23 @@ suspend fun importContacts(
while (jsonReader.hasNext()) {
name = jsonReader.nextName()
val dataValue = jsonReader.nextString()
var base64 = false
if (name.length > 10 && name.substring(name.length - 10) == "__base64__") {
base64 = true
name = name.substring(0, name.length - 10)
}
if (name in contactDataFields) {
op.withValue(name, dataValue)
if (base64) {
op.withValue(
name,
Base64.decode(
dataValue,
Base64.NO_WRAP
)
)
} else {
op.withValue(name, dataValue)
}
}
}
op.withYieldAllowed(true)
Expand Down

0 comments on commit a1b81b4

Please sign in to comment.