Skip to content

Commit

Permalink
Fix race condition on connect/disconnect (Android) (#57)
Browse files Browse the repository at this point in the history
Fix race condition on connect/disconnect (Android)
  • Loading branch information
bitgamma authored Dec 6, 2024
1 parent 46dc14c commit e78f46d
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions android/src/main/java/im/status/ethereum/keycard/SmartCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
private CardChannel cardChannel;
private EventEmitter eventEmitter;
private static final String TAG = "SmartCard";
private Boolean started = false;
private Boolean listening = false;
private boolean started = false;
private volatile boolean listening = false;
private HashMap<String, String> pairings;
private String[] caPubKeys;
private String skipVerificationUID;
private final Object lock = new Object();

private static final String MASTER_PATH = "m";
private static final String ROOT_PATH = "m/44'/60'/0'/0";
Expand Down Expand Up @@ -109,31 +110,39 @@ public void stop(Activity activity) {
}

public void startNFC() {
this.listening = true;
if (this.cardChannel != null) {
eventEmitter.emit("keyCardOnConnected", null);
synchronized(lock) {
this.listening = true;
if (this.cardChannel != null) {
eventEmitter.emit("keyCardOnConnected", null);
}
}
}

public void stopNFC() {
this.listening = false;
synchronized(lock) {
this.listening = false;
}
}

@Override
public void onConnected(final CardChannel channel) {
this.cardChannel = channel;
synchronized(lock) {
this.cardChannel = channel;

if (this.listening) {
eventEmitter.emit("keyCardOnConnected", null);
if (this.listening) {
eventEmitter.emit("keyCardOnConnected", null);
}
}
}

@Override
public void onDisconnected() {
this.cardChannel = null;

if (this.listening) {
eventEmitter.emit("keyCardOnDisconnected", null);
synchronized(lock) {
this.cardChannel = null;

if (this.listening) {
eventEmitter.emit("keyCardOnDisconnected", null);
}
}
}

Expand Down

0 comments on commit e78f46d

Please sign in to comment.