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 CardChannel plainChannel;
private AssetManager assets; private AssetManager assets;
private String capPath; private String capPath;
private EventEmitter eventEmitter;
private GlobalPlatformCommandSet cmdSet; private GlobalPlatformCommandSet cmdSet;
private static final String TAG = "SmartCardInstaller"; 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.plainChannel = channel;
this.assets = assets; this.assets = assets;
this.capPath = capPath; this.capPath = capPath;
this.eventEmitter = eventEmitter;
} }
public void start() throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException { public void start() throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
Log.i(TAG, "installation started..."); Log.i(TAG, "installation started...");
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
eventEmitter.emit("keycardInstallationProgress", 0.05);
Log.i(TAG, "select ISD..."); Log.i(TAG, "select ISD...");
cmdSet = new GlobalPlatformCommandSet(this.plainChannel); cmdSet = new GlobalPlatformCommandSet(this.plainChannel);
cmdSet.select().checkOK(); cmdSet.select().checkOK();
Log.i(TAG, "opening secure channel..."); Log.i(TAG, "opening secure channel...");
cmdSet.openSecureChannel(); cmdSet.openSecureChannel();
Log.i(TAG, "deleting old version (if present)..."); Log.i(TAG, "deleting old version (if present)...");
cmdSet.deleteKeycardInstancesAndPackage(); cmdSet.deleteKeycardInstancesAndPackage();
eventEmitter.emit("keycardInstallationProgress", 0.1);
Log.i(TAG, "loading package..."); Log.i(TAG, "loading package...");
cmdSet.loadKeycardPackage(this.assets.open(this.capPath), new LoadCallback() { cmdSet.loadKeycardPackage(this.assets.open(this.capPath), new LoadCallback() {
public void blockLoaded(int loadedBlock, int blockCount) { public void blockLoaded(int loadedBlock, int blockCount) {
Log.i(TAG, String.format("load %d/%d...", loadedBlock, 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..."); Log.i(TAG, "installing NDEF applet...");
cmdSet.installNDEFApplet(Hex.decode("0024d40f12616e64726f69642e636f6d3a706b67696d2e7374617475732e657468657265756d")).checkOK(); cmdSet.installNDEFApplet(Hex.decode("0024d40f12616e64726f69642e636f6d3a706b67696d2e7374617475732e657468657265756d")).checkOK();
eventEmitter.emit("keycardInstallationProgress", 0.72);
Log.i(TAG, "installing Keycard applet..."); Log.i(TAG, "installing Keycard applet...");
cmdSet.installKeycardApplet().checkOK(); cmdSet.installKeycardApplet().checkOK();
eventEmitter.emit("keycardInstallationProgress", 0.88);
long duration = System.currentTimeMillis() - startTime; long duration = System.currentTimeMillis() - startTime;
Log.i(TAG, String.format("\n\ninstallation completed in %d seconds", duration / 1000)); 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.content.pm.PackageManager;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.EventLog;
import android.util.Log; import android.util.Log;
import com.facebook.react.bridge.*; import com.facebook.react.bridge.*;
@ -37,6 +38,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
private ReactContext reactContext; private ReactContext reactContext;
private NfcAdapter nfcAdapter; private NfcAdapter nfcAdapter;
private CardChannel cardChannel; private CardChannel cardChannel;
private EventEmitter eventEmitter;
private static final String TAG = "SmartCard"; private static final String TAG = "SmartCard";
private static final String WALLET_PATH = "m/44'/0'/0'/0/0"; 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.activity = activity;
this.reactContext = reactContext; this.reactContext = reactContext;
this.nfcAdapter = NfcAdapter.getDefaultAdapter(activity.getBaseContext()); this.nfcAdapter = NfcAdapter.getDefaultAdapter(activity.getBaseContext());
this.eventEmitter = new EventEmitter(reactContext);
} }
public String getName() { public String getName() {
@ -75,12 +78,12 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
@Override @Override
public void onConnected(final CardChannel channel) { public void onConnected(final CardChannel channel) {
this.cardChannel = channel; this.cardChannel = channel;
sendEvent(reactContext, "keyCardOnConnected", null); eventEmitter.emit("keyCardOnConnected", null);
} }
@Override @Override
public void onDisconnected() { public void onDisconnected() {
sendEvent(reactContext, "keyCardOnDisconnected", null); eventEmitter.emit( "keyCardOnDisconnected", null);
} }
@Override @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 { public SmartCardSecrets init() throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel); KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK(); cmdSet.select().checkOK();
eventEmitter.emit("keycardInstallationProgress", 0.90);
SmartCardSecrets s = SmartCardSecrets.generate(); SmartCardSecrets s = SmartCardSecrets.generate();
eventEmitter.emit("keycardInstallationProgress", 0.93);
cmdSet.init(s.getPin(), s.getPuk(), s.getPairingPassword()).checkOK(); cmdSet.init(s.getPin(), s.getPuk(), s.getPairingPassword()).checkOK();
eventEmitter.emit("keycardInstallationProgress", 1.0);
return s; 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 { 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(); installer.start();
} }
public SmartCardSecrets installAppletAndInitCard(AssetManager assets, String capPath) throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException { 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(); installer.start();
return init(); return init();