From 41bd52b0eab4704caecb0c4df60707864f828a3f Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Thu, 2 Jun 2022 21:06:11 +0600 Subject: [PATCH] 5522-Enable_autoConfig_for_basic_account_setup issue: https://gitlab.e.foundation/e/backlog/-/issues/5522 Enable mail configuration retrieve from api support for basic mail account setup page. --- app/ui/legacy/build.gradle | 2 + .../k9/activity/setup/AccountSetupBasics.java | 69 +++++++++++++------ .../accountmanager/EeloAccountCreator.java | 19 ++++- ...very.java => MailAutoConfigDiscovery.java} | 27 ++++++-- .../MailAutoConfigDiscoveryHelper.kt | 44 ++++++++++++ 5 files changed, 132 insertions(+), 29 deletions(-) rename app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/{EeloMailAutoConfigDiscovery.java => MailAutoConfigDiscovery.java} (87%) create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/MailAutoConfigDiscoveryHelper.kt diff --git a/app/ui/legacy/build.gradle b/app/ui/legacy/build.gradle index eafb08efa1..7fb11f6c7c 100644 --- a/app/ui/legacy/build.gradle +++ b/app/ui/legacy/build.gradle @@ -68,6 +68,8 @@ dependencies { implementation "com.squareup.retrofit2:retrofit:${versions.retrofit}" implementation "com.squareup.retrofit2:converter-simplexml:${versions.retrofit}" + + implementation "com.github.fahim44:FullScreenLoadingDialog:1.0.6" } android { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.java index 03a4ec4c30..b7c6dbf8fe 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupBasics.java @@ -1,15 +1,27 @@ +/* + * 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.activity.setup; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager.NameNotFoundException; -import android.net.Uri; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; -import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; @@ -18,16 +30,16 @@ import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; +import androidx.annotation.Nullable; +import androidx.lifecycle.LifecycleOwner; import com.fsck.k9.Account; import com.fsck.k9.Core; import com.fsck.k9.DI; import com.fsck.k9.EmailAddressValidator; import com.fsck.k9.Preferences; import com.fsck.k9.account.AccountCreator; -import com.fsck.k9.mail.oauth.OAuth2Provider; -import com.fsck.k9.preferences.AccountManager; -import com.fsck.k9.ui.base.K9Activity; import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection; +import com.fsck.k9.activity.setup.accountmanager.MailAutoConfigDiscoveryHelper; import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings; import com.fsck.k9.autodiscovery.api.DiscoveryResults; import com.fsck.k9.autodiscovery.api.DiscoveryTarget; @@ -35,14 +47,17 @@ import com.fsck.k9.autodiscovery.providersxml.ProvidersXmlDiscovery; import com.fsck.k9.helper.Utility; import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.ServerSettings; +import com.fsck.k9.mail.oauth.OAuth2Provider; import com.fsck.k9.mailstore.SpecialLocalFoldersCreator; -import com.fsck.k9.ui.R; import com.fsck.k9.ui.ConnectionSettings; +import com.fsck.k9.ui.R; +import com.fsck.k9.ui.base.K9Activity; import com.fsck.k9.ui.settings.ExtraAccountDiscovery; import com.fsck.k9.view.ClientCertificateSpinner; import com.fsck.k9.view.ClientCertificateSpinner.OnClientCertificateChangedListener; import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputLayout; +import com.lamonjush.fullscreenloadingdialog.FullScreenLoadingDialog; import timber.log.Timber; /** @@ -318,22 +333,34 @@ public class AccountSetupBasics extends K9Activity if(provider != null && provider.toString().trim().equals("GMAIL")){ Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT); startActivity(intent); + return; } - else { - ConnectionSettings extraConnectionSettings = ExtraAccountDiscovery.discover(email); - if (extraConnectionSettings != null) { - finishAutoSetup(extraConnectionSettings); - return; - } - ConnectionSettings connectionSettings = providersXmlDiscoveryDiscover(email, DiscoveryTarget.INCOMING_AND_OUTGOING); - if (connectionSettings != null) { - finishAutoSetup(connectionSettings); - } else { - // We don't have default settings for this account, start the manual setup process. - onManualSetup(); - } + ConnectionSettings extraConnectionSettings = ExtraAccountDiscovery.discover(email); + if (extraConnectionSettings != null) { + finishAutoSetup(extraConnectionSettings); + return; + } + + ConnectionSettings connectionSettings = + providersXmlDiscoveryDiscover(email, DiscoveryTarget.INCOMING_AND_OUTGOING); + if (connectionSettings != null) { + finishAutoSetup(connectionSettings); + return; } + // We don't have predefine configuration, try to retrieve it by api call + FullScreenLoadingDialog.getInstance() + .setSpinKitColor(R.color.color_default_accent) + .show(this); + MailAutoConfigDiscoveryHelper.INSTANCE.retrieveConfiguration(email, this, config -> { + FullScreenLoadingDialog.getInstance().dismiss(); + if (config != null) { + finishAutoSetup(config); + return; + } + // We can't find default settings for this account, start the manual setup process. + onManualSetup(); + }); } @Override 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 25f1fbcf30..16627d6565 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 @@ -1,5 +1,20 @@ -package com.fsck.k9.activity.setup.accountmanager; +/* + * 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.activity.setup.accountmanager; import android.content.Context; @@ -38,7 +53,7 @@ public class EeloAccountCreator { ConnectionSettings connectionSettings = providersXmlDiscoveryDiscover(emailId); if (connectionSettings == null) { // connection details not predefined in the xml. Try to load from the api - connectionSettings = EeloMailAutoConfigDiscovery.retrieveConfigFromApi(emailId); + connectionSettings = MailAutoConfigDiscovery.retrieveConfigFromApi(emailId); } // providers.xml doesn't have the connection details & can't retrieve details from api // & it is google account, meaning custom domain for google account is used. diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigDiscovery.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/MailAutoConfigDiscovery.java similarity index 87% rename from app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigDiscovery.java rename to app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/MailAutoConfigDiscovery.java index 91c8e4aae1..96fdb26338 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigDiscovery.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/MailAutoConfigDiscovery.java @@ -1,6 +1,22 @@ -package com.fsck.k9.activity.setup.accountmanager; +/* + * 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.activity.setup.accountmanager; +import androidx.annotation.Nullable; import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings; import com.fsck.k9.helper.EmailHelper; import com.fsck.k9.helper.Utility; @@ -14,15 +30,14 @@ import retrofit2.Retrofit; import retrofit2.converter.simplexml.SimpleXmlConverterFactory; import timber.log.Timber; - -public class EeloMailAutoConfigDiscovery { +public class MailAutoConfigDiscovery { /** - * Try to retrieve /e/ cloud mail info from the api + * Try to retrieve mail info from the api * - * @param email retrieved from accountManager for /e/ cloud account * @return mail's incoming & outgoing connection detail, null if failed to retrieve */ + @Nullable public static ConnectionSettings retrieveConfigFromApi(String email) { String domain = EmailHelper.getDomainFromEmailAddress(email); if (domain == null) { @@ -170,6 +185,6 @@ public class EeloMailAutoConfigDiscovery { return getAutoConfigSettings(response.getEmailProvider().getOutgoingServer()); } - private EeloMailAutoConfigDiscovery() { + private MailAutoConfigDiscovery() { } } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/MailAutoConfigDiscoveryHelper.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/MailAutoConfigDiscoveryHelper.kt new file mode 100644 index 0000000000..7cf8b8c467 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/MailAutoConfigDiscoveryHelper.kt @@ -0,0 +1,44 @@ +/* + * 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.activity.setup.accountmanager + +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.lifecycleScope +import com.fsck.k9.ui.ConnectionSettings +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + +/** + * We can't use lifecycleScope from Java class directly. + * The main requirement of this class is to use lifecycleScope from java class + */ +object MailAutoConfigDiscoveryHelper { + + fun retrieveConfiguration(email: String, lifecycleOwner: LifecycleOwner, callback: ConfigureLoaderCallback) { + lifecycleOwner.lifecycleScope.launch(Dispatchers.IO) { + val config = MailAutoConfigDiscovery.retrieveConfigFromApi(email) + lifecycleOwner.lifecycleScope.launch(Dispatchers.Main) { + callback.onConfigureLoaded(config) + } + } + } + + interface ConfigureLoaderCallback { + + fun onConfigureLoaded(connectionSettings: ConnectionSettings?) + } +} \ No newline at end of file -- GitLab