瀏覽代碼

Add new withEuiccChannel() method to EuiccChannelManager

Peter Cai 1 年之前
父節點
當前提交
7cb872a664

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

@@ -159,6 +159,16 @@ open class DefaultEuiccChannelManager(
             findEuiccChannelByPort(physicalSlotId, portId)
         }
 
+    override suspend fun <R> withEuiccChannel(
+        physicalSlotId: Int,
+        portId: Int,
+        fn: (EuiccChannel) -> R
+    ): R {
+        val channel = findEuiccChannelByPortBlocking(physicalSlotId, portId)
+            ?: throw EuiccChannelManager.EuiccChannelNotFoundException()
+        return fn(channel)
+    }
+
     override suspend fun waitForReconnect(physicalSlotId: Int, portId: Int, timeoutMillis: Long) {
         if (physicalSlotId == EuiccChannelManager.USB_CHANNEL_ID) return
 

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

@@ -64,6 +64,16 @@ interface EuiccChannelManager {
     suspend fun findEuiccChannelByPort(physicalSlotId: Int, portId: Int): EuiccChannel?
     fun findEuiccChannelByPortBlocking(physicalSlotId: Int, portId: Int): EuiccChannel?
 
+    class EuiccChannelNotFoundException: Exception("EuiccChannel not found")
+
+    /**
+     * Find a EuiccChannel by its slot and port, then run a callback with a reference to it.
+     * The reference is not supposed to be held outside of the callback.
+     *
+     * If a channel for that slot / port is not found, EuiccChannelNotFoundException is thrown
+     */
+    suspend fun <R> withEuiccChannel(physicalSlotId: Int, portId: Int, fn: (EuiccChannel) -> R): R
+
     /**
      * Invalidate all EuiccChannels previously cached by this Manager
      */