Do not create mobile account automatically

After restore, do not show mobile account status as green if baresip is not anymore default phone app
This commit is contained in:
Juha Heinanen
2026-06-30 19:40:19 +03:00
parent 880ae5d332
commit 6f42723ff9
3 changed files with 116 additions and 72 deletions

View File

@ -40,6 +40,7 @@ import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.net.Uri
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Build.VERSION
import android.os.CountDownTimer
import android.os.Handler
@ -64,6 +65,7 @@ import android.view.View
import android.widget.RemoteViews
import android.widget.Toast
import androidx.annotation.Keep
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@ -318,7 +320,8 @@ class BaresipService: Service() {
if (intent.action == Intent.ACTION_AIRPLANE_MODE_CHANGED) {
val isAirplaneModeOn = intent.getBooleanExtra("state", false)
Log.d(TAG, "Airplane mode changed: $isAirplaneModeOn")
updateMobileStatus()
if (VERSION.SDK_INT >= 29)
updateMobileStatus()
}
}
}
@ -334,7 +337,8 @@ class BaresipService: Service() {
override fun onReceive(ctx: Context, intent: Intent) {
if (intent.action == "android.intent.action.SIM_STATE_CHANGED") {
Log.d(TAG, "SIM state changed")
updateMobileStatus()
if (VERSION.SDK_INT >= 29)
updateMobileStatus()
}
}
}
@ -836,9 +840,27 @@ class BaresipService: Service() {
if (ev[0] == "create") {
val ua = UserAgent(uap)
ua.status = if (ua.account.isMobile)
R.drawable.circle_green
else if (ua.account.regint == 0)
if (VERSION.SDK_INT < 29 && ua.account.isMobile) {
Log.d(TAG, "Removing Mobile account on API < 29")
CallHistoryNew.clear(ua.account.aor)
Message.clearMessagesOfAor(ua.account.aor)
Api.ua_destroy(uap)
Account.saveAccounts()
return
}
ua.status = if (ua.account.isMobile) {
val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady()
if (VERSION.SDK_INT >= 29) {
if (Utils.pstnAccountHandle(this) == null || !isSimReady || isAirplaneModeOn)
R.drawable.circle_white
else
circleRed.getValue(colorblind)
} else {
R.drawable.circle_white
}
} else if (ua.account.regint == 0)
R.drawable.circle_white
else
circleYellow.getValue(colorblind)
@ -1755,7 +1777,8 @@ class BaresipService: Service() {
Handler(Looper.getMainLooper()).post {
addMobileUserAgent()
updateMobileStatus()
if (VERSION.SDK_INT >= 29)
updateMobileStatus()
val mobileUa = uas.value.find { it.account.isMobile }
if (mobileUa != null)
@ -1777,6 +1800,18 @@ class BaresipService: Service() {
}
}
registerTelephony()
Api.net_debug()
postServiceEvent(ServiceEvent("started", arrayListOf(callActionUri), System.nanoTime()))
callActionUri = ""
Log.d(TAG, "Battery optimizations are ignored: " +
"${pm.isIgnoringBatteryOptimizations(packageName)}")
Log.d(TAG, "Partial wake lock/wifi lock is held: " +
"${partialWakeLock.isHeld}/${wifiLock.isHeld}")
updateStatusNotification()
}
fun registerTelephony() {
if (VERSION.SDK_INT >= 31 && !telephonyCallbackRegistered)
try {
telephonyManager.registerTelephonyCallback(mainExecutor, telephonyCallback)
@ -1791,14 +1826,6 @@ class BaresipService: Service() {
telephonyCallbackRegistered = true
Log.d(TAG, "Registered PhoneStateListener")
}
Api.net_debug()
postServiceEvent(ServiceEvent("started", arrayListOf(callActionUri), System.nanoTime()))
callActionUri = ""
Log.d(TAG, "Battery optimizations are ignored: " +
"${pm.isIgnoringBatteryOptimizations(packageName)}")
Log.d(TAG, "Partial wake lock/wifi lock is held: " +
"${partialWakeLock.isHeld}/${wifiLock.isHeld}")
updateStatusNotification()
}
@Suppress("unused")
@ -2257,53 +2284,58 @@ class BaresipService: Service() {
}
fun addMobileUserAgent() {
if (VERSION.SDK_INT < 29) return
if (VERSION.SDK_INT >= 29) {
val mobileAccountHandle = Utils.pstnAccountHandle(this)
val existingMobileUa = uas.value.find { it.account.isMobile }
val mobileAccountHandle = Utils.pstnAccountHandle(this)
val existingMobileUa = uas.value.find { it.account.isMobile }
val mobileAor = "sip:mobile@pstn"
if (existingMobileUa == null && mobileAccountHandle == null)
return
val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady()
val status = if (mobileAccountHandle == null || !isSimReady)
R.drawable.circle_white
else if (existingMobileUa?.status == circleGreen.getValue(colorblind))
circleGreen.getValue(colorblind)
else if (isAirplaneModeOn)
R.drawable.circle_white
else
circleRed.getValue(colorblind)
val mobileAor = "sip:mobile@pstn"
if (existingMobileUa != null) {
if (existingMobileUa.status != status) {
Log.d(TAG, "Updating existing Mobile UA status to $status")
existingMobileUa.updateStatus(status)
updateStatusNotification()
if (isMainVisible)
registrationUpdate.postValue(System.currentTimeMillis())
val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady()
val status = if (mobileAccountHandle == null || !isSimReady)
R.drawable.circle_white
else if (existingMobileUa?.status == circleGreen.getValue(colorblind))
circleGreen.getValue(colorblind)
else if (isAirplaneModeOn)
R.drawable.circle_white
else
circleRed.getValue(colorblind)
if (existingMobileUa != null) {
if (existingMobileUa.status != status) {
Log.d(TAG, "Updating existing Mobile UA status to $status")
existingMobileUa.updateStatus(status)
updateStatusNotification()
if (isMainVisible)
registrationUpdate.postValue(System.currentTimeMillis())
}
return
}
return
Log.d(TAG, "Injecting new virtual Mobile account: $mobileAor")
val account = Account(0L, mobileAor)
account.isMobile = true
account.nickName = "Mobile"
account.regint = 0
account.telProvider = ""
val mobileUa = UserAgent(0L, account)
mobileUa.status = status
val updatedUas = uas.value.toMutableList()
updatedUas.add(mobileUa)
uas.value = updatedUas.toList()
uasStatus.value = UserAgent.statusMap()
registerTelephony()
Account.saveAccounts()
CallHistoryNew.save()
Message.save()
updateStatusNotification()
}
Log.d(TAG, "Injecting new virtual Mobile account: $mobileAor")
val account = Account(0L, mobileAor)
account.isMobile = true
account.nickName = "Mobile"
account.regint = 0
account.telProvider = ""
val mobileUa = UserAgent(0L, account)
mobileUa.status = status
val updatedUas = uas.value.toMutableList()
updatedUas.add(mobileUa)
uas.value = updatedUas.toList()
uasStatus.value = UserAgent.statusMap()
Account.saveAccounts()
CallHistoryNew.save()
Message.save()
updateStatusNotification()
}
private fun toast(message: String, length: Int = Toast.LENGTH_SHORT) {
@ -2312,11 +2344,13 @@ class BaresipService: Service() {
}
}
@RequiresApi(Build.VERSION_CODES.Q)
private fun updateMobileStatus(newStatus: Int? = null) {
uas.value.find { it.account.isMobile }?.let { ua ->
val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady()
val status = newStatus ?: if (!isSimReady)
val mobileAccountHandle = Utils.pstnAccountHandle(this)
val status = newStatus ?: if (mobileAccountHandle == null || !isSimReady)
R.drawable.circle_white
else if (ua.status == circleGreen.getValue(colorblind))
ua.status
@ -2340,16 +2374,19 @@ class BaresipService: Service() {
private fun updateMobileStatusFromServiceState(state: Int) {
if (state == previousMobileServiceState) return
previousMobileServiceState = state
val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady()
val status = if (state == ServiceState.STATE_IN_SERVICE && isSimReady)
circleGreen.getValue(colorblind)
else if (isAirplaneModeOn || !isSimReady)
R.drawable.circle_white
else
circleRed.getValue(colorblind)
Log.d(TAG, "Mobile service state changed: $state, updating status to $status (Airplane=$isAirplaneModeOn)")
updateMobileStatus(status)
if (VERSION.SDK_INT >= 29) {
val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady()
val mobileAccountHandle = Utils.pstnAccountHandle(this)
val status = if (state == ServiceState.STATE_IN_SERVICE && isSimReady && mobileAccountHandle != null)
circleGreen.getValue(colorblind)
else if (mobileAccountHandle == null || isAirplaneModeOn || !isSimReady)
R.drawable.circle_white
else
circleRed.getValue(colorblind)
Log.d(TAG, "Mobile service state changed: $state, updating status to $status (Airplane=$isAirplaneModeOn)")
updateMobileStatus(status)
}
}
@SuppressLint("MissingPermission")

View File

@ -977,11 +977,14 @@ private fun SettingsContent(
val roleManager = ctx.getSystemService(ROLE_SERVICE) as RoleManager
val requestPermissionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted)
ActivityResultContracts.RequestMultiplePermissions()
) { results ->
if (results[Manifest.permission.READ_PHONE_STATE] == true)
Log.d(TAG, "READ_PHONE_STATE permission granted")
if (results[Manifest.permission.READ_PHONE_NUMBERS] == true)
Log.d(TAG, "READ_PHONE_NUMBERS permission granted")
BaresipService.instance?.addMobileUserAgent()
restart = true
}
val dialerRoleRequest = rememberLauncherForActivityResult(
@ -990,13 +993,17 @@ private fun SettingsContent(
val isHeld = roleManager.isRoleHeld(RoleManager.ROLE_DIALER)
viewModel.defaultDialer.value = isHeld
if (isHeld) {
if (Utils.checkPermissions(ctx, arrayOf(Manifest.permission.READ_PHONE_NUMBERS)))
val permissions = arrayOf(Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_PHONE_NUMBERS)
if (Utils.checkPermissions(ctx, permissions)) {
BaresipService.instance?.addMobileUserAgent()
restart = true
}
else
requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_NUMBERS)
requestPermissionLauncher.launch(permissions)
}
else
BaresipService.instance?.addMobileUserAgent()
restart = true
}
Switch(
checked = defaultDialer,

View File

@ -8,7 +8,7 @@ import com.tutpro.baresip.BaresipService.Companion.uasStatus
class UserAgent(val uap: Long, virtualAccount: Account? = null) {
val account = virtualAccount ?: Account(Api.ua_account(uap))
var status = if (uap != 0L) R.drawable.circle_white else R.drawable.circle_green
var status = R.drawable.circle_white
fun callAlloc(xCall: Long, videoMode: Int): Long {
if (uap == 0L) return 0L