Skip to content

Commit

Permalink
Use original create method when renaming address book account
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Nov 5, 2024
1 parent 154d1e6 commit 74b5764
Showing 1 changed file with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ open class LocalAddressBook @AssistedInject constructor(
val newAccountName = accountName(context, info)
if (addressBookAccount.name != newAccountName)
// rename, move contacts/groups and update [AndroidAddressBook.]account
renameAccount(newAccountName)
renameAccount(newAccountName, info)

// Update the account user data
accountManager.setAndVerifyUserData(addressBookAccount, USER_DATA_COLLECTION_ID, info.id.toString())
Expand Down Expand Up @@ -213,14 +213,18 @@ open class LocalAddressBook @AssistedInject constructor(
* @return whether the account was renamed successfully
*/
@VisibleForTesting
internal fun renameAccount(newName: String): Boolean {
internal fun renameAccount(newName: String, info: Collection): Boolean {
val oldAccount = addressBookAccount
logger.info("Renaming address book from \"${oldAccount.name}\" to \"$newName\"")

// create new account
val newAccount = Account(newName, oldAccount.type)
if (!SystemAccountUtils.createAccount(context, newAccount, Bundle()))
val localAddressBook = try {
create(context, provider!!, info, false, newName)
} catch (_: java.lang.IllegalStateException) {
logger.info("Failed to create new address book account \"$newName\"")
return false
}
val newAccount = localAddressBook.addressBookAccount

// move contacts and groups to new account
val batch = BatchOperation(provider!!)
Expand Down Expand Up @@ -423,21 +427,40 @@ open class LocalAddressBook @AssistedInject constructor(
const val USER_DATA_COLLECTION_ID = "collection_id"
const val USER_DATA_READ_ONLY = "read_only"

/**
* Contacts provider settings for [AndroidAddressBook] and the address book account.
*/
val contactsContractSettings = ContentValues(2).apply {
// SHOULD_SYNC is just a hint that this account's contacts (in our terms: the contacts of this local address book) are syncable.
put(ContactsContract.Settings.SHOULD_SYNC, 1)
// UNGROUPED_VISIBLE is required for making contacts work over Bluetooth (especially with some car systems).
put(ContactsContract.Settings.UNGROUPED_VISIBLE, 1)
}

// create/query/delete

/**
* Creates a new local address book.
*
* @param context app context to resolve string resources
* @param provider contacts provider client
* @param info collection where to take the name and settings from
* @param forceReadOnly `true`: set the address book to "force read-only"; `false`: determine read-only flag from [info]
* @param context app context to resolve string resources
* @param provider contacts provider client
* @param info collection where to take the name and settings from
* @param forceReadOnly `true`: set the address book to "force read-only";
* `false`: determine read-only flag from [info]
* @param addressBookAccountName name of the address book account
*/
fun create(context: Context, provider: ContentProviderClient, info: Collection, forceReadOnly: Boolean): LocalAddressBook {
fun create(
context: Context,
provider: ContentProviderClient,
info: Collection,
forceReadOnly: Boolean,
addressBookAccountName: String? = null
): LocalAddressBook {
val entryPoint = EntryPointAccessors.fromApplication<LocalAddressBookCompanionEntryPoint>(context)
val logger = entryPoint.logger()

val account = Account(accountName(context, info), context.getString(R.string.account_type_address_book))
val accountName = addressBookAccountName ?: accountName(context, info)
val account = Account(accountName, context.getString(R.string.account_type_address_book))
val userData = initialUserData(info.url.toString(), info.id.toString())
logger.log(Level.INFO, "Creating local address book $account", userData)
if (!SystemAccountUtils.createAccount(context, account, userData))
Expand All @@ -448,10 +471,14 @@ open class LocalAddressBook @AssistedInject constructor(
addressBook.updateSyncFrameworkSettings()

// initialize Contacts Provider Settings
val values = ContentValues(2)
values.put(ContactsContract.Settings.SHOULD_SYNC, 1)
values.put(ContactsContract.Settings.UNGROUPED_VISIBLE, 1)
addressBook.settings = values
addressBook.settings = ContentValues(2).apply {
// SHOULD_SYNC is just a hint that an account's contacts (the contacts of this local
// address book) are syncable.
put(ContactsContract.Settings.SHOULD_SYNC, 1)
// UNGROUPED_VISIBLE is required for making contacts work over Bluetooth (especially
// with some car systems).
put(ContactsContract.Settings.UNGROUPED_VISIBLE, 1)
}
addressBook.readOnly = forceReadOnly || !info.privWriteContent || info.forceReadOnly

return addressBook
Expand Down

0 comments on commit 74b5764

Please sign in to comment.