Clean up TelephonyManager channels in case of a forced restart

This commit is contained in:
Peter Cai 2022-05-03 16:34:15 -04:00
parent 103be89d17
commit 7140840129
2 changed files with 17 additions and 2 deletions

View File

@ -3,6 +3,8 @@ package im.angry.openeuicc
import android.app.Application
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 {
@ -12,4 +14,19 @@ class OpenEuiccApplication : Application() {
val euiccChannelManager by lazy {
EuiccChannelManager(this)
}
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
}
}
}
}
}

View File

@ -21,8 +21,6 @@ class TelephonyManagerApduChannel(
// TODO: On Tiramisu, we need to specify the portId also if we want MEP support
fun tryConnectUiccSlot(tm: TelephonyManager, slotId: Int): Pair<ApduChannel, EuiccChannelStateManager>? {
try {
// FIXME: Clean up previously opened channels across restarts
tm.iccCloseLogicalChannelBySlot(slotId, 1)
val channel = tm.iccOpenLogicalChannelBySlot(slotId, EUICC_APP_ID, 0)
if (channel.status != STATUS_NO_ERROR || channel.channel == INVALID_CHANNEL) {
Log.e(TAG, "Unable to open eUICC channel for slot ${slotId} via TelephonyManager: ${channel.status}")