Im trying to implement the Android Account Transfer API which basically lets you transfer accounts for your app from an old phone to a new phone so the user won't have to login again when they use your app on the new phone.

However I feel like the documentation is missing information. Account Transfer API

When I call AccountTransferClient.sendData(accountType, transferData) to transfer accounts to the new phone, it doesnt tell me where I get transferData from. I tried looking at the AccountTransferClient docs for more info but it doesn't say much either.

Thanks!

1

There are 1 best solutions below

0
mco On BEST ANSWER

Found the sample app https://github.com/android/identity-samples/tree/main/AccountTransferApi and apparently what they do is put account info in a JSON obj and then turn the JSON obj into a byte array (which is the transferData).

val jsonArray = JSONArray()
for (account in accounts) {
    val accountJsonObject = JSONObject()
    try {
        accountJsonObject.put(KEY_ACCOUNT_NAME, account.name)
        val password = accountManager.getPassword(account)
        accountJsonObject.put(KEY_ACCOUNT_PASSWORD, password)
    } catch (e: JSONException) {
        Log.e(TAG, "Error while creating bytes for transfer", e)
        return null
    }
    jsonArray.put(accountJsonObject)
}
val jsonObject = JSONObject()
try {
    jsonObject.put(KEY_ACCOUNT_ARRAY, jsonArray)
} catch (e: JSONException) {
    Log.e(TAG, "Error", e)
    return null
}
val transferData = jsonObject.toString().toByteArray(Charset.forName("UTF-8"))

Anyways highly recommend the sample app. The documentation is not clear enough!