Show alert if contact does not have suitable URI

Show alert if calling or messaging is tried on mobile account when device is in airplane mode
This commit is contained in:
Juha Heinanen
2026-05-19 09:16:56 +03:00
parent 5ebfccfe8c
commit caa1baf0c8
10 changed files with 103 additions and 40 deletions

View File

@ -618,7 +618,7 @@ private fun checkOnClick(
newUris.add(u) newUris.add(u)
} }
if (newUris.isEmpty()) { if (newUris.isEmpty() && currentState.email.trim().isEmpty()) {
alertTitle.value = ctx.getString(R.string.notice) alertTitle.value = ctx.getString(R.string.notice)
alertMessage.value = ctx.getString(R.string.sip_or_tel_uri) alertMessage.value = ctx.getString(R.string.sip_or_tel_uri)
showAlert.value = true showAlert.value = true

View File

@ -359,16 +359,26 @@ private fun Calls(
) )
secondButtonText.value = ctx.getString(R.string.call) secondButtonText.value = ctx.getString(R.string.call)
secondAction.value = { secondAction.value = {
handleIntent(ctx, viewModel, intent, "call") if (ua.account.isMobile && Utils.isAirplaneModeOn(ctx)) {
navController.navigate("main") { alertTitle.value = ctx.getString(R.string.notice)
popUpTo("main") alertMessage.value = ctx.getString(R.string.no_airplane_mode)
launchSingleTop = true showAlert.value = true
} else {
handleIntent(ctx, viewModel, intent, "call")
navController.navigate("main") {
popUpTo("main")
launchSingleTop = true
}
} }
} }
thirdButtonText.value = ctx.getString(R.string.send_message) thirdButtonText.value = ctx.getString(R.string.send_message)
thirdAction.value = { thirdAction.value = {
if (ua.account.isMobile) { if (ua.account.isMobile) {
if (!Utils.isDefaultSmsApp(ctx)) { if (Utils.isAirplaneModeOn(ctx)) {
alertTitle.value = ctx.getString(R.string.notice)
alertMessage.value = ctx.getString(R.string.no_airplane_mode)
showAlert.value = true
} else if (!Utils.isDefaultSmsApp(ctx)) {
alertTitle.value = ctx.getString(R.string.notice) alertTitle.value = ctx.getString(R.string.notice)
alertMessage.value = ctx.getString(R.string.enable_default_messaging) alertMessage.value = ctx.getString(R.string.enable_default_messaging)
showAlert.value = true showAlert.value = true

View File

@ -582,19 +582,24 @@ private fun NewMessage(
var msgUri = "" var msgUri = ""
addMessage(msg) addMessage(msg)
if (ua.account.isMobile) { if (ua.account.isMobile) {
val destination = Utils.uriUserPart(peerUri) if (Utils.isAirplaneModeOn(ctx)) {
if (Utils.sendSms(ctx, destination, msgText)) { dialogMessage.value = ctx.getString(R.string.no_airplane_mode)
msg.direction = MESSAGE_UP showDialog.value = true
newMessage.value = TextFieldValue("")
viewModel.updateAorPeerMessage(aor, peerUri, "")
keyboardController?.hide()
} else { } else {
Toast.makeText( val destination = Utils.uriUserPart(peerUri)
ctx, "${ctx.getString(R.string.message_failed)}!", if (Utils.sendSms(ctx, destination, msgText)) {
Toast.LENGTH_SHORT msg.direction = MESSAGE_UP
).show() newMessage.value = TextFieldValue("")
msg.direction = MESSAGE_UP_FAIL viewModel.updateAorPeerMessage(aor, peerUri, "")
msg.responseReason = ctx.getString(R.string.message_failed) keyboardController?.hide()
} else {
Toast.makeText(
ctx, "${ctx.getString(R.string.message_failed)}!",
Toast.LENGTH_SHORT
).show()
msg.direction = MESSAGE_UP_FAIL
msg.responseReason = ctx.getString(R.string.message_failed)
}
} }
} else { } else {
if (Utils.isTelUri(peerUri)) if (Utils.isTelUri(peerUri))

View File

@ -594,9 +594,18 @@ private fun NewChatPeer(ctx: Context, navController: NavController, account: Acc
val peerText = newPeer.trim() val peerText = newPeer.trim()
if (peerText.isNotEmpty()) { if (peerText.isNotEmpty()) {
val uris = Contact.contactUris(peerText, account.isMobile) val uris = Contact.contactUris(peerText, account.isMobile)
if (uris.isEmpty()) if (uris.isEmpty()) {
makeChat(ctx, navController, account, peerText) if (Contact.nameExists(peerText, BaresipService.contacts, true)) {
else if (uris.size == 1) alertTitle.value = ctx.getString(R.string.notice)
alertMessage.value = if (account.isMobile)
String.format(ctx.getString(R.string.contact_no_tel_uri), peerText)
else
String.format(ctx.getString(R.string.contact_no_sip_or_tel_uri), peerText)
showAlert.value = true
} else {
makeChat(ctx, navController, account, peerText)
}
} else if (uris.size == 1)
makeChat(ctx, navController, account, uris[0]) makeChat(ctx, navController, account, uris[0])
else { else {
items.value = uris items.value = uris

View File

@ -106,8 +106,11 @@ sealed class Contact {
} }
is AndroidContact -> { is AndroidContact -> {
if (c.name == name) { if (c.name == name) {
for (u in c.uris) for (u in c.uris) {
if (tel && !u.startsWith("tel:"))
continue
uris.add(u) uris.add(u)
}
return uris return uris
} }
} }
@ -207,7 +210,7 @@ sealed class Contact {
cur?.close() cur?.close()
val newList = mutableListOf<AndroidContact>() val newList = mutableListOf<AndroidContact>()
for ((_, value) in contacts) for ((_, value) in contacts)
if (value.name != "" && value.uris.isNotEmpty()) if (value.name != "" && (value.uris.isNotEmpty() || value.email != ""))
newList.add(value) newList.add(value)
BaresipService.androidContacts.value = newList.toList() BaresipService.androidContacts.value = newList.toList()
} }

View File

@ -290,7 +290,10 @@ private fun Uris(
if (ua == null) if (ua == null)
Log.w(TAG, "Message clickable did not find AoR $aor") Log.w(TAG, "Message clickable did not find AoR $aor")
else { else {
if (ua.account.isMobile && !Utils.isDefaultSmsApp(ctx)) { if (ua.account.isMobile && Utils.isAirplaneModeOn(ctx)) {
handleDialog(ctx, ctx.getString(R.string.notice),
ctx.getString(R.string.no_airplane_mode))
} else if (ua.account.isMobile && !Utils.isDefaultSmsApp(ctx)) {
handleDialog(ctx, ctx.getString(R.string.notice), handleDialog(ctx, ctx.getString(R.string.notice),
ctx.getString(R.string.enable_default_messaging)) ctx.getString(R.string.enable_default_messaging))
} else { } else {
@ -321,13 +324,18 @@ private fun Uris(
if (ua == null) if (ua == null)
Log.w(TAG, "Call clickable did not find AoR $aor") Log.w(TAG, "Call clickable did not find AoR $aor")
else { else {
val intent = Intent(ctx, MainActivity::class.java) if (ua.account.isMobile && Utils.isAirplaneModeOn(ctx)) {
intent.putExtra("uap", ua.uap) handleDialog(ctx, ctx.getString(R.string.notice),
intent.putExtra("peer", uri) ctx.getString(R.string.no_airplane_mode))
handleIntent(ctx, viewModel, intent, "call") } else {
navController.navigate("main") { val intent = Intent(ctx, MainActivity::class.java)
popUpTo("main") { inclusive = false } intent.putExtra("uap", ua.uap)
launchSingleTop = true intent.putExtra("peer", uri)
handleIntent(ctx, viewModel, intent, "call")
navController.navigate("main") {
popUpTo("main") { inclusive = false }
launchSingleTop = true
}
} }
} }
} }

View File

@ -156,7 +156,7 @@ private fun ContactsScreen(navController: NavController) {
} }
} }
line.startsWith("END:VCARD", ignoreCase = true) -> { line.startsWith("END:VCARD", ignoreCase = true) -> {
if (name.isNotEmpty() && uris.isNotEmpty()) { if (name.isNotEmpty() && (uris.isNotEmpty() || email.isNotEmpty())) {
val existingContact = Contact.baresipContact(name) val existingContact = Contact.baresipContact(name)
if (existingContact != null) { if (existingContact != null) {
var updated = false var updated = false

View File

@ -2046,14 +2046,26 @@ private fun callClick(ctx: Context, viewModel: ViewModel, dialerState: ViewModel
val uriText = dialerState.callUri.value.trim() val uriText = dialerState.callUri.value.trim()
if (uriText.isNotEmpty()) { if (uriText.isNotEmpty()) {
if (Utils.checkPermissions(ctx, arrayOf(RECORD_AUDIO))) { if (Utils.checkPermissions(ctx, arrayOf(RECORD_AUDIO))) {
val uris = Contact.contactUris(uriText) val aor = viewModel.selectedAor.value
if (uris.isEmpty()) val ua = UserAgent.ofAor(aor)
makeCall( val uris = Contact.contactUris(uriText, ua?.account?.isMobile ?: false)
ctx, if (uris.isEmpty()) {
viewModel, if (Contact.nameExists(uriText, BaresipService.contacts, true)) {
uriText, alertTitle.value = ctx.getString(R.string.notice)
dialerState alertMessage.value = if (ua?.account?.isMobile == true)
) String.format(ctx.getString(R.string.contact_no_tel_uri), uriText)
else
String.format(ctx.getString(R.string.contact_no_sip_or_tel_uri), uriText)
showAlert.value = true
} else {
makeCall(
ctx,
viewModel,
uriText,
dialerState
)
}
}
else if (uris.size == 1) else if (uris.size == 1)
makeCall( makeCall(
ctx, ctx,
@ -2120,6 +2132,12 @@ private fun makeCall(ctx: Context, viewModel: ViewModel, uriText: String,
showAlert.value = true showAlert.value = true
return return
} }
else if (ua.account.isMobile && Utils.isAirplaneModeOn(ctx)) {
alertTitle.value = ctx.getString(R.string.notice)
alertMessage.value = ctx.getString(R.string.no_airplane_mode)
showAlert.value = true
return
}
else if (Utils.isAudioMode(ctx,AudioManager.MODE_IN_CALL) && else if (Utils.isAudioMode(ctx,AudioManager.MODE_IN_CALL) &&
!Call.calls().any { it.ua.account.aor == ua.account.aor }) !Call.calls().any { it.ua.account.aor == ua.account.aor })
Toast.makeText(ctx, R.string.call_already_active, Toast.LENGTH_SHORT).show() Toast.makeText(ctx, R.string.call_already_active, Toast.LENGTH_SHORT).show()

View File

@ -873,6 +873,13 @@ object Utils {
Configuration.UI_MODE_NIGHT_YES Configuration.UI_MODE_NIGHT_YES
} }
fun isAirplaneModeOn(ctx: Context): Boolean {
return android.provider.Settings.Global.getInt(
ctx.contentResolver,
android.provider.Settings.Global.AIRPLANE_MODE_ON, 0
) != 0
}
@Suppress("unused") @Suppress("unused")
fun isPSTNCallActive(ctx: Context): Boolean { fun isPSTNCallActive(ctx: Context): Boolean {
val tm = ctx.getSystemService(Context.TELECOM_SERVICE) as? TelecomManager ?: return false val tm = ctx.getSystemService(Context.TELECOM_SERVICE) as? TelecomManager ?: return false

View File

@ -464,6 +464,8 @@
on top of contacts list.</string> on top of contacts list.</string>
<string name="invalid_contact">Invalid contact name \'%1$s\'</string> <string name="invalid_contact">Invalid contact name \'%1$s\'</string>
<string name="contact_already_exists">Contact \'%1$s\' already exists.</string> <string name="contact_already_exists">Contact \'%1$s\' already exists.</string>
<string name="contact_no_sip_or_tel_uri">Contact \'%1$s\' has no SIP or TEL URI</string>
<string name="contact_no_tel_uri">Contact \'%1$s\' has no TEL URI</string>
<string name="android_contact_help">If checked, this contact is added to Android contacts.</string> <string name="android_contact_help">If checked, this contact is added to Android contacts.</string>
<string name="avatar_image">Profile image</string> <string name="avatar_image">Profile image</string>
<!-- Contacts Activity --> <!-- Contacts Activity -->
@ -595,6 +597,7 @@
</string> </string>
<string name="no_notifications">You are not able to use this application without \"Notifications\" <string name="no_notifications">You are not able to use this application without \"Notifications\"
permission.</string> permission.</string>
<string name="no_airplane_mode">Airplane mode is ON. Turn it OFF to use mobile account.</string>
<string name="no_calls">baresip needs \"Microphone\" permission for voice calls.</string> <string name="no_calls">baresip needs \"Microphone\" permission for voice calls.</string>
<string name="no_video_calls">Grant \"Camera\" permission to make or answer video calls.</string> <string name="no_video_calls">Grant \"Camera\" permission to make or answer video calls.</string>
<string name="no_backup">You are not able create backup without \"Storage\" permission.</string> <string name="no_backup">You are not able create backup without \"Storage\" permission.</string>