From aec82325210cc83e9b280f582bfe226a883db847 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Mon, 25 Dec 2023 20:40:40 -0500 Subject: [PATCH] OpenEuiccService: check all ports before deleting a profile --- .../openeuicc/core/EuiccChannelManager.kt | 10 +++++++ .../openeuicc/service/OpenEuiccService.kt | 26 ++++++++++++------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt b/app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt index 0657c98..59c3aa9 100644 --- a/app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt +++ b/app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt @@ -135,6 +135,16 @@ open class EuiccChannelManager(protected val context: Context) { } } + fun findAllEuiccChannelsByPhysicalSlotBlocking(physicalSlotId: Int): List? = runBlocking { + if (!checkPrivileges()) return@runBlocking null + for (card in tm.uiccCardsInfoCompat) { + if (card.physicalSlotIndex != physicalSlotId) continue + return@runBlocking card.ports.mapNotNull { tryOpenEuiccChannel(it) } + .ifEmpty { null } + } + return@runBlocking null + } + fun findEuiccChannelByPortBlocking(physicalSlotId: Int, portId: Int): EuiccChannel? = runBlocking { if (!checkPrivileges()) return@runBlocking null withContext(Dispatchers.IO) { diff --git a/app/src/main/java/im/angry/openeuicc/service/OpenEuiccService.kt b/app/src/main/java/im/angry/openeuicc/service/OpenEuiccService.kt index d98fca8..cba4bc8 100644 --- a/app/src/main/java/im/angry/openeuicc/service/OpenEuiccService.kt +++ b/app/src/main/java/im/angry/openeuicc/service/OpenEuiccService.kt @@ -27,6 +27,10 @@ class OpenEuiccService : EuiccService() { openEuiccApplication.euiccChannelManager .findEuiccChannelByPortBlocking(slotId, portId) + private fun findAllChannels(physicalSlotId: Int): List? = + openEuiccApplication.euiccChannelManager + .findAllEuiccChannelsByPhysicalSlotBlocking(physicalSlotId) + override fun onGetEid(slotId: Int): String? = findChannel(slotId)?.lpa?.eID @@ -141,23 +145,25 @@ class OpenEuiccService : EuiccService() { override fun onDeleteSubscription(slotId: Int, iccid: String): Int { Log.i(TAG, "onDeleteSubscription slotId=$slotId iccid=$iccid") try { - val channel = findChannel(slotId) ?: return RESULT_FIRST_USER + val channels = findAllChannels(slotId) ?: return RESULT_FIRST_USER - if (!channel.profileExists(iccid)) { + if (!channels[0].profileExists(iccid)) { return RESULT_FIRST_USER } - val profile = channel.lpa.profiles.find { - it.iccid == iccid - } ?: return RESULT_FIRST_USER + // If the profile is enabled by ANY channel (port), we cannot delete it + channels.forEach { channel -> + val profile = channel.lpa.profiles.find { + it.iccid == iccid + } ?: return RESULT_FIRST_USER - if (profile.state == LocalProfileInfo.State.Enabled) { - // Must disable the profile first - // TODO: Need to check "other port" as well for MEP - return RESULT_FIRST_USER + if (profile.state == LocalProfileInfo.State.Enabled) { + // Must disable the profile first + return RESULT_FIRST_USER + } } - return if (channel.lpa.deleteProfile(iccid)) { + return if (channels[0].lpa.deleteProfile(iccid)) { RESULT_OK } else { RESULT_FIRST_USER