diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index d8c03fcb..59c66ebd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -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() @@ -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, numbers: List): 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() participants.forEach { @@ -681,7 +708,7 @@ class ThreadActivity : SimpleActivity() { val subscriptionIdToSimId = HashMap() 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() { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt index 935336eb..75eb264d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt @@ -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) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 218b8b3b..ddde568f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -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) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/DirectReplyReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/DirectReplyReceiver.kt index 3814d0a3..caac5115 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/DirectReplyReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/DirectReplyReceiver.kt @@ -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)