Catch foreground service not allowed exceptions.

This commit is contained in:
Michelle Tang
2025-09-02 17:31:52 -04:00
committed by Greyson Parrelli
parent 712616e569
commit 3b715bc461

View File

@@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.service
import android.app.ForegroundServiceStartNotAllowedException
import android.app.PendingIntent
import android.app.Service
import android.content.Context
@@ -190,6 +191,14 @@ class GenericForegroundService : Service() {
stopSelf()
}
private fun stop() {
lock.withLock {
allActiveMessages.clear()
}
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
stopSelf()
}
fun replaceTitle(id: Int, title: String) {
lock.withLock {
updateEntry(id) { oldEntry ->
@@ -236,7 +245,11 @@ class GenericForegroundService : Service() {
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 if (Build.VERSION.SDK_INT >= 31 && e is ForegroundServiceStartNotAllowedException) {
Log.w(TAG, "Unable to start foreground service", e)
stop()
} else {
Log.w(TAG, "Unhandled exception being thrown when starting a foreground service.", e)
throw e
}
}