浏览代码

PrivilegedTelephonyUtils: Nuke direct EuiccChannel usage

Peter Cai 1 年之前
父节点
当前提交
32f5e3f71a

+ 12 - 7
app-common/src/main/java/im/angry/openeuicc/util/LPAUtils.kt

@@ -48,16 +48,21 @@ fun LocalProfileAssistant.disableActiveProfile(refresh: Boolean): Boolean =
     } ?: true
     } ?: 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
- * will disconnect the eUICC and might need some time before being operational again.
+ * Disable the current active profile if any. If refresh is true, also cause a refresh command.
  * See EuiccManager.waitForReconnect()
  * See EuiccManager.waitForReconnect()
+ *
+ * Return the iccid of the profile being disabled, or null if no active profile found or failed to
+ * disable.
  */
  */
-fun LocalProfileAssistant.disableActiveProfileWithUndo(refreshOnDisable: Boolean): () -> Unit =
+fun LocalProfileAssistant.disableActiveProfileKeepIccId(refresh: Boolean): String? =
     profiles.find { it.isEnabled }?.let {
     profiles.find { it.isEnabled }?.let {
-        disableProfile(it.iccid, refreshOnDisable)
-        return { enableProfile(it.iccid) }
-    } ?: { }
+        Log.i(TAG, "Disabling active profile ${it.iccid}")
+        if (disableProfile(it.iccid, refresh)) {
+            it.iccid
+        } else {
+            null
+        }
+    }
 
 
 /**
 /**
  * Begin a "tracked" operation where notifications may be generated by the eSIM
  * Begin a "tracked" operation where notifications may be generated by the eSIM

+ 16 - 6
app/src/main/java/im/angry/openeuicc/util/PrivilegedTelephonyUtils.kt

@@ -21,7 +21,7 @@ fun TelephonyManager.setDsdsEnabled(euiccManager: EuiccChannelManager, enabled:
         euiccManager.flowEuiccPorts().onEach { (slotId, portId) ->
         euiccManager.flowEuiccPorts().onEach { (slotId, portId) ->
             euiccManager.withEuiccChannel(slotId, portId) {
             euiccManager.withEuiccChannel(slotId, portId) {
                 if (!it.port.card.isRemovable) {
                 if (!it.port.card.isRemovable) {
-                    it.lpa.disableActiveProfileWithUndo(false)
+                    it.lpa.disableActiveProfile(false)
                 }
                 }
             }
             }
         }
         }
@@ -32,7 +32,7 @@ fun TelephonyManager.setDsdsEnabled(euiccManager: EuiccChannelManager, enabled:
 
 
 // Disable eSIM profiles before switching the slot mapping
 // Disable eSIM profiles before switching the slot mapping
 // This ensures that unmapped eSIM ports never have "ghost" profiles enabled
 // This ensures that unmapped eSIM ports never have "ghost" profiles enabled
-fun TelephonyManager.updateSimSlotMapping(
+suspend fun TelephonyManager.updateSimSlotMapping(
     euiccManager: EuiccChannelManager, newMapping: Collection<UiccSlotMapping>,
     euiccManager: EuiccChannelManager, newMapping: Collection<UiccSlotMapping>,
     currentMapping: Collection<UiccSlotMapping> = simSlotMapping
     currentMapping: Collection<UiccSlotMapping> = simSlotMapping
 ) {
 ) {
@@ -43,14 +43,24 @@ fun TelephonyManager.updateSimSlotMapping(
         }
         }
     }
     }
 
 
-    val undo = unmapped.mapNotNull { mapping ->
-        euiccManager.findEuiccChannelByPortBlocking(mapping.physicalSlotIndex, mapping.portIndex)?.let { channel ->
+    val undo: List<suspend () -> Unit> = unmapped.mapNotNull { mapping ->
+        euiccManager.withEuiccChannel(mapping.physicalSlotIndex, mapping.portIndex) { channel ->
             if (!channel.port.card.isRemovable) {
             if (!channel.port.card.isRemovable) {
-                return@mapNotNull channel.lpa.disableActiveProfileWithUndo(false)
+                channel.lpa.disableActiveProfileKeepIccId(false)
             } else {
             } else {
                 // Do not do anything for external eUICCs -- we can't really trust them to work properly
                 // Do not do anything for external eUICCs -- we can't really trust them to work properly
                 // with no profile enabled.
                 // with no profile enabled.
-                return@mapNotNull null
+                null
+            }
+        }?.let { iccid ->
+            // Generate undo closure because we can't keep reference to `channel` in the closure above
+            {
+                euiccManager.withEuiccChannel(
+                    mapping.physicalSlotIndex,
+                    mapping.portIndex
+                ) { channel ->
+                    channel.lpa.enableProfile(iccid)
+                }
             }
             }
         }
         }
     }
     }