Commit 31d3a16f by Alberto Doval

Added initialize method to BaseScan class.

Refactored EDA500K implementation.
parent 63828a3b
......@@ -24,10 +24,14 @@
<config-file target="res/xml/config.xml" parent="/*">
<feature name="BarcodeScan">
<param name="android-package" value="com.cocodin.barcodescan.plugin.BarcodeScan" />
<param name="onload" value="true" />
<param name="android-package" value="com.cocodin.barcodescan.plugin.BarcodeScan" />
<param name="onload" value="true" />
</feature>
</config-file>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.honeywell.decode.permission.DECODE" />
</config-file>
<source-file src="src/android/com/cocodin/barcodescan/plugin/BarcodeScan.java" target-dir="src/com/cocodin/barcodescan/plugin" />
<source-file src="src/android/com/cocodin/barcodescan/plugin/BaseScan.java" target-dir="src/com/cocodin/barcodescan/plugin" />
......
......@@ -13,6 +13,10 @@ import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static android.R.attr.action;
public class BarcodeScan extends CordovaPlugin {
......@@ -30,46 +34,69 @@ public class BarcodeScan extends CordovaPlugin {
private BaseScan mDevice;
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
private Map<String, BaseScan> mDevices = new HashMap<String, BaseScan>();
public void initialize(final CordovaInterface cordova, final CordovaWebView webView) {
super.initialize(cordova, webView);
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
init(cordova, webView);
}
});
Log.d(TAG, "Initializing BarcodeScan Plugin");
}
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) {
Log.d(TAG, "execute: " + action);
if (action.equalsIgnoreCase("scan")) {
public void init(CordovaInterface cordova, CordovaWebView webView) {
for (int i=0;i<jaDevices.length();i++) {
try {
String deviceName = args.get(0).toString();
//ensure device is created and is the correct device (user could change the device in mobile UI)
if (mDevice == null || !mDevice.getDeviceName().equalsIgnoreCase(deviceName)) {
mDevice = selectDevice(deviceName);
String deviceName = jaDevices.getString(i);
if (EDA50K.equalsIgnoreCase(deviceName)) {
mDevices.put(EDA50K, new com.cocodin.barcodescan.plugin.devices.EDA50K(cordova, webView));
} else if (C4050.equalsIgnoreCase(deviceName)) {
mDevices.put(C4050, new com.cocodin.barcodescan.plugin.devices.C4050(cordova, webView));
} else if (NQUIRE300.equalsIgnoreCase(deviceName)) {
mDevices.put(NQUIRE300, new com.cocodin.barcodescan.plugin.devices.NQuire300(cordova, webView));
} else {
mDevices.put(CAMERA, new com.cocodin.barcodescan.plugin.devices.Camera(cordova, webView));
}
mDevice.scan(cordova, webView, args, callbackContext);
}
catch (Exception e) {
Log.e(TAG, e.getMessage());
callbackContext.sendPluginResult(new PluginResult(Status.ERROR, e.getMessage()));
}
} else if (action.equalsIgnoreCase("getDevices")) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, jaDevices));
}
return true;
}
public BaseScan selectDevice(String deviceName) {
if (EDA50K.equalsIgnoreCase(deviceName)) {
return new com.cocodin.barcodescan.plugin.devices.EDA50K();
}
else if (C4050.equalsIgnoreCase(deviceName)) {
return new com.cocodin.barcodescan.plugin.devices.C4050();
}
else if (NQUIRE300.equalsIgnoreCase(deviceName)) {
return new com.cocodin.barcodescan.plugin.devices.NQuire300();
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) {
Log.d(TAG, "execute: " + action);
if (action.equalsIgnoreCase("scan")) {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
String deviceName = args.get(0).toString();
//ensure device is created and is the correct device (user could change the device in mobile UI)
if (mDevice == null || !mDevice.getDeviceName().equalsIgnoreCase(deviceName)) {
mDevice = selectDevice(deviceName);
}
mDevice.scan(cordova, webView, args, callbackContext);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
callbackContext.sendPluginResult(new PluginResult(Status.ERROR, e.getMessage()));
}
}
});
}
else {
return new com.cocodin.barcodescan.plugin.devices.Camera();
else if (action.equalsIgnoreCase("getDevices")) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, jaDevices));
}
return true;
}
public BaseScan selectDevice(String deviceName) {
return mDevices.get(deviceName);
}
@Override
......
......@@ -11,6 +11,12 @@ import org.json.JSONArray;
public abstract class BaseScan {
public BaseScan(CordovaInterface cordova, CordovaWebView webView) {
};
public abstract void initialize(CordovaInterface cordova, CordovaWebView webView);
public abstract String getDeviceName();
public abstract void scan(final CordovaInterface cordova, CordovaWebView webView, JSONArray args, final CallbackContext callbackContext);
......
......@@ -26,6 +26,16 @@ public class C4050 extends BaseScan {
private static Barcode2DWithSoft mInstance;
public C4050(CordovaInterface cordova, CordovaWebView webView) {
super(cordova, webView);
initialize(cordova, webView);
}
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
}
@Override
public String getDeviceName() {
return TAG;
......@@ -81,7 +91,7 @@ public class C4050 extends BaseScan {
public void onDestroy() {
}
@Override
public void onResume(boolean multitasking) {
......
......@@ -20,6 +20,17 @@ public class Camera extends BaseScan {
public String getDeviceName() {
return TAG;
}
public Camera(CordovaInterface cordova, CordovaWebView webView) {
super(cordova, webView);
initialize(cordova, webView);
}
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
}
@Override
public void scan(CordovaInterface cordova, CordovaWebView webView, JSONArray args, CallbackContext callbackContext) {
BarcodeScanner barcodeScanner = new BarcodeScanner();
......
......@@ -36,21 +36,24 @@ public class EDA50K extends BaseScan implements BarcodeListener {
private CallbackContext currentCallbackContext = null;
public EDA50K(CordovaInterface cordova, CordovaWebView webView) {
super(cordova, webView);
initialize(cordova, webView);
}
@Override
public String getDeviceName() {
return TAG;
}
@Override
public void scan(final CordovaInterface cordova, CordovaWebView webView, JSONArray args,
final CallbackContext callbackContext) {
public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
if (manager == null) {
AidcManager.create(cordova.getActivity(), new CreatedCallback() {
@Override
public void onCreated(AidcManager aidcManager) {
manager = aidcManager;
barcodeReader = manager.createBarcodeReader("dcs.scanner.ring");
barcodeReader = manager.createBarcodeReader();
if (barcodeReader != null) {
......@@ -97,19 +100,20 @@ public class EDA50K extends BaseScan implements BarcodeListener {
barcodeReader.setProperties(properties);
//Perform first read after initialization
readBarcode(callbackContext);
claim();
} else {
callbackContext.error("Failed to open barcode reader");
}
};
}
});
} else {
readBarcode(callbackContext);
}
}
@Override
public void scan(final CordovaInterface cordova, CordovaWebView webView, JSONArray args, final CallbackContext callbackContext) {
this.currentCallbackContext = callbackContext;
}
@Override
public void onStart() {
}
......@@ -139,18 +143,16 @@ public class EDA50K extends BaseScan implements BarcodeListener {
}
}
private void readBarcode(CallbackContext callbackContext) {
private void claim() {
if (barcodeReader != null) {
try {
barcodeReader.claim();
callbackContext.success();
Log.d(TAG, "Claim OK");
} catch (ScannerUnavailableException e) {
e.printStackTrace();
callbackContext.error("Unable to claim reader");
Log.e(TAG, e.getMessage());
}
} else {
callbackContext.error("Reader not open");
Log.e(TAG, "Barcode reader not opened");
}
}
......@@ -176,9 +178,7 @@ public class EDA50K extends BaseScan implements BarcodeListener {
if (currentCallbackContext != null) {
try {
JSONObject obj = new JSONObject();
obj.put("success", true);
obj.put("data", barcodeReadEvent.getBarcodeData());
obj.put("text", barcodeReadEvent.getBarcodeData());
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
result.setKeepCallback(true);
currentCallbackContext.sendPluginResult(result);
......@@ -190,6 +190,7 @@ public class EDA50K extends BaseScan implements BarcodeListener {
@Override
public void onFailureEvent(BarcodeFailureEvent barcodeFailureEvent) {
Log.i(TAG, "Error reading barcode.");
if (currentCallbackContext != null) {
try {
JSONObject obj = new JSONObject();
......
......@@ -28,6 +28,16 @@ public class NQuire300 extends BaseScan {
return TAG;
}
public NQuire300(CordovaInterface cordova, CordovaWebView webView) {
super(cordova, webView);
initialize(cordova, webView);
}
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
}
@Override
public void scan(CordovaInterface cordova, CordovaWebView webView, JSONArray args, CallbackContext callbackContext) {
Context context = cordova.getActivity();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment