package epson.scan.lib;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcel;
import android.os.Parcelable;

import com.epson.iprint.shared.SharedParamScan;
import com.epson.mobilephone.common.wifidirect.escprLib;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import epson.common.Constants;
import epson.common.ExternalFileUtils;
import epson.common.Utils;
import epson.print.Util.EPLog;
import epson.scan.activity.ScanContinueParam;

public class escanLib implements Parcelable, EscanLibInterface {
    public static final Parcelable.Creator<escanLib> CREATOR = new Parcelable.Creator<escanLib>() {
        public escanLib createFromParcel(Parcel parcel) {
            return new escanLib();
        }

        public escanLib[] newArray(int i) {
            return new escanLib[i];
        }
    };
    private static int[] escanJobAttrib;
    private static int[] escanStatus;
    volatile boolean bInvalidId = false;
    private String cachePath = null;
    private String debugString = "";
    int error = 0;
    private int[] escanSupportOption;
    private int[] escanSupportResolution;
    private boolean isAdfRotate = false;
    private boolean isCanselOK = false;
    private boolean isJobDone = true;
    private boolean isScanning;
    private List<ScannerInfo> listFoundScanner;
    private List<String> listScannedFile = new ArrayList();
    private ArrayList<Integer> mEscIVersionList = new ArrayList<>();
    private boolean mIsSearch;
    private int offsetX;
    private int offsetY;
    private String printerList = "";
    private int scanAreaHeight;
    private int scanAreaWidth;
    private Handler scanHandler;
    private String scanner = "";
    volatile String scannerId;
    volatile String scannerIp;

    /* renamed from: si */
    ScannerInfo f414si;
    int[] status = new int[3];

    private native int escanWrapperInitDriver();

    public native int BMPAppendRaster(char[] cArr, char[] cArr2, int i);

    public native int BMPClose(char[] cArr);

    public native int BMPCreate(char[] cArr, int i, int i2, int i3);

    public int describeContents() {
        return 0;
    }

    public native int doScan(int i, int i2, int i3, boolean z, int i4);

    public native int escanProbeScanner(int i, int i2, char[] cArr, int i3);

    public native int escanProbeScannerById(int i, char[] cArr, char[] cArr2);

    public native int escanWrapperCancelFindScanner();

    public native int escanWrapperCancelJob();

    public native int escanWrapperEndJob();

    public native int escanWrapperEndPage(int i);

    public native int escanWrapperFindScanner(int i);

    public native int escanWrapperGetScanningImageInfo(int[] iArr);

    public native int escanWrapperGetStatus(int[] iArr);

    public native int escanWrapperGetSupportedOption(int[] iArr, int[] iArr2);

    public native int escanWrapperRecvScanImageBlock(int[] iArr);

    public native int escanWrapperReleaseDriver();

    public native int escanWrapperSetImageName(String str);

    public native int escanWrapperSetScanner(int i);

    public native int escanWrapperSetWaitingConfirmFlag(boolean z);

    public native int escanWrapperStartJob(int[] iArr);

    public native int escanWrapperStartPage();

    public void writeToParcel(Parcel parcel, int i) {
    }

    static {
        System.loadLibrary("escan");
    }

    public int escanWrapperInitDriver(Context context) {
        cachePath = new File(ExternalFileUtils.getInstance(context).getScannedImageDir(), "escan.cache").getPath();
        return escanWrapperInitDriver();
    }

    private void updateStatusCB(int[] iArr) {
        status = iArr;
        EPLog.i("escanLib.java", "ESCAN SCAN status = " + status[0] + PreferencesConstants.COOKIE_DELIMITER + status[1] + PreferencesConstants.COOKIE_DELIMITER + status[2]);
    }

    private void findScannerCB() {
        String str = scanner;
        if (str != null && str.length() > 0) {
            String name = getClass().getName();
            EPLog.i(name, "EXTRACT Scanner information: " + scanner);
            String[] split = scanner.split("\\|\\|");
            f414si = new ScannerInfo();
            f414si.setModelName(split[1]);
            f414si.setIp(split[2]);
            f414si.setFriendlyName(split[3]);
            f414si.setScannerId(split[4]);
            if (split.length > 6) {
                f414si.setCommonDeviceName(split[6]);
            } else {
                f414si.setCommonDeviceName((String) null);
            }
            if (this.scannerIp == null || scannerId == null || !this.scannerIp.equals(this.f414si.getIp()) || scannerId.equals(this.f414si.getScannerId())) {
                if (this.listFoundScanner == null) {
                    listFoundScanner = new ArrayList();
                    mEscIVersionList = new ArrayList<>();
                }
                if (this.listFoundScanner.size() > 0) {
                    for (ScannerInfo next : listFoundScanner) {
                        if (next.getScannerId() != null && f414si.getScannerId().equals(next.getScannerId())) {
                            return;
                        }
                    }
                }
                if (mIsSearch && scanHandler != null) {
                    Message obtain = Message.obtain();
                    obtain.what = 8;
                    Bundle bundle = new Bundle();
                    bundle.putString("name", split[1]);
                    bundle.putString(escprLib.PRINTER_IP, split[2]);
                    bundle.putString("id", split[4]);
                    if (split.length > 6) {
                        bundle.putString("commonDeviceName", split[6]);
                    }
                    obtain.setData(bundle);
                    scanHandler.sendMessage(obtain);
                }
                listFoundScanner.add(this.f414si);
                mEscIVersionList.add(Integer.valueOf(extractEscVersion(split[5])));
            } else {
                bInvalidId = true;
                String name2 = getClass().getName();
                EPLog.e(name2, "Invalid IP Printer: Expected = " + scannerId);
            }
        }
        scanner = "";
    }

    private int extractEscVersion(String str) {
        if (str == null) {
            return -1;
        }
        switch (str.charAt(0)) {
            case '1':
                return 1;
            case '2':
                return 2;
            default:
                return -1;
        }
    }

    private void recvScanImageBlockCB() {
        Handler handler = scanHandler;
        if (handler != null) {
            handler.sendEmptyMessage(10);
        }
    }

    private void endPageCB() {
        Handler handler = scanHandler;
        if (handler != null) {
            handler.sendEmptyMessage(11);
        }
        isCanselOK = false;
    }

    private void javaDebugCB(String str) {
        EPLog.i(getClass().getName(), str.toString());
    }

    private void newFileNotifyCB(String str) {
        listScannedFile.add(str.toString());
        mEscIVersionList.add(-1);
        String name = getClass().getName();
        EPLog.i(name, "newFileNotifyCB() call, Scan new file: " + str.toString() + ", totals: " + listScannedFile.size());
        isCanselOK = true;
    }

    public void resetEscanLib() {
        isJobDone = false;
        error = 0;
        status = new int[3];
        listFoundScanner = new ArrayList();
        mEscIVersionList.clear();
    }

    public int probeScannerById(String str, String str2) {
        resetEscanLib();
        resetIPAdressCheck();
        int escanProbeScannerById = escanProbeScannerById(120, str.toCharArray(), str2.toCharArray());
        EPLog.i("EPSON escanLib", "scanners.probeScannerById() error = " + String.valueOf(escanProbeScannerById));
        return escanProbeScannerById;
    }

    public int probeScannerByIp(String str, String str2) {
        resetEscanLib();
        resetIPAdressCheck();
        scannerId = str;
        scannerIp = str2;
        int escanProbeScanner = escanProbeScanner(120, 2, str2.toCharArray(), str2.toCharArray().length);
        if (escanProbeScanner != 0 || !this.bInvalidId) {
            return escanProbeScanner;
        }
        return -500001;
    }

    public void resetIPAdressCheck() {
        scannerId = null;
        scannerId = null;
        bInvalidId = false;
    }

    public int startScanJob(String str) {
        isScanning = true;
        isCanselOK = false;
        error = escanWrapperSetImageName(str);
        if (this.error != 0) {
            EPLog.i("EPSON escanLib", "scanners.escanWrapperSetImageName(scannedImageNamePath) error = " + String.valueOf(this.error));
            return error;
        }
        EPLog.i("EPSON escanLib", escanJobAttrib[0] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[1] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[2] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[3] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[4] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[5] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[6] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[7] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[8] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[9] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[10] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[11] + PreferencesConstants.COOKIE_DELIMITER + escanJobAttrib[12]);
        error = escanWrapperStartJob(escanJobAttrib);
        if (this.error != 0) {
            EPLog.i("EPSON escanLib", "scanners.escanWrapperStartJob(escanJobAttrib) error = " + String.valueOf(this.error));
            return error;
        }
        EPLog.i("**** isAdfRotate = ", String.valueOf(this.isAdfRotate));
        error = doScan(0, 0, 0, isAdfRotate, ScanContinueParam.getAvailableScanPageCount());
        escanWrapperEndJob();
        isScanning = false;
        return error;
    }

    public int cancelScanJob() {
        error = escanWrapperCancelJob();
        return error;
    }

    public int GetSupportedOption() {
        escanSupportOption = new int[7];
        escanSupportResolution = new int[64];
        error = escanWrapperGetSupportedOption(this.escanSupportOption, escanSupportResolution);
        EPLog.i("epson.print.escanLib", "getSupportedOption: " + escanSupportOption[1] + PreferencesConstants.COOKIE_DELIMITER + escanSupportOption[2] + PreferencesConstants.COOKIE_DELIMITER + escanSupportOption[3] + PreferencesConstants.COOKIE_DELIMITER + escanSupportOption[4] + PreferencesConstants.COOKIE_DELIMITER + escanSupportOption[5] + PreferencesConstants.COOKIE_DELIMITER + escanSupportOption[6]);
        return error;
    }

    public void makeJobAttribute(Context context, SharedParamScan sharedParamScan) {
        escanJobAttrib = new int[13];
        escanJobAttrib[0] = Utils.getPrefInt(context, Constants.SCAN_REFS_SUPPORTED_OPTIONS, Constants.SCAN_REFS_OPTIONS_VERSION);
        setScanArea(context, sharedParamScan);
        escanJobAttrib[7] = sharedParamScan.isAvailable() ? sharedParamScan.getScan_type() : Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_COLOR);
        if (Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_2SIDED) == 1 && Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_SOURCE) == 1) {
            escanJobAttrib[8] = 2;
        } else if (Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_2SIDED) == 0 && Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_SOURCE) == 1) {
            escanJobAttrib[8] = 1;
        } else {
            escanJobAttrib[8] = 0;
        }
        int[] iArr = escanJobAttrib;
        iArr[9] = 1;
        iArr[10] = Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_GAMMA);
        escanJobAttrib[11] = Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_DENSITY);
        if (escanJobAttrib[8] == 2) {
            isAdfRotate = Utils.getPrefBoolean(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_ROTATE);
        } else {
            isAdfRotate = false;
        }
        escanJobAttrib[12] = Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_ADF_PAPER_GUIDE);
        EPLog.i("makeJobAttribute", "**** PAPER_GUIDE =  " + escanJobAttrib[12]);
    }

    public void setScanArea(Context context, SharedParamScan sharedParamScan) {
        int i;
        int[] iArr = escanJobAttrib;
        if (sharedParamScan.isAvailable()) {
            i = sharedParamScan.getRes_main();
        } else {
            i = Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_RESOLUTION);
        }
        iArr[1] = i;
        int[] iArr2 = escanJobAttrib;
        iArr2[2] = offsetX;
        iArr2[3] = offsetY;
        iArr2[4] = sharedParamScan.isAvailable() ? sharedParamScan.getPixel_main() : scanAreaWidth;
        escanJobAttrib[5] = sharedParamScan.isAvailable() ? sharedParamScan.getPixel_sub() : scanAreaHeight;
        int[] iArr3 = escanJobAttrib;
        int i2 = iArr3[1];
        if (i2 == 75) {
            iArr3[6] = 16;
        } else if (i2 != 300) {
            iArr3[6] = 8;
        } else {
            iArr3[6] = 4;
        }
    }

    public int getError() {
        return error;
    }

    public void setError(int i) {
        error = i;
    }

    public void setJobDone(boolean z) {
        isJobDone = z;
    }

    public boolean isJobDone() {
        return isJobDone;
    }

    public int[] getEscanSupportOption() {
        return escanSupportOption;
    }

    public void setEscanSupportOption(int[] iArr) {
        escanSupportOption = iArr;
    }

    public int[] getEscanSupportResolution() {
        return escanSupportResolution;
    }

    public void setEscanSupportResolution(int[] iArr) {
        escanSupportResolution = iArr;
    }

    public int getOffsetX() {
        return offsetX;
    }

    public void setOffsetX(int i) {
        offsetX = i;
    }

    public int getOffsetY() {
        return offsetY;
    }

    public void setOffsetY(int i) {
        offsetY = i;
    }

    public int getScanAreaWidth() {
        return scanAreaWidth;
    }

    public void setScanAreaWidth(int i) {
        scanAreaWidth = i;
    }

    public int getScanAreaHeight() {
        return scanAreaHeight;
    }

    public void setScanAreaHeight(int i) {
        scanAreaHeight = i;
    }

    public List<ScannerInfo> getListFoundScanner() {
        return listFoundScanner;
    }

    public void setListFoundScanner(List<ScannerInfo> list) {
        listFoundScanner = list;
    }

    public List<String> getListScannedFile() {
        return listScannedFile;
    }

    public void setListScannedFile(List<String> list) {
        listScannedFile = list;
    }

    public int getEscIVersion(int i) {
        if (i < 0 || i >= mEscIVersionList.size()) {
            return -1;
        }
        return mEscIVersionList.get(i).intValue();
    }

    public boolean isScanning() {
        return isScanning;
    }

    public void setScanning(boolean z) {
        isScanning = z;
    }

    public int[] getStatus() {
        return status;
    }

    public void setStatus(int[] iArr) {
        status = iArr;
    }

    public boolean isCanselOK() {
        return isCanselOK;
    }

    public Handler getScanHandler() {
        return scanHandler;
    }

    public void setScanHandler(Handler handler) {
        scanHandler = handler;
    }

    public void setSearchStt(boolean z) {
        mIsSearch = z;
    }

    public boolean getSearchStt() {
        return mIsSearch;
    }

    public ScannerInfo probeByIpAndGetScannerInfo(String str, String str2) {
        List<ScannerInfo> listFoundScanner2;
        resetEscanLib();
        if (str == null || str.isEmpty()) {
            str = null;
        }
        if (probeScannerByIp(str, str2) == 0 && (listFoundScanner2 = getListFoundScanner()) != null) {
            return listFoundScanner2.get(0);
        }
        return null;
    }

    @Nullable
    public I1ScanParams getScanParams() {
        int[] iArr = escanJobAttrib;
        if (iArr == null || iArr.length < 3) {
            return null;
        }
        I1ScanParams i1ScanParams = new I1ScanParams();
        switch (escanJobAttrib[8]) {
            case 0:
                i1ScanParams.inputDevice = 0;
                i1ScanParams.twoSide = 0;
                break;
            case 1:
                i1ScanParams.inputDevice = 1;
                i1ScanParams.twoSide = 0;
                break;
            case 2:
                i1ScanParams.inputDevice = 1;
                i1ScanParams.twoSide = 1;
                break;
            default:
                return null;
        }
        int[] iArr2 = escanJobAttrib;
        i1ScanParams.colorType = iArr2[7];
        i1ScanParams.resolution = iArr2[1];
        i1ScanParams.gamma = iArr2[10];
        i1ScanParams.brightness = 255 - iArr2[11];
        return i1ScanParams;
    }
}
