Skip to content

Commit

Permalink
feat (OONI Run v2): Change backend for fetching descriptors (#713)
Browse files Browse the repository at this point in the history
## Proposed Changes

- Change the backend for fetching descriptors and refactor code
appropriately.
  • Loading branch information
aanorbel authored Apr 9, 2024
1 parent 7c9876c commit 50448c9
Show file tree
Hide file tree
Showing 17 changed files with 327 additions and 253 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ android {
buildConfigField 'String', 'BASE_SOFTWARE_NAME', '"ooniprobe-android-dev"'
resValue "string", "APP_ID", 'org.openobservatory.ooniprobe.dev'
resValue "string", "APP_NAME", "OONI Dev"
buildConfigField 'String', 'OONI_API_BASE_URL', '"https://api.dev.ooni.io"'
buildConfigField 'String', 'COUNTLY_KEY', '"e6c2cfe53e85951d50567467cef3f9fa2eab32c3"'
}
experimental {
Expand All @@ -70,6 +71,7 @@ android {
buildConfigField 'String', 'BASE_SOFTWARE_NAME', '"ooniprobe-android-experimental"'
resValue "string", "APP_ID", 'org.openobservatory.ooniprobe.experimental'
resValue "string", "APP_NAME", "OONI Exp"
buildConfigField 'String', 'OONI_API_BASE_URL', '"https://api.dev.ooni.io"'
buildConfigField 'String', 'COUNTLY_KEY', '"e6c2cfe53e85951d50567467cef3f9fa2eab32c3"'
}
fdroid {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

import javax.inject.Inject;

import io.noties.markwon.Markwon;

public class OverviewActivity extends ReviewUpdatesAbstractActivity implements ConfirmDialogFragment.OnClickListener {
public class OverviewActivity extends ReviewUpdatesAbstractActivity implements ConfirmDialogFragment.OnClickListener {
private static final String TEST = "test";

ActivityOverviewBinding binding;
Expand Down Expand Up @@ -104,10 +105,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
String.format(
"Created by %s on %s\n\n%s",
testDescriptor.getAuthor(),
new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH).format(testDescriptor.getDescriptorCreationTime()),
new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH).format(testDescriptor.getDateCreated()),
descriptor.getDescription()
)
);
if (Boolean.TRUE.equals(testDescriptor.isExpired())) {
binding.expiredTag.getRoot().setVisibility(View.VISIBLE);
}
} else {
markwon.setMarkdown(binding.desc, descriptor.getDescription());
}
Expand Down Expand Up @@ -258,13 +262,17 @@ private void onManualUpdatesFetchComplete(WorkInfo workInfo) {
if (workInfo != null) {
switch (workInfo.getState()) {
case SUCCEEDED -> {
binding.reviewUpdates.setVisibility(View.VISIBLE);
binding.reviewUpdates.setOnClickListener(view -> getReviewUpdatesLauncher().launch(
ReviewDescriptorUpdatesActivity.newIntent(
OverviewActivity.this,
workInfo.getOutputData().getString(ManualUpdateDescriptorsWorker.KEY_UPDATED_DESCRIPTORS)
)
));

String descriptor = workInfo.getOutputData().getString(ManualUpdateDescriptorsWorker.KEY_UPDATED_DESCRIPTORS);
if (descriptor != null && !descriptor.isEmpty()) {
binding.reviewUpdates.setVisibility(View.VISIBLE);
binding.reviewUpdates.setOnClickListener(view -> getReviewUpdatesLauncher().launch(
ReviewDescriptorUpdatesActivity.newIntent(
OverviewActivity.this,
descriptor
)
));
}
binding.swipeRefresh.setRefreshing(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class DescriptorUpdateFragment : Fragment() {
SimpleDateFormat(
"MMM dd, yyyy",
Locale.getDefault()
).format(descriptor.descriptorCreationTime)
).format(descriptor.dateCreated)
}"
description.text = absDescriptor.description // Use markdown
icon.setImageResource(absDescriptor.getDisplayIcon(requireContext()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.openobservatory.ooniprobe.adapters

import android.content.res.Resources
import android.graphics.PorterDuff
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.common.AbstractDescriptor
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.databinding.ItemSeperatorBinding
import org.openobservatory.ooniprobe.databinding.ItemTestsuiteBinding
import org.openobservatory.ooniprobe.model.database.InstalledDescriptor
import java.util.Date

class DashboardAdapter(
private val items: List<Any>,
Expand Down Expand Up @@ -62,6 +59,9 @@ class DashboardAdapter(
icon.setImageResource(item.getDisplayIcon(holder.itemView.context)).also {
if (item is InstalledDescriptor){
icon.setColorFilter(item.color)
if (item.descriptor?.isExpired == true) {
expiredTag.root.visibility = View.VISIBLE
}
holder.setIsRecyclable(false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,6 @@ abstract class AbstractDescriptor<T : BaseNettest>(
open var descriptor: TestDescriptor? = null
) : BaseDescriptor<T>(name = name, nettests = nettests) {

/**
* 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.
*/
open fun isEnabled(preferenceManager: PreferenceManager): Boolean {
return when (name) {
OONITests.EXPERIMENTAL.label -> preferenceManager.isExperimentalOn
else -> nettests.any {
preferenceManager.resolveStatus(
name = it.name,
prefix = preferencePrefix(),
)
}
}
}

/**
* Returns the runtime of the current descriptor.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import com.raizlabs.android.dbflow.sql.language.SQLite
import org.openobservatory.engine.BaseNettest
import org.openobservatory.engine.LoggerArray
import org.openobservatory.engine.OONIRunFetchResponse
import org.openobservatory.engine.OONIRunDescriptor
import org.openobservatory.ooniprobe.BuildConfig
import org.openobservatory.ooniprobe.activity.adddescriptor.adapter.GroupedItem
import org.openobservatory.ooniprobe.model.database.InstalledDescriptor
Expand Down Expand Up @@ -59,23 +59,26 @@ class TestDescriptorManager @Inject constructor(
)
val ooniContext = session.newContextWithTimeout(300)

val response: OONIRunFetchResponse = session.ooniRunFetch(ooniContext, runId)
val response: OONIRunDescriptor =
session.ooniRunFetch(ooniContext, BuildConfig.OONI_API_BASE_URL, runId)
return TestDescriptor(
runId = runId,
name = response.descriptor.name,
nameIntl = response.descriptor.nameIntl,
author = response.descriptor.author,
shortDescription = response.descriptor.shortDescription,
shortDescriptionIntl = response.descriptor.shortDescriptionIntl,
description = response.descriptor.description,
descriptionIntl = response.descriptor.descriptionIntl,
icon = response.descriptor.icon,
color = response.descriptor.color,
animation = response.descriptor.animation,
isArchived = response.archived,
descriptorCreationTime = response.creationTime,
translationCreationTime = response.translationCreationTime,
nettests = response.descriptor.nettests
name = response.name,
shortDescription = response.shortDescription,
description = response.description,
author = response.author,
nettests = response.nettests,
nameIntl = response.nameIntl,
shortDescriptionIntl = response.shortDescriptionIntl,
descriptionIntl = response.descriptionIntl,
icon = response.icon,
color = response.color,
animation = response.animation,
expirationDate = response.expirationDate,
dateCreated = response.dateCreated,
dateUpdated = response.dateUpdated,
revision = response.revision,
isExpired = response.isExpired
)
}

Expand Down Expand Up @@ -107,11 +110,11 @@ class TestDescriptorManager @Inject constructor(

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

fun delete(descriptor: InstalledDescriptor): Boolean {
preferenceManager.sp.all.entries.forEach {entry ->
preferenceManager.sp.all.entries.forEach { entry ->
if (entry.key.contains(descriptor.testDescriptor.runId.toString())) {
preferenceManager.sp.edit().remove(entry.key).apply()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class ManualUpdateDescriptorsWorker(

val descriptors = inputData.getLongArray(KEY_DESCRIPTOR_IDS)?.let {
d.testDescriptorManager.getDescriptorsFromIds(it.toTypedArray())
}.run {
} ?: run {
d.testDescriptorManager.getDescriptorWithAutoUpdateDisabled()
}

if(descriptors.isEmpty()) {
if (descriptors.isEmpty()) {
Log.e(TAG, "No descriptors to update")
return Result.success()
}
Expand Down Expand Up @@ -135,7 +135,12 @@ class ManualUpdateDescriptorsWorker(
Log.e(TAG, "fetching updates complete")

setProgressAsync(Data.Builder().putInt(PROGRESS, 100).build())
Result.success(outputData)
if (updatedDescriptors.isEmpty()) {
Log.e(TAG, "No descriptors were updated")
Result.success()
} else {
Result.success(outputData)
}

} catch (exception: Exception) {
Log.e(TAG, "Error Updating")
Expand Down
Loading

0 comments on commit 50448c9

Please sign in to comment.