package com.epson.mobilephone.common.ble;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.epson.iprint.prtlogger.Analytics;
import com.epson.mobilephone.common.EpLog;
import com.epson.mobilephone.common.ble.BleWork;
import com.epson.mobilephone.common.ble.util.BLEUtility;
import com.epson.mobilephone.common.ble.util.ScannedDevice;
import epson.epsonconnectregistration.ActivityECConfiguration;
import epson.maintain.activity.PrinterNotFoundDialogCreator;
import epson.print.ActivityIACommon;

import epson.print.CommonDefine;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.lang.CharEncoding;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

public class BleWorkActivity extends ActivityIACommon {
    private static final int CONNECT_TIME_OUT = 60000;
    private static final int FIRST_DELAY = 10000;
    private static final int FIRST_DELAY_SEC = 10;
    private static final int FIRST_PROGRESS_PER = 90;
    private static final int FIRST_PROGRESS_SEC = 90;
    private static final int REQUEST_CODE_EPSON_CONNECT_REGISTRATION = 212;
    private static final int SECOND_DELAY = 200000;
    private static final int TOTAL_MARGIN_SEC = 30;
    private static final int TOTAL_PROGRESS_SEC = 210;
    int count = 0;
    private boolean isAlive;
    boolean isStopSetUpProgress = false;
    AlertDialog mAlertDialog;
    private BleWork mBleWork = null;
    private CallbackWork mCompletion = null;
    private Context mContext;
    private DeviceAdapter mDeviceAdapter;
    AlertDialog mDialog;
    private CallbackWork mNext = null;
    TextView mPercentView;
    ProbePrinter mProbePrinter;
    ProgressBar mProgressPercent;
    private BLEUtility.BleWorkCallback mSequenceCallbackFailed = null;
    private BLEUtility.BleWorkCallback mSequenceCallbackSuccess = null;
    private Handler mTimeoutHandler;
    private Handler mWifiSearchHandler;
    private Runnable runnableSetUpProgress;
    private Runnable runnableWifiSearch;
    private Handler setUpProgressHandler;

    public interface CallbackWork {
        void call(Object obj);
    }

    protected void onCreate(Bundle bundle) {
        EpLog.m76i();
        super.onCreate(bundle);
        requestWindowFeature(5);
        setContentView((int) R.layout.activity_scan);
        setActionBar((int) R.string.BLE_select_unconfigured_printer_title, true);
        findViewById(R.id.progress_bar).setVisibility(8);
        mContext = this;
        callBackFuncs();
        getWindow().addFlags(128);
        mProbePrinter = ProbePrinter.create().setContext(mContext);
        mBleWork = BleWork.getInstace();
        mBleWork.resetSequence();
    }

    protected void onResume() {
        ArrayList parcelableArrayListExtra;
        super.onResume();
        mProbePrinter.bindEpsonService();
        EpLog.m77i("mSequence = " + BleWork.getInstace().mSequence);
        if (mDeviceAdapter == null && BleWork.getInstace().mSequence == BleWork.jobSequence.UNINITIALIZED && (parcelableArrayListExtra = getIntent().getParcelableArrayListExtra(ScannedDevice.TYPE)) != null && parcelableArrayListExtra.size() > 0) {
            init(parcelableArrayListExtra);
        }
    }

    protected void onPause() {
        super.onPause();
        EpLog.m76i();
    }

    protected void onStart() {
        super.onStart();
        isAlive = true;
    }

    protected void onStop() {
        super.onStop();
        EpLog.m76i();
        isAlive = false;
        getWindow().clearFlags(128);
    }

    protected void onDestroy() {
        super.onDestroy();
        mProbePrinter.unbindEpsonService();
    }

    private void callBackFuncs() {
        mCompletion = new CallbackWork() {
            public void call(Object obj) {
                if (!(obj instanceof Boolean)) {
                    EpLog.m71e(" type mismatch !");
                }
                StringBuilder sb = new StringBuilder();
                sb.append("result = ");
                Boolean bool = (Boolean) obj;
                sb.append(bool.booleanValue());
                EpLog.m77i(sb.toString());
                if (bool.booleanValue()) {
                    BleWorkActivity.this.count = BleWorkActivity.TOTAL_PROGRESS_SEC;
                    EpLog.m77i("count = " + BleWorkActivity.this.count);
                }
                BleWorkActivity.this.stopCallbacks();
                if (BleWorkActivity.mAlertDialog != null && BleWorkActivity.mAlertDialog.isShowing()) {
                    BleWorkActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            EpLog.m77i("dismiss ");
                            BleWorkActivity.mAlertDialog.dismiss();
                            BleWorkActivity.mAlertDialog = null;
                        }
                    });
                }
                BleWorkActivity.mBleWork.resetSequence();
                BleWorkActivity.this.settingResultDailog(bool.booleanValue());
            }
        };
        mNext = new CallbackWork() {
            public void call(Object obj) {
                BleWorkActivity.this.stopWifiSearch();
                Handler unused = BleWorkActivity.mWifiSearchHandler = new Handler();
                BleWorkActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        EpLog.m77i("setSSID");
                        BleWorkActivity.mBleWork.setSSID(new BLEUtility.BleWorkCallback() {
                            public void call(Object obj) {
                                Runnable unused = BleWorkActivity.this.runnableWifiSearch = new Runnable() {
                                    int postCount = 0;

                                    public void run() {
                                        int i = postCount;
                                        if (i >= 2) {
                                            EpLog.m77i("postCount >= 2 ");
                                            BleWorkActivity.mProbePrinter.interruptSearch();
                                            if (BleWorkActivity.mAlertDialog != null) {
                                                BleWorkActivity.mAlertDialog.dismiss();
                                                BleWorkActivity.mAlertDialog = null;
                                            }
                                            if (BleWorkActivity.mCompletion != null) {
                                                BleWorkActivity.mCompletion.call(false);
                                                return;
                                            }
                                            return;
                                        }
                                        if (i == 1) {
                                            BleWorkActivity.mProbePrinter.setMacAddress(BleWorkActivity.mBleWork.getDeviceMacAddress());
                                            BleWorkActivity.mProbePrinter.setBleCallback(BleWorkActivity.mCompletion);
                                            try {
                                                BleWorkActivity.mProbePrinter.search();
                                            } catch (IllegalStateException e) {
                                                EpLog.m71e(e.getMessage());
                                                BleWorkActivity.mProbePrinter.interruptSearch();
                                                if (BleWorkActivity.mAlertDialog != null) {
                                                    BleWorkActivity.mAlertDialog.dismiss();
                                                    BleWorkActivity.mAlertDialog = null;
                                                }
                                                if (BleWorkActivity.mCompletion != null) {
                                                    BleWorkActivity.mCompletion.call(false);
                                                }
                                            }
                                        }
                                        postCount++;
                                        if (BleWorkActivity.mCompletion != null) {
                                            StringBuilder sb = new StringBuilder();
                                            sb.append("postDelayed  postCount = ");
                                            sb.append(this.postCount);
                                            sb.append(" delay ");
                                            sb.append(this.postCount == 1 ? 10000 : BleWorkActivity.SECOND_DELAY);
                                            EpLog.m77i(sb.toString());
                                            BleWorkActivity.mWifiSearchHandler.postDelayed(this, postCount == 1 ? 10000 : 200000);
                                        }
                                    }
                                };
                                BleWorkActivity.mWifiSearchHandler.post(BleWorkActivity.this.runnableWifiSearch);
                            }
                        }, new BLEUtility.BleWorkCallback() {
                            public void call(Object obj) {
                                EpLog.m71e("BleWorkCallback");
                                if (BleWorkActivity.mCompletion != null) {
                                    BleWorkActivity.mCompletion.call(false);
                                }
                            }
                        });
                    }
                });
            }
        };
        mSequenceCallbackSuccess = new BLEUtility.BleWorkCallback() {
            public void call(Object obj) {
                BleWork.jobSequence jobsequence = (BleWork.jobSequence) obj;
                EpLog.m77i("mSequenceCallbackSuccess  " + jobsequence);
                if (jobsequence == BleWork.jobSequence.UNINITIALIZED || jobsequence == BleWork.jobSequence.BEFORE_CHECK_SSID || jobsequence == BleWork.jobSequence.MIB_GET_MACADDRESS || jobsequence == BleWork.jobSequence.AFTER_SETTING_PASSWORD) {
                    BleWorkActivity.this.stopConnectTimeOut();
                }
                if (!(jobsequence == BleWork.jobSequence.UNINITIALIZED || jobsequence == BleWork.jobSequence.BEFORE_CHECK_SSID || !BleWorkActivity.mBleWork.getSSIDPassword().isEmpty() || BleWorkActivity.mAlertDialog == null || ((jobsequence == BleWork.jobSequence.CHECK_SSID && BleWorkActivity.mBleWork.getSecurityType() == 1) || BleWorkActivity.mBleWork.getSecurityType() == 255))) {
                    BleWorkActivity.mAlertDialog.dismiss();
                    BleWorkActivity.mAlertDialog = null;
                    EpLog.m77i("mAlertDialog        dismiss " + jobsequence);
                }
                if (jobsequence == BleWork.jobSequence.CHECK_SSID) {
                    BleWorkActivity.this.setSSIDPassword();
                } else if (jobsequence == BleWork.jobSequence.MIB_GET_SSID_LIST) {
                    BleWorkActivity.this.showSSIDList();
                }
            }
        };
        mSequenceCallbackFailed = new BLEUtility.BleWorkCallback() {
            public void call(Object obj) {
                BleWorkActivity.this.failedDialog((BleWork.jobSequence) obj);
            }
        };
    }

    private void stopCallbacks() {
        stopConnectTimeOut();
        stopWifiSearch();
        isStopSetUpProgress = true;
        stopSetUpProgress();
    }

    private void stopSetUpProgress() {
        Handler handler = setUpProgressHandler;
        if (handler != null) {
            handler.removeCallbacks(this.runnableSetUpProgress);
            setUpProgressHandler = null;
            runnableSetUpProgress = null;
        }
    }

    private void stopWifiSearch() {
        Handler handler = mWifiSearchHandler;
        if (handler != null) {
            handler.removeCallbacks(this.runnableWifiSearch);
            mWifiSearchHandler = null;
            runnableWifiSearch = null;
        }
    }

    private void settingResultDailog(final boolean z) {
        runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder cancelable = new AlertDialog.Builder(BleWorkActivity.mContext).setCancelable(false);
                if (z) {
                    cancelable.setTitle(BleWorkActivity.this.getString(R.string.BLE_success_setup_printer_title));
                    cancelable.setMessage(BleWorkActivity.this.getString(R.string.BLE_success_setup_printer_msg));
                    cancelable.setPositiveButton(BleWorkActivity.this.getString(R.string.ok_button), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Analytics.SetUpPath setUpPath;
                            if (BleWorkActivity.this.getCallingActivity() != null) {
                                BleWorkActivity.this.setResult(-1);
                                setUpPath = Analytics.SetUpPath.Button;
                            } else {
                                setUpPath = Analytics.SetUpPath.Home;
                            }
                            Analytics.sendSetup(BleWorkActivity.mBleWork.getDeviceName(), setUpPath, Analytics.SetUpResult.Success);
                            Intent intent = new Intent(BleWorkActivity.this, ActivityECConfiguration.class);
                            intent.putExtra(ActivityECConfiguration.INTENT_EC_BLE_CONTENT, true);
                            BleWorkActivity.this.startActivityForResult(intent, BleWorkActivity.REQUEST_CODE_EPSON_CONNECT_REGISTRATION);
                        }
                    });
                } else {
                    cancelable.setTitle(BleWorkActivity.this.getString(R.string.BLE_fail_setup_printer_title));
                    cancelable.setMessage(BleWorkActivity.this.getString(R.string.BLE_fail_setup_printer_msg));
                    cancelable.setPositiveButton(BleWorkActivity.this.getString(R.string.BLE_fail_setup_printer_other_method_button), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            BleWorkActivity.this.startActivity(PrinterNotFoundDialogCreator.getStartIntent(BleWorkActivity.this.getApplicationContext()));
                            Analytics.sendSetup(BleWorkActivity.mBleWork.getDeviceName(), BleWorkActivity.this.getCallingActivity() != null ? Analytics.SetUpPath.Button : Analytics.SetUpPath.Home, Analytics.SetUpResult.Fail);
                            BleWorkActivity.this.finish();
                        }
                    }).setNeutralButton(BleWorkActivity.this.getString(R.string.str_btn_close), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Analytics.sendSetup(BleWorkActivity.mBleWork.getDeviceName(), BleWorkActivity.this.getCallingActivity() != null ? Analytics.SetUpPath.Button : Analytics.SetUpPath.Home, Analytics.SetUpResult.Fail);
                            BleWorkActivity.this.finish();
                        }
                    });
                }
                BleWorkActivity.mAlertDialog = cancelable.create();
                BleWorkActivity.mAlertDialog.show();
            }
        });
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        if (i == REQUEST_CODE_EPSON_CONNECT_REGISTRATION) {
            finish();
        }
    }

    private void waitingCommunicationDaialog() {
        if (mAlertDialog != null) {
            EpLog.m77i("dismiss ");
            mAlertDialog.dismiss();
            mAlertDialog = null;
        }
        isStopSetUpProgress = false;
        mAlertDialog = new AlertDialog.Builder(mContext).setCancelable(false).create();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.ble_progress_layout, (ViewGroup) null);
        mAlertDialog.setView(inflate);
        mProgressPercent = (ProgressBar) inflate.findViewById(R.id.progress_percent);
        mProgressPercent.setVisibility(0);
        mPercentView = (TextView) inflate.findViewById(R.id.percent);
        mPercentView.setVisibility(0);
        inflate.findViewById(R.id.space_upper).setVisibility(0);
        mProgressPercent.setProgress(0);
        mProgressPercent.setMax(100);
        mPercentView.setText("0%");
        mAlertDialog.setMessage(getResources().getString(R.string.BLE_wait_setup_complete_msg));
        mAlertDialog.show();
        count = 0;
        stopSetUpProgress();
        setUpProgressHandler = new Handler();
        runnableSetUpProgress = new Runnable() {
            public void run() {
                int i;
                if (BleWorkActivity.this.isStopSetUpProgress) {
                    EpLog.m77i("☆☆runnableSetUpProgress retuen ");
                    return;
                }
                EpLog.m77i("mSequence " + BleWorkActivity.mBleWork.mSequence);
                BleWorkActivity bleWorkActivity = BleWorkActivity.this;
                bleWorkActivity.count = bleWorkActivity.count + 1;
                int i2 = BleWorkActivity.this.count;
                int i3 = BleWorkActivity.TOTAL_PROGRESS_SEC;
                if (i2 > BleWorkActivity.TOTAL_PROGRESS_SEC) {
                    if (BleWorkActivity.mAlertDialog != null) {
                        BleWorkActivity.mAlertDialog.dismiss();
                        BleWorkActivity.mAlertDialog = null;
                    }
                    EpLog.m71e("TIME OUT");
                    BleWorkActivity.mCompletion.call(false);
                    CallbackWork unused = BleWorkActivity.mCompletion = null;
                    return;
                }
                if (BleWorkActivity.this.count <= 90) {
                    i = BleWorkActivity.this.count;
                } else {
                    if (BleWorkActivity.this.count <= BleWorkActivity.TOTAL_PROGRESS_SEC) {
                        i3 = BleWorkActivity.this.count;
                    }
                    i = (int) ((((double) (i3 - 90)) / 12.0d) + 90.0d);
                }
                EpLog.m77i(BleWorkActivity.this.count + "::" + i + " %");
                BleWorkActivity.this.updateProgress(i);
                if (!BleWorkActivity.this.isStopSetUpProgress) {
                    BleWorkActivity.this.setUpProgressHandler.postDelayed(this, 1000);
                }
            }
        };
        setUpProgressHandler.post(this.runnableSetUpProgress);
    }

    private void updateProgress(int i) {
        mProgressPercent.setProgress(i);
        TextView textView = mPercentView;
        textView.setText(i + " %");
        mProgressPercent.invalidate();
        mPercentView.invalidate();
    }

    private void init(ArrayList<ScannedDevice> arrayList) {
        ListView listView = (ListView) findViewById(R.id.list);
        Collections.sort(arrayList, new ScannedDeviceComparator());
        mDeviceAdapter = new DeviceAdapter(this, R.layout.listitem_device, arrayList != null ? arrayList : new ArrayList<>());
        listView.setAdapter(mDeviceAdapter);
        if (arrayList.size() == 1) {
            selectDevice((ScannedDevice) mDeviceAdapter.getItem(0));
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                ScannedDevice scannedDevice = (ScannedDevice) BleWorkActivity.mDeviceAdapter.getItem(i);
                if (scannedDevice != null) {
                    BleWorkActivity.this.selectDevice(scannedDevice);
                }
            }
        });
    }

    public class ScannedDeviceComparator implements Comparator<ScannedDevice> {
        public ScannedDeviceComparator() {
        }

        public int compare(ScannedDevice scannedDevice, ScannedDevice scannedDevice2) {
            if (scannedDevice.getRssi() == scannedDevice2.getRssi()) {
                return 0;
            }
            return scannedDevice.getRssi() < scannedDevice2.getRssi() ? 1 : -1;
        }
    }

    private void selectDevice(@NonNull ScannedDevice scannedDevice) {
        String displayName = scannedDevice.getDisplayName();
        BleScanWork.getInstace().stopScan();
        mBleWork.resetSequence();
        mBleWork.setSequenceCallback(mSequenceCallbackSuccess, mSequenceCallbackFailed);
        EpLog.m69d("              getInfoForConnectDialog");
        getInfoForConnectDialog(displayName);
        mBleWork.init(this, scannedDevice);
    }

    private void getInfoForConnectDialog(String str) {
        mAlertDialog = new AlertDialog.Builder(this).setCancelable(false).create();
        mAlertDialog.setView(LayoutInflater.from(mContext).inflate(R.layout.ble_progress_layout, (ViewGroup) null));
        String property = System.getProperty("line.separator");
        AlertDialog alertDialog = mAlertDialog;
        alertDialog.setMessage(str + property + property + getString(R.string.BLE_inquiry_access_point_information_msg));
        if (this.isAlive && !isFinishing()) {
            mAlertDialog.show();
        }
        setConnectTimeOut();
    }

    private void setConnectTimeOut() {
        mTimeoutHandler = new Handler();
        mTimeoutHandler.postDelayed(new Runnable() {
            public void run() {
                EpLog.m71e(" timeout ------------" + BleWorkActivity.mBleWork.mSequence);
                if (BleWorkActivity.mBleWork.mSequence == BleWork.jobSequence.AFTER_SETTING_PASSWORD || BleWorkActivity.mBleWork.mSequence == BleWork.jobSequence.MIB_GET_MACADDRESS || BleWorkActivity.mBleWork.mSequence == BleWork.jobSequence.BEFORE_CHECK_SSID || BleWorkActivity.mBleWork.mSequence == BleWork.jobSequence.UNINITIALIZED) {
                    BleWorkActivity.mBleWork.failedProcessing();
                }
                BleWorkActivity.mBleWork.setRetryFinish();
            }
        }, DateUtils.MILLIS_PER_MINUTE);
    }

    private void stopConnectTimeOut() {
        Handler handler = mTimeoutHandler;
        if (handler != null) {
            handler.removeCallbacksAndMessages((Object) null);
            mTimeoutHandler = null;
        }
    }

    private void failedDialog(final BleWork.jobSequence jobsequence) {
        runOnUiThread(new Runnable() {
            public void run() {
                BleWorkActivity.this.stopCallbacks();
                BleWork.jobSequence jobsequence = jobsequence;
                EpLog.m77i("sequence = " + jobsequence);
                if (BleWorkActivity.mAlertDialog != null) {
                    BleWorkActivity.mAlertDialog.dismiss();
                }
                BleWorkActivity bleWorkActivity = BleWorkActivity.this;
                bleWorkActivity.mAlertDialog = null;
                AlertDialog.Builder cancelable = new AlertDialog.Builder(bleWorkActivity.mContext).setCancelable(false);
                cancelable.setMessage(BleWorkActivity.this.getString(R.string.BLE_setup_general_error_msg));
                cancelable.setPositiveButton(BleWorkActivity.this.getString(R.string.BLE_setup_abort_button), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Analytics.sendSetup(BleWorkActivity.mBleWork.getDeviceName(), BleWorkActivity.this.getCallingActivity() != null ? Analytics.SetUpPath.Button : Analytics.SetUpPath.Home, Analytics.SetUpResult.Error);
                        BleWorkActivity.this.finish();
                    }
                });
                if (BleWorkActivity.this.isAlive && !BleWorkActivity.this.isFinishing()) {
                    BleWorkActivity.mAlertDialog = cancelable.create();
                    BleWorkActivity.mAlertDialog.show();
                }
            }
        });
    }

    private void SSIDfailedDialog(BleWork.jobSequence jobsequence) {
        runOnUiThread(new Runnable(jobsequence) {
            private final /* synthetic */ BleWork.jobSequence f$1;

            {
                f$1 = r2;
            }

            public final void run() {
                BleWorkActivity.lambda$SSIDfailedDialog$1(BleWorkActivity.this, f$1);
            }
        });
    }

    public static /* synthetic */ void lambda$SSIDfailedDialog$1(BleWorkActivity bleWorkActivity, BleWork.jobSequence jobsequence) {
        bleWorkActivity.stopCallbacks();
        EpLog.m77i("sequence = " + jobsequence);
        AlertDialog alertDialog = bleWorkActivity.mAlertDialog;
        if (alertDialog != null) {
            alertDialog.dismiss();
        }
        bleWorkActivity.mAlertDialog = null;
        boolean z = !bleWorkActivity.mBleWork.getSsidLis().isEmpty();
        AlertDialog.Builder cancelable = new AlertDialog.Builder(bleWorkActivity.mContext).setCancelable(false);
        cancelable.setMessage(bleWorkActivity.getString(R.string.BLE_not_support_security_type));
        cancelable.setPositiveButton(bleWorkActivity.getString(z ? R.string.str_btn_close : R.string.BLE_setup_abort_button), new DialogInterface.OnClickListener(z) {
            private final /* synthetic */ boolean f$1;

            {
                f$1 = r2;
            }

            public final void onClick(DialogInterface dialogInterface, int i) {
                BleWorkActivity.lambda$null$0(BleWorkActivity.this, f$1, dialogInterface, i);
            }
        });
        if (bleWorkActivity.isAlive && !bleWorkActivity.isFinishing()) {
            bleWorkActivity.mAlertDialog = cancelable.create();
            bleWorkActivity.mAlertDialog.show();
        }
    }

    public static /* synthetic */ void lambda$null$0(BleWorkActivity bleWorkActivity, boolean z, DialogInterface dialogInterface, int i) {
        Analytics.sendSetup(bleWorkActivity.mBleWork.getDeviceName(), bleWorkActivity.getCallingActivity() != null ? Analytics.SetUpPath.Button : Analytics.SetUpPath.Home, z ? Analytics.SetUpResult.Error119 : Analytics.SetUpResult.Error106);
        if (z) {
            bleWorkActivity.mBleWork.mSequence = BleWork.jobSequence.MIB_GET_SSID_LIST;
            bleWorkActivity.mBleWork.setSecurityType(0);
            bleWorkActivity.showSSIDList();
            return;
        }
        bleWorkActivity.mBleWork.disconnect();
        bleWorkActivity.finish();
    }

    private void showSSIDList() {
        runOnUiThread(new Runnable() {
            public void run() {
                BleWorkActivity.mBleWork.mSequence = BleWork.jobSequence.MIB_SET_SSID;
                BleWorkActivity.mBleWork.disconnect(false);
                EpLog.m69d("sequence = " + BleWorkActivity.mBleWork.mSequence);
                List<String> ssidLis = BleWorkActivity.mBleWork.getSsidLis();
                ssidLis.remove(BleWorkActivity.this.getResources().getText(R.string.BLE_select_SSID_manual_button).toString());
                BleWorkActivity.this.sortSSIDlist(ssidLis);
                ssidLis.add(BleWorkActivity.this.getResources().getText(R.string.BLE_select_SSID_manual_button).toString());
                final String[] strArr = (String[]) ssidLis.toArray(new String[ssidLis.size()]);
                LinearLayout linearLayout = new LinearLayout(BleWorkActivity.this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
                layoutParams.setMargins(40, 25, 40, 25);
                TextView textView = new TextView(BleWorkActivity.this);
                textView.setText(BleWorkActivity.this.getResources().getText(R.string.BLE_select_SSID_screen_msg));
                textView.setTextSize(20.0f);
                textView.setLayoutParams(layoutParams);
                linearLayout.addView(textView);
                AlertDialog.Builder cancelable = new AlertDialog.Builder(BleWorkActivity.this).setCancelable(false);
                cancelable.setCustomTitle(linearLayout);
                cancelable.setItems(strArr, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String str = strArr[i];
                        EpLog.m69d("select = " + str);
                        if (i == strArr.length - 1) {
                            BleWorkActivity.this.setSSID();
                            return;
                        }
                        EpLog.m77i("☆" + BleWorkActivity.mBleWork.mSequence + "::" + BleWorkActivity.mBleWork.mStatus);
                        BleWorkActivity.mBleWork.setSSID(str);
                        if (BleWorkActivity.mBleWork.mSequence == BleWork.jobSequence.DISCONNECTED || BleWorkActivity.mBleWork.mStatus == 0) {
                            EpLog.m77i("☆☆ " + BleWorkActivity.mBleWork.mSequence + "::" + BleWorkActivity.mBleWork.mStatus);
                            BleWorkActivity.mBleWork.mSequence = BleWork.jobSequence.BEFORE_CHECK_SSID;
                            BleWorkActivity.mBleWork.connect();
                            EpLog.m69d("              getInfoForConnectDialog");
                            BleWorkActivity.this.getInfoForConnectDialog(BleWorkActivity.mBleWork.getDeviceName());
                            return;
                        }
                        EpLog.m77i("☆☆☆☆ " + BleWorkActivity.mBleWork.mSequence + "::" + BleWorkActivity.mBleWork.mStatus);
                        BleWorkActivity.mBleWork.setSSIDType();
                    }
                });
                cancelable.setNegativeButton(BleWorkActivity.this.getString(R.string.BLE_setup_abort_button), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EpLog.m76i();
                        BleWorkActivity.mBleWork.disconnect(true);
                    }
                });
                cancelable.show();
            }
        });
    }

    private void sortSSIDlist(List<String> list) {
        Collections.sort(list);
        String replaceAll = BleWork.getSSID(this).toUpperCase().replaceAll(CommonDefine.UNDER_BAR, "-");
        int i = 0;
        while (true) {
            if (i >= list.size()) {
                i = -1;
                break;
            }
            String replaceAll2 = list.get(i).toUpperCase().replaceAll(CommonDefine.UNDER_BAR, "-");
            if (replaceAll2.replaceAll("-A$", "-G").equals(replaceAll) || replaceAll2.replaceFirst("-A-", "-G-").equals(replaceAll) || replaceAll.replaceAll("-5G$", "").equals(replaceAll2) || replaceAll.replaceAll("-5GHZ$", "").equals(replaceAll2)) {
                break;
            }
            i++;
        }
        if (i > 0) {
            list.add(0, list.get(i));
            list.remove(i + 1);
        }
    }

    private void setSSID() {
        runOnUiThread(new Runnable() {
            public void run() {
                EpLog.m77i("inputs = ");
                final EditText editText = new EditText(BleWorkActivity.this);
                editText.setInputType(33);
                editText.setFilters(BleWorkActivity.this.getInputFilters(100));
                editText.addTextChangedListener(new TextWatcher() {
                    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                    }

                    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                    }

                    public void afterTextChanged(Editable editable) {
                        EpLog.m77i("afterTextChanged " + editable);
                        BleWorkActivity.this.validateInputText(100, editable.toString());
                    }
                });
                AlertDialog.Builder cancelable = new AlertDialog.Builder(BleWorkActivity.this).setCancelable(false);
                cancelable.setTitle(BleWorkActivity.this.getString(R.string.BLE_input_SSID_by_manual_dialog_title));
                cancelable.setMessage(BleWorkActivity.this.getString(R.string.BLE_input_SSID_by_manual_dialog_msg));
                cancelable.setView(editText);
                cancelable.setPositiveButton(BleWorkActivity.this.getString(R.string.ok_button), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EpLog.m77i(editText.getText().toString());
                        BleWorkActivity.mBleWork.setSSID(editText.getText().toString());
                        BleWorkActivity.mBleWork.setSecurityType(255);
                        BleWorkActivity.mBleWork.mSequence = BleWork.jobSequence.SET_PASSWORD;
                        BleWorkActivity.this.setSSIDPassword();
                    }
                });
                cancelable.setNegativeButton(BleWorkActivity.this.getString(R.string.no_button), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EpLog.m76i();
                        ((InputMethodManager) BleWorkActivity.this.getSystemService("input_method")).hideSoftInputFromWindow(editText.getWindowToken(), 2);
                        if (!BleWorkActivity.mBleWork.getSsidLis().isEmpty()) {
                            BleWorkActivity.mBleWork.mSequence = BleWork.jobSequence.MIB_GET_SSID_LIST;
                            BleWorkActivity.mBleWork.setSecurityType(0);
                            BleWorkActivity.this.showSSIDList();
                            return;
                        }
                        BleWorkActivity.mBleWork.disconnect();
                    }
                });
                if (!BleWorkActivity.this.isFinishing()) {
                    BleWorkActivity.mDialog = cancelable.create();
                    BleWorkActivity.mDialog.show();
                    BleWorkActivity.mDialog.getButton(-1).setEnabled(false);
                }
            }
        });
    }

    private void setSSIDPassword() {
        EpLog.m76i();
        if (!mBleWork.getSSIDPassword().isEmpty() || mBleWork.getSecurityType() == 1 || (mBleWork.getSecurityType() == 255 && mBleWork.mSequence != BleWork.jobSequence.SET_PASSWORD)) {
            runOnUiThread(new Runnable() {
                public void run() {
                    EpLog.m77i("password mNext");
                    BleWorkActivity.mNext.call("");
                    if (BleWorkActivity.this.setUpProgressHandler == null && BleWorkActivity.this.runnableSetUpProgress == null) {
                        if (BleWorkActivity.mAlertDialog != null) {
                            BleWorkActivity.mAlertDialog.dismiss();
                            BleWorkActivity.mAlertDialog = null;
                        }
                        EpLog.m69d("waitingCommunicationDaialog");
                        BleWorkActivity.this.waitingCommunicationDaialog();
                    }
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                public void run() {
                    EpLog.m77i("inputs = ");
                    final short securityType = BleWorkActivity.mBleWork.getSecurityType();
                    if (securityType == 7 || securityType == 10) {
                        BleWorkActivity bleWorkActivity = BleWorkActivity.this;
                        bleWorkActivity.SSIDfailedDialog(bleWorkActivity.mBleWork.mSequence);
                        return;
                    }
                    final EditText editText = new EditText(BleWorkActivity.this);
                    editText.setInputType(33);
                    editText.setFilters(BleWorkActivity.this.getInputFilters(securityType));
                    editText.addTextChangedListener(new TextWatcher() {
                        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                        }

                        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                        }

                        public void afterTextChanged(Editable editable) {
                            EpLog.m77i("afterTextChanged " + editable);
                            BleWorkActivity.this.validateInputText(securityType, editable.toString());
                        }
                    });
                    AlertDialog.Builder cancelable = new AlertDialog.Builder(BleWorkActivity.this).setCancelable(false);
                    cancelable.setTitle(BleWorkActivity.this.getString(R.string.BLE_input_password_of_SSID_dialog_title));
                    BleWorkActivity bleWorkActivity2 = BleWorkActivity.this;
                    boolean z = true;
                    cancelable.setMessage(bleWorkActivity2.getString(R.string.BLE_input_password_of_SSID_dialog_msg, new Object[]{bleWorkActivity2.mBleWork.getSSID()}));
                    cancelable.setView(editText);
                    cancelable.setPositiveButton(BleWorkActivity.this.getString(R.string.ok_button), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            EpLog.m77i(editText.getText().toString());
                            BleWorkActivity.mBleWork.setSSIDPassword(editText.getText().toString());
                            BleWorkActivity.mBleWork.mSequence = BleWork.jobSequence.MIB_GET_MACADDRESS;
                            BleWorkActivity.mBleWork.connect();
                            BleWorkActivity.this.setConnectTimeOut();
                            EpLog.m69d("waitingCommunicationDaialog");
                            BleWorkActivity.this.waitingCommunicationDaialog();
                            ((InputMethodManager) BleWorkActivity.this.getSystemService("input_method")).hideSoftInputFromWindow(editText.getWindowToken(), 2);
                        }
                    });
                    cancelable.setNegativeButton(BleWorkActivity.this.getString(R.string.no_button), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            EpLog.m76i();
                            ((InputMethodManager) BleWorkActivity.this.getSystemService("input_method")).hideSoftInputFromWindow(editText.getWindowToken(), 2);
                            if (!BleWorkActivity.mBleWork.getSsidLis().isEmpty()) {
                                BleWorkActivity.mBleWork.mSequence = BleWork.jobSequence.MIB_GET_SSID_LIST;
                                BleWorkActivity.mBleWork.setSecurityType(0);
                                BleWorkActivity.this.showSSIDList();
                                return;
                            }
                            BleWorkActivity.mBleWork.disconnect();
                        }
                    });
                    if (!BleWorkActivity.this.isFinishing()) {
                        BleWorkActivity.mDialog = cancelable.create();
                        BleWorkActivity.mDialog.show();
                        Button button = BleWorkActivity.mDialog.getButton(-1);
                        if (securityType != 255) {
                            z = false;
                        }
                        button.setEnabled(z);
                    }
                    EpLog.m69d("sequence = " + BleWorkActivity.mBleWork.mSequence);
                    BleWorkActivity.mBleWork.mSequence = BleWork.jobSequence.AFTER_SETTING_PASSWORD;
                    BleWorkActivity.mBleWork.disconnect(false);
                    EpLog.m69d("sequence = " + BleWorkActivity.mBleWork.mSequence);
                }
            });
        }
    }

    /* access modifiers changed from: private */
    @NonNull
    public InputFilter[] getInputFilters(int i) {
        int i2 = 26;
        if (i != 7) {
            if (i != 100) {
                switch (i) {
                    case 2:
                    case 4:
                        break;
                    case 3:
                        i2 = 10;
                        break;
                    case 5:
                        break;
                    default:
                        switch (i) {
                            case 9:
                            case 10:
                            case 11:
                                break;
                            default:
                                i2 = 63;
                                break;
                        }
                }
            } else {
                i2 = 32;
            }
            return new InputFilter[]{new InputFilter.LengthFilter(i2), new PasswordFilter(i)};
        }
        i2 = 63;
        return new InputFilter[]{new InputFilter.LengthFilter(i2), new PasswordFilter(i)};
    }

    class PasswordFilter implements InputFilter {
        int filterType;

        PasswordFilter(int i) {
            filterType = i;
        }

        public CharSequence filter(CharSequence charSequence, int i, int i2, Spanned spanned, int i3, int i4) {
            String charSequence2 = charSequence.toString();
            if (charSequence2.contains(" ")) {
                return StringUtils.deleteWhitespace(charSequence2);
            }
            try {
                charSequence = charSequence2.replace(new String(new byte[]{-62, -91}, CharEncoding.UTF_8), new String("\\".getBytes(), CharEncoding.UTF_8));
            } catch (UnsupportedEncodingException e) {
                EpLog.m71e(e.getMessage());
            }
            if (charSequence.toString().matches("^[a-zA-Z0-9!-/:-@\\[-\\`\\{-\\~]+$")) {
                return charSequence;
            }
            EpLog.m77i("in NG");
            return "";
        }
    }

    /* JADX WARNING: Can't fix incorrect switch cases order */
    /* JADX WARNING: Code restructure failed: missing block: B:12:0x0031, code lost:
        if (r11 > 63) goto L_0x00ac;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:14:0x0037, code lost:
        if (r11 <= 63) goto L_0x00ad;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:16:0x0055, code lost:
        if (java.util.Arrays.asList(new java.lang.Integer[]{13, 26}).contains(java.lang.Integer.valueOf(r11)) != false) goto L_0x00ad;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:18:0x0072, code lost:
        if (java.util.Arrays.asList(new java.lang.Integer[]{5, 10}).contains(java.lang.Integer.valueOf(r11)) != false) goto L_0x00ad;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:20:0x009d, code lost:
        if (java.util.Arrays.asList(new java.lang.Integer[]{5, 10, 13, 26}).contains(java.lang.Integer.valueOf(r11)) != false) goto L_0x00ad;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:23:0x00a4, code lost:
        if (r11 <= 32) goto L_0x00ad;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:25:0x00a9, code lost:
        if (r11 <= 63) goto L_0x00ad;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void validateInputText(int r10, java.lang.String r11) {
        /*
            r9 = this;
            java.lang.String r0 = r11.toString()
            java.lang.String r1 = "^[^ -~｡-ﾟ]+$"
            boolean r0 = r0.matches(r1)
            r1 = 0
            r2 = 1
            if (r0 != 0) goto L_0x00ac
            int r11 = r11.length()
            r0 = 7
            r3 = 63
            r4 = 8
            if (r10 == r0) goto L_0x00a7
            r0 = 100
            if (r10 == r0) goto L_0x00a0
            r0 = 26
            r5 = 13
            r6 = 10
            r7 = 2
            r8 = 5
            switch(r10) {
                case 2: goto L_0x0075;
                case 3: goto L_0x0058;
                case 4: goto L_0x003b;
                case 5: goto L_0x00a7;
                default: goto L_0x0028;
            }
        L_0x0028:
            switch(r10) {
                case 9: goto L_0x0035;
                case 10: goto L_0x0035;
                case 11: goto L_0x0035;
                default: goto L_0x002b;
            }
        L_0x002b:
            if (r11 == 0) goto L_0x00ad
            if (r11 == r8) goto L_0x00ad
            if (r11 < r4) goto L_0x00ac
            if (r11 > r3) goto L_0x00ac
            goto L_0x00ad
        L_0x0035:
            if (r11 < r4) goto L_0x00ac
            if (r11 > r3) goto L_0x00ac
            goto L_0x00ad
        L_0x003b:
            java.lang.Integer[] r10 = new java.lang.Integer[r7]
            java.lang.Integer r3 = java.lang.Integer.valueOf(r5)
            r10[r1] = r3
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)
            r10[r2] = r0
            java.util.List r10 = java.util.Arrays.asList(r10)
            java.lang.Integer r11 = java.lang.Integer.valueOf(r11)
            boolean r10 = r10.contains(r11)
            if (r10 == 0) goto L_0x00ac
            goto L_0x00ad
        L_0x0058:
            java.lang.Integer[] r10 = new java.lang.Integer[r7]
            java.lang.Integer r0 = java.lang.Integer.valueOf(r8)
            r10[r1] = r0
            java.lang.Integer r0 = java.lang.Integer.valueOf(r6)
            r10[r2] = r0
            java.util.List r10 = java.util.Arrays.asList(r10)
            java.lang.Integer r11 = java.lang.Integer.valueOf(r11)
            boolean r10 = r10.contains(r11)
            if (r10 == 0) goto L_0x00ac
            goto L_0x00ad
        L_0x0075:
            r10 = 4
            java.lang.Integer[] r10 = new java.lang.Integer[r10]
            java.lang.Integer r3 = java.lang.Integer.valueOf(r8)
            r10[r1] = r3
            java.lang.Integer r3 = java.lang.Integer.valueOf(r6)
            r10[r2] = r3
            java.lang.Integer r3 = java.lang.Integer.valueOf(r5)
            r10[r7] = r3
            r3 = 3
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)
            r10[r3] = r0
            java.util.List r10 = java.util.Arrays.asList(r10)
            java.lang.Integer r11 = java.lang.Integer.valueOf(r11)
            boolean r10 = r10.contains(r11)
            if (r10 == 0) goto L_0x00ac
            goto L_0x00ad
        L_0x00a0:
            if (r11 < r2) goto L_0x00ac
            r10 = 32
            if (r11 > r10) goto L_0x00ac
            goto L_0x00ad
        L_0x00a7:
            if (r11 < r4) goto L_0x00ac
            if (r11 > r3) goto L_0x00ac
            goto L_0x00ad
        L_0x00ac:
            r2 = 0
        L_0x00ad:
            android.app.AlertDialog r10 = r9.mDialog
            r11 = -1
            android.widget.Button r10 = r10.getButton(r11)
            r10.setEnabled(r2)
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.ble.BleWorkActivity.validateInputText(int, java.lang.String):void");
    }
}
