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.NetworkRequest
import android.net.Uri import android.net.Uri
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.os.Build
import android.os.Build.VERSION import android.os.Build.VERSION
import android.os.CountDownTimer import android.os.CountDownTimer
import android.os.Handler import android.os.Handler
@@ -64,6 +65,7 @@ import android.view.View
import android.widget.RemoteViews import android.widget.RemoteViews
import android.widget.Toast import android.widget.Toast
import androidx.annotation.Keep import androidx.annotation.Keep
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@@ -318,6 +320,7 @@ class BaresipService: Service() {
if (intent.action == Intent.ACTION_AIRPLANE_MODE_CHANGED) { if (intent.action == Intent.ACTION_AIRPLANE_MODE_CHANGED) {
val isAirplaneModeOn = intent.getBooleanExtra("state", false) val isAirplaneModeOn = intent.getBooleanExtra("state", false)
Log.d(TAG, "Airplane mode changed: $isAirplaneModeOn") Log.d(TAG, "Airplane mode changed: $isAirplaneModeOn")
if (VERSION.SDK_INT >= 29)
updateMobileStatus() updateMobileStatus()
} }
} }
@@ -334,6 +337,7 @@ class BaresipService: Service() {
override fun onReceive(ctx: Context, intent: Intent) { override fun onReceive(ctx: Context, intent: Intent) {
if (intent.action == "android.intent.action.SIM_STATE_CHANGED") { if (intent.action == "android.intent.action.SIM_STATE_CHANGED") {
Log.d(TAG, "SIM state changed") Log.d(TAG, "SIM state changed")
if (VERSION.SDK_INT >= 29)
updateMobileStatus() updateMobileStatus()
} }
} }
@@ -836,9 +840,27 @@ class BaresipService: Service() {
if (ev[0] == "create") { if (ev[0] == "create") {
val ua = UserAgent(uap) val ua = UserAgent(uap)
ua.status = if (ua.account.isMobile) if (VERSION.SDK_INT < 29 && ua.account.isMobile) {
R.drawable.circle_green Log.d(TAG, "Removing Mobile account on API < 29")
else if (ua.account.regint == 0) 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 R.drawable.circle_white
else else
circleYellow.getValue(colorblind) circleYellow.getValue(colorblind)
@@ -1755,6 +1777,7 @@ class BaresipService: Service() {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
addMobileUserAgent() addMobileUserAgent()
if (VERSION.SDK_INT >= 29)
updateMobileStatus() updateMobileStatus()
val mobileUa = uas.value.find { it.account.isMobile } 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) if (VERSION.SDK_INT >= 31 && !telephonyCallbackRegistered)
try { try {
telephonyManager.registerTelephonyCallback(mainExecutor, telephonyCallback) telephonyManager.registerTelephonyCallback(mainExecutor, telephonyCallback)
@@ -1791,14 +1826,6 @@ class BaresipService: Service() {
telephonyCallbackRegistered = true telephonyCallbackRegistered = true
Log.d(TAG, "Registered PhoneStateListener") 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") @Suppress("unused")
@@ -2257,11 +2284,14 @@ class BaresipService: Service() {
} }
fun addMobileUserAgent() { fun addMobileUserAgent() {
if (VERSION.SDK_INT < 29) return if (VERSION.SDK_INT >= 29) {
val mobileAccountHandle = Utils.pstnAccountHandle(this) val mobileAccountHandle = Utils.pstnAccountHandle(this)
val existingMobileUa = uas.value.find { it.account.isMobile } val existingMobileUa = uas.value.find { it.account.isMobile }
if (existingMobileUa == null && mobileAccountHandle == null)
return
val mobileAor = "sip:mobile@pstn" val mobileAor = "sip:mobile@pstn"
val isAirplaneModeOn = Utils.isAirplaneModeOn(this) val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
@@ -2300,11 +2330,13 @@ class BaresipService: Service() {
uas.value = updatedUas.toList() uas.value = updatedUas.toList()
uasStatus.value = UserAgent.statusMap() uasStatus.value = UserAgent.statusMap()
registerTelephony()
Account.saveAccounts() Account.saveAccounts()
CallHistoryNew.save() CallHistoryNew.save()
Message.save() Message.save()
updateStatusNotification() updateStatusNotification()
} }
}
private fun toast(message: String, length: Int = Toast.LENGTH_SHORT) { private fun toast(message: String, length: Int = Toast.LENGTH_SHORT) {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
@@ -2312,11 +2344,13 @@ class BaresipService: Service() {
} }
} }
@RequiresApi(Build.VERSION_CODES.Q)
private fun updateMobileStatus(newStatus: Int? = null) { private fun updateMobileStatus(newStatus: Int? = null) {
uas.value.find { it.account.isMobile }?.let { ua -> uas.value.find { it.account.isMobile }?.let { ua ->
val isAirplaneModeOn = Utils.isAirplaneModeOn(this) val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady() val isSimReady = isSimReady()
val status = newStatus ?: if (!isSimReady) val mobileAccountHandle = Utils.pstnAccountHandle(this)
val status = newStatus ?: if (mobileAccountHandle == null || !isSimReady)
R.drawable.circle_white R.drawable.circle_white
else if (ua.status == circleGreen.getValue(colorblind)) else if (ua.status == circleGreen.getValue(colorblind))
ua.status ua.status
@@ -2340,17 +2374,20 @@ class BaresipService: Service() {
private fun updateMobileStatusFromServiceState(state: Int) { private fun updateMobileStatusFromServiceState(state: Int) {
if (state == previousMobileServiceState) return if (state == previousMobileServiceState) return
previousMobileServiceState = state previousMobileServiceState = state
if (VERSION.SDK_INT >= 29) {
val isAirplaneModeOn = Utils.isAirplaneModeOn(this) val isAirplaneModeOn = Utils.isAirplaneModeOn(this)
val isSimReady = isSimReady() 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) circleGreen.getValue(colorblind)
else if (isAirplaneModeOn || !isSimReady) else if (mobileAccountHandle == null || isAirplaneModeOn || !isSimReady)
R.drawable.circle_white R.drawable.circle_white
else else
circleRed.getValue(colorblind) circleRed.getValue(colorblind)
Log.d(TAG, "Mobile service state changed: $state, updating status to $status (Airplane=$isAirplaneModeOn)") Log.d(TAG, "Mobile service state changed: $state, updating status to $status (Airplane=$isAirplaneModeOn)")
updateMobileStatus(status) updateMobileStatus(status)
} }
}
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
private fun sendUssd(ussdCode: String) { private fun sendUssd(ussdCode: String) {
@@ -977,11 +977,14 @@ private fun SettingsContent(
val roleManager = ctx.getSystemService(ROLE_SERVICE) as RoleManager val roleManager = ctx.getSystemService(ROLE_SERVICE) as RoleManager
val requestPermissionLauncher = rememberLauncherForActivityResult( val requestPermissionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission() ActivityResultContracts.RequestMultiplePermissions()
) { isGranted -> ) { results ->
if (isGranted) 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") Log.d(TAG, "READ_PHONE_NUMBERS permission granted")
BaresipService.instance?.addMobileUserAgent() BaresipService.instance?.addMobileUserAgent()
restart = true
} }
val dialerRoleRequest = rememberLauncherForActivityResult( val dialerRoleRequest = rememberLauncherForActivityResult(
@@ -990,13 +993,17 @@ private fun SettingsContent(
val isHeld = roleManager.isRoleHeld(RoleManager.ROLE_DIALER) val isHeld = roleManager.isRoleHeld(RoleManager.ROLE_DIALER)
viewModel.defaultDialer.value = isHeld viewModel.defaultDialer.value = isHeld
if (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() BaresipService.instance?.addMobileUserAgent()
restart = true
}
else else
requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_NUMBERS) requestPermissionLauncher.launch(permissions)
} }
else else
BaresipService.instance?.addMobileUserAgent() BaresipService.instance?.addMobileUserAgent()
restart = true
} }
Switch( Switch(
checked = defaultDialer, checked = defaultDialer,
@@ -8,7 +8,7 @@ import com.tutpro.baresip.BaresipService.Companion.uasStatus
class UserAgent(val uap: Long, virtualAccount: Account? = null) { class UserAgent(val uap: Long, virtualAccount: Account? = null) {
val account = virtualAccount ?: Account(Api.ua_account(uap)) 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 { fun callAlloc(xCall: Long, videoMode: Int): Long {
if (uap == 0L) return 0L if (uap == 0L) return 0L