Skip to content

Commit

Permalink
Improve CloudNotificationAction handling
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 authored and mueller-ma committed Jul 9, 2024
1 parent 325dac6 commit f888f60
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,13 @@ class BackgroundTasksManager : BroadcastReceiver() {
)
}

fun enqueueNotificationAction(context: Context, action: CloudNotificationAction) {
val split = action.action.split(":", limit = 3)
fun enqueueNotificationAction(context: Context, action: CloudNotificationAction.Action.ItemCommandAction) {
enqueueItemUpload(
context,
WORKER_TAG_PREFIX_NOTIFICATION + split[1],
split[1],
WORKER_TAG_PREFIX_NOTIFICATION + action.itemName,
action.itemName,
null,
ItemUpdateWorker.ValueWithInfo(split[2]),
ItemUpdateWorker.ValueWithInfo(action.command),
isImportant = true,
showToast = true,
asCommand = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.content.IntentCompat
import androidx.core.net.toUri
import org.openhab.habdroid.background.BackgroundTasksManager
import org.openhab.habdroid.core.NotificationHelper.Companion.NOTIFICATION_ACTION_ACTION
import org.openhab.habdroid.core.NotificationHelper.Companion.NOTIFICATION_ACTION_LABEL
import org.openhab.habdroid.core.NotificationHelper.Companion.NOTIFICATION_ACTION_EXTRA
import org.openhab.habdroid.core.NotificationHelper.Companion.NOTIFICATION_ID_EXTRA
import org.openhab.habdroid.model.CloudNotificationAction
import org.openhab.habdroid.util.openInBrowser
Expand All @@ -32,28 +32,26 @@ class NotificationActionReceiver : BroadcastReceiver() {
if (notificationId == 0) {
return
}
val rawAction = intent.getStringExtra(NOTIFICATION_ACTION_ACTION) ?: return
val label = intent.getStringExtra(NOTIFICATION_ACTION_LABEL) ?: return
val action = CloudNotificationAction(label, rawAction)
Log.d(TAG, "Received action from $notificationId: $action")
executeAction(context, action)
val cna = IntentCompat.getParcelableExtra(
intent,
NOTIFICATION_ACTION_EXTRA,
CloudNotificationAction::class.java
) ?: return
Log.d(TAG, "Received action from $notificationId: $cna")

when (val action = cna.action) {
is CloudNotificationAction.Action.ItemCommandAction ->
BackgroundTasksManager.enqueueNotificationAction(context, action)
is CloudNotificationAction.Action.UrlAction ->
action.url.toUri().openInBrowser(context)
else -> {
// TODO
Log.e(TAG, "Not yet implemented")
}
}
}

companion object {
private val TAG = NotificationActionReceiver::class.java.simpleName

fun executeAction(context: Context, action: CloudNotificationAction) {
when {
action.action.startsWith("command:") -> {
BackgroundTasksManager.enqueueNotificationAction(context, action)
}
action.action.startsWith("http://") || action.action.startsWith("https://") -> {
action.action.toUri().openInBrowser(context)
} else -> {
// TODO
Log.e(TAG, "Not yet implemented")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ class NotificationHelper(private val context: Context) {
private fun createActionIntent(action: CloudNotificationAction, notificationId: Int): PendingIntent {
val intent = Intent(context, NotificationActionReceiver::class.java)
intent.putExtra(NOTIFICATION_ID_EXTRA, notificationId)
intent.putExtra(NOTIFICATION_ACTION_LABEL, action.label)
intent.putExtra(NOTIFICATION_ACTION_ACTION, action.action)
intent.putExtra(NOTIFICATION_ACTION_EXTRA, action)
return PendingIntent.getBroadcast(
context,
notificationId + action.hashCode(),
Expand Down Expand Up @@ -273,8 +272,7 @@ class NotificationHelper(private val context: Context) {
companion object {
private val TAG = NotificationHelper::class.java.simpleName
const val NOTIFICATION_ID_EXTRA = "notification_id"
const val NOTIFICATION_ACTION_LABEL = "notification_action_label"
const val NOTIFICATION_ACTION_ACTION = "notification_action_action"
const val NOTIFICATION_ACTION_EXTRA = "notification_action"

private fun getChannelId(severity: String?) = if (severity.isNullOrEmpty()) {
NotificationUpdateObserver.CHANNEL_ID_MESSAGE_DEFAULT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,25 @@ fun JSONObject.toCloudNotification(): CloudNotification {
@Parcelize
data class CloudNotificationAction internal constructor(
val label: String,
val action: String
) : Parcelable
private val internalAction: String
) : Parcelable {
sealed class Action {
class UrlAction(val url: String) : Action()
class ItemCommandAction(val itemName: String, val command: String) : Action()
object NoAction : Action()
}

val action: Action get() {
val split = internalAction.split(":", limit = 3)
return when {
split[0] == "command" && split.size == 3 ->
Action.ItemCommandAction(split[1], split[2])
internalAction.startsWith("http://") || internalAction.startsWith("https://") ->
Action.UrlAction(internalAction)
else -> Action.NoAction
}
}
}

fun String?.toCloudNotificationAction(): CloudNotificationAction? {
val split = this?.split("=", limit = 2)
Expand Down

0 comments on commit f888f60

Please sign in to comment.