From b8ea3ce67d9d5f3a4f482a69a1889d57ea136df9 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 15 Oct 2024 14:50:26 +0200 Subject: [PATCH] ElementCall: request audio focus and start CallForeground service only when the call is effectively started. --- .../call/impl/ui/CallScreenPresenter.kt | 1 + .../features/call/impl/ui/CallScreenState.kt | 1 + .../call/impl/ui/CallScreenStateProvider.kt | 2 ++ .../call/impl/ui/ElementCallActivity.kt | 24 +++++++++---------- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenPresenter.kt b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenPresenter.kt index 3a4a31f190..918acc1200 100644 --- a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenPresenter.kt +++ b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenPresenter.kt @@ -180,6 +180,7 @@ class CallScreenPresenter @AssistedInject constructor( urlState = urlState.value, webViewError = webViewError, userAgent = userAgent, + isCallActive = isJoinedCall, isInWidgetMode = isInWidgetMode, eventSink = { handleEvents(it) }, ) diff --git a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenState.kt b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenState.kt index 48a4672e1a..7da23f24ff 100644 --- a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenState.kt +++ b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenState.kt @@ -13,6 +13,7 @@ data class CallScreenState( val urlState: AsyncData, val webViewError: String?, val userAgent: String, + val isCallActive: Boolean, val isInWidgetMode: Boolean, val eventSink: (CallScreenEvents) -> Unit, ) diff --git a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenStateProvider.kt b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenStateProvider.kt index bb4794749d..f891c4c526 100644 --- a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenStateProvider.kt +++ b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/CallScreenStateProvider.kt @@ -24,6 +24,7 @@ internal fun aCallScreenState( urlState: AsyncData = AsyncData.Success("https://call.element.io/some-actual-call?with=parameters"), webViewError: String? = null, userAgent: String = "", + isCallActive: Boolean = true, isInWidgetMode: Boolean = false, eventSink: (CallScreenEvents) -> Unit = {}, ): CallScreenState { @@ -31,6 +32,7 @@ internal fun aCallScreenState( urlState = urlState, webViewError = webViewError, userAgent = userAgent, + isCallActive = isCallActive, isInWidgetMode = isInWidgetMode, eventSink = eventSink, ) diff --git a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/ElementCallActivity.kt b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/ElementCallActivity.kt index b55c39f41b..63aae37e8f 100644 --- a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/ElementCallActivity.kt +++ b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/ui/ElementCallActivity.kt @@ -26,6 +26,7 @@ import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.rememberUpdatedState @@ -95,7 +96,6 @@ class ElementCallActivity : pictureInPicturePresenter.setPipView(this) audioManager = getSystemService(AUDIO_SERVICE) as AudioManager - requestAudioFocus() setContent { val pipState = pictureInPicturePresenter.present() @@ -103,6 +103,11 @@ class ElementCallActivity : ElementThemeApp(appPreferencesStore) { val state = presenter.present() eventSink = state.eventSink + LaunchedEffect(state.isCallActive) { + if (state.isCallActive) { + setCallIsActive() + } + } CallScreenView( state = state, pipState = pipState, @@ -115,6 +120,11 @@ class ElementCallActivity : } } + private fun setCallIsActive() { + requestAudioFocus() + CallForegroundService.start(this) + } + @Composable private fun ListenToAndroidEvents(pipState: PictureInPictureState) { val pipEventSink by rememberUpdatedState(pipState.eventSink) @@ -156,18 +166,6 @@ class ElementCallActivity : setCallType(intent) } - override fun onStart() { - super.onStart() - CallForegroundService.stop(this) - } - - override fun onStop() { - super.onStop() - if (!isFinishing && !isChangingConfigurations) { - CallForegroundService.start(this) - } - } - override fun onDestroy() { super.onDestroy() releaseAudioFocus()