Skip to content

Commit 6212bed

Browse files
authored
feat: only auto-update descriptors if installed (#655)
1 parent 95b4cd7 commit 6212bed

File tree

7 files changed

+74
-2
lines changed

7 files changed

+74
-2
lines changed

composeApp/src/androidMain/kotlin/org/ooni/probe/AndroidApplication.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class AndroidApplication : Application() {
5555
startSingleRunInner = appWorkerManager::startSingleRun,
5656
configureAutoRun = appWorkerManager::configureAutoRun,
5757
configureDescriptorAutoUpdate = appWorkerManager::configureDescriptorAutoUpdate,
58+
cancelDescriptorAutoUpdate = appWorkerManager::cancelDescriptorAutoUpdate,
5859
startDescriptorsUpdate = appWorkerManager::startDescriptorsUpdate,
5960
launchAction = ::launchAction,
6061
batteryOptimization = batteryOptimization,

composeApp/src/androidMain/kotlin/org/ooni/probe/background/AppWorkerManager.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class AppWorkerManager(
5353
}
5454
}
5555

56+
suspend fun cancelDescriptorAutoUpdate(): Boolean {
57+
return withContext(backgroundDispatcher) {
58+
workManager.cancelUniqueWork(DescriptorUpdateWorker.AutoUpdateWorkerName)
59+
true
60+
}
61+
}
62+
5663
suspend fun configureDescriptorAutoUpdate(): Boolean {
5764
return withContext(backgroundDispatcher) {
5865
val request = PeriodicWorkRequestBuilder<DescriptorUpdateWorker>(1, TimeUnit.DAYS)

composeApp/src/commonMain/kotlin/org/ooni/probe/App.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ fun App(
9898
LaunchedEffect(Unit) {
9999
dependencies.bootstrapTestDescriptors()
100100
dependencies.bootstrapPreferences()
101-
dependencies.configureDescriptorAutoUpdate()
102-
dependencies.startDescriptorsUpdate(null)
103101
dependencies.startSingleRunInner(RunSpecification.OnlyUploadMissingResults)
104102
}
103+
LaunchedEffect(Unit) {
104+
dependencies.observeAndConfigureAutoUpdate()
105+
}
105106
LaunchedEffect(Unit) {
106107
dependencies.finishInProgressData()
107108
}

composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import org.ooni.probe.domain.GetSettings
6161
import org.ooni.probe.domain.GetStorageUsed
6262
import org.ooni.probe.domain.MarkJustFinishedTestAsSeen
6363
import org.ooni.probe.domain.ObserveAndConfigureAutoRun
64+
import org.ooni.probe.domain.ObserveAndConfigureAutoUpdate
6465
import org.ooni.probe.domain.RunBackgroundStateManager
6566
import org.ooni.probe.domain.RunDescriptors
6667
import org.ooni.probe.domain.RunNetTest
@@ -119,6 +120,7 @@ class Dependencies(
119120
val startSingleRunInner: (RunSpecification) -> Unit,
120121
private val configureAutoRun: suspend (AutoRunParameters) -> Unit,
121122
val configureDescriptorAutoUpdate: suspend () -> Boolean,
123+
val cancelDescriptorAutoUpdate: suspend () -> Boolean,
122124
val startDescriptorsUpdate: suspend (List<InstalledTestDescriptorModel>?) -> Unit,
123125
val localeDirection: (() -> LayoutDirection)? = null,
124126
private val isWebViewAvailable: () -> Boolean,
@@ -347,6 +349,17 @@ class Dependencies(
347349
getAutoRunSettings = getAutoRunSettings::invoke,
348350
)
349351
}
352+
353+
val observeAndConfigureAutoUpdate by lazy {
354+
ObserveAndConfigureAutoUpdate(
355+
backgroundContext = backgroundContext,
356+
listAllInstalledTestDescriptors = testDescriptorRepository::listAll,
357+
configureDescriptorAutoUpdate = configureDescriptorAutoUpdate,
358+
cancelDescriptorAutoUpdate = cancelDescriptorAutoUpdate,
359+
startDescriptorsUpdate = startDescriptorsUpdate,
360+
)
361+
}
362+
350363
private val rejectDescriptorUpdate by lazy {
351364
RejectDescriptorUpdate(
352365
updateDescriptorRejectedRevision = testDescriptorRepository::updateRejectedRevision,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.ooni.probe.domain
2+
3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.flow.Flow
5+
import kotlinx.coroutines.flow.distinctUntilChanged
6+
import kotlinx.coroutines.flow.launchIn
7+
import kotlinx.coroutines.flow.map
8+
import kotlinx.coroutines.flow.onEach
9+
import org.ooni.probe.data.models.InstalledTestDescriptorModel
10+
import kotlin.coroutines.CoroutineContext
11+
12+
class ObserveAndConfigureAutoUpdate(
13+
private val backgroundContext: CoroutineContext,
14+
private val listAllInstalledTestDescriptors: () -> Flow<List<InstalledTestDescriptorModel>>,
15+
private val configureDescriptorAutoUpdate: suspend () -> Boolean,
16+
private val cancelDescriptorAutoUpdate: suspend () -> Boolean,
17+
private val startDescriptorsUpdate: suspend (List<InstalledTestDescriptorModel>?) -> Unit,
18+
) {
19+
operator fun invoke() =
20+
listAllInstalledTestDescriptors()
21+
.map { it.isNotEmpty() }
22+
.distinctUntilChanged()
23+
.onEach { enabled ->
24+
if (enabled) {
25+
configureDescriptorAutoUpdate()
26+
startDescriptorsUpdate(null)
27+
} else {
28+
cancelDescriptorAutoUpdate()
29+
}
30+
}
31+
.launchIn(CoroutineScope(backgroundContext))
32+
}

composeApp/src/desktopMain/kotlin/org/ooni/probe/BuildDependencies.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ val dependencies = Dependencies(
4343
startSingleRunInner = ::startSingleRun,
4444
configureAutoRun = ::configureAutoRun,
4545
configureDescriptorAutoUpdate = ::configureDescriptorAutoUpdate,
46+
cancelDescriptorAutoUpdate = ::cancelDescriptorAutoUpdate,
4647
startDescriptorsUpdate = ::startDescriptorsUpdate,
4748
launchAction = ::launchAction,
4849
batteryOptimization = object : BatteryOptimization {},
@@ -93,6 +94,11 @@ private fun configureDescriptorAutoUpdate(): Boolean {
9394
return true
9495
}
9596

97+
private fun cancelDescriptorAutoUpdate(): Boolean {
98+
// TODO: Desktop - background running
99+
return true
100+
}
101+
96102
private fun startDescriptorsUpdate(descriptors: List<InstalledTestDescriptorModel>?) {
97103
// TODO: Desktop - background running
98104
CoroutineScope(Dispatchers.IO).launch {

composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class SetupDependencies(
8888
startSingleRunInner = ::startSingleRun,
8989
configureAutoRun = ::configureAutoRun,
9090
configureDescriptorAutoUpdate = ::configureDescriptorAutoUpdate,
91+
cancelDescriptorAutoUpdate = ::cancelDescriptorAutoUpdate,
9192
startDescriptorsUpdate = ::startDescriptorsUpdate,
9293
localeDirection = ::localeDirection,
9394
launchAction = ::launchAction,
@@ -294,6 +295,17 @@ class SetupDependencies(
294295
} ?: false
295296
}
296297

298+
private fun cancelDescriptorAutoUpdate(): Boolean {
299+
Logger.d("Cancelling descriptor auto update")
300+
return try {
301+
BGTaskScheduler.sharedScheduler.cancelTaskRequestWithIdentifier(OrganizationConfig.updateDescriptorTaskId)
302+
true
303+
} catch (e: Exception) {
304+
Logger.e(e) { "Failed to cancel descriptor auto update" }
305+
false
306+
}
307+
}
308+
297309
private fun configureDescriptorAutoUpdate(): Boolean {
298310
Logger.d("Configuring descriptor auto update")
299311
return BGTaskScheduler.sharedScheduler.submitTaskRequest(

0 commit comments

Comments
 (0)