diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt b/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt index 365d1311..a7cf378e 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalAddressBook.kt @@ -45,8 +45,19 @@ class LocalAddressBook( val accountManager = AccountManager.get(context) val account = Account(accountName(mainAccount, info), App.addressBookAccountType) - if (!accountManager.addAccountExplicitly(account, null, initialUserData(mainAccount, info.uid!!))) - throw ContactsStorageException("Couldn't create address book account") + val userData = initialUserData(mainAccount, info.uid!!) + Logger.log.log(Level.INFO, "Creating local address book $account", userData) + if (!accountManager.addAccountExplicitly(account, null, userData)) + throw IllegalStateException("Couldn't create address book account") + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { + // Android < 7 seems to lose the initial user data sometimes, so set it a second time + // https://forums.bitfire.at/post/11644 + userData.keySet().forEach { key -> + accountManager.setUserData(account, key, userData.getString(key)) + } + } + val addressBook = LocalAddressBook(context, account, provider) ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true) @@ -119,7 +130,7 @@ class LocalAddressBook( if (name != null && type != null) return Account(name, type) else - throw IllegalStateException("Address book doesn't exist anymore") + throw IllegalStateException("No main account assigned to address book account") } } set(newMainAccount) {