瀏覽代碼

core: Reconnect to USB readers after switching profiles as well

Apparently some reader + card combination results in the need to
re-establish the ISD-R even though this is not a modem.

It doesn't hurt to run waitForReconnect() anyway :)
Peter Cai 1 年之前
父節點
當前提交
6e3176668a

+ 19 - 9
app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelManager.kt

@@ -184,20 +184,30 @@ open class DefaultEuiccChannelManager(
     }
 
     override suspend fun waitForReconnect(physicalSlotId: Int, portId: Int, timeoutMillis: Long) {
-        if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) return
-
-        // If there is already a valid channel, we close it proactively
-        // Sometimes the current channel can linger on for a bit even after it should have become invalid
-        channelCache.find { it.slotId == physicalSlotId && it.portId == portId }?.apply {
-            if (valid) close()
+        if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) {
+            usbChannel?.close()
+            usbChannel = null
+        } else {
+            // If there is already a valid channel, we close it proactively
+            // Sometimes the current channel can linger on for a bit even after it should have become invalid
+            channelCache.find { it.slotId == physicalSlotId && it.portId == portId }?.apply {
+                if (valid) close()
+            }
         }
 
         withTimeout(timeoutMillis) {
             while (true) {
                 try {
-                    // tryOpenEuiccChannel() will automatically dispose of invalid channels
-                    // and recreate when needed
-                    val channel = findEuiccChannelByPort(physicalSlotId, portId)!!
+                    val channel = if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) {
+                        // tryOpenUsbEuiccChannel() will always try to reopen the channel, even if
+                        // a USB channel already exists
+                        tryOpenUsbEuiccChannel()
+                        usbChannel!!
+                    } else {
+                        // tryOpenEuiccChannel() will automatically dispose of invalid channels
+                        // and recreate when needed
+                        findEuiccChannelByPort(physicalSlotId, portId)!!
+                    }
                     check(channel.valid) { "Invalid channel" }
                     break
                 } catch (e: Exception) {

+ 1 - 1
app-common/src/main/java/im/angry/openeuicc/service/EuiccChannelManagerService.kt

@@ -448,7 +448,7 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
         portId: Int,
         iccid: String,
         enable: Boolean, // Enable or disable the profile indicated in iccid
-        reconnectTimeoutMillis: Long = 0 // 0 = do not wait for reconnect, useful for USB readers
+        reconnectTimeoutMillis: Long = 0 // 0 = do not wait for reconnect
     ): ForegroundTaskSubscriberFlow =
         launchForegroundTask(
             getString(R.string.task_profile_switch),

+ 1 - 5
app-common/src/main/java/im/angry/openeuicc/ui/EuiccManagementFragment.kt

@@ -228,11 +228,7 @@ open class EuiccManagementFragment : Fragment(), EuiccProfilesChangedListener,
                 portId,
                 iccid,
                 enable,
-                reconnectTimeoutMillis = if (isUsb) {
-                    0
-                } else {
-                    30 * 1000
-                }
+                reconnectTimeoutMillis = 30 * 1000
             ).waitDone()
 
             when (err) {