Skip to content

Commit

Permalink
fix(suite-native): enable to confirm receive address outside our app
Browse files Browse the repository at this point in the history
- We are closing device connection on going to background, so we have to reopen it once needed. We can open the device even on background. But Suite has to be still running, not killed.
- Closing all the devices OnActivityEntersBackground is a good thing because we want to allow other apps (like browser) to work with the Trezor too and it is a good practice to frees up system resources.
  • Loading branch information
matejkriz committed Oct 10, 2024
1 parent 65a8499 commit 5186cc9
Showing 1 changed file with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ class ReactNativeUsbModule : Module() {
for (device in devicesList) {
if (usbManager.hasPermission(device)) {
Log.d(LOG_TAG, "Has permission, send event onDeviceConnected: $device")
val webUsbDevice = openDevice(device.deviceName)

val webUsbDevice = if (hasOpenedConnection(device.deviceName)) {
Log.d(LOG_TAG, "Device already opened: $device")
getWebUSBDevice(device)
} else {
Log.d(LOG_TAG, "Opening device: $device")
openDevice(device.deviceName)
}

sendEvent(ON_DEVICE_CONNECT_EVENT_NAME, webUsbDevice)
devicesHistory[device.deviceName] = webUsbDevice
} else if (!devicesRequestedPermissions.contains(
Expand Down Expand Up @@ -218,7 +226,7 @@ class ReactNativeUsbModule : Module() {
Log.e(LOG_TAG, "Failed to open device ${device.deviceName}")
throw Exception("Failed to open device ${device.deviceName}")
}
Log.d(LOG_TAG, "Opening device ${device.deviceName}")

openedConnections[device.deviceName] = usbConnection

// log all endpoints for debug purposes
Expand Down Expand Up @@ -300,7 +308,10 @@ class ReactNativeUsbModule : Module() {
val dataByteArray = data.split(",").map { it.toInt().toByte() }.toByteArray()
Log.d(LOG_TAG, "dataByteArray: $dataByteArray")
val device = getDeviceByName(deviceName)
val usbConnection = getOpenedConnection(deviceName)
val usbConnection = openedConnections.getOrPut(device.deviceName) {
Log.d(LOG_TAG, "Reopening device ${device.deviceName}")
usbManager.openDevice(device) ?: throw Exception("Failed to open device ${device.deviceName}")
}

val usbEndpoint = device.getInterface(INTERFACE_INDEX).getEndpoint(1)
if (usbEndpoint == null) {
Expand All @@ -315,7 +326,11 @@ class ReactNativeUsbModule : Module() {
private fun transferIn(deviceName: String, endpointNumber: Int, length: Int): IntArray {
Log.d(LOG_TAG, "Reading data from device $deviceName")
val device = getDeviceByName(deviceName)
val usbConnection = getOpenedConnection(deviceName)

val usbConnection = openedConnections.getOrPut(device.deviceName) {
Log.d(LOG_TAG, "Reopening device ${device.deviceName}")
usbManager.openDevice(device) ?: throw Exception("Failed to open device ${device.deviceName}")
}

val usbEndpoint = device.getInterface(INTERFACE_INDEX).getEndpoint(0)

Expand Down

0 comments on commit 5186cc9

Please sign in to comment.