Enumerate via TelephonyManager's UICC card info list

This commit is contained in:
Peter Cai 2022-05-11 18:56:14 -04:00
parent 4840236e23
commit 14b1352b8a

View file

@ -4,6 +4,7 @@ import android.content.Context
import android.os.Handler import android.os.Handler
import android.os.HandlerThread import android.os.HandlerThread
import android.se.omapi.SEService import android.se.omapi.SEService
import android.telephony.UiccCardInfo
import android.util.Log import android.util.Log
import im.angry.openeuicc.OpenEuiccApplication import im.angry.openeuicc.OpenEuiccApplication
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -41,9 +42,9 @@ class EuiccChannelManager(private val context: Context) {
} }
} }
private suspend fun findEuiccChannelBySlot(slotId: Int): EuiccChannel? { private suspend fun tryOpenEuiccChannel(uiccInfo: UiccCardInfo): EuiccChannel? {
ensureSEService() ensureSEService()
val existing = channels.find { it.slotId == slotId } val existing = channels.find { it.slotId == uiccInfo.slotIndex }
if (existing != null) { if (existing != null) {
if (existing.valid) { if (existing.valid) {
return existing return existing
@ -53,21 +54,14 @@ class EuiccChannelManager(private val context: Context) {
} }
} }
val cardInfo = tm.uiccCardsInfo.find { it.slotIndex == slotId } ?: return null
val channelInfo = EuiccChannelInfo( val channelInfo = EuiccChannelInfo(
slotId, cardInfo.cardId, "SIM $slotId", cardInfo.isRemovable uiccInfo.slotIndex, uiccInfo.cardId, "SIM ${uiccInfo.slotIndex}", uiccInfo.isRemovable
) )
val (shouldTryTelephonyManager, cardId) =
cardInfo.let {
Pair(it.isEuicc && !it.isRemovable, it.cardId)
}
var euiccChannel: EuiccChannel? = null var euiccChannel: EuiccChannel? = null
if (shouldTryTelephonyManager) { if (uiccInfo.isEuicc && !uiccInfo.isRemovable) {
Log.d(TAG, "Using TelephonyManager for slot $slotId") Log.d(TAG, "Using TelephonyManager for slot ${uiccInfo.slotIndex}")
// TODO: On Tiramisu, we should also connect all available "ports" for MEP support // TODO: On Tiramisu, we should also connect all available "ports" for MEP support
euiccChannel = TelephonyManagerChannel.tryConnect(tm, channelInfo) euiccChannel = TelephonyManagerChannel.tryConnect(tm, channelInfo)
} }
@ -83,6 +77,12 @@ class EuiccChannelManager(private val context: Context) {
return euiccChannel return euiccChannel
} }
private suspend fun findEuiccChannelBySlot(slotId: Int): EuiccChannel? {
return tm.uiccCardsInfo.find { it.slotIndex == slotId }?.let {
tryOpenEuiccChannel(it)
}
}
fun findEuiccChannelBySlotBlocking(slotId: Int): EuiccChannel? = runBlocking { fun findEuiccChannelBySlotBlocking(slotId: Int): EuiccChannel? = runBlocking {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
findEuiccChannelBySlot(slotId) findEuiccChannelBySlot(slotId)
@ -93,9 +93,9 @@ class EuiccChannelManager(private val context: Context) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
ensureSEService() ensureSEService()
for (slotId in 0 until MAX_SIMS) { for (uiccInfo in tm.uiccCardsInfo) {
if (findEuiccChannelBySlot(slotId) != null) { if (tryOpenEuiccChannel(uiccInfo) != null) {
Log.d(TAG, "Found eUICC on slot $slotId") Log.d(TAG, "Found eUICC on slot ${uiccInfo.slotIndex}")
} }
} }
} }