Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 300024ac authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

feature: Start add account session after logout

parent 9911cce9
Loading
Loading
Loading
Loading
+85 −13
Original line number Diff line number Diff line
package at.bitfire.davdroid

import android.accounts.AccountManager
import android.accounts.AccountManager.KEY_ACCOUNT_SESSION_BUNDLE
import android.accounts.AccountManager.KEY_BOOLEAN_RESULT
import android.accounts.AccountManager.KEY_INTENT
import android.accounts.AccountManagerFuture
import android.accounts.AuthenticatorException
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import at.bitfire.davdroid.log.Logger
import com.owncloud.android.lib.common.operations.OperationCancelledException
import java.io.IOException

class ReLoginWithOidcActivity : AppCompatActivity() {

    private lateinit var addAccountLauncher: ActivityResultLauncher<Intent>
    private lateinit var accountManager : AccountManager
    private var sessionBundle: Bundle? = null

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)

        accountManager = AccountManager.get(this)
        // Initialize the ActivityResultLauncher
        addAccountLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            result ->
            if (result.resultCode == RESULT_OK) {
                // Account was added successfully
                // Handle the result here
                accountManager.finishSession(sessionBundle,
                    null, { _ ->
                                Logger.log.info("Vincent: Account added")
                                finish()
                            }, null)
            }
        }
    }

    override fun onStart() {
        super.onStart()

        val accountName = intent?.extras?.getString(AccountManager.KEY_ACCOUNT_NAME, null) ?: return
        val accountManager = AccountManager.get(this)

        val accountRemovalCallback: (AliasFuture) -> Unit =  { future ->
            val success = future.result.getBoolean(KEY_BOOLEAN_RESULT)

            if (success) {
                Logger.log.info("Vincent: $accountName removed")
                loginAccount(accountManager)
                loginAccount()
            } else {
                Logger.log.info("failed to remove account: $accountName ")
            }
        }

        logoutAccount(accountName, accountManager, accountRemovalCallback)
        logoutAccount(accountName, accountRemovalCallback)
    }

    private fun logoutAccount(accountName: String,
                              accountManager: AccountManager,
                              callback : (AliasFuture) -> Unit)
    {
        val eAccounts= accountManager.getAccountsByType(applicationContext.getString(R.string.eelo_account_type))
@@ -38,22 +70,62 @@ class ReLoginWithOidcActivity : AppCompatActivity() {
        eAccounts.first { it.name == accountName }.run {
            Logger.log.info("Vincent: try to remove account: ${this.name} ") //todo remove this before merge
            accountManager.removeAccount(this, activity, callback, null)

        } ?: finish()
    }

    private fun loginAccount(accountManager: AccountManager) {
    private fun loginAccount() {
        val eAccountType = applicationContext.getString(R.string.eelo_account_type)
        val authTokenType = null

        Logger.log.info("Vincent: Will start add Account Session")
        accountManager.startAddAccountSession(eAccountType,

        try {
            //todo only work for API 33 ?
            accountManager.startAddAccountSession(
                eAccountType,
                authTokenType,
                arrayOf(),
                null,
                this,
            { _ -> finish() },
                { future -> startAddAccount(future) },
                null
            )
        } catch (exception: AuthenticatorException) {
            Logger.log.info("Vincent: AuthenticatorException: can't add account: ${exception.message}")

        } catch (exception: IOException) {
            Logger.log.info("Vincent: IOException: can't add account: ${exception.message}")
        } catch (exception: OperationCancelledException) {
            Logger.log.info("Vincent: OperationCancelledException: can't add account: ${exception.message}")
        }
    }


    private fun startAddAccount(future: AliasFuture) {
        try {
            val intent: Intent?


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                intent = future.result.getParcelable(KEY_INTENT, Intent::class.java)
                sessionBundle = future.result.getParcelable(KEY_ACCOUNT_SESSION_BUNDLE, Bundle::class.java)
            } else {
                intent = future.result.getParcelable<Intent>(KEY_INTENT)
                sessionBundle = future.result.getParcelable<Bundle>(KEY_ACCOUNT_SESSION_BUNDLE)
            }

            if (sessionBundle == null) {
                Logger.log.info("Vincent: session bundle is null")
            }


            intent?.let {
                addAccountLauncher.launch(it)
            }
        } catch (exception: Exception) {
            Logger.log.info("Vincent: StartAddAccount: exception: ${exception.message}")
        }
    }
}