Avoid douple mobile account in account spinner when default phone app setting is enabled

This commit is contained in:
Juha Heinanen
2026-08-07 22:20:28 +03:00
parent 7ea2b7699d
commit 88664e709c
3 changed files with 47 additions and 21 deletions

View File

@ -866,22 +866,34 @@ class BaresipService: Service() {
if (ev[0] == "create") { if (ev[0] == "create") {
val ua = UserAgent(uap) val ua = UserAgent(uap)
if (VERSION.SDK_INT < 29 && ua.account.isMobile) { if (Utils.uriMatch(ua.account.aor, "sip:mobile@pstn"))
ua.account.isMobile = true
var removeMobile = false
if (ua.account.isMobile) {
if (VERSION.SDK_INT < 29) {
Log.d(TAG, "Removing Mobile account on API < 29") Log.d(TAG, "Removing Mobile account on API < 29")
removeMobile = true
} else if (Utils.pstnAccountHandle(this) == null || !isSimReady()) {
Log.d(TAG, "Removing Mobile account (SIM not ready or not default dialer)")
removeMobile = true
}
}
if (removeMobile) {
CallHistoryNew.clear(ua.account.aor) CallHistoryNew.clear(ua.account.aor)
Message.clearMessagesOfAor(ua.account.aor) Message.clearMessagesOfAor(ua.account.aor)
Blocked.clear(ua.account.aor) Blocked.clear(ua.account.aor)
BlockRule.clear(ua.account.aor) BlockRule.clear(ua.account.aor)
Api.ua_destroy(uap) Api.ua_destroy(uap)
Account.saveAccounts() if (isNativeReady) Account.saveAccounts()
return return
} }
ua.status = if (ua.account.isMobile) { ua.status = if (ua.account.isMobile) {
val isAirplaneModeOn = Utils.isAirplaneModeOn(this) val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady()
if (VERSION.SDK_INT >= 29) { if (VERSION.SDK_INT >= 29) {
if (Utils.pstnAccountHandle(this) == null || !isSimReady || isAirplaneModeOn) if (isAirplaneModeOn)
R.drawable.circle_white R.drawable.circle_white
else else
circleRed.getValue(colorblind) circleRed.getValue(colorblind)
@ -905,7 +917,7 @@ class BaresipService: Service() {
Api.account_set_auth_pass(acc.accp, NO_AUTH_PASS) Api.account_set_auth_pass(acc.accp, NO_AUTH_PASS)
} }
Log.d(TAG, "got uaEvent $event/${acc.aor}") Log.d(TAG, "got uaEvent $ev/${acc.aor}")
return return
} }
@ -1787,6 +1799,7 @@ class BaresipService: Service() {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
addMobileUserAgent() addMobileUserAgent()
Account.saveAccounts()
if (VERSION.SDK_INT >= 29) if (VERSION.SDK_INT >= 29)
updateMobileStatus() updateMobileStatus()
@ -2303,7 +2316,7 @@ class BaresipService: Service() {
val mobileAccountHandle = Utils.pstnAccountHandle(this) val mobileAccountHandle = Utils.pstnAccountHandle(this)
val isSimReady = isSimReady() val isSimReady = isSimReady()
val existingMobileUa = uas.value.find { it.account.isMobile } val existingMobileUa = uas.value.find { it.account.isMobile || Utils.uriMatch(it.account.aor, "sip:mobile@pstn") }
if (mobileAccountHandle == null || !isSimReady) { if (mobileAccountHandle == null || !isSimReady) {
if (existingMobileUa != null) { if (existingMobileUa != null) {
@ -2311,7 +2324,7 @@ class BaresipService: Service() {
if (existingMobileUa.uap != 0L) if (existingMobileUa.uap != 0L)
Api.ua_destroy(existingMobileUa.uap) Api.ua_destroy(existingMobileUa.uap)
existingMobileUa.remove() existingMobileUa.remove()
Account.saveAccounts() if (isNativeReady) Account.saveAccounts()
updateStatusNotification() updateStatusNotification()
} }
return return
@ -2347,13 +2360,10 @@ class BaresipService: Service() {
val mobileUa = UserAgent(0L, account) val mobileUa = UserAgent(0L, account)
mobileUa.status = status mobileUa.status = status
val updatedUas = uas.value.toMutableList() mobileUa.add()
updatedUas.add(mobileUa)
uas.value = updatedUas.toList()
uasStatus.value = UserAgent.statusMap()
registerTelephony() registerTelephony()
Account.saveAccounts() if (isNativeReady) Account.saveAccounts()
CallHistoryNew.save() CallHistoryNew.save()
Message.save() Message.save()
updateStatusNotification() updateStatusNotification()

View File

@ -18,7 +18,16 @@ class UserAgent(val uap: Long, virtualAccount: Account? = null) {
fun add() { fun add() {
synchronized(uas) { synchronized(uas) {
val updatedUas = uas.value.toMutableList() val updatedUas = uas.value.toMutableList()
val index = updatedUas.indexOfFirst { Utils.uriMatch(it.account.aor, this.account.aor) }
if (index == -1) {
updatedUas.add(this) updatedUas.add(this)
} else {
if (updatedUas[index].uap == 0L && this.uap != 0L) {
updatedUas[index] = this
} else {
return
}
}
uas.value = updatedUas.toList() uas.value = updatedUas.toList()
uasStatus.value = statusMap() uasStatus.value = statusMap()
} }

View File

@ -152,11 +152,14 @@ object Utils {
} }
fun uriMatch(firstUri: String, secondUri: String): Boolean { fun uriMatch(firstUri: String, secondUri: String): Boolean {
if (firstUri.startsWith("tel:")) val first = firstUri.removePrefix("<").removeSuffix(">")
return firstUri == secondUri || firstUri.substringAfter(":") == uriUserPart(secondUri) val second = secondUri.removePrefix("<").removeSuffix(">")
if (firstUri.startsWith("sip:")) if (first.startsWith("tel:", ignoreCase = true))
return uriUserPart(firstUri) == uriUserPart(secondUri) && return first.equals(second, ignoreCase = true) ||
uriHostPart(firstUri) == uriHostPart(secondUri) first.substringAfter(":").equals(uriUserPart(second), ignoreCase = true)
if (first.startsWith("sip:", ignoreCase = true))
return uriUserPart(first).equals(uriUserPart(second), ignoreCase = true) &&
uriHostPart(first).equals(uriHostPart(second), ignoreCase = true)
return false return false
} }
@ -346,14 +349,18 @@ object Utils {
fun paramValue(params: String, name: String): String { fun paramValue(params: String, name: String): String {
if (params == "") return "" if (params == "") return ""
for (param in params.split(";")) for (p in params.split(";")) {
val param = p.trim()
if (param.substringBefore("=") == name) return param.substringAfter("=") if (param.substringBefore("=") == name) return param.substringAfter("=")
}
return "" return ""
} }
fun paramExists(params: String, name: String): Boolean { fun paramExists(params: String, name: String): Boolean {
for (param in params.split(";")) for (p in params.split(";")) {
val param = p.trim()
if (param.substringBefore("=") == name) return true if (param.substringBefore("=") == name) return true
}
return false return false
} }