UsbPrinter.java 7.8 KB

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