From 6b5fdade0b29251441f8d636a5db06104153d237 Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Fri, 30 Jul 2021 16:31:18 +1200 Subject: [PATCH 1/5] Import e accounts from account manager --- app/ui/legacy/src/main/AndroidManifest.xml | 8 +- .../java/com/fsck/k9/activity/MessageList.kt | 76 ++++++++++++++++- .../accountmanager/AccountManagerConstants.kt | 8 ++ .../accountmanager/EeloAccountCreator.java | 84 +++++++++++++++++++ 4 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/AccountManagerConstants.kt create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java diff --git a/app/ui/legacy/src/main/AndroidManifest.xml b/app/ui/legacy/src/main/AndroidManifest.xml index 13dc11d980..b44aade7d5 100644 --- a/app/ui/legacy/src/main/AndroidManifest.xml +++ b/app/ui/legacy/src/main/AndroidManifest.xml @@ -1,2 +1,8 @@ - + + + + + + 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 a3601caca3..e8df5f0e0d 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 @@ -1,5 +1,6 @@ package com.fsck.k9.activity +import android.accounts.AccountManager import android.annotation.SuppressLint import android.app.SearchManager import android.content.Context @@ -32,10 +33,15 @@ import com.fsck.k9.Preferences import com.fsck.k9.account.BackgroundAccountRemover import com.fsck.k9.activity.compose.MessageActions import com.fsck.k9.activity.setup.AccountSetupBasics +import com.fsck.k9.activity.setup.accountmanager.AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY +import com.fsck.k9.activity.setup.accountmanager.AccountManagerConstants.EELO_ACCOUNT_TYPE +import com.fsck.k9.activity.setup.accountmanager.AccountManagerConstants.GOOGLE_ACCOUNT_TYPE +import com.fsck.k9.activity.setup.accountmanager.EeloAccountCreator import com.fsck.k9.controller.MessageReference import com.fsck.k9.fragment.MessageListFragment import com.fsck.k9.fragment.MessageListFragment.MessageListFragmentListener import com.fsck.k9.helper.Contacts +import com.fsck.k9.helper.EmailHelper import com.fsck.k9.helper.ParcelableUtil import com.fsck.k9.mailstore.SearchStatusManager import com.fsck.k9.mailstore.StorageManager @@ -130,11 +136,22 @@ open class MessageList : private var viewSwitcher: ViewSwitcher? = null private lateinit var recentChangesSnackbar: Snackbar + private lateinit var accountManager: AccountManager + public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val accounts = preferences.accounts + var accounts = preferences.accounts deleteIncompleteAccounts(accounts) + + accountManager = AccountManager.get(this) + +// TODO remove old accounts automatically + if (addNewAccountsAutomatically(accounts)) { + accounts = preferences.accounts + } + + val hasAccountSetup = accounts.any { it.isFinishedSetup } if (!hasAccountSetup) { AccountSetupBasics.actionNewAccount(this) @@ -1602,6 +1619,63 @@ open class MessageList : val noThreading: Boolean = false ) + private fun addNewAccountsAutomatically(accounts: List): Boolean { + return try { + val eeloAccounts: Array = getEeloAccountsOnDevice() + val googleAccounts: Array = getGoogleAccountsOnDevice() + var accountWasAdded = false + for (eeloAccount in eeloAccounts) { + val emailId: String = accountManager.getUserData(eeloAccount, ACCOUNT_EMAIL_ADDRESS_KEY) + if (!emailId.contains("@")) continue + var accountIsSignedIn = false + for (account in accounts) { + if (emailId == account.email) { + accountIsSignedIn = true + break + } + } + if (!accountIsSignedIn) { + val password: String = accountManager.getPassword(eeloAccount) + EeloAccountCreator.createAccount(this, emailId, password) + accountWasAdded = true + } + } +// for (googleAccount in googleAccounts) { +// val emailId: String = accountManager.getUserData( +// googleAccount, +// ACCOUNT_EMAIL_ADDRESS_KEY +// ) +// var accountIsSignedIn = false +// for (account in accounts) { +// if (emailId == account.email) { +// if (account.name == null) { // we need to fix an old bug +// account.name = emailId +// account.save(Preferences.getPreferences(this)) +// } +// accountIsSignedIn = true +// break +// } +// } +// if (!accountIsSignedIn) { +// GoogleAccountCreator.createAccount(this, emailId) +// accountWasAdded = true +// } +// } + accountWasAdded + } catch (e: SecurityException) { + e.printStackTrace() + false + } + } + + private fun getEeloAccountsOnDevice(): Array { + return accountManager.getAccountsByType(EELO_ACCOUNT_TYPE) + } + + private fun getGoogleAccountsOnDevice(): Array { + return accountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE) + } + companion object : KoinComponent { private const val EXTRA_SEARCH = "search_bytes" private const val EXTRA_NO_THREADING = "no_threading" diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/AccountManagerConstants.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/AccountManagerConstants.kt new file mode 100644 index 0000000000..622b445446 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/AccountManagerConstants.kt @@ -0,0 +1,8 @@ +package com.fsck.k9.activity.setup.accountmanager + +object AccountManagerConstants { + const val EELO_ACCOUNT_TYPE = "e.foundation.webdav.eelo" + const val GOOGLE_ACCOUNT_TYPE = "e.foundation.webdav.google" + const val ACCOUNT_EMAIL_ADDRESS_KEY = "email_address" + const val MAIL_CONTENT_AUTHORITY = "foundation.e.mail.provider.AppContentProvider" +} \ No newline at end of file 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 new file mode 100644 index 0000000000..9366c5112b --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java @@ -0,0 +1,84 @@ +package com.fsck.k9.activity.setup.accountmanager; + + +import android.content.Context; + +import com.fsck.k9.Account; +import com.fsck.k9.Account.DeletePolicy; +import com.fsck.k9.Core; +import com.fsck.k9.DI; +import com.fsck.k9.Preferences; +import com.fsck.k9.account.AccountCreator; +import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings; +import com.fsck.k9.autodiscovery.api.DiscoveryResults; +import com.fsck.k9.autodiscovery.api.DiscoveryTarget; +import com.fsck.k9.autodiscovery.providersxml.ProvidersXmlDiscovery; +import com.fsck.k9.mail.ServerSettings; +import com.fsck.k9.ui.ConnectionSettings; +import timber.log.Timber; + + +public class EeloAccountCreator { + private static final ProvidersXmlDiscovery providersXmlDiscovery = DI.get(ProvidersXmlDiscovery.class); + private static final AccountCreator accountCreator = DI.get(AccountCreator.class); + + public static void createAccount(Context context, String emailId, String password) { + Preferences preferences = Preferences.getPreferences(context); + + Account account = preferences.newAccount(); + account.setEmail(emailId); + account.setDescription(emailId); + + ConnectionSettings connectionSettings = providersXmlDiscoveryDiscover(emailId); + if (connectionSettings == null) { + Timber.e("Error while trying to initialise account configuration."); + return; + } + ServerSettings incomingSettings = connectionSettings.getIncoming(); + incomingSettings.newPassword(password); + ServerSettings outgoingSettings = connectionSettings.getOutgoing(); + outgoingSettings.newPassword(password); + account.setIncomingServerSettings(incomingSettings); + account.setOutgoingServerSettings(outgoingSettings); + + DeletePolicy deletePolicy = accountCreator.getDefaultDeletePolicy(incomingSettings.type); + account.setDeletePolicy(deletePolicy); + + preferences.saveAccount(account); + Core.setServicesEnabled(context); + } + + private static ConnectionSettings providersXmlDiscoveryDiscover(String email) { + DiscoveryResults discoveryResults = + providersXmlDiscovery.discover(email, DiscoveryTarget.INCOMING_AND_OUTGOING); + if (discoveryResults == null || + (discoveryResults.getIncoming().size() < 1 || discoveryResults.getOutgoing().size() < 1)) { + return null; + } + DiscoveredServerSettings incoming = discoveryResults.getIncoming().get(0); + DiscoveredServerSettings outgoing = discoveryResults.getOutgoing().get(0); + return new ConnectionSettings( + new ServerSettings( + incoming.getProtocol(), + incoming.getHost(), + incoming.getPort(), + incoming.getSecurity(), + incoming.getAuthType(), + incoming.getUsername(), + null, + null + ), + new ServerSettings( + outgoing.getProtocol(), + outgoing.getHost(), + outgoing.getPort(), + outgoing.getSecurity(), + outgoing.getAuthType(), + outgoing.getUsername(), + null, + null + ) + ); + } +} + -- GitLab From 575b09086e40a0eb9a41d71f4b5b90cc01fe6f8c Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Sun, 26 Sep 2021 12:53:13 +1300 Subject: [PATCH 2/5] Setup folders when auto adding account --- .../k9/activity/setup/accountmanager/EeloAccountCreator.java | 5 +++++ 1 file changed, 5 insertions(+) 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 9366c5112b..908007e5f3 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 @@ -14,6 +14,7 @@ import com.fsck.k9.autodiscovery.api.DiscoveryResults; import com.fsck.k9.autodiscovery.api.DiscoveryTarget; import com.fsck.k9.autodiscovery.providersxml.ProvidersXmlDiscovery; import com.fsck.k9.mail.ServerSettings; +import com.fsck.k9.mailstore.SpecialLocalFoldersCreator; import com.fsck.k9.ui.ConnectionSettings; import timber.log.Timber; @@ -21,11 +22,13 @@ import timber.log.Timber; public class EeloAccountCreator { private static final ProvidersXmlDiscovery providersXmlDiscovery = DI.get(ProvidersXmlDiscovery.class); private static final AccountCreator accountCreator = DI.get(AccountCreator.class); + private static final SpecialLocalFoldersCreator localFoldersCreator = DI.get(SpecialLocalFoldersCreator.class); public static void createAccount(Context context, String emailId, String password) { Preferences preferences = Preferences.getPreferences(context); Account account = preferences.newAccount(); + account.setChipColor(accountCreator.pickColor()); account.setEmail(emailId); account.setDescription(emailId); @@ -44,6 +47,8 @@ public class EeloAccountCreator { DeletePolicy deletePolicy = accountCreator.getDefaultDeletePolicy(incomingSettings.type); account.setDeletePolicy(deletePolicy); + localFoldersCreator.createSpecialLocalFolders(account); + preferences.saveAccount(account); Core.setServicesEnabled(context); } -- GitLab From 10257755b928eac4d724100236aec271d128b59f Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Sun, 26 Sep 2021 13:16:50 +1300 Subject: [PATCH 3/5] Mark account setup complete --- .../k9/activity/setup/accountmanager/EeloAccountCreator.java | 1 + 1 file changed, 1 insertion(+) 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 908007e5f3..cb4e6f3dd0 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 @@ -48,6 +48,7 @@ public class EeloAccountCreator { account.setDeletePolicy(deletePolicy); localFoldersCreator.createSpecialLocalFolders(account); + account.markSetupFinished(); preferences.saveAccount(account); Core.setServicesEnabled(context); -- GitLab From a06f6a7f4006bedeb73543fe83e02edca8261e78 Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Sun, 26 Sep 2021 13:38:54 +1300 Subject: [PATCH 4/5] Fix account password not saved --- .../activity/setup/accountmanager/EeloAccountCreator.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 cb4e6f3dd0..cef043ba2b 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 @@ -37,11 +37,9 @@ public class EeloAccountCreator { Timber.e("Error while trying to initialise account configuration."); return; } - ServerSettings incomingSettings = connectionSettings.getIncoming(); - incomingSettings.newPassword(password); - ServerSettings outgoingSettings = connectionSettings.getOutgoing(); - outgoingSettings.newPassword(password); + ServerSettings incomingSettings = connectionSettings.getIncoming().newPassword(password); account.setIncomingServerSettings(incomingSettings); + ServerSettings outgoingSettings = connectionSettings.getOutgoing().newPassword(password); account.setOutgoingServerSettings(outgoingSettings); DeletePolicy deletePolicy = accountCreator.getDefaultDeletePolicy(incomingSettings.type); -- GitLab From 8071c0bb7d26d60730bf475242c570dd15cb7b06 Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Mon, 27 Sep 2021 21:25:47 +1300 Subject: [PATCH 5/5] Try auto add google accounts --- .../java/com/fsck/k9/activity/MessageList.kt | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) 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 e8df5f0e0d..6c0c057326 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 @@ -1640,27 +1640,25 @@ open class MessageList : accountWasAdded = true } } -// for (googleAccount in googleAccounts) { -// val emailId: String = accountManager.getUserData( -// googleAccount, -// ACCOUNT_EMAIL_ADDRESS_KEY -// ) -// var accountIsSignedIn = false -// for (account in accounts) { -// if (emailId == account.email) { -// if (account.name == null) { // we need to fix an old bug -// account.name = emailId -// account.save(Preferences.getPreferences(this)) -// } -// accountIsSignedIn = true -// break -// } -// } -// if (!accountIsSignedIn) { + for (googleAccount in googleAccounts) { + val emailId: String = accountManager.getUserData(googleAccount, ACCOUNT_EMAIL_ADDRESS_KEY) + var accountIsSignedIn = false + for (account in accounts) { + if (emailId == account.email) { + if (account.name == null) { // we need to fix an old bug + account.name = emailId + Preferences.getPreferences(this).saveAccount(account) + } + accountIsSignedIn = true + break + } + } + if (!accountIsSignedIn) { // GoogleAccountCreator.createAccount(this, emailId) -// accountWasAdded = true -// } -// } + EeloAccountCreator.createAccount(this, emailId, "") + accountWasAdded = true + } + } accountWasAdded } catch (e: SecurityException) { e.printStackTrace() -- GitLab