diff --git a/app/k9mail/src/main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt b/app/k9mail/src/main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt index 94d0afc5c940bc1e91bb3ac9053cea1a5f798ad0..8f1bbeca327985d4ee30abf174eee17aa4df2786 100644 --- a/app/k9mail/src/main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt +++ b/app/k9mail/src/main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt @@ -94,9 +94,6 @@ class RealOAuth2TokenProvider( val handler = Handler(handlerThread.looper) val accounts: ArrayList = arrayListOf() - for (account in osAccountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE)) { - accounts.add(account) - } for (account in osAccountManager.getAccountsByType(EELO_ACCOUNT_TYPE)) { accounts.add(account) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt index 5c99c460c12cc190970a0369710fd15040adb635..4dd0cd34245f945edb01e3396deec36cfb16e362 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt @@ -161,6 +161,13 @@ open class MessageList : } private fun onAccountConfigurationFinish(savedInstanceState: Bundle?) { + val notLoadedGoogleEmail = EeloAccountCreator.retrieveNotLoadedFirstGoogleAccount(applicationContext, preferences) + if (notLoadedGoogleEmail != null) { + AccountSetupBasics.actionNewAccount(this, notLoadedGoogleEmail, true) + finish() + return + } + val accounts = preferences.accounts val hasAccountSetup = accounts.any { it.isFinishedSetup } if (!hasAccountSetup) { 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 cbfb08b70e9e788a0b6235be0143f34ec71ff991..51435ee179d12ca9465de46e8d3ca3f95fcad9f0 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 @@ -4,7 +4,6 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.os.Bundle -import android.provider.Settings import android.text.Editable import android.view.View import android.view.ViewGroup @@ -25,9 +24,11 @@ import com.fsck.k9.autodiscovery.providersxml.ProvidersXmlDiscovery import com.fsck.k9.helper.SimpleTextWatcher import com.fsck.k9.helper.Utility.requiredFieldValid import com.fsck.k9.mail.AuthType +import com.fsck.k9.mail.ConnectionSecurity import com.fsck.k9.mail.ServerSettings import com.fsck.k9.mailstore.SpecialLocalFoldersCreator import com.fsck.k9.oauth.OAuthConfigurationProvider +import com.fsck.k9.preferences.Protocols import com.fsck.k9.ui.ConnectionSettings import com.fsck.k9.ui.R import com.fsck.k9.ui.base.K9Activity @@ -71,6 +72,8 @@ class AccountSetupBasics : K9Activity() { private var account: Account? = null private var checkedIncoming = false + private var isGoogleAccount = false + public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setLayout(R.layout.account_setup_basics) @@ -101,6 +104,15 @@ class AccountSetupBasics : K9Activity() { validateFields() updateUi() + + val email = intent.extras?.getString(ACCOUNT_EMAIL, "") + email?.let { + if (it.isNotBlank()) { + isGoogleAccount = intent.extras?.getBoolean(IS_GOOGLE_ACCOUNT, false) ?: false + emailView.setText(email) + attemptAutoSetupUsingOnlyEmailAddress() + } + } } private fun initializeViewListeners() { @@ -197,7 +209,11 @@ class AccountSetupBasics : K9Activity() { return } - val connectionSettings = providersXmlDiscoveryDiscover(email) + var connectionSettings = providersXmlDiscoveryDiscover(email) + + if (connectionSettings == null && isGoogleAccount) { + connectionSettings = providersDefaultGoogleAccountDiscover(email) + } if (connectionSettings != null && connectionSettings.incoming.authenticationType == AuthType.XOAUTH2 && @@ -209,15 +225,33 @@ class AccountSetupBasics : K9Activity() { } } + private fun providersDefaultGoogleAccountDiscover(email: String): ConnectionSettings { + return ConnectionSettings( + ServerSettings( + Protocols.IMAP, + "imap.gmail.com", + 993, + ConnectionSecurity.SSL_TLS_REQUIRED, + AuthType.XOAUTH2, + email, + null, + null + ), + ServerSettings( + Protocols.SMTP, + "smtp.gmail.com", + 465, + ConnectionSecurity.SSL_TLS_REQUIRED, + AuthType.XOAUTH2, + email, + null, + null + ) + ) + } + private fun startOAuthFlow(connectionSettings: ConnectionSettings) { val account = createAccount(connectionSettings) - - if (oAuthConfigurationProvider.isGoogle(account.incomingServerSettings.host!!)) { - val intent = Intent(Settings.ACTION_ADD_ACCOUNT) - startActivity(intent) - return - } - val intent = OAuthFlowActivity.buildLaunchIntent(this, account.uuid) startActivityForResult(intent, REQUEST_CODE_OAUTH) } @@ -404,12 +438,22 @@ class AccountSetupBasics : K9Activity() { private const val STATE_KEY_CHECKED_INCOMING = "com.fsck.k9.AccountSetupBasics.checkedIncoming" private const val REQUEST_CODE_CHECK_SETTINGS = AccountSetupCheckSettings.ACTIVITY_REQUEST_CODE private const val REQUEST_CODE_OAUTH = Activity.RESULT_FIRST_USER + 1 + private const val ACCOUNT_EMAIL = "email" + private const val IS_GOOGLE_ACCOUNT = "isGoogleAccount" @JvmStatic fun actionNewAccount(context: Context) { val intent = Intent(context, AccountSetupBasics::class.java) context.startActivity(intent) } + + @JvmStatic + fun actionNewAccount(context: Context, email: String, isGoogleAccount: Boolean) { + val intent = Intent(context, AccountSetupBasics::class.java) + intent.putExtra(ACCOUNT_EMAIL, email) + intent.putExtra(IS_GOOGLE_ACCOUNT, isGoogleAccount) + context.startActivity(intent) + } } } 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 07571c3ef35df359b3591c33beb86c9ec712bbf4..027949c994cbc7e1539243e5bfeb25c1b62acbc8 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 @@ -63,7 +63,6 @@ public class EeloAccountCreator { deleteIncompleteAccounts(accounts, accountRemover); loadEeloAccounts(context, accounts, accountManager); - loadGoogleAccounts(context, accounts, accountManager); } catch (SecurityException e) { Timber.e(e, "Failed to load accounts from accountManager because of security violation"); } @@ -73,13 +72,13 @@ public class EeloAccountCreator { } @RequiresApi(api = VERSION_CODES.N) - private static void loadGoogleAccounts(@NonNull Context context, List accounts, - @NonNull AccountManager accountManager) { - android.accounts.Account[] googleAccounts = - accountManager.getAccountsByType(AccountManagerConstants.GOOGLE_ACCOUNT_TYPE); + @Nullable + public static String retrieveNotLoadedFirstGoogleAccount(@NonNull Context context, @NonNull Preferences preferences) { + AccountManager accountManager = AccountManager.get(context); + List accounts = preferences.getAccounts(); + android.accounts.Account[] googleAccounts = accountManager.getAccountsByType(AccountManagerConstants.GOOGLE_ACCOUNT_TYPE); for (android.accounts.Account googleAccount : googleAccounts) { - String emailId = - accountManager.getUserData(googleAccount, AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY); + String emailId = accountManager.getUserData(googleAccount, AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY); if (isInvalidEmail(emailId)) { continue; } @@ -89,9 +88,10 @@ public class EeloAccountCreator { .peek(account -> updateAccountNameIfMissing(context, emailId, account)) .findAny().isPresent(); if (!accountAlreadyPresent) { - createAccount(context, emailId, "", true); + return emailId; } } + return null; } private static void updateAccountNameIfMissing(@NonNull Context context, String emailId, Account account) {