Move stale TM channel clean-up to EuiccChannelManager

This commit is contained in:
Peter Cai 2022-05-12 21:28:33 -04:00
parent 2be81f9285
commit 6b1056d907
2 changed files with 18 additions and 14 deletions

View File

@ -4,8 +4,6 @@ import android.app.Application
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import im.angry.openeuicc.core.EuiccChannelManager
import im.angry.openeuicc.util.*
import java.lang.Exception
class OpenEuiccApplication : Application() {
val telephonyManager by lazy {
@ -22,16 +20,6 @@ class OpenEuiccApplication : Application() {
override fun onCreate() {
super.onCreate()
// Clean up channels left open in TelephonyManager
// due to a (potentially) forced restart
for (slotId in 0 until EuiccChannelManager.MAX_SIMS) {
for (channel in 0 until 10) {
try {
telephonyManager.iccCloseLogicalChannelBySlot(slotId, channel)
} catch (_: Exception) {
// We do not care
}
}
}
euiccChannelManager.closeAllStaleChannels()
}
}

View File

@ -7,18 +7,19 @@ import android.se.omapi.SEService
import android.telephony.UiccCardInfo
import android.util.Log
import im.angry.openeuicc.OpenEuiccApplication
import im.angry.openeuicc.util.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.lang.Exception
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
class EuiccChannelManager(private val context: Context) {
companion object {
const val TAG = "EuiccChannelManager"
const val MAX_SIMS = 3
}
private val channels = mutableListOf<EuiccChannel>()
@ -124,4 +125,19 @@ class EuiccChannelManager(private val context: Context) {
seService?.shutdown()
seService = null
}
// Clean up channels left open in TelephonyManager
// due to a (potentially) forced restart
// This should be called every time the application is restarted
fun closeAllStaleChannels() {
for (card in tm.uiccCardsInfo) {
for (channel in 0 until 10) {
try {
tm.iccCloseLogicalChannelBySlot(card.slotIndex, channel)
} catch (_: Exception) {
// We do not care
}
}
}
}
}