e-ntfy-android/app/src/main/java/io/heckel/ntfy/msg/UserActionManager.kt

33 lines
1.3 KiB
Kotlin
Raw Normal View History

2022-04-17 04:32:29 +02:00
package io.heckel.ntfy.msg
import android.content.Context
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.workDataOf
import io.heckel.ntfy.util.Log
/**
* Trigger user actions clicked from notification popups.
*
* The indirection via WorkManager is required since this code may be executed
* in a doze state and Internet may not be available. It's also best practice, apparently.
*/
object UserActionManager {
private const val TAG = "NtfyUserActionEx"
private const val WORK_NAME_PREFIX = "io.heckel.ntfy.USER_ACTION_"
2022-04-17 20:29:29 +02:00
fun enqueue(context: Context, notificationId: String, actionId: String) {
2022-04-17 04:32:29 +02:00
val workManager = WorkManager.getInstance(context)
2022-04-17 20:29:29 +02:00
val workName = WORK_NAME_PREFIX + notificationId + "_" + actionId
Log.d(TAG,"Enqueuing work to execute user action for notification $notificationId, action $actionId, work: $workName")
2022-04-17 04:32:29 +02:00
val workRequest = OneTimeWorkRequest.Builder(UserActionWorker::class.java)
.setInputData(workDataOf(
2022-04-17 20:29:29 +02:00
UserActionWorker.INPUT_DATA_NOTIFICATION_ID to notificationId,
UserActionWorker.INPUT_DATA_ACTION_ID to actionId,
2022-04-17 04:32:29 +02:00
))
.build()
workManager.enqueueUniqueWork(workName, ExistingWorkPolicy.KEEP, workRequest)
}
}