From b43bc31ce35a766fe857dc9c9ebde6e24e0a0ed6 Mon Sep 17 00:00:00 2001 From: Norbel AMBANUMBEN Date: Sat, 16 Mar 2024 15:01:56 +0100 Subject: [PATCH] feat: (OONI Run v2) Add link cancellation toast https://github.com/ooni/run/issues/151 --- .../ooniprobe/activity/oonirun/OoniRunV2Activity.kt | 2 +- .../openobservatory/ooniprobe/common/TaskExecutor.kt | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/oonirun/OoniRunV2Activity.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/oonirun/OoniRunV2Activity.kt index b5a092621..e832525af 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/oonirun/OoniRunV2Activity.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/oonirun/OoniRunV2Activity.kt @@ -85,7 +85,7 @@ class OoniRunV2Activity : AbstractActivity() { val executor = TaskExecutor() binding.cancelButton.setOnClickListener { executor.cancelTask() - finishWithError(message = getString(R.string.Modal_Cancel)) + finishWithError(message = "Link installation cancelled") } executor.executeTask({ try { diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/TaskExecutor.kt b/app/src/main/java/org/openobservatory/ooniprobe/common/TaskExecutor.kt index 8b821654d..57e5afdfb 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/TaskExecutor.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/TaskExecutor.kt @@ -44,8 +44,7 @@ typealias OnTaskComplete = (R) -> Unit class TaskExecutor { private val executor = Executors.newSingleThreadExecutor() private val handler = Handler(Looper.getMainLooper()) - private var future: Future<*>? = null - + private lateinit var future: Future<*> /** * Executes a task in a separate thread and posts the result on the main thread. * @param task The task to be executed. @@ -54,8 +53,10 @@ class TaskExecutor { fun executeTask(task: Task, onComplete: OnTaskComplete) { future = executor.submit { val result = task.call() - handler.post { - onComplete(result) + if (!future.isCancelled) { + handler.post { + onComplete(result) + } } } } @@ -90,6 +91,6 @@ class TaskExecutor { * Cancels the currently running task. */ fun cancelTask() { - future?.cancel(true) + this.future.cancel(true) } } \ No newline at end of file