diff --git a/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt b/app/src/main/java/at/bitfire/davdroid/MailAccountSyncHelper.kt index 42e78eb37803682661ca464ef550c3439c62e194..66406e36e9830cf6827ca1caf639003910423d8a 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,13 +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 accountLoggedIn(applicationContext : Context?) { + 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 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 98076add8106154013d729f71b3a44848bcdacca..142ed8505ad6543e95bd295525767fd3b4cc5841 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/syncadapter/GoogleAccountAuthenticatorService.kt b/app/src/main/java/at/bitfire/davdroid/syncadapter/GoogleAccountAuthenticatorService.kt index 82fe2de0f648ed0e9eacd48272a44dc03b6d9f7f..c5752de8f44519de0ef0e79f2c666248c09957de 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/account/AccountActivity.kt b/app/src/main/java/at/bitfire/davdroid/ui/account/AccountActivity.kt index 91ef9620d9d8c6d93dbf887db25b42a90f3e7fab..e5522d85a430403c3eb83d99660f13a06cd5370f 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 35248273dd7012ac7dfe1643180d33c6e285f399..f76d27a609704f19cd58efc168fd088188f2eb4d 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 packageToBeOpened = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_PACKAGE_AFTER_AUTH) + val activityToBeOpened = requireActivity().intent.getStringExtra(LoginActivity.OPEN_APP_ACTIVITY_AFTER_AUTH) + openApp(packageToBeOpened, activityToBeOpened) + MailAccountSyncHelper.onAccountLoggedIn(context?.applicationContext) + } + + 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) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) + intent.component = ComponentName(packageToBeOpened!!, activityToBeOpened!!) + 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 cba6d8e54663fc4620e9008eae6a2980720f160a..8e8401d0aa1821f33065a842e55936a139c92547 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 bd6d58e1327a72a3d4349e27bd893dfb8ae2f2da..971ad07b119eb99f34e879801080fdb03210b886 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