diff --git a/app/src/main/java/org/linphone/core/CoreContext.kt b/app/src/main/java/org/linphone/core/CoreContext.kt index b6dc6c77c..812642c77 100644 --- a/app/src/main/java/org/linphone/core/CoreContext.kt +++ b/app/src/main/java/org/linphone/core/CoreContext.kt @@ -339,7 +339,7 @@ class CoreContext( if (service != null) { Log.i("[Context] Starting foreground service") - notificationsManager.startForeground(service, useAutoStartDescription) + notificationsManager.startForegroundToKeepAppAlive(service, useAutoStartDescription) } core = Factory.instance().createCoreWithConfig(coreConfig, context) diff --git a/app/src/main/java/org/linphone/core/CoreService.kt b/app/src/main/java/org/linphone/core/CoreService.kt index 5cc609cfd..fbafc16d8 100644 --- a/app/src/main/java/org/linphone/core/CoreService.kt +++ b/app/src/main/java/org/linphone/core/CoreService.kt @@ -46,7 +46,7 @@ class CoreService : CoreService() { ) if (!contextCreated) { // Only start foreground notification if context already exists, otherwise context will do it itself - coreContext.notificationsManager.startForeground(this, false) + coreContext.notificationsManager.startForegroundToKeepAppAlive(this, false) } } else if (intent?.extras?.get("StartForeground") == true) { Log.i("[Service] Starting as foreground due to device boot or app update") @@ -61,7 +61,7 @@ class CoreService : CoreService() { coreContext.start() } else { // Only start foreground notification if context already exists, otherwise context will do it itself - coreContext.notificationsManager.startForeground(this, true) + coreContext.notificationsManager.startForegroundToKeepAppAlive(this, true) } coreContext.checkIfForegroundServiceNotificationCanBeRemovedAfterDelay(5000) } else { diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index a65995421..d28715188 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -484,7 +484,7 @@ class NotificationsManager(private val context: Context) { val coreService = service if (coreService != null) { - startForeground(coreService, useAutoStartDescription = false) + startForeground(coreService) } else { Log.w( "[Notifications Manager] Can't start service as foreground without a service, starting it now" @@ -536,10 +536,10 @@ class NotificationsManager(private val context: Context) { } } - fun startForeground(coreService: CoreService, useAutoStartDescription: Boolean = true) { + private fun startForeground(coreService: CoreService) { service = coreService - val notification = serviceNotification ?: createServiceNotification(useAutoStartDescription) + val notification = serviceNotification ?: createServiceNotification(false) if (notification == null) { Log.e( "[Notifications Manager] Failed to create service notification, aborting foreground service!" @@ -571,6 +571,33 @@ class NotificationsManager(private val context: Context) { ) } + fun startForegroundToKeepAppAlive( + coreService: CoreService, + useAutoStartDescription: Boolean = true + ) { + service = coreService + + val notification = serviceNotification ?: createServiceNotification(useAutoStartDescription) + if (notification == null) { + Log.e( + "[Notifications Manager] Failed to create service notification, aborting foreground service!" + ) + return + } + + currentForegroundServiceNotificationId = SERVICE_NOTIF_ID + Log.i( + "[Notifications Manager] Starting service as foreground [$currentForegroundServiceNotificationId]" + ) + + Compatibility.startDataSyncForegroundService( + coreService, + currentForegroundServiceNotificationId, + notification, + false + ) + } + private fun startForeground( notificationId: Int, callNotification: Notification, @@ -884,7 +911,7 @@ class NotificationsManager(private val context: Context) { startForeground(notifiable.notificationId, notification, isCallActive) } else if (coreService != null && currentForegroundServiceNotificationId == SERVICE_NOTIF_ID) { // To add microphone & camera foreground service use to foreground service if needed - startForeground(coreService, useAutoStartDescription = false) + startForeground(coreService) } } @@ -901,7 +928,7 @@ class NotificationsManager(private val context: Context) { // To remove microphone & camera foreground service use to foreground service if needed val coreService = service if (coreService != null && currentForegroundServiceNotificationId == SERVICE_NOTIF_ID) { - startForeground(coreService, useAutoStartDescription = false) + startForeground(coreService) } }