From 8180992a4a2cc8cccdcf4a22c4f19c80b1b865a9 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Tue, 9 Jul 2024 16:56:21 +0200 Subject: [PATCH] Implement ui action Signed-off-by: mueller-ma --- .../habdroid/core/FcmMessageListenerService.kt | 1 - .../habdroid/core/NotificationHandlingReceiver.kt | 9 +++++++++ .../org/openhab/habdroid/core/NotificationHelper.kt | 2 +- .../org/openhab/habdroid/model/CloudNotification.kt | 7 +++++++ .../java/org/openhab/habdroid/ui/MainActivity.kt | 12 +++++++++--- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt b/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt index 383da43ba4..63ab47dabf 100644 --- a/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt +++ b/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt @@ -78,4 +78,3 @@ class FcmMessageListenerService : FirebaseMessagingService() { private val TAG = FcmMessageListenerService::class.java.simpleName } } - diff --git a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt index a02005fbf2..4a1455a971 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt @@ -22,6 +22,7 @@ import androidx.core.net.toUri import org.openhab.habdroid.BuildConfig import org.openhab.habdroid.background.BackgroundTasksManager import org.openhab.habdroid.model.CloudNotificationAction +import org.openhab.habdroid.ui.MainActivity import org.openhab.habdroid.util.openInBrowser class NotificationHandlingReceiver : BroadcastReceiver() { @@ -46,6 +47,14 @@ class NotificationHandlingReceiver : BroadcastReceiver() { BackgroundTasksManager.enqueueNotificationAction(context, action) is CloudNotificationAction.Action.UrlAction -> action.url.toUri().openInBrowser(context) + is CloudNotificationAction.Action.UiCommandAction -> { + val commandIntent = Intent(context, MainActivity::class.java).apply { + flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or + Intent.FLAG_ACTIVITY_NEW_TASK + putExtra(MainActivity.EXTRA_UI_COMMAND, action.command) + } + context.startActivity(commandIntent) + } 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 82e762c793..db8170c9eb 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt @@ -140,7 +140,7 @@ class NotificationHelper(private val context: Context) { ): Notification { val iconBitmap = getNotificationIcon(message.icon) - val contentIntent = if (message.onClickAction == null){ + val contentIntent = if (message.onClickAction == null) { makeNotificationClickIntent(message.id, notificationId) } else { createActionIntent(message.onClickAction, notificationId) 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 26c193e5d3..bf0274601a 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt @@ -94,6 +94,7 @@ data class CloudNotificationAction internal constructor( sealed class Action { class UrlAction(val url: String) : Action() class ItemCommandAction(val itemName: String, val command: String) : Action() + class UiCommandAction(val command: String) : Action() object NoAction : Action() } @@ -104,6 +105,12 @@ data class CloudNotificationAction internal constructor( Action.ItemCommandAction(split[1], split[2]) internalAction.startsWith("http://") || internalAction.startsWith("https://") -> Action.UrlAction(internalAction) + split[0] == "ui" && split.size == 3 -> { + Action.UiCommandAction("${split[1]}${split[2]}") + } + split[0] == "ui" && split.size == 2 -> { + Action.UiCommandAction("navigate:${split[1]}") + } else -> Action.NoAction } } diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt b/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt index 77bbb6ecf3..622e9aa426 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt +++ b/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt @@ -848,6 +848,11 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { handleLink(link, serverId) } + if (!intent.getStringExtra(EXTRA_UI_COMMAND).isNullOrEmpty()) { + val command = intent.getStringExtra(EXTRA_UI_COMMAND) ?: return + handleUiCommand(command, prefs.getPrimaryServerId()) + } + when (intent.action) { NfcAdapter.ACTION_NDEF_DISCOVERED, Intent.ACTION_VIEW -> { val tag = intent.data?.toTagData() @@ -1506,11 +1511,11 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { ItemClient.listenForItemChange(this, connection ?: return, item) { _, payload -> val state = payload.getString("value") Log.d(TAG, "Got state by event: $state") - handleUiCommand(state) + handleUiCommand(state, prefs.getActiveServerId()) } } - private fun handleUiCommand(command: String) { + private fun handleUiCommand(command: String, serverId: Int) { val prefix = command.substringBefore(":") val commandContent = command.removePrefix("$prefix:") when (prefix) { @@ -1538,7 +1543,7 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { } } } - "navigate" -> handleLink(commandContent, prefs.getActiveServerId()) + "navigate" -> handleLink(commandContent, serverId) "close" -> uiCommandItemNotification?.dismiss() "back" -> onBackPressedCallback.handleOnBackPressed() "reload" -> recreate() @@ -1645,6 +1650,7 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { const val EXTRA_SUBPAGE = "subpage" const val EXTRA_LINK = "link" const val EXTRA_PERSISTED_NOTIFICATION_ID = "persistedNotificationId" + const val EXTRA_UI_COMMAND = "uiCommand" const val SNACKBAR_TAG_DEMO_MODE_ACTIVE = "demoModeActive" const val SNACKBAR_TAG_PRESS_AGAIN_EXIT = "pressAgainToExit"