package epson.scan.activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.epson.iprint.prtlogger.CommonLog;
import com.epson.iprint.shared.SharedParamScan;
import com.epson.lib.escani2.EscanI2Lib;
import com.epson.lib.escani2.I2LibScannerInfoAndCapability;
import com.epson.lib.escani2.ScanI2Params;
import com.epson.lib.escani2.ScanSize;
import com.epson.lib.escani2.ScannerI2Info;
import com.epson.mobilephone.common.wifidirect.NfcTagUtils;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;

import epson.common.Constants;
import epson.print.ActivityIACommon;
import epson.print.ActivityNfcPrinter;
import epson.print.ActivityPrintWeb;
import epson.print.CommonDefine;
import epson.print.CustomTitleDialogFragment;
import epson.print.EPImageUtil;
import epson.print.R;
import epson.scan.i2lib.I2ScanParamManager;
import epson.scan.i2lib.I2ScanTask;
import epson.scan.lib.ScanInfoStorage;
import epson.server.screens.StorageServer;
import epson.server.utils.Define;

public class I2ScanActivity extends ActivityIACommon implements I2ScanAreaView.FlickCallback, CustomTitleDialogFragment.Callback, ScanContinueParam.ScanContinueDialogButtonClick {
    private static final int DIALOG_ID_CONFIRM_CANCEL = 1;
    private static final int DIALOG_ID_SCAN_ERROR = 2;
    private static final String DIALOG_TAG_CONFIRM_CANCEL = "cancel-dialog";
    private static final String DIALOG_TAG_SCAN_END = "scan-end";
    private static final String DIALOG_TAG_SCAN_ERROR = "scan-error";
    private static final String PARAM_KEY_SCAN_AUTO_START = "scan-auto-start";
    private static final int REQUEST_CODE_I2SETTING = 1;
    private static final int REQUEST_CODE_NFC_SCANNER_CHANGE = 5;
    private static final int REQUEST_CODE_PRINT = 2;
    private static final int REQUEST_CODE_START_MAIL_ACTIVITY = 3;
    private static final int REQUEST_CODE_WIFI_DIRECT_SIMPLE_AP_CONNECT = 4;
    public static final int RESULT_ESC_I_VERSION_CHANGE = 1;
    private static final String TAG = "I2ScanActivity";
    private boolean mActivityIsVisible;
    private int mCurrentPage;
    private SharedParamScan mExternalIntentData;
    private I2ScanAreaView mI2ScanAreaView;
    private I2ScanTask mI2ScanTask;
    private final LocalHandler mLocalHandler = new LocalHandler(this);
    private CustomButtonWrapper mMailButton;
    private Button mNextPageButton;
    private TextView mPageDisplayText;
    private Button mPreviousPageButton;
    private CustomButtonWrapper mPrintButton;
    private ProgressCallback mProgressCallback;
    private View mProgressViewGroup;
    private CustomButtonWrapper mSaveButton;
    private Button mScanButton;
    private Button mScanCancelButton;
    private ArrayList<String> mScanFileList;
    private int[] mScanFileSizeAndOffset;
    private CommonLog mScanLog;
    private TextView mScanProgressText;
    private boolean mStartScanAtScannerChanged;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_i2_scan);
        mScanFileList = new ArrayList<>();
        mProgressCallback = new ProgressCallback();
        setActionBar(R.string.str_scan_btn, true);
        mI2ScanAreaView = (I2ScanAreaView) findViewById(R.id.areaView);
        mI2ScanAreaView.setFlickCallback(this);
        mProgressViewGroup = findViewById(R.id.llScanningProgress);
        mScanProgressText = (TextView) findViewById(R.id.scanProgressText);
        mPageDisplayText = (TextView) findViewById(R.id.tvScanResult);
        mSaveButton = new CustomButtonWrapper((Button) findViewById(R.id.btnSave), R.drawable.scan_save);
        mSaveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                I2ScanActivity.this.onSaveButtonClicked();
            }
        });
        mMailButton = new CustomButtonWrapper((Button) findViewById(R.id.btnMail), R.drawable.scan_mail);
        mMailButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                I2ScanActivity.this.startMailActivity();
            }
        });
        mPrintButton = new CustomButtonWrapper((Button) findViewById(R.id.btnPrint), R.drawable.scan_print);
        mPrintButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                I2ScanActivity.this.startPrintActivity();
            }
        });
        mScanButton = (Button) findViewById(R.id.btnScan);
        mScanButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                I2ScanActivity i2ScanActivity = I2ScanActivity.this;
                ScanContinueParam.judgeScanContinue(i2ScanActivity, i2ScanActivity.mScanFileList);
            }
        });
        mScanCancelButton = (Button) findViewById(R.id.btnCancelScan);
        mScanCancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                I2ScanActivity.this.confirmCancelScan();
            }
        });
        mNextPageButton = (Button) findViewById(R.id.btnNext);
        mNextPageButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                I2ScanActivity.this.changePage(1);
            }
        });
        mPreviousPageButton = (Button) findViewById(R.id.btnPrev);
        mPreviousPageButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                I2ScanActivity.this.changePage(-1);
            }
        });
        mExternalIntentData = ScanActivityCommonParams.getExternalIntentData(getIntent());
        if (!returnToI1ActivityIfEscIVersionMismatch()) {
            mProgressViewGroup.setVisibility(View.GONE);
            if (bundle == null) {
                resetUserDefineSize(false);
            }
            updateUi();
            mScanLog = ScanActivityCommonParams.getCommonLog(getIntent());
            startScanIfNeeded(getIntent());
        }
    }

    private void startScanIfNeeded(Intent intent) {
        if (ScanActivityCommonParams.startScanAtActivityChange(intent)) {
            mStartScanAtScannerChanged = false;
            requestStartScan();
        }
    }

    private void resetUserDefineSize(boolean z) {
        ScanI2Params loadScanI2Params = I2ScanParamManager.loadScanI2Params(this);
        if (z || loadScanI2Params.scanSize.isPixelSize()) {
            try {
                I2ScanParamManager.updateScanSize(this, ScanSize.getLocaleDefaultSize());
            } catch (IOException unused) {
            }
        }
    }

    private void updateUi() {
        int[] iArr;
        int[] iArr2;
        I2LibScannerInfoAndCapability i2ScannerAllInfo = ScannerPropertyWrapper.load(this).getI2ScannerAllInfo();
        if (i2ScannerAllInfo == null || i2ScannerAllInfo.scannerI2Info == null) {
            ScanInfoStorage.saveEscIVersion(this, 0);
            ScannerPropertyWrapper.clearSavedData(this);
            returnToI1ActivityIfEscIVersionMismatch();
            return;
        }
        ScannerI2Info scannerI2Info = i2ScannerAllInfo.scannerI2Info;
        ScanI2Params loadScanI2Params = I2ScanParamManager.loadScanI2Params(this);
        SharedParamScan sharedParamScan = mExternalIntentData;
        if (sharedParamScan != null) {
            iArr = new int[]{sharedParamScan.getPixel_main(), mExternalIntentData.getPixel_sub(), 0, 0};
            iArr2 = scannerI2Info.getSensorPixelSize(loadScanI2Params.inputUnit, mExternalIntentData.getRes_main(), mExternalIntentData.getRes_sub());
        } else {
            iArr = loadScanI2Params.scanSize.getSize(scannerI2Info, loadScanI2Params);
            iArr2 = scannerI2Info.getSensorPixelSize(loadScanI2Params.inputUnit, loadScanI2Params.resolutionMain, loadScanI2Params.resolutionSub);
        }
        if (loadScanI2Params.inputUnit == EscanI2Lib.InputUnit.ADF) {
            mI2ScanAreaView.setArea(iArr[0], iArr[1]);
        } else {
            mI2ScanAreaView.setMovableAreaModeSize(iArr2[0], iArr2[1], new Rect(iArr[2], iArr[3], iArr[0] + iArr[2], iArr[1] + iArr[3]));
        }
        displayScanFileList();
        if (mExternalIntentData != null) {
            mMailButton.setVisibility(View.GONE);
            mPrintButton.setVisibility(View.GONE);
        }
    }

    private boolean returnToI1ActivityIfEscIVersionMismatch() {
        if (ScanInfoStorage.loadEscIVersion(this) == 2) {
            return false;
        }
        Intent intent = new Intent();
        intent.putExtra(PARAM_KEY_SCAN_AUTO_START, mStartScanAtScannerChanged);
        setResult(1, intent);
        mStartScanAtScannerChanged = false;
        finish();
        return true;
    }

    private void startScan() {
        I2ScanTask i2ScanTask = mI2ScanTask;
        if (i2ScanTask == null || i2ScanTask.getStatus() == AsyncTask.Status.FINISHED) {
            saveScanSizeIfChanged();
            mScanCancelButton.setEnabled(true);
            mScanCancelButton.setVisibility(View.VISIBLE);
            mScanButton.setEnabled(false);
            mProgressViewGroup.setVisibility(View.VISIBLE);
            mPageDisplayText.setVisibility(View.GONE);
            mNextPageButton.setVisibility(View.GONE);
            mPreviousPageButton.setVisibility(View.GONE);
            mI2ScanAreaView.setPromptStringVisibility(4);
            if (!WiFiDirectManager.isNeedConnect(this, WiFiDirectManager.DEVICE_TYPE_SCANNER) || !WiFiDirectManager.reconnect(this, WiFiDirectManager.DEVICE_TYPE_SCANNER, 4)) {
                if (!ScanContinueParam.isContinueScanning()) {
                    ScanCount.countUpScanPage(this);
                }
                mI2ScanTask = new I2ScanTask(this, mProgressCallback, mExternalIntentData, mScanLog);
                mI2ScanTask.execute(new Void[0]);
            }
        }
    }

    private void confirmCancelScan() {
        if (mI2ScanTask != null && mActivityIsVisible) {
            mProgressViewGroup.setVisibility(4);
            CustomTitleDialogFragment.newInstance(1, getString(R.string.str_msg_scan_cancel), 0, R.string.str_yes, R.string.str_no).show(getSupportFragmentManager(), DIALOG_TAG_CONFIRM_CANCEL);
        }
    }

    private void localScanEnd(I2ScanTask.TaskError taskError) {
        dismissCancelConfirmDialog();
        updateScanEnd();
        updateScanFileList(taskError);
        displayScanFileList();
    }

    private void updateScanEnd() {
        mProgressViewGroup.setVisibility(View.GONE);
        mScanButton.setEnabled(true);
    }

    private void showProgressWithoutTextViewCancelButton() {
        mProgressViewGroup.setVisibility(View.VISIBLE);
        mScanCancelButton.setVisibility(4);
        mScanProgressText.setVisibility(4);
    }

    protected void onResume() {
        super.onResume();
        NfcTagUtils.enableForegroundDispatch(this, (IntentFilter[]) null, (String[][]) null);
        mActivityIsVisible = true;
    }

    protected void onPause() {
        super.onPause();
        mActivityIsVisible = false;
        NfcTagUtils.disableForegroundDispatch(this);
    }

    protected void onStop() {
        super.onStop();
        if (mProgressViewGroup.getVisibility() == 0) {
            cancelScan();
        }
        dismissCancelConfirmDialog();
    }

    private void dismissCancelConfirmDialog() {
        CustomTitleDialogFragment customTitleDialogFragment = (CustomTitleDialogFragment) getSupportFragmentManager().findFragmentByTag(DIALOG_TAG_CONFIRM_CANCEL);
        if (customTitleDialogFragment != null) {
            customTitleDialogFragment.dismiss();
        }
    }

    private void cancelScan() {
        mProgressViewGroup.setVisibility(View.VISIBLE);
        I2ScanTask i2ScanTask = mI2ScanTask;
        if (i2ScanTask != null) {
            i2ScanTask.cancelScan();
            mScanCancelButton.setEnabled(false);
        }
    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        I2ScanTask i2ScanTask = mI2ScanTask;
        if ((i2ScanTask == null || i2ScanTask.getStatus() == AsyncTask.Status.FINISHED) && "android.nfc.action.NDEF_DISCOVERED".equals(intent.getAction())) {
            checkNfcTagAndStartProcess(intent);
        }
    }

    private void checkNfcTagAndStartProcess(Intent intent) {
        NfcTagUtils.EpsonNfcConnectInfo parseNECTag = NfcTagUtils.parseNECTag(this, intent);
        if (parseNECTag != null) {
            ScannerPropertyWrapper.load(this).getScannerId();
            Intent intent2 = new Intent();
            intent2.setClass(this, ActivityNfcPrinter.class);
            intent2.putExtra(ActivityNfcPrinter.CONNECTINFO, parseNECTag);
            intent2.putExtra(ActivityNfcPrinter.CHANGEMODE, 2);
            startActivityForResult(intent2, 5);
            mStartScanAtScannerChanged = true;
        }
    }

    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
    }

    public void onBackPressed() {
        if (mProgressViewGroup.getVisibility() != 0) {
            resetUserDefineSize(mI2ScanAreaView.isScanAreaModified());
            ScanActivity.removeTempContents(this);
            finish();
        }
    }

    private void onSaveButtonClicked() {
        if (mExternalIntentData == null) {
            startSaveActivity();
        } else if (!mScanFileList.isEmpty()) {
            showProgressWithoutTextViewCancelButton();
            new ImageConvertAndFinishTask(mExternalIntentData).execute(new ArrayList[]{mScanFileList});
        }
    }

    private void startSaveActivity() {
        ArrayList<String> arrayList = mScanFileList;
        if (arrayList != null && arrayList.size() > 0) {
            Intent intent = new Intent(this, StorageServer.class);
            intent.putExtra(Define.PICK_SERVER_FOR, 2);
            intent.putStringArrayListExtra(Define.SAVING_FILE_PATH, arrayList);
            intent.putExtra(StorageServer.FOR_PRINT_LOGGER_CALL_FROM, 1);
            startActivity(intent);
        }
    }

    private void startMailActivity() {
        if (!mScanFileList.isEmpty()) {
            Intent intent = new Intent(this, MailActivity.class);
            intent.putStringArrayListExtra(Define.SAVING_FILE_PATH, mScanFileList);
            startActivityForResult(intent, 3);
        }
    }

    private void startPrintActivity() {
        if (!mScanFileList.isEmpty()) {
            Intent intent = new Intent(this, ActivityPrintWeb.class);
            intent.putExtra("from", 1);
            intent.putStringArrayListExtra("print_web", mScanFileList);
            intent.putExtra("print_log", ScanActivity.getPrintLog());
            startActivityForResult(intent, 2);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.scan_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.btn_scan_settings) {
            return super.onOptionsItemSelected(menuItem);
        }
        goSettingActivity();
        return true;
    }

    private void saveScanSizeIfChanged() {
        if (mI2ScanAreaView.isScanAreaModified()) {
            Rect movableScanArea = mI2ScanAreaView.getMovableScanArea();
            try {
                I2ScanParamManager.updateScanSize(this, new ScanSize(movableScanArea.width(), movableScanArea.height(), movableScanArea.left, movableScanArea.top));
            } catch (IOException unused) {
            }
        }
    }

    private void goSettingActivity() {
        saveScanSizeIfChanged();
        boolean z = mExternalIntentData != null;
        startActivityForResult(I2ScanSettingActivity.getStartIntent(this, z, z, mExternalIntentData), 1);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        boolean z = false;
        if (i != 1) {
            switch (i) {
                case 4:
                    if (i2 == -1) {
                        requestStartScan();
                        return;
                    } else {
                        updateScanEnd();
                        return;
                    }
                case 5:
                    onI2SettingEnd(i2, false);
                    return;
                default:
                    return;
            }
        } else {
            if (i2 != -1 || I2ScanSettingActivity.getOldDataValidation(intent)) {
                z = true;
            }
            onI2SettingEnd(i2, z);
        }
    }

    private void onI2SettingEnd(int i, boolean z) {
        if (!returnToI1ActivityIfEscIVersionMismatch() && i == -1) {
            if (!z) {
                mScanFileList.clear();
                mCurrentPage = 0;
                ScanContinueParam.resetParameter();
                ScanActivity.removeTempContents(this);
            }
            updateUi();
            if (mStartScanAtScannerChanged) {
                mStartScanAtScannerChanged = false;
                requestStartScan();
            }
        }
    }

    private void requestStartScan() {
        runOnUiThread(new Runnable() {
            public void run() {
                I2ScanActivity.this.startScan();
            }
        });
    }

    private void updateScanFileList(I2ScanTask.TaskError taskError) {
        if (!isFinishing() && !I2ScanTask.TaskError.isInvalidScanArea(taskError)) {
            if (mI2ScanTask != null) {
                if (ScanContinueParam.isContinueScanning()) {
                    mScanFileList.addAll(mI2ScanTask.getScanFileList());
                } else {
                    mScanFileList.clear();
                    mScanFileList.addAll(mI2ScanTask.getScanFileList());
                }
                int size = mScanFileList.size();
                if (size > 0) {
                    ScanCount.saveScanCount(getApplicationContext(), size);
                    ScanContinueParam.setScannedFileCount(size);
                    if (ScanContinueParam.isReachMaximumScan() && I2ScanParamManager.loadScanI2Params(this).inputUnit == EscanI2Lib.InputUnit.ADF) {
                        ScanContinueParam.showScanErrorLimit(this);
                    }
                    ScannerI2Info scannerI2Info = ScannerPropertyWrapper.load(this).getI2ScannerAllInfo().scannerI2Info;
                    ScanI2Params loadScanI2Params = I2ScanParamManager.loadScanI2Params(this);
                    SharedParamScan sharedParamScan = mExternalIntentData;
                    if (sharedParamScan == null) {
                        mScanFileSizeAndOffset = loadScanI2Params.scanSize.getValidScanSize(scannerI2Info, loadScanI2Params);
                    } else {
                        mScanFileSizeAndOffset = new int[]{sharedParamScan.getPixel_main(), mExternalIntentData.getPixel_sub(), 0, 0};
                    }
                }
            } else {
                mScanFileList.clear();
            }
            if (taskError != null && !taskError.isCancel()) {
                displayScanError(taskError);
            }
        }
    }

    private void displayScanError(@NonNull I2ScanTask.TaskError taskError) {
        String str;
        if (mActivityIsVisible) {
            int[] i2ScanErrorTitleAndMessage = getI2ScanErrorTitleAndMessage(taskError);
            if (i2ScanErrorTitleAndMessage[1] == 0) {
                str = "";
            } else {
                str = getString(i2ScanErrorTitleAndMessage[1]);
            }
            CustomTitleDialogFragment.newInstance(2, str, i2ScanErrorTitleAndMessage[0], R.string.str_ok, 0).show(getSupportFragmentManager(), DIALOG_TAG_SCAN_ERROR);
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:24:0x005f, code lost:
        r1[0] = epson.print.R.string.str_err_msg_scan_coveropen_title;
        r1[1] = epson.print.R.string.str_err_msg_scan_coveropen;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:25:0x006a, code lost:
        r1[0] = epson.print.R.string.str_err_msg_scan_paperjam_title;
        r1[1] = epson.print.R.string.str_err_msg_scan_paperjam;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:26:0x0075, code lost:
        r1[0] = epson.print.R.string.str_err_msg_scan_paperout_title;
        r1[1] = epson.print.R.string.str_err_msg_scan_paperout;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private int[] getI2ScanErrorTitleAndMessage(@android.support.annotation.NonNull epson.scan.i2lib.I2ScanTask.TaskError r8) {
        /*
            r7 = this;
            r0 = 2
            int[] r1 = new int[r0]
            int r2 = r8.error1
            r3 = 2131625131(0x7f0e04ab, float:1.8877461E38)
            r4 = 2131625132(0x7f0e04ac, float:1.8877463E38)
            r5 = 1
            r6 = 0
            switch(r2) {
                case 1: goto L_0x0028;
                case 2: goto L_0x0012;
                default: goto L_0x0010;
            }
        L_0x0010:
            goto L_0x00b6
        L_0x0012:
            int r8 = r8.error2
            if (r8 == r0) goto L_0x001c
            r1[r6] = r4
            r1[r5] = r3
            goto L_0x00b6
        L_0x001c:
            r8 = 2131625276(0x7f0e053c, float:1.8877755E38)
            r1[r6] = r8
            r8 = 2131625194(0x7f0e04ea, float:1.887759E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x0028:
            int r8 = r8.error2
            r0 = -500001(0xfffffffffff85edf, float:NaN)
            if (r8 == r0) goto L_0x00ac
            r0 = -5007(0xffffffffffffec71, float:NaN)
            if (r8 == r0) goto L_0x00a1
            r0 = -1501(0xfffffffffffffa23, float:NaN)
            if (r8 == r0) goto L_0x0096
            r0 = -1300(0xfffffffffffffaec, float:NaN)
            if (r8 == r0) goto L_0x008b
            r0 = -1114(0xfffffffffffffba6, float:NaN)
            if (r8 == r0) goto L_0x0080
            r0 = -1100(0xfffffffffffffbb4, float:NaN)
            if (r8 == r0) goto L_0x0080
            switch(r8) {
                case -5003: goto L_0x0075;
                case -5002: goto L_0x006a;
                case -5001: goto L_0x005f;
                default: goto L_0x0046;
            }
        L_0x0046:
            switch(r8) {
                case -215: goto L_0x005f;
                case -214: goto L_0x0080;
                case -213: goto L_0x0096;
                case -212: goto L_0x006a;
                case -211: goto L_0x00a1;
                case -210: goto L_0x0075;
                default: goto L_0x0049;
            }
        L_0x0049:
            switch(r8) {
                case -202: goto L_0x0054;
                case -201: goto L_0x0054;
                default: goto L_0x004c;
            }
        L_0x004c:
            switch(r8) {
                case -102: goto L_0x0054;
                case -101: goto L_0x0054;
                default: goto L_0x004f;
            }
        L_0x004f:
            r1[r6] = r4
            r1[r5] = r3
            goto L_0x00b6
        L_0x0054:
            r8 = 2131625073(0x7f0e0471, float:1.8877344E38)
            r1[r6] = r8
            r8 = 2131625120(0x7f0e04a0, float:1.8877439E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x005f:
            r8 = 2131625128(0x7f0e04a8, float:1.8877455E38)
            r1[r6] = r8
            r8 = 2131625127(0x7f0e04a7, float:1.8877453E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x006a:
            r8 = 2131625137(0x7f0e04b1, float:1.8877473E38)
            r1[r6] = r8
            r8 = 2131625136(0x7f0e04b0, float:1.8877471E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x0075:
            r8 = 2131625139(0x7f0e04b3, float:1.8877478E38)
            r1[r6] = r8
            r8 = 2131625138(0x7f0e04b2, float:1.8877475E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x0080:
            r8 = 2131625126(0x7f0e04a6, float:1.8877451E38)
            r1[r6] = r8
            r8 = 2131625125(0x7f0e04a5, float:1.887745E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x008b:
            r8 = 2131625135(0x7f0e04af, float:1.887747E38)
            r1[r6] = r8
            r8 = 2131625134(0x7f0e04ae, float:1.8877467E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x0096:
            r8 = 2131625124(0x7f0e04a4, float:1.8877447E38)
            r1[r6] = r8
            r8 = 2131625123(0x7f0e04a3, float:1.8877445E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x00a1:
            r8 = 2131625130(0x7f0e04aa, float:1.887746E38)
            r1[r6] = r8
            r8 = 2131625129(0x7f0e04a9, float:1.8877457E38)
            r1[r5] = r8
            goto L_0x00b6
        L_0x00ac:
            r8 = 2131624371(0x7f0e01b3, float:1.887592E38)
            r1[r6] = r8
            r8 = 2131625160(0x7f0e04c8, float:1.887752E38)
            r1[r5] = r8
        L_0x00b6:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.scan.activity.I2ScanActivity.getI2ScanErrorTitleAndMessage(epson.scan.i2lib.I2ScanTask$TaskError):int[]");
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void setScanFileList(ArrayList<String> arrayList, int[] iArr) {
        mScanFileList = arrayList;
        if (mScanFileList == null) {
            mScanFileList = new ArrayList<>();
        }
        mScanFileSizeAndOffset = iArr;
        displayScanFileList();
    }

    private void displayScanFileList() {
        if (!isFinishing()) {
            boolean z = true;
            int i = 0;
            if (!mScanFileList.isEmpty()) {
                mCurrentPage = mScanFileList.size() - 1;
                if (mScanFileList.size() <= 1) {
                    mPageDisplayText.setVisibility(View.GONE);
                } else {
                    mPageDisplayText.setVisibility(View.VISIBLE);
                }
            } else {
                mCurrentPage = 0;
                z = false;
            }
            mSaveButton.setEnabled(z);
            mMailButton.setEnabled(z);
            mPrintButton.setEnabled(z);
            I2ScanAreaView i2ScanAreaView = mI2ScanAreaView;
            if (z) {
                i = 4;
            }
            i2ScanAreaView.setPromptStringVisibility(i);
            updateScanPreviewPage();
        }
    }

    private void changePage(int i) {
        mCurrentPage += i;
        int i2 = mCurrentPage;
        if (i2 < 0) {
            mCurrentPage = 0;
        } else if (i2 >= mScanFileList.size()) {
            mCurrentPage = mScanFileList.size() - 1;
        }
        updateScanPreviewPage();
    }

    private void updateScanPreviewPage() {
        int size = mScanFileList.size();
        int i = 0;
        if (size > 1) {
            mPageDisplayText.setVisibility(View.VISIBLE);
            TextView textView = mPageDisplayText;
            textView.setText((mCurrentPage + 1) + Constants.CROSS + size);
        } else {
            mPageDisplayText.setVisibility(View.GONE);
        }
        int i2 = mCurrentPage > 0 ? 0 : 8;
        if (mCurrentPage >= mScanFileList.size() - 1) {
            i = 8;
        }
        mPreviousPageButton.setVisibility(i2);
        mNextPageButton.setVisibility(i);
        if (mScanFileList.isEmpty()) {
            mI2ScanAreaView.setPreviewImage((String) null, (int[]) null);
        } else {
            mI2ScanAreaView.setPreviewImage(mScanFileList.get(mCurrentPage), mScanFileSizeAndOffset);
        }
        mI2ScanAreaView.invalidate();
    }

    private void updateScanProgressPreview(int i, int i2) {
        File scanFile = mI2ScanTask.getScanFile(i, i2);
        if (scanFile != null || scanFile.canRead()) {
            mI2ScanAreaView.setPreviewImage(scanFile.getPath());
            mI2ScanAreaView.invalidate();
        }
    }

    public void flick(int i) {
        changePage(i);
    }

    public void onLocalNegativeCallback(int i) {
        if (i == 1) {
            mScanCancelButton.setEnabled(true);
            mProgressViewGroup.setVisibility(View.VISIBLE);
        }
    }

    public void onLocalPositiveCallback(int i) {
        if (i == 1) {
            cancelScan();
        }
    }

    private void finishWithScanParam(@NonNull ArrayList<String> arrayList) {
        Intent intent = new Intent();
        intent.putExtra("scan-file", arrayList);
        setResult(-1, intent);
        finish();
    }

    public void onRequestScanAction() {
        startScan();
    }

    private class ProgressCallback implements I2ScanTask.ScanResultReceiver {
        private ProgressCallback() {
        }

        public void onProgressUpdate(int i, int i2) {
            I2ScanActivity.mLocalHandler.sendUpdateScanProgressPreviewMessage(i, i2);
        }

        public void onScanEnd(I2ScanTask.TaskError taskError) {
            I2ScanActivity.this.localScanEnd(taskError);
        }
    }

    static boolean getAutoStartRequestedFromReturnIntent(Intent intent) {
        if (intent == null) {
            return false;
        }
        return intent.getBooleanExtra(PARAM_KEY_SCAN_AUTO_START, false);
    }

    private static class LocalHandler extends Handler {
        static final int MESSAGE_UPDATE_PREVIEW = 1;
        private final WeakReference<I2ScanActivity> mActivity;

        LocalHandler(I2ScanActivity i2ScanActivity) {
            mActivity = new WeakReference<>(i2ScanActivity);
        }

        public void handleMessage(Message message) {
            I2ScanActivity i2ScanActivity;
            if (message.what == 1 && (i2ScanActivity = (I2ScanActivity) mActivity.get()) != null && !i2ScanActivity.isFinishing()) {
                i2ScanActivity.updateScanProgressPreview(message.arg1, message.arg2);
            }
        }

        private void sendUpdateScanProgressPreviewMessage(int i, int i2) {
            removeMessages(1);
            sendMessage(obtainMessage(1, i, i2));
        }
    }

    private class ImageConvertAndFinishTask extends AsyncTask<ArrayList<String>, Void, Integer> {
        private final int ERROR_CODE_FILE_WRITE_ERROR = 2;
        private final int ERROR_CODE_NO_ERROR = 0;
        private final int ERROR_CODE_NO_FILE = 1;
        private ArrayList<String> mScanFileList;
        private SharedParamScan mScanParams;

        ImageConvertAndFinishTask(@NonNull SharedParamScan sharedParamScan) {
            mScanParams = sharedParamScan;
        }

        protected Integer doInBackground(ArrayList<String>... arrayListArr) {
            int i;
            if (arrayListArr.length <= 0) {
                return 1;
            }
            ArrayList<String> arrayList = arrayListArr[0];
            if (arrayList.size() <= 0) {
                return 1;
            }
            File file = new File(mScanParams.getFolder_name());
            if (!file.exists() && !file.mkdirs()) {
                return 2;
            }
            mScanFileList = new ArrayList<>();
            EPImageUtil ePImageUtil = new EPImageUtil();
            String str = file.getPath() + CommonDefine.SLASH + mScanParams.getFile_name() + "_%03d.bmp";
            boolean z = mScanParams.getScan_type() == 3;
            Iterator<String> it = arrayList.iterator();
            int i2 = 0;
            while (it.hasNext()) {
                String next = it.next();
                i2++;
                String format = String.format(Locale.US, str, new Object[]{Integer.valueOf(i2)});
                if (z) {
                    i = EPImageUtil.jpegTo1BitBmpPixelRoundDown(next, format, 127);
                } else {
                    i = ePImageUtil.jpg2bmp(next, format, 1);
                }
                if (i != 0) {
                    return 2;
                }
                mScanFileList.add(format);
            }
            return 0;
        }

        protected void onPostExecute(Integer num) {
            I2ScanActivity.this.updateScanEnd();
            if (num != null && num.intValue() != 1) {
                if (num.intValue() == 0) {
                    ArrayList<String> arrayList = mScanFileList;
                    if (arrayList != null) {
                        I2ScanActivity.this.finishWithScanParam(arrayList);
                        return;
                    }
                    return;
                }
                I2ScanActivity.this.displayScanError(convertError(num.intValue()));
            }
        }

        @NonNull
        private I2ScanTask.TaskError convertError(int i) {
            return new I2ScanTask.TaskError(1, i != 2 ? EscanI2Lib.ERROR_CODE_GENERAL_ERROR : -202);
        }
    }
}
