Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Feb 9, 2024
1 parent 706af9b commit 5284b9b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Stores/src/main/java/AppUpdateWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.google.common.util.concurrent.ListenableFuture
import com.infomaniak.lib.core.utils.SentryLog
import com.infomaniak.lib.stores.updatemanagers.WorkerUpdateManager
import io.sentry.Sentry
import io.sentry.SentryLevel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -65,7 +66,7 @@ class AppUpdateWorker(appContext: Context, params: WorkerParameters) : Listenabl
.setConstraints(Constraints.Builder().setRequiresBatteryNotLow(true).build())
// We start with a delayed duration, so that when the app quickly come back to foreground because the user
// was just switching apps, the service is not launched
.setInitialDelay(INITIAL_DELAY, TimeUnit.SECONDS)
.setInitialDelay(INITIAL_DELAY_SECONDS, TimeUnit.SECONDS)
.build()

workManager.enqueueUniqueWork(TAG, ExistingWorkPolicy.KEEP, workRequest)
Expand All @@ -74,21 +75,28 @@ class AppUpdateWorker(appContext: Context, params: WorkerParameters) : Listenabl

suspend fun cancelWorkIfNeeded() = withContext(Dispatchers.IO) {

val workInfo = workManager.getWorkInfos(
val workInfos = workManager.getWorkInfos(
WorkQuery.Builder.fromUniqueWorkNames(listOf(TAG))
.addStates(listOf(WorkInfo.State.BLOCKED, WorkInfo.State.ENQUEUED))
.build(),
).get()

workInfo.forEach {
workManager.cancelWorkById(it.id)
workInfos.forEachIndexed { index, workInfo ->
workManager.cancelWorkById(workInfo.id)
SentryLog.d(TAG, "Work cancelled")
// TODO: Check this Sentry in approximately 1 month (end of March 2024) to know if the `forEach` is useful or not.
if (index > 0) {
Sentry.withScope { scope ->
scope.level = SentryLevel.WARNING
Sentry.captureMessage("There is more than one work infos, we must keep forEach")
}
}
}
}
}

companion object {
private const val TAG = "AppUpdateWorker"
private const val INITIAL_DELAY = 10L // 10s
private const val INITIAL_DELAY_SECONDS = 10L
}
}

0 comments on commit 5284b9b

Please sign in to comment.