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

Commit 1b2627ec authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

fix: try to fix account not kept at the end

parent 993b1c94
Loading
Loading
Loading
Loading
Loading
+25 −3
Original line number Diff line number Diff line
@@ -7,13 +7,18 @@ package at.bitfire.davdroid
import android.accounts.AccountManager
import android.accounts.AccountManager.KEY_ACCOUNT_NAME
import android.accounts.AccountManager.KEY_BOOLEAN_RESULT
import android.accounts.AccountManager.KEY_INTENT
import android.accounts.AccountManagerFuture
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import at.bitfire.davdroid.log.Logger

class ReLoginWithOidcActivity : AppCompatActivity() {

    private var needLogout = true

    override fun onStart() {
        super.onStart()

@@ -21,8 +26,12 @@ class ReLoginWithOidcActivity : AppCompatActivity() {
            finish()
            return
        }

        Logger.log.info("Vincent: onStart() call and gonna logout account")
        if (needLogout) {
            logoutAccount(accountName)
        }
    }

    private fun logoutAccount(accountName: String)
    {
@@ -34,6 +43,7 @@ class ReLoginWithOidcActivity : AppCompatActivity() {
            val success = future.result.getBoolean(KEY_BOOLEAN_RESULT)

            if (success) {
                needLogout = false
                loginAccount(eAccountType)
            } else {
                Logger.log.info("failed to remove account: $accountName ")
@@ -52,21 +62,33 @@ class ReLoginWithOidcActivity : AppCompatActivity() {
    private fun loginAccount(accountType: String) {
        val accountManager = AccountManager.get(this)

        val callback: (AliasFuture) -> Unit = { future ->
            val intent = getIntentFromFuture(future)
            intent?.let(::startActivity)
            finish()
        }

        try {
            accountManager.addAccount(
                accountType,
                null,
                null,
                null,
                this,
                null,
                { future -> callback(future)},
                null
            )
        } catch (exception: Exception) {
            Logger.log.warning("${exception.javaClass}: can't add account: ${exception.message}")
        }
    }

        finish()
    private fun getIntentFromFuture(future: AliasFuture): Intent? {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            future.result?.getParcelable(KEY_INTENT, Intent::class.java)
        } else {
            future.result?.getParcelable(KEY_INTENT)
        }
    }
}