Skip to content

Commit

Permalink
Updated OoniRunActivity to fetch v2 descriptor async
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Aug 14, 2023
1 parent b82cc0b commit ea8c3da
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.openobservatory.ooniprobe.BuildConfig;
import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.common.TaskExecutor;
import org.openobservatory.ooniprobe.common.ThirdPartyServices;
import org.openobservatory.ooniprobe.databinding.ActivityOonirunBinding;
import org.openobservatory.ooniprobe.domain.GetTestSuite;
Expand Down Expand Up @@ -59,7 +60,6 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(binding.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
items = new ArrayList<>();
manageIntent(getIntent());
}

Expand All @@ -70,21 +70,40 @@ protected void onNewIntent(Intent intent) {
}

private void manageIntent(Intent intent) {
items = new ArrayList<>();

Uri uri = intent.getData();
if (uri == null) return;

String host = uri.getHost();

if ("runv2".equals(host) || "run.test.ooni.org".equals(host)) {
try {

binding.progressIndicator.setVisibility(View.VISIBLE);
long runId = Long.parseLong(uri.getPathSegments().get(0));
FetchTestDescriptorResponse response = TestDescriptorManager.fetchDataFromRunId(runId, this);
loadScreen(response);
} catch (Exception exception) {
exception.printStackTrace();
ThirdPartyServices.logException(exception);
loadInvalidAttributes();
}

TaskExecutor executor = new TaskExecutor();
executor.executeTask(
() -> {
try {
return TestDescriptorManager.fetchDataFromRunId(runId, this);
} catch (Exception exception) {
exception.printStackTrace();
ThirdPartyServices.logException(exception);
return null;
}
},
descriptorResponse -> {
if (descriptorResponse!=null) {
binding.progressIndicator.setVisibility(View.GONE);
loadScreen(descriptorResponse);
} else {
binding.progressIndicator.setVisibility(View.GONE);
loadInvalidAttributes();
}
return null;
});

} else if (isTestRunning()) {
Toast.makeText(this, getString(R.string.OONIRun_TestRunningError), Toast.LENGTH_LONG).show();
finish();
Expand Down Expand Up @@ -149,6 +168,7 @@ private void loadScreen(FetchTestDescriptorResponse response) {
binding.iconBig.setVisibility(View.GONE);
// TODO: 18/07/2023 (aanorbel) Add translation
binding.run.setText("Install");
binding.run.setVisibility(View.VISIBLE);
binding.run.setOnClickListener(
v -> {
response.descriptor.save();
Expand Down Expand Up @@ -186,6 +206,7 @@ private void loadOutOfDate() {
binding.title.setText(R.string.OONIRun_OONIProbeOutOfDate);
binding.desc.setText(R.string.OONIRun_OONIProbeNewerVersion);
binding.run.setText(R.string.OONIRun_Update);
binding.run.setVisibility(View.VISIBLE);
binding.icon.setImageResource(R.drawable.update);
binding.iconBig.setImageResource(R.drawable.update);
binding.iconBig.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -215,6 +236,8 @@ private void loadSuite(AbstractSuite suite, List<String> urls) {
mTransactiont.replace(R.id.items, OoniRunListFragment.newInstance(), OoniRunListFragment.class.getName());
mTransactiont.commit();

binding.run.setText(R.string.OONIRun_Close);
binding.run.setVisibility(View.VISIBLE);
binding.run.setOnClickListener(v -> {

RunningActivity.runAsForegroundService(OoniRunActivity.this, suite.asArray(),this::finish, preferenceManager);
Expand All @@ -226,6 +249,7 @@ private void loadInvalidAttributes() {
binding.title.setText(R.string.OONIRun_InvalidParameter);
binding.desc.setText(R.string.OONIRun_InvalidParameter_Msg);
binding.run.setText(R.string.OONIRun_Close);
binding.run.setVisibility(View.VISIBLE);
binding.icon.setImageResource(R.drawable.question_mark);
binding.iconBig.setImageResource(R.drawable.question_mark);
binding.iconBig.setVisibility(View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openobservatory.ooniprobe.common

import android.os.Handler
import android.os.Looper
import java.util.concurrent.Callable
import java.util.concurrent.Executors

abstract class ProgressTask<P, R> {
abstract fun runTask(progressToken: OnTaskProgressUpdate<P>): R
}

typealias Task<R> = Callable<R>

typealias OnTaskProgressUpdate<P> = (P) -> Unit

typealias OnTaskComplete<R> = (R) -> Void

class TaskExecutor {
private val executor = Executors.newSingleThreadExecutor()
private val handler = Handler(Looper.getMainLooper())

fun <R> executeTask(task: Task<R>, onComplete: OnTaskComplete<R>) {
executor.execute {
val result = task.call()
handler.post {
onComplete(result)
}
}
}

fun <P, R> executeProgressTask(
progressTask: ProgressTask<P, R>,
onProgress: OnTaskProgressUpdate<P>,
onComplete: OnTaskComplete<R>
) {
executor.execute {
val result = progressTask.runTask(progressToken = { progress ->
handler.post {
onProgress(progress)
}
})

handler.post {
onComplete(result)
}
}
}
}
18 changes: 14 additions & 4 deletions app/src/main/res/layout/activity_oonirun.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand Down Expand Up @@ -34,7 +35,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/OONIRun_InvalidParameter"
tools:text="@string/OONIRun_InvalidParameter"
android:visibility="gone"/>

<TextView
Expand All @@ -43,7 +44,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/OONIRun_InvalidParameter" />
tools:text="@string/OONIRun_InvalidParameter" />

<TextView
android:id="@+id/short_desc"
Expand All @@ -52,7 +53,7 @@
android:gravity="center_horizontal"
android:maxLines="2"
android:ellipsize="end"
android:text="@string/OONIRun_InvalidParameter"
tools:text="@string/OONIRun_InvalidParameter"
android:visibility="gone"/>

<TextView
Expand All @@ -62,7 +63,14 @@
android:gravity="center_horizontal"
android:maxLines="2"
android:ellipsize="end"
android:text="@string/OONIRun_YouAreAboutToRun" />
tools:text="@string/OONIRun_YouAreAboutToRun" />

<ProgressBar
android:id="@+id/progress_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
</LinearLayout>

<androidx.appcompat.widget.Toolbar
Expand Down Expand Up @@ -99,6 +107,8 @@
android:layout_margin="16dp"
android:text="@string/OONIRun_Run"
android:textAllCaps="false"
android:visibility="gone"
tools:visibility="visible"
android:backgroundTint="@color/color_base"
app:cornerRadius="24dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout><!-- app:layout_scrollFlags="scroll|exitUntilCollapsed|snap" -->

0 comments on commit ea8c3da

Please sign in to comment.