Loading app/src/main/kotlin/at/bitfire/davdroid/token/MurenaTokenManager.kt +18 −3 Original line number Original line Diff line number Diff line Loading @@ -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 { Loading Loading @@ -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. Loading Loading @@ -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 = Loading Loading
app/src/main/kotlin/at/bitfire/davdroid/token/MurenaTokenManager.kt +18 −3 Original line number Original line Diff line number Diff line Loading @@ -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 { Loading Loading @@ -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. Loading Loading @@ -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 = Loading