From dfe873bc1c3b402071d8b3cf4688f9b6adad14ab Mon Sep 17 00:00:00 2001 From: Yoan Pintas Date: Sat, 16 Nov 2024 02:22:29 +0100 Subject: [PATCH] Restore android call service (#1134) --- vector/src/main/AndroidManifest.xml | 4 +++- .../app/core/services/CallAndroidService.kt | 17 +++++++++----- .../app/features/call/VectorCallActivity.kt | 23 +++++++++---------- .../app/features/call/VectorCallActivity.kt | 23 +++++++++---------- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/vector/src/main/AndroidManifest.xml b/vector/src/main/AndroidManifest.xml index 1f4eb1ef93..a15f56a045 100644 --- a/vector/src/main/AndroidManifest.xml +++ b/vector/src/main/AndroidManifest.xml @@ -44,7 +44,6 @@ - @@ -58,6 +57,9 @@ + + + diff --git a/vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt b/vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt index b14c5604eb..1f5b0f7b5c 100644 --- a/vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt +++ b/vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt @@ -182,8 +182,11 @@ class CallAndroidService : VectorAndroidService() { title = callInformation.opponentMatrixItem?.getBestName() ?: callInformation.opponentUserId, fromBg = fromBg ) - // TCHAP fix crash when a call incoming on Androd 14 and higher - notificationManager.notify(callId.hashCode(), notification) + if (knownCalls.isEmpty()) { + startForegroundCompat(callId.hashCode(), notification) + } else { + notificationManager.notify(callId.hashCode(), notification) + } knownCalls[callId] = callInformation } @@ -200,8 +203,7 @@ class CallAndroidService : VectorAndroidService() { } val notification = notificationUtils.buildCallEndedNotification(false) val notificationId = callId.hashCode() - // TCHAP fix crash when a call incoming on Androd 14 and higher - notificationManager.notify(notificationId, notification) + startForegroundCompat(notificationId, notification) if (knownCalls.isEmpty()) { Timber.tag(loggerTag.value).v("No more call, stop the service") stopForegroundCompat() @@ -262,8 +264,11 @@ class CallAndroidService : VectorAndroidService() { call = call, title = callInformation.opponentMatrixItem?.getBestName() ?: callInformation.opponentUserId ) - // TCHAP fix crash when a call incoming on Androd 14 and higher - startForegroundCompat(callId.hashCode(), notification) + if (knownCalls.isEmpty()) { + startForegroundCompat(callId.hashCode(), notification) + } else { + notificationManager.notify(callId.hashCode(), notification) + } knownCalls[callId] = callInformation } diff --git a/vector/src/withdmvoip/java/im/vector/app/features/call/VectorCallActivity.kt b/vector/src/withdmvoip/java/im/vector/app/features/call/VectorCallActivity.kt index a688c6caab..7919320871 100644 --- a/vector/src/withdmvoip/java/im/vector/app/features/call/VectorCallActivity.kt +++ b/vector/src/withdmvoip/java/im/vector/app/features/call/VectorCallActivity.kt @@ -251,19 +251,17 @@ class VectorCallActivity : } private fun startMicrophoneService() { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) - == PackageManager.PERMISSION_GRANTED) { - // Only start the service if the app is in the foreground - if (isAppInForeground()) { - Timber.tag(loggerTag.value).v("Starting microphone foreground service") - val intent = Intent(this, MicrophoneAccessService::class.java) - ContextCompat.startForegroundService(this, intent) - } else { - Timber.tag(loggerTag.value).v("App is not in foreground; cannot start microphone service") - } - } else { - Timber.tag(loggerTag.value).v("Microphone permission not granted; cannot start service") + val isConnected = withState(callViewModel) { + val callState = it.callState.invoke() + callState is CallState.Connected } + if (!isAppInForeground()) return + if (!isConnected) return + if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) return + + Timber.tag(loggerTag.value).d("Starting microphone foreground service") + val intent = Intent(this, MicrophoneAccessService::class.java) + ContextCompat.startForegroundService(this, intent) } private fun isAppInForeground(): Boolean { @@ -292,6 +290,7 @@ class VectorCallActivity : turnScreenOffAndKeyguardOn() removeOnPictureInPictureModeChangedListener(pictureInPictureModeChangedInfoConsumer) screenCaptureServiceConnection.unbind() + stopMicrophoneService() super.onDestroy() } diff --git a/vector/src/withvoip/java/im/vector/app/features/call/VectorCallActivity.kt b/vector/src/withvoip/java/im/vector/app/features/call/VectorCallActivity.kt index a688c6caab..7919320871 100644 --- a/vector/src/withvoip/java/im/vector/app/features/call/VectorCallActivity.kt +++ b/vector/src/withvoip/java/im/vector/app/features/call/VectorCallActivity.kt @@ -251,19 +251,17 @@ class VectorCallActivity : } private fun startMicrophoneService() { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) - == PackageManager.PERMISSION_GRANTED) { - // Only start the service if the app is in the foreground - if (isAppInForeground()) { - Timber.tag(loggerTag.value).v("Starting microphone foreground service") - val intent = Intent(this, MicrophoneAccessService::class.java) - ContextCompat.startForegroundService(this, intent) - } else { - Timber.tag(loggerTag.value).v("App is not in foreground; cannot start microphone service") - } - } else { - Timber.tag(loggerTag.value).v("Microphone permission not granted; cannot start service") + val isConnected = withState(callViewModel) { + val callState = it.callState.invoke() + callState is CallState.Connected } + if (!isAppInForeground()) return + if (!isConnected) return + if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) return + + Timber.tag(loggerTag.value).d("Starting microphone foreground service") + val intent = Intent(this, MicrophoneAccessService::class.java) + ContextCompat.startForegroundService(this, intent) } private fun isAppInForeground(): Boolean { @@ -292,6 +290,7 @@ class VectorCallActivity : turnScreenOffAndKeyguardOn() removeOnPictureInPictureModeChangedListener(pictureInPictureModeChangedInfoConsumer) screenCaptureServiceConnection.unbind() + stopMicrophoneService() super.onDestroy() }