Skip to content

Commit

Permalink
Fix PendingIntent
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 b527c54 commit 69ea76a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.openhab.habdroid.core

import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
Expand All @@ -23,6 +24,7 @@ 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.PendingIntent_Immutable
import org.openhab.habdroid.util.openInBrowser

class NotificationHandlingReceiver : BroadcastReceiver() {
Expand Down Expand Up @@ -72,20 +74,34 @@ class NotificationHandlingReceiver : BroadcastReceiver() {
}
}

fun createActionIntent(context: Context, notificationId: Int, cna: CloudNotificationAction): Intent {
val cnaAction = cna.action
return if (cnaAction is CloudNotificationAction.Action.UiCommandAction) {
Intent(context, MainActivity::class.java).apply {
action = Intent.ACTION_VIEW
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(MainActivity.EXTRA_UI_COMMAND, cnaAction.command)
fun createActionPendingIntent(context: Context, notificationId: Int, cna: CloudNotificationAction): PendingIntent {
return when (val cnaAction = cna.action) {
is CloudNotificationAction.Action.UiCommandAction -> {
val intent = Intent(context, MainActivity::class.java).apply {
action = Intent.ACTION_VIEW
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(MainActivity.EXTRA_UI_COMMAND, cnaAction.command)
}
PendingIntent.getActivity(
context,
notificationId + cna.hashCode(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable
)
}
} else {
Intent(context, NotificationHandlingReceiver::class.java).apply {
action = ACTION_NOTIF_ACTION
putExtra(EXTRA_NOTIFICATION_ID, notificationId)
putExtra(EXTRA_NOTIFICATION_ACTION, cna)
else -> {
val intent = Intent(context, NotificationHandlingReceiver::class.java).apply {
action = ACTION_NOTIF_ACTION
putExtra(EXTRA_NOTIFICATION_ID, notificationId)
putExtra(EXTRA_NOTIFICATION_ACTION, cna)
}
PendingIntent.getBroadcast(
context,
notificationId + cna.hashCode(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import org.openhab.habdroid.R
import org.openhab.habdroid.background.NotificationUpdateObserver
import org.openhab.habdroid.core.connection.ConnectionFactory
import org.openhab.habdroid.model.CloudNotification
import org.openhab.habdroid.model.CloudNotificationAction
import org.openhab.habdroid.model.IconResource
import org.openhab.habdroid.ui.MainActivity
import org.openhab.habdroid.util.HttpClient
Expand Down Expand Up @@ -94,16 +93,6 @@ class NotificationHelper(private val context: Context) {
)
}

private fun createActionIntent(action: CloudNotificationAction, notificationId: Int): PendingIntent {
val intent = NotificationHandlingReceiver.createActionIntent(context, notificationId, action)
return PendingIntent.getBroadcast(
context,
notificationId + action.hashCode(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable
)
}

private fun createChannelForSeverity(severity: String?) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
Expand Down Expand Up @@ -143,7 +132,7 @@ class NotificationHelper(private val context: Context) {
val contentIntent = if (message.onClickAction == null) {
makeNotificationClickIntent(message.id, notificationId)
} else {
createActionIntent(message.onClickAction, notificationId)
NotificationHandlingReceiver.createActionPendingIntent(context, notificationId, message.onClickAction)
}
val channelId = getChannelId(message.severity)

Expand All @@ -167,7 +156,8 @@ class NotificationHelper(private val context: Context) {
.setPublicVersion(publicVersion)

message.actions?.forEach {
val action = NotificationCompat.Action(null, it.label, createActionIntent(it, notificationId))
val pi = NotificationHandlingReceiver.createActionPendingIntent(context, notificationId, it)
val action = NotificationCompat.Action(null, it.label, pi)
builder.addAction(action)
}

Expand Down

0 comments on commit 69ea76a

Please sign in to comment.