From 32ee18240b8751ae6da538a161d983acc60505b8 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli <greyson@signal.org> Date: Fri, 22 Apr 2022 14:30:37 -0400 Subject: [PATCH] Fix crash that occurs if we don't have permission to add an account. --- .../contacts/SystemContactsRepository.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/contacts/lib/src/main/java/org/signal/contacts/SystemContactsRepository.kt b/contacts/lib/src/main/java/org/signal/contacts/SystemContactsRepository.kt index 0a64c8e349..94af14c419 100644 --- a/contacts/lib/src/main/java/org/signal/contacts/SystemContactsRepository.kt +++ b/contacts/lib/src/main/java/org/signal/contacts/SystemContactsRepository.kt @@ -87,15 +87,19 @@ object SystemContactsRepository { var account: Account? = if (accounts.isNotEmpty()) accounts[0] else null if (account == null) { - Log.i(TAG, "Attempting to create a new account...") - val newAccount = Account(accountDisplayName, applicationId) - - if (accountManager.addAccountExplicitly(newAccount, null, null)) { - Log.i(TAG, "Successfully created a new account.") - ContentResolver.setIsSyncable(newAccount, ContactsContract.AUTHORITY, 1) - account = newAccount - } else { - Log.w(TAG, "Failed to create a new account!") + try { + Log.i(TAG, "Attempting to create a new account...") + val newAccount = Account(accountDisplayName, applicationId) + + if (accountManager.addAccountExplicitly(newAccount, null, null)) { + Log.i(TAG, "Successfully created a new account.") + ContentResolver.setIsSyncable(newAccount, ContactsContract.AUTHORITY, 1) + account = newAccount + } else { + Log.w(TAG, "Failed to create a new account!") + } + } catch (e: SecurityException) { + Log.w(TAG, "Failed to add an account.", e) } } -- GitLab