Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NotificationChannelGroup crash (WPB-6233) #2691

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ class GlobalObserversManager @Inject constructor(
val callback: LogoutCallback = object : LogoutCallback {
override suspend fun invoke(userId: UserId, reason: LogoutReason) {
notificationManager.stopObservingOnLogout(userId)
notificationChannelsManager.deleteChannelGroup(userId)
if (reason != LogoutReason.SELF_SOFT_LOGOUT) {
userDataStoreProvider.getOrCreate(userId).clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@

package com.wire.android.notification

import android.app.NotificationChannelGroup
import android.content.ContentResolver
import android.content.Context
import android.media.AudioAttributes
import android.net.Uri
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationChannelGroupCompat
import androidx.core.app.NotificationManagerCompat
import com.wire.android.appLogger
import com.wire.kalium.logger.obfuscateId
import com.wire.kalium.logic.data.user.SelfUser
import com.wire.kalium.logic.data.user.UserId
import javax.inject.Inject
Expand All @@ -53,7 +52,10 @@
)
}

// Creating user-specific NotificationChannels for each user, they will be grouped by User in App Settings.
/**
* Creating user-specific NotificationChannels for each user, they will be grouped by User in App Settings.
* And removing the ChannelGroups (with all the channels in it) that are not belongs to any user in a list (user logged out e.x.)
*/
fun createUserNotificationChannels(allUsers: List<SelfUser>) {
appLogger.i("$TAG: creating all the notification channels for ${allUsers.size} users")
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
Expand All @@ -68,15 +70,23 @@

// OngoingCall is not user specific channel, but common for all users.
createOngoingNotificationChannel()

deleteRedundantChannelGroups(allUsers)

Check warning on line 74 in app/src/main/kotlin/com/wire/android/notification/NotificationChannelsManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/notification/NotificationChannelsManager.kt#L74

Added line #L74 was not covered by tests
}

/**
* Deletes NotificationChanelGroup (and all NotificationChannels that belongs to it) for a specific User.
* Use it on logout.
* Deletes NotificationChanelGroup (and all NotificationChannels that belongs to it)
* for the users that are not in [activeUsers] list.
*/
fun deleteChannelGroup(userId: UserId) {
appLogger.i("$TAG: deleting notification channels for ${userId.toString().obfuscateId()} user")
notificationManagerCompat.deleteNotificationChannelGroup(NotificationConstants.getChanelGroupIdForUser(userId))
private fun deleteRedundantChannelGroups(activeUsers: List<SelfUser>) {
val groupsToKeep = activeUsers.map { NotificationConstants.getChanelGroupIdForUser(it.id) }

Check warning on line 82 in app/src/main/kotlin/com/wire/android/notification/NotificationChannelsManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/notification/NotificationChannelsManager.kt#L82

Added line #L82 was not covered by tests

notificationManagerCompat.notificationChannelGroups
.filter { group -> groupsToKeep.none { it == group.id } }
.forEach { group ->
appLogger.i("$TAG: deleting notification channels for ${group.name} group")
notificationManagerCompat.deleteNotificationChannelGroup(group.id)

Check warning on line 88 in app/src/main/kotlin/com/wire/android/notification/NotificationChannelsManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/notification/NotificationChannelsManager.kt#L84-L88

Added lines #L84 - L88 were not covered by tests
}
}

/**
Expand All @@ -85,10 +95,9 @@
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannelGroup(userId: UserId, userName: String): String {
val chanelGroupId = NotificationConstants.getChanelGroupIdForUser(userId)
val channelGroup = NotificationChannelGroup(
chanelGroupId,
getChanelGroupNameForUser(userName)
)
val channelGroup = NotificationChannelGroupCompat.Builder(chanelGroupId)
.setName(getChanelGroupNameForUser(userName))
.build()

Check warning on line 100 in app/src/main/kotlin/com/wire/android/notification/NotificationChannelsManager.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/notification/NotificationChannelsManager.kt#L98-L100

Added lines #L98 - L100 were not covered by tests
notificationManagerCompat.createNotificationChannelGroup(channelGroup)
return chanelGroupId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ class ForgotLockScreenViewModel @Inject constructor(
// TODO: we should have a dedicated manager to perform these required actions in AR after every LogoutUseCase call
private suspend fun hardLogoutAccount(userId: UserId) {
notificationManager.stopObservingOnLogout(userId)
notificationChannelsManager.deleteChannelGroup(userId)
coreLogic.getSessionScope(userId).logout(reason = LogoutReason.SELF_HARD_LOGOUT, waitUntilCompletes = true)
userDataStoreProvider.getOrCreate(userId).clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ class SelfUserProfileViewModel @Inject constructor(
}

notificationManager.stopObservingOnLogout(selfUserId)
notificationChannelsManager.deleteChannelGroup(selfUserId)
accountSwitch(SwitchAccountParam.TryToSwitchToNextAccount).also {
if (it == SwitchAccountResult.NoOtherAccountToSwitch) {
globalDataStore.clearAppLockPasscode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ class ForgotLockScreenViewModelTest {
val logoutActionsCalledExactly = if (userLogoutActionsCalled) 1 else 0
coVerify(exactly = logoutActionsCalledExactly) { logoutUseCase(any(), any()) }
coVerify(exactly = logoutActionsCalledExactly) { notificationManager.stopObservingOnLogout(any()) }
coVerify(exactly = logoutActionsCalledExactly) { notificationChannelsManager.deleteChannelGroup(any()) }
coVerify(exactly = logoutActionsCalledExactly) { userDataStore.clear() }
}
private fun testLoggingOut(
Expand Down
Loading