Do heavy startup operations in background thread to prevent UI hangs and RemoteServiceException crashes on slow devices

Moved the foreground service promotion to the very start of the command handler to satisfy OS requirements
This commit is contained in:
Juha Heinanen
2026-06-25 20:00:06 +03:00
parent 0fcfc6abc5
commit a18235c2f0

View File

@ -507,6 +507,8 @@ class BaresipService: Service() {
}
if (isStartReceived || isServiceRunning) return START_STICKY
showStatusNotification()
isStartReceived = true
if (VERSION.SDK_INT < 31) {
@ -539,6 +541,14 @@ class BaresipService: Service() {
Config.initialize(this)
}
val userAgent = Config.variable("user_agent")
val software = if (userAgent != "")
userAgent
else
"baresip v${BuildConfig.VERSION_NAME} " +
"(Android ${VERSION.RELEASE}/${System.getProperty("os.arch") ?: "?"})"
Thread {
if (contactsMode != "android")
Contact.restoreBaresipContacts()
if (contactsMode != "baresip") {
@ -592,24 +602,16 @@ class BaresipService: Service() {
Log.i(TAG, "AEC/AGC/NS available = $aecAvailable/$agcAvailable/$nsAvailable")
val userAgent = Config.variable("user_agent")
Thread {
baresipStart(
filesPath,
addresses.removePrefix(";"),
logLevel,
if (userAgent != "")
userAgent
else
"baresip v${BuildConfig.VERSION_NAME} " +
"(Android ${VERSION.RELEASE}/${System.getProperty("os.arch") ?: "?"})"
software
)
}.start()
isServiceRunning = true
showStatusNotification()
if (linkAddresses.isEmpty())
toast(getString(R.string.no_network), Toast.LENGTH_LONG)
@ -1244,6 +1246,7 @@ class BaresipService: Service() {
"call closed" -> {
Log.d(TAG, "AoR $aor call $callp is closed prm: ${ev[1]}")
nm.cancel(CALL_NOTIFICATION_ID)
val connection = ConnectionService.connections[callp]
if (connection != null) {
val cause = when {
@ -2177,6 +2180,7 @@ class BaresipService: Service() {
fun handleExternalCallRemoved(telecomCall: android.telecom.Call) {
val callp = telecomCall.hashCode().toLong()
nm.cancel(CALL_NOTIFICATION_ID)
val call = calls.find { it.callp == callp }
if (call != null) {
val uap = call.ua.uap