remember the last used SIM card on a per-number basis

This commit is contained in:
tibbi 2020-05-03 23:12:00 +02:00
parent f5d3c2ab70
commit cc2a4e596f
4 changed files with 33 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.smsmessenger.activities
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
@ -146,6 +147,8 @@ class ThreadActivity : SimpleActivity() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
thread_type_message.requestFocus()
}
setupSIMSelector()
}
}
setupButtons()
@ -265,7 +268,10 @@ class ThreadActivity : SimpleActivity() {
addAttachment(it)
}
}
}
@SuppressLint("MissingPermission")
private fun setupSIMSelector() {
val availableSIMs = SubscriptionManager.from(this).activeSubscriptionInfoList
if (availableSIMs.size > 1) {
availableSIMs.forEachIndexed { index, subscriptionInfo ->
@ -277,8 +283,10 @@ class ThreadActivity : SimpleActivity() {
availableSIMCards.add(SIMCard)
}
currentSIMCardIndex = 0
thread_select_sim_icon.applyColorFilter(textColor)
val numbers = participants.map { it.phoneNumber }.toTypedArray()
currentSIMCardIndex = availableSIMs.indexOfFirstOrNull { it.subscriptionId == config.getUseSIMIdAtNumber(numbers.first()) } ?: 0
thread_select_sim_icon.applyColorFilter(config.textColor)
thread_select_sim_icon.beVisible()
thread_select_sim_number.beVisible()
@ -291,8 +299,8 @@ class ThreadActivity : SimpleActivity() {
}
}
thread_select_sim_number.setTextColor(textColor.getContrastColor())
thread_select_sim_number.text = "1"
thread_select_sim_number.setTextColor(config.textColor.getContrastColor())
thread_select_sim_number.text = (availableSIMCards[currentSIMCardIndex].id).toString()
}
}
@ -469,6 +477,9 @@ class ThreadActivity : SimpleActivity() {
val SIMId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId
if (SIMId != null) {
settings.subscriptionId = SIMId
numbers.forEach {
config.saveUseSIMIdAtNumber(it, SIMId)
}
}
val transaction = Transaction(this, settings)

View File

@ -0,0 +1,11 @@
package com.simplemobiletools.smsmessenger.extensions
inline fun <T> List<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? {
var index = 0
for (item in this) {
if (predicate(item))
return index
index++
}
return null
}

View File

@ -7,4 +7,10 @@ class Config(context: Context) : BaseConfig(context) {
companion object {
fun newInstance(context: Context) = Config(context)
}
fun saveUseSIMIdAtNumber(number: String, SIMId: Int) {
prefs.edit().putInt(USE_SIM_ID_PREFIX + number, SIMId).apply()
}
fun getUseSIMIdAtNumber(number: String) = prefs.getInt(USE_SIM_ID_PREFIX + number, 0)
}

View File

@ -9,6 +9,7 @@ const val THREAD_TEXT = "thread_text"
const val THREAD_NUMBER = "thread_number"
const val THREAD_ATTACHMENT_URI = "thread_attachment_uri"
const val THREAD_ATTACHMENT_URIS = "thread_attachment_uris"
const val USE_SIM_ID_PREFIX = "use_sim_id_"
// view types for the thread list view
const val THREAD_DATE_TIME = 1
@ -16,10 +17,6 @@ const val THREAD_RECEIVED_MESSAGE = 2
const val THREAD_SENT_MESSAGE = 3
const val THREAD_SENT_MESSAGE_ERROR = 4
// constants used at passing info to SmsSentReceiver
const val MESSAGE_BODY = "message_body"
const val MESSAGE_ADDRESS = "message_address"
fun refreshMessages() {
EventBus.getDefault().post(Events.RefreshMessages())
}