-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combine broadcast receivers dealing with notifications into one
Signed-off-by: Danny Baumann <[email protected]>
- Loading branch information
1 parent
f888f60
commit f7735d8
Showing
5 changed files
with
85 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
mobile/src/main/java/org/openhab/habdroid/core/NotificationActionReceiver.kt
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
mobile/src/main/java/org/openhab/habdroid/core/NotificationDismissedReceiver.kt
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters