diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java index 82876d5f9466c9e5d957b66968b982c01d4717e2..1cc0eb0256e3bc263be072b91872ba0f3878c9e3 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -21,6 +21,7 @@ import java.util.concurrent.Future; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; +import android.accounts.AccountManager; import android.content.Context; import android.os.Process; import android.os.SystemClock; @@ -28,6 +29,7 @@ import android.os.SystemClock; import androidx.annotation.NonNull; import androidx.annotation.VisibleForTesting; import androidx.work.ListenableWorker.Result; +import app.k9mail.core.android.common.accountmanager.AccountManagerConstants; import com.fsck.k9.Account; import com.fsck.k9.Account.DeletePolicy; import com.fsck.k9.Account.Expunge; @@ -645,6 +647,11 @@ public class MessagingController { migrateAccountToOAuth(account); } + if (updatePassword(account)) { + notificationController.clearAuthenticationErrorNotification(account, true); + return; + } + notificationController.showAuthenticationErrorNotification(account, incoming); } @@ -653,11 +660,51 @@ public class MessagingController { migrateAccountToOAuth(account); } + if (updatePassword(account)) { + notificationController.clearAuthenticationErrorNotification(account, true); + return; + } + if (shouldShowErrorNotification(account, exception)) { notificationController.showAuthenticationErrorNotification(account, incoming); } } + private boolean updatePassword(Account account) { + AccountManager accountManager = AccountManager.get(context); + + for (String accountType : AccountManagerConstants.INSTANCE.getALL_ACCOUNT_TYPES()) { + for (android.accounts.Account deviceAccount : accountManager.getAccountsByType(accountType)) { + // Check if the account email matches with account manager + String emailId = accountManager.getUserData(deviceAccount, + AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY); + if (emailId != null && !emailId.equalsIgnoreCase(account.getEmail())) { + continue; + } + + ServerSettings incoming = account.getIncomingServerSettings(); + ServerSettings outgoing = account.getOutgoingServerSettings(); + + // Only set if its plain password + if (incoming.authenticationType != AuthType.PLAIN + && outgoing.authenticationType != AuthType.PLAIN) { + continue; + } + + // Update incoming and outgoing server settings + String password = accountManager.getPassword(deviceAccount); + if (password != null && (!password.equals(incoming.password) || !password.equals(outgoing.password))) { + account.setIncomingServerSettings(incoming.newPassword(password)); + account.setOutgoingServerSettings(outgoing.newPassword(password)); + preferences.saveAccount(account); + return true; + } + } + } + + return false; + } + // on network switch, sometime mail app failed to authenticate with auth2 accounts, but other operations works perfectly. // It is ignorable, because oauth2 authentication is handled via accountManager private boolean shouldShowErrorNotification(Account account, Exception exception) { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java index f1f90719625a16a32b03522c6ea34c6af12bbe7b..e50152166be0ff1f436659b8f101537aa7534178 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java @@ -139,6 +139,26 @@ public class EeloAccountCreator { } } + private static void updatePasswordIfChanged(Account account, String password) { + ServerSettings incoming = account.getIncomingServerSettings(); + ServerSettings outgoing = account.getOutgoingServerSettings(); + + // Only set if its plain password + if (incoming.authenticationType != AuthType.PLAIN + && outgoing.authenticationType != AuthType.PLAIN) { + return; + } + + // Update incoming and outgoing server settings + if (!password.equals(incoming.password) || !password.equals(outgoing.password)) { + account.setIncomingServerSettings(incoming.newPassword(password)); + account.setOutgoingServerSettings(outgoing.newPassword(password)); + + // Save account since its updated + Preferences.getPreferences().saveAccount(account); + } + } + private static boolean isInvalidEmail(String emailId) { return emailId == null || !emailId.contains("@"); } @@ -163,12 +183,13 @@ public class EeloAccountCreator { continue; } + String password = accountManager.getPassword(eeloAccount); var existenceAccount = accounts.stream() .filter(account -> emailId.equalsIgnoreCase(account.getEmail())) + .peek(account -> updatePasswordIfChanged(account, password)) .findAny(); if (!existenceAccount.isPresent()) { - String password = accountManager.getPassword(eeloAccount); createAccount(context, emailId, password, null, AccountManagerConstants.EELO_ACCOUNT_TYPE); continue; }