Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import co.touchlab.kermit.Logger
import dev.dirs.ProjectDirectories
import dev.hydraulic.conveyor.control.SoftwareUpdateController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import okio.Path.Companion.toPath
import org.ooni.engine.DesktopOonimkallBridge
import org.ooni.engine.models.NetworkType
import org.ooni.probe.background.BackgroundWorkManager
import org.ooni.probe.config.BatteryOptimization
import org.ooni.probe.config.FlavorConfigInterface
import org.ooni.probe.config.OptionalFeature
import org.ooni.probe.data.buildDatabaseDriver
import org.ooni.probe.data.models.AutoRunParameters
import org.ooni.probe.data.models.BatteryState
import org.ooni.probe.data.models.InstalledTestDescriptorModel
import org.ooni.probe.data.models.PlatformAction
import org.ooni.probe.data.models.RunSpecification
import org.ooni.probe.di.Dependencies
import org.ooni.probe.shared.Platform
import org.ooni.probe.shared.PlatformInfo
Expand All @@ -31,6 +25,11 @@ import java.nio.charset.StandardCharsets

private val projectDirectories = ProjectDirectories.from("org", "OONI", "Probe")

private val backgroundWorkManager: BackgroundWorkManager = BackgroundWorkManager(
runBackgroundTaskProvider = { dependencies.runBackgroundTask },
getDescriptorUpdateProvider = { dependencies.getDescriptorUpdate },
)

val dependencies = Dependencies(
platformInfo = buildPlatformInfo(),
oonimkallBridge = DesktopOonimkallBridge(),
Expand All @@ -41,11 +40,11 @@ val dependencies = Dependencies(
networkTypeFinder = ::networkTypeFinder,
buildDataStore = ::buildDataStore,
getBatteryState = { BatteryState.Unknown },
startSingleRunInner = ::startSingleRun,
configureAutoRun = ::configureAutoRun,
configureDescriptorAutoUpdate = ::configureDescriptorAutoUpdate,
cancelDescriptorAutoUpdate = ::cancelDescriptorAutoUpdate,
startDescriptorsUpdate = ::startDescriptorsUpdate,
startSingleRunInner = backgroundWorkManager::startSingleRun,
configureAutoRun = backgroundWorkManager::configureAutoRun,
configureDescriptorAutoUpdate = backgroundWorkManager::configureDescriptorAutoUpdate,
cancelDescriptorAutoUpdate = backgroundWorkManager::cancelDescriptorAutoUpdate,
startDescriptorsUpdate = backgroundWorkManager::startDescriptorsUpdate,
launchAction = ::launchAction,
batteryOptimization = object : BatteryOptimization {},
isWebViewAvailable = ::isWebViewAvailable,
Expand Down Expand Up @@ -86,40 +85,11 @@ private fun readAssetFile(path: String): String {
*/
private fun networkTypeFinder() = NetworkType.Unknown("unknown")

// TODO: Desktop - Confirm appropriate path and configuration
private fun buildDataStore() =
PreferenceDataStoreFactory.create {
projectDirectories.dataDir.toPath().resolve("probe.preferences_pb").toFile()
}

private fun startSingleRun(spec: RunSpecification) {
// TODO: Desktop - background running
CoroutineScope(Dispatchers.IO).launch {
dependencies.runBackgroundTask(spec).collect()
}
}

private fun configureAutoRun(params: AutoRunParameters) {
// TODO: Desktop - background running
}

private fun configureDescriptorAutoUpdate(): Boolean {
// TODO: Desktop - background running
return true
}

private fun cancelDescriptorAutoUpdate(): Boolean {
// TODO: Desktop - background running
return true
}

private fun startDescriptorsUpdate(descriptors: List<InstalledTestDescriptorModel>?) {
// TODO: Desktop - background running
CoroutineScope(Dispatchers.IO).launch {
dependencies.getDescriptorUpdate(descriptors.orEmpty())
}
}

private fun launchAction(action: PlatformAction): Boolean =
when (action) {
is PlatformAction.FileSharing -> shareFile(action)
Expand Down
16 changes: 0 additions & 16 deletions composeApp/src/desktopMain/kotlin/org/ooni/probe/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import io.github.kdroidfilter.platformtools.darkmodedetector.windows.setWindowsA
import io.github.vinceglb.autolaunch.AutoLaunch
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import ooniprobe.composeapp.generated.resources.Dashboard_Running_Running
Expand Down Expand Up @@ -64,14 +62,6 @@ fun main(args: Array<String>) {
autoLaunch.enable()
}

// start an hourly background task that calls startSingleRun
CoroutineScope(Dispatchers.IO).launch {
while (true) {
delay(1000 * 60 * 60)
startSingleRun()
}
}

application {
var isWindowVisible by remember { mutableStateOf(!autoLaunch.isStartedViaAutostart()) }
val deepLink by deepLinkFlow.collectAsState(null)
Expand Down Expand Up @@ -167,9 +157,3 @@ private fun RunBackgroundState.text(): String =
else ->
""
}

private fun startSingleRun() {
CoroutineScope(Dispatchers.IO).launch {
dependencies.runBackgroundTask(null).collect()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.ooni.probe.background

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.ooni.probe.data.models.AutoRunParameters
import org.ooni.probe.data.models.InstalledTestDescriptorModel
import org.ooni.probe.data.models.RunSpecification
import org.ooni.probe.domain.descriptors.FetchDescriptorsUpdates
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours

class BackgroundWorkManager(
private val coroutineContext: CoroutineContext = Dispatchers.IO,
private val runBackgroundTaskProvider: () -> RunBackgroundTask,
private val getDescriptorUpdateProvider: () -> FetchDescriptorsUpdates,
) {
private var autoRunJob: Job? = null
private var autoUpdateJob: Job? = null

fun startSingleRun(spec: RunSpecification?) {
CoroutineScope(coroutineContext).launch {
runBackgroundTaskProvider()(spec).collect()
}
}

fun startDescriptorsUpdate(descriptors: List<InstalledTestDescriptorModel>?) {
CoroutineScope(coroutineContext).launch {
getDescriptorUpdateProvider()(descriptors.orEmpty())
}
}

fun configureAutoRun(params: AutoRunParameters) {
autoRunJob?.cancel()

if (params is AutoRunParameters.Enabled) {
autoRunJob = CoroutineScope(coroutineContext + SupervisorJob()).launch {
while (true) {
delay(AUTO_RUN_PERIOD)
startSingleRun(null)
}
}
}
}

fun configureDescriptorAutoUpdate(): Boolean {
autoUpdateJob?.cancel()
autoUpdateJob = CoroutineScope(coroutineContext + SupervisorJob()).launch {
while (true) {
delay(AUTO_UPDATE_PERIOD)
startDescriptorsUpdate(null)
}
}
return true
}

fun cancelDescriptorAutoUpdate(): Boolean {
autoUpdateJob?.cancel()
autoUpdateJob = null
return true
}

companion object {
private val AUTO_RUN_PERIOD = 1.hours
private val AUTO_UPDATE_PERIOD = 1.days
}
}
Loading