Commit 3ab84f4d by Alberto Doval

Big refactoring.

Separated classes for each device.
Implemented Scan base class for inherit from.
parent cd9e2c0b
{
"name": "barcode-scan-chainway",
"name": "barcode-scan",
"version": "1.0.0",
"cordova": {
"id": "barcode-scan-chainway",
"id": "barcode-scan",
"platforms": [
"android"
]
},
"description": "Chainway barcode scan plugin for hybrid apps."
"description": "Cordova Barcode Scan plugin for hybrid apps. Some devices supported: honeywell, chainway, nquire..."
}
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="barcode-scan-chainway"
id="barcode-scan"
version="1.0.0">
<name>Barcode Scan Chainway Cordova plugin</name>
<name>Cordova Barcode Scan plugin for hybrid apps. Some devices supported: honeywell, chainway, nquire...</name>
<description></description>
<license>MIT</license>
<keywords>barcode, barcode scan, chainway</keywords>
<repo>http://labs.cocodin.com:8888/cocodin-development-team/barcode-scan-chainway.git</repo>
<keywords>barcode, barcode scan</keywords>
<repo>http://labs.cocodin.com:8888/cocodin-development-team/barcode-scan.git</repo>
<engines>
<engine name="cordova-android" version=">=6.0.0" />
......@@ -15,22 +15,27 @@
<!-- android -->
<platform name="android">
<js-module src="www/android/barcode-scan-chainway.js" name="BarcodeScanChainway">
<js-module src="www/android/barcode-scan.js" name="BarcodeScan">
<runs/>
<clobbers target="cordova.plugins.BarcodeScanChainway" />
<clobbers target="cordova.plugins.BarcodeScan" />
</js-module>
<framework src="src/android/barcode-scan-chainway.gradle" custom="true" type="gradleReference" />
<framework src="src/android/barcode-scan.gradle" custom="true" type="gradleReference" />
<config-file target="res/xml/config.xml" parent="/*">
<feature name="BarcodeScanChainway">
<param name="android-package" value="com.cocodin.barcodescan.plugin.BarcodeScanChainway" />
<feature name="BarcodeScan">
<param name="android-package" value="com.cocodin.barcodescan.plugin.BarcodeScan" />
<param name="onload" value="true" />
</feature>
</config-file>
<source-file src="src/android/com/cocodin/barcodescan/plugin/BarcodeScanChainway.java" target-dir="src/com/cocodin/barcodescan/plugin" />
<source-file src="src/android/com/cocodin/barcodescan/plugin/PluginHelper.java" target-dir="src/com/cocodin/barcodescan/plugin" />
<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" />
<source-file src="src/android/com/cocodin/barcodescan/plugin/devices/C4050.java" target-dir="src/com/cocodin/barcodescan/plugin/devices" />
<source-file src="src/android/com/cocodin/barcodescan/plugin/devices/Camera.java" target-dir="src/com/cocodin/barcodescan/plugin/devices" />
<source-file src="src/android/com/cocodin/barcodescan/plugin/devices/EDA50K.java" target-dir="src/com/cocodin/barcodescan/plugin/devices" />
<source-file src="src/android/com/cocodin/barcodescan/plugin/devices/NQuire300.java" target-dir="src/com/cocodin/barcodescan/plugin/devices" />
<source-file src="src/libs/DeviceAPIver20160627.jar" target-dir="libs" />
<source-file src="src/libs/arm64-v8a/libbarcodereader44.so" target-dir="libs/arm64-v8a" />
<source-file src="src/libs/arm64-v8a/libDeviceAPI.so" target-dir="libs/arm64-v8a" />
......
/**
*/
package com.cocodin.barcodescan.plugin;
import android.util.Log;
import com.cocodin.barcodescan.plugin.devices.*;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import java.util.Arrays;
public class BarcodeScan extends CordovaPlugin {
private static final String TAG = "BarcodeScan";
public static final String CAMERA = "camera";
public static final String C4050 = "c4050";
public static final String NQUIRE300 = "NQuire300";
public static final String EDA50K = "EDA50K";
public static JSONArray jaDevices = new JSONArray(Arrays.asList(CAMERA, C4050, NQUIRE300, EDA50K));
private BaseScan mDevice;
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(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")) {
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 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();
}
else {
return new com.cocodin.barcodescan.plugin.devices.Camera();
}
}
@Override
public void onStart() {
if (mDevice != null) {
mDevice.onStart();
}
super.onStart();
}
@Override
public void onStop() {
if (mDevice != null) {
mDevice.onStop();
}
super.onStop();
}
@Override
public void onDestroy() {
if (mDevice != null) {
mDevice.onDestroy();
}
super.onDestroy();
}
}
/**
*/
package com.cocodin.barcodescan.plugin;
import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
public class BarcodeScanChainway extends CordovaPlugin {
private static final String TAG = "BarcodeScan";
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(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")) {
PluginHelper.scan(cordova, webView, args, callbackContext);
} else if (action.equalsIgnoreCase("getDevices")) {
PluginHelper.getDevices(cordova, webView, args, callbackContext);
}
return true;
}
}
package com.cocodin.barcodescan.plugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
/**
* Created by alberto.doval on 21/10/17.
*/
public abstract class BaseScan {
public abstract String getDeviceName();
public abstract void scan(final CordovaInterface cordova, CordovaWebView webView, JSONArray args, final CallbackContext callbackContext);
public abstract void onStart();
public abstract void onStop();
public abstract void onDestroy();
}
package com.cocodin.barcodescan.plugin.devices;
import android.content.Context;
import android.util.Log;
import com.cocodin.barcodescan.plugin.BaseScan;
import com.zebra.adc.decoder.Barcode2DWithSoft;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.cordova.PluginResult.Status;
/**
* Created by alberto.doval on 22/10/17.
*/
public class C4050 extends BaseScan {
private static final String TAG = "C4050";
private static Barcode2DWithSoft mInstance;
@Override
public String getDeviceName() {
return TAG;
}
@Override
public void scan(CordovaInterface cordova, CordovaWebView webView, JSONArray args, final CallbackContext callbackContext) {
Context context = cordova.getActivity();
try {
mInstance = Barcode2DWithSoft.getInstance();
if (mInstance != null) {
mInstance.open(context);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
callbackContext.sendPluginResult(new PluginResult(Status.ERROR, e.getMessage()));
}
mInstance.setScanCallback(new Barcode2DWithSoft.ScanCallback() {
@Override
public void onScanComplete(int i, int length, byte[] data) {
Log.i(TAG, "onScanComplete() i=" + i);
if (length < 1) {
return;
}
String barCode = new String(data);
barCode = barCode.replaceAll("\u0000", "");
Log.i(TAG, barCode);
try {
JSONObject result = new JSONObject().put("text", barCode);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
} catch (JSONException e) {
callbackContext.sendPluginResult(new PluginResult(Status.ERROR, e.getMessage()));
}
}
});
mInstance.scan();
}
@Override
public void onStart() {
}
@Override
public void onStop() {
}
@Override
public void onDestroy() {
}
}
package com.cocodin.barcodescan.plugin.devices;
import com.cocodin.barcodescan.plugin.BaseScan;
import com.phonegap.plugins.barcodescanner.BarcodeScanner;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
/**
* Created by alberto.doval on 22/10/17.
*/
public class Camera extends BaseScan {
private static final String TAG = "camera";
@Override
public String getDeviceName() {
return TAG;
}
@Override
public void scan(CordovaInterface cordova, CordovaWebView webView, JSONArray args, CallbackContext callbackContext) {
BarcodeScanner barcodeScanner = new BarcodeScanner();
barcodeScanner.cordova = cordova;
barcodeScanner.webView = webView;
barcodeScanner.execute("scan", new JSONArray(), callbackContext);
}
@Override
public void onStart() {
}
@Override
public void onStop() {
}
@Override
public void onDestroy() {
}
}
package com.cocodin.barcodescan.plugin.devices;
import android.util.Log;
import android.widget.Toast;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONObject;
import com.cocodin.barcodescan.plugin.BaseScan;
import com.honeywell.aidc.AidcManager;
import com.honeywell.aidc.AidcManager.CreatedCallback;
import com.honeywell.aidc.BarcodeFailureEvent;
import com.honeywell.aidc.BarcodeReadEvent;
import com.honeywell.aidc.BarcodeReader;
import com.honeywell.aidc.BarcodeReader.BarcodeListener;
import com.honeywell.aidc.ScannerUnavailableException;
import com.honeywell.aidc.UnsupportedPropertyException;
import java.util.HashMap;
import java.util.Map;
/**
* Created by alberto.doval on 22/10/17.
*/
public class EDA50K extends BaseScan implements BarcodeListener {
private static String TAG = EDA50K.class.getName();
private BarcodeReader barcodeReader;
private AidcManager manager;
private CallbackContext currentCallbackContext = null;
@Override
public String getDeviceName() {
return TAG;
}
@Override
public void scan(final CordovaInterface cordova, CordovaWebView webView, JSONArray args, final CallbackContext callbackContext) {
if (manager == null) {
AidcManager.create(cordova.getActivity(), new CreatedCallback() {
@Override
public void onCreated(AidcManager aidcManager) {
manager = aidcManager;
barcodeReader = manager.createBarcodeReader("dcs.scanner.ring");
if (barcodeReader != null) {
barcodeReader.addBarcodeListener(EDA50K.this);
// Map<String, Object> asdfsadf =
// barcodeReader.getAllDefaultProperties();
// for (String key : asdfsadf.keySet()) {
// Log.d("chromium", key + "=" + asdfsadf.get(key));
// }
// set the trigger mode to auto control
try {
barcodeReader.setProperty(BarcodeReader.PROPERTY_TRIGGER_CONTROL_MODE, BarcodeReader.TRIGGER_CONTROL_MODE_AUTO_CONTROL);
} catch (UnsupportedPropertyException e) {
Toast.makeText(cordova.getActivity(), "Failed to apply properties", Toast.LENGTH_SHORT).show();
}
Map<String, Object> properties = new HashMap<String, Object>();
// Set Symbologies On/Off
properties.put(BarcodeReader.PROPERTY_CODE_128_ENABLED, true);
properties.put(BarcodeReader.PROPERTY_GS1_128_ENABLED, true);
properties.put(BarcodeReader.PROPERTY_QR_CODE_ENABLED, true);
properties.put(BarcodeReader.PROPERTY_CODE_39_ENABLED, true);
properties.put(BarcodeReader.PROPERTY_DATAMATRIX_ENABLED, true);
properties.put(BarcodeReader.PROPERTY_UPC_A_ENABLE, true);
properties.put(BarcodeReader.PROPERTY_EAN_13_ENABLED, true);
properties.put(BarcodeReader.PROPERTY_AZTEC_ENABLED, false);
properties.put(BarcodeReader.PROPERTY_CODABAR_ENABLED, false);
properties.put(BarcodeReader.PROPERTY_INTERLEAVED_25_ENABLED, false);
properties.put(BarcodeReader.PROPERTY_PDF_417_ENABLED, false);
// Set Max Code 39 barcode length
properties.put(BarcodeReader.PROPERTY_CODE_39_MAXIMUM_LENGTH, 48);
// Turn on center decoding
properties.put(BarcodeReader.PROPERTY_CENTER_DECODE, true);
// Disable bad read response, handle in onFailureEvent
properties.put(BarcodeReader.PROPERTY_NOTIFICATION_BAD_READ_ENABLED, false);
properties.put(BarcodeReader.PROPERTY_DATA_PROCESSOR_LAUNCH_BROWSER, false);
// Apply the settings
barcodeReader.setProperties(properties);
//Perform first read after initialization
readBarcode(callbackContext);
} else {
callbackContext.error("Failed to open barcode reader");
}
}
});
}
else {
readBarcode(callbackContext);
}
}
@Override
public void onStart() {
}
@Override
public void onStop() {
}
private void readBarcode(CallbackContext callbackContext) {
if (barcodeReader != null) {
try {
barcodeReader.claim();
callbackContext.success();
} catch (ScannerUnavailableException e) {
e.printStackTrace();
callbackContext.error("Unable to claim reader");
}
} else {
callbackContext.error("Reader not open");
}
}
@Override
public void onDestroy() {
if (barcodeReader != null) {
// close BarcodeReader to clean up resources.
barcodeReader.close();
barcodeReader = null;
}
if (manager != null) {
// close AidcManager to disconnect from the scanner service.
// once closed, the object can no longer be used.
manager.close();
}
}
@Override
public void onBarcodeEvent(BarcodeReadEvent barcodeReadEvent) {
Log.d(TAG, barcodeReadEvent.getBarcodeData());
if (currentCallbackContext != null) {
try {
JSONObject obj = new JSONObject();
obj.put("success", true);
obj.put("data", barcodeReadEvent.getBarcodeData());
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
result.setKeepCallback(true);
currentCallbackContext.sendPluginResult(result);
} catch (Exception x) {
Log.e(TAG, x.getMessage());
}
}
}
@Override
public void onFailureEvent(BarcodeFailureEvent barcodeFailureEvent) {
if (currentCallbackContext != null) {
try {
JSONObject obj = new JSONObject();
obj.put("success", false);
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
result.setKeepCallback(true);
currentCallbackContext.sendPluginResult(result);
} catch (Exception x) {
Log.e(TAG, x.getMessage());
}
}
}
}
package com.cocodin.barcodescan.plugin.devices;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.cocodin.barcodescan.plugin.BaseScan;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
/**
* Created by alberto.doval on 22/10/17.
*/
public class NQuire300 extends BaseScan {
private static final String TAG = "NQuire300";
private boolean MQuireOn = false;
private BroadcastReceiver barcodeScannerBroadcastReceiver;
@Override
public String getDeviceName() {
return TAG;
}
@Override
public void scan(CordovaInterface cordova, CordovaWebView webView, JSONArray args, CallbackContext callbackContext) {
Context context = cordova.getActivity();
//cbContext = callbackContext;
if (!MQuireOn) {
initMquire(context, callbackContext);
}
}
@Override
public void onStart() {
}
@Override
public void onStop() {
}
@Override
public void onDestroy() {
}
private void initMquire(Context context, final CallbackContext callbackContext) {
Intent intent = new Intent("ACTION_BAR_SCANCFG");
intent.putExtra("EXTRA_SCAN_MODE", 3);
intent.putExtra("EXTRA_SCAN_AUTOENT", 0);
intent.putExtra("EXTRA_SCAN_NOTY_LED", 1);
intent.putExtra("EXTRA_SCAN_NOTY_SND", 1);
context.sendBroadcast(intent);
MQuireOn = true;
barcodeScannerBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String scanResult_1 = intent.getStringExtra("SCAN_BARCODE1");
final String scanStatus = intent.getStringExtra("SCAN_STATE");
if (null == scanResult_1 || null == scanStatus || scanResult_1.isEmpty() || scanStatus.isEmpty()) {
return;
}
if ("ok".equals(scanStatus)) {
callbackContext.success(scanResult_1);
}
}
};
context.registerReceiver(barcodeScannerBroadcastReceiver, new IntentFilter("nlscan.action.SCANNER_RESULT"));
}
}
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