package epson.maintain.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import epson.maintain.FirmwareManager;
import epson.print.MyPrinter;
import epson.print.R;

public class FirmwareUpdateActivity extends Activity implements FirmwareManager.FWUpdateListener {
    static final String PRINTER_IP = "PRINTER_IP";
    static final String PRINTER_MODEL_NAME = "PRINTER_MODEL_NAME";
    static final int RECONNECT_SIMPLE_AP = 100;
    private static final int REQUEST_CODE_EULA = 101;
    Button mCancelButton;
    AlertDialog mDialog = null;
    FirmwareManager mFWManager;
    final DialogInterface.OnClickListener mGoBackOnClickListenner = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            goBack();
        }
    };
    boolean mInterrupted = false;
    TextView mMessageText;
    String mPrinterIP;
    ProgressBar mProgress;
    boolean mReconnectingSimpleAP = false;
    boolean mShouldReconnectSimpleAP = false;
    TextView mTitleText;
    private boolean mWaitEulaCheckActivityEnd;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.fwupdate_progress);
        mFWManager = new FirmwareManager(this, MyPrinter.getCurPrinter(this));
        mMessageText = (TextView) findViewById(R.id.messageText);
        mTitleText = (TextView) findViewById(R.id.messageTitle);
        mProgress = (ProgressBar) findViewById(R.id.horizontalProgressBar);
        mCancelButton = (Button) findViewById(R.id.cancelButton);
        mWaitEulaCheckActivityEnd = false;
        MyPrinter curPrinter = MyPrinter.getCurPrinter(this);
        Intent intent = getIntent();
        mPrinterIP = intent.getStringExtra("PRINTER_IP");
        if (mPrinterIP == null) {
            mPrinterIP = curPrinter.getIp();
        }
        mFWManager = new FirmwareManager(this, new MyPrinter(intent.getStringExtra(PRINTER_MODEL_NAME), mPrinterIP, curPrinter.getPrinterId(), curPrinter.getSerialNo()));
        mCancelButton.setText(R.string.str_cancel);
        mCancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                mFWManager.cancelProc();
            }
        });
        setMessage(getResources().getString(R.string.fw_checking_version), (String) null);
        setProcCancelable(false);
        if (bundle == null) {
            mFWManager.startVersionCheck(this);
        } else {
            mInterrupted = true;
        }
    }

    protected void onResume() {
        super.onResume();
        Log.d(toString(), String.format("onResume mReconnectingSimpleAP=%b mInterrupted=%b", new Object[]{Boolean.valueOf(mReconnectingSimpleAP), Boolean.valueOf(mInterrupted)}));
        if (mInterrupted) {
            showAlertDialog(getResources().getString(R.string.fw_error_interrupted), getResources().getString(R.string.fw_try_again), mGoBackOnClickListenner, (DialogInterface.OnClickListener) null);
        }
    }

    protected void onPause() {
        super.onPause();
        AlertDialog alertDialog = mDialog;
        if ((alertDialog == null || !alertDialog.isShowing()) && !mWaitEulaCheckActivityEnd && !mReconnectingSimpleAP) {
            mInterrupted = true;
            mFWManager.interruptProc();
        }
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        Log.d(toString(), String.format("onAcitivityReuslt requestCode=%d resultCode=%d", new Object[]{Integer.valueOf(i), Integer.valueOf(i2)}));
        if (i == 100) {
            mShouldReconnectSimpleAP = false;
            mReconnectingSimpleAP = false;
            if (i2 != -1 || !WiFiDirectManager.isSimpleAP(this)) {
                fwManagerDidEndProc(FirmwareManager.FWUpdateProcResult.Fail, FirmwareManager.FWUpdateStep.ReadyUpdate);
            } else {
                mFWManager.startFWUpdate(this);
            }
        } else if (i == 101) {
            mWaitEulaCheckActivityEnd = false;
            if (i2 != -1) {
                goBack();
            } else if (!mInterrupted) {
                callStartDownload();
            }
        }
    }

    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        if (keyEvent.getAction() == 0 && keyEvent.getKeyCode() == 4) {
            return true;
        }
        return super.dispatchKeyEvent(keyEvent);
    }

    public java.lang.String[] getEndProcTitleAndMessage(epson.maintain.FirmwareManager.FWUpdateProcResult r6, epson.maintain.FirmwareManager.FWUpdateStep r7) {
        throw new UnsupportedOperationException("Method not decompiled: epson.maintain.activity.getEndProcTitleAndMessage(epson.maintain.FirmwareManager$FWUpdateProcResult, epson.maintain.FirmwareManager$FWUpdateStep):java.lang.String[]");
    }

    public void fwManagerProcDoing(int i) {
        mProgress.setProgress(i);
    }

    public void fwManagerDidEndProc(FirmwareManager.FWUpdateProcResult fWUpdateProcResult, FirmwareManager.FWUpdateStep fWUpdateStep) {
        int i;
        String[] endProcTitleAndMessage = getEndProcTitleAndMessage(fWUpdateProcResult, fWUpdateStep);
        String str = endProcTitleAndMessage[0];
        String str2 = endProcTitleAndMessage[1];
        getWindow().clearFlags(128);
        if (fWUpdateProcResult == FirmwareManager.FWUpdateProcResult.Success && (i = C20104.$SwitchMap$epson$maintain$FirmwareManager$FWUpdateStep[fWUpdateStep.ordinal()]) != 5) {
            switch (i) {
                case 1:
                    showAlertDialog(str, str2, getString(R.string.str_yes), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            startEulaActivity();
                        }
                    }, getString(R.string.str_no), mGoBackOnClickListenner);
                    return;
                case 2:
                    setMessage(getResources().getString(R.string.fw_transferring), (String) null);
                    getWindow().addFlags(128);
                    if (mShouldReconnectSimpleAP) {
                        mReconnectingSimpleAP = true;
                        if (!WiFiDirectManager.reconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, 100)) {
                            mReconnectingSimpleAP = false;
                            fwManagerDidEndProc(FirmwareManager.FWUpdateProcResult.Fail, FirmwareManager.FWUpdateStep.ReadyUpdate);
                            return;
                        }
                        return;
                    }
                    mFWManager.startFWUpdate(this);
                    return;
                case 3:
                    if (WiFiDirectManager.isSimpleAP(this)) {
                        new DisconnectAndStartGetUpdateInfTask().execute(new Void[0]);
                        return;
                    } else {
                        mFWManager.startGetUpdateInf(this);
                        return;
                    }
                default:
                    String obj = toString();
                    Log.d(obj, "Not Reachable:" + fWUpdateStep);
                    break;
            }
        }
        if (str != null || str2 != null) {
            showAlertDialog(str, str2, mGoBackOnClickListenner, (DialogInterface.OnClickListener) null);
        } else if (fWUpdateProcResult != FirmwareManager.FWUpdateProcResult.Interrupted) {
            goBack();
        }
    }

    private void startEulaActivity() {
        mWaitEulaCheckActivityEnd = true;
        startActivityForResult(new Intent(this, CheckEulaActivity.class), 101);
    }

    private void callStartDownload() {
        getWindow().addFlags(128);
        setProgressVisibility(true);
        setProcCancelable(true);
        setMessage(getResources().getString(R.string.fw_downloading), (String) null);
        mFWManager.startFWDownload(this);
    }

    private void setProcCancelable(boolean z) {
        mCancelButton.setVisibility(z ? 0 : 8);
    }

    private void setProgressVisibility(boolean z) {
        mProgress.setVisibility(z ? 0 : 8);
    }

    private void setMessage(String str, String str2) {
        if (str != null) {
            mTitleText.setText(str);
            mTitleText.setVisibility(View.VISIBLE);
        } else {
            mTitleText.setVisibility(View.GONE);
        }
        if (str2 != null) {
            mMessageText.setText(str2);
            mMessageText.setVisibility(View.VISIBLE);
            return;
        }
        mMessageText.setVisibility(View.GONE);
    }

    private void showAlertDialog(String str, String str2, DialogInterface.OnClickListener onClickListener, DialogInterface.OnClickListener onClickListener2) {
        String str3 = null;
        String string = onClickListener != null ? getString(R.string.str_ok) : null;
        if (onClickListener2 != null) {
            str3 = getString(R.string.str_cancel);
        }
        showAlertDialog(str, str2, string, onClickListener, str3, onClickListener2);
    }

    private void showAlertDialog(String str, String str2, String str3, DialogInterface.OnClickListener onClickListener, String str4, DialogInterface.OnClickListener onClickListener2) {
        dismissAlertDialog();
        mDialog = new AlertDialog.Builder(this).setTitle(str).setMessage(str2).setCancelable(false).setPositiveButton(str3, onClickListener).setNegativeButton(str4, onClickListener2).create();
        mDialog.show();
    }

    private void dismissAlertDialog() {
        AlertDialog alertDialog = mDialog;
        if (alertDialog != null && alertDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    private void goBack() {
        FirmwareManager.removeSavedFile();
        dismissAlertDialog();
        finish();
    }

    class DisconnectAndStartGetUpdateInfTask extends AsyncTask<Void, Void, Boolean> {
        DisconnectAndStartGetUpdateInfTask() {
        }

        protected Boolean doInBackground(Void... voidArr) {
            FirmwareUpdateActivity firmwareUpdateActivity = FirmwareUpdateActivity.this;
            return Boolean.valueOf(MaintainActivity.disableSimpleApAndWait(firmwareUpdateActivity, mPrinterIP));
        }

        protected void onPostExecute(Boolean bool) {
            super.onPostExecute(bool);
            if (bool.booleanValue()) {
                FirmwareUpdateActivity firmwareUpdateActivity = FirmwareUpdateActivity.this;
                mShouldReconnectSimpleAP = true;
                mFWManager.startGetUpdateInf(FirmwareUpdateActivity.this);
                return;
            }
            fwManagerDidEndProc(FirmwareManager.FWUpdateProcResult.Fail, FirmwareManager.FWUpdateStep.GetUpdateInf);
        }
    }
}
