Avoid race condition when recreating the messages NotificationChannel

This commit is contained in:
cketti 2022-03-08 22:53:22 +01:00
parent 2607b24b3f
commit aa90f4369d
2 changed files with 12 additions and 11 deletions

View file

@ -490,11 +490,6 @@ class Account(override val uuid: String) : BaseAccount {
(oldSyncMode != FolderMode.NONE && syncMode == FolderMode.NONE)
}
@Synchronized
fun incrementMessagesNotificationChannelVersion() {
messagesNotificationChannelVersion++
}
@Synchronized
fun isSortAscending(sortType: SortType): Boolean {
return sortAscending.getOrPut(sortType) { sortType.isDefaultAscending }

View file

@ -144,12 +144,16 @@ class NotificationChannelManager(
fun getChannelIdFor(account: Account, channelType: ChannelType): String {
return if (channelType == ChannelType.MESSAGES) {
"messages_channel_${account.uuid}${account.messagesNotificationChannelSuffix}"
getMessagesChannelId(account, account.messagesNotificationChannelSuffix)
} else {
"miscellaneous_channel_${account.uuid}"
}
}
private fun getMessagesChannelId(account: Account, suffix: String): String {
return "messages_channel_${account.uuid}$suffix"
}
@RequiresApi(Build.VERSION_CODES.O)
fun getNotificationConfiguration(account: Account): NotificationConfiguration {
val channelId = getChannelIdFor(account, ChannelType.MESSAGES)
@ -175,11 +179,8 @@ class NotificationChannelManager(
return
}
notificationManager.deleteNotificationChannel(oldChannelId)
account.incrementMessagesNotificationChannelVersion()
val newChannelId = getChannelIdFor(account, ChannelType.MESSAGES)
val newChannelVersion = account.messagesNotificationChannelVersion + 1
val newChannelId = getMessagesChannelId(account, "_$newChannelVersion")
val channelName = resourceProvider.messagesChannelName
val importance = oldNotificationChannel.importance
@ -195,6 +196,11 @@ class NotificationChannelManager(
Timber.v("Old NotificationChannel: %s", oldNotificationChannel)
Timber.v("New NotificationChannel: %s", newNotificationChannel)
notificationManager.createNotificationChannel(newNotificationChannel)
// To avoid a race condition we first create the new NotificationChannel, point the Account to it,
// then delete the old one.
account.messagesNotificationChannelVersion = newChannelVersion
notificationManager.deleteNotificationChannel(oldChannelId)
}
@RequiresApi(Build.VERSION_CODES.O)