diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/OoniRunActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/OoniRunActivity.java index 8938ee8aa..df89ae0f7 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/OoniRunActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/OoniRunActivity.java @@ -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; @@ -59,7 +60,6 @@ protected void onCreate(Bundle savedInstanceState) { setSupportActionBar(binding.toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); - items = new ArrayList<>(); manageIntent(getIntent()); } @@ -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(); @@ -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(); @@ -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); @@ -215,6 +236,8 @@ private void loadSuite(AbstractSuite suite, List 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); @@ -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); diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/TaskExecutor.kt b/app/src/main/java/org/openobservatory/ooniprobe/common/TaskExecutor.kt new file mode 100644 index 000000000..4c9808382 --- /dev/null +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/TaskExecutor.kt @@ -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 { + abstract fun runTask(progressToken: OnTaskProgressUpdate

): R +} + +typealias Task = Callable + +typealias OnTaskProgressUpdate

= (P) -> Unit + +typealias OnTaskComplete = (R) -> Void + +class TaskExecutor { + private val executor = Executors.newSingleThreadExecutor() + private val handler = Handler(Looper.getMainLooper()) + + fun executeTask(task: Task, onComplete: OnTaskComplete) { + executor.execute { + val result = task.call() + handler.post { + onComplete(result) + } + } + } + + fun executeProgressTask( + progressTask: ProgressTask, + onProgress: OnTaskProgressUpdate

, + onComplete: OnTaskComplete + ) { + executor.execute { + val result = progressTask.runTask(progressToken = { progress -> + handler.post { + onProgress(progress) + } + }) + + handler.post { + onComplete(result) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_oonirun.xml b/app/src/main/res/layout/activity_oonirun.xml index 6946ef3ad..911d45cb4 100644 --- a/app/src/main/res/layout/activity_oonirun.xml +++ b/app/src/main/res/layout/activity_oonirun.xml @@ -1,6 +1,7 @@ @@ -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"/> + tools:text="@string/OONIRun_InvalidParameter" /> + tools:text="@string/OONIRun_YouAreAboutToRun" /> + + \ No newline at end of file