Skip to content

Commit

Permalink
fix: OONI cards not enabled by default. (#654)
Browse files Browse the repository at this point in the history
Fixes OONI cards not enabled by default.

## Proposed Changes

  - Set default value for ooni tests to enabled.
  • Loading branch information
aanorbel authored Jan 26, 2024
1 parent 6c0dd75 commit b432281
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@ open class OONIDescriptor<T : BaseNettest>(
) : Serializable, BaseDescriptor<T>(name = name, nettests = nettests) {

/**
* Checks if any of the nettests are enabled based on the preferences stored in the provided [PreferenceManager].
* This function is used to determine if the current descriptor is enabled.
* If the descriptor is [OONITests.EXPERIMENTAL], this function returns the preference value stored for the `experimental` preference key.
* If the descriptor is [OONITests.WEBSITES], this function returns true if at least one category is enabled in the preferences.
* Otherwise, it checks if any of the nettests are enabled based on the preferences stored in the provided [PreferenceManager].
*
* @param preferenceManager The [PreferenceManager] instance used to resolve the status of each nettest.
* @return Boolean Returns true if at least one nettest is enabled, false otherwise.
*/
fun isEnabled(preferenceManager: PreferenceManager): Boolean {
return nettests.any {
preferenceManager.resolveStatus(
name = it.name,
prefix = preferencePrefix(),
)
return when (name) {
OONITests.EXPERIMENTAL.label -> preferenceManager.isExperimentalOn
OONITests.WEBSITES.label -> preferenceManager.countEnabledCategory() > 0
else -> nettests.any {
preferenceManager.resolveStatus(
name = it.name,
prefix = preferencePrefix(),
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ fun PreferenceManager.resolveStatus(
resolveStatus(name = name, prefix = prefix)
)
} else {
return sp.getBoolean(getPreferenceKey(name = name, prefix = prefix), false)
/**
* If the preference key does not exist, we return the default value for the test.
* This is needed because ooni provided tests will not be explicitly enabled by default.
*
* However, we want to show them as enabled in the UI.
*
* Using the **[OONIDescriptor.preferencePrefix]** as an identifier, we can determine if the test is an ooni test(prefix is blank)
* and set the default value accordingly.
*
* The prefix is blank for ooni tests because they do not have a descriptor id.
* Additionally, for backward compatibility, the preference key for ooni tests
* is a lookup from **[PreferenceManager.getPreferenceKey]** with the test name.
*/
return sp.getBoolean(getPreferenceKey(name = name, prefix = prefix), prefix.isBlank())
}
}

Expand Down

0 comments on commit b432281

Please sign in to comment.