Added capability to send SMS messages

This commit is contained in:
Juha Heinanen
2026-05-16 19:56:48 +03:00
parent ff75845dae
commit a6981d121b
4 changed files with 75 additions and 41 deletions
+2
View File
@@ -34,6 +34,8 @@
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" /> <uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SEND_SMS"
tools:ignore="SmsAndCallLogPolicy" />
<uses-feature <uses-feature
android:name="android.hardware.telephony" android:name="android.hardware.telephony"
@@ -552,25 +552,14 @@ private fun NewMessage(
msg.add() msg.add()
var msgUri = "" var msgUri = ""
addMessage(msg) addMessage(msg)
if (Utils.isTelUri(peerUri)) if (ua.account.isMobile) {
if (ua.account.telProvider == "") { val destination = Utils.uriUserPart(peerUri)
dialogMessage.value = String.format( if (Utils.sendSms(ctx, destination, msgText)) {
ctx.getString(R.string.no_telephony_provider), msg.direction = MESSAGE_UP
Utils.plainAor(aor) newMessage.value = TextFieldValue("")
) viewModel.updateAorPeerMessage(aor, peerUri, "")
showDialog.value = true keyboardController?.hide()
} } else {
else {
msgUri = Utils.telToSip(peerUri, ua.account)
}
else
msgUri = peerUri
if (msgUri != "")
if (Api.message_send(ua.uap,
msgUri,
msgText,
time.toString()
) != 0) {
Toast.makeText( Toast.makeText(
ctx, "${ctx.getString(R.string.message_failed)}!", ctx, "${ctx.getString(R.string.message_failed)}!",
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
@@ -578,11 +567,39 @@ private fun NewMessage(
msg.direction = MESSAGE_UP_FAIL msg.direction = MESSAGE_UP_FAIL
msg.responseReason = ctx.getString(R.string.message_failed) msg.responseReason = ctx.getString(R.string.message_failed)
} }
else { } else {
newMessage.value = TextFieldValue("") if (Utils.isTelUri(peerUri))
viewModel.updateAorPeerMessage(aor, peerUri, "") if (ua.account.telProvider == "") {
keyboardController?.hide() dialogMessage.value = String.format(
} ctx.getString(R.string.no_telephony_provider),
Utils.plainAor(aor)
)
showDialog.value = true
} else {
msgUri = Utils.telToSip(peerUri, ua.account)
}
else
msgUri = peerUri
if (msgUri != "")
if (Api.message_send(
ua.uap,
msgUri,
msgText,
time.toString()
) != 0
) {
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 {
newMessage.value = TextFieldValue("")
viewModel.updateAorPeerMessage(aor, peerUri, "")
keyboardController?.hide()
}
}
} }
}, },
containerColor = MaterialTheme.colorScheme.secondary, containerColor = MaterialTheme.colorScheme.secondary,
@@ -805,23 +805,22 @@ private fun BottomBar(ctx: Context, viewModel: ViewModel, navController: NavCont
) )
} }
if (!isMobile) IconButton(
IconButton( enabled = aor.isNotEmpty(),
enabled = aor.isNotEmpty(), onClick = {
onClick = { navController.navigate("chats/$aor")
navController.navigate("chats/$aor") },
}, modifier = Modifier
modifier = Modifier .weight(1f)
.weight(1f) .size(buttonSize)
.size(buttonSize) ) {
) { Icon(
Icon( imageVector = Icons.AutoMirrored.Filled.Chat,
imageVector = Icons.AutoMirrored.Filled.Chat, contentDescription = null,
contentDescription = null, Modifier.size(buttonSize),
Modifier.size(buttonSize), tint = if (hasUnreadMessages) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.secondary
tint = if (hasUnreadMessages) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.secondary )
) }
}
IconButton( IconButton(
enabled = aor.isNotEmpty(), enabled = aor.isNotEmpty(),
@@ -1440,4 +1440,20 @@ object Utils {
Log.e(TAG, "--------------------------------------") Log.e(TAG, "--------------------------------------")
} }
fun sendSms(ctx: Context, destination: String, message: String): Boolean {
return try {
val smsManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
ctx.getSystemService(android.telephony.SmsManager::class.java)
} else {
@Suppress("DEPRECATION")
android.telephony.SmsManager.getDefault()
}
smsManager.sendTextMessage(destination, null, message, null, null)
true
} catch (e: Exception) {
Log.e(TAG, "Failed to send SMS: ${e.message}")
false
}
}
} }