Skip to content

Commit

Permalink
Combine broadcast receivers dealing with notifications into one
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 authored and mueller-ma committed Jul 9, 2024
1 parent f888f60 commit f7735d8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 104 deletions.
5 changes: 1 addition & 4 deletions mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@
android:name=".background.CopyToClipboardReceiver"
android:exported="false" />
<receiver
android:name=".core.NotificationDismissedReceiver"
android:exported="false" />
<receiver
android:name=".core.NotificationActionReceiver"
android:name=".core.NotificationHandlingReceiver"
android:exported="false" />

<activity
Expand Down

This file was deleted.

This file was deleted.

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

0 comments on commit f7735d8

Please sign in to comment.