From 853441f8cda3443ca5d3985e5fac80012615197d Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 23 Aug 2023 17:15:59 +0600 Subject: [PATCH 1/4] WIP: implement basic setup --- .../fsck/k9/setup/AccountManagerConstants.kt | 2 +- .../com/fsck/k9/setup/EeloAccountHelper.kt | 11 +++- .../accountmanager/EeloAccountCreator.java | 59 +++++++++++++++---- 3 files changed, 57 insertions(+), 15 deletions(-) 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 2cb7c4bc01..dd1f2c498f 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 @@ -30,7 +30,7 @@ object AccountManagerConstants { 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 e273661fcd..e3d02b2d9d 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/accountmanager/EeloAccountCreator.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java index 4cc9cd0077..98d2106365 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) { + 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.eeo.one"; + 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, -- GitLab From 0e6dc84f1b6320e5808dd38d0a930144d0d3eea4 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 29 Aug 2023 17:48:52 +0600 Subject: [PATCH 2/4] pass userEmailHint for openId account setup flow --- .../src/main/java/com/fsck/k9/setup/AccountManagerConstants.kt | 1 + .../main/java/com/fsck/k9/activity/setup/AccountSetupBasics.kt | 1 + .../k9/activity/setup/accountmanager/EeloAccountCreator.java | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) 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 dd1f2c498f..85ad1d27d4 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,6 +24,7 @@ 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" 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 97a18c9241..9bc6fd2327 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 98d2106365..89b33e460d 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 @@ -259,7 +259,7 @@ public class EeloAccountCreator { } private static ConnectionSettings providersDefaultOpenIdAccountDiscover(@NonNull String email, @NonNull String accountType) { - String incomingHost = "mail.eeo.one"; + String incomingHost = "mail.ecloud.global"; String outgoingHost = incomingHost; int outgoingPort = 587; ConnectionSecurity outgoingConnectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED; -- GitLab From d126106b8ed8cb3d986e86dff1a746646e61f9a0 Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Wed, 30 Aug 2023 06:42:54 +0000 Subject: [PATCH 3/4] Apply 1 suggestion(s) to 1 file(s) --- .../k9/activity/setup/accountmanager/EeloAccountCreator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 89b33e460d..19f092e340 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 @@ -125,7 +125,7 @@ public class EeloAccountCreator { } private static boolean isOpenIdAccount(AccountManager accountManager, android.accounts.Account account) { - String authState = accountManager.getUserData(account, AccountManagerConstants.KEY_AUTH_STATE); + 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) { -- GitLab From 4697418e02c8afc63045a60a0a2aad2ab9b59958 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 30 Aug 2023 09:31:30 +0000 Subject: [PATCH 4/4] Apply 1 suggestion(s) to 1 file(s) --- .../k9/activity/setup/accountmanager/EeloAccountCreator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 19f092e340..6eba1ec75b 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 @@ -125,7 +125,7 @@ public class EeloAccountCreator { } private static boolean isOpenIdAccount(AccountManager accountManager, android.accounts.Account account) { - final String authState = accountManager.getUserData(account, AccountManagerConstants.KEY_AUTH_STATE); + 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) { -- GitLab