Skip to content

Commit

Permalink
Implement ui action
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jul 9, 2024
1 parent 311c4cb commit 8180992
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,3 @@ class FcmMessageListenerService : FirebaseMessagingService() {
private val TAG = FcmMessageListenerService::class.java.simpleName
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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
}
}
Expand Down
12 changes: 9 additions & 3 deletions mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 8180992

Please sign in to comment.