From a816dc704dad056ec10f44b12025bad71c9cbbba Mon Sep 17 00:00:00 2001 From: Jonah Hirsch Date: Sun, 12 Sep 2021 18:09:28 -0700 Subject: [PATCH] Add ability for Actions to be Cancelled By sending an Intent such as: ``` Intent(context, IntentServiceAction::class.java).apply { action = IntentServiceAction.ACTION_STOP } ``` Any running actions will be stopped with an InterruptedException --- .../action/ActionReceivers.kt | 23 +++++++++++++++++++ .../src/main/res/values/strings.xml | 1 + 2 files changed, 24 insertions(+) diff --git a/taskerpluginlibrary/src/main/java/com/joaomgcd/taskerpluginlibrary/action/ActionReceivers.kt b/taskerpluginlibrary/src/main/java/com/joaomgcd/taskerpluginlibrary/action/ActionReceivers.kt index 5668e99..27b6d82 100644 --- a/taskerpluginlibrary/src/main/java/com/joaomgcd/taskerpluginlibrary/action/ActionReceivers.kt +++ b/taskerpluginlibrary/src/main/java/com/joaomgcd/taskerpluginlibrary/action/ActionReceivers.kt @@ -3,8 +3,11 @@ package com.joaomgcd.taskerpluginlibrary.action import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import com.joaomgcd.taskerpluginlibrary.R import com.joaomgcd.taskerpluginlibrary.extensions.runFromTasker +import com.joaomgcd.taskerpluginlibrary.runner.ArgsSignalFinish import com.joaomgcd.taskerpluginlibrary.runner.IntentServiceParallel +import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultError import net.dinglisch.android.tasker.TaskerPlugin @@ -20,11 +23,31 @@ class BroadcastReceiverAction : BroadcastReceiver() { } class IntentServiceAction : IntentServiceParallel("IntentServiceTaskerAction") { + private var taskerIntent: Intent? = null + override fun onHandleIntent(intent: Intent) { startForegroundIfNeeded() + taskerIntent = intent val result = TaskerPluginRunnerAction.runFromIntent(this, intent) if (!result.hasStartedForeground) { startForegroundIfNeeded() } } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (intent?.action.equals(ACTION_STOP)) { + stopSelf() + taskerIntent?.let { + TaskerPluginResultError( + InterruptedException(getString(R.string.cancelled)) + ).signalFinish(ArgsSignalFinish(this, it)) + } + return START_NOT_STICKY + } + return super.onStartCommand(intent, flags, startId) + } + + companion object { + const val ACTION_STOP = "ACTION_STOP" + } } \ No newline at end of file diff --git a/taskerpluginlibrary/src/main/res/values/strings.xml b/taskerpluginlibrary/src/main/res/values/strings.xml index 4a295fa..113c81d 100644 --- a/taskerpluginlibrary/src/main/res/values/strings.xml +++ b/taskerpluginlibrary/src/main/res/values/strings.xml @@ -7,4 +7,5 @@ If there\'s an error, contains an error code Error Message If there\'s an error, contains an error message + Cancelled