From 53387e66175938f8fccb5038b8189741196876ce Mon Sep 17 00:00:00 2001 From: AmitShilo Date: Fri, 20 Sep 2024 15:19:42 +0300 Subject: [PATCH 1/3] Implemented a microphone access service to handle background microphone usage in calls. Signed-off-by: AmitShilo --- .../src/main/res/values/strings.xml | 2 + vector/src/main/AndroidManifest.xml | 7 +++ .../app/core/services/CallAndroidService.kt | 4 ++ .../app/features/call/VectorCallActivity.kt | 29 ++++++++++ .../call/audio/MicrophoneAccessService.kt | 53 +++++++++++++++++++ .../notifications/NotificationUtils.kt | 13 +++++ 6 files changed, 108 insertions(+) create mode 100644 vector/src/main/java/im/vector/app/features/call/audio/MicrophoneAccessService.kt diff --git a/library/ui-strings/src/main/res/values/strings.xml b/library/ui-strings/src/main/res/values/strings.xml index ef02e525af..5bbb6351f7 100644 --- a/library/ui-strings/src/main/res/values/strings.xml +++ b/library/ui-strings/src/main/res/values/strings.xml @@ -652,6 +652,8 @@ Ending call… + Microphone in use + Information diff --git a/vector/src/main/AndroidManifest.xml b/vector/src/main/AndroidManifest.xml index d13037b4a9..2a92e54865 100644 --- a/vector/src/main/AndroidManifest.xml +++ b/vector/src/main/AndroidManifest.xml @@ -395,6 +395,13 @@ android:foregroundServiceType="mediaProjection" tools:targetApi="Q" /> + + + Date: Thu, 26 Sep 2024 16:50:04 +0700 Subject: [PATCH 2/3] add declare for FOREGROUND_SERVICE_MICROPHONE in vector manifest Signed-off-by: AmitShilo --- vector/src/main/AndroidManifest.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/vector/src/main/AndroidManifest.xml b/vector/src/main/AndroidManifest.xml index 2a92e54865..cc686dbda2 100644 --- a/vector/src/main/AndroidManifest.xml +++ b/vector/src/main/AndroidManifest.xml @@ -56,6 +56,9 @@ + + + Date: Tue, 8 Oct 2024 15:18:21 +0700 Subject: [PATCH 3/3] Bugfix: fix crash when getting call on lockscreen Signed-off-by: AmitShilo --- .../app/features/call/VectorCallActivity.kt | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/call/VectorCallActivity.kt b/vector/src/main/java/im/vector/app/features/call/VectorCallActivity.kt index b1e6b47613..483fdf8d71 100644 --- a/vector/src/main/java/im/vector/app/features/call/VectorCallActivity.kt +++ b/vector/src/main/java/im/vector/app/features/call/VectorCallActivity.kt @@ -16,6 +16,7 @@ package im.vector.app.features.call +import android.Manifest import android.app.Activity import android.app.KeyguardManager import android.app.PictureInPictureParams @@ -41,6 +42,8 @@ import androidx.core.content.getSystemService import androidx.core.util.Consumer import androidx.core.view.isInvisible import androidx.core.view.isVisible +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.ProcessLifecycleOwner import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Mavericks import com.airbnb.mvrx.viewModel @@ -248,16 +251,26 @@ class VectorCallActivity : } private fun startMicrophoneService() { - if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED && - ContextCompat.checkSelfPermission(this, android.Manifest.permission.FOREGROUND_SERVICE_MICROPHONE) == PackageManager.PERMISSION_GRANTED) { - Timber.tag(loggerTag.value).d("Starting MicrophoneAccessService.") - val intent = Intent(this, MicrophoneAccessService::class.java) - ContextCompat.startForegroundService(this, intent) + 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).w("Permissions not granted. Cannot start MicrophoneAccessService.") + Timber.tag(loggerTag.value).v("Microphone permission not granted; cannot start service") } } + private fun isAppInForeground(): Boolean { + val appProcess = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED) + return appProcess + } private fun stopMicrophoneService() { Timber.tag(loggerTag.value).d("Stopping MicrophoneAccessService (if needed).") val intent = Intent(this, MicrophoneAccessService::class.java)