diff --git a/app/ui/legacy/src/main/AndroidManifest.xml b/app/ui/legacy/src/main/AndroidManifest.xml index 13dc11d9802dc5da6f15c5a2102906ba4f0fa777..b44aade7d54be8b97c2690d74be1c57beb39c396 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 a3601caca32e2c2c254c2661578980ab3325404b..6c0c0573267adecfc4c4d649d7f109c61405fde4 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,61 @@ 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 + Preferences.getPreferences(this).saveAccount(account) + } + accountIsSignedIn = true + break + } + } + if (!accountIsSignedIn) { +// GoogleAccountCreator.createAccount(this, emailId) + EeloAccountCreator.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 0000000000000000000000000000000000000000..622b445446d1f44a5f95a8f089ade0a6d37cf319 --- /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 0000000000000000000000000000000000000000..cef043ba2b29c4c8a47073a1b2262d1cc594eba9 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java @@ -0,0 +1,88 @@ +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.mailstore.SpecialLocalFoldersCreator; +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); + 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); + + ConnectionSettings connectionSettings = providersXmlDiscoveryDiscover(emailId); + 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); + account.setOutgoingServerSettings(outgoingSettings); + + DeletePolicy deletePolicy = accountCreator.getDefaultDeletePolicy(incomingSettings.type); + account.setDeletePolicy(deletePolicy); + + localFoldersCreator.createSpecialLocalFolders(account); + account.markSetupFinished(); + + 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 + ) + ); + } +} +