package com.epson.mobilephone.common.usb;

import android.content.Context;
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 com.epson.mobilephone.common.EpLog;
import java.util.ArrayList;
import java.util.HashMap;

public class UsbPrintDriver {
    private static final String TAG = "UsbPrintDriver";
    private static UsbPrintDriver instance;
    private Context context;
    private UsbManager usbManager;
    private ArrayList<UsbPrinter> usbPrintersPemmited = null;

    private UsbPrintDriver(Context context2) {
        usbManager = (UsbManager) context2.getSystemService("usb");
        context = context2;
    }

    public static synchronized UsbPrintDriver getInstance(Context context2) {
        UsbPrintDriver usbPrintDriver;
        synchronized (UsbPrintDriver.class) {
            if (instance == null) {
                instance = new UsbPrintDriver(context2);
            }
            usbPrintDriver = instance;
        }
        return usbPrintDriver;
    }

    public UsbManager getUsbManager() {
        return usbManager;
    }

    public boolean isExistPermitedDevice() {
        synchronized (this) {
            return findDevice(0, new int[3]) != -1;
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:18:0x0058, code lost:
        return;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void deletePrinterPermmited(android.hardware.usb.UsbDevice r5) {
        /*
            r4 = this;
            monitor-enter(r4)
            if (r5 != 0) goto L_0x0005
            monitor-exit(r4)     // Catch:{ all -> 0x0059 }
            return
        L_0x0005:
            java.util.ArrayList<com.epson.mobilephone.common.usb.UsbPrinter> r0 = r4.usbPrintersPemmited     // Catch:{ all -> 0x0059 }
            if (r0 != 0) goto L_0x000b
            monitor-exit(r4)     // Catch:{ all -> 0x0059 }
            return
        L_0x000b:
            java.util.ArrayList<com.epson.mobilephone.common.usb.UsbPrinter> r0 = r4.usbPrintersPemmited     // Catch:{ all -> 0x0059 }
            java.util.Iterator r0 = r0.iterator()     // Catch:{ all -> 0x0059 }
        L_0x0011:
            boolean r1 = r0.hasNext()     // Catch:{ all -> 0x0059 }
            if (r1 == 0) goto L_0x0057
            java.lang.Object r1 = r0.next()     // Catch:{ all -> 0x0059 }
            com.epson.mobilephone.common.usb.UsbPrinter r1 = (com.epson.mobilephone.common.usb.UsbPrinter) r1     // Catch:{ all -> 0x0059 }
            android.hardware.usb.UsbDevice r2 = r1.getUsbDevice()     // Catch:{ all -> 0x0059 }
            int r2 = r2.getDeviceId()     // Catch:{ all -> 0x0059 }
            int r3 = r5.getDeviceId()     // Catch:{ all -> 0x0059 }
            if (r2 != r3) goto L_0x0011
            java.lang.String r5 = "Tag"
            java.lang.StringBuilder r0 = new java.lang.StringBuilder     // Catch:{ all -> 0x0059 }
            r0.<init>()     // Catch:{ all -> 0x0059 }
            java.lang.String r2 = "EPSON delete usbPrinter = "
            r0.append(r2)     // Catch:{ all -> 0x0059 }
            java.lang.String r2 = r1.toString()     // Catch:{ all -> 0x0059 }
            r0.append(r2)     // Catch:{ all -> 0x0059 }
            java.lang.String r0 = r0.toString()     // Catch:{ all -> 0x0059 }
            com.epson.mobilephone.common.EpLog.m78i(r5, r0)     // Catch:{ all -> 0x0059 }
            r1.closePort()     // Catch:{ all -> 0x0059 }
            java.util.ArrayList<com.epson.mobilephone.common.usb.UsbPrinter> r5 = r4.usbPrintersPemmited     // Catch:{ all -> 0x0059 }
            boolean r5 = r5.remove(r1)     // Catch:{ all -> 0x0059 }
            if (r5 == 0) goto L_0x0057
            java.lang.String r5 = "Tag"
            java.lang.String r0 = "EPSON delete success"
            com.epson.mobilephone.common.EpLog.m78i(r5, r0)     // Catch:{ all -> 0x0059 }
        L_0x0057:
            monitor-exit(r4)     // Catch:{ all -> 0x0059 }
            return
        L_0x0059:
            r5 = move-exception
            monitor-exit(r4)     // Catch:{ all -> 0x0059 }
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.usb.UsbPrintDriver.deletePrinterPermmited(android.hardware.usb.UsbDevice):void");
    }

    public synchronized ArrayList<UsbPrinter> findPrinters(boolean z, int i) {
        ArrayList<UsbPrinter> arrayList;
        synchronized (this) {
            arrayList = new ArrayList<>();
            HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
            for (String str : deviceList.keySet()) {
                UsbDevice usbDevice = deviceList.get(str);
                if (z) {
                    if (!this.usbManager.hasPermission(usbDevice)) {
                        int i2 = i;
                    }
                }
                if (usbDevice.getVendorId() == i) {
                    int interfaceCount = usbDevice.getInterfaceCount();
                    for (int i3 = 0; i3 < interfaceCount; i3++) {
                        UsbInterface usbInterface = usbDevice.getInterface(i3);
                        if (7 == usbInterface.getInterfaceClass() && 1 == usbInterface.getInterfaceSubclass()) {
                            int i4 = 2;
                            if (2 == usbInterface.getInterfaceProtocol()) {
                                int endpointCount = usbInterface.getEndpointCount();
                                UsbEndpoint usbEndpoint = null;
                                UsbEndpoint usbEndpoint2 = null;
                                int i5 = 0;
                                while (i5 < endpointCount) {
                                    UsbEndpoint endpoint = usbInterface.getEndpoint(i5);
                                    if (usbEndpoint == null && i4 == endpoint.getType() && endpoint.getDirection() == 0) {
                                        usbEndpoint = endpoint;
                                    } else if (usbEndpoint2 == null && i4 == endpoint.getType() && 128 == endpoint.getDirection()) {
                                        usbEndpoint2 = endpoint;
                                    }
                                    i5++;
                                    i4 = 2;
                                }
                                if (!(usbEndpoint == null || usbEndpoint2 == null)) {
                                    UsbPrinter usbPrinter = new UsbPrinter(this.usbManager, usbDevice, usbInterface, usbEndpoint, usbEndpoint2);
                                    EpLog.m78i("EPSON", "Found Printer " + usbPrinter.toString());
                                    arrayList.add(usbPrinter);
                                }
                            }
                        }
                    }
                }
            }
        }
        return arrayList;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:17:0x004c, code lost:
        return -1;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public int findDevice(int r5, int[] r6) {
        /*
            r4 = this;
            java.lang.String r0 = "UsbPrintDriver"
            java.lang.String r1 = "Call findDevice"
            com.epson.mobilephone.common.EpLog.m78i(r0, r1)
            monitor-enter(r4)
            r0 = 1
            if (r5 != 0) goto L_0x0016
            int r1 = com.epson.mobilephone.common.PrintingLib.define.Constants.EPSON_VENDERID     // Catch:{ all -> 0x0014 }
            java.util.ArrayList r1 = r4.findPrinters(r0, r1)     // Catch:{ all -> 0x0014 }
            r4.usbPrintersPemmited = r1     // Catch:{ all -> 0x0014 }
            goto L_0x0016
        L_0x0014:
            r5 = move-exception
            goto L_0x004e
        L_0x0016:
            if (r5 < 0) goto L_0x004b
            java.util.ArrayList<com.epson.mobilephone.common.usb.UsbPrinter> r1 = r4.usbPrintersPemmited     // Catch:{ all -> 0x0014 }
            if (r1 == 0) goto L_0x004b
            java.util.ArrayList<com.epson.mobilephone.common.usb.UsbPrinter> r1 = r4.usbPrintersPemmited     // Catch:{ all -> 0x0014 }
            int r1 = r1.size()     // Catch:{ all -> 0x0014 }
            if (r5 >= r1) goto L_0x004b
            java.util.ArrayList<com.epson.mobilephone.common.usb.UsbPrinter> r1 = r4.usbPrintersPemmited     // Catch:{ all -> 0x0014 }
            java.lang.Object r1 = r1.get(r5)     // Catch:{ all -> 0x0014 }
            com.epson.mobilephone.common.usb.UsbPrinter r1 = (com.epson.mobilephone.common.usb.UsbPrinter) r1     // Catch:{ all -> 0x0014 }
            r2 = 0
            android.hardware.usb.UsbDevice r3 = r1.getUsbDevice()     // Catch:{ all -> 0x0014 }
            int r3 = r3.getVendorId()     // Catch:{ all -> 0x0014 }
            r6[r2] = r3     // Catch:{ all -> 0x0014 }
            android.hardware.usb.UsbDevice r2 = r1.getUsbDevice()     // Catch:{ all -> 0x0014 }
            int r2 = r2.getProductId()     // Catch:{ all -> 0x0014 }
            r6[r0] = r2     // Catch:{ all -> 0x0014 }
            r2 = 2
            int r1 = r1.getDeviceNumbers()     // Catch:{ all -> 0x0014 }
            r6[r2] = r1     // Catch:{ all -> 0x0014 }
            int r5 = r5 + r0
            monitor-exit(r4)     // Catch:{ all -> 0x0014 }
            return r5
        L_0x004b:
            monitor-exit(r4)     // Catch:{ all -> 0x0014 }
            r5 = -1
            return r5
        L_0x004e:
            monitor-exit(r4)     // Catch:{ all -> 0x0014 }
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.usb.UsbPrintDriver.findDevice(int, int[]):int");
    }

    public int openPort(int[] iArr) {
        EpLog.m78i(TAG, "Call openPort");
        synchronized (this) {
            for (int i = 0; i < usbPrintersPemmited.size(); i++) {
                UsbPrinter usbPrinter = usbPrintersPemmited.get(i);
                if (iArr[0] == usbPrinter.getUsbDevice().getVendorId() && iArr[1] == usbPrinter.getUsbDevice().getProductId() && iArr[2] == usbPrinter.getDeviceNumbers()) {
                    int openPort = usbPrinter.openPort();
                    return openPort;
                }
            }
            return -1;
        }
    }

    public UsbPrinter findPrinterOpened(int i) {
        synchronized (this) {
            for (int i2 = 0; i2 < usbPrintersPemmited.size(); i2++) {
                UsbPrinter usbPrinter = usbPrintersPemmited.get(i2);
                UsbDeviceConnection usbPrinter2 = usbPrinter.getUsbPrinter();
                if (usbPrinter2 != null) {
                    if (i == usbPrinter2.getFileDescriptor()) {
                        return usbPrinter;
                    }
                }
            }
            return null;
        }
    }

    public long readPort(int i, byte[] bArr, int i2) {
        EpLog.m78i(TAG, "Call readPort");
        synchronized (this) {
            UsbPrinter findPrinterOpened = findPrinterOpened(i);
            if (findPrinterOpened == null) {
                return -1;
            }
            long readPort = findPrinterOpened.readPort(bArr, i2);
            return readPort;
        }
    }

    public long writePort(int i, byte[] bArr, int i2) {
        EpLog.m78i(TAG, "Call writePort");
        synchronized (this) {
            UsbPrinter findPrinterOpened = findPrinterOpened(i);
            if (findPrinterOpened == null) {
                return -1;
            }
            long writePort = findPrinterOpened.writePort(bArr, i2);
            return writePort;
        }
    }

    public long getDeviceIdString(int i, byte[] bArr, int i2) {
        EpLog.m78i(TAG, "Call getDeviceIdString");
        synchronized (this) {
            UsbPrinter findPrinterOpened = findPrinterOpened(i);
            if (findPrinterOpened == null) {
                return -1;
            }
            long deviceIdString = findPrinterOpened.getDeviceIdString(bArr, i2);
            return deviceIdString;
        }
    }

    public int softReset(int i) {
        EpLog.m78i(TAG, "Call softReset");
        synchronized (this) {
            UsbPrinter findPrinterOpened = findPrinterOpened(i);
            if (findPrinterOpened == null) {
                return -1;
            }
            int softReset = findPrinterOpened.softReset();
            return softReset;
        }
    }

    public void closePort(int i) {
        EpLog.m78i(TAG, "Call closePort");
        synchronized (this) {
            UsbPrinter findPrinterOpened = findPrinterOpened(i);
            if (findPrinterOpened != null) {
                findPrinterOpened.closePort();
            }
        }
    }
}
