UsbPrinter.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.epson.mobilephone.common.usb;
  2. import android.hardware.usb.UsbDevice;
  3. import android.hardware.usb.UsbDeviceConnection;
  4. import android.hardware.usb.UsbEndpoint;
  5. import android.hardware.usb.UsbInterface;
  6. import android.hardware.usb.UsbManager;
  7. import android.os.Build;
  8. import com.evernote.edam.limits.Constants;
  9. public class UsbPrinter {
  10. private static final String TAG = "UsbPrinter";
  11. protected static final int USBLP_BULK_TIMEOUT = 5000;
  12. protected static final int USBLP_CTL_TIMEOUT = 5000;
  13. protected static final int USBLP_REQ_GET_ID = 0;
  14. protected static final int USBLP_REQ_GET_STATUS = 1;
  15. protected static final int USBLP_REQ_RESET = 2;
  16. public static final int USB_OP_FAIL = -1;
  17. public static final int USB_OP_SUCCESS = 0;
  18. private UsbEndpoint endPointIn = null;
  19. private UsbEndpoint endPointOut = null;
  20. private long startTime = 0;
  21. private UsbDevice usbDevice = null;
  22. private UsbInterface usbInterface = null;
  23. private UsbManager usbManager = null;
  24. private UsbDeviceConnection usbPrinter = null;
  25. private class RequestType {
  26. public static final int DIR_IN = 128;
  27. public static final int DIR_OUT = 0;
  28. public static final int RECIPIENT_DEVICE = 0;
  29. public static final int RECIPIENT_ENDPOINT = 2;
  30. public static final int RECIPIENT_INTERFACE = 1;
  31. public static final int RECIPIENT_OTHER = 3;
  32. public static final int TYPE_CLASS = 32;
  33. public static final int TYPE_RESERVED = 96;
  34. public static final int TYPE_STANDARD = 0;
  35. public static final int TYPE_VENDOR = 64;
  36. private RequestType() {
  37. }
  38. }
  39. public UsbPrinter(UsbManager usbManager2, UsbDevice usbDevice2, UsbInterface usbInterface2, UsbEndpoint usbEndpoint, UsbEndpoint usbEndpoint2) {
  40. usbManager = usbManager2;
  41. usbDevice = usbDevice2;
  42. usbInterface = usbInterface2;
  43. endPointOut = usbEndpoint;
  44. endPointIn = usbEndpoint2;
  45. }
  46. public UsbDevice getUsbDevice() {
  47. return usbDevice;
  48. }
  49. public UsbDeviceConnection getUsbPrinter() {
  50. return usbPrinter;
  51. }
  52. public int getDeviceNumbers() {
  53. int deviceId = getUsbDevice().getDeviceId();
  54. int i = deviceId / 1000;
  55. return (i << 16) + (deviceId - (i * 1000));
  56. }
  57. public synchronized int openPort() {
  58. try {
  59. usbPrinter = usbManager.openDevice(this.usbDevice);
  60. if (this.usbPrinter == null) {
  61. return -1;
  62. }
  63. return usbPrinter.getFileDescriptor();
  64. } catch (Exception unused) {
  65. return -1;
  66. }
  67. }
  68. public synchronized long readPort(byte[] bArr, int i) {
  69. if (this.usbPrinter == null) {
  70. return -1;
  71. }
  72. if (!this.usbPrinter.claimInterface(this.usbInterface, true)) {
  73. return -1;
  74. }
  75. setStartTime();
  76. EpLog.i(TAG, "bulkTransfer length=" + i);
  77. int bulkTransfer = usbPrinter.bulkTransfer(this.endPointIn, bArr, i, Constants.EDAM_BUSINESS_NOTEBOOKS_MAX);
  78. EpLog.i(TAG, "bulkTransfer readByte=" + bulkTransfer);
  79. if (bulkTransfer < 0 && (getElapsedTime()) >= 4500.0d) {
  80. bulkTransfer = 0;
  81. EpLog.i(TAG, "bulkTransfer timeout occurred");
  82. }
  83. usbPrinter.releaseInterface(this.usbInterface);
  84. return (long) bulkTransfer;
  85. }
  86. public synchronized long writePort(byte[] bArr, int i) {
  87. if (this.usbPrinter == null) {
  88. return -1;
  89. }
  90. if (!this.usbPrinter.claimInterface(this.usbInterface, true)) {
  91. return -1;
  92. }
  93. setStartTime();
  94. EpLog.i(TAG, "bulkTransfer length=" + i);
  95. int bulkTransfer = usbPrinter.bulkTransfer(this.endPointOut, bArr, i, Constants.EDAM_BUSINESS_NOTEBOOKS_MAX);
  96. EpLog.i(TAG, "bulkTransfer writtenByte=" + bulkTransfer);
  97. if (bulkTransfer < 0 && (getElapsedTime()) >= 4500.0d) {
  98. bulkTransfer = 0;
  99. EpLog.i(TAG, "bulkTransfer timeout occurred");
  100. }
  101. usbPrinter.releaseInterface(this.usbInterface);
  102. return (long) bulkTransfer;
  103. }
  104. private void setStartTime() {
  105. startTime = System.currentTimeMillis();
  106. }
  107. private long getElapsedTime() {
  108. return System.currentTimeMillis() - startTime;
  109. }
  110. public synchronized long getDeviceIdString(byte[] bArr, int i) {
  111. if (this.usbPrinter == null) {
  112. return -1;
  113. }
  114. if (!this.usbPrinter.claimInterface(this.usbInterface, true)) {
  115. return -1;
  116. }
  117. int controlTransfer = usbPrinter.controlTransfer(161, 0, 0, usbInterface.getId() << 8, bArr, i, Constants.EDAM_BUSINESS_NOTEBOOKS_MAX);
  118. if (controlTransfer > 0) {
  119. EpLog.i("EPSON", "DeviceIdString = " + new String(bArr, 2, controlTransfer - 2));
  120. }
  121. usbPrinter.releaseInterface(this.usbInterface);
  122. return (long) controlTransfer;
  123. }
  124. /* JADX WARNING: Code restructure failed: missing block: B:16:0x0034, code lost:
  125. return -1;
  126. */
  127. /* Code decompiled incorrectly, please refer to instructions dump. */
  128. public synchronized int softReset() {
  129. /*
  130. r10 = this;
  131. monitor-enter(r10)
  132. android.hardware.usb.UsbDeviceConnection r0 = r10.usbPrinter // Catch:{ all -> 0x0035 }
  133. r1 = -1
  134. if (r0 != 0) goto L_0x0008
  135. monitor-exit(r10)
  136. return r1
  137. L_0x0008:
  138. android.hardware.usb.UsbDeviceConnection r0 = r10.usbPrinter // Catch:{ all -> 0x0035 }
  139. android.hardware.usb.UsbInterface r2 = r10.usbInterface // Catch:{ all -> 0x0035 }
  140. r3 = 1
  141. boolean r0 = r0.claimInterface(r2, r3) // Catch:{ all -> 0x0035 }
  142. if (r0 == 0) goto L_0x0033
  143. android.hardware.usb.UsbDeviceConnection r2 = r10.usbPrinter // Catch:{ all -> 0x0035 }
  144. r3 = 35
  145. r4 = 2
  146. r5 = 0
  147. android.hardware.usb.UsbInterface r0 = r10.usbInterface // Catch:{ all -> 0x0035 }
  148. int r6 = r0.getId() // Catch:{ all -> 0x0035 }
  149. r7 = 0
  150. r8 = 0
  151. r9 = 5000(0x1388, float:7.006E-42)
  152. int r0 = r2.controlTransfer(r3, r4, r5, r6, r7, r8, r9) // Catch:{ all -> 0x0035 }
  153. android.hardware.usb.UsbDeviceConnection r2 = r10.usbPrinter // Catch:{ all -> 0x0035 }
  154. android.hardware.usb.UsbInterface r3 = r10.usbInterface // Catch:{ all -> 0x0035 }
  155. r2.releaseInterface(r3) // Catch:{ all -> 0x0035 }
  156. if (r0 < 0) goto L_0x0033
  157. r0 = 0
  158. monitor-exit(r10)
  159. return r0
  160. L_0x0033:
  161. monitor-exit(r10)
  162. return r1
  163. L_0x0035:
  164. r0 = move-exception
  165. monitor-exit(r10)
  166. throw r0
  167. */
  168. throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.usb.UsbPrinter.softReset():int");
  169. }
  170. public synchronized void closePort() {
  171. if (this.usbPrinter != null) {
  172. usbPrinter.close();
  173. usbPrinter = null;
  174. }
  175. }
  176. public String toString() {
  177. String str = "deviceName = " + usbDevice.getDeviceName() + " : endPointOut = " + Integer.toHexString(this.endPointOut.getAddress()) + " : endPointIn = " + Integer.toHexString(this.endPointIn.getAddress());
  178. if (Build.VERSION.SDK_INT < 21) {
  179. return str;
  180. }
  181. return (str + " : productName = " + usbDevice.getProductName()) + " : SerialNo = " + usbDevice.getSerialNumber();
  182. }
  183. public boolean equals(Object obj) {
  184. if (obj == null) {
  185. return false;
  186. }
  187. if (obj == this) {
  188. return true;
  189. }
  190. if ((obj instanceof UsbPrinter) && ((UsbPrinter) obj).getUsbDevice().getDeviceId() == getUsbDevice().getDeviceId()) {
  191. return true;
  192. }
  193. return false;
  194. }
  195. }