| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package com.izouma.handheld;
- import android.content.Intent;
- import com.google.gson.Gson;
- import com.izouma.handheld.device.Device;
- import org.apache.cordova.CordovaInterface;
- import org.apache.cordova.CordovaPlugin;
- import org.apache.cordova.CallbackContext;
- import org.apache.cordova.CordovaWebView;
- import org.apache.cordova.PluginResult;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.List;
- /**
- * This class echoes a string called from JavaScript.
- */
- public class Handheld extends CordovaPlugin {
- private Device device;
- @Override
- public void initialize(CordovaInterface cordova, CordovaWebView webView) {
- super.initialize(cordova, webView);
- device = DeviceFactory.getDevice(this);
- device.init();
- }
- @Override
- public void onResume(boolean multitasking) {
- super.onResume(multitasking);
- device.onResume();
- }
- @Override
- public void onPause(boolean multitasking) {
- super.onPause(multitasking);
- device.onPause();
- }
- @Override
- public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
- if (action.equals("scanCode")) {
- this.scanCode(callbackContext);
- return true;
- } else if (action.equals("stopScan")) {
- device.stopScan();
- callbackContext.success();
- return true;
- } else if (action.equals("readTag")) {
- ReadTagOptions options = null;
- try {
- JSONObject jsonObject = args.getJSONObject(0);
- Gson gson = new Gson();
- options = gson.fromJson(jsonObject.toString(), ReadTagOptions.class);
- } catch (Exception e) {
- e.printStackTrace();
- }
- this.readTag(callbackContext, options);
- return true;
- } else if (action.equals("stopRead")) {
- device.stopRead();
- callbackContext.success();
- return true;
- } else if (action.equals("pause")) {
- device.onPause();
- callbackContext.success();
- return true;
- } else if (action.equals("resume")) {
- device.onResume();
- callbackContext.success();
- return true;
- }
- return false;
- }
- private void scanCode(final CallbackContext callbackContext) {
- device.scanCode(new Device.ScanCodeListener() {
- @Override
- public void onScanResult(String result) {
- PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
- pluginResult.setKeepCallback(false);
- callbackContext.sendPluginResult(pluginResult);
- }
- });
- }
- private void readTag(final CallbackContext callbackContext, ReadTagOptions options) {
- if (options == null) {
- options = new ReadTagOptions();
- }
- device.readTag(options, new Device.ReadTagListener() {
- @Override
- public void onReadData(TagData tagData) {
- Gson gson = new Gson();
- try {
- JSONObject jsonObject = new JSONObject(gson.toJson(tagData));
- PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject);
- pluginResult.setKeepCallback(false);
- callbackContext.sendPluginResult(pluginResult);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onReadData(List<TagData> list) {
- Gson gson = new Gson();
- try {
- JSONArray jsonArray = new JSONArray(gson.toJson(list));
- PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonArray);
- pluginResult.setKeepCallback(true);
- callbackContext.sendPluginResult(pluginResult);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- });
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- device.destroy();
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent intent) {
- super.onActivityResult(requestCode, resultCode, intent);
- device.onActivityResult(requestCode, resultCode, intent);
- }
- }
|