Skip to content

Commit cc2a4e5

Browse files
committed
remember the last used SIM card on a per-number basis
1 parent f5d3c2a commit cc2a4e5

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.simplemobiletools.smsmessenger.activities
22

3+
import android.annotation.SuppressLint
34
import android.app.Activity
45
import android.content.Intent
56
import android.graphics.BitmapFactory
@@ -146,6 +147,8 @@ class ThreadActivity : SimpleActivity() {
146147
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
147148
thread_type_message.requestFocus()
148149
}
150+
151+
setupSIMSelector()
149152
}
150153
}
151154
setupButtons()
@@ -265,7 +268,10 @@ class ThreadActivity : SimpleActivity() {
265268
addAttachment(it)
266269
}
267270
}
271+
}
268272

273+
@SuppressLint("MissingPermission")
274+
private fun setupSIMSelector() {
269275
val availableSIMs = SubscriptionManager.from(this).activeSubscriptionInfoList
270276
if (availableSIMs.size > 1) {
271277
availableSIMs.forEachIndexed { index, subscriptionInfo ->
@@ -277,8 +283,10 @@ class ThreadActivity : SimpleActivity() {
277283
availableSIMCards.add(SIMCard)
278284
}
279285

280-
currentSIMCardIndex = 0
281-
thread_select_sim_icon.applyColorFilter(textColor)
286+
val numbers = participants.map { it.phoneNumber }.toTypedArray()
287+
currentSIMCardIndex = availableSIMs.indexOfFirstOrNull { it.subscriptionId == config.getUseSIMIdAtNumber(numbers.first()) } ?: 0
288+
289+
thread_select_sim_icon.applyColorFilter(config.textColor)
282290
thread_select_sim_icon.beVisible()
283291
thread_select_sim_number.beVisible()
284292

@@ -291,8 +299,8 @@ class ThreadActivity : SimpleActivity() {
291299
}
292300
}
293301

294-
thread_select_sim_number.setTextColor(textColor.getContrastColor())
295-
thread_select_sim_number.text = "1"
302+
thread_select_sim_number.setTextColor(config.textColor.getContrastColor())
303+
thread_select_sim_number.text = (availableSIMCards[currentSIMCardIndex].id).toString()
296304
}
297305
}
298306

@@ -469,6 +477,9 @@ class ThreadActivity : SimpleActivity() {
469477
val SIMId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId
470478
if (SIMId != null) {
471479
settings.subscriptionId = SIMId
480+
numbers.forEach {
481+
config.saveUseSIMIdAtNumber(it, SIMId)
482+
}
472483
}
473484

474485
val transaction = Transaction(this, settings)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.simplemobiletools.smsmessenger.extensions
2+
3+
inline fun <T> List<T>.indexOfFirstOrNull(predicate: (T) -> Boolean): Int? {
4+
var index = 0
5+
for (item in this) {
6+
if (predicate(item))
7+
return index
8+
index++
9+
}
10+
return null
11+
}

app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Config.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ class Config(context: Context) : BaseConfig(context) {
77
companion object {
88
fun newInstance(context: Context) = Config(context)
99
}
10+
11+
fun saveUseSIMIdAtNumber(number: String, SIMId: Int) {
12+
prefs.edit().putInt(USE_SIM_ID_PREFIX + number, SIMId).apply()
13+
}
14+
15+
fun getUseSIMIdAtNumber(number: String) = prefs.getInt(USE_SIM_ID_PREFIX + number, 0)
1016
}

app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Constants.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ const val THREAD_TEXT = "thread_text"
99
const val THREAD_NUMBER = "thread_number"
1010
const val THREAD_ATTACHMENT_URI = "thread_attachment_uri"
1111
const val THREAD_ATTACHMENT_URIS = "thread_attachment_uris"
12+
const val USE_SIM_ID_PREFIX = "use_sim_id_"
1213

1314
// view types for the thread list view
1415
const val THREAD_DATE_TIME = 1
1516
const val THREAD_RECEIVED_MESSAGE = 2
1617
const val THREAD_SENT_MESSAGE = 3
1718
const val THREAD_SENT_MESSAGE_ERROR = 4
1819

19-
// constants used at passing info to SmsSentReceiver
20-
const val MESSAGE_BODY = "message_body"
21-
const val MESSAGE_ADDRESS = "message_address"
22-
2320
fun refreshMessages() {
2421
EventBus.getDefault().post(Events.RefreshMessages())
2522
}

0 commit comments

Comments
 (0)