Skip to content

Commit

Permalink
Updated suite to run from BaseDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Nov 2, 2023
1 parent 51f350e commit 1461e29
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View.GONE
import android.view.View.VISIBLE
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.activity.AbstractActivity
import org.openobservatory.ooniprobe.activity.RunningActivity
import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Companion.NOT_SELECT_ANY
import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Companion.SELECT_ALL
import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Companion.SELECT_SOME
Expand All @@ -17,6 +16,8 @@ import org.openobservatory.ooniprobe.activity.runtests.models.GroupItem
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.databinding.ActivityRunTestsBinding
import org.openobservatory.ooniprobe.test.suite.AbstractSuite
import org.openobservatory.ooniprobe.test.suite.DynamicTestSuite
import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite
import java.io.Serializable
import javax.inject.Inject

Expand Down Expand Up @@ -53,29 +54,42 @@ class RunTestsActivity : AbstractActivity() {
var testSuites: List<AbstractSuite>? = intent.extras?.getParcelableArrayList(TESTS)
testSuites?.let { ts ->
val tsGroups: List<GroupItem> = ts.map { testSuite ->
GroupItem(
selected = false,
name = testSuite.name,
nettests = testSuite.getTestList(preferenceManager).map { nettest ->
ChildItem(
selected = preferenceManager.resolveStatus(nettest.name),
name = nettest.name,
inputs = nettest.inputs
)
})
if (testSuite is ExperimentalSuite) {
return@map GroupItem(
selected = false,
name = testSuite.name,
nettests = testSuite.getTestList(preferenceManager).map { nettest ->
ChildItem(
selected = preferenceManager.isExperimentalOn,
name = nettest.name,
inputs = nettest.inputs
)
})
} else {
return@map GroupItem(
selected = false,
name = testSuite.name,
nettests = testSuite.getTestList(preferenceManager).map { nettest ->
ChildItem(
selected = preferenceManager.resolveStatus(nettest.name),
name = nettest.name,
inputs = nettest.inputs
)
})
}
}

mAdapter = RunTestsExpandableListViewAdapter(this, tsGroups, viewModel)

binding.expandableListView.setAdapter(mAdapter)
binding.selectAll.setOnClickListener {
viewModel.selectedAllBtnStatus.value = SELECT_ALL
viewModel.setSelectedAllBtnStatus(SELECT_ALL)
mAdapter.notifyDataSetChanged()
updateStatusIndicator()
}

binding.selectNone.setOnClickListener {
viewModel.selectedAllBtnStatus.value = NOT_SELECT_ANY
viewModel.setSelectedAllBtnStatus(NOT_SELECT_ANY)
mAdapter.notifyDataSetChanged()
updateStatusIndicator()
}
Expand All @@ -88,11 +102,13 @@ class RunTestsActivity : AbstractActivity() {
binding.selectNone.isActivated = true
binding.selectAll.isActivated = false
}

NOT_SELECT_ANY -> {

binding.selectNone.isActivated = true
binding.selectAll.isActivated = false
}

SELECT_SOME -> {
binding.selectNone.isActivated = true
binding.selectAll.isActivated = true
Expand All @@ -102,6 +118,42 @@ class RunTestsActivity : AbstractActivity() {
updateStatusIndicator()
}
}

binding.bottomBar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.runButton -> {
val selectedChildItems: List<String> = getChildItemsSelectedIdList()
if (selectedChildItems.isNotEmpty()) {
RunningActivity.runAsForegroundService(
this@RunTestsActivity,
ArrayList(getGroupItemsAtLeastOneChildEnabled().map {
val testSuite = AbstractSuite.getTestSuiteByName(it.name)
return@map DynamicTestSuite(
name = testSuite.name,
title = testSuite.title,
cardDesc = testSuite.cardDesc,
icon = testSuite.icon,
icon_24 = testSuite.iconGradient,
color = testSuite.color,
themeLight = testSuite.themeLight,
themeDark = testSuite.themeDark,
desc1 = testSuite.desc1,
anim = testSuite.anim,
dataUsage = testSuite.dataUsage,
nettest = it.nettests.filter { nattest -> nattest.selected }
)
}),
{ finish() },
preferenceManager
)

}
true
}

else -> false
}
}
}

}
Expand All @@ -121,4 +173,16 @@ class RunTestsActivity : AbstractActivity() {
}
return childItemSelectedIdList
}

private fun getGroupItemsAtLeastOneChildEnabled(): List<GroupItem> {
val items: MutableList<GroupItem> = ArrayList()
for (i in 0 until mAdapter.groupCount) {
if (mAdapter.getGroup(i).nettests.any { it.selected }) {
items.add(mAdapter.getGroup(i).apply {
nettests = nettests.filter { it.selected }
})
}
}
return items
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package org.openobservatory.ooniprobe.activity.runtests
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.common.disableTest
import org.openobservatory.ooniprobe.common.enableTest
import org.openobservatory.ooniprobe.test.suite.CircumventionSuite
import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite
import org.openobservatory.ooniprobe.test.suite.InstantMessagingSuite
import org.openobservatory.ooniprobe.test.suite.PerformanceSuite
import javax.inject.Inject


Expand All @@ -22,6 +28,43 @@ class RunTestsViewModel() : ViewModel() {

fun setSelectedAllBtnStatus(selectedStatus: String) {
selectedAllBtnStatus.postValue(selectedStatus)
when (selectedStatus) {
SELECT_ALL -> {
InstantMessagingSuite().getTestList(preferenceManager).forEach {
enableTest(it.name)
}
CircumventionSuite().getTestList(preferenceManager).forEach {
enableTest(it.name)
}
PerformanceSuite().getTestList(preferenceManager).forEach {
enableTest(it.name)
}
}

NOT_SELECT_ANY -> {
InstantMessagingSuite().getTestList(preferenceManager).forEach {
disableTest(it.name)
}
CircumventionSuite().getTestList(preferenceManager).forEach {
disableTest(it.name)
}
PerformanceSuite().getTestList(preferenceManager).forEach {
disableTest(it.name)
}
}
}
}

fun disableTest(name: String) {
if (!ExperimentalSuite().getTestList(preferenceManager).map { it.name }.contains(name)) {
preferenceManager.disableTest(name)
}
}

fun enableTest(name: String) {
if (!ExperimentalSuite().getTestList(preferenceManager).map { it.name }.contains(name)) {
preferenceManager.enableTest(name)
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class RunTestsExpandableListViewAdapter(
setOnClickListener {
if (childItem.selected) {
childItem.selected = false
mViewModel.disableTest(childItem.name)
if (isNotSelectedAnyChildItems(groupItem.nettests)) {
groupItem.selected = false
}
Expand All @@ -149,6 +150,7 @@ class RunTestsExpandableListViewAdapter(
}
} else {
childItem.selected = true
mViewModel.enableTest(childItem.name)
groupItem.selected = true
if (isSelectedAllItems(mGroupListData)) {
mViewModel.setSelectedAllBtnStatus(SELECT_ALL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package org.openobservatory.ooniprobe.activity.runtests.models
import org.openobservatory.engine.BaseDescriptor
import org.openobservatory.engine.BaseNettest

data class ChildItem(
class ChildItem(
var selected: Boolean,
var name: String,
var inputs: List<String>?
)
override var name: String,
override var inputs: List<String>?
) : BaseNettest(name = name, inputs = inputs)

data class GroupItem(
class GroupItem(
var selected: Boolean,
var name: String,
var nettests: List<ChildItem>
)
override var name: String,
override var nettests: List<ChildItem>
) : BaseDescriptor<ChildItem>(name = name, nettests = nettests)
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class PreferenceManager {
public static final int COUNT_WEBSITE_CATEGORIES = 31;
public static final int ASK_UPDATE_APP = 16;

private final SharedPreferences sp;
private final Resources r;
final SharedPreferences sp;
final Resources r;

@Inject PreferenceManager(Context context) {
androidx.preference.PreferenceManager.setDefaultValues(context, R.xml.preferences_global, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.openobservatory.ooniprobe.common

import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.test.test.*


fun PreferenceManager.enableTest(name: String) {
this.setValue(name, true)
}

fun PreferenceManager.disableTest(name: String) {
this.setValue(name, false)
}

fun PreferenceManager.setValue(name: String, value: Boolean) {
when (name) {
WebConnectivity.NAME -> {}
Dash.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.run_dash), value)
apply()
}
}

FacebookMessenger.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.test_facebook_messenger), value)
apply()
}
}

HttpHeaderFieldManipulation.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.run_http_header_field_manipulation), value)
apply()
}
}

HttpInvalidRequestLine.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.run_http_invalid_request_line), value)
apply()
}
}

Ndt.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.run_ndt), value)
apply()
}
}

Psiphon.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.test_psiphon), value)
apply()
}
}

RiseupVPN.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.test_riseupvpn), value)
apply()
}
}

Signal.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.test_signal), value)
apply()
}
}

Telegram.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.test_telegram), value)
apply()
}
}

Tor.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.test_tor), value)
apply()
}
}

Whatsapp.NAME -> {
with(sp.edit()) {
putBoolean(r.getString(R.string.test_whatsapp), value)
apply()
}
}

else -> {
throw IllegalArgumentException("Unknown preference for: $name")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public static CircumventionSuite initForAutoRun() {
@Override public AbstractTest[] getTestList(@Nullable PreferenceManager pm) {
if (super.getTestList(pm) == null) {
ArrayList<AbstractTest> list = new ArrayList<>();
if (pm == null || pm.isTestPsiphon())
// if (pm == null || pm.isTestPsiphon())
list.add(new Psiphon());
if (pm == null || pm.isTestTor())
// if (pm == null || pm.isTestTor())
list.add(new Tor());
super.setTestList(Lists.transform(list, test -> {
if (getAutoRun()) test.setOrigin(AbstractTest.AUTORUN);
Expand Down
Loading

0 comments on commit 1461e29

Please sign in to comment.