|
| 1 | +package com.joaomgcd.taskerpluginsample.tasker.cancellable |
| 2 | + |
| 3 | +import android.app.Notification |
| 4 | +import android.app.PendingIntent |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.graphics.drawable.Icon |
| 8 | +import android.os.Build |
| 9 | +import com.joaomgcd.taskerpluginlibrary.action.IntentServiceAction |
| 10 | +import com.joaomgcd.taskerpluginlibrary.action.TaskerPluginRunnerActionNoOutputOrInput |
| 11 | +import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfig |
| 12 | +import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigHelperNoOutputOrInput |
| 13 | +import com.joaomgcd.taskerpluginlibrary.input.TaskerInput |
| 14 | +import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult |
| 15 | +import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess |
| 16 | +import com.joaomgcd.taskerpluginsample.R |
| 17 | +import com.joaomgcd.taskerpluginsample.tasker.ActivityConfigTaskerNoOutputOrInput |
| 18 | + |
| 19 | + |
| 20 | +class CancellableRunner : TaskerPluginRunnerActionNoOutputOrInput() { |
| 21 | + override val notificationProperties get() = NotificationProperties(iconResId = R.drawable.plugin) { context -> |
| 22 | + val intent = Intent(context, IntentServiceAction::class.java).apply { |
| 23 | + action = IntentServiceAction.ACTION_STOP |
| 24 | + } |
| 25 | + val pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) |
| 26 | + val text = context.getString(R.string.cancel) |
| 27 | + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 28 | + val action = Notification.Action.Builder(Icon.createWithResource(context, R.mipmap.ic_launcher), text, pendingIntent).build() |
| 29 | + addAction(action) |
| 30 | + } else { |
| 31 | + addAction(R.mipmap.ic_launcher, text, pendingIntent) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> { |
| 36 | + Thread.sleep(10000) |
| 37 | + return TaskerPluginResultSucess() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class CancellableHelper(config: TaskerPluginConfig<Unit>) : TaskerPluginConfigHelperNoOutputOrInput<CancellableRunner>(config) { |
| 42 | + override val runnerClass = CancellableRunner::class.java |
| 43 | + override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) { |
| 44 | + blurbBuilder.append("This will start a task that takes 10 seconds that can be cancelled from its Notification.") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class CancellableActivity : ActivityConfigTaskerNoOutputOrInput<CancellableRunner, CancellableHelper>() { |
| 49 | + override fun getNewHelper(config: TaskerPluginConfig<Unit>) = CancellableHelper(config) |
| 50 | +} |
0 commit comments