diff --git a/app/core/src/main/java/com/fsck/k9/setup/AccountManagerConstants.kt b/app/core/src/main/java/com/fsck/k9/setup/AccountManagerConstants.kt index 2cb7c4bc013d9385871ce662a33f83b57a1ba800..85ad1d27d4ff2d936b747c8831e033331fb27302 100644 --- a/app/core/src/main/java/com/fsck/k9/setup/AccountManagerConstants.kt +++ b/app/core/src/main/java/com/fsck/k9/setup/AccountManagerConstants.kt @@ -24,13 +24,14 @@ object AccountManagerConstants { const val MAIL_CONTENT_AUTHORITY = "foundation.e.mail.provider.AppContentProvider" const val AUTH_TOKEN_TYPE = "oauth2-access-token" const val KEY_AUTH_STATE = "auth_state" + const val USERNAME_HINT = "userNameHint" const val OPEN_APP_PACKAGE_AFTER_AUTH = "open_app_package_after_auth" const val OPEN_APP_ACTIVITY_AFTER_AUTH = "open_app_activity_after_auth" const val IGNORE_ACCOUNT_SETUP = "ignore_account_setup" - val OPENID_ACCOUNT_TYPES = listOf(GOOGLE_ACCOUNT_TYPE, YAHOO_ACCOUNT_TYPE) + val OPENID_ACCOUNT_TYPES = listOf(EELO_ACCOUNT_TYPE, GOOGLE_ACCOUNT_TYPE, YAHOO_ACCOUNT_TYPE) val ALL_ACCOUNT_TYPES = listOf(EELO_ACCOUNT_TYPE, GOOGLE_ACCOUNT_TYPE, YAHOO_ACCOUNT_TYPE) fun getOpenIdAccountTypeByHostName(hostname: String): String? { diff --git a/app/core/src/main/java/com/fsck/k9/setup/EeloAccountHelper.kt b/app/core/src/main/java/com/fsck/k9/setup/EeloAccountHelper.kt index e273661fcd54f52ec9f098b28c075d10b2e50382..e3d02b2d9d5b35fdefe42b2312775b2a7e2c7dac 100644 --- a/app/core/src/main/java/com/fsck/k9/setup/EeloAccountHelper.kt +++ b/app/core/src/main/java/com/fsck/k9/setup/EeloAccountHelper.kt @@ -118,9 +118,16 @@ object EeloAccountHelper { val openIdAccounts = accountManager.getAccountsByType(accountType) for (openIdAccount in openIdAccounts) { val emailId = accountManager.getUserData(openIdAccount, AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY) - if (email.equals(emailId, ignoreCase = true)) { - return openIdAccount + if (!email.equals(emailId, ignoreCase = true)) { + continue } + + val authState = accountManager.getUserData(openIdAccount, AccountManagerConstants.KEY_AUTH_STATE) + if (authState.isNullOrEmpty()) { + return null + } + + return openIdAccount } return null diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.kt index 97a18c9241274ba072694663c88049893a0edfdb..9bc6fd23278e7a59bf8ec8a63d6eb6e0fc94ed9a 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.kt @@ -234,6 +234,7 @@ class AccountSetupBasics : K9Activity() { val options = Bundle() options.putString(AccountManagerConstants.OPEN_APP_PACKAGE_AFTER_AUTH, packageName) options.putString(AccountManagerConstants.OPEN_APP_ACTIVITY_AFTER_AUTH, MessageList::class.java.name) + options.putString(AccountManagerConstants.USERNAME_HINT, emailView.text?.toString()) osAccountManager.addAccount(accountType, null, null, options, this, null, null) } 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 4cc9cd0077e21b0713baf522f499337af551c1f0..6eba1ec75b20395841b42f2b2e615c0d6b58b419 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 @@ -105,6 +105,10 @@ public class EeloAccountCreator { continue; } + if (!isOpenIdAccount(accountManager, openIdAccount)) { + continue; + } + var existenceAccount = accounts.stream() .filter(account -> emailId.equalsIgnoreCase(account.getEmail())) .peek(account -> updateAccountNameIfMissing(context, emailId, account)) @@ -112,7 +116,7 @@ public class EeloAccountCreator { if (!existenceAccount.isPresent()) { String authState = accountManager.getUserData(openIdAccount, AccountManagerConstants.KEY_AUTH_STATE); - createAccount(context, emailId, "", authState); + createAccount(context, emailId, "", authState, accountType); continue; } @@ -120,6 +124,10 @@ public class EeloAccountCreator { } } + private static boolean isOpenIdAccount(AccountManager accountManager, android.accounts.Account account) { + final String authState = accountManager.getUserData(account, AccountManagerConstants.KEY_AUTH_STATE); + return authState != null && !authState.trim().isEmpty(); + } private static void updateAccountNameIfMissing(@NonNull Context context, String emailId, Account account) { if (account.getName() == null) { // we need to fix an old bug account.setName(emailId); @@ -146,13 +154,17 @@ public class EeloAccountCreator { continue; } + if (isOpenIdAccount(accountManager, eeloAccount)) { + continue; + } + var existenceAccount = accounts.stream() .filter(account -> emailId.equalsIgnoreCase(account.getEmail())) .findAny(); if (!existenceAccount.isPresent()) { String password = accountManager.getPassword(eeloAccount); - createAccount(context, emailId, password, null); + createAccount(context, emailId, password, null, AccountManagerConstants.EELO_ACCOUNT_TYPE); continue; } @@ -166,7 +178,7 @@ public class EeloAccountCreator { .forEach(account -> accountRemover.removeAccountAsync(account.getUuid())); } - private static void createAccount(Context context, String emailId, String password, @Nullable String authState) { + private static void createAccount(Context context, String emailId, String password, @Nullable String authState, @NonNull String accountType) { Preferences preferences = Preferences.getPreferences(); Account account = preferences.newAccount(); @@ -181,18 +193,24 @@ public class EeloAccountCreator { connectionSettings = MailAutoConfigDiscovery.retrieveConfigFromApi(emailId); } // providers.xml doesn't have the connection details & can't retrieve details from api - // & it is google account (authState should not be null), meaning custom domain for google account is used. - // In this case, provide default gmail configuration. + // In this case, fall back to default configuration. if (connectionSettings == null && authState != null) { - connectionSettings = providersDefaultGoogleAccountDiscover(emailId); + connectionSettings = providersDefaultOpenIdAccountDiscover(emailId, accountType); } + if (connectionSettings == null) { Timber.e("Error while trying to initialise account configuration."); return; } ServerSettings incomingSettings = connectionSettings.getIncoming().newPassword(password); - account.setIncomingServerSettings(incomingSettings); ServerSettings outgoingSettings = connectionSettings.getOutgoing().newPassword(password); + + if (authState != null && !authState.trim().isEmpty()) { + incomingSettings = incomingSettings.newAuthenticationType(AuthType.XOAUTH2); + outgoingSettings = outgoingSettings.newAuthenticationType(AuthType.XOAUTH2); + } + + account.setIncomingServerSettings(incomingSettings); account.setOutgoingServerSettings(outgoingSettings); account.setOAuthState(authState); @@ -240,11 +258,28 @@ public class EeloAccountCreator { ); } - private static ConnectionSettings providersDefaultGoogleAccountDiscover(String email) { + private static ConnectionSettings providersDefaultOpenIdAccountDiscover(@NonNull String email, @NonNull String accountType) { + String incomingHost = "mail.ecloud.global"; + String outgoingHost = incomingHost; + int outgoingPort = 587; + ConnectionSecurity outgoingConnectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED; + + if (accountType.equals(AccountManagerConstants.GOOGLE_ACCOUNT_TYPE)) { + incomingHost = "imap.gmail.com"; + outgoingHost = "smtp.gmail.com"; + outgoingPort = 465; + outgoingConnectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED; + } else if (accountType.equals(AccountManagerConstants.YAHOO_ACCOUNT_TYPE)) { + incomingHost = "imap.mail.yahoo.com"; + outgoingHost = "smtp.mail.yahoo.com"; + outgoingPort = 465; + outgoingConnectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED; + } + return new ConnectionSettings( new ServerSettings( Protocols.IMAP, - "imap.gmail.com", + incomingHost, 993, ConnectionSecurity.SSL_TLS_REQUIRED, AuthType.XOAUTH2, @@ -254,9 +289,9 @@ public class EeloAccountCreator { ), new ServerSettings( Protocols.SMTP, - "smtp.gmail.com", - 465, - ConnectionSecurity.SSL_TLS_REQUIRED, + outgoingHost, + outgoingPort, + outgoingConnectionSecurity, AuthType.XOAUTH2, email, null,