Handheld.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.izouma.handheld;
  2. import android.content.Intent;
  3. import com.google.gson.Gson;
  4. import com.izouma.handheld.device.Device;
  5. import org.apache.cordova.CordovaInterface;
  6. import org.apache.cordova.CordovaPlugin;
  7. import org.apache.cordova.CallbackContext;
  8. import org.apache.cordova.CordovaWebView;
  9. import org.apache.cordova.PluginResult;
  10. import org.json.JSONArray;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13. import java.util.List;
  14. /**
  15. * This class echoes a string called from JavaScript.
  16. */
  17. public class Handheld extends CordovaPlugin {
  18. private Device device;
  19. @Override
  20. public void initialize(CordovaInterface cordova, CordovaWebView webView) {
  21. super.initialize(cordova, webView);
  22. device = DeviceFactory.getDevice(this);
  23. device.init();
  24. }
  25. @Override
  26. public void onResume(boolean multitasking) {
  27. super.onResume(multitasking);
  28. device.onResume();
  29. }
  30. @Override
  31. public void onPause(boolean multitasking) {
  32. super.onPause(multitasking);
  33. device.onPause();
  34. }
  35. @Override
  36. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  37. if (action.equals("scanCode")) {
  38. this.scanCode(callbackContext);
  39. return true;
  40. } else if (action.equals("stopScan")) {
  41. device.stopScan();
  42. callbackContext.success();
  43. return true;
  44. } else if (action.equals("readTag")) {
  45. ReadTagOptions options = null;
  46. try {
  47. JSONObject jsonObject = args.getJSONObject(0);
  48. Gson gson = new Gson();
  49. options = gson.fromJson(jsonObject.toString(), ReadTagOptions.class);
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. this.readTag(callbackContext, options);
  54. return true;
  55. } else if (action.equals("stopRead")) {
  56. device.stopRead();
  57. callbackContext.success();
  58. return true;
  59. } else if (action.equals("pause")) {
  60. device.onPause();
  61. callbackContext.success();
  62. return true;
  63. } else if (action.equals("resume")) {
  64. device.onResume();
  65. callbackContext.success();
  66. return true;
  67. }
  68. return false;
  69. }
  70. private void scanCode(final CallbackContext callbackContext) {
  71. device.scanCode(new Device.ScanCodeListener() {
  72. @Override
  73. public void onScanResult(String result) {
  74. PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
  75. pluginResult.setKeepCallback(false);
  76. callbackContext.sendPluginResult(pluginResult);
  77. }
  78. });
  79. }
  80. private void readTag(final CallbackContext callbackContext, ReadTagOptions options) {
  81. if (options == null) {
  82. options = new ReadTagOptions();
  83. }
  84. device.readTag(options, new Device.ReadTagListener() {
  85. @Override
  86. public void onReadData(TagData tagData) {
  87. Gson gson = new Gson();
  88. try {
  89. JSONObject jsonObject = new JSONObject(gson.toJson(tagData));
  90. PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject);
  91. pluginResult.setKeepCallback(false);
  92. callbackContext.sendPluginResult(pluginResult);
  93. } catch (JSONException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. @Override
  98. public void onReadData(List<TagData> list) {
  99. Gson gson = new Gson();
  100. try {
  101. JSONArray jsonArray = new JSONArray(gson.toJson(list));
  102. PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonArray);
  103. pluginResult.setKeepCallback(true);
  104. callbackContext.sendPluginResult(pluginResult);
  105. } catch (JSONException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. });
  110. }
  111. @Override
  112. public void onDestroy() {
  113. super.onDestroy();
  114. device.destroy();
  115. }
  116. @Override
  117. public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  118. super.onActivityResult(requestCode, resultCode, intent);
  119. device.onActivityResult(requestCode, resultCode, intent);
  120. }
  121. }