diff --git a/app/k9mail/proguard-rules.pro b/app/k9mail/proguard-rules.pro index 122f6ebbfacc2126a02c4217bb50c4c888373690..dbde0f80d5d160221dc7a18d8434c85cd1204bdf 100644 --- a/app/k9mail/proguard-rules.pro +++ b/app/k9mail/proguard-rules.pro @@ -91,4 +91,19 @@ -keep class org.simpleframework.xml.** { *; } --keep class com.fsck.k9.activity.setup.accountmanager.** { *; } \ No newline at end of file +-keep class com.fsck.k9.activity.setup.accountmanager.** { *; } + +-keepattributes *Annotation* +-keepclassmembers class * { + @org.greenrobot.eventbus.Subscribe ; +} +-keep enum org.greenrobot.eventbus.ThreadMode { *; } + +# If using AsyncExecutord, keep required constructor of default event used. +# Adjust the class name if a custom failure event type is used. +-keepclassmembers class org.greenrobot.eventbus.util.ThrowableFailureEvent { + (java.lang.Throwable); +} + +# Accessed via reflection, avoid renaming or removal +-keep class org.greenrobot.eventbus.android.AndroidComponentsImpl \ No newline at end of file diff --git a/app/ui/legacy/build.gradle b/app/ui/legacy/build.gradle index 1403b14b51ae08b8e23a807b5751a1aa875261dc..fdaeca5375596dc62808cda66b7d83823512cf98 100644 --- a/app/ui/legacy/build.gradle +++ b/app/ui/legacy/build.gradle @@ -71,6 +71,7 @@ dependencies { implementation "com.squareup.retrofit2:converter-simplexml:${versions.retrofit}" implementation "com.github.fahim44:FullScreenLoadingDialog:1.0.6" + implementation "org.greenrobot:eventbus:3.3.1" } android { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountCreationAction.kt b/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountCreationAction.kt new file mode 100644 index 0000000000000000000000000000000000000000..0d62366e5852537488d9abfd8d5d28296c2e8d2b --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountCreationAction.kt @@ -0,0 +1,20 @@ +/* + * Copyright ECORP SAS 2022 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.fsck.k9.account + +class AccountCreationAction { +} \ No newline at end of file diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountSyncReceiver.kt b/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountSyncReceiver.kt index ce18077553c243cf0bd1c72bb1963de692eeafcd..96ac4d2d9383957ef06036d9371b99508723ad07 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountSyncReceiver.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountSyncReceiver.kt @@ -23,6 +23,7 @@ import com.fsck.k9.Preferences import com.fsck.k9.activity.setup.accountmanager.EeloAccountCreator import com.fsck.k9.controller.push.PushController import java.util.concurrent.Executors +import org.greenrobot.eventbus.EventBus import org.koin.core.component.KoinComponent import org.koin.core.component.inject @@ -55,9 +56,10 @@ class AccountSyncReceiver : BroadcastReceiver(), KoinComponent { EeloAccountCreator.loadAccountsFromAccountManager( it.applicationContext, preferences, - accountRemover, - null - ) + accountRemover + ) { + EventBus.getDefault().post(AccountCreationAction()) + } } } } 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..9a40e5f937aef6180af43be2c13d3c0cb464e1f3 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 @@ -1,10 +1,10 @@ package com.fsck.k9.activity.setup +import android.accounts.AccountManager 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 @@ -16,6 +16,7 @@ import com.fsck.k9.Account import com.fsck.k9.Core import com.fsck.k9.EmailAddressValidator import com.fsck.k9.Preferences +import com.fsck.k9.account.AccountCreationAction import com.fsck.k9.account.AccountCreator import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection import com.fsck.k9.activity.setup.accountmanager.MailAutoConfigDiscovery @@ -28,6 +29,7 @@ import com.fsck.k9.mail.AuthType import com.fsck.k9.mail.ServerSettings import com.fsck.k9.mailstore.SpecialLocalFoldersCreator import com.fsck.k9.oauth.OAuthConfigurationProvider +import com.fsck.k9.setup.AccountManagerConstants import com.fsck.k9.ui.ConnectionSettings import com.fsck.k9.ui.R import com.fsck.k9.ui.base.K9Activity @@ -39,6 +41,9 @@ import com.google.android.material.textfield.TextInputEditText import com.lamonjush.fullscreenloadingdialog.FullScreenLoadingDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.greenrobot.eventbus.EventBus +import org.greenrobot.eventbus.Subscribe +import org.greenrobot.eventbus.ThreadMode import org.koin.android.ext.android.inject /** @@ -70,6 +75,7 @@ class AccountSetupBasics : K9Activity() { private var uiState = UiState.EMAIL_ADDRESS_ONLY private var account: Account? = null private var checkedIncoming = false + private var accountManagerAuthRunning = false public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -101,6 +107,13 @@ class AccountSetupBasics : K9Activity() { validateFields() updateUi() + + EventBus.getDefault().register(this) + } + + override fun onDestroy() { + EventBus.getDefault().unregister(this) + super.onDestroy() } private fun initializeViewListeners() { @@ -189,6 +202,8 @@ class AccountSetupBasics : K9Activity() { } private fun attemptAutoSetupUsingOnlyEmailAddress() { + accountManagerAuthRunning = false + val email = emailView.text?.toString() ?: error("Email missing") val extraConnectionSettings = ExtraAccountDiscovery.discover(email) @@ -210,18 +225,30 @@ 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) + if (oAuthConfigurationProvider.isGoogle(connectionSettings.incoming.host!!)) { + startGoogleOAuthFlow() return } + val account = createAccount(connectionSettings) + val intent = OAuthFlowActivity.buildLaunchIntent(this, account.uuid) startActivityForResult(intent, REQUEST_CODE_OAUTH) } + private fun startGoogleOAuthFlow() { + accountManagerAuthRunning = true + val osAccountManager = AccountManager.get(this) + osAccountManager.addAccount(AccountManagerConstants.GOOGLE_ACCOUNT_TYPE, null, null, null, this, null, null) + } + + @Subscribe(threadMode = ThreadMode.MAIN_ORDERED) + fun onAccountCreatedViaAccountManager(accountCreationAction: AccountCreationAction) { + if (accountManagerAuthRunning) { + finish() + } + } + private fun startPasswordFlow() { uiState = UiState.PASSWORD_FLOW @@ -232,6 +259,8 @@ class AccountSetupBasics : K9Activity() { } private fun attemptAutoSetup() { + accountManagerAuthRunning = false + if (clientCertificateCheckBox.isChecked) { // Auto-setup doesn't support client certificates. onManualSetup()