ソースを参照

New withEuiccChannel() variant with logical slot ID

Peter Cai 1 年間 前
コミット
0961ef70f4

+ 16 - 0
app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelManager.kt

@@ -176,6 +176,22 @@ open class DefaultEuiccChannelManager(
         }
     }
 
+    override suspend fun <R> withEuiccChannel(
+        logicalSlotId: Int,
+        fn: suspend (EuiccChannel) -> R
+    ): R {
+        val channel = findEuiccChannelBySlotBlocking(logicalSlotId)
+            ?: throw EuiccChannelManager.EuiccChannelNotFoundException()
+        val wrapper = EuiccChannelWrapper(channel)
+        try {
+            return withContext(Dispatchers.IO) {
+                fn(wrapper)
+            }
+        } finally {
+            wrapper.invalidateWrapper()
+        }
+    }
+
     override suspend fun waitForReconnect(physicalSlotId: Int, portId: Int, timeoutMillis: Long) {
         if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) return
 

+ 8 - 0
app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt

@@ -81,6 +81,14 @@ interface EuiccChannelManager {
         fn: suspend (EuiccChannel) -> R
     ): R
 
+    /**
+     * Same as withEuiccChannel(Int, Int, (EuiccChannel) -> R) but instead uses logical slot ID
+     */
+    suspend fun <R> withEuiccChannel(
+        logicalSlotId: Int,
+        fn: suspend (EuiccChannel) -> R
+    ): R
+
     /**
      * Invalidate all EuiccChannels previously cached by this Manager
      */