Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crash signing via wc #1786

Merged
merged 7 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/src/main/res/navigation/external_sign_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
android:id="@+id/ConfirmSignExtrinsicFragment"
android:name="io.novafoundation.nova.feature_external_sign_impl.presentation.signExtrinsic.ExternalSignFragment"
android:label="ConfirmSignExtrinsicFragment"
app:useAdd="true"
tools:layout="@layout/fragment_confirm_sign_extrinsic">

<action
Expand Down
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem arose when it was necessary to sign several batch all transactions from the ledger.

How the problem manifested itself:

  • Open 1 ledger searches and register BR
  • Sign transaction 1
  • Open 2 ledger searches and register BR
  • Close screen 1 and unregister BR
  • Sign transaction 2
  • Close screen 2 and try to unregister BR

The crash occurs because BR is already in the unregistered state

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class UsbLedgerDeviceDiscoveryService(
private val contextManager: ContextManager
) : LedgerDeviceDiscoveryService {

private var discoveringSubscribersManager = DiscoveringSubscribersManager()

private val appContext = contextManager.getApplicationContext()

override val discoveredDevices = MutableStateFlow(emptyList<LedgerDevice>())
Expand All @@ -37,6 +39,7 @@ class UsbLedgerDeviceDiscoveryService(
val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
device?.let { discoverDevices() }
}

UsbManager.ACTION_USB_DEVICE_DETACHED -> {
val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
device?.let { discoverDevices() }
Expand All @@ -50,12 +53,22 @@ class UsbLedgerDeviceDiscoveryService(
addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
}
appContext.registerReceiver(usbReceiver, filter)

if (discoveringSubscribersManager.noSubscribers()) {
appContext.registerReceiver(usbReceiver, filter)
}

discoveringSubscribersManager.addSubscriber()

discoverDevices()
}

override fun stopDiscovery() {
appContext.unregisterReceiver(usbReceiver)
discoveringSubscribersManager.removeSubscriber()

if (discoveringSubscribersManager.noSubscribers()) {
appContext.unregisterReceiver(usbReceiver)
}

coroutineScope.coroutineContext.cancelChildren()
}
Expand All @@ -78,3 +91,22 @@ class UsbLedgerDeviceDiscoveryService(
return LedgerDevice(id, name, connection = connection)
}
}

private class DiscoveringSubscribersManager {

private var subscribers = 0

fun addSubscriber() {
subscribers++
}

fun removeSubscriber() {
if (subscribers == 0) return

subscribers--
}

fun noSubscribers(): Boolean {
return subscribers == 0
}
}
Loading