Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter>
</activity>
<activity
android:name=".tasker.cancellable.CancellableActivity"
android:exported="true"
android:icon="@mipmap/ic_launcher"
android:label="Cancellable">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter>
</activity>

<activity
android:name="com.joaomgcd.taskerpluginsample.tasker.gottime.ActivityConfigGotTime"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.joaomgcd.taskerpluginsample.tasker.cancellable

import android.app.Notification
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Icon
import android.os.Build
import com.joaomgcd.taskerpluginlibrary.action.IntentServiceAction
import com.joaomgcd.taskerpluginlibrary.action.TaskerPluginRunnerActionNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfig
import com.joaomgcd.taskerpluginlibrary.config.TaskerPluginConfigHelperNoOutputOrInput
import com.joaomgcd.taskerpluginlibrary.input.TaskerInput
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResult
import com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginResultSucess
import com.joaomgcd.taskerpluginsample.R
import com.joaomgcd.taskerpluginsample.tasker.ActivityConfigTaskerNoOutputOrInput


class CancellableRunner : TaskerPluginRunnerActionNoOutputOrInput() {
override val notificationProperties get() = NotificationProperties(iconResId = R.drawable.plugin) { context ->
val intent = Intent(context, IntentServiceAction::class.java).apply {
action = IntentServiceAction.ACTION_STOP
}
val pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
val text = context.getString(R.string.cancel)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val action = Notification.Action.Builder(Icon.createWithResource(context, R.mipmap.ic_launcher), text, pendingIntent).build()
addAction(action)
} else {
addAction(R.mipmap.ic_launcher, text, pendingIntent)
}
}

override fun run(context: Context, input: TaskerInput<Unit>): TaskerPluginResult<Unit> {
Thread.sleep(10000)
return TaskerPluginResultSucess()
}
}

class CancellableHelper(config: TaskerPluginConfig<Unit>) : TaskerPluginConfigHelperNoOutputOrInput<CancellableRunner>(config) {
override val runnerClass = CancellableRunner::class.java
override fun addToStringBlurb(input: TaskerInput<Unit>, blurbBuilder: StringBuilder) {
blurbBuilder.append("This will start a task that takes 10 seconds that can be cancelled from its Notification.")
}
}

class CancellableActivity : ActivityConfigTaskerNoOutputOrInput<CancellableRunner, CancellableHelper>() {
override fun getNewHelper(config: TaskerPluginConfig<Unit>) = CancellableHelper(config)
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@
<string name="time_taken_description">The time it took for the background work to complete in millis</string>
<string name="result">Result</string>
<string name="result_description">Text user input in the dialog</string>

<string name="cancel">Cancel</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class TaskerPluginRunnerAction<TInput : Any, TOutput : Any>() : TaskerP
internal fun runWithIntent(context: IntentServiceParallel?, taskerIntent: Intent?) :RunnerActionResult{
if (context == null) return RunnerActionResult(false)
if (taskerIntent == null) return RunnerActionResult(false)
context.startForegroundIfNeeded()
startForegroundIfNeeded(context)
try {
val input = taskerIntent.getTaskerInput(context, getInputClass(taskerIntent))
var result = run(context, input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ abstract class TaskerPluginRunner<TInput : Any, TOutput : Any> {
val notificationChannelDescriptionResId: Int = R.string.tasker_plugin_service_description,
val titleResId: Int = R.string.app_name,
val textResId: Int = R.string.running_tasker_plugin,
val iconResId: Int = R.mipmap.ic_launcher) {
val iconResId: Int = R.mipmap.ic_launcher,
val notificationChannelId: String = NOTIFICATION_CHANNEL_ID,
val notificationBuilderExtender: Notification.Builder.(context: Context) -> Notification.Builder = {this}) {
@TargetApi(Build.VERSION_CODES.O)
fun getNotification(context: Context) = Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
fun getNotification(context: Context) = Notification.Builder(context, notificationChannelId)
.setContentTitle(context.getString(titleResId))
.setContentText(context.getString(textResId))
.setSmallIcon(Icon.createWithResource(context, iconResId))
.notificationBuilderExtender(context)
.build()
}

Expand All @@ -47,13 +50,18 @@ abstract class TaskerPluginRunner<TInput : Any, TOutput : Any> {
TaskerPluginRunner.startForegroundIfNeeded(this, notificationProperties)
}

@TargetApi(Build.VERSION_CODES.O)
fun startForegroundIfNeeded(intentServiceParallel: IntentServiceParallel) {
TaskerPluginRunner.startForegroundIfNeeded(intentServiceParallel, notificationProperties)
}


companion object {
const val NOTIFICATION_CHANNEL_ID = "taskerpluginforegroundd"
private const val NOTIFICATION_CHANNEL_ID = "taskerpluginforegroundd"
@TargetApi(Build.VERSION_CODES.O)
fun Service.createNotificationChannel(channelId: String, notificationProperties: NotificationProperties) {
fun Service.createNotificationChannel(notificationProperties: NotificationProperties) {
val notificationManager = getSystemService(NotificationManager::class.java)
val channel = NotificationChannel(channelId, getString(notificationProperties.notificationChannelNameResId), NotificationManager.IMPORTANCE_NONE)
val channel = NotificationChannel(notificationProperties.notificationChannelId, getString(notificationProperties.notificationChannelNameResId), NotificationManager.IMPORTANCE_NONE)
channel.description = getString(notificationProperties.notificationChannelDescriptionResId)
notificationManager.createNotificationChannel(channel)
}
Expand All @@ -64,8 +72,7 @@ abstract class TaskerPluginRunner<TInput : Any, TOutput : Any> {
@TargetApi(Build.VERSION_CODES.O)
fun startForegroundIfNeeded(intentService: Service, notificationProperties: NotificationProperties = NotificationProperties()) {
if (!intentService.hasToRunServicesInForeground) return
val channelId = NOTIFICATION_CHANNEL_ID
intentService.createNotificationChannel(channelId, notificationProperties)
intentService.createNotificationChannel(notificationProperties)
val notification: Notification = notificationProperties.getNotification(intentService)
intentService.startForeground(this.hashCode(), notification)
}
Expand Down
1 change: 1 addition & 0 deletions taskerpluginlibrary/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<string name="error_code_description">If there\'s an error, contains an error code</string>
<string name="error_message">Error Message</string>
<string name="error_message_description">If there\'s an error, contains an error message</string>
<string name="cancelled">Cancelled</string>
</resources>