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,7 +181,15 @@ object NotificationUtils {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun cancelAllNotificationsForAccount(context: Context?, conversationUser: UserEntity) {
|
private inline fun scanNotifications(
|
||||||
|
context: Context?,
|
||||||
|
conversationUser: UserEntity,
|
||||||
|
callback: (
|
||||||
|
notificationManager: NotificationManager,
|
||||||
|
statusBarNotification: StatusBarNotification,
|
||||||
|
notification: Notification
|
||||||
|
) -> Unit
|
||||||
|
) {
|
||||||
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
|
||||||
}
|
}
|
||||||
@ -193,63 +201,40 @@ 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 &&
|
||||||
|
!notification.extras.isEmpty &&
|
||||||
|
conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID)
|
||||||
|
) {
|
||||||
|
callback(notificationManager, statusBarNotification, notification)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cancelAllNotificationsForAccount(context: Context?, conversationUser: UserEntity) {
|
||||||
|
scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, _ ->
|
||||||
notificationManager.cancel(statusBarNotification.id)
|
notificationManager.cancel(statusBarNotification.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun cancelExistingNotificationWithId(context: Context?, conversationUser: UserEntity, notificationId: Long?) {
|
fun cancelExistingNotificationWithId(context: Context?, conversationUser: UserEntity, notificationId: Long?) {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
|
scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, notification ->
|
||||||
return
|
if (notificationId == notification.extras.getLong(BundleKeys.KEY_NOTIFICATION_ID)) {
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
notificationManager.cancel(statusBarNotification.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun findNotificationForRoom(
|
fun findNotificationForRoom(
|
||||||
context: Context?,
|
context: Context?,
|
||||||
conversationUser: UserEntity,
|
conversationUser: UserEntity,
|
||||||
roomTokenOrId: String
|
roomTokenOrId: String
|
||||||
): StatusBarNotification? {
|
): StatusBarNotification? {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
|
scanNotifications(context, conversationUser) { _, statusBarNotification, notification ->
|
||||||
return null
|
if (roomTokenOrId == notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)) {
|
||||||
}
|
|
||||||
|
|
||||||
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 statusBarNotification
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,26 +243,12 @@ object NotificationUtils {
|
|||||||
conversationUser: UserEntity,
|
conversationUser: UserEntity,
|
||||||
roomTokenOrId: String
|
roomTokenOrId: String
|
||||||
) {
|
) {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
|
scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, notification ->
|
||||||
return
|
if (roomTokenOrId == notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)) {
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
) {
|
|
||||||
notificationManager.cancel(statusBarNotification.id)
|
notificationManager.cancel(statusBarNotification.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun getRingtoneUri(
|
private fun getRingtoneUri(
|
||||||
context: Context,
|
context: Context,
|
||||||
|
Loading…
Reference in New Issue
Block a user