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.R;
import epson.print.MyPrinter;

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) {
            FirmwareUpdateActivity.this.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) {
                FirmwareUpdateActivity.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);
    }

    /* access modifiers changed from: package-private */
    /* JADX WARNING: Can't fix incorrect switch cases order */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public java.lang.String[] getEndProcTitleAndMessage(epson.maintain.FirmwareManager.FWUpdateProcResult r6, epson.maintain.FirmwareManager.FWUpdateStep r7) {
        /*
            r5 = this;
            int[] r0 = epson.maintain.activity.FirmwareUpdateActivity.C20104.$SwitchMap$epson$maintain$FirmwareManager$FWUpdateProcResult
            int r6 = r6.ordinal()
            r6 = r0[r6]
            r0 = 2131624842(0x7f0e038a, float:1.8876875E38)
            r1 = 0
            r2 = 0
            r3 = 2
            r4 = 1
            switch(r6) {
                case 1: goto L_0x011e;
                case 2: goto L_0x0101;
                case 3: goto L_0x00be;
                case 4: goto L_0x0073;
                case 5: goto L_0x0055;
                case 6: goto L_0x003a;
                case 7: goto L_0x001f;
                case 8: goto L_0x0014;
                default: goto L_0x0012;
            }
        L_0x0012:
            goto L_0x0130
        L_0x0014:
            java.lang.String r6 = r5.toString()
            java.lang.String r7 = "MSG_CANCELED"
            android.util.Log.d(r6, r7)
            goto L_0x0130
        L_0x001f:
            java.lang.String[] r6 = new java.lang.String[r3]
            android.content.res.Resources r7 = r5.getResources()
            java.lang.String r7 = r7.getString(r0)
            r6[r2] = r7
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624826(0x7f0e037a, float:1.8876843E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x003a:
            java.lang.String[] r6 = new java.lang.String[r3]
            android.content.res.Resources r7 = r5.getResources()
            java.lang.String r7 = r7.getString(r0)
            r6[r2] = r7
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624831(0x7f0e037f, float:1.8876853E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x0055:
            java.lang.String[] r6 = new java.lang.String[r3]
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624832(0x7f0e0380, float:1.8876855E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r2] = r7
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624833(0x7f0e0381, float:1.8876857E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x0073:
            int[] r6 = epson.maintain.activity.FirmwareUpdateActivity.C20104.$SwitchMap$epson$maintain$FirmwareManager$FWUpdateStep
            int r7 = r7.ordinal()
            r6 = r6[r7]
            if (r6 == r4) goto L_0x00a0
            r7 = 5
            if (r6 == r7) goto L_0x0082
            goto L_0x0130
        L_0x0082:
            java.lang.String[] r6 = new java.lang.String[r3]
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624839(0x7f0e0387, float:1.887687E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r2] = r7
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624840(0x7f0e0388, float:1.8876871E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x00a0:
            java.lang.String[] r6 = new java.lang.String[r3]
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624835(0x7f0e0383, float:1.887686E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r2] = r7
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624836(0x7f0e0384, float:1.8876863E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x00be:
            int[] r6 = epson.maintain.activity.FirmwareUpdateActivity.C20104.$SwitchMap$epson$maintain$FirmwareManager$FWUpdateStep
            int r7 = r7.ordinal()
            r6 = r6[r7]
            switch(r6) {
                case 1: goto L_0x00e4;
                case 2: goto L_0x00e4;
                case 3: goto L_0x00ca;
                case 4: goto L_0x00ca;
                case 5: goto L_0x00ca;
                default: goto L_0x00c9;
            }
        L_0x00c9:
            goto L_0x0130
        L_0x00ca:
            java.lang.String[] r6 = new java.lang.String[r3]
            android.content.res.Resources r7 = r5.getResources()
            r3 = 2131624827(0x7f0e037b, float:1.8876845E38)
            java.lang.String r7 = r7.getString(r3)
            r6[r2] = r7
            android.content.res.Resources r7 = r5.getResources()
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x00e4:
            java.lang.String[] r6 = new java.lang.String[r3]
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624828(0x7f0e037c, float:1.8876847E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r2] = r7
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624829(0x7f0e037d, float:1.8876849E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x0101:
            java.lang.String[] r6 = new java.lang.String[r3]
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624837(0x7f0e0385, float:1.8876865E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r2] = r7
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624838(0x7f0e0386, float:1.8876867E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x011e:
            java.lang.String[] r6 = new java.lang.String[r3]
            r6[r2] = r1
            android.content.res.Resources r7 = r5.getResources()
            r0 = 2131624834(0x7f0e0382, float:1.8876859E38)
            java.lang.String r7 = r7.getString(r0)
            r6[r4] = r7
            goto L_0x0131
        L_0x0130:
            r6 = r1
        L_0x0131:
            if (r6 != 0) goto L_0x0137
            java.lang.String[] r6 = new java.lang.String[]{r1, r1}
        L_0x0137:
            return r6
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.maintain.activity.FirmwareUpdateActivity.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) {
                            FirmwareUpdateActivity.this.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);
    }

    /* access modifiers changed from: package-private */
    public void setProcCancelable(boolean z) {
        mCancelButton.setVisibility(z ? 0 : 8);
    }

    /* access modifiers changed from: package-private */
    public void setProgressVisibility(boolean z) {
        mProgress.setVisibility(z ? 0 : 8);
    }

    /* access modifiers changed from: package-private */
    public void setMessage(String str, String str2) {
        if (str != null) {
            mTitleText.setText(str);
            mTitleText.setVisibility(0);
        } else {
            mTitleText.setVisibility(8);
        }
        if (str2 != null) {
            mMessageText.setText(str2);
            mMessageText.setVisibility(0);
            return;
        }
        mMessageText.setVisibility(8);
    }

    /* access modifiers changed from: package-private */
    public 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);
    }

    /* access modifiers changed from: package-private */
    public 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();
    }

    /* access modifiers changed from: package-private */
    public void dismissAlertDialog() {
        AlertDialog alertDialog = mDialog;
        if (alertDialog != null && alertDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    /* access modifiers changed from: package-private */
    public 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, firmwareUpdateActivity.mPrinterIP));
        }

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