package com.epson.lib.escani2;

import com.epson.lib.escani2.EscanI2NativeInterface;
import com.epson.lib.escani2.ScanSize;

import java.util.ArrayList;
import java.util.Locale;

public class EscanI2Lib {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    public static final int ERROR_CODE_COMM_ERROR = -214;
    public static final int ERROR_CODE_COVER_OPEN = -215;
    public static final int ERROR_CODE_DOUBLE_FEED = -211;
    public static final int ERROR_CODE_ESCANI2_ERROR = -1000;
    public static final int ERROR_CODE_FILE_WRITE_ERROR = -202;
    public static final int ERROR_CODE_GENERAL_ERROR = -200;
    public static final int ERROR_CODE_INTERNAL_ERROR = -100;
    public static final int ERROR_CODE_JOB_CANCELED = 1;
    public static final int ERROR_CODE_MEMORY_ALLOCATE_ERROR = -201;
    public static final int ERROR_CODE_NO_ERROR = 0;
    public static final int ERROR_CODE_PAPER_EMPTY = -210;
    public static final int ERROR_CODE_PAPER_JAM = -212;
    public static final int ERROR_CODE_SCANNER_OCCUPIED = -213;
    private static final String LOG_TAG = "EscanI2Lib";
    static EscanI2NativeInterface sEscanI2NativeInterface;
    static EscanI2Lib sInstance;
    int mDuplex;
    private boolean mLibraryInitialized;
    int mMaxScanSheets;
    Observer mObserver;
    final Object mObserverLock = new Object();
    private boolean mScannerLocked;
    private boolean mSetPaperSize;
    private boolean mSetScannerDone;
    volatile boolean mWaitStartRequest;
    int marg_jpeg_quality;
    int marg_over_scan;
    int marg_threshold;
    int mautoCtop;
    int mautoSkew;
    int mbufferSize;
    ColorMode mcolorMode;
    DoubleFeedLevel mdoubleFeedLevel;
    int mheight;
    ImageFormat mimageFormat;
    InputUnit minputSource;
    int mlookupTableNo;
    int moffsetX;
    int moffsetY;
    int mresolutionMain;
    int mresolutionSub;
    Gamma muserGamma;
    int mwidth;

    public enum AutoCrop {
        FALSE,
        TRUE
    }

    public enum AutoSkew {
        FALSE,
        TRUE
    }

    public enum ColorMode {
        MONO_1BIT,
        GRAYSCALE_8BIT,
        COLOR_24BIT
    }

    public enum Gamma {
        GAMMA_100,
        GAMMA_180
    }

    public enum ImageFormat {
        JPG,
        RAW
    }

    public enum InputUnit {
        FLATBED,
        ADF,
        TRANSPARENT_UNIT
    }

    public interface Observer {
        void update(int i, int i2);
    }

    public enum PagerGuidPosition {
        CENTER,
        UPPER_LEFT
    }

    private boolean checkResolution(int i, int i2) {
        return i != 300 ? i == 600 && i2 == 600 : i2 == 300;
    }

    private boolean isDeviceChige() {
        return false;
    }

    private int trunsErrorCode(int i, boolean z) {
        if (i == -5007) {
            return -211;
        }
        if (i == -1501) {
            return -213;
        }
        if (i == -1114 || i == -1100) {
            return -214;
        }
        switch (i) {
            case -5003:
                return z ? -210 : 0;
            case -5002:
                return -212;
            case -5001:
                return -215;
            default:
                switch (i) {
                    case -102:
                        return -202;
                    case -101:
                        return -201;
                    default:
                        return i;
                }
        }
    }

    public enum DoubleFeedLevel {
        LEVEL_NONE(0),
        LEVEL_1(1),
        LEVEL_2(2),
        LEVEL_UNDEFINED(255);

        private int mValue;

        private DoubleFeedLevel(int i) {
            mValue = i;
        }

        public int getValue() {
            return mValue;
        }
    }

    private EscanI2Lib() {
    }

    public static synchronized EscanI2Lib getInstance() {
        EscanI2Lib escanI2Lib;
        synchronized (EscanI2Lib.class) {
            if (sEscanI2NativeInterface == null) {
                sEscanI2NativeInterface = new EscanI2NativeIf();
            }
            if (sInstance == null) {
                sInstance = new EscanI2Lib();
            }
            escanI2Lib = sInstance;
        }
        return escanI2Lib;
    }

    public static synchronized EscanI2Lib getInstance(EscanI2NativeInterface escanI2NativeInterface) {
        EscanI2Lib escanI2Lib;
        synchronized (EscanI2Lib.class) {
            sEscanI2NativeInterface = escanI2NativeInterface;
            if (sInstance == null) {
                sInstance = new EscanI2Lib();
            }
            escanI2Lib = sInstance;
        }
        return escanI2Lib;
    }

    public synchronized int initializeLibrary() {
        int initializeLibraryJni;
        if (!mLibraryInitialized) {
            mLibraryInitialized = false;
            mSetScannerDone = false;
            mScannerLocked = false;
            mSetPaperSize = false;
            initializeLibraryJni = sEscanI2NativeInterface.initializeLibraryJni();
            if (initializeLibraryJni == 0) {
                mLibraryInitialized = true;
            }
            sEscanI2NativeInterface.setPageWriteCallback(new EscanI2NativeInterface.PageWriteCallback() {
                public void pageWriteDone(int i, int i2) {
                    EscanI2Lib.this.updatePages(i, i2);
                }
            });
        } else {
            throw new RuntimeException("initializeLibrary() has already called.");
        }
        return initializeLibraryJni;
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(8:10|11|12|13|22|21|8|7) */
    /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x0008 */
    /* JADX WARNING: Removed duplicated region for block: B:7:0x0008 A[LOOP:0: B:7:0x0008->B:21:0x0008, LOOP_START, SYNTHETIC, Splitter:B:7:0x0008] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized int releaseLibrary() {
        /*
            r4 = this;
            monitor-enter(r4)
            boolean r0 = r4.mLibraryInitialized     // Catch:{ all -> 0x002b }
            r1 = 0
            if (r0 != 0) goto L_0x0008
            monitor-exit(r4)
            return r1
        L_0x0008:
            boolean r0 = r4.mWaitStartRequest     // Catch:{ all -> 0x002b }
            if (r0 == 0) goto L_0x0015
            r4.cancelWaiting()     // Catch:{ all -> 0x002b }
            r2 = 100
            java.lang.Thread.sleep(r2)     // Catch:{ InterruptedException -> 0x0008 }
            goto L_0x0008
        L_0x0015:
            r4.releaseScanner()     // Catch:{ all -> 0x002b }
            r4.mLibraryInitialized = r1     // Catch:{ all -> 0x002b }
            r4.mSetScannerDone = r1     // Catch:{ all -> 0x002b }
            r4.mScannerLocked = r1     // Catch:{ all -> 0x002b }
            r4.mSetPaperSize = r1     // Catch:{ all -> 0x002b }
            r0 = 0
            r4.mObserver = r0     // Catch:{ all -> 0x002b }
            com.epson.lib.escani2.EscanI2NativeInterface r0 = sEscanI2NativeInterface     // Catch:{ all -> 0x002b }
            int r0 = r0.releaseLibraryJni()     // Catch:{ all -> 0x002b }
            monitor-exit(r4)
            return r0
        L_0x002b:
            r0 = move-exception
            monitor-exit(r4)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.lib.escani2.EscanI2Lib.releaseLibrary():int");
    }

    public int NOT_setScanner(String str) {
        return setScanner(str);
    }

    public synchronized int setScanner(String str) {
        if (mLibraryInitialized) {
            mSetScannerDone = true;
        } else {
            throw new RuntimeException("initializeLibrary() has not been called.");
        }
        return trunsErrorCode(sEscanI2NativeInterface.setScannerJni(str), false);
    }

    public int waitStartRequest() {
        mWaitStartRequest = true;
        int waitStartRequest = sEscanI2NativeInterface.waitStartRequest();
        mWaitStartRequest = false;
        return trunsErrorCode(waitStartRequest, false);
    }

    public int cancelWaiting() {
        sEscanI2NativeInterface.cancelWaitingJni();
        return 0;
    }

    public synchronized int setPaperSize(ScanSize.PaperSize paperSize, int i, int i2, boolean z) {
        if (!mSetScannerDone) {
            throw new RuntimeException("setScanner() has not been called.");
        } else if (checkResolution(i, i2)) {
            int[] iArr = ScanSize.get300DpiPixelFromPaperSize(paperSize);
            int i3 = 2550;
            if (i == 600) {
                iArr[0] = iArr[0] * 2;
                iArr[1] = iArr[1] * 2;
                i3 = 5100;
            }
            if (!z) {
                if (!isDeviceChige()) {
                    moffsetX = (i3 - iArr[0]) / 2;
                    moffsetY = 0;
                    mwidth = iArr[0];
                    mheight = iArr[1];
                    mresolutionMain = i;
                    mresolutionSub = i2;
                }
            }
            moffsetX = 0;
            moffsetY = 0;
            mwidth = iArr[0];
            mheight = iArr[1];
            mresolutionMain = i;
            mresolutionSub = i2;
        } else {
            throw new RuntimeException(String.format(Locale.US, "%dx%d dpi is not supported.", new Object[]{Integer.valueOf(i), Integer.valueOf(i2)}));
        }
        return 0;
    }

    public synchronized int startScan(AutoCrop autoCrop, AutoSkew autoSkew, ColorMode colorMode, Gamma gamma, int i, InputUnit inputUnit, int i2, int i3, boolean z, boolean z2, ImageFormat imageFormat, int i4, int i5, DoubleFeedLevel doubleFeedLevel) {
        ImageFormat imageFormat2;
        int trunsErrorCode;
        synchronized (this) {
            if (mLibraryInitialized) {
                mautoCtop = autoCrop.ordinal();
                mautoSkew = autoSkew.ordinal();
                mcolorMode = colorMode;
                muserGamma = gamma;
                mlookupTableNo = i;
                minputSource = inputUnit;
                mMaxScanSheets = i2;
                mDuplex = 0;
                if (z) {
                    mDuplex = 1;
                }
                marg_over_scan = 0;
                if (!z2 || isDeviceChige()) {
                    imageFormat2 = imageFormat;
                } else {
                    marg_over_scan = 1;
                    imageFormat2 = imageFormat;
                }
                mimageFormat = imageFormat2;
                marg_jpeg_quality = i4;
                marg_threshold = i5;
                mdoubleFeedLevel = doubleFeedLevel;
                mbufferSize = 65536;
                mScannerLocked = true;
                trunsErrorCode = trunsErrorCode(sEscanI2NativeInterface.startScan(moffsetX, moffsetY, mwidth, mheight, mautoCtop, mautoSkew, mresolutionMain, mresolutionSub, mcolorMode.ordinal(), muserGamma.ordinal(), mlookupTableNo, minputSource.ordinal(), mDuplex > 0 ? mMaxScanSheets * 2 : mMaxScanSheets, i3, mDuplex, marg_over_scan, mimageFormat.ordinal(), marg_jpeg_quality, marg_threshold, mdoubleFeedLevel.getValue(), mbufferSize), true);
            } else {
                throw new RuntimeException("initializeLibrary() has not been called.");
            }
        }
        return trunsErrorCode;
    }

    public synchronized int startScan(ScanI2Params scanI2Params, int i, int i2, int i3, int i4, ImageFormat imageFormat, int i5, int i6, int i7) {
        int trunsErrorCode;
        ScanI2Params scanI2Params2 = scanI2Params;
        synchronized (this) {
            int startScan = sEscanI2NativeInterface.startScan(i3, i4, i, i2, scanI2Params2.autoCrop.ordinal(), scanI2Params2.autoSkew.ordinal(), scanI2Params2.resolutionMain, scanI2Params2.resolutionSub, scanI2Params2.colorMode.ordinal(), scanI2Params2.userGamma.ordinal(), scanI2Params2.lookupTableNo, scanI2Params2.inputUnit.ordinal(), scanI2Params2.maxScanSheets, scanI2Params2.maxWriteSheets, scanI2Params2.duplex ? 1 : 0, scanI2Params2.overScan ? 1 : 0, imageFormat.ordinal(), i5, i6, scanI2Params2.doubleFeedLevel.getValue(), i7);
            mScannerLocked = true;
            trunsErrorCode = trunsErrorCode(startScan, true);
        }
        return trunsErrorCode;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0019, code lost:
        return r0;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized int releaseScanner() {
        /*
            r3 = this;
            monitor-enter(r3)
            boolean r0 = r3.mScannerLocked     // Catch:{ all -> 0x001a }
            r1 = 0
            if (r0 != 0) goto L_0x0008
            monitor-exit(r3)
            return r1
        L_0x0008:
            r3.mScannerLocked = r1     // Catch:{ all -> 0x001a }
            com.epson.lib.escani2.EscanI2NativeInterface r0 = sEscanI2NativeInterface     // Catch:{ all -> 0x001a }
            int r0 = r0.releaseScannerJni()     // Catch:{ all -> 0x001a }
            int r0 = r3.trunsErrorCode(r0, r1)     // Catch:{ all -> 0x001a }
            r2 = 1
            if (r0 != r2) goto L_0x0018
            r0 = 0
        L_0x0018:
            monitor-exit(r3)
            return r0
        L_0x001a:
            r0 = move-exception
            monitor-exit(r3)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.lib.escani2.EscanI2Lib.releaseScanner():int");
    }

    public void cancelScan() {
        sEscanI2NativeInterface.cancelScanJni();
    }

    public int writeScanImage(int i, String str, String str2) {
        if (!mScannerLocked) {
            throw new RuntimeException("startScan() has not been called");
        } else if (str == null) {
            throw new IllegalArgumentException("outfile_fmt_a must not be null.");
        } else if (mDuplex <= 0 || str2 != null) {
            return trunsErrorCode(sEscanI2NativeInterface.scan(i, str, str2), false);
        } else {
            throw new IllegalArgumentException("outfile_fmt_b must not be null.");
        }
    }

    public void setObserver(Observer observer) {
        synchronized (mObserverLock) {
            mObserver = observer;
        }
    }

    public int getScanPages(int i) {
        if (mLibraryInitialized) {
            return sEscanI2NativeInterface.getScanPages(i);
        }
        throw new RuntimeException("initializeLibrary() has not been called.");
    }

    public int cleanAdf() {
        return trunsErrorCode(sEscanI2NativeInterface.cleanAdf(), true);
    }

    public int freeBlockedCleanAdf() {
        return trunsErrorCode(sEscanI2NativeInterface.freeBlockedCleanAdf(), false);
    }

    public int calibrateAdf() {
        return trunsErrorCode(sEscanI2NativeInterface.calibrateAdf(), true);
    }

    public int freeBlockedCalibrateAdf() {
        return trunsErrorCode(sEscanI2NativeInterface.freeBlockedCalibrateAdf(), false);
    }

    public int setPowerSaveTime(int i) {
        return trunsErrorCode(sEscanI2NativeInterface.setPowerSaveTime(i), false);
    }

    public int setPowerOffTime(int i) {
        return trunsErrorCode(sEscanI2NativeInterface.setPowerOffTime(i), false);
    }

    public int getMaintAppliedParameters(int[] iArr) {
        int[] maintAppliedParameters = sEscanI2NativeInterface.getMaintAppliedParameters();
        if (maintAppliedParameters == null) {
            return -100;
        }
        iArr[0] = maintAppliedParameters[0];
        iArr[1] = maintAppliedParameters[1];
        return 0;
    }

    public int getMaintCapability(ArrayList<Integer> arrayList, ArrayList<Integer> arrayList2) {
        int[] maintCapability = sEscanI2NativeInterface.getMaintCapability();
        boolean z = false;
        for (int i : maintCapability) {
            if (i == -1) {
                z = true;
            } else if (!z) {
                arrayList.add(Integer.valueOf(i));
            } else {
                arrayList2.add(Integer.valueOf(i));
            }
        }
        if (maintCapability == null) {
            return -100;
        }
        return 0;
    }

    public int getScannerInfo(ScannerI2Info scannerI2Info) {
        return sEscanI2NativeInterface.getScannerInfo(scannerI2Info);
    }

    public int getCapability(ScannerI2Capability scannerI2Capability) {
        return sEscanI2NativeInterface.getCapability(scannerI2Capability);
    }

    private void updatePages(int i, int i2) {
        synchronized (mObserverLock) {
            if (mObserver != null) {
                mObserver.update(i, i2);
            }
        }
    }
}
