From 3b204b4ddae3601eb32b9ebf41a89cd85b5d7573 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 13 Jul 2022 22:47:49 +0600 Subject: [PATCH 1/2] remove google account handle by accountManager code --- .../main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt | 2 ++ .../main/java/com/fsck/k9/activity/setup/AccountSetupBasics.kt | 2 ++ .../k9/activity/setup/accountmanager/EeloAccountCreator.java | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) 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 94d0afc5c9..2623eb5406 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,11 @@ 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/setup/AccountSetupBasics.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.kt index cbfb08b70e..f34f5e6ef4 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 @@ -212,11 +212,13 @@ class AccountSetupBasics : K9Activity() { 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) 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 07571c3ef3..79b25cddc4 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,7 @@ public class EeloAccountCreator { deleteIncompleteAccounts(accounts, accountRemover); loadEeloAccounts(context, accounts, accountManager); - loadGoogleAccounts(context, accounts, accountManager); + //loadGoogleAccounts(context, accounts, accountManager); } catch (SecurityException e) { Timber.e(e, "Failed to load accounts from accountManager because of security violation"); } -- GitLab From 9f94927f76ccb37cd729550e01e2b4597949deab Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Thu, 14 Jul 2022 13:37:10 +0600 Subject: [PATCH 2/2] redirect to accountSetup for googleAccount setup by accountManager --- .../k9/backends/RealOAuth2TokenProvider.kt | 5 -- .../java/com/fsck/k9/activity/MessageList.kt | 7 ++ .../k9/activity/setup/AccountSetupBasics.kt | 64 +++++++++++++++---- .../accountmanager/EeloAccountCreator.java | 16 ++--- 4 files changed, 68 insertions(+), 24 deletions(-) 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 2623eb5406..8f1bbeca32 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,11 +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 5c99c460c1..4dd0cd3424 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 f34f5e6ef4..51435ee179 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,17 +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) } @@ -406,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 79b25cddc4..027949c994 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) { -- GitLab