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.MediaStore
import android.provider.Telephony
import android.telephony.SmsManager
import android.telephony.SmsMessage
import android.telephony.SubscriptionManager
import android.telephony.SubscriptionInfo
import android.text.TextUtils
import android.util.TypedValue
import android.view.Gravity
@ -520,15 +521,15 @@ class ThreadActivity : SimpleActivity() {
@SuppressLint("MissingPermission")
private fun setupSIMSelector() {
val availableSIMs = SubscriptionManager.from(this).activeSubscriptionInfoList ?: return
val availableSIMs = subscriptionManagerCompat().activeSubscriptionInfoList ?: return
if (availableSIMs.size > 1) {
availableSIMs.forEachIndexed { index, subscriptionInfo ->
var label = subscriptionInfo.displayName?.toString() ?: ""
if (subscriptionInfo.number?.isNotEmpty() == true) {
label += " (${subscriptionInfo.number})"
}
val SIMCard = SIMCard(index + 1, subscriptionInfo.subscriptionId, label)
availableSIMCards.add(SIMCard)
val simCard = SIMCard(index + 1, subscriptionInfo.subscriptionId, label)
availableSIMCards.add(simCard)
}
val numbers = ArrayList<String>()
@ -542,8 +543,7 @@ class ThreadActivity : SimpleActivity() {
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.beVisible()
thread_select_sim_number.beVisible()
@ -553,6 +553,10 @@ class ThreadActivity : SimpleActivity() {
currentSIMCardIndex = (currentSIMCardIndex + 1) % availableSIMCards.size
val currentSIMCard = availableSIMCards[currentSIMCardIndex]
thread_select_sim_number.text = currentSIMCard.id.toString()
val currentSubscriptionId = currentSIMCard.subscriptionId
numbers.forEach {
config.saveUseSIMIdAtNumber(it, currentSubscriptionId)
}
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() {
val numbers = ArrayList<String>()
participants.forEach {
@ -681,7 +708,7 @@ class ThreadActivity : SimpleActivity() {
val subscriptionIdToSimId = HashMap<Int, String>()
subscriptionIdToSimId[-1] = "?"
SubscriptionManager.from(this).activeSubscriptionInfoList?.forEachIndexed { index, subscriptionInfo ->
subscriptionManagerCompat().activeSubscriptionInfoList?.forEachIndexed { index, subscriptionInfo ->
subscriptionIdToSimId[subscriptionInfo.subscriptionId] = "${index + 1}"
}
@ -908,12 +935,9 @@ class ThreadActivity : SimpleActivity() {
}
val settings = getSendMessageSettings()
val SIMId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId
if (SIMId != null) {
settings.subscriptionId = SIMId
numbers.forEach {
config.saveUseSIMIdAtNumber(it, SIMId)
}
val currentSubscriptionId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId
if (currentSubscriptionId != null) {
settings.subscriptionId = currentSubscriptionId
}
val transaction = Transaction(this, settings)
@ -1106,7 +1130,7 @@ class ThreadActivity : SimpleActivity() {
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
if ((SubscriptionManager.from(this).activeSubscriptionInfoList?.size ?: 0) > 1) {
if ((subscriptionManagerCompat().activeSubscriptionInfoList?.size ?: 0) > 1) {
val SIMId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId
if (SIMId != null) {
updateMessageSubscriptionId(latestMessage.id, SIMId)
@ -1118,6 +1142,7 @@ class ThreadActivity : SimpleActivity() {
}
setupAdapter()
setupSIMSelector()
}
private fun updateMessageType() {

View File

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

View File

@ -22,6 +22,7 @@ import android.os.Looper
import android.provider.ContactsContract.PhoneLookup
import android.provider.OpenableColumns
import android.provider.Telephony.*
import android.telephony.SubscriptionManager
import android.text.TextUtils
import androidx.core.app.NotificationCompat
import androidx.core.app.RemoteInput
@ -993,3 +994,12 @@ fun Context.clearAllMessagesIfNeeded() {
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()
if (address != null) {
val availableSIMs = SubscriptionManager.from(context).activeSubscriptionInfoList
val availableSIMs = context.subscriptionManagerCompat().activeSubscriptionInfoList
if ((availableSIMs?.size ?: 0) > 1) {
val currentSIMCardIndex = context.config.getUseSIMIdAtNumber(address)
val wantedId = availableSIMs.getOrNull(currentSIMCardIndex)