refactor: simplify profile operation #129

Merged
PeterCxy merged 4 commits from septs/OpenEUICC:enabled-profile into master 2024-12-19 00:19:01 +01:00
3 changed files with 14 additions and 20 deletions

View file

@ -111,7 +111,7 @@ class DownloadWizardSlotSelectFragment : DownloadWizardActivity.DownloadWizardSt
} catch (e: Exception) { } catch (e: Exception) {
"" ""
}, },
channel.lpa.profiles.find { it.state == LocalProfileInfo.State.Enabled }?.displayName, channel.lpa.profiles.enabled?.displayName,
channel.intrinsicChannelName, channel.intrinsicChannelName,
) )
} }

View file

@ -16,9 +16,10 @@ val LocalProfileInfo.isEnabled: Boolean
get() = state == LocalProfileInfo.State.Enabled get() = state == LocalProfileInfo.State.Enabled
val List<LocalProfileInfo>.operational: List<LocalProfileInfo> val List<LocalProfileInfo>.operational: List<LocalProfileInfo>
get() = filter { get() = filter { it.profileClass == LocalProfileInfo.Clazz.Operational }
it.profileClass == LocalProfileInfo.Clazz.Operational
} val List<LocalProfileInfo>.enabled: LocalProfileInfo?
get() = find { it.isEnabled }
val List<EuiccChannel>.hasMultipleChips: Boolean val List<EuiccChannel>.hasMultipleChips: Boolean
get() = distinctBy { it.slotId }.size > 1 get() = distinctBy { it.slotId }.size > 1
@ -39,7 +40,7 @@ fun LocalProfileAssistant.switchProfile(
* See EuiccManager.waitForReconnect() * See EuiccManager.waitForReconnect()
*/ */
fun LocalProfileAssistant.disableActiveProfile(refresh: Boolean): Boolean = fun LocalProfileAssistant.disableActiveProfile(refresh: Boolean): Boolean =
profiles.find { it.isEnabled }?.let { profiles.enabled?.let {
Log.i(TAG, "Disabling active profile ${it.iccid}") Log.i(TAG, "Disabling active profile ${it.iccid}")
disableProfile(it.iccid, refresh) disableProfile(it.iccid, refresh)
} ?: true } ?: true
@ -52,7 +53,7 @@ fun LocalProfileAssistant.disableActiveProfile(refresh: Boolean): Boolean =
* disable. * disable.
*/ */
fun LocalProfileAssistant.disableActiveProfileKeepIccId(refresh: Boolean): String? = fun LocalProfileAssistant.disableActiveProfileKeepIccId(refresh: Boolean): String? =
profiles.find { it.isEnabled }?.let { profiles.enabled?.let {
Log.i(TAG, "Disabling active profile ${it.iccid}") Log.i(TAG, "Disabling active profile ${it.iccid}")
if (disableProfile(it.iccid, refresh)) { if (disableProfile(it.iccid, refresh)) {
it.iccid it.iccid

View file

@ -186,13 +186,10 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
) )
} }
try { return@withEuiccChannelManager try {
return@withEuiccChannelManager euiccChannelManager.withEuiccChannel( euiccChannelManager.withEuiccChannel(slotId, port) { channel ->
slotId,
port
) { channel ->
val filteredProfiles = val filteredProfiles =
if (runBlocking { preferenceRepository.unfilteredProfileListFlow.first() }) if (preferenceRepository.unfilteredProfileListFlow.first())
channel.lpa.profiles channel.lpa.profiles
else else
channel.lpa.profiles.operational channel.lpa.profiles.operational
@ -224,7 +221,7 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
) )
} }
} catch (e: EuiccChannelManager.EuiccChannelNotFoundException) { } catch (e: EuiccChannelManager.EuiccChannelNotFoundException) {
return@withEuiccChannelManager GetEuiccProfileInfoListResult( GetEuiccProfileInfoListResult(
RESULT_FIRST_USER, RESULT_FIRST_USER,
arrayOf(), arrayOf(),
true true
@ -246,11 +243,7 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
// Check that the profile has been disabled on all slots // Check that the profile has been disabled on all slots
val enabledAnywhere = ports.any { port -> val enabledAnywhere = ports.any { port ->
euiccChannelManager.withEuiccChannel(slotId, port) { channel -> euiccChannelManager.withEuiccChannel(slotId, port) { channel ->
val profile = channel.lpa.profiles.find { channel.lpa.profiles.enabled?.iccid == iccid
it.iccid == iccid
} ?: return@withEuiccChannel false
profile.state == LocalProfileInfo.State.Enabled
} }
} }
@ -354,8 +347,8 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
// iccid == null means disabling // iccid == null means disabling
val foundIccid = val foundIccid =
euiccChannelManager.withEuiccChannel(foundSlotId, foundPortId) { channel -> euiccChannelManager.withEuiccChannel(foundSlotId, foundPortId) { channel ->
channel.lpa.profiles.find { it.state == LocalProfileInfo.State.Enabled } channel.lpa.profiles.enabled?.iccid
}?.iccid ?: return@withEuiccChannelManager RESULT_FIRST_USER } ?: return@withEuiccChannelManager RESULT_FIRST_USER
Pair(foundIccid, false) Pair(foundIccid, false)
} else { } else {
Pair(iccid, true) Pair(iccid, true)