Fixed crash when Service is started before CoreContext
This commit is contained in:
parent
4ebcfb9b09
commit
187fff9311
3 changed files with 35 additions and 8 deletions
|
@ -339,7 +339,7 @@ class CoreContext(
|
||||||
|
|
||||||
if (service != null) {
|
if (service != null) {
|
||||||
Log.i("[Context] Starting foreground service")
|
Log.i("[Context] Starting foreground service")
|
||||||
notificationsManager.startForeground(service, useAutoStartDescription)
|
notificationsManager.startForegroundToKeepAppAlive(service, useAutoStartDescription)
|
||||||
}
|
}
|
||||||
|
|
||||||
core = Factory.instance().createCoreWithConfig(coreConfig, context)
|
core = Factory.instance().createCoreWithConfig(coreConfig, context)
|
||||||
|
|
|
@ -46,7 +46,7 @@ class CoreService : CoreService() {
|
||||||
)
|
)
|
||||||
if (!contextCreated) {
|
if (!contextCreated) {
|
||||||
// Only start foreground notification if context already exists, otherwise context will do it itself
|
// 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) {
|
} else if (intent?.extras?.get("StartForeground") == true) {
|
||||||
Log.i("[Service] Starting as foreground due to device boot or app update")
|
Log.i("[Service] Starting as foreground due to device boot or app update")
|
||||||
|
@ -61,7 +61,7 @@ class CoreService : CoreService() {
|
||||||
coreContext.start()
|
coreContext.start()
|
||||||
} else {
|
} else {
|
||||||
// Only start foreground notification if context already exists, otherwise context will do it itself
|
// 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)
|
coreContext.checkIfForegroundServiceNotificationCanBeRemovedAfterDelay(5000)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -484,7 +484,7 @@ class NotificationsManager(private val context: Context) {
|
||||||
|
|
||||||
val coreService = service
|
val coreService = service
|
||||||
if (coreService != null) {
|
if (coreService != null) {
|
||||||
startForeground(coreService, useAutoStartDescription = false)
|
startForeground(coreService)
|
||||||
} else {
|
} else {
|
||||||
Log.w(
|
Log.w(
|
||||||
"[Notifications Manager] Can't start service as foreground without a service, starting it now"
|
"[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
|
service = coreService
|
||||||
|
|
||||||
val notification = serviceNotification ?: createServiceNotification(useAutoStartDescription)
|
val notification = serviceNotification ?: createServiceNotification(false)
|
||||||
if (notification == null) {
|
if (notification == null) {
|
||||||
Log.e(
|
Log.e(
|
||||||
"[Notifications Manager] Failed to create service notification, aborting foreground service!"
|
"[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(
|
private fun startForeground(
|
||||||
notificationId: Int,
|
notificationId: Int,
|
||||||
callNotification: Notification,
|
callNotification: Notification,
|
||||||
|
@ -884,7 +911,7 @@ class NotificationsManager(private val context: Context) {
|
||||||
startForeground(notifiable.notificationId, notification, isCallActive)
|
startForeground(notifiable.notificationId, notification, isCallActive)
|
||||||
} else if (coreService != null && currentForegroundServiceNotificationId == SERVICE_NOTIF_ID) {
|
} else if (coreService != null && currentForegroundServiceNotificationId == SERVICE_NOTIF_ID) {
|
||||||
// To add microphone & camera foreground service use to foreground service if needed
|
// 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
|
// To remove microphone & camera foreground service use to foreground service if needed
|
||||||
val coreService = service
|
val coreService = service
|
||||||
if (coreService != null && currentForegroundServiceNotificationId == SERVICE_NOTIF_ID) {
|
if (coreService != null && currentForegroundServiceNotificationId == SERVICE_NOTIF_ID) {
|
||||||
startForeground(coreService, useAutoStartDescription = false)
|
startForeground(coreService)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue