ソースを参照

refactor: Commonize logic for disabling the active profile

Peter Cai 1 年間 前
コミット
df9cece94b

+ 3 - 6
app-common/src/main/java/im/angry/openeuicc/ui/EuiccManagementFragment.kt

@@ -130,12 +130,9 @@ open class EuiccManagementFragment : Fragment(), EuiccProfilesChangedListener,
         lifecycleScope.launch {
             beginTrackedOperation {
                 val res = if (enable) {
-                    channel.lpa.profiles.find { it.isEnabled }?.let {
-                        Log.i(TAG, "Profile ${it.iccid} is enabled; disabling before switching to new profile")
-                        // Don't refresh -- we'll refresh later when enabling.
-                        // Also ignore errors -- this is only a hard error if the enabling step fails.
-                        channel.lpa.disableProfile(it.iccid, false)
-                    }
+                    // Disable any active profile first, but don't do a refresh and ignore
+                    // any errors -- we only error if the enabling action fails
+                    channel.lpa.disableActiveProfile(false)
                     channel.lpa.enableProfile(iccid)
                 } else {
                     channel.lpa.disableProfile(iccid)

+ 11 - 0
app-common/src/main/java/im/angry/openeuicc/util/LPAUtils.kt

@@ -1,5 +1,6 @@
 package im.angry.openeuicc.util
 
+import android.util.Log
 import im.angry.openeuicc.core.EuiccChannel
 import net.typeblog.lpac_jni.LocalProfileAssistant
 import net.typeblog.lpac_jni.LocalProfileInfo
@@ -19,6 +20,16 @@ val List<LocalProfileInfo>.operational: List<LocalProfileInfo>
 val List<EuiccChannel>.hasMultipleChips: Boolean
     get() = distinctBy { it.slotId }.size > 1
 
+/**
+ * Disable the current active profile if any. If refresh is true, also cause a refresh command.
+ * See EuiccManager.waitForReconnect()
+ */
+fun LocalProfileAssistant.disableActiveProfile(refresh: Boolean): Boolean =
+    profiles.find { it.isEnabled }?.let {
+        Log.i("LPAUtils", "Disabling active profile ${it.iccid}")
+        disableProfile(it.iccid, refresh)
+    } ?: true
+
 /**
  * Disable the active profile, return a lambda that reverts this action when called.
  * If refreshOnDisable is true, also cause a eUICC refresh command. Note that refreshing

+ 3 - 3
app/src/main/java/im/angry/openeuicc/service/OpenEuiccService.kt

@@ -284,9 +284,9 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
             }
 
             // Disable any active profile first if present
-            channel.lpa.profiles.find {
-                it.state == LocalProfileInfo.State.Enabled
-            }?.let { if (!channel.lpa.disableProfile(it.iccid)) return RESULT_FIRST_USER }
+            if (!channel.lpa.disableActiveProfile(false)) {
+                return RESULT_FIRST_USER
+            }
 
             if (iccid != null) {
                 if (!channel.lpa.enableProfile(iccid)) {