emit installation progress events

This commit is contained in:
Dmitry Novotochinov 2018-12-19 13:28:28 +03:00 committed by Dmitry Novotochinov
parent b89b752aa4
commit 6171fe44e5
3 changed files with 59 additions and 13 deletions

View File

@ -0,0 +1,30 @@
package im.status.ethereum.keycard;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import javax.annotation.Nullable;
public class EventEmitter {
private ReactContext reactContext;
public EventEmitter(ReactContext reactContext) {
this.reactContext = reactContext;
}
public void emit(String eventName, @Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
public void emit(String eventName, double progress) {
WritableMap params = Arguments.createMap();
params.putDouble("progress", progress);
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}

View File

@ -17,44 +17,58 @@ public class Installer {
private CardChannel plainChannel;
private AssetManager assets;
private String capPath;
private EventEmitter eventEmitter;
private GlobalPlatformCommandSet cmdSet;
private static final String TAG = "SmartCardInstaller";
public Installer(CardChannel channel, AssetManager assets, String capPath) {
public Installer(CardChannel channel, AssetManager assets, String capPath, EventEmitter eventEmitter) {
this.plainChannel = channel;
this.assets = assets;
this.capPath = capPath;
this.eventEmitter = eventEmitter;
}
public void start() throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
Log.i(TAG, "installation started...");
long startTime = System.currentTimeMillis();
eventEmitter.emit("keycardInstallationProgress", 0.05);
Log.i(TAG, "select ISD...");
cmdSet = new GlobalPlatformCommandSet(this.plainChannel);
cmdSet.select().checkOK();
Log.i(TAG, "opening secure channel...");
cmdSet.openSecureChannel();
Log.i(TAG, "deleting old version (if present)...");
cmdSet.deleteKeycardInstancesAndPackage();
eventEmitter.emit("keycardInstallationProgress", 0.1);
Log.i(TAG, "loading package...");
cmdSet.loadKeycardPackage(this.assets.open(this.capPath), new LoadCallback() {
public void blockLoaded(int loadedBlock, int blockCount) {
Log.i(TAG, String.format("load %d/%d...", loadedBlock, blockCount));
eventEmitter.emit("keycardInstallationProgress", 0.1 + (0.6 * loadedBlock / blockCount));
}
});
Log.i(TAG, "installing NDEF applet...");
cmdSet.installNDEFApplet(Hex.decode("0024d40f12616e64726f69642e636f6d3a706b67696d2e7374617475732e657468657265756d")).checkOK();
eventEmitter.emit("keycardInstallationProgress", 0.72);
Log.i(TAG, "installing Keycard applet...");
cmdSet.installKeycardApplet().checkOK();
eventEmitter.emit("keycardInstallationProgress", 0.88);
long duration = System.currentTimeMillis() - startTime;
Log.i(TAG, String.format("\n\ninstallation completed in %d seconds", duration / 1000));
}

View File

@ -9,6 +9,7 @@ import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.nfc.NfcAdapter;
import android.support.annotation.Nullable;
import android.util.EventLog;
import android.util.Log;
import com.facebook.react.bridge.*;
@ -37,6 +38,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
private ReactContext reactContext;
private NfcAdapter nfcAdapter;
private CardChannel cardChannel;
private EventEmitter eventEmitter;
private static final String TAG = "SmartCard";
private static final String WALLET_PATH = "m/44'/0'/0'/0/0";
@ -49,6 +51,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
this.activity = activity;
this.reactContext = reactContext;
this.nfcAdapter = NfcAdapter.getDefaultAdapter(activity.getBaseContext());
this.eventEmitter = new EventEmitter(reactContext);
}
public String getName() {
@ -75,12 +78,12 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
@Override
public void onConnected(final CardChannel channel) {
this.cardChannel = channel;
sendEvent(reactContext, "keyCardOnConnected", null);
eventEmitter.emit("keyCardOnConnected", null);
}
@Override
public void onDisconnected() {
sendEvent(reactContext, "keyCardOnDisconnected", null);
eventEmitter.emit( "keyCardOnDisconnected", null);
}
@Override
@ -109,21 +112,20 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
}
}
private void sendEvent(ReactContext reactContext,
String eventName,
@Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
public SmartCardSecrets init() throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK();
eventEmitter.emit("keycardInstallationProgress", 0.90);
SmartCardSecrets s = SmartCardSecrets.generate();
eventEmitter.emit("keycardInstallationProgress", 0.93);
cmdSet.init(s.getPin(), s.getPuk(), s.getPairingPassword()).checkOK();
eventEmitter.emit("keycardInstallationProgress", 1.0);
return s;
}
@ -354,12 +356,12 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
}
public void installApplet(AssetManager assets, String capPath) throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
Installer installer = new Installer(this.cardChannel, assets, capPath);
Installer installer = new Installer(this.cardChannel, assets, capPath, eventEmitter);
installer.start();
}
public SmartCardSecrets installAppletAndInitCard(AssetManager assets, String capPath) throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
Installer installer = new Installer(this.cardChannel, assets, capPath);
Installer installer = new Installer(this.cardChannel, assets, capPath, eventEmitter);
installer.start();
return init();