package epson.scan.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.epson.iprint.shared.SharedParamScan;
import com.epson.mobilephone.common.PrintingLib.define.Constants;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
import epson.common.Constants;
import epson.common.Utils;
import epson.print.ActivityIACommon;
import epson.print.R;
import epson.print.EPPrinterManager;
import epson.print.Util.EPLog;
import epson.scan.lib.ScanCommonParams;
import epson.scan.lib.ScanInfoStorage;
import epson.scan.lib.ScanSettingHelper;
import epson.scan.lib.ScanSizeHelper;
import epson.scan.lib.ScannerInfo;

public class ScanSettingsActivity extends ActivityIACommon {
    public static final String HIDE_COLOR_MODE = "hide-color-mode";
    public static final String HIDE_RESOLUTION = "hide-resolution";
    public static final String IS_NEW_SAVE = "IS_NEW_SAVE";
    public static final String NO_CLEAR_RESULT = "NO_CLEAR_RESULT";
    public static final String PARAM_KEY_EXTERNAL_DATA = "external-scan-params";
    private static final int REQUEST_CODE_BRIGHTNESS = 8;
    private static final int REQUEST_CODE_CHANGE_ESC_I_VERSION = 100;
    private static final int REQUEST_CODE_SELECT_SCANNER = 1;
    private static final String SCAN_SETTINGS_LAST_DENSITY = "SCAN_SETTINGS_LAST_DENSITY";
    private static final String SCAN_SETTINGS_LAST_DENSITY_STATUS = "SCAN_SETTINGS_LAST_DENSITY_STATUS";
    private static final String SCAN_SETTINGS_LAST_VALUE = "SCAN_SETTINGS_LAST_VALUE";
    private static final String SCAN_SUPPORTED_RESOLUTION_LIST = "SCAN_SUPPORTED_RESOLUTION_LIST";
    private static final String SCAN_SUPPORTED_SIZE_LIST = "SCAN_SUPPORTED_SIZE_LIST";
    private String autoConnectSsid = null;
    private Context context;
    private AlertDialog errorDialog;
    private boolean isNeedSaveSupportedOptions = false;
    private LinearLayout llScanName;
    private LinearLayout llScanSettingsAdvance2Sided;
    private LinearLayout llScanSettingsColor;
    private LinearLayout llScanSettingsDensity;
    private LinearLayout llScanSettingsGamma;
    private LinearLayout llScanSettingsResolution;
    private LinearLayout llScanSettingsSize;
    private LinearLayout llScanSettingsSource;
    private View mBrightnessSeparator;
    private SharedParamScan mExternalIntentData;
    private boolean mHideColorMode;
    private boolean mHideResolution;
    private View.OnClickListener mLLScanNameClick = new View.OnClickListener() {
        public void onClick(View view) {
            ScanSettingsActivity.this.llScanName.setClickable(false);
            new AsyncTask<Void, Void, Void>() {
                protected void onPreExecute() {
                }

                protected Void doInBackground(Void... voidArr) {
                    WiFiDirectManager.disconnect(ScanSettingsActivity.this.getApplicationContext(), WiFiDirectManager.DEVICE_TYPE_SCANNER, ScanSettingsActivity.this.scanInfo.getIp());
                    WiFiDirectManager.disconnectSimpleAP(ScanSettingsActivity.this.getApplicationContext(), ScanSettingsActivity.this.autoConnectSsid, ScanSettingsActivity.this.scanInfo.getIp());
                    return null;
                }

                protected void onPostExecute(Void voidR) {
                    ScanSettingsActivity.this.startActivityForResult(ScanSearchActivity.getStartIntent(ScanSettingsActivity.this.getApplicationContext(), ScanSettingsActivity.this.scanInfo.getScannerId(), ScanSettingsActivity.this.scanInfo.getLocation(), ScanSettingsActivity.this.autoConnectSsid), 1);
                    ScanSettingsActivity.this.llScanName.setClickable(true);
                }
            }.execute(new Void[0]);
        }
    };
    private View.OnClickListener mLLScanSettings2Sided = new View.OnClickListener() {
        public void onClick(View view) {
            ScanSettingsActivity scanSettingsActivity = ScanSettingsActivity.this;
            scanSettingsActivity.goToSettingAdvanceDetailsScreen(Constants.SCAN_SETTINGS_DETAIL_TITLE, R.string.str_settings_2sided_title, scanSettingsActivity.scanInfo.getTwoSidedScanningValue(), 7);
        }
    };
    private View.OnClickListener mLLScanSettingsColor = new View.OnClickListener() {
        public void onClick(View view) {
            if (ScanSettingsActivity.this.scanInfo.getIp().equals("")) {
                return;
            }
            if (ScanSettingsActivity.this.progressGetOption == null || !ScanSettingsActivity.this.progressGetOption.isShown()) {
                ScanSettingsActivity scanSettingsActivity = ScanSettingsActivity.this;
                scanSettingsActivity.startDetailSettingScreenForResult(Constants.SCAN_SETTINGS_DETAIL_TITLE, R.string.str_settings_color_title, scanSettingsActivity.scanInfo.getColorValue(), 5);
            }
        }
    };
    private View.OnClickListener mLLScanSettingsDensity = new View.OnClickListener() {
        public void onClick(View view) {
            ScanSettingsActivity.this.goToSettingAdvanceDensityScreen();
        }
    };
    private View.OnClickListener mLLScanSettingsGamma = new View.OnClickListener() {
        public void onClick(View view) {
            ScanSettingsActivity scanSettingsActivity = ScanSettingsActivity.this;
            scanSettingsActivity.goToSettingAdvanceDetailsScreen(Constants.SCAN_SETTINGS_DETAIL_TITLE, R.string.str_settings_gamma_title, scanSettingsActivity.scanInfo.getGammaValue(), 9);
        }
    };
    private View.OnClickListener mLLScanSettingsResolution = new View.OnClickListener() {
        public void onClick(View view) {
            if (ScanSettingsActivity.this.scanInfo.getIp().equals("")) {
                return;
            }
            if (ScanSettingsActivity.this.progressGetOption == null || !ScanSettingsActivity.this.progressGetOption.isShown()) {
                ScanSettingsActivity scanSettingsActivity = ScanSettingsActivity.this;
                scanSettingsActivity.startDetailSettingScreenForResult(Constants.SCAN_SETTINGS_DETAIL_TITLE, R.string.str_settings_resolution_title, scanSettingsActivity.scanInfo.getResolutionValue(), 6);
            }
        }
    };
    private View.OnClickListener mLLScanSettingsSize = new View.OnClickListener() {
        public void onClick(View view) {
            ScanSettingsActivity scanSettingsActivity = ScanSettingsActivity.this;
            scanSettingsActivity.startDetailSettingScreenForResult(Constants.SCAN_SETTINGS_DETAIL_TITLE, R.string.str_scanning_size, scanSettingsActivity.scanInfo.getSizeValue(), 11);
        }
    };
    private View.OnClickListener mLLScanSettingsSource = new View.OnClickListener() {
        public void onClick(View view) {
            if (ScanSettingsActivity.this.scanInfo.getIp().equals("")) {
                return;
            }
            if (ScanSettingsActivity.this.progressGetOption == null || !ScanSettingsActivity.this.progressGetOption.isShown()) {
                ScanSettingsActivity.this.startInputUnitSettingActivity();
            }
        }
    };
    private ScanInfoStorage mScanInfoStorage;
    private View progressGetOption;
    private ScannerInfo scanInfo;
    private ScannerInfo siOrigin;
    private TextView tvScanner2Sided;
    private TextView tvScannerColor;
    private TextView tvScannerDensity;
    private TextView tvScannerGamma;
    private TextView tvScannerName;
    private TextView tvScannerResolution;
    private TextView tvScannerSize;
    private TextView tvScannerSource;
    private View viewScanSettingsAdvance2Sided;
    private View viewScanSettingsSource;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mScanInfoStorage = ScanInfoStorage.getInstance();
        setContentView((int) R.layout.scan_settings_main);
        setActionBar((int) R.string.str_lbl_title_scan_settings, true);
        context = this;
        llScanName = (LinearLayout) findViewById(R.id.llScannerName);
        llScanName.setOnClickListener(mLLScanNameClick);
        llScanSettingsSource = (LinearLayout) findViewById(R.id.llScanSettingsSource);
        llScanSettingsSource.setOnClickListener(mLLScanSettingsSource);
        llScanSettingsSize = (LinearLayout) findViewById(R.id.llScanSettingsSize);
        llScanSettingsSize.setOnClickListener(mLLScanSettingsSize);
        viewScanSettingsSource = findViewById(R.id.viewScanSettingsSource);
        llScanSettingsColor = (LinearLayout) findViewById(R.id.llScanSettingsColor);
        llScanSettingsColor.setOnClickListener(mLLScanSettingsColor);
        llScanSettingsResolution = (LinearLayout) findViewById(R.id.llScanSettingsResolution);
        llScanSettingsResolution.setOnClickListener(mLLScanSettingsResolution);
        tvScannerName = (TextView) findViewById(R.id.tvScannerName);
        tvScannerSource = (TextView) findViewById(R.id.tvScannerSource);
        tvScannerSize = (TextView) findViewById(R.id.tvScannerSize);
        tvScannerColor = (TextView) findViewById(R.id.tvScannerColor);
        tvScannerResolution = (TextView) findViewById(R.id.tvScannerResolution);
        progressGetOption = findViewById(R.id.progressGetOption);
        llScanSettingsAdvance2Sided = (LinearLayout) findViewById(R.id.llScanSettingsAdvance2Sided);
        llScanSettingsAdvance2Sided.setOnClickListener(mLLScanSettings2Sided);
        viewScanSettingsAdvance2Sided = findViewById(R.id.viewScanSettingsAdvance2Sided);
        llScanSettingsDensity = (LinearLayout) findViewById(R.id.llScanSettingsDensity);
        llScanSettingsDensity.setOnClickListener(mLLScanSettingsDensity);
        mBrightnessSeparator = findViewById(R.id.brightnessSeparator);
        llScanSettingsGamma = (LinearLayout) findViewById(R.id.llScanSettingsGamma);
        llScanSettingsGamma.setOnClickListener(mLLScanSettingsGamma);
        tvScanner2Sided = (TextView) findViewById(R.id.tvScanner2Sided);
        tvScannerDensity = (TextView) findViewById(R.id.tvScannerDensity);
        tvScannerGamma = (TextView) findViewById(R.id.tvScannerGamma);
        Intent intent = getIntent();
        mHideResolution = intent.getBooleanExtra(HIDE_RESOLUTION, false);
        if (mHideResolution) {
            llScanSettingsResolution.setVisibility(8);
        }
        mHideColorMode = intent.getBooleanExtra(HIDE_COLOR_MODE, false);
        if (mHideColorMode) {
            llScanSettingsColor.setVisibility(8);
        }
        mExternalIntentData = (SharedParamScan) intent.getSerializableExtra(PARAM_KEY_EXTERNAL_DATA);
        scanInfo = new ScannerInfo();
        getScannerSavedSettings();
        if (this.scanInfo.getIp().equals("")) {
            setBlankSettings();
            showNewSelectedScannerInfo();
        } else {
            setResolutionClickable();
            setColorModeClickable();
        }
        siOrigin = new ScannerInfo(this.scanInfo);
        updateScannerInfoIfEscIVersionChanged(getIntent());
    }

    private void updateScannerInfoIfEscIVersionChanged(Intent intent) {
        ScannerPropertyWrapper scannerPropertyWrapper = SettingActivityParams.getScannerPropertyWrapper(intent);
        if (scannerPropertyWrapper != null) {
            if (!scannerPropertyWrapper.isPrinterSet()) {
                resetSettings();
                return;
            }
            ScannerInfo i1ScannerInfo = scannerPropertyWrapper.getI1ScannerInfo();
            if (i1ScannerInfo != null) {
                isNeedSaveSupportedOptions = true;
                updateSelectedScannerInfo(i1ScannerInfo, scannerPropertyWrapper.getSimpleApSsid());
            }
        }
    }

    private void updatePrinterIcon() {
        if (this.scanInfo.isModelNameEmpty()) {
            ((ImageView) findViewById(R.id.imageViewPrinterLocation)).setVisibility(8);
            return;
        }
        ((ImageView) findViewById(R.id.imageViewPrinterLocation)).setVisibility(0);
        switch (this.scanInfo.getLocation()) {
            case 1:
                ((ImageView) findViewById(R.id.imageViewPrinterLocation)).setImageResource(R.C2136drawable.print_local);
                return;
            case 2:
                ((ImageView) findViewById(R.id.imageViewPrinterLocation)).setImageResource(R.C2136drawable.print_remote);
                return;
            case 3:
                ((ImageView) findViewById(R.id.imageViewPrinterLocation)).setImageResource(R.C2136drawable.print_ip);
                return;
            default:
                return;
        }
    }

    private void getScannerSavedSettings() {
        EPLog.m313i("epson.scan.activity.ScanSettingsActivity", "Start get saved option");
        loadRefScannerName();
        loadRefScannerSourceSetting();
        loadRefScannerColorSetting();
        loadRefScannerResolutionSetting();
        loadRefScannerBasicOptions();
        scanInfo.setSizeValue(Constants.PaperName.EPS_MSID_UNKNOWN.getCode());
        mScanInfoStorage.loadScanSetting(this, scanInfo);
        int[] iArr = new int[64];
        for (int i = 0; i < 64; i++) {
            Context context2 = context;
            iArr[i] = Utils.getPrefInt(context2, epson.common.Constants.SCAN_REFS_SUPPORTED_OPTIONS, epson.common.Constants.SCAN_REFS_OPTIONS_RESOLUTION_ + i);
        }
        scanInfo.setSupportedResolutionList(iArr);
        autoConnectSsid = WiFiDirectManager.getConnectInfo(this, WiFiDirectManager.DEVICE_TYPE_SCANNER);
        updateScanner2SidedSetting(this.scanInfo.getTwoSidedScanningName(), scanInfo.getSupportedAdfDuplex());
        tvScannerDensity.setText(String.valueOf(255 - scanInfo.getDensityValue()));
        tvScannerGamma.setText(this.scanInfo.getGammaName());
    }

    @Deprecated
    private void loadRefScannerName() {
        mScanInfoStorage.loadScannerConnectivityInfo(this, scanInfo);
        displayScannerName();
        updatePrinterIcon();
    }

    private void displayScannerName() {
        String ipPrinterUserName;
        if (this.scanInfo.getLocation() != 3 || (ipPrinterUserName = new EPPrinterManager(this).getIpPrinterUserName(this.scanInfo.getScannerId())) == null) {
            String modelName = scanInfo.getModelName();
            if (modelName == null || modelName.isEmpty()) {
                tvScannerName.setText(R.string.str_lbl_title_scan);
            } else {
                tvScannerName.setText(modelName);
            }
        } else {
            tvScannerName.setText(ipPrinterUserName);
        }
    }

    @Deprecated
    private void loadRefScannerSourceSetting() {
        mScanInfoStorage.loadSupportedAdfSize(this, scanInfo);
        mScanInfoStorage.loadReadUnitValue(this, scanInfo);
        tvScannerSource.setText(getString(this.scanInfo.getSourceName()));
        if (this.scanInfo.getSupportedAdfHeight() == 0 && scanInfo.getSupportedAdfWidth() == 0) {
            llScanSettingsSource.setVisibility(8);
            viewScanSettingsSource.setVisibility(8);
            return;
        }
        llScanSettingsSource.setVisibility(0);
        viewScanSettingsSource.setVisibility(0);
    }

    @Deprecated
    private void loadRefScannerColorSetting() {
        mScanInfoStorage.loadColorValue(this, scanInfo);
        tvScannerColor.setText(getString(this.scanInfo.getColorName()));
    }

    @Deprecated
    private void loadRefScannerResolutionSetting() {
        mScanInfoStorage.loadResolution(this, scanInfo);
        tvScannerResolution.setText(this.scanInfo.getResolutionName(this));
    }

    private void loadRefScannerBasicOptions() {
        mScanInfoStorage.loadSupportedValue(this, scanInfo);
    }

    private void startInputUnitSettingActivity() {
        if (this.scanInfo.getSupportedAdfWidth() > 0 && scanInfo.getSupportedAdfHeight() > 0) {
            startActivityForResult(InputUnitSettingActivity.getStartIntent(this, scanInfo.getSourceValue(), scanInfo.getAdfPaperGuide()), 4);
        }
    }

    private void startDetailSettingScreenForResult(String str, int i, int i2, int i3) {
        Intent intent = new Intent(this, ScanSettingsDetailActivity.class);
        intent.putExtra(str, i);
        intent.putExtra(SCAN_SETTINGS_LAST_VALUE, i2);
        intent.putExtra(epson.common.Constants.SCAN_REFS_OPTIONS_ADF_WIDTH, scanInfo.getSupportedAdfWidth());
        intent.putExtra(epson.common.Constants.SCAN_REFS_OPTIONS_ADF_HEIGHT, scanInfo.getSupportedAdfHeight());
        intent.putExtra(SCAN_SUPPORTED_RESOLUTION_LIST, scanInfo.getSupportedResolutionList());
        intent.putExtra(epson.common.Constants.SCAN_REFS_SETTINGS_ADF_PAPER_GUIDE, scanInfo.getAdfPaperGuide());
        int[] iArr = new int[0];
        switch (this.scanInfo.getSourceValue()) {
            case 0:
                iArr = ScanSizeHelper.getSupportedScanSizeList(this.scanInfo.getSourceValue(), 0, scanInfo.getSupportedBasicWidth(), scanInfo.getSupportedBasicHeight(), scanInfo.getSupportedBasicResolution());
                break;
            case 1:
                iArr = ScanSizeHelper.getSupportedScanSizeList(this.scanInfo.getSourceValue(), scanInfo.getTwoSidedScanningValue(), scanInfo.getSupportedAdfWidth(), scanInfo.getSupportedAdfHeight(), scanInfo.getSupportedBasicResolution());
                break;
        }
        intent.putExtra(SCAN_SUPPORTED_SIZE_LIST, iArr);
        intent.setAction("android.intent.action.VIEW");
        startActivityForResult(intent, i3);
    }

    private void goToSettingAdvanceDensityScreen() {
        startActivityForResult(ScanSettingsAdvanceDensityActivity.getStartIntent(this, scanInfo.getDensityValue(), scanInfo.isDensityStatus()), 8);
    }

    private void goToSettingAdvanceDetailsScreen(String str, int i, int i2, int i3) {
        Intent intent = new Intent(this, ScanSettingsAdvanceDetailActivity.class);
        intent.putExtra(str, i);
        intent.putExtra(SCAN_SETTINGS_LAST_VALUE, i2);
        intent.putExtra(epson.common.Constants.SCAN_REFS_OPTIONS_SUPPORTED_ADF_DUPLEX, scanInfo.getSupportedAdfDuplex());
        int i4 = 1;
        if (!this.scanInfo.isAdfDuplexRotaitonYes()) {
            i4 = 0;
        }
        intent.putExtra(epson.common.Constants.SCAN_REFS_SETTINGS_ROTATE, i4);
        intent.setAction("android.intent.action.VIEW");
        startActivityForResult(intent, i3);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1) {
            onScannerSelectResult(i2, intent);
        } else if (i != 100) {
            if (i2 == -1) {
                Bundle extras = intent.getExtras();
                boolean z = false;
                switch (i) {
                    case 4:
                        scanInfo.setSourceValue(InputUnitSettingActivity.getInputUnit(intent));
                        int adfAlignment = InputUnitSettingActivity.getAdfAlignment(intent);
                        if (adfAlignment >= 0) {
                            scanInfo.setAdfPaperGuide(adfAlignment);
                        }
                        tvScannerSource.setText(this.scanInfo.getSourceName());
                        if (this.scanInfo.getSourceValue() == 0) {
                            scanInfo.setTwoSidedScanningValue(0);
                        }
                        updateScanner2SidedSetting(this.scanInfo.getTwoSidedScanningName(), scanInfo.getSupportedAdfDuplex());
                        scanInfo.setSizeValue(Constants.PaperName.EPS_MSID_UNKNOWN.getCode());
                        tvScannerSize.setText(getString(ScanSizeHelper.getScanSizeName(this.scanInfo.getSizeValue())));
                        break;
                    case 5:
                        scanInfo.setColorValue(extras.getInt(epson.common.Constants.SCAN_REFS_SETTINGS_COLOR));
                        tvScannerColor.setText(getString(this.scanInfo.getColorName()));
                        break;
                    case 6:
                        scanInfo.setResolutionValue(extras.getInt(epson.common.Constants.SCAN_REFS_SETTINGS_RESOLUTION));
                        tvScannerResolution.setText(this.scanInfo.getResolutionName(this));
                        break;
                    case 7:
                        scanInfo.setTwoSidedScanningValue(extras.getInt(epson.common.Constants.SCAN_REFS_SETTINGS_2SIDED));
                        String string = extras.getString(epson.common.Constants.SCAN_REFS_SETTINGS_2SIDED_NAME);
                        int i3 = R.string.str_yes;
                        if (!string.equals(getString(R.string.str_yes))) {
                            i3 = R.string.str_no;
                        }
                        tvScanner2Sided.setText(i3);
                        ScannerInfo scannerInfo = scanInfo;
                        if (extras.getInt(epson.common.Constants.SCAN_REFS_SETTINGS_ROTATE) == 1) {
                            z = true;
                        }
                        scannerInfo.setAdfDuplexRotaitonYes(z);
                        if (this.scanInfo.getTwoSidedScanningValue() == 1) {
                            scanInfo.setSourceValue(1);
                            tvScannerSource.setText(getString(this.scanInfo.getSourceName()));
                        }
                        scanInfo.setSizeValue(Constants.PaperName.EPS_MSID_UNKNOWN.getCode());
                        tvScannerSize.setText(getString(ScanSizeHelper.getScanSizeName(this.scanInfo.getSizeValue())));
                        break;
                    case 8:
                        scanInfo.setDensityValue(ScanSettingsAdvanceDensityActivity.getDensityFromReturnIntent(intent));
                        scanInfo.setDensityStatus(ScanSettingsAdvanceDensityActivity.getChangeAble(intent));
                        tvScannerDensity.setText(String.valueOf(255 - scanInfo.getDensityValue()));
                        break;
                    case 9:
                        scanInfo.setGammaValue(extras.getInt(epson.common.Constants.SCAN_REFS_SETTINGS_GAMMA));
                        String string2 = extras.getString(epson.common.Constants.SCAN_REFS_SETTINGS_GAMMA_NAME);
                        int i4 = R.string.str_settings_gamma_10;
                        if (!string2.equals(getString(R.string.str_settings_gamma_10))) {
                            i4 = R.string.str_settings_gamma_18;
                        }
                        tvScannerGamma.setText(i4);
                        break;
                    case 11:
                        scanInfo.setSizeValue(extras.getInt(epson.common.Constants.SCAN_REFS_SCANNER_CHOSEN_SIZE));
                        tvScannerSize.setText(getString(ScanSizeHelper.getScanSizeName(this.scanInfo.getSizeValue())));
                        break;
                }
            }
            EPLog.m313i(getLocalClassName(), "back to settings");
        } else if (i2 == 1) {
            onScannerSelectResult(-1, intent);
        } else {
            setResult(i2, intent);
            finish();
        }
    }

    private void onScannerSelectResult(int i, Intent intent) {
        if (i == -1) {
            ScannerPropertyWrapper scannerPropertyWrapper = SettingActivityParams.getScannerPropertyWrapper(intent);
            if (scannerPropertyWrapper != null) {
                if (scannerPropertyWrapper.getEscIVersion() != 2) {
                    ScannerInfo i1ScannerInfo = scannerPropertyWrapper.getI1ScannerInfo();
                    if (i1ScannerInfo != null) {
                        updateSelectedScannerInfo(i1ScannerInfo, scannerPropertyWrapper.getSimpleApSsid());
                    }
                } else if (SettingActivityParams.returnIfEscIVersionChanged(getIntent())) {
                    setResult(1, SettingActivityParams.getSettingReturnIntent(scannerPropertyWrapper));
                    finish();
                } else {
                    startActivityForResult(SettingActivityParams.getChangeSettingActivityStartIntent(this, scannerPropertyWrapper, true, mHideColorMode, mHideResolution, mExternalIntentData), 100);
                }
            }
        } else {
            updateIpAddressPrinter();
        }
    }

    private void updateIpAddressPrinter() {
        if (this.scanInfo.getLocation() == 3 && !new EPPrinterManager(this).doseContainScannerIdInIpPrinterList(this.scanInfo.getScannerId())) {
            resetSettings();
        }
    }

    private void updateSelectedScannerInfo(ScannerInfo scannerInfo, String str) {
        isNeedSaveSupportedOptions = true;
        scanInfo.setScannerId(scannerInfo.getScannerId());
        scanInfo.setIp(scannerInfo.getIp());
        scanInfo.setLocation(scannerInfo.getLocation());
        scanInfo.setModelName(scannerInfo.getModelName());
        autoConnectSsid = str;
        displayScannerName();
        updatePrinterIcon();
        scanInfo.setSupportedOptions(new int[7]);
        scanInfo.setSupportedResolutionList(new int[64]);
        ScanSettingHelper.updateNewSelectedScannerSupportedOptions(this.scanInfo);
        setBlankSettings();
        scanInfo.setSupportedOptions(scannerInfo.getSupportedOptions());
        scanInfo.setSupportedResolutionList(scannerInfo.getSupportedResolutionList());
        ScanSettingHelper.updateNewSelectedScannerSupportedOptions(this.scanInfo);
        ScanSettingHelper.setDefaultSettings3(ScanCommonParams.load(this), scanInfo);
        showNewSelectedScannerInfo();
        if (!this.scanInfo.getIp().equals("")) {
            setResolutionClickable();
            setColorModeClickable();
            llScanSettingsDensity.setClickable(true);
            llScanSettingsGamma.setClickable(true);
        }
    }

    private void setBlankSettings() {
        scanInfo.setSourceValue(0);
        scanInfo.setSupportedAdfHeight(0);
        scanInfo.setSupportedAdfWidth(0);
        scanInfo.setSupportedAdfDuplex(0);
        scanInfo.setTwoSidedScanningValue(0);
        scanInfo.setResolutionValue(150);
        scanInfo.setSizeValue(Constants.PaperName.EPS_MSID_UNKNOWN.getCode());
        scanInfo.setColorValue(1);
        scanInfo.setDensityStatus(false);
        scanInfo.setDensityValue(128);
        scanInfo.setGammaValue(1);
        llScanSettingsResolution.setClickable(false);
        llScanSettingsColor.setClickable(false);
        llScanSettingsDensity.setClickable(false);
        llScanSettingsGamma.setClickable(false);
    }

    private void showNewSelectedScannerInfo() {
        displayNewDocumentSourceSetting();
        tvScannerSize.setText(getString(ScanSizeHelper.getScanSizeName(this.scanInfo.getSizeValue())));
        tvScannerColor.setText(getString(this.scanInfo.getColorName()));
        tvScannerResolution.setText(this.scanInfo.getResolutionName(this));
        tvScannerDensity.setText(String.valueOf(255 - scanInfo.getDensityValue()));
        tvScannerGamma.setText(this.scanInfo.getGammaName());
    }

    private void displayNewDocumentSourceSetting() {
        if (this.scanInfo.getSupportedAdfHeight() == 0 && scanInfo.getSupportedAdfWidth() == 0) {
            llScanSettingsSource.setVisibility(8);
            viewScanSettingsSource.setVisibility(8);
        } else {
            llScanSettingsSource.setVisibility(0);
            viewScanSettingsSource.setVisibility(0);
            tvScannerSource.setText(getString(this.scanInfo.getSourceName()));
        }
        updateScanner2SidedSetting(this.scanInfo.getTwoSidedScanningName(), scanInfo.getSupportedAdfDuplex());
    }

    private void updateScanner2SidedSetting(int i, int i2) {
        tvScanner2Sided.setText(i);
        if (i2 != 1) {
            llScanSettingsAdvance2Sided.setVisibility(8);
            viewScanSettingsAdvance2Sided.setVisibility(8);
            return;
        }
        llScanSettingsAdvance2Sided.setVisibility(0);
        viewScanSettingsAdvance2Sided.setVisibility(0);
    }

    private void setResolutionClickable() {
        if (!mHideResolution) {
            llScanSettingsResolution.setClickable(true);
        }
    }

    private void setColorModeClickable() {
        if (!mHideColorMode) {
            llScanSettingsColor.setClickable(true);
        }
    }

    public void onBackPressed() {
        new EPPrinterManager(this).rollbackIPPrinterInfo();
        setResult(0);
        finish();
    }

    private void onSettingDone() {
        if (this.progressGetOption.getVisibility() == 8) {
            if (!this.scanInfo.isReSearch() && !this.scanInfo.getIp().equals("")) {
                scanInfo.setReSearch(true);
            }
            if (this.isNeedSaveSupportedOptions) {
                ScanSettingHelper.saveSupportedOptionToRef(this.context, scanInfo);
                String str = autoConnectSsid;
                if (str == null || str.length() <= 0) {
                    WiFiDirectManager.resetConnectInfo(this, WiFiDirectManager.DEVICE_TYPE_SCANNER);
                } else {
                    WiFiDirectManager.setConnectInfo(this, autoConnectSsid, WiFiDirectManager.DEVICE_TYPE_SCANNER, scanInfo.getModelName());
                }
            }
            boolean z = false;
            isNeedSaveSupportedOptions = false;
            if (this.scanInfo.getModelName().equals(this.siOrigin.getModelName()) && scanInfo.getSourceValue() == siOrigin.getSourceValue() && scanInfo.getColorValue() == siOrigin.getColorValue() && scanInfo.getResolutionValue() == siOrigin.getResolutionValue() && scanInfo.getTwoSidedScanningValue() == siOrigin.getTwoSidedScanningValue() && scanInfo.isAdfDuplexRotaitonYes() == siOrigin.isAdfDuplexRotaitonYes() && scanInfo.getAdfPaperGuide() == siOrigin.getAdfPaperGuide() && scanInfo.getDensityValue() == siOrigin.getDensityValue() && scanInfo.getGammaValue() == siOrigin.getGammaValue()) {
                z = true;
            }
            ScanInfoStorage.saveEscIVersion(this, 1);
            ScanSettingHelper.saveAllUserSettingsToRef(this.context, scanInfo);
            backToScanScreen(z);
        }
    }

    private void backToScanScreen(boolean z) {
        new EPPrinterManager(this).commitIPPrinterInfo();
        Intent intent = getIntent();
        intent.putExtra(epson.common.Constants.ACT_RESULT, epson.common.Constants.ACT_RESULT_SAVE);
        intent.putExtra(IS_NEW_SAVE, true);
        intent.putExtra(NO_CLEAR_RESULT, z);
        setResult(-1, intent);
        finish();
    }

    protected void onStop() {
        super.onStop();
        EPLog.m313i(getLocalClassName(), "scannsetting screen, onStop");
        progressGetOption.setVisibility(8);
        AlertDialog alertDialog = errorDialog;
        if (alertDialog != null && alertDialog.isShowing()) {
            errorDialog.dismiss();
        }
    }

    private void resetSettings() {
        scanInfo.setScannerId("");
        scanInfo.setIp("");
        scanInfo.setModelName("");
        scanInfo.setLocation(1);
        autoConnectSsid = "";
        setBlankSettings();
        showNewSelectedScannerInfo();
        displayScannerName();
        updatePrinterIcon();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.C2138menu.menu_done, menu);
        return super.onCreateOptionsMenu(menu);
    }

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