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 eb78dfe0339c1e9a5ec7b47442c1989e1b78c5cc..8419ef4ff4ced6a6d2e1a41816916a70c31b383c 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 @@ -681,6 +681,26 @@ public class MessagingController { notificationController.showAuthenticationErrorNotification(account, incoming); } + public void handleAuthenticationFailure(Account account, boolean incoming, Exception exception) { + if (account.shouldMigrateToOAuth()) { + migrateAccountToOAuth(account); + } + + if (shouldShowErrorNotification(account, exception)) { + notificationController.showAuthenticationErrorNotification(account, incoming); + } + } + + // 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) { + if (account.getIncomingServerSettings().authenticationType != AuthType.XOAUTH2) { + return true; + } + + return !(exception instanceof AuthenticationFailedException); + } + private void migrateAccountToOAuth(Account account) { account.setIncomingServerSettings(account.getIncomingServerSettings().newAuthenticationType(AuthType.XOAUTH2)); account.setOutgoingServerSettings(account.getOutgoingServerSettings().newAuthenticationType(AuthType.XOAUTH2)); @@ -691,7 +711,7 @@ public class MessagingController { public void handleException(Account account, Exception exception) { if (exception instanceof AuthenticationFailedException) { - handleAuthenticationFailure(account, true); + handleAuthenticationFailure(account, true, exception); } else { notifyUserIfCertificateProblem(account, exception, true); } @@ -1601,7 +1621,7 @@ public class MessagingController { lastFailure = e; wasPermanentFailure = false; - handleAuthenticationFailure(account, false); + handleAuthenticationFailure(account, false, e); handleSendFailure(account, localFolder, message, e); } catch (CertificateValidationException e) { outboxStateRepository.decrementSendAttempts(messageId); @@ -2781,7 +2801,7 @@ public class MessagingController { syncFailed = true; if (exception instanceof AuthenticationFailedException) { - handleAuthenticationFailure(account, true); + handleAuthenticationFailure(account, true, exception); } else { notifyUserIfCertificateProblem(account, exception, true); }