From f888f6075c53dce019798fbee31508c33dc8139e Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Tue, 9 Jul 2024 09:41:10 +0200 Subject: [PATCH] Improve CloudNotificationAction handling Signed-off-by: Danny Baumann --- .../background/BackgroundTasksManager.kt | 9 ++--- .../core/NotificationActionReceiver.kt | 40 +++++++++---------- .../habdroid/core/NotificationHelper.kt | 6 +-- .../habdroid/model/CloudNotification.kt | 21 +++++++++- 4 files changed, 44 insertions(+), 32 deletions(-) diff --git a/mobile/src/main/java/org/openhab/habdroid/background/BackgroundTasksManager.kt b/mobile/src/main/java/org/openhab/habdroid/background/BackgroundTasksManager.kt index aa329f17c1b..9332ac9aa6f 100644 --- a/mobile/src/main/java/org/openhab/habdroid/background/BackgroundTasksManager.kt +++ b/mobile/src/main/java/org/openhab/habdroid/background/BackgroundTasksManager.kt @@ -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, diff --git a/mobile/src/main/java/org/openhab/habdroid/core/NotificationActionReceiver.kt b/mobile/src/main/java/org/openhab/habdroid/core/NotificationActionReceiver.kt index 36a8426ad8f..2e2bfa74928 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationActionReceiver.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationActionReceiver.kt @@ -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 @@ -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") - } - } - } } } diff --git a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt index b0f5b5f5d0c..f1d4163c31f 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt @@ -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(), @@ -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 diff --git a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt index 2e0743d1765..26c193e5d3d 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt @@ -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)