Reduce code duplication in NotificationUtils

Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
This commit is contained in:
Dariusz Olszewski 2022-05-05 22:21:39 +02:00 committed by Andy Scherzinger
parent b96bba47b5
commit 2f73433170
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B

View File

@ -181,7 +181,15 @@ object NotificationUtils {
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) {
return
}
@ -193,63 +201,40 @@ object NotificationUtils {
for (statusBarNotification in statusBarNotifications) {
notification = statusBarNotification.notification
if (notification != null && !notification.extras.isEmpty) {
if (conversationUser.id == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID)) {
if (
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)
}
}
}
}
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)
) {
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? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || conversationUser.id == -1L || context == null) {
return null
}
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)
) {
scanNotifications(context, conversationUser) { _, statusBarNotification, notification ->
if (roomTokenOrId == notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)) {
return statusBarNotification
}
}
}
return null
}
@ -258,26 +243,12 @@ object NotificationUtils {
conversationUser: UserEntity,
roomTokenOrId: String
) {
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) &&
roomTokenOrId == statusBarNotification.notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)
) {
scanNotifications(context, conversationUser) { notificationManager, statusBarNotification, notification ->
if (roomTokenOrId == notification.extras.getString(BundleKeys.KEY_ROOM_TOKEN)) {
notificationManager.cancel(statusBarNotification.id)
}
}
}
}
private fun getRingtoneUri(
context: Context,