Respect default SMS SIM setting

This commit is contained in:
Naveen 2022-09-18 17:40:14 +05:30
parent 46a933d69b
commit 6842e1c5b6
2 changed files with 45 additions and 12 deletions

View File

@ -13,7 +13,9 @@ import android.os.Bundle
import android.provider.ContactsContract import android.provider.ContactsContract
import android.provider.MediaStore import android.provider.MediaStore
import android.provider.Telephony import android.provider.Telephony
import android.telephony.SmsManager
import android.telephony.SmsMessage import android.telephony.SmsMessage
import android.telephony.SubscriptionInfo
import android.telephony.SubscriptionManager import android.telephony.SubscriptionManager
import android.text.TextUtils import android.text.TextUtils
import android.util.TypedValue import android.util.TypedValue
@ -520,15 +522,15 @@ class ThreadActivity : SimpleActivity() {
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
private fun setupSIMSelector() { private fun setupSIMSelector() {
val availableSIMs = SubscriptionManager.from(this).activeSubscriptionInfoList ?: return val availableSIMs = subscriptionManagerCompat().activeSubscriptionInfoList ?: return
if (availableSIMs.size > 1) { if (availableSIMs.size > 1) {
availableSIMs.forEachIndexed { index, subscriptionInfo -> availableSIMs.forEachIndexed { index, subscriptionInfo ->
var label = subscriptionInfo.displayName?.toString() ?: "" var label = subscriptionInfo.displayName?.toString() ?: ""
if (subscriptionInfo.number?.isNotEmpty() == true) { if (subscriptionInfo.number?.isNotEmpty() == true) {
label += " (${subscriptionInfo.number})" label += " (${subscriptionInfo.number})"
} }
val SIMCard = SIMCard(index + 1, subscriptionInfo.subscriptionId, label) val simCard = SIMCard(index + 1, subscriptionInfo.subscriptionId, label)
availableSIMCards.add(SIMCard) availableSIMCards.add(simCard)
} }
val numbers = ArrayList<String>() val numbers = ArrayList<String>()
@ -542,8 +544,7 @@ class ThreadActivity : SimpleActivity() {
return return
} }
currentSIMCardIndex = availableSIMs.indexOfFirstOrNull { it.subscriptionId == config.getUseSIMIdAtNumber(numbers.first()) } ?: 0 currentSIMCardIndex = getProperSimIndex(availableSIMs, numbers)
thread_select_sim_icon.applyColorFilter(getProperTextColor()) thread_select_sim_icon.applyColorFilter(getProperTextColor())
thread_select_sim_icon.beVisible() thread_select_sim_icon.beVisible()
thread_select_sim_number.beVisible() thread_select_sim_number.beVisible()
@ -553,6 +554,10 @@ class ThreadActivity : SimpleActivity() {
currentSIMCardIndex = (currentSIMCardIndex + 1) % availableSIMCards.size currentSIMCardIndex = (currentSIMCardIndex + 1) % availableSIMCards.size
val currentSIMCard = availableSIMCards[currentSIMCardIndex] val currentSIMCard = availableSIMCards[currentSIMCardIndex]
thread_select_sim_number.text = currentSIMCard.id.toString() thread_select_sim_number.text = currentSIMCard.id.toString()
val currentSubscriptionId = currentSIMCard.subscriptionId
numbers.forEach {
config.saveUseSIMIdAtNumber(it, currentSubscriptionId)
}
toast(currentSIMCard.label) toast(currentSIMCard.label)
} }
} }
@ -562,6 +567,25 @@ class ThreadActivity : SimpleActivity() {
} }
} }
@SuppressLint("MissingPermission")
private fun getProperSimIndex(availableSIMs: MutableList<SubscriptionInfo>, numbers: List<String>): Int {
val userPreferredSimId = config.getUseSIMIdAtNumber(numbers.first())
val userPreferredSimIdx = availableSIMs.indexOfFirstOrNull { it.subscriptionId == userPreferredSimId }
val lastMessage = messages.lastOrNull()
val senderPreferredSimIdx = availableSIMs.indexOfFirstOrNull { it.subscriptionId == lastMessage?.subscriptionId }
val defaultSmsSubscriptionId = SmsManager.getDefaultSmsSubscriptionId()
val systemPreferredSimIdx = if (defaultSmsSubscriptionId >= 0) {
val defaultSmsSIM = subscriptionManagerCompat().getActiveSubscriptionInfo(defaultSmsSubscriptionId)
availableSIMs.indexOfFirstOrNull { it.subscriptionId == defaultSmsSIM.subscriptionId }
} else {
null
}
return userPreferredSimIdx ?: senderPreferredSimIdx ?: systemPreferredSimIdx ?: 0
}
private fun blockNumber() { private fun blockNumber() {
val numbers = ArrayList<String>() val numbers = ArrayList<String>()
participants.forEach { participants.forEach {
@ -908,12 +932,9 @@ class ThreadActivity : SimpleActivity() {
} }
val settings = getSendMessageSettings() val settings = getSendMessageSettings()
val SIMId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId val currentSubscriptionId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId
if (SIMId != null) { if (currentSubscriptionId != null) {
settings.subscriptionId = SIMId settings.subscriptionId = currentSubscriptionId
numbers.forEach {
config.saveUseSIMIdAtNumber(it, SIMId)
}
} }
val transaction = Transaction(this, settings) val transaction = Transaction(this, settings)
@ -1106,7 +1127,7 @@ class ThreadActivity : SimpleActivity() {
messages.filter { !it.isReceivedMessage() && it.id > lastMaxId }.forEach { latestMessage -> messages.filter { !it.isReceivedMessage() && it.id > lastMaxId }.forEach { latestMessage ->
// subscriptionIds seem to be not filled out at sending with multiple SIM cards, so fill it manually // subscriptionIds seem to be not filled out at sending with multiple SIM cards, so fill it manually
if ((SubscriptionManager.from(this).activeSubscriptionInfoList?.size ?: 0) > 1) { if ((subscriptionManagerCompat().activeSubscriptionInfoList?.size ?: 0) > 1) {
val SIMId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId val SIMId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId
if (SIMId != null) { if (SIMId != null) {
updateMessageSubscriptionId(latestMessage.id, SIMId) updateMessageSubscriptionId(latestMessage.id, SIMId)
@ -1118,6 +1139,7 @@ class ThreadActivity : SimpleActivity() {
} }
setupAdapter() setupAdapter()
setupSIMSelector()
} }
private fun updateMessageType() { private fun updateMessageType() {

View File

@ -16,12 +16,14 @@ import android.media.AudioAttributes
import android.media.AudioManager import android.media.AudioManager
import android.media.RingtoneManager import android.media.RingtoneManager
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.provider.ContactsContract.PhoneLookup import android.provider.ContactsContract.PhoneLookup
import android.provider.OpenableColumns import android.provider.OpenableColumns
import android.provider.Telephony.* import android.provider.Telephony.*
import android.telephony.SubscriptionManager
import android.text.TextUtils import android.text.TextUtils
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.RemoteInput import androidx.core.app.RemoteInput
@ -993,3 +995,12 @@ fun Context.clearAllMessagesIfNeeded() {
config.wasDbCleared = true config.wasDbCleared = true
} }
} }
fun Context.subscriptionManagerCompat(): SubscriptionManager {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getSystemService(SubscriptionManager::class.java)
} else {
@Suppress("DEPRECATION")
SubscriptionManager.from(this)
}
}