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 android.support.annotation.Nullable;
import com.epson.iprint.shared.SharedParamScan;
import com.epson.mobilephone.common.wifidirect.escprLib;
import epson.common.Constants;
import epson.common.ExternalFileUtils;
import epson.common.Utils;
import epson.print.Util.EPLog;
import epson.scan.activity.ScanContinueParam;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

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) {
        this.cachePath = new File(ExternalFileUtils.getInstance(context).getScannedImageDir(), "escan.cache").getPath();
        return escanWrapperInitDriver();
    }

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

    private void findScannerCB() {
        String str = this.scanner;
        if (str != null && str.length() > 0) {
            String name = getClass().getName();
            EPLog.m313i(name, "EXTRACT Scanner information: " + this.scanner);
            String[] split = this.scanner.split("\\|\\|");
            this.f414si = new ScannerInfo();
            this.f414si.setModelName(split[1]);
            this.f414si.setIp(split[2]);
            this.f414si.setFriendlyName(split[3]);
            this.f414si.setScannerId(split[4]);
            if (split.length > 6) {
                this.f414si.setCommonDeviceName(split[6]);
            } else {
                this.f414si.setCommonDeviceName((String) null);
            }
            if (this.scannerIp == null || this.scannerId == null || !this.scannerIp.equals(this.f414si.getIp()) || this.scannerId.equals(this.f414si.getScannerId())) {
                if (this.listFoundScanner == null) {
                    this.listFoundScanner = new ArrayList();
                    this.mEscIVersionList = new ArrayList<>();
                }
                if (this.listFoundScanner.size() > 0) {
                    for (ScannerInfo next : this.listFoundScanner) {
                        if (next.getScannerId() != null && this.f414si.getScannerId().equals(next.getScannerId())) {
                            return;
                        }
                    }
                }
                if (this.mIsSearch && this.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);
                    this.scanHandler.sendMessage(obtain);
                }
                this.listFoundScanner.add(this.f414si);
                this.mEscIVersionList.add(Integer.valueOf(extractEscVersion(split[5])));
            } else {
                this.bInvalidId = true;
                String name2 = getClass().getName();
                EPLog.m307e(name2, "Invalid IP Printer: Expected = " + this.scannerId);
            }
        }
        this.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 = this.scanHandler;
        if (handler != null) {
            handler.sendEmptyMessage(10);
        }
    }

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

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

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

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

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

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

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

    public int startScanJob(String str) {
        this.isScanning = true;
        this.isCanselOK = false;
        this.error = escanWrapperSetImageName(str);
        if (this.error != 0) {
            EPLog.m313i("EPSON escanLib", "scanners.escanWrapperSetImageName(scannedImageNamePath) error = " + String.valueOf(this.error));
            return this.error;
        }
        EPLog.m313i("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]);
        this.error = escanWrapperStartJob(escanJobAttrib);
        if (this.error != 0) {
            EPLog.m313i("EPSON escanLib", "scanners.escanWrapperStartJob(escanJobAttrib) error = " + String.valueOf(this.error));
            return this.error;
        }
        EPLog.m313i("**** isAdfRotate = ", String.valueOf(this.isAdfRotate));
        this.error = doScan(0, 0, 0, this.isAdfRotate, ScanContinueParam.getAvailableScanPageCount());
        escanWrapperEndJob();
        this.isScanning = false;
        return this.error;
    }

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

    public int GetSupportedOption() {
        this.escanSupportOption = new int[7];
        this.escanSupportResolution = new int[64];
        this.error = escanWrapperGetSupportedOption(this.escanSupportOption, this.escanSupportResolution);
        EPLog.m313i("epson.print.escanLib", "getSupportedOption: " + this.escanSupportOption[1] + PreferencesConstants.COOKIE_DELIMITER + this.escanSupportOption[2] + PreferencesConstants.COOKIE_DELIMITER + this.escanSupportOption[3] + PreferencesConstants.COOKIE_DELIMITER + this.escanSupportOption[4] + PreferencesConstants.COOKIE_DELIMITER + this.escanSupportOption[5] + PreferencesConstants.COOKIE_DELIMITER + this.escanSupportOption[6]);
        return this.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) {
            this.isAdfRotate = Utils.getPrefBoolean(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_ROTATE);
        } else {
            this.isAdfRotate = false;
        }
        escanJobAttrib[12] = Utils.getPrefInt(context, Constants.SCAN_REFS_USED_SCANNER_PATH, Constants.SCAN_REFS_SETTINGS_ADF_PAPER_GUIDE);
        EPLog.m313i("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] = this.offsetX;
        iArr2[3] = this.offsetY;
        iArr2[4] = sharedParamScan.isAvailable() ? sharedParamScan.getPixel_main() : this.scanAreaWidth;
        escanJobAttrib[5] = sharedParamScan.isAvailable() ? sharedParamScan.getPixel_sub() : this.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 this.error;
    }

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

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

    public boolean isJobDone() {
        return this.isJobDone;
    }

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

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

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

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

    public int getOffsetX() {
        return this.offsetX;
    }

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

    public int getOffsetY() {
        return this.offsetY;
    }

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

    public int getScanAreaWidth() {
        return this.scanAreaWidth;
    }

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

    public int getScanAreaHeight() {
        return this.scanAreaHeight;
    }

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

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

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

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

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

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

    public boolean isScanning() {
        return this.isScanning;
    }

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

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

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

    public boolean isCanselOK() {
        return this.isCanselOK;
    }

    public Handler getScanHandler() {
        return this.scanHandler;
    }

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

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

    public boolean getSearchStt() {
        return this.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;
    }
}
