Browse Source

EuiccChannelManager: Stop emitting real EuiccChannel for USB

Peter Cai 1 year ago
parent
commit
573dce56a6

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

@@ -251,20 +251,20 @@ open class DefaultEuiccChannelManager(
         }
     }.flowOn(Dispatchers.IO)
 
-    override suspend fun enumerateUsbEuiccChannel(): Pair<UsbDevice?, EuiccChannel?> =
+    override suspend fun tryOpenUsbEuiccChannel(): Pair<UsbDevice?, Boolean> =
         withContext(Dispatchers.IO) {
             usbManager.deviceList.values.forEach { device ->
                 Log.i(TAG, "Scanning USB device ${device.deviceId}:${device.vendorId}")
                 val iface = device.getSmartCardInterface() ?: return@forEach
                 // If we don't have permission, tell UI code that we found a candidate device, but we
                 // need permission to be able to do anything with it
-                if (!usbManager.hasPermission(device)) return@withContext Pair(device, null)
+                if (!usbManager.hasPermission(device)) return@withContext Pair(device, false)
                 Log.i(TAG, "Found CCID interface on ${device.deviceId}:${device.vendorId}, and has permission; trying to open channel")
                 try {
                     val channel = euiccChannelFactory.tryOpenUsbEuiccChannel(device, iface)
                     if (channel != null && channel.lpa.valid) {
                         usbChannel = channel
-                        return@withContext Pair(device, channel)
+                        return@withContext Pair(device, true)
                     }
                 } catch (e: Exception) {
                     // Ignored -- skip forward
@@ -272,7 +272,7 @@ open class DefaultEuiccChannelManager(
                 }
                 Log.i(TAG, "No valid eUICC channel found on USB device ${device.deviceId}:${device.vendorId}")
             }
-            return@withContext Pair(null, null)
+            return@withContext Pair(null, false)
         }
 
     override fun invalidate() {

+ 6 - 2
app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt

@@ -31,9 +31,13 @@ interface EuiccChannelManager {
      * 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
      * as a "port" with id 99. When user interaction is required to obtain permission
-     * to interact with the device, the second return value (EuiccChannel) will be null.
+     * to interact with the device, the second return value will be false.
+     *
+     * Returns (usbDevice, canOpen). canOpen is false if either (1) no usb reader is found;
+     * or (2) usb reader is found, but user interaction is required for access;
+     * or (3) usb reader is found, but we are unable to open ISD-R.
      */
-    suspend fun enumerateUsbEuiccChannel(): Pair<UsbDevice?, EuiccChannel?>
+    suspend fun tryOpenUsbEuiccChannel(): Pair<UsbDevice?, Boolean>
 
     /**
      * Wait for a slot + port to reconnect (i.e. become valid again)

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

@@ -143,7 +143,7 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
         euiccChannelManagerService.waitForForegroundTask()
 
         val (usbDevice, _) = withContext(Dispatchers.IO) {
-            euiccChannelManager.enumerateUsbEuiccChannel()
+            euiccChannelManager.tryOpenUsbEuiccChannel()
         }
 
         val newPages: MutableList<Page> = mutableListOf()

+ 6 - 9
app-common/src/main/java/im/angry/openeuicc/ui/UsbCcidReaderFragment.kt

@@ -20,7 +20,6 @@ import androidx.fragment.app.Fragment
 import androidx.fragment.app.commit
 import androidx.lifecycle.lifecycleScope
 import im.angry.openeuicc.common.R
-import im.angry.openeuicc.core.EuiccChannel
 import im.angry.openeuicc.core.EuiccChannelManager
 import im.angry.openeuicc.util.*
 import kotlinx.coroutines.Dispatchers
@@ -73,7 +72,6 @@ class UsbCcidReaderFragment : Fragment(), OpenEuiccContextMarker {
     private lateinit var loadingProgress: ProgressBar
 
     private var usbDevice: UsbDevice? = null
-    private var usbChannel: EuiccChannel? = null
 
     override fun onCreateView(
         inflater: LayoutInflater,
@@ -140,26 +138,25 @@ class UsbCcidReaderFragment : Fragment(), OpenEuiccContextMarker {
         permissionButton.visibility = View.GONE
         loadingProgress.visibility = View.VISIBLE
 
-        val (device, channel) = withContext(Dispatchers.IO) {
-            euiccChannelManager.enumerateUsbEuiccChannel()
+        val (device, canOpen) = withContext(Dispatchers.IO) {
+            euiccChannelManager.tryOpenUsbEuiccChannel()
         }
 
         loadingProgress.visibility = View.GONE
 
         usbDevice = device
-        usbChannel = channel
 
-        if (device != null && channel == null && !usbManager.hasPermission(device)) {
+        if (device != null && !canOpen && !usbManager.hasPermission(device)) {
             text.text = getString(R.string.usb_permission_needed)
             text.visibility = View.VISIBLE
             permissionButton.visibility = View.VISIBLE
-        } else if (device != null && channel != null) {
+        } else if (device != null && canOpen) {
             childFragmentManager.commit {
                 replace(
                     R.id.child_container,
                     appContainer.uiComponentFactory.createEuiccManagementFragment(
-                        channel.slotId,
-                        channel.portId
+                        EuiccChannelManager.USB_CHANNEL_ID,
+                        0
                     )
                 )
             }