From d182c9c2d6d571094b978618a785f36fc36c307b Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 15 Aug 2022 16:43:01 +0600 Subject: [PATCH] 5113-Redirect_to_listPage_after_adding_google_account issue: https://gitlab.e.foundation/e/backlog/-/issues/5113 On receive broadcast of accountSync for new account. App will setup account for mail app. After account setup complete, an local broadcast will through using eventBus, which AccountSetupBasics will listen. Onlistening, it will redirect to MessageList activity. --- app/k9mail/proguard-rules.pro | 17 +++++++- app/ui/legacy/build.gradle | 1 + .../fsck/k9/account/AccountCreationAction.kt | 20 +++++++++ .../fsck/k9/account/AccountSyncReceiver.kt | 8 ++-- .../k9/activity/setup/AccountSetupBasics.kt | 41 ++++++++++++++++--- 5 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/account/AccountCreationAction.kt diff --git a/app/k9mail/proguard-rules.pro b/app/k9mail/proguard-rules.pro index 122f6ebbfa..dbde0f80d5 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 1403b14b51..fdaeca5375 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 0000000000..0d62366e58 --- /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 ce18077553..96ac4d2d93 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 cbfb08b70e..9a40e5f937 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() -- GitLab