Add additional foreground service timeout check.

This commit is contained in:
Michelle Tang
2025-08-05 10:20:20 -04:00
committed by GitHub
parent 2ace1e75e1
commit 87af103012

View File

@@ -178,6 +178,10 @@ class GenericForegroundService : Service() {
override fun onTimeout(startId: Int, foregroundServiceType: Int) {
Log.i(TAG, "[onTimeout] startId: $startId, fgsType: $foregroundServiceType")
stopDueToTimeout()
}
private fun stopDueToTimeout() {
lock.withLock {
hasTimedOut = true
allActiveMessages.clear()
@@ -217,16 +221,25 @@ class GenericForegroundService : Service() {
private fun postObligatoryForegroundNotification(active: Entry) {
lastPosted = active
startForeground(
NOTIFICATION_ID,
NotificationCompat.Builder(this, active.channelId)
.setSmallIcon(active.iconRes)
.setContentTitle(active.title)
.setProgress(active.progressMax, active.progress, active.indeterminate)
.setContentIntent(PendingIntent.getActivity(this, 0, MainActivity.clearTop(this), mutable()))
.setVibrate(longArrayOf(0))
.build()
)
try {
startForeground(
NOTIFICATION_ID,
NotificationCompat.Builder(this, active.channelId)
.setSmallIcon(active.iconRes)
.setContentTitle(active.title)
.setProgress(active.progressMax, active.progress, active.indeterminate)
.setContentIntent(PendingIntent.getActivity(this, 0, MainActivity.clearTop(this), mutable()))
.setVibrate(longArrayOf(0))
.build()
)
} catch (e: Exception) {
if (Build.VERSION.SDK_INT >= 31 && e.message?.contains("Time limit", ignoreCase = true) == true) {
Log.w(TAG, "Foreground service timed out, but not in onTimeout call", e)
stopDueToTimeout()
} else {
throw e
}
}
}
private fun updateEntry(id: Int, transform: (Entry) -> Entry) {