Take a Mutex before modifying the EuiccChannel list

This commit is contained in:
Peter Cai 2022-05-11 20:36:09 -04:00
parent cc7f9a2957
commit 146b7fdc8d

View file

@ -9,6 +9,8 @@ import android.util.Log
import im.angry.openeuicc.OpenEuiccApplication import im.angry.openeuicc.OpenEuiccApplication
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
@ -23,6 +25,8 @@ class EuiccChannelManager(private val context: Context) {
private var seService: SEService? = null private var seService: SEService? = null
private val lock = Mutex()
private val tm by lazy { private val tm by lazy {
(context.applicationContext as OpenEuiccApplication).telephonyManager (context.applicationContext as OpenEuiccApplication).telephonyManager
} }
@ -43,38 +47,43 @@ class EuiccChannelManager(private val context: Context) {
} }
private suspend fun tryOpenEuiccChannel(uiccInfo: UiccCardInfo): EuiccChannel? { private suspend fun tryOpenEuiccChannel(uiccInfo: UiccCardInfo): EuiccChannel? {
ensureSEService() lock.withLock {
val existing = channels.find { it.slotId == uiccInfo.slotIndex } ensureSEService()
if (existing != null) { val existing = channels.find { it.slotId == uiccInfo.slotIndex }
if (existing.valid) { if (existing != null) {
return existing if (existing.valid) {
} else { return existing
existing.close() } else {
channels.remove(existing) existing.close()
channels.remove(existing)
}
} }
val channelInfo = EuiccChannelInfo(
uiccInfo.slotIndex,
uiccInfo.cardId,
"SIM ${uiccInfo.slotIndex}",
uiccInfo.isRemovable
)
var euiccChannel: EuiccChannel? = null
if (uiccInfo.isEuicc && !uiccInfo.isRemovable) {
Log.d(TAG, "Using TelephonyManager for slot ${uiccInfo.slotIndex}")
// TODO: On Tiramisu, we should also connect all available "ports" for MEP support
euiccChannel = TelephonyManagerChannel.tryConnect(tm, channelInfo)
}
if (euiccChannel == null) {
euiccChannel = OmapiChannel.tryConnect(seService!!, channelInfo)
}
if (euiccChannel != null) {
channels.add(euiccChannel)
}
return euiccChannel
} }
val channelInfo = EuiccChannelInfo(
uiccInfo.slotIndex, uiccInfo.cardId, "SIM ${uiccInfo.slotIndex}", uiccInfo.isRemovable
)
var euiccChannel: EuiccChannel? = null
if (uiccInfo.isEuicc && !uiccInfo.isRemovable) {
Log.d(TAG, "Using TelephonyManager for slot ${uiccInfo.slotIndex}")
// TODO: On Tiramisu, we should also connect all available "ports" for MEP support
euiccChannel = TelephonyManagerChannel.tryConnect(tm, channelInfo)
}
if (euiccChannel == null) {
euiccChannel = OmapiChannel.tryConnect(seService!!, channelInfo)
}
if (euiccChannel != null) {
channels.add(euiccChannel)
}
return euiccChannel
} }
private suspend fun findEuiccChannelBySlot(slotId: Int): EuiccChannel? { private suspend fun findEuiccChannelBySlot(slotId: Int): EuiccChannel? {