only refresh when lifecycle state is resumed

This commit is contained in:
Adam Velebil 2023-01-13 10:11:14 +01:00
parent 5e0f316827
commit 2cc3ede286
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10

View File

@ -19,6 +19,7 @@ package com.yubico.authenticator.oath
import android.content.Context
import android.os.Build
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.yubico.authenticator.*
@ -144,11 +145,22 @@ class OathManager(
if (expirations.isNotEmpty()) {
val earliest = expirations.min() * 1000
val now = System.currentTimeMillis()
refreshJob = coroutineScope.launch {
if (earliest > now) {
delay(earliest - now)
val delayMs = earliest - now
Log.d(TAG, "Will execute refresh in ${delayMs}ms")
if (delayMs > 0) {
delay(delayMs)
}
val currentState = lifecycleOwner.lifecycle.currentState
if (currentState.isAtLeast(Lifecycle.State.RESUMED)) {
requestRefresh()
} else {
Log.d(
TAG,
"Cannot run credential refresh in current lifecycle state: $currentState"
)
}
}
}
}