Add log if foreground service fails to start + finish call activity in onResume if no more call to display

This commit is contained in:
Sylvain Berfini 2026-02-17 17:21:34 +01:00
parent 2dde1c2509
commit c50acdf8bc
5 changed files with 30 additions and 9 deletions

View file

@ -34,15 +34,17 @@ class Api28Compatibility {
companion object { companion object {
private const val TAG = "[API 28 Compatibility]" private const val TAG = "[API 28 Compatibility]"
fun startServiceForeground(service: Service, id: Int, notification: Notification) { fun startServiceForeground(service: Service, id: Int, notification: Notification): Boolean {
try { try {
service.startForeground( service.startForeground(
id, id,
notification notification
) )
return true
} catch (e: Exception) { } catch (e: Exception) {
Log.e("$TAG Can't start service as foreground! $e") Log.e("$TAG Can't start service as foreground! $e")
} }
return false
} }
fun enterPipMode(activity: Activity): Boolean { fun enterPipMode(activity: Activity): Boolean {

View file

@ -44,16 +44,18 @@ class Api34Compatibility {
id: Int, id: Int,
notification: Notification, notification: Notification,
foregroundServiceType: Int foregroundServiceType: Int
) { ): Boolean {
try { try {
service.startForeground( service.startForeground(
id, id,
notification, notification,
foregroundServiceType foregroundServiceType
) )
return true
} catch (e: Exception) { } catch (e: Exception) {
Log.e("$TAG Can't start service as foreground! $e") Log.e("$TAG Can't start service as foreground! $e")
} }
return false
} }
fun hasFullScreenIntentPermission(context: Context): Boolean { fun hasFullScreenIntentPermission(context: Context): Boolean {

View file

@ -52,16 +52,16 @@ class Compatibility {
id: Int, id: Int,
notification: Notification, notification: Notification,
foregroundServiceType: Int foregroundServiceType: Int
) { ): Boolean {
if (Version.sdkAboveOrEqual(Version.API34_ANDROID_14_UPSIDE_DOWN_CAKE)) { if (Version.sdkAboveOrEqual(Version.API34_ANDROID_14_UPSIDE_DOWN_CAKE)) {
Api34Compatibility.startServiceForeground( return Api34Compatibility.startServiceForeground(
service, service,
id, id,
notification, notification,
foregroundServiceType foregroundServiceType
) )
} else { } else {
Api28Compatibility.startServiceForeground(service, id, notification) return Api28Compatibility.startServiceForeground(service, id, notification)
} }
} }

View file

@ -805,12 +805,15 @@ class NotificationsManager
Log.i( Log.i(
"$TAG Service found, starting it as foreground using notification ID [$INCOMING_CALL_ID] with type PHONE_CALL" "$TAG Service found, starting it as foreground using notification ID [$INCOMING_CALL_ID] with type PHONE_CALL"
) )
Compatibility.startServiceForeground( val success = Compatibility.startServiceForeground(
service, service,
INCOMING_CALL_ID, INCOMING_CALL_ID,
notification, notification,
Compatibility.FOREGROUND_SERVICE_TYPE_PHONE_CALL Compatibility.FOREGROUND_SERVICE_TYPE_PHONE_CALL
) )
if (!success) {
Log.e("$TAG Failed to start incoming call foreground service!")
}
notificationsMap[INCOMING_CALL_ID] = notification notificationsMap[INCOMING_CALL_ID] = notification
currentInCallServiceNotificationId = INCOMING_CALL_ID currentInCallServiceNotificationId = INCOMING_CALL_ID
inCallServiceForegroundNotificationPublished = true inCallServiceForegroundNotificationPublished = true
@ -926,12 +929,15 @@ class NotificationsManager
Log.i( Log.i(
"$TAG Service found, starting it as foreground using notification ID [${notifiable.notificationId}] with type(s) [${foregroundServiceTypeMaskToString(mask)}]($mask)" "$TAG Service found, starting it as foreground using notification ID [${notifiable.notificationId}] with type(s) [${foregroundServiceTypeMaskToString(mask)}]($mask)"
) )
Compatibility.startServiceForeground( val success = Compatibility.startServiceForeground(
service, service,
notifiable.notificationId, notifiable.notificationId,
notification, notification,
mask mask
) )
if (!success) {
Log.e("$TAG Failed to start call foreground service!")
}
notificationsMap[notifiable.notificationId] = notification notificationsMap[notifiable.notificationId] = notification
currentInCallServiceNotificationId = notifiable.notificationId currentInCallServiceNotificationId = notifiable.notificationId
inCallServiceForegroundNotificationPublished = true inCallServiceForegroundNotificationPublished = true
@ -977,12 +983,15 @@ class NotificationsManager
Log.i( Log.i(
"$TAG Service found, starting it as foreground using dummy notification ID [$DUMMY_NOTIF_ID]" "$TAG Service found, starting it as foreground using dummy notification ID [$DUMMY_NOTIF_ID]"
) )
Compatibility.startServiceForeground( val success = Compatibility.startServiceForeground(
service, service,
DUMMY_NOTIF_ID, DUMMY_NOTIF_ID,
notification, notification,
Compatibility.FOREGROUND_SERVICE_TYPE_PHONE_CALL Compatibility.FOREGROUND_SERVICE_TYPE_PHONE_CALL
) )
if (!success) {
Log.e("$TAG Failed to start dummy call foreground service!")
}
notificationsMap[INCOMING_CALL_ID] = notification notificationsMap[INCOMING_CALL_ID] = notification
currentInCallServiceNotificationId = DUMMY_NOTIF_ID currentInCallServiceNotificationId = DUMMY_NOTIF_ID
inCallServiceForegroundNotificationPublished = true inCallServiceForegroundNotificationPublished = true
@ -1810,12 +1819,15 @@ class NotificationsManager
Log.i( Log.i(
"$TAG Keep alive for third party accounts Service found, starting it as foreground using notification ID [$KEEP_ALIVE_FOR_THIRD_PARTY_ACCOUNTS_ID] with type [SPECIAL_USE]" "$TAG Keep alive for third party accounts Service found, starting it as foreground using notification ID [$KEEP_ALIVE_FOR_THIRD_PARTY_ACCOUNTS_ID] with type [SPECIAL_USE]"
) )
Compatibility.startServiceForeground( val success = Compatibility.startServiceForeground(
service, service,
KEEP_ALIVE_FOR_THIRD_PARTY_ACCOUNTS_ID, KEEP_ALIVE_FOR_THIRD_PARTY_ACCOUNTS_ID,
notification, notification,
Compatibility.FOREGROUND_SERVICE_TYPE_SPECIAL_USE Compatibility.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
) )
if (!success) {
Log.e("$TAG Failed to start keep alive foreground service!")
}
currentKeepAliveThirdPartyAccountsForegroundServiceNotificationId = KEEP_ALIVE_FOR_THIRD_PARTY_ACCOUNTS_ID currentKeepAliveThirdPartyAccountsForegroundServiceNotificationId = KEEP_ALIVE_FOR_THIRD_PARTY_ACCOUNTS_ID
} else { } else {
Log.w("$TAG Keep alive for third party accounts Service hasn't started yet...") Log.w("$TAG Keep alive for third party accounts Service hasn't started yet...")

View file

@ -376,6 +376,11 @@ class CallActivity : GenericActivity() {
if (::callViewModel.isInitialized) { if (::callViewModel.isInitialized) {
callViewModel.pipMode.value = isInPipMode callViewModel.pipMode.value = isInPipMode
} }
if (callsViewModel.callsCount.value == 0) {
Log.w("$TAG Call activity is being resumed but no call was found, finishing activity")
finish()
}
} }
override fun onPause() { override fun onPause() {