Handheld.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.izouma.handheld;
  2. import android.Manifest;
  3. import android.app.AlertDialog;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.net.Uri;
  8. import android.os.Build;
  9. import android.support.v4.app.ActivityCompat;
  10. import com.google.gson.Gson;
  11. import com.izouma.handheld.device.Device;
  12. import org.apache.cordova.CordovaInterface;
  13. import org.apache.cordova.CordovaPlugin;
  14. import org.apache.cordova.CallbackContext;
  15. import org.apache.cordova.CordovaWebView;
  16. import org.apache.cordova.PluginResult;
  17. import org.json.JSONArray;
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20. import java.util.List;
  21. /**
  22. * This class echoes a string called from JavaScript.
  23. */
  24. public class Handheld extends CordovaPlugin {
  25. private Device device;
  26. private CallbackContext callbackContext;
  27. @Override
  28. public void initialize(CordovaInterface cordova, CordovaWebView webView) {
  29. super.initialize(cordova, webView);
  30. device = DeviceFactory.getDevice(this);
  31. device.init();
  32. }
  33. @Override
  34. public void onResume(boolean multitasking) {
  35. super.onResume(multitasking);
  36. device.onResume();
  37. }
  38. @Override
  39. public void onPause(boolean multitasking) {
  40. super.onPause(multitasking);
  41. device.onPause();
  42. }
  43. @Override
  44. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  45. if (action.equals("scanCode")) {
  46. this.scanCode(callbackContext);
  47. return true;
  48. } else if (action.equals("stopScan")) {
  49. device.stopScan();
  50. callbackContext.success();
  51. return true;
  52. } else if (action.equals("readTag")) {
  53. ReadTagOptions options = null;
  54. try {
  55. JSONObject jsonObject = args.getJSONObject(0);
  56. Gson gson = new Gson();
  57. options = gson.fromJson(jsonObject.toString(), ReadTagOptions.class);
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. this.readTag(callbackContext, options);
  62. return true;
  63. } else if (action.equals("stopRead")) {
  64. device.stopRead();
  65. callbackContext.success();
  66. return true;
  67. } else if (action.equals("pause")) {
  68. device.onPause();
  69. callbackContext.success();
  70. return true;
  71. } else if (action.equals("resume")) {
  72. device.onResume();
  73. callbackContext.success();
  74. return true;
  75. }
  76. return false;
  77. }
  78. private void scanCode(final CallbackContext callbackContext) {
  79. this.callbackContext = callbackContext;
  80. device.scanCode(new Device.ScanCodeListener() {
  81. @Override
  82. public void onScanResult(String result) {
  83. PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
  84. pluginResult.setKeepCallback(false);
  85. callbackContext.sendPluginResult(pluginResult);
  86. }
  87. });
  88. }
  89. private void readTag(final CallbackContext callbackContext, ReadTagOptions options) {
  90. if (options == null) {
  91. options = new ReadTagOptions();
  92. }
  93. device.readTag(options, new Device.ReadTagListener() {
  94. @Override
  95. public void onReadData(TagData tagData) {
  96. Gson gson = new Gson();
  97. try {
  98. JSONObject jsonObject = new JSONObject(gson.toJson(tagData));
  99. PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject);
  100. pluginResult.setKeepCallback(false);
  101. callbackContext.sendPluginResult(pluginResult);
  102. } catch (JSONException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. @Override
  107. public void onReadData(List<TagData> list) {
  108. Gson gson = new Gson();
  109. try {
  110. JSONArray jsonArray = new JSONArray(gson.toJson(list));
  111. PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonArray);
  112. pluginResult.setKeepCallback(true);
  113. callbackContext.sendPluginResult(pluginResult);
  114. } catch (JSONException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. });
  119. }
  120. @Override
  121. public void onDestroy() {
  122. super.onDestroy();
  123. device.destroy();
  124. }
  125. @Override
  126. public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  127. super.onActivityResult(requestCode, resultCode, intent);
  128. device.onActivityResult(requestCode, resultCode, intent);
  129. }
  130. @Override
  131. public void requestPermissions(int requestCode) {
  132. super.requestPermissions(requestCode);
  133. cordova.requestPermissions(this, requestCode, new String[]{Manifest.permission.CAMERA});
  134. }
  135. @Override
  136. public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
  137. super.onRequestPermissionResult(requestCode, permissions, grantResults);
  138. if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  139. device.scanCode(new Device.ScanCodeListener() {
  140. @Override
  141. public void onScanResult(String result) {
  142. PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
  143. pluginResult.setKeepCallback(false);
  144. callbackContext.sendPluginResult(pluginResult);
  145. }
  146. });
  147. } else {
  148. new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
  149. .setTitle("提示")
  150. .setMessage("需要相机权限")
  151. .setPositiveButton("打开设置", new DialogInterface.OnClickListener() {
  152. @Override
  153. public void onClick(DialogInterface dialog, int which) {
  154. dialog.dismiss();
  155. Intent localIntent = new Intent();
  156. localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  157. localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
  158. localIntent.setData(Uri.fromParts("package", cordova.getActivity().getPackageName(), null));
  159. cordova.getActivity().startActivity(localIntent);
  160. }
  161. })
  162. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  163. @Override
  164. public void onClick(DialogInterface dialog, int which) {
  165. dialog.dismiss();
  166. }
  167. })
  168. .create()
  169. .show();
  170. }
  171. }
  172. }