From b14cf28c8fa915dd7dfd21014373958b19c7cc13 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 5 Oct 2022 15:22:05 +0600 Subject: [PATCH 1/5] 5113-Redirect_to_app_after_google_auth_complete issue: https://gitlab.e.foundation/e/backlog/-/issues/5113 redirect to provided app's activity after the authentication completed. - add intent packages on google auth request - don't send mail sync broadcast if mail app needed to be open after login to avoid mailAppCrash --- .../bitfire/davdroid/MailAccountSyncHelper.kt | 18 +++++++++++++- .../GoogleAccountAuthenticatorService.kt | 7 ++++++ .../ui/setup/AccountDetailsFragment.kt | 24 +++++++++++++++++-- .../ui/setup/GoogleAuthenticatorFragment.kt | 3 +++ .../davdroid/ui/setup/LoginActivity.kt | 4 ++++ 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt index 42e78eb37..f0a1a8cb9 100644 --- a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt +++ b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt @@ -26,12 +26,18 @@ object MailAccountSyncHelper { private const val MAIL_RECEIVER_CLASS = "com.fsck.k9.account.AccountSyncReceiver" private const val ACTION_PREFIX = "foundation.e.accountmanager.account." - fun accountLoggedIn(applicationContext : Context?) { + fun accountLoggedIn(applicationContext : Context?, OpenAppPackage: String?) { + if (openMailAppActivity(OpenAppPackage)) { + return + } + val intent = getIntent() intent.action = ACTION_PREFIX + "create" applicationContext?.sendBroadcast(intent) } + /**/ + fun accountLoggedOut(applicationContext: Context?, email: String?) { email?.let { if (!it.contains("@")) { @@ -44,6 +50,16 @@ object MailAccountSyncHelper { } } + // if mail app's activity is needed to open after the auth, no need to send broadCast. + // because, sending broadcast will fail. The cause is, mail app will be opened in a new task + private fun openMailAppActivity(openAppPackage: String?): Boolean { + if (openAppPackage == null || openAppPackage != MAIL_PACKAGE) { + return false + } + + return true + } + private fun getIntent() : Intent { val intent = Intent() intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/GoogleAccountAuthenticatorService.kt b/app/src/main/java/at/bitfire/davdroid/syncadapter/GoogleAccountAuthenticatorService.kt index 82fe2de0f..c5752de8f 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/GoogleAccountAuthenticatorService.kt +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/GoogleAccountAuthenticatorService.kt @@ -21,6 +21,7 @@ import android.app.Service import android.content.Context import android.content.Intent import android.os.Bundle +import android.util.Log import at.bitfire.davdroid.R import at.bitfire.davdroid.db.AppDatabase import at.bitfire.davdroid.log.Logger @@ -139,6 +140,12 @@ class GoogleAccountAuthenticatorService : Service(), OnAccountsUpdateListener { LoginActivity.SETUP_ACCOUNT_PROVIDER_TYPE, LoginActivity.ACCOUNT_PROVIDER_GOOGLE ) + + options?.let { + intent.putExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH, it.getString(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH)) + intent.putExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH, it.getString(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH)) + } + val bundle = Bundle(1) bundle.putParcelable(AccountManager.KEY_INTENT, intent) return bundle diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt index 35248273d..28ecea3b8 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt @@ -8,12 +8,14 @@ import android.accounts.Account import android.accounts.AccountAuthenticatorResponse import android.accounts.AccountManager import android.app.Activity +import android.content.ComponentName import android.content.ContentResolver import android.content.Context import android.content.Intent import android.os.Bundle import android.provider.CalendarContract import android.text.Editable +import android.text.TextUtils import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -38,7 +40,6 @@ import at.bitfire.davdroid.resource.TaskUtils import at.bitfire.davdroid.settings.AccountSettings import at.bitfire.davdroid.settings.SettingsManager import at.bitfire.davdroid.syncadapter.AccountUtils -import at.bitfire.davdroid.ui.account.AccountActivity import at.bitfire.vcard4android.GroupMethod import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint @@ -154,7 +155,6 @@ class AccountDetailsFragment : Fragment() { ).observe(viewLifecycleOwner, Observer { success -> if (success) { Toast.makeText(context, R.string.message_account_added_successfully, Toast.LENGTH_LONG).show() - MailAccountSyncHelper.accountLoggedIn(context?.applicationContext) requireActivity().setResult(Activity.RESULT_OK) requireActivity().finish() @@ -174,6 +174,8 @@ class AccountDetailsFragment : Fragment() { intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.eelo_account_type)) requireActivity().startService(intent) } + + handlePostAuthOperations() } }) } @@ -182,6 +184,24 @@ class AccountDetailsFragment : Fragment() { return v.root } + private fun handlePostAuthOperations() { + val toBeOpenPackage = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH) + val toBeOpenActivity = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH) + MailAccountSyncHelper.accountLoggedIn(context?.applicationContext, toBeOpenPackage) + openAppAfterAuth(toBeOpenPackage, toBeOpenActivity) + } + + private fun openAppAfterAuth(toBeOpenPackage: String?, toBeOpenActivity: String?) { + if (!TextUtils.isEmpty(toBeOpenPackage) && !TextUtils.isEmpty(toBeOpenActivity)) { + val intent = Intent(Intent.ACTION_MAIN) + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) + intent.component = ComponentName(toBeOpenPackage!!, toBeOpenActivity!!) + requireActivity().applicationContext.startActivity(intent) + } + } @HiltViewModel class AccountDetailsModel @Inject constructor( diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/GoogleAuthenticatorFragment.kt b/app/src/main/java/at/bitfire/davdroid/ui/setup/GoogleAuthenticatorFragment.kt index cba6d8e54..8e8401d0a 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/GoogleAuthenticatorFragment.kt +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/GoogleAuthenticatorFragment.kt @@ -28,6 +28,7 @@ import android.os.Looper import android.text.Layout import android.text.SpannableString import android.text.style.AlignmentSpan +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -196,6 +197,8 @@ class GoogleAuthenticatorFragment : Fragment(), AuthorizationService.TokenRespon intent.putExtra(LoginActivity.SETUP_ACCOUNT_PROVIDER_TYPE, LoginActivity.ACCOUNT_PROVIDER_GOOGLE) intent.putExtra(LoginActivity.ACCOUNT_PROVIDER_GOOGLE_AUTH_COMPLETE, true) + intent.putExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH, requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH)) + intent.putExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH, requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH)) return PendingIntent.getActivity(context, request.hashCode(), intent, 0) } diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginActivity.kt b/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginActivity.kt index bd6d58e13..971ad07b1 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginActivity.kt +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginActivity.kt @@ -8,6 +8,7 @@ import android.os.Bundle import android.text.Layout import android.text.SpannableString import android.text.style.AlignmentSpan +import android.util.Log import android.view.MenuItem import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity @@ -47,6 +48,9 @@ class LoginActivity: AppCompatActivity() { const val ACCOUNT_PROVIDER_EELO = "eelo" const val ACCOUNT_PROVIDER_GOOGLE = "google" const val ACCOUNT_PROVIDER_GOOGLE_AUTH_COMPLETE = "google_auth_complete" + + const val OPEN_APP_PACKAGE_AFTER_AUTH = "open_app_package_after_auth" + const val OPEN_APP_ACTIVITY_AFTER_AUTH = "open_app_activity_after_auth" } @Inject -- GitLab From d73fa5a3a8362b94465143ffca00d796e386da1c Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 5 Oct 2022 17:08:07 +0600 Subject: [PATCH 2/5] Fix typo: openAppPackage --- .../main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt index f0a1a8cb9..09ad9469f 100644 --- a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt +++ b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt @@ -26,8 +26,8 @@ object MailAccountSyncHelper { private const val MAIL_RECEIVER_CLASS = "com.fsck.k9.account.AccountSyncReceiver" private const val ACTION_PREFIX = "foundation.e.accountmanager.account." - fun accountLoggedIn(applicationContext : Context?, OpenAppPackage: String?) { - if (openMailAppActivity(OpenAppPackage)) { + fun accountLoggedIn(applicationContext : Context?, openAppPackage: String?) { + if (openMailAppActivity(openAppPackage)) { return } -- GitLab From fb6a45b21d6a56568f578013d2d6d350d6362949 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 5 Oct 2022 17:31:29 +0600 Subject: [PATCH 3/5] Update according to review --- .../bitfire/davdroid/MailAccountSyncHelper.kt | 6 +++--- .../ui/setup/AccountDetailsFragment.kt | 18 +++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt index 09ad9469f..6af46f1ce 100644 --- a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt +++ b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt @@ -26,7 +26,7 @@ object MailAccountSyncHelper { private const val MAIL_RECEIVER_CLASS = "com.fsck.k9.account.AccountSyncReceiver" private const val ACTION_PREFIX = "foundation.e.accountmanager.account." - fun accountLoggedIn(applicationContext : Context?, openAppPackage: String?) { + fun onAccountLoggedIn(applicationContext : Context?, openAppPackage: String) { if (openMailAppActivity(openAppPackage)) { return } @@ -52,8 +52,8 @@ object MailAccountSyncHelper { // if mail app's activity is needed to open after the auth, no need to send broadCast. // because, sending broadcast will fail. The cause is, mail app will be opened in a new task - private fun openMailAppActivity(openAppPackage: String?): Boolean { - if (openAppPackage == null || openAppPackage != MAIL_PACKAGE) { + private fun openMailAppActivity(openAppPackage: String): Boolean { + if (openAppPackage != MAIL_PACKAGE) { return false } diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt index 28ecea3b8..eed2a7709 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt @@ -185,20 +185,24 @@ class AccountDetailsFragment : Fragment() { } private fun handlePostAuthOperations() { - val toBeOpenPackage = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH) - val toBeOpenActivity = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH) - MailAccountSyncHelper.accountLoggedIn(context?.applicationContext, toBeOpenPackage) - openAppAfterAuth(toBeOpenPackage, toBeOpenActivity) + val packageToBeOpened = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH) + val activityToBeOpened = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH) + + packageToBeOpened?.let { + MailAccountSyncHelper.onAccountLoggedIn(context?.applicationContext, it) + } + + openAppAfterAuth(packageToBeOpened, activityToBeOpened) } - private fun openAppAfterAuth(toBeOpenPackage: String?, toBeOpenActivity: String?) { - if (!TextUtils.isEmpty(toBeOpenPackage) && !TextUtils.isEmpty(toBeOpenActivity)) { + private fun openAppAfterAuth(packageToBeOpened: String?, activityToBeOpened: String?) { + if (!TextUtils.isEmpty(packageToBeOpened) && !TextUtils.isEmpty(activityToBeOpened)) { val intent = Intent(Intent.ACTION_MAIN) intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) - intent.component = ComponentName(toBeOpenPackage!!, toBeOpenActivity!!) + intent.component = ComponentName(packageToBeOpened!!, activityToBeOpened!!) requireActivity().applicationContext.startActivity(intent) } } -- GitLab From e531464bece64fb5035d9dedd2356c685b2d0b71 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 5 Oct 2022 17:42:51 +0600 Subject: [PATCH 4/5] Simplify logic --- .../bitfire/davdroid/MailAccountSyncHelper.kt | 21 +++---------------- .../davdroid/resource/LocalAddressBook.kt | 4 ++-- .../davdroid/ui/account/AccountActivity.kt | 4 ++-- .../ui/setup/AccountDetailsFragment.kt | 6 +----- 4 files changed, 8 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt index 6af46f1ce..66406e36e 100644 --- a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt +++ b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt @@ -1,4 +1,5 @@ /* + * Copyright MURENA SAS 2022 * 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 @@ -26,19 +27,13 @@ object MailAccountSyncHelper { private const val MAIL_RECEIVER_CLASS = "com.fsck.k9.account.AccountSyncReceiver" private const val ACTION_PREFIX = "foundation.e.accountmanager.account." - fun onAccountLoggedIn(applicationContext : Context?, openAppPackage: String) { - if (openMailAppActivity(openAppPackage)) { - return - } - + fun onAccountLoggedIn(applicationContext : Context?) { val intent = getIntent() intent.action = ACTION_PREFIX + "create" applicationContext?.sendBroadcast(intent) } - /**/ - - fun accountLoggedOut(applicationContext: Context?, email: String?) { + fun onAccountLoggedOut(applicationContext: Context?, email: String?) { email?.let { if (!it.contains("@")) { return@let @@ -50,16 +45,6 @@ object MailAccountSyncHelper { } } - // if mail app's activity is needed to open after the auth, no need to send broadCast. - // because, sending broadcast will fail. The cause is, mail app will be opened in a new task - private fun openMailAppActivity(openAppPackage: String): Boolean { - if (openAppPackage != MAIL_PACKAGE) { - return false - } - - return true - } - private fun getIntent() : Intent { val intent = Intent() intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) diff --git a/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.kt b/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.kt index 98076add8..142ed8505 100644 --- a/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.kt +++ b/app/src/main/java/at/bitfire/davdroid/resource/LocalAddressBook.kt @@ -259,7 +259,7 @@ open class LocalAddressBook( try { if (it.result) { Handler(Looper.getMainLooper()).post { - MailAccountSyncHelper.accountLoggedOut(context.applicationContext, email) + MailAccountSyncHelper.onAccountLoggedOut(context.applicationContext, email) } } } catch (e: Exception) { @@ -273,7 +273,7 @@ open class LocalAddressBook( try { if (it.result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) { Handler(Looper.getMainLooper()).post { - MailAccountSyncHelper.accountLoggedOut(context.applicationContext, email) + MailAccountSyncHelper.onAccountLoggedOut(context.applicationContext, email) } } } catch (e: Exception) { diff --git a/app/src/main/java/at/bitfire/davdroid/ui/account/AccountActivity.kt b/app/src/main/java/at/bitfire/davdroid/ui/account/AccountActivity.kt index 91ef9620d..e5522d85a 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/account/AccountActivity.kt +++ b/app/src/main/java/at/bitfire/davdroid/ui/account/AccountActivity.kt @@ -140,7 +140,7 @@ class AccountActivity: AppCompatActivity() { try { if (future.result) Handler(Looper.getMainLooper()).post { - MailAccountSyncHelper.accountLoggedOut(applicationContext, email) + MailAccountSyncHelper.onAccountLoggedOut(applicationContext, email) finish() } } catch (e: Exception) { @@ -154,7 +154,7 @@ class AccountActivity: AppCompatActivity() { try { if (future.result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) Handler(Looper.getMainLooper()).post { - MailAccountSyncHelper.accountLoggedOut(applicationContext, email) + MailAccountSyncHelper.onAccountLoggedOut(applicationContext, email) finish() } } catch (e: Exception) { diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt index eed2a7709..499281fe3 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt @@ -187,12 +187,8 @@ class AccountDetailsFragment : Fragment() { private fun handlePostAuthOperations() { val packageToBeOpened = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH) val activityToBeOpened = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH) - - packageToBeOpened?.let { - MailAccountSyncHelper.onAccountLoggedIn(context?.applicationContext, it) - } - openAppAfterAuth(packageToBeOpened, activityToBeOpened) + MailAccountSyncHelper.onAccountLoggedIn(context?.applicationContext) } private fun openAppAfterAuth(packageToBeOpened: String?, activityToBeOpened: String?) { -- GitLab From abc62cf44256ffaa9864ea50604d53a061965003 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 5 Oct 2022 17:49:53 +0600 Subject: [PATCH 5/5] Rename method: openApp --- .../at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt index 499281fe3..f76d27a60 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.kt @@ -187,11 +187,11 @@ class AccountDetailsFragment : Fragment() { private fun handlePostAuthOperations() { val packageToBeOpened = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH) val activityToBeOpened = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH) - openAppAfterAuth(packageToBeOpened, activityToBeOpened) + openApp(packageToBeOpened, activityToBeOpened) MailAccountSyncHelper.onAccountLoggedIn(context?.applicationContext) } - private fun openAppAfterAuth(packageToBeOpened: String?, activityToBeOpened: String?) { + private fun openApp(packageToBeOpened: String?, activityToBeOpened: String?) { if (!TextUtils.isEmpty(packageToBeOpened) && !TextUtils.isEmpty(activityToBeOpened)) { val intent = Intent(Intent.ACTION_MAIN) intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) -- GitLab