Skip to content

Commit

Permalink
Feat: Update OverviewActivity.java interface with missing functions (
Browse files Browse the repository at this point in the history
…#653)

Fixes # <issue>

## Proposed Changes

- Adds uninstall button
- Adds setting for auto-update
- Updates preferences to properly save autorun settings
  • Loading branch information
aanorbel authored Feb 13, 2024
1 parent 5feeaab commit bd68a91
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.common.ReadMorePlugin;
import org.openobservatory.ooniprobe.databinding.ActivityOverviewBinding;
import org.openobservatory.ooniprobe.model.database.InstalledDescriptor;
import org.openobservatory.ooniprobe.model.database.Result;
import org.openobservatory.ooniprobe.model.database.TestDescriptor;

import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.inject.Inject;
Expand Down Expand Up @@ -76,7 +79,20 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
binding.desc.setTextDirection(View.TEXT_DIRECTION_RTL);
}
} else {
markwon.setMarkdown(binding.desc, descriptor.getDescription());
if (descriptor instanceof InstalledDescriptor) {
TestDescriptor testDescriptor = ((InstalledDescriptor) descriptor).getTestDescriptor();
markwon.setMarkdown(
binding.desc,
String.format(
"Created by %s on %s\n\n%s",
testDescriptor.getAuthor(),
new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH).format(testDescriptor.getDescriptorCreationTime()),
descriptor.getDescription()
)
);
} else {
markwon.setMarkdown(binding.desc, descriptor.getDescription());
}
}
Result lastResult = Result.getLastResult(descriptor.getName());
if (lastResult == null) {
Expand Down Expand Up @@ -119,6 +135,18 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
binding.expandableListView.expandGroup(i);
}

if (descriptor instanceof InstalledDescriptor) {
binding.uninstallLink.setVisibility(View.VISIBLE);
binding.automaticUpdatesContainer.setVisibility(View.VISIBLE);
binding.automaticUpdatesSwitch.setChecked(((InstalledDescriptor) descriptor).getTestDescriptor().isAutoUpdate());
} else {
binding.uninstallLink.setVisibility(View.GONE);
/**
* We need to set the height to 0 because the layout is broken when the view is gone
*/
binding.automaticUpdatesContainer.getLayoutParams().height = 0;
}

setUpOnCLickListeners();
}

Expand Down Expand Up @@ -149,6 +177,8 @@ public void setThemeColor(int color) {

private void setUpOnCLickListeners() {
binding.customUrl.setOnClickListener(view -> customUrlClick());
binding.uninstallLink.setOnClickListener(view -> viewModel.uninstallLinkClicked(this, (InstalledDescriptor) descriptor));
binding.automaticUpdatesSwitch.setOnCheckedChangeListener((compoundButton, isChecked) -> viewModel.automaticUpdatesSwitchClicked(isChecked));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class AddDescriptorActivity : AbstractActivity() {
when (item.itemId) {
R.id.add_descriptor -> {
viewModel.onAddButtonClicked(
selectedNettest = adapter.nettests.filter { it.selected },
disabledAutorunNettests = adapter.nettests.filter { it.selected },
automatedUpdates = binding.automaticUpdatesSwitch.isChecked
)
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,16 @@ class AddDescriptorViewModel constructor(
/**
* This method is called when the "Add Link" button is clicked.
* It adds the descriptor to the descriptor manager and signals that the activity should finish.
* @param selectedNettest is the list of selected nettests.
* @param disabledAutorunNettests is the list of disabled nettests for autorun.
* @param automatedUpdates is a boolean indicating whether automated updates should be enabled.
*/
fun onAddButtonClicked(selectedNettest: List<GroupedItem>, automatedUpdates: Boolean) {
fun onAddButtonClicked(disabledAutorunNettests: List<GroupedItem>, automatedUpdates: Boolean) {
descriptor.value?.let { descriptor ->
descriptorManager.addDescriptor(
descriptor = descriptor.apply {
isAutoUpdate = automatedUpdates
nettests = selectedNettest.filter { it.selected }.map { nettest ->
OONIRunNettest(
name = nettest.name,
inputs = nettest.inputs
)
}
},
disabledAutorunNettests = disabledAutorunNettests
).also {
finishActivity()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package org.openobservatory.ooniprobe.activity.overview
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.openobservatory.engine.BaseNettest
import org.openobservatory.ooniprobe.activity.AbstractActivity
import org.openobservatory.ooniprobe.activity.OverviewActivity
import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel
import org.openobservatory.ooniprobe.common.AbstractDescriptor
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.common.TestDescriptorManager
import org.openobservatory.ooniprobe.common.disableTest
import org.openobservatory.ooniprobe.common.enableTest
import org.openobservatory.ooniprobe.model.database.InstalledDescriptor
import javax.inject.Inject

class TestGroupItem(
Expand All @@ -18,10 +22,12 @@ class TestGroupItem(
class OverviewViewModel() : ViewModel() {
var descriptor: MutableLiveData<AbstractDescriptor<BaseNettest>> = MutableLiveData()
lateinit var preferenceManager: PreferenceManager
lateinit var descriptorManager: TestDescriptorManager

@Inject
constructor(preferenceManager: PreferenceManager) : this() {
constructor(preferenceManager: PreferenceManager, descriptorManager: TestDescriptorManager) : this() {
this.preferenceManager = preferenceManager
this.descriptorManager = descriptorManager
}


Expand Down Expand Up @@ -73,6 +79,18 @@ class OverviewViewModel() : ViewModel() {
this.descriptor.postValue(descriptor)
}

fun uninstallLinkClicked(activity: AbstractActivity, descriptor: InstalledDescriptor) {
descriptorManager.delete(descriptor)
activity.finish()
}

fun automaticUpdatesSwitchClicked(isChecked: Boolean) {
descriptor.value?.let {
it.descriptor?.isAutoUpdate = isChecked
it.descriptor?.save()
}
}

companion object {
const val SELECT_ALL = "SELECT_ALL"
const val SELECT_SOME = "SELECT_SOME"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ abstract class AbstractDescriptor<T : BaseNettest>(
* @return String representing the preference prefix.
*/
fun preferencePrefix(): String {
return OONITests.values().find { it.label == name }?.let { "" } ?: "descriptor_id_"
return when (descriptor?.runId != null) {
true -> descriptor?.preferencePrefix() ?: ""
else -> ""
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import org.openobservatory.engine.BaseNettest
import org.openobservatory.engine.LoggerArray
import org.openobservatory.engine.OONIRunFetchResponse
import org.openobservatory.ooniprobe.BuildConfig
import org.openobservatory.ooniprobe.activity.adddescriptor.adapter.GroupedItem
import org.openobservatory.ooniprobe.model.database.InstalledDescriptor
import org.openobservatory.ooniprobe.model.database.Result
import org.openobservatory.ooniprobe.model.database.Result_Table
import org.openobservatory.ooniprobe.model.database.TestDescriptor
import org.openobservatory.ooniprobe.model.database.TestDescriptor_Table
import org.openobservatory.ooniprobe.model.database.Url
Expand All @@ -20,7 +24,10 @@ import javax.inject.Singleton
* This class is responsible for managing the test descriptors
*/
@Singleton
class TestDescriptorManager @Inject constructor(private val context: Context) {
class TestDescriptorManager @Inject constructor(
private val context: Context,
private val preferenceManager: PreferenceManager
) {
private val descriptors: List<OONIDescriptor<BaseNettest>> = ooniDescriptors(context)

fun getDescriptors(): List<OONIDescriptor<BaseNettest>> {
Expand Down Expand Up @@ -72,17 +79,42 @@ class TestDescriptorManager @Inject constructor(private val context: Context) {
)
}

fun addDescriptor(descriptor: TestDescriptor): Boolean {
fun addDescriptor(
descriptor: TestDescriptor,
disabledAutorunNettests: List<GroupedItem>
): Boolean {
descriptor.getNettests()
.filter { it.name == WebConnectivity.NAME }
.forEach {
it.inputs?.forEach { url -> Url.checkExistingUrl(url) }
}
descriptor.getNettests().map { it.name }
.filter { item -> disabledAutorunNettests.map { it.name }.contains(item) }
.forEach { item ->
preferenceManager.enableTest(
name = item,
prefix = descriptor.preferencePrefix(),
autoRun = true
)
}
return descriptor.save()
}

fun getRunV2Descriptors(): List<TestDescriptor> {
return SQLite.select().from(TestDescriptor::class.java)
.where(TestDescriptor_Table.isArchived.eq(false)).queryList()
}

fun delete(descriptor: InstalledDescriptor): Boolean {
preferenceManager.sp.all.entries.forEach {entry ->
if (entry.key.contains(descriptor.testDescriptor.runId.toString())) {
preferenceManager.sp.edit().remove(entry.key).apply()
}
}
return SQLite.select().from(Result::class.java)
.where(Result_Table.descriptor_runId.eq(descriptor.testDescriptor.runId))
.queryList().forEach { it.delete(context) }.run {
descriptor.testDescriptor.delete()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ class TestDescriptor(
var translationCreationTime: Date? = null,
@Column(typeConverter = NettestConverter::class)
var nettests: Any = emptyList<OONIRunNettest>()
) : BaseModel(), Serializable
) : BaseModel(), Serializable {
fun preferencePrefix(): String {
return "${runId}_"
}
}

private const val DESCRIPTOR_TEST_NAME = "ooni_run"
class InstalledDescriptor(
Expand Down
45 changes: 42 additions & 3 deletions app/src/main/res/layout/activity_overview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,34 @@
android:layout_height="wrap_content"
android:paddingVertical="32dp" />

<LinearLayout
android:id="@+id/automatic_updates_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:layout_below="@+id/desc">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Install updates automatically" />

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/automatic_updates_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/run_automatically"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/desc"
android:layout_alignBottom="@id/desc"
android:layout_alignEnd="@id/automatic_updates_container"
android:layout_alignBottom="@id/automatic_updates_container"
android:text="Run automatically" />

<LinearLayout
Expand Down Expand Up @@ -165,7 +187,7 @@
</LinearLayout>
</RelativeLayout>

<ExpandableListView
<org.openobservatory.ooniprobe.common.views.CustomExpandableListView
android:id="@+id/expandable_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -175,6 +197,23 @@
android:paddingHorizontal="16dp"
tools:listitem="@layout/overview_test_group_list_item"
app:layout_constraintTop_toBottomOf="@id/header" />
<Button
android:id="@+id/uninstall_link"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uninstall Link"
android:textAllCaps="false"
android:textColor="@color/color_red9"
app:layout_constraintTop_toBottomOf="@id/expandable_list_view"
app:layout_constraintStart_toStartOf="parent"
app:cornerRadius="24dp"
app:rippleColor="@color/ripple_material_dark"
app:strokeColor="@color/color_red9"
android:layout_marginVertical="16dp"
android:layout_marginStart="16dp"
android:visibility="gone"
tools:visibility="visible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

Expand Down

0 comments on commit bd68a91

Please sign in to comment.