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

Commit 07a5b25d authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

refactor: Run code of BootCompleteReceiver in a coroutine as suggested by codeRabbit

parent 518d49b4
Loading
Loading
Loading
Loading
+28 −12
Original line number Diff line number Diff line
@@ -24,6 +24,10 @@ import at.bitfire.davdroid.ui.NotificationUtils
import at.bitfire.davdroid.ui.NotificationUtils.CHANNEL_GENERAL
import at.bitfire.davdroid.ui.NotificationUtils.NOTIFY_SWITCH_TO_OPENID
import at.bitfire.davdroid.ui.NotificationUtils.notifyIfPossible
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

/**
 * There are circumstances when Android drops automatic sync of accounts and resets them
@@ -34,16 +38,23 @@ import at.bitfire.davdroid.ui.NotificationUtils.notifyIfPossible
 */
class BootCompletedReceiver: BroadcastReceiver() {

    private val job = SupervisorJob()
    private val scope = CoroutineScope(job + Dispatchers.IO)

    override fun onReceive(context: Context, intent: Intent) {
        Logger.log.info("Device has been rebooted; checking sync intervals etc.")

        val pendingResult = goAsync()
        scope.launch {
            try {
                val eAccountType = context.getString(R.string.eelo_account_type)
                val accountManager = AccountManager.get(context)
                val canNotify = canNotify(context)

                AccountUtils.getMainAccounts(context)
                    .forEach {
                val needReLogin = eAccountType == it.type && !isLoggedWithOpenId(it, accountManager)
                        val needReLogin =
                            eAccountType == it.type && !isLoggedWithOpenId(it, accountManager)

                        if (canNotify && needReLogin) {
                            notifySwitchToOpenId(it, context)
@@ -52,6 +63,11 @@ class BootCompletedReceiver: BroadcastReceiver() {
                            accountSettings.initSync()
                        }
                    }
            } finally {
                pendingResult.finish()
                job.cancel()
            }
        }
    }

    private fun isLoggedWithOpenId(account: Account, accountManager: AccountManager): Boolean {