Skip to content

Commit

Permalink
feat: disable ndt and dash in background run (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel authored Dec 18, 2024
1 parent f88eea6 commit c26ff4b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import kotlin.time.times
sealed class TestType {
abstract val name: String
abstract val labelRes: StringResource
open val isManualRunEnabled: Boolean = true
open val isBackgroundRunEnabled: Boolean = true
open val iconRes: DrawableResource? = null
open val url: String? = null

Expand All @@ -52,13 +54,15 @@ sealed class TestType {
data object Dash : TestType() {
override val name: String = "dash"
override val labelRes: StringResource = Res.string.Test_Dash_Fullname
override val isBackgroundRunEnabled: Boolean = false
override val url: String = "https://ooni.org/nettest/dash"

override fun runtime(inputs: List<String>?) = 45.seconds
}

data class Experimental(
override val name: String,
override val isBackgroundRunEnabled: Boolean = false,
) : TestType() {
override val labelRes: StringResource = Res.string.Test_Experimental_Fullname
override val iconRes: DrawableResource = Res.drawable.test_experimental
Expand Down Expand Up @@ -94,6 +98,7 @@ sealed class TestType {
data object Ndt : TestType() {
override val name: String = "ndt"
override val labelRes: StringResource = Res.string.Test_NDT_Fullname
override val isBackgroundRunEnabled: Boolean = false
override val url: String = "https://ooni.org/nettest/ndt"

override fun runtime(inputs: List<String>?) = 45.seconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class PreferenceRepository(
isAutoRun: Boolean,
): Flow<Boolean> =
dataStore.data.map {
it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] == true
it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] ?: netTest.defaultPreferenceValue(isAutoRun)
}.distinctUntilChanged()

fun areNetTestsEnabled(
Expand All @@ -157,7 +157,7 @@ class PreferenceRepository(
dataStore.data.map {
list.associate { (descriptor, netTest) ->
Pair(descriptor, netTest) to (
it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] == true
it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] ?: netTest.defaultPreferenceValue(isAutoRun)
)
}
}.distinctUntilChanged()
Expand All @@ -174,6 +174,17 @@ class PreferenceRepository(
}
}

suspend fun setAreNetTestsEnabled(
list: List<Pair<Descriptor, NetTest>>,
isAutoRun: Boolean,
) {
dataStore.edit {
list.forEach { (descriptor, netTest) ->
it[booleanPreferencesKey(getNetTestKey(descriptor, netTest, isAutoRun))] = netTest.defaultPreferenceValue(isAutoRun)
}
}
}

private fun getNetTestKey(
descriptor: Descriptor,
netTest: NetTest,
Expand Down Expand Up @@ -210,4 +221,8 @@ class PreferenceRepository(
return dataStore.data.map { it.contains(preferenceKeyFromSettingsKey(key).preferenceKey) }
.firstOrNull() ?: false
}

private fun NetTest.defaultPreferenceValue(isAutoRun: Boolean): Boolean {
return if (isAutoRun) test.isBackgroundRunEnabled else test.isManualRunEnabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ class BootstrapPreferences(
preferencesRepository.setAreNetTestsEnabled(
list = allTests,
isAutoRun = false,
isEnabled = true,
)
preferencesRepository.setAreNetTestsEnabled(
list = allTests,
isAutoRun = true,
isEnabled = true,
)

preferencesRepository.setValuesByKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,28 @@ class GetDefaultTestDescriptors {
animation = Animation.Experimental,
dataUsage = Res.string.TestResults_NotAvailable,
netTests = listOf(
NetTest(TestType.Experimental("stunreachability")),
NetTest(TestType.Experimental("openvpn")),
NetTest(
TestType.Experimental(
name = "stunreachability",
isBackgroundRunEnabled = true,
),
),
NetTest(
TestType.Experimental(
name = "openvpn",
isBackgroundRunEnabled = true,
),
),
// NetTest(TestType.Experimental("riseupvpn")),
// NetTest(TestType.Experimental("echcheck")),
),
longRunningTests = listOf(
// NetTest(TestType.Experimental("torsf")),
NetTest(TestType.Experimental("vanilla_tor")),
NetTest(
TestType.Experimental(
name = "vanilla_tor",
),
),
),
)
}
Expand Down

0 comments on commit c26ff4b

Please sign in to comment.