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 androidx.annotation.NonNull;

import com.epson.iprint.prtlogger.Analytics;
import com.epson.mobilephone.common.ble.util.BLEUtility;
import com.epson.mobilephone.common.ble.util.ScannedDevice;

import org.apache.commons.lang.CharEncoding;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import epson.epsonconnectregistration.ActivityECConfiguration;
import epson.maintain.activity.PrinterNotFoundDialogCreator;
import epson.print.ActivityIACommon;
import epson.print.CommonDefine;
import epson.print.R;
import epson.print.Util.EPLog;

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.i();
        super.onCreate(bundle);
        requestWindowFeature(5);
        setContentView(R.layout.activity_scan);
        setActionBar(R.string.BLE_select_unconfigured_printer_title, true);
        findViewById(R.id.progress_bar).setVisibility(View.GONE);
        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.i("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.i();
    }

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

    protected void onStop() {
        super.onStop();
        EPLog.i();
        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.e(" type mismatch !");
                }
                StringBuilder sb = new StringBuilder();
                sb.append("result = ");
                Boolean bool = (Boolean) obj;
                sb.append(bool.booleanValue());
                EPLog.i(sb.toString());
                if (bool.booleanValue()) {
                    count = TOTAL_PROGRESS_SEC;
                    EPLog.i("count = " + count);
                }
                stopCallbacks();
                if (mAlertDialog != null && mAlertDialog.isShowing()) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            EPLog.i("dismiss ");
                            mAlertDialog.dismiss();
                            mAlertDialog = null;
                        }
                    });
                }
                mBleWork.resetSequence();
                settingResultDailog(bool.booleanValue());
            }
        };
        mNext = new CallbackWork() {
            public void call(Object obj) {
                stopWifiSearch();
                Handler unused = mWifiSearchHandler = new Handler();
                runOnUiThread(new Runnable() {
                    public void run() {
                        EPLog.i("setSSID");
                        mBleWork.setSSID(new BLEUtility.BleWorkCallback() {
                            public void call(Object obj) {
                                Runnable unused = runnableWifiSearch = new Runnable() {
                                    int postCount = 0;

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

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

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

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

    private void settingResultDailog(final boolean z) {
        runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder cancelable = new AlertDialog.Builder(mContext).setCancelable(false);
                if (z) {
                    cancelable.setTitle(getString(R.string.BLE_success_setup_printer_title));
                    cancelable.setMessage(getString(R.string.BLE_success_setup_printer_msg));
                    cancelable.setPositiveButton(getString(R.string.ok_button), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Analytics.SetUpPath setUpPath;
                            if (getCallingActivity() != null) {
                                setResult(-1);
                                setUpPath = Analytics.SetUpPath.Button;
                            } else {
                                setUpPath = Analytics.SetUpPath.Home;
                            }
                            Analytics.sendSetup(mBleWork.getDeviceName(), setUpPath, Analytics.SetUpResult.Success);
                            Intent intent = new Intent(this, ActivityECConfiguration.class);
                            intent.putExtra(ActivityECConfiguration.INTENT_EC_BLE_CONTENT, true);
                            startActivityForResult(intent, REQUEST_CODE_EPSON_CONNECT_REGISTRATION);
                        }
                    });
                } else {
                    cancelable.setTitle(getString(R.string.BLE_fail_setup_printer_title));
                    cancelable.setMessage(getString(R.string.BLE_fail_setup_printer_msg));
                    cancelable.setPositiveButton(getString(R.string.BLE_fail_setup_printer_other_method_button), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            startActivity(PrinterNotFoundDialogCreator.getStartIntent(getApplicationContext()));
                            Analytics.sendSetup(mBleWork.getDeviceName(), getCallingActivity() != null ? Analytics.SetUpPath.Button : Analytics.SetUpPath.Home, Analytics.SetUpResult.Fail);
                            finish();
                        }
                    }).setNeutralButton(getString(R.string.str_btn_close), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Analytics.sendSetup(mBleWork.getDeviceName(), getCallingActivity() != null ? Analytics.SetUpPath.Button : Analytics.SetUpPath.Home, Analytics.SetUpResult.Fail);
                            finish();
                        }
                    });
                }
                mAlertDialog = cancelable.create();
                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.i("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(View.VISIBLE);
        mPercentView = (TextView) inflate.findViewById(R.id.percent);
        mPercentView.setVisibility(View.VISIBLE);
        inflate.findViewById(R.id.space_upper).setVisibility(View.VISIBLE);
        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 (isStopSetUpProgress) {
                    EPLog.i("☆☆runnableSetUpProgress retuen ");
                    return;
                }
                EPLog.i("mSequence " + mBleWork.mSequence);
                BleWorkActivity bleWorkActivity = this;
                count = count + 1;
                int i2 = count;
                int i3 = TOTAL_PROGRESS_SEC;
                if (i2 > TOTAL_PROGRESS_SEC) {
                    if (mAlertDialog != null) {
                        mAlertDialog.dismiss();
                        mAlertDialog = null;
                    }
                    EPLog.e("TIME OUT");
                    mCompletion.call(false);
                    CallbackWork unused = mCompletion = null;
                    return;
                }
                if (count <= 90) {
                    i = count;
                } else {
                    if (count <= TOTAL_PROGRESS_SEC) {
                        i3 = count;
                    }
                    i = ((((i3 - 90)) / 12.0d) + 90.0d);
                }
                EPLog.i(count + "::" + i + " %");
                updateProgress(i);
                if (!isStopSetUpProgress) {
                    setUpProgressHandler.postDelayed(this, 1000);
                }
            }
        };
        setUpProgressHandler.post(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) mDeviceAdapter.getItem(i);
                if (scannedDevice != null) {
                    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.d("              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 (isAlive && !isFinishing()) {
            mAlertDialog.show();
        }
        setConnectTimeOut();
    }

    private void setConnectTimeOut() {
        mTimeoutHandler = new Handler();
        mTimeoutHandler.postDelayed(new Runnable() {
            public void run() {
                EPLog.e(" timeout ------------" + mBleWork.mSequence);
                if (mBleWork.mSequence == BleWork.jobSequence.AFTER_SETTING_PASSWORD || mBleWork.mSequence == BleWork.jobSequence.MIB_GET_MACADDRESS || mBleWork.mSequence == BleWork.jobSequence.BEFORE_CHECK_SSID || mBleWork.mSequence == BleWork.jobSequence.UNINITIALIZED) {
                    mBleWork.failedProcessing();
                }
                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() {
                stopCallbacks();
                BleWork.jobSequence jobsequence = jobsequence;
                EPLog.i("sequence = " + jobsequence);
                if (mAlertDialog != null) {
                    mAlertDialog.dismiss();
                }
                BleWorkActivity bleWorkActivity = this;
                mAlertDialog = null;
                AlertDialog.Builder cancelable = new AlertDialog.Builder(mContext).setCancelable(false);
                cancelable.setMessage(getString(R.string.BLE_setup_general_error_msg));
                cancelable.setPositiveButton(getString(R.string.BLE_setup_abort_button), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Analytics.sendSetup(mBleWork.getDeviceName(), getCallingActivity() != null ? Analytics.SetUpPath.Button : Analytics.SetUpPath.Home, Analytics.SetUpResult.Error);
                        finish();
                    }
                });
                if (isAlive && !isFinishing()) {
                    mAlertDialog = cancelable.create();
                    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() {
                lambda$SSIDfailedDialog$1(this, f$1);
            }
        });
    }

    public static /* synthetic */ void lambda$SSIDfailedDialog$1(BleWorkActivity bleWorkActivity, BleWork.jobSequence jobsequence) {
        stopCallbacks();
        EPLog.i("sequence = " + jobsequence);
        AlertDialog alertDialog = mAlertDialog;
        if (alertDialog != null) {
            alertDialog.dismiss();
        }
        mAlertDialog = null;
        boolean z = !mBleWork.getSsidLis().isEmpty();
        AlertDialog.Builder cancelable = new AlertDialog.Builder(mContext).setCancelable(false);
        cancelable.setMessage(getString(R.string.BLE_not_support_security_type));
        cancelable.setPositiveButton(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) {
                lambda$null$0(this, f$1, dialogInterface, i);
            }
        });
        if (isAlive && !isFinishing()) {
            mAlertDialog = cancelable.create();
            mAlertDialog.show();
        }
    }

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

    private void showSSIDList() {
        runOnUiThread(new Runnable() {
            public void run() {
                mBleWork.mSequence = BleWork.jobSequence.MIB_SET_SSID;
                mBleWork.disconnect(false);
                EPLog.d("sequence = " + mBleWork.mSequence);
                List<String> ssidLis = mBleWork.getSsidLis();
                ssidLis.remove(getResources().getText(R.string.BLE_select_SSID_manual_button).toString());
                sortSSIDlist(ssidLis);
                ssidLis.add(getResources().getText(R.string.BLE_select_SSID_manual_button).toString());
                final String[] strArr = (String[]) ssidLis.toArray(new String[ssidLis.size()]);
                LinearLayout linearLayout = new LinearLayout(this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
                layoutParams.setMargins(40, 25, 40, 25);
                TextView textView = new TextView(this);
                textView.setText(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(this).setCancelable(false);
                cancelable.setCustomTitle(linearLayout);
                cancelable.setItems(strArr, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String str = strArr[i];
                        EPLog.d("select = " + str);
                        if (i == strArr.length - 1) {
                            setSSID();
                            return;
                        }
                        EPLog.i("☆" + mBleWork.mSequence + "::" + mBleWork.mStatus);
                        mBleWork.setSSID(str);
                        if (mBleWork.mSequence == BleWork.jobSequence.DISCONNECTED || mBleWork.mStatus == 0) {
                            EPLog.i("☆☆ " + mBleWork.mSequence + "::" + mBleWork.mStatus);
                            mBleWork.mSequence = BleWork.jobSequence.BEFORE_CHECK_SSID;
                            mBleWork.connect();
                            EPLog.d("              getInfoForConnectDialog");
                            getInfoForConnectDialog(mBleWork.getDeviceName());
                            return;
                        }
                        EPLog.i("☆☆☆☆ " + mBleWork.mSequence + "::" + mBleWork.mStatus);
                        mBleWork.setSSIDType();
                    }
                });
                cancelable.setNegativeButton(getString(R.string.BLE_setup_abort_button), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EPLog.i();
                        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.i("inputs = ");
                final EditText editText = new EditText(this);
                editText.setInputType(33);
                editText.setFilters(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.i("afterTextChanged " + editable);
                        validateInputText(100, editable.toString());
                    }
                });
                AlertDialog.Builder cancelable = new AlertDialog.Builder(this).setCancelable(false);
                cancelable.setTitle(getString(R.string.BLE_input_SSID_by_manual_dialog_title));
                cancelable.setMessage(getString(R.string.BLE_input_SSID_by_manual_dialog_msg));
                cancelable.setView(editText);
                cancelable.setPositiveButton(getString(R.string.ok_button), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EPLog.i(editText.getText().toString());
                        mBleWork.setSSID(editText.getText().toString());
                        mBleWork.setSecurityType(255);
                        mBleWork.mSequence = BleWork.jobSequence.SET_PASSWORD;
                        setSSIDPassword();
                    }
                });
                cancelable.setNegativeButton(getString(R.string.no_button), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EPLog.i();
                        ((InputMethodManager) getSystemService("input_method")).hideSoftInputFromWindow(editText.getWindowToken(), 2);
                        if (!mBleWork.getSsidLis().isEmpty()) {
                            mBleWork.mSequence = BleWork.jobSequence.MIB_GET_SSID_LIST;
                            mBleWork.setSecurityType(0);
                            showSSIDList();
                            return;
                        }
                        mBleWork.disconnect();
                    }
                });
                if (!isFinishing()) {
                    mDialog = cancelable.create();
                    mDialog.show();
                    mDialog.getButton(-1).setEnabled(false);
                }
            }
        });
    }

    private void setSSIDPassword() {
        EPLog.i();
        if (!mBleWork.getSSIDPassword().isEmpty() || mBleWork.getSecurityType() == 1 || (mBleWork.getSecurityType() == 255 && mBleWork.mSequence != BleWork.jobSequence.SET_PASSWORD)) {
            runOnUiThread(new Runnable() {
                public void run() {
                    EPLog.i("password mNext");
                    mNext.call("");
                    if (setUpProgressHandler == null && runnableSetUpProgress == null) {
                        if (mAlertDialog != null) {
                            mAlertDialog.dismiss();
                            mAlertDialog = null;
                        }
                        EPLog.d("waitingCommunicationDaialog");
                        waitingCommunicationDaialog();
                    }
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                public void run() {
                    EPLog.i("inputs = ");
                    final short securityType = mBleWork.getSecurityType();
                    if (securityType == 7 || securityType == 10) {
                        BleWorkActivity bleWorkActivity = this;
                        SSIDfailedDialog(mBleWork.mSequence);
                        return;
                    }
                    final EditText editText = new EditText(this);
                    editText.setInputType(33);
                    editText.setFilters(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.i("afterTextChanged " + editable);
                            validateInputText(securityType, editable.toString());
                        }
                    });
                    AlertDialog.Builder cancelable = new AlertDialog.Builder(this).setCancelable(false);
                    cancelable.setTitle(getString(R.string.BLE_input_password_of_SSID_dialog_title));
                    BleWorkActivity bleWorkActivity2 = 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(getString(R.string.ok_button), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            EPLog.i(editText.getText().toString());
                            mBleWork.setSSIDPassword(editText.getText().toString());
                            mBleWork.mSequence = BleWork.jobSequence.MIB_GET_MACADDRESS;
                            mBleWork.connect();
                            setConnectTimeOut();
                            EPLog.d("waitingCommunicationDaialog");
                            waitingCommunicationDaialog();
                            ((InputMethodManager) getSystemService("input_method")).hideSoftInputFromWindow(editText.getWindowToken(), 2);
                        }
                    });
                    cancelable.setNegativeButton(getString(R.string.no_button), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            EPLog.i();
                            ((InputMethodManager) getSystemService("input_method")).hideSoftInputFromWindow(editText.getWindowToken(), 2);
                            if (!mBleWork.getSsidLis().isEmpty()) {
                                mBleWork.mSequence = BleWork.jobSequence.MIB_GET_SSID_LIST;
                                mBleWork.setSecurityType(0);
                                showSSIDList();
                                return;
                            }
                            mBleWork.disconnect();
                        }
                    });
                    if (!isFinishing()) {
                        mDialog = cancelable.create();
                        mDialog.show();
                        Button button = mDialog.getButton(-1);
                        if (securityType != 255) {
                            z = false;
                        }
                        button.setEnabled(z);
                    }
                    EPLog.d("sequence = " + mBleWork.mSequence);
                    mBleWork.mSequence = BleWork.jobSequence.AFTER_SETTING_PASSWORD;
                    mBleWork.disconnect(false);
                    EPLog.d("sequence = " + 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.e(e.getMessage());
            }
            if (charSequence.toString().matches("^[a-zA-Z0-9!-/:-@\\[-\\`\\{-\\~]+$")) {
                return charSequence;
            }
            EPLog.i("in NG");
            return "";
        }
    }


    public void validateInputText(int r10, java.lang.String r11) {

        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.ble.validateInputText(int, java.lang.String):void");
    }
}
