Merge pull request #431 from Naveen3Singh/sim_selection

Respect default SMS SIM setting
This commit is contained in:
Tibor Kaputa 2022-09-20 09:57:55 +02:00 committed by GitHub
commit b5c6ec5952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 16 deletions

View File

@ -13,8 +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.SubscriptionManager import android.telephony.SubscriptionInfo
import android.text.TextUtils import android.text.TextUtils
import android.util.TypedValue import android.util.TypedValue
import android.view.Gravity import android.view.Gravity
@ -520,15 +521,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 +543,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 +553,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 +566,29 @@ 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 = if (lastMessage?.isReceivedMessage() == true) {
availableSIMs.indexOfFirstOrNull { it.subscriptionId == lastMessage.subscriptionId }
} else {
null
}
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 {
@ -681,7 +708,7 @@ class ThreadActivity : SimpleActivity() {
val subscriptionIdToSimId = HashMap<Int, String>() val subscriptionIdToSimId = HashMap<Int, String>()
subscriptionIdToSimId[-1] = "?" subscriptionIdToSimId[-1] = "?"
SubscriptionManager.from(this).activeSubscriptionInfoList?.forEachIndexed { index, subscriptionInfo -> subscriptionManagerCompat().activeSubscriptionInfoList?.forEachIndexed { index, subscriptionInfo ->
subscriptionIdToSimId[subscriptionInfo.subscriptionId] = "${index + 1}" subscriptionIdToSimId[subscriptionInfo.subscriptionId] = "${index + 1}"
} }
@ -908,12 +935,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 +1130,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 +1142,7 @@ class ThreadActivity : SimpleActivity() {
} }
setupAdapter() setupAdapter()
setupSIMSelector()
} }
private fun updateMessageType() { private fun updateMessageType() {

View File

@ -53,7 +53,7 @@ class ThreadAdapter(
private var fontSize = activity.getTextSize() private var fontSize = activity.getTextSize()
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
private val hasMultipleSIMCards = (SubscriptionManager.from(activity).activeSubscriptionInfoList?.size ?: 0) > 1 private val hasMultipleSIMCards = (activity.subscriptionManagerCompat().activeSubscriptionInfoList?.size ?: 0) > 1
init { init {
setupDragListener(true) setupDragListener(true)

View File

@ -22,6 +22,7 @@ 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 +994,12 @@ fun Context.clearAllMessagesIfNeeded() {
config.wasDbCleared = true config.wasDbCleared = true
} }
} }
fun Context.subscriptionManagerCompat(): SubscriptionManager {
return if (isMarshmallowPlus()) {
getSystemService(SubscriptionManager::class.java)
} else {
@Suppress("DEPRECATION")
SubscriptionManager.from(this)
}
}

View File

@ -26,7 +26,7 @@ class DirectReplyReceiver : BroadcastReceiver() {
val settings = context.getSendMessageSettings() val settings = context.getSendMessageSettings()
if (address != null) { if (address != null) {
val availableSIMs = SubscriptionManager.from(context).activeSubscriptionInfoList val availableSIMs = context.subscriptionManagerCompat().activeSubscriptionInfoList
if ((availableSIMs?.size ?: 0) > 1) { if ((availableSIMs?.size ?: 0) > 1) {
val currentSIMCardIndex = context.config.getUseSIMIdAtNumber(address) val currentSIMCardIndex = context.config.getUseSIMIdAtNumber(address)
val wantedId = availableSIMs.getOrNull(currentSIMCardIndex) val wantedId = availableSIMs.getOrNull(currentSIMCardIndex)