From 62411ace050a0a257041ef4604120decb1c7626b Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 3 Dec 2018 13:06:54 +0100 Subject: [PATCH] Fix assigning account number when Account is first saved (fixes #3787) --- app/core/src/main/java/com/fsck/k9/Account.java | 1 + .../com/fsck/k9/AccountPreferenceSerializer.kt | 4 ++-- .../src/main/java/com/fsck/k9/Preferences.java | 16 ++++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/Account.java b/app/core/src/main/java/com/fsck/k9/Account.java index 67672841d..7f61a12df 100644 --- a/app/core/src/main/java/com/fsck/k9/Account.java +++ b/app/core/src/main/java/com/fsck/k9/Account.java @@ -102,6 +102,7 @@ public class Account implements BaseAccount, StoreConfig { public static final SortType DEFAULT_SORT_TYPE = SortType.SORT_DATE; public static final boolean DEFAULT_SORT_ASCENDING = false; public static final long NO_OPENPGP_KEY = 0; + public static final int UNASSIGNED_ACCOUNT_NUMBER = -1; private DeletePolicy deletePolicy = DeletePolicy.NEVER; diff --git a/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt b/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt index e91f43ca0..9df268c0f 100644 --- a/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt +++ b/app/core/src/main/java/com/fsck/k9/AccountPreferenceSerializer.kt @@ -93,7 +93,7 @@ class AccountPreferenceSerializer( autoExpandFolder = storage.getString("$accountUuid.autoExpandFolderName", INBOX) - accountNumber = storage.getInt("$accountUuid.accountNumber", 0) + accountNumber = storage.getInt("$accountUuid.accountNumber", UNASSIGNED_ACCOUNT_NUMBER) chipColor = storage.getInt("$accountUuid.chipColor", FALLBACK_ACCOUNT_COLOR) @@ -503,7 +503,7 @@ class AccountPreferenceSerializer( idleRefreshMinutes = 24 isPushPollOnConnect = true displayCount = K9.DEFAULT_VISIBLE_LIMIT - accountNumber = -1 + accountNumber = UNASSIGNED_ACCOUNT_NUMBER isNotifyNewMail = true folderNotifyNewMailMode = FolderMode.ALL isNotifySync = true diff --git a/app/core/src/main/java/com/fsck/k9/Preferences.java b/app/core/src/main/java/com/fsck/k9/Preferences.java index c553853d8..536ee7eef 100644 --- a/app/core/src/main/java/com/fsck/k9/Preferences.java +++ b/app/core/src/main/java/com/fsck/k9/Preferences.java @@ -203,14 +203,18 @@ public class Preferences { public void saveAccount(Account account) { StorageEditor editor = storage.edit(); - if (!accounts.containsKey(account.getUuid())) { - int accountNumber = generateAccountNumber(); - account.setAccountNumber(accountNumber); + ensureAssignedAccountNumber(account); + processChangedValues(account); + accountPreferenceSerializer.save(storage, editor, account); + } + + private void ensureAssignedAccountNumber(Account account) { + if (account.getAccountNumber() != Account.UNASSIGNED_ACCOUNT_NUMBER) { + return; } - processChangedValues(account); - - accountPreferenceSerializer.save(storage, editor, account); + int accountNumber = generateAccountNumber(); + account.setAccountNumber(accountNumber); } private void processChangedValues(Account account) {