From d6c6de2b5e0a2dd959b0ca37e493ea532a4761f2 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 17 Mar 2025 17:36:18 +0100 Subject: [PATCH] Wait for foreground service to be started before being stopped to try preventing ForegroundServiceDidNotStartInTimeException/RemoteServiceException due to Context.startForegroundService() did not then call Service.startForeground() --- .../notifications/NotificationsManager.kt | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index b79b8b68c..553000ad5 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -114,6 +114,9 @@ class NotificationsManager } private var inCallService: CoreInCallService? = null + private var inCallServiceForegroundNotificationPublished = false + private var waitForInCallServiceForegroundToStopIt = false + private var keepAliveService: CoreKeepAliveThirdPartyAccountsService? = null private val callNotificationsMap: HashMap = HashMap() @@ -255,7 +258,13 @@ class NotificationsManager @WorkerThread override fun onLastCallEnded(core: Core) { Log.i("$TAG Last call ended, stopping foreground service") - stopInCallCallForegroundService() + if (inCallServiceForegroundNotificationPublished) { + stopInCallCallForegroundService() + } else { + // Wait for foreground service to have been started before stopping it + Log.w("$TAG We would like to stop the foreground service but it wasn't started yet, wait for it") + waitForInCallServiceForegroundToStopIt = true + } } @WorkerThread @@ -485,7 +494,13 @@ class NotificationsManager coreContext.postOnCoreThread { core -> if (core.callsNb == 0) { Log.w("$TAG No call anymore, stopping service") - stopInCallCallForegroundService() + if (inCallServiceForegroundNotificationPublished) { + stopInCallCallForegroundService() + } else { + // Wait for foreground service to have been started before stopping it + Log.w("$TAG We would like to stop the foreground service but it wasn't started yet, wait for it") + waitForInCallServiceForegroundToStopIt = true + } } else if (currentInCallServiceNotificationId == -1) { Log.i( "$TAG At least a call is still running and no foreground Service notification was found" @@ -702,6 +717,11 @@ class NotificationsManager Compatibility.FOREGROUND_SERVICE_TYPE_PHONE_CALL ) currentInCallServiceNotificationId = INCOMING_CALL_ID + inCallServiceForegroundNotificationPublished = true + if (waitForInCallServiceForegroundToStopIt) { + Log.i("$TAG We were waiting for foreground service to be started to stop it, doing it") + stopInCallCallForegroundService() + } } else { Log.e("$TAG POST_NOTIFICATIONS permission isn't granted, don't start foreground service!") } @@ -808,6 +828,11 @@ class NotificationsManager mask ) currentInCallServiceNotificationId = notifiable.notificationId + inCallServiceForegroundNotificationPublished = true + if (waitForInCallServiceForegroundToStopIt) { + Log.i("$TAG We were waiting for foreground service to be started to stop it, doing it") + stopInCallCallForegroundService() + } } else { Log.e("$TAG POST_NOTIFICATIONS permission isn't granted, don't start foreground service!") } @@ -822,6 +847,7 @@ class NotificationsManager ) service.stopForeground(STOP_FOREGROUND_REMOVE) service.stopSelf() + inCallServiceForegroundNotificationPublished = false } else { Log.w("$TAG Can't stop foreground Service & notif, no Service was found") }