Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable ndt and dash in background run #353

Merged
merged 1 commit into from
Dec 18, 2024
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 @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're already bootstrapping the preferences correctly, why do we the default here? That's a different approach from the other defaults we have. I feel we can skip this, and just have them set on BootstrapPreferences, but maybe I'm missing something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We bootstrap references in multiplatform but the android app has default preferences that are not available on disk untill the user changes them.

}.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 @@ -194,7 +194,7 @@ class SetupDependencies(
}
}

private fun presentViewController(uiViewController: UIViewController): Boolean {
private fun presentViewController(uiViewController: UIViewController): Boolean {
return findCurrentViewController()?.let { viewController ->

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
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