ソースを参照

Add EuiccChannelManager.flowEuiccPorts()

This is to prepare for refactoring EuiccChannel usage out everywhere
Peter Cai 1 年間 前
コミット
2ece6af174

+ 25 - 8
app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelManager.kt

@@ -10,6 +10,15 @@ import im.angry.openeuicc.di.AppContainer
 import im.angry.openeuicc.util.*
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.delay
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.collect
+import kotlinx.coroutines.flow.count
+import kotlinx.coroutines.flow.flow
+import kotlinx.coroutines.flow.flowOn
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.flow.mapNotNull
+import kotlinx.coroutines.flow.onEach
+import kotlinx.coroutines.flow.toList
 import kotlinx.coroutines.runBlocking
 import kotlinx.coroutines.sync.Mutex
 import kotlinx.coroutines.sync.withLock
@@ -222,17 +231,25 @@ open class DefaultEuiccChannelManager(
 
     override suspend fun enumerateEuiccChannels(): List<EuiccChannel> =
         withContext(Dispatchers.IO) {
-            uiccCards.flatMap { info ->
-                info.ports.mapNotNull { port ->
-                    tryOpenEuiccChannel(port)?.also {
-                        Log.d(
-                            TAG,
-                            "Found eUICC on slot ${info.physicalSlotIndex} port ${port.portIndex}"
-                        )
-                    }
+            flowEuiccPorts().mapNotNull { (slotId, portId) ->
+                findEuiccChannelByPort(slotId, portId)
+            }.toList()
+        }
+
+    override fun flowEuiccPorts(): Flow<Pair<Int, Int>> = flow {
+        uiccCards.forEach { info ->
+            info.ports.forEach { port ->
+                tryOpenEuiccChannel(port)?.also {
+                    Log.d(
+                        TAG,
+                        "Found eUICC on slot ${info.physicalSlotIndex} port ${port.portIndex}"
+                    )
+
+                    emit(Pair(info.physicalSlotIndex, port.portIndex))
                 }
             }
         }
+    }.flowOn(Dispatchers.IO)
 
     override suspend fun enumerateUsbEuiccChannel(): Pair<UsbDevice?, EuiccChannel?> =
         withContext(Dispatchers.IO) {

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

@@ -1,6 +1,7 @@
 package im.angry.openeuicc.core
 
 import android.hardware.usb.UsbDevice
+import kotlinx.coroutines.flow.Flow
 
 /**
  * EuiccChannelManager holds references to, and manages the lifecycles of, individual
@@ -24,6 +25,8 @@ interface EuiccChannelManager {
      */
     suspend fun enumerateEuiccChannels(): List<EuiccChannel>
 
+    fun flowEuiccPorts(): Flow<Pair<Int, Int>>
+
     /**
      * Scan all possible USB devices for CCID readers that may contain eUICC cards.
      * If found, try to open it for access, and add it to the internal EuiccChannel cache