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