package com.epson.mobilephone.common.usb;

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Build;

import com.evernote.edam.limits.Constants;

import epson.print.Util.EPLog;

public class UsbPrinter {
    private static final String TAG = "UsbPrinter";
    protected static final int USBLP_BULK_TIMEOUT = 5000;
    protected static final int USBLP_CTL_TIMEOUT = 5000;
    protected static final int USBLP_REQ_GET_ID = 0;
    protected static final int USBLP_REQ_GET_STATUS = 1;
    protected static final int USBLP_REQ_RESET = 2;
    public static final int USB_OP_FAIL = -1;
    public static final int USB_OP_SUCCESS = 0;
    private UsbEndpoint endPointIn = null;
    private UsbEndpoint endPointOut = null;
    private long startTime = 0;
    private UsbDevice usbDevice = null;
    private UsbInterface usbInterface = null;
    private UsbManager usbManager = null;
    private UsbDeviceConnection usbPrinter = null;

    private class RequestType {
        public static final int DIR_IN = 128;
        public static final int DIR_OUT = 0;
        public static final int RECIPIENT_DEVICE = 0;
        public static final int RECIPIENT_ENDPOINT = 2;
        public static final int RECIPIENT_INTERFACE = 1;
        public static final int RECIPIENT_OTHER = 3;
        public static final int TYPE_CLASS = 32;
        public static final int TYPE_RESERVED = 96;
        public static final int TYPE_STANDARD = 0;
        public static final int TYPE_VENDOR = 64;

        private RequestType() {
        }
    }

    public UsbPrinter(UsbManager usbManager2, UsbDevice usbDevice2, UsbInterface usbInterface2, UsbEndpoint usbEndpoint, UsbEndpoint usbEndpoint2) {
        usbManager = usbManager2;
        usbDevice = usbDevice2;
        usbInterface = usbInterface2;
        endPointOut = usbEndpoint;
        endPointIn = usbEndpoint2;
    }

    public UsbDevice getUsbDevice() {
        return usbDevice;
    }

    public UsbDeviceConnection getUsbPrinter() {
        return usbPrinter;
    }

    public int getDeviceNumbers() {
        int deviceId = getUsbDevice().getDeviceId();
        int i = deviceId / 1000;
        return (i << 16) + (deviceId - (i * 1000));
    }

    public synchronized int openPort() {
        try {
            usbPrinter = usbManager.openDevice(usbDevice);
            if (usbPrinter == null) {
                return -1;
            }
            return usbPrinter.getFileDescriptor();
        } catch (Exception unused) {
            return -1;
        }
    }

    public synchronized long readPort(byte[] bArr, int i) {
        if (usbPrinter == null) {
            return -1;
        }
        if (!this.usbPrinter.claimInterface(usbInterface, true)) {
            return -1;
        }
        setStartTime();
        EPLog.i(TAG, "bulkTransfer length=" + i);
        int bulkTransfer = usbPrinter.bulkTransfer(endPointIn, bArr, i, Constants.EDAM_BUSINESS_NOTEBOOKS_MAX);
        EPLog.i(TAG, "bulkTransfer readByte=" + bulkTransfer);
        if (bulkTransfer < 0 && (getElapsedTime()) >= 4500.0d) {
            bulkTransfer = 0;
            EPLog.i(TAG, "bulkTransfer timeout occurred");
        }
        usbPrinter.releaseInterface(usbInterface);
        return (long) bulkTransfer;
    }

    public synchronized long writePort(byte[] bArr, int i) {
        if (usbPrinter == null) {
            return -1;
        }
        if (!this.usbPrinter.claimInterface(usbInterface, true)) {
            return -1;
        }
        setStartTime();
        EPLog.i(TAG, "bulkTransfer length=" + i);
        int bulkTransfer = usbPrinter.bulkTransfer(endPointOut, bArr, i, Constants.EDAM_BUSINESS_NOTEBOOKS_MAX);
        EPLog.i(TAG, "bulkTransfer writtenByte=" + bulkTransfer);
        if (bulkTransfer < 0 && (getElapsedTime()) >= 4500.0d) {
            bulkTransfer = 0;
            EPLog.i(TAG, "bulkTransfer timeout occurred");
        }
        usbPrinter.releaseInterface(usbInterface);
        return (long) bulkTransfer;
    }

    private void setStartTime() {
        startTime = System.currentTimeMillis();
    }

    private long getElapsedTime() {
        return System.currentTimeMillis() - startTime;
    }

    public synchronized long getDeviceIdString(byte[] bArr, int i) {
        if (usbPrinter == null) {
            return -1;
        }
        if (!this.usbPrinter.claimInterface(usbInterface, true)) {
            return -1;
        }
        int controlTransfer = usbPrinter.controlTransfer(161, 0, 0, usbInterface.getId() << 8, bArr, i, Constants.EDAM_BUSINESS_NOTEBOOKS_MAX);
        if (controlTransfer > 0) {
            EPLog.i("EPSON", "DeviceIdString = " + new String(bArr, 2, controlTransfer - 2));
        }
        usbPrinter.releaseInterface(usbInterface);
        return (long) controlTransfer;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:16:0x0034, code lost:
        return -1;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized int softReset() {
        /*
            r10 = this;
            monitor-enter(r10)
            android.hardware.usb.UsbDeviceConnection r0 = r10.usbPrinter     // Catch:{ all -> 0x0035 }
            r1 = -1
            if (r0 != 0) goto L_0x0008
            monitor-exit(r10)
            return r1
        L_0x0008:
            android.hardware.usb.UsbDeviceConnection r0 = r10.usbPrinter     // Catch:{ all -> 0x0035 }
            android.hardware.usb.UsbInterface r2 = r10.usbInterface     // Catch:{ all -> 0x0035 }
            r3 = 1
            boolean r0 = r0.claimInterface(r2, r3)     // Catch:{ all -> 0x0035 }
            if (r0 == 0) goto L_0x0033
            android.hardware.usb.UsbDeviceConnection r2 = r10.usbPrinter     // Catch:{ all -> 0x0035 }
            r3 = 35
            r4 = 2
            r5 = 0
            android.hardware.usb.UsbInterface r0 = r10.usbInterface     // Catch:{ all -> 0x0035 }
            int r6 = r0.getId()     // Catch:{ all -> 0x0035 }
            r7 = 0
            r8 = 0
            r9 = 5000(0x1388, float:7.006E-42)
            int r0 = r2.controlTransfer(r3, r4, r5, r6, r7, r8, r9)     // Catch:{ all -> 0x0035 }
            android.hardware.usb.UsbDeviceConnection r2 = r10.usbPrinter     // Catch:{ all -> 0x0035 }
            android.hardware.usb.UsbInterface r3 = r10.usbInterface     // Catch:{ all -> 0x0035 }
            r2.releaseInterface(r3)     // Catch:{ all -> 0x0035 }
            if (r0 < 0) goto L_0x0033
            r0 = 0
            monitor-exit(r10)
            return r0
        L_0x0033:
            monitor-exit(r10)
            return r1
        L_0x0035:
            r0 = move-exception
            monitor-exit(r10)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.usb.UsbPrinter.softReset():int");
    }

    public synchronized void closePort() {
        if (usbPrinter != null) {
            usbPrinter.close();
            usbPrinter = null;
        }
    }

    public String toString() {
        String str = "deviceName = " + usbDevice.getDeviceName() + " : endPointOut = " + Integer.toHexString(endPointOut.getAddress()) + " : endPointIn = " + Integer.toHexString(endPointIn.getAddress());
        if (Build.VERSION.SDK_INT < 21) {
            return str;
        }
        return (str + " : productName = " + usbDevice.getProductName()) + " : SerialNo = " + usbDevice.getSerialNumber();
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if ((obj instanceof UsbPrinter) && ((UsbPrinter) obj).getUsbDevice().getDeviceId() == getUsbDevice().getDeviceId()) {
            return true;
        }
        return false;
    }
}
