瀏覽代碼

Move findEuiccChannelBySlot to non-blocking

Peter Cai 1 年之前
父節點
當前提交
8ac46bd778
共有 1 個文件被更改,包括 18 次插入15 次删除
  1. 18 15
      app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelManager.kt

+ 18 - 15
app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelManager.kt

@@ -88,23 +88,26 @@ open class DefaultEuiccChannelManager(
         }
     }
 
-    override fun findEuiccChannelBySlotBlocking(logicalSlotId: Int): EuiccChannel? =
-        runBlocking {
-            withContext(Dispatchers.IO) {
-                if (logicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) {
-                    return@withContext usbChannel
-                }
+    private suspend fun findEuiccChannelBySlot(logicalSlotId: Int): EuiccChannel? =
+        withContext(Dispatchers.IO) {
+            if (logicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) {
+                return@withContext usbChannel
+            }
 
-                for (card in uiccCards) {
-                    for (port in card.ports) {
-                        if (port.logicalSlotIndex == logicalSlotId) {
-                            return@withContext tryOpenEuiccChannel(port)
-                        }
+            for (card in uiccCards) {
+                for (port in card.ports) {
+                    if (port.logicalSlotIndex == logicalSlotId) {
+                        return@withContext tryOpenEuiccChannel(port)
                     }
                 }
-
-                null
             }
+
+            null
+        }
+
+    override fun findEuiccChannelBySlotBlocking(logicalSlotId: Int): EuiccChannel? =
+        runBlocking {
+            findEuiccChannelBySlot(logicalSlotId)
         }
 
     override fun findEuiccChannelByPhysicalSlotBlocking(physicalSlotId: Int): EuiccChannel? =
@@ -164,7 +167,7 @@ open class DefaultEuiccChannelManager(
         portId: Int,
         fn: suspend (EuiccChannel) -> R
     ): R {
-        val channel = findEuiccChannelByPortBlocking(physicalSlotId, portId)
+        val channel = findEuiccChannelByPort(physicalSlotId, portId)
             ?: throw EuiccChannelManager.EuiccChannelNotFoundException()
         val wrapper = EuiccChannelWrapper(channel)
         try {
@@ -180,7 +183,7 @@ open class DefaultEuiccChannelManager(
         logicalSlotId: Int,
         fn: suspend (EuiccChannel) -> R
     ): R {
-        val channel = findEuiccChannelBySlotBlocking(logicalSlotId)
+        val channel = findEuiccChannelBySlot(logicalSlotId)
             ?: throw EuiccChannelManager.EuiccChannelNotFoundException()
         val wrapper = EuiccChannelWrapper(channel)
         try {