Skip to content

Commit

Permalink
fix: keep OngoingCallService always running during call [WPB-10467] 🍒 (…
Browse files Browse the repository at this point in the history
…#3302)

Co-authored-by: MichaĹ‚ Saleniuk <[email protected]>
Co-authored-by: MichaĹ‚ Saleniuk <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2024
1 parent cdab6d0 commit ee3e89e
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class CallNotificationManager @Inject constructor(
hideIncomingCallNotification()
}

fun hideIncomingCallNotification() {
private fun hideIncomingCallNotification() {
appLogger.i("$TAG: hiding incoming call")

// This delay is just so when the user receives two calling signals one straight after the other [INCOMING -> CANCEL]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.wire.android.notification

import android.os.Build
import androidx.annotation.VisibleForTesting
import com.wire.android.R
import com.wire.android.appLogger
Expand Down Expand Up @@ -270,7 +269,7 @@ class WireNotificationManager @Inject constructor(
// start observing ongoing calls for all users, but only if not yet started
if (observingJobs.ongoingCallJob.get().let { it == null || !it.isActive }) {
val job = scope.launch(dispatcherProvider.default()) {
observeOngoingCalls(currentScreenState)
observeOngoingCalls()
}
observingJobs.ongoingCallJob.set(job)
}
Expand Down Expand Up @@ -405,34 +404,23 @@ class WireNotificationManager @Inject constructor(
/**
* Infinitely listen for the established calls of a current user and run OngoingCall foreground Service
* to show corresponding notification and do not lose a call.
* @param currentScreenState StateFlow that informs which screen is currently visible,
* so we can listen established calls only when the app is in background.
*/
private suspend fun observeOngoingCalls(currentScreenState: StateFlow<CurrentScreen>) {
currentScreenState
.flatMapLatest { currentScreen ->
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE && currentScreen !is CurrentScreen.InBackground) {
flowOf(null)
private suspend fun observeOngoingCalls() {
coreLogic.getGlobalScope().session.currentSessionFlow()
.flatMapLatest {
if (it is CurrentSessionResult.Success && it.accountInfo.isValid()) {
coreLogic.getSessionScope(it.accountInfo.userId).calls.establishedCall()
.map { it.isNotEmpty() }
} else {
coreLogic.getGlobalScope().session.currentSessionFlow()
.flatMapLatest {
if (it is CurrentSessionResult.Success && it.accountInfo.isValid()) {
coreLogic.getSessionScope(it.accountInfo.userId).calls.establishedCall()
.map {
it.firstOrNull()
}
} else {
flowOf(null)
}
}
flowOf(false)
}
}
.distinctUntilChanged()
.onCompletion {
servicesManager.stopOngoingCallService()
}
.collect { call ->
if (call != null) servicesManager.startOngoingCallService()
.collect { isOngoingCall ->
if (isOngoingCall) servicesManager.startOngoingCallService()
else servicesManager.stopOngoingCallService()
}
}
Expand Down Expand Up @@ -492,12 +480,6 @@ class WireNotificationManager @Inject constructor(
}
}

data class MessagesNotificationsData(
val newNotifications: List<LocalNotification>,
val userId: QualifiedID,
val userName: String
)

private data class UserObservingJobs(
val currentScreenJob: Job,
val incomingCallsJob: Job,
Expand Down
Loading

0 comments on commit ee3e89e

Please sign in to comment.