Skip to content

Commit b7c970c

Browse files
committed
Add sample Cancellable action
1 parent 44abf54 commit b7c970c

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@
104104
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
105105
</intent-filter>
106106
</activity>
107+
<activity
108+
android:name=".tasker.cancellable.CancellableActivity"
109+
android:exported="true"
110+
android:icon="@mipmap/ic_launcher"
111+
android:label="Cancellable">
112+
<intent-filter>
113+
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
114+
</intent-filter>
115+
</activity>
107116

108117
<activity
109118
android:name="com.joaomgcd.taskerpluginsample.tasker.gottime.ActivityConfigGotTime"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@
5353
<string name="time_taken_description">The time it took for the background work to complete in millis</string>
5454
<string name="result">Result</string>
5555
<string name="result_description">Text user input in the dialog</string>
56+
57+
<string name="cancel">Cancel</string>
5658
</resources>

0 commit comments

Comments
 (0)