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

Verified Commit 7e2446af authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

refactor: fix next alarm schedule when cached token is used in MurenaTokenManager

parent e1c3269c
Loading
Loading
Loading
Loading
Loading
+18 −3
Original line number Original line Diff line number Diff line
@@ -37,6 +37,7 @@ import java.text.SimpleDateFormat
import java.util.Date
import java.util.Date
import java.util.Locale
import java.util.Locale
import java.util.logging.Level
import java.util.logging.Level
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.minutes


object MurenaTokenManager {
object MurenaTokenManager {
@@ -123,12 +124,12 @@ object MurenaTokenManager {
        val pendingIntent = getPendingIntent(context)
        val pendingIntent = getPendingIntent(context)


        if (alarmManager == null || pendingIntent == null) {
        if (alarmManager == null || pendingIntent == null) {
            Logger.log.warning("could not schedule token refresh alarm.")
            Logger.log.warning("Could not schedule token refresh alarm.")
            return
            return
        }
        }


        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent)
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent)
        Logger.log.info("Next token refresh scheduled at ${timeInMillis.asDateString()}")
        Logger.log.info("Next token refresh alarm scheduled at ${timeInMillis.asDateString()}")
    }
    }


   // Refreshes the authentication token and updates stored credentials if successful.
   // Refreshes the authentication token and updates stored credentials if successful.
@@ -178,8 +179,22 @@ object MurenaTokenManager {
            }
            }


            if (updatedAuthState != null) {
            if (updatedAuthState != null) {
                accountSettings.credentials(credentials.copy(authState = updatedAuthState))
                if (authState.accessToken == updatedAuthState.accessToken) {
                    val nextRefreshAt =
                        updatedAuthState.accessTokenExpirationTime?.plus(1.hours.inWholeMilliseconds)

                    if (nextRefreshAt != null) {
                        Logger.log.finest(
                            "Token is fresh and is being reused, " +
                                    "scheduling next refresh at ${nextRefreshAt.asDateString()}"
                        )
                        setTokenRefreshAlarm(context, nextRefreshAt)
                    }
                    return
                }

                Logger.log.info("Token refreshed for ${accountSettings.account}")
                Logger.log.info("Token refreshed for ${accountSettings.account}")
                accountSettings.credentials(credentials.copy(authState = updatedAuthState))


                // Schedule at least 2 minutes early for the new token.
                // Schedule at least 2 minutes early for the new token.
                val refreshAt =
                val refreshAt =