diff --git a/mobile/src/main/AndroidManifest.xml b/mobile/src/main/AndroidManifest.xml index 246c5d73f5d..99e8e836ee0 100644 --- a/mobile/src/main/AndroidManifest.xml +++ b/mobile/src/main/AndroidManifest.xml @@ -277,10 +277,7 @@ android:name=".background.CopyToClipboardReceiver" android:exported="false" /> - - 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 - } -} diff --git a/mobile/src/main/java/org/openhab/habdroid/core/NotificationDismissedReceiver.kt b/mobile/src/main/java/org/openhab/habdroid/core/NotificationDismissedReceiver.kt deleted file mode 100644 index 98fdc3d77a1..00000000000 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationDismissedReceiver.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2010-2024 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ - -package org.openhab.habdroid.core - -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.util.Log -import org.openhab.habdroid.core.NotificationHelper.Companion.NOTIFICATION_ID_EXTRA - -class NotificationDismissedReceiver : BroadcastReceiver() { - override fun onReceive(context: Context, intent: Intent) { - Log.d(TAG, "onReceive(): $intent") - val notificationId = intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1) - if (notificationId < 0) { - return - } - Log.d(TAG, "Dismissed notification $notificationId") - NotificationHelper(context).updateGroupNotification() - } - - companion object { - private val TAG = NotificationDismissedReceiver::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 new file mode 100644 index 00000000000..a02005fbf28 --- /dev/null +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2010-2024 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ + +package org.openhab.habdroid.core + +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.BuildConfig +import org.openhab.habdroid.background.BackgroundTasksManager +import org.openhab.habdroid.model.CloudNotificationAction +import org.openhab.habdroid.util.openInBrowser + +class NotificationHandlingReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + Log.d(TAG, "onReceive(): $intent") + val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 0) + when (intent.action) { + ACTION_DISMISSED -> { + Log.d(TAG, "Dismissed notification $notificationId") + NotificationHelper(context).updateGroupNotification() + } + ACTION_NOTIF_ACTION -> { + val cna = IntentCompat.getParcelableExtra( + intent, + EXTRA_NOTIFICATION_ACTION, + 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 = NotificationHandlingReceiver::class.java.simpleName + + const val ACTION_DISMISSED = "${BuildConfig.APPLICATION_ID}.action.NOTIFICATION_DISMISSED" + const val ACTION_NOTIF_ACTION = "${BuildConfig.APPLICATION_ID}.action.NOTIFICATION_ACTION" + + const val EXTRA_NOTIFICATION_ID = "notification_id" + const val EXTRA_NOTIFICATION_ACTION = "notification_action" + + fun createDismissedIntent(context: Context, notificationId: Int): Intent { + return Intent(context, NotificationHandlingReceiver::class.java).apply { + action = ACTION_DISMISSED + putExtra(EXTRA_NOTIFICATION_ID, notificationId) + } + } + + fun createActionIntent(context: Context, notificationId: Int, cna: CloudNotificationAction): Intent { + return Intent(context, NotificationHandlingReceiver::class.java).apply { + action = ACTION_NOTIF_ACTION + putExtra(EXTRA_NOTIFICATION_ID, notificationId) + putExtra(EXTRA_NOTIFICATION_ACTION, cna) + } + } + } +} 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 f1d4163c31f..82e762c7932 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt @@ -85,8 +85,7 @@ class NotificationHelper(private val context: Context) { } private fun createDeleteIntent(notificationId: Int): PendingIntent { - val intent = Intent(context, NotificationDismissedReceiver::class.java) - intent.putExtra(NOTIFICATION_ID_EXTRA, notificationId) + val intent = NotificationHandlingReceiver.createDismissedIntent(context, notificationId) return PendingIntent.getBroadcast( context, notificationId, @@ -96,9 +95,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_EXTRA, action) + val intent = NotificationHandlingReceiver.createActionIntent(context, notificationId, action) return PendingIntent.getBroadcast( context, notificationId + action.hashCode(), @@ -271,8 +268,6 @@ 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_EXTRA = "notification_action" private fun getChannelId(severity: String?) = if (severity.isNullOrEmpty()) { NotificationUpdateObserver.CHANNEL_ID_MESSAGE_DEFAULT