mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-20 03:59:35 +01:00
Reduce code duplication in NotificationUtils
Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
This commit is contained in:
parent
b96bba47b5
commit
2f73433170
@ -181,82 +181,14 @@ object NotificationUtils {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun cancelAllNotificationsForAccount(context: Context?, conversationUser: UserEntity) {
|
private inline fun scanNotifications(
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
||||||
|
|
||||||
val statusBarNotifications = notificationManager.activeNotifications
|
|
||||||
var notification: Notification?
|
|
||||||
for (statusBarNotification in statusBarNotifications) {
|
|
||||||
notification = statusBarNotification.notification
|
|
||||||
|
|
||||||
if (notification != null && !notification.extras.isEmpty) {
|
|
||||||
if (conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID)) {
|
|
||||||
notificationManager.cancel(statusBarNotification.id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun cancelExistingNotificationWithId(context: Context?, conversationUser: UserEntity, notificationId: Long?) {
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
||||||
|
|
||||||
val statusBarNotifications = notificationManager.activeNotifications
|
|
||||||
var notification: Notification?
|
|
||||||
for (statusBarNotification in statusBarNotifications) {
|
|
||||||
notification = statusBarNotification.notification
|
|
||||||
|
|
||||||
if (notification != null && !notification.extras.isEmpty) {
|
|
||||||
if (
|
|
||||||
conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) &&
|
|
||||||
notificationId == notification.extras.getLong(BundleKeys.KEY_NOTIFICATION_ID)
|
|
||||||
) {
|
|
||||||
notificationManager.cancel(statusBarNotification.id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun findNotificationForRoom(
|
|
||||||
context: Context?,
|
context: Context?,
|
||||||
conversationUser: UserEntity,
|
conversationUser: UserEntity,
|
||||||
roomTokenOrId: String
|
callback: (
|
||||||
): StatusBarNotification? {
|
notificationManager: NotificationManager,
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
|
statusBarNotification: StatusBarNotification,
|
||||||
return null
|
notification: Notification
|
||||||
}
|
) -> Unit
|
||||||
|
|
||||||
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
||||||
|
|
||||||
val statusBarNotifications = notificationManager.activeNotifications
|
|
||||||
var notification: Notification?
|
|
||||||
for (statusBarNotification in statusBarNotifications) {
|
|
||||||
notification = statusBarNotification.notification
|
|
||||||
|
|
||||||
if (notification != null && !notification.extras.isEmpty) {
|
|
||||||
if (
|
|
||||||
conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) &&
|
|
||||||
roomTokenOrId == statusBarNotification.notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)
|
|
||||||
) {
|
|
||||||
return statusBarNotification
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun cancelExistingNotificationsForRoom(
|
|
||||||
context: Context?,
|
|
||||||
conversationUser: UserEntity,
|
|
||||||
roomTokenOrId: String
|
|
||||||
) {
|
) {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
|
||||||
return
|
return
|
||||||
@ -269,12 +201,51 @@ object NotificationUtils {
|
|||||||
for (statusBarNotification in statusBarNotifications) {
|
for (statusBarNotification in statusBarNotifications) {
|
||||||
notification = statusBarNotification.notification
|
notification = statusBarNotification.notification
|
||||||
|
|
||||||
if (notification != null && !notification.extras.isEmpty) {
|
if (
|
||||||
if (conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) &&
|
notification != null &&
|
||||||
roomTokenOrId == statusBarNotification.notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)
|
!notification.extras.isEmpty &&
|
||||||
) {
|
conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID)
|
||||||
notificationManager.cancel(statusBarNotification.id)
|
) {
|
||||||
}
|
callback(notificationManager, statusBarNotification, notification)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cancelAllNotificationsForAccount(context: Context?, conversationUser: UserEntity) {
|
||||||
|
scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, _ ->
|
||||||
|
notificationManager.cancel(statusBarNotification.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cancelExistingNotificationWithId(context: Context?, conversationUser: UserEntity, notificationId: Long?) {
|
||||||
|
scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, notification ->
|
||||||
|
if (notificationId == notification.extras.getLong(BundleKeys.KEY_NOTIFICATION_ID)) {
|
||||||
|
notificationManager.cancel(statusBarNotification.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findNotificationForRoom(
|
||||||
|
context: Context?,
|
||||||
|
conversationUser: UserEntity,
|
||||||
|
roomTokenOrId: String
|
||||||
|
): StatusBarNotification? {
|
||||||
|
scanNotifications(context, conversationUser) { _, statusBarNotification, notification ->
|
||||||
|
if (roomTokenOrId == notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)) {
|
||||||
|
return statusBarNotification
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cancelExistingNotificationsForRoom(
|
||||||
|
context: Context?,
|
||||||
|
conversationUser: UserEntity,
|
||||||
|
roomTokenOrId: String
|
||||||
|
) {
|
||||||
|
scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, notification ->
|
||||||
|
if (roomTokenOrId == notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)) {
|
||||||
|
notificationManager.cancel(statusBarNotification.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user