From 13677052fc0a2de7e55ed60687dcd522b39bf10d Mon Sep 17 00:00:00 2001 From: Dmitry Muhomor Date: Thu, 18 Jul 2024 16:25:06 +0300 Subject: [PATCH] add workaround for update check job being started before VPN is up --- .../apps/autoupdate/UpdateCheckJob.kt | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/app/grapheneos/apps/autoupdate/UpdateCheckJob.kt b/app/src/main/java/app/grapheneos/apps/autoupdate/UpdateCheckJob.kt index e0d4b1da..3cdf20dc 100644 --- a/app/src/main/java/app/grapheneos/apps/autoupdate/UpdateCheckJob.kt +++ b/app/src/main/java/app/grapheneos/apps/autoupdate/UpdateCheckJob.kt @@ -24,13 +24,17 @@ import app.grapheneos.apps.util.componentName import app.grapheneos.apps.util.isAppInstallationAllowed import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlin.time.Duration.Companion.seconds private const val TAG = "UpdateCheckJob" class UpdateCheckJob : JobService() { + private var job: Job? = null - override fun onStartJob(jobParams: JobParameters?): Boolean { + override fun onStartJob(jobParams: JobParameters): Boolean { ApplicationImpl.exitIfNotInitialized() Log.d(TAG, "onStartJob") @@ -38,7 +42,12 @@ class UpdateCheckJob : JobService() { return false } - CoroutineScope(Dispatchers.Main).launch { + job = CoroutineScope(Dispatchers.Main).launch { + // When there's an enabled VPN, this job is sometimes executed before VPN establishes + // its connection, which makes the job fail due to network not being up at that point. + // As a workaround, delay the network request for a bit. + delay(10.seconds) + val repoUpdateError = PackageStates.requestRepoUpdate() if (repoUpdateError != null) { showUpdateCheckFailedNotification(repoUpdateError) @@ -62,8 +71,10 @@ class UpdateCheckJob : JobService() { } override fun onStopJob(params: JobParameters): Boolean { - // ignore the stop signal: update check is inexpensive and doesn't have relevant cancellation points - return false + Log.d(TAG, "onStopJob, reason: ${params.stopReason}") + job?.cancel() + // "true" means "reschedule the job" + return true } private fun showUpdatesAvailableNotification(rPackageGroups: List>) {