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
@@ -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,6 +320,7 @@ class BaresipService: Service() {
if (intent.action == Intent.ACTION_AIRPLANE_MODE_CHANGED) {
val isAirplaneModeOn = intent.getBooleanExtra("state", false)
Log.d(TAG, "Airplane mode changed: $isAirplaneModeOn")
if (VERSION.SDK_INT >= 29)
updateMobileStatus()
}
}
@@ -334,6 +337,7 @@ 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")
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,6 +1777,7 @@ class BaresipService: Service() {
Handler(Looper.getMainLooper()).post {
addMobileUserAgent()
if (VERSION.SDK_INT >= 29)
updateMobileStatus()
val mobileUa = uas.value.find { it.account.isMobile }
@@ -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,11 +2284,14 @@ 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 }
if (existingMobileUa == null && mobileAccountHandle == null)
return
val mobileAor = "sip:mobile@pstn"
val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
@@ -2300,11 +2330,13 @@ class BaresipService: Service() {
uas.value = updatedUas.toList()
uasStatus.value = UserAgent.statusMap()
registerTelephony()
Account.saveAccounts()
CallHistoryNew.save()
Message.save()
updateStatusNotification()
}
}
private fun toast(message: String, length: Int = Toast.LENGTH_SHORT) {
Handler(Looper.getMainLooper()).post {
@@ -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,17 +2374,20 @@ class BaresipService: Service() {
private fun updateMobileStatusFromServiceState(state: Int) {
if (state == previousMobileServiceState) return
previousMobileServiceState = state
if (VERSION.SDK_INT >= 29) {
val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady()
val status = if (state == ServiceState.STATE_IN_SERVICE && isSimReady)
val mobileAccountHandle = Utils.pstnAccountHandle(this)
val status = if (state == ServiceState.STATE_IN_SERVICE && isSimReady && mobileAccountHandle != null)
circleGreen.getValue(colorblind)
else if (isAirplaneModeOn || !isSimReady)
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")
private fun sendUssd(ussdCode: String) {
@@ -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,
@@ -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