package epson.scan.activity;

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


import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.epson.mobilephone.common.wifidirect.ActivityWiFiDirectStart;
import com.epson.mobilephone.common.wifidirect.ActivityiPrintConnect;
import com.epson.mobilephone.common.wifidirect.MacAddrUtils;
import com.epson.mobilephone.common.wifidirect.SearchWiFiDirectPrinterTask;
import com.epson.mobilephone.common.wifidirect.WiFiControl;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
import com.epson.mobilephone.common.wifidirect.WiFiDirectPrinterListUtils;
import com.epson.mobilephone.common.wifidirect.escprLib;

import java.util.Locale;
import java.util.concurrent.Semaphore;

import epson.common.Constants;
import epson.common.Utils;
import epson.maintain.activity.PrinterNotFoundDialogCreator;
import epson.print.ActivityIACommon;
import epson.print.CommonDefine;
import epson.print.CustomLayoutDialogFragment;
import epson.print.CustomTitleDialogFragment;
import epson.print.MyPrinter;
import epson.print.R;
import epson.print.Util.EPLog;
import epson.print.screen.WorkingDialog;
import epson.print.widgets.ListControlHelper;
import epson.print.widgets.PrinterInfoBuilder;
import epson.scan.lib.CancelableFind;
import epson.scan.lib.CancelablePropertyTaker;
import epson.scan.lib.EscanLibException;
import epson.scan.lib.escanLib;

public class ScanSearchActivity extends ActivityIACommon implements CustomTitleDialogFragment.Callback {
    private static final int DIALOG_ID_PRINTER_NOT_FOUND_WITH_WEB_GUIDANCE = 1;
    private static final String DIALOG_TAG_PRINTER_NOT_FOUND = "printer_not_found_dialog";
    private static final int Menu_Delete = 1;
    private static final int Menu_Edit = 2;
    private static final String PARAM_KEY_SCANNER_ID = "scanner-id";
    private static final String PARAM_KEY_SCANNER_LOCATION = "scanner-location";
    private static final String PARAM_KEY_SCANNER_SSID = "scanner-ssid";
    private static final String TAG = "ScanSearch";
    private static final Semaphore sEscanLibSemaphore = new Semaphore(1);
    private final int ADD_SCANER = 8;
    private final String COMMONDEVICENAME = "commonDeviceName";
    private final int CONNECT_SIMPLEAP = 7;
    private final int DELAY = 100;
    private final int DELETE_PRINTER = 9;
    private final int EDIT_IPPRINTER = 10;
    private final int FINISH_FIND_SCANER = 5;
    private final int FOUND_SIMPLEAP = 6;
    private final String PRINTER_ID = "id";
    private final String PRINTER_IP = escprLib.PRINTER_IP;
    private final String PRINTER_NAME = "name";
    private final int PROBE_SCANNER = 11;
    private final String SCANER_ID = "id";
    private final String SCANER_INDEX = "index";
    private final String SCANER_IP = escprLib.PRINTER_IP;
    private final String SCANER_LOCATION = "location";
    private final String SCANER_NAME = "name";
    private final int SEARCH_PRINTER_P2P = 12;
    private final int SEARCH_SCANER = 1;
    private final int SELECT_SCANER = 4;
    private boolean bCheckWiFiStatus = false;
    private String connectionInfo = null;
    private AlertDialog errorDialog;
    ListControlHelper helper = null;
    private volatile boolean isFinishSearchScanner = true;
    private Boolean isFocused = true;
    private boolean isJapaneseLocale = false;
    private volatile boolean isSearchSimpleAp = false;
    private boolean isSelected = false;
    private AdapterView.AdapterContextMenuInfo listItemIndex;
    private volatile boolean mActivityBackRequested;
    private boolean mActivityForegroundLifetime;
    Button mAddButton;
    ScannerListAdapter mBuilder;
    private Context mContext = null;
    int mDeletePos;
    private escanLib mEscanLib;
    private FindScannerTask mFindScannerTask;
    private GetScannerCapabilityAndFinishTask mGetScannerCapabilityAndFinishTask;
    private Handler mHandler = new Handler(new Handler.Callback() {

        public boolean handleMessage(android.os.Message r7) {

            throw new UnsupportedOperationException("Method not decompiled: epson.scan.activity.ScanSearchActivity.C242011.handleMessage(android.os.Message):boolean");
        }
    });
    RadioButton mIpButton;
    ViewGroup mLayout;
    private TextView mListEmptyMessageTextView;
    private ListView mListView;
    RadioButton mLocalButton;
    private String mMyPrinterForSimpleApDisconnect;
    private volatile boolean mProgress;
    ProgressBar mProgressBar;
    WorkingDialog mProgressDialog;
    private volatile boolean mScannerFindCancelRequested;
    Button mSearchButton;
    private String mSimpleApSsid;
    TextView mTextDetail;
    View mWiFiSettingButton;
    private String scannerId;
    private String scannerKey;
    private int scanner_location;
    private SearchWiFiDirectPrinterTask searchWiFiDirectPrinter = null;
    private TextView tvResult;
    WiFiDirectPrinterListUtils wiFiDirectPrinterListUtils = null;

    public void onLocalNegativeCallback(int i) {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (Locale.getDefault().getLanguage().equalsIgnoreCase(Locale.JAPAN.getLanguage()) || Locale.getDefault().getLanguage().equalsIgnoreCase(Locale.JAPANESE.getLanguage())) {
            isJapaneseLocale = true;
        } else {
            isJapaneseLocale = false;
        }
        mLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.search_printer_layout, (ViewGroup) null);
        addWiFiSetupButton();
        mEscanLib = new escanLib();
        mEscanLib.escanWrapperInitDriver(this);
        Bundle extras = getIntent().getExtras();
        scanner_location = extras.getInt(PARAM_KEY_SCANNER_LOCATION);
        if (this.scanner_location == 0) {
            scanner_location = 1;
        }
        if (this.scanner_location != 3) {
            scannerId = extras.getString(PARAM_KEY_SCANNER_ID);
            connectionInfo = extras.getString(PARAM_KEY_SCANNER_SSID);
        } else {
            scannerKey = extras.getString(PARAM_KEY_SCANNER_ID);
        }
        setContentView((View) mLayout);
        tvResult = (TextView) mLayout.findViewById(R.id.empty);
        tvResult.setText(getString(R.string.lbl_scanner_search_processing));
        mListEmptyMessageTextView = (TextView) mLayout.findViewById(R.id.list_empty_message);
        mListView = (ListView) mLayout.findViewById(16908298);
        EPLog.i("epson.scan.activity.ScanSearchActivity", "builElements");
        buildElements();
        setNotFoundPrinterButton();
        setActionBar(R.string.title_addprint, true);
        mContext = this;
    }

    private void setNotFoundPrinterButton() {
        mLayout.findViewById(R.id.not_found_printer_epsonsn).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ScanSearchActivity scanSearchActivity = ScanSearchActivity.this;
                scanSearchActivity.startActivity(PrinterNotFoundDialogCreator.getStartIntent(scanSearchActivity.getApplicationContext()));
            }
        });
    }

    private void addWiFiSetupButton() {
        mWiFiSettingButton = mLayout.findViewById(R.id.rlWifiSettings);
        if (this.isJapaneseLocale) {
            ((TextView) mWiFiSettingButton).setText(R.string.str_goto_wifidirect_settings);
        } else {
            ((TextView) mWiFiSettingButton).setText(R.string.str_wifidirect_settings);
        }
        mWiFiSettingButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ScanSearchActivity.this.startWifiPrinterSelect();
            }
        });
    }

    private void startWifiPrinterSelect() {
        startActivityForResult(new Intent(this, ActivityWiFiDirectStart.class), 7);
    }

    private void buildElements() {
        mLocalButton = (RadioButton) mLayout.findViewById(R.id.btn_local);
        mIpButton = (RadioButton) mLayout.findViewById(R.id.btn_ip);
        mSearchButton = (Button) mLayout.findViewById(R.id.function_button);
        mProgressBar = (ProgressBar) mLayout.findViewById(R.id.progress_bar);
        mAddButton = (Button) mLayout.findViewById(R.id.edit_button);
        mTextDetail = (TextView) mLayout.findViewById(R.id.detail);
        mTextDetail.setVisibility(View.VISIBLE);
        findViewById(R.id.btn_remote).setVisibility(View.GONE);
        mIpButton.setBackground(getResources().getDrawable(R.drawable.printer_selector_background_remote));
        mLayout.findViewById(R.id.ble_wifi_setting).setVisibility(View.GONE);
        Utils.setDrawble2TextView(this, mLocalButton, R.drawable.printer_selector_icon_local);
        Utils.setDrawble2TextView(this, mIpButton, R.drawable.printer_selector_icon_ip);
        TextView textView = null;
        if (this.scanner_location != 3) {
            mListView.setEmptyView((View) null);
            mListEmptyMessageTextView.setText((CharSequence) null);
            mListEmptyMessageTextView.setVisibility(View.GONE);
            mAddButton.setVisibility(View.GONE);
            mSearchButton.setVisibility(View.VISIBLE);
            searchButtonSetEnabled(false);
            mProgressBar.setVisibility(View.VISIBLE);
            tvResult.setVisibility(View.VISIBLE);
            mWiFiSettingButton.setVisibility(View.VISIBLE);
            mBuilder = new ScannerListAdapter(this, mLayout, 2);
            mBuilder.setResource(WiFiDirectPrinterListUtils.getCurPrinterString(this, scannerId, connectionInfo));
            helper = new ListControlHelper((PrinterInfoBuilder) mBuilder.getBuilder());
            wiFiDirectPrinterListUtils = new WiFiDirectPrinterListUtils(this, mBuilder.getData(), helper, connectionInfo);
            mLocalButton.setChecked(true);
            mTextDetail.setText(R.string.str_detailtext_local_scan);
            mLayout.findViewById(R.id.not_found_printer_epsonsn).setVisibility(View.VISIBLE);
        } else {
            mListEmptyMessageTextView.setText(R.string.printer_list_empty_message);
            textView = mListEmptyMessageTextView;
            mAddButton.setVisibility(View.VISIBLE);
            mSearchButton.setVisibility(View.GONE);
            mProgressBar.setVisibility(View.GONE);
            tvResult.setVisibility(View.GONE);
            mWiFiSettingButton.setVisibility(View.GONE);
            mBuilder = new ScannerListAdapter(this, mLayout, 1);
            mBuilder.setResource(this.scannerKey);
            mIpButton.setChecked(true);
            mTextDetail.setText(R.string.str_detailtext_ip_scan);
            mLayout.findViewById(R.id.not_found_printer_epsonsn).setVisibility(View.GONE);
        }
        mBuilder.build();
        mBuilder.refresh();
        mListView.setEmptyView(textView);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                if (!ScanSearchActivity.this.isSelected && ScanSearchActivity.this.isFocused.booleanValue()) {
                    Boolean unused = ScanSearchActivity.this.isFocused = false;
                    boolean unused2 = ScanSearchActivity.this.isSelected = true;
                    MyPrinter myPrinter = (MyPrinter) ScanSearchActivity.mBuilder.getData().elementAt(i);
                    String ip = myPrinter.getIp();
                    if (!WiFiControl.ConnectType.NONE.equals(WiFiDirectManager.getConnectType(ScanSearchActivity.this, ip))) {
                        ScanSearchActivity.this.interruptSearch();
                        WiFiDirectManager.connect(ScanSearchActivity.this, ip, 7);
                        return;
                    }
                    myPrinter.setLocation(ScanSearchActivity.this.scanner_location);
                    ScanSearchActivity scanSearchActivity = ScanSearchActivity.this;
                    String unused3 = scanSearchActivity.mSimpleApSsid = scanSearchActivity.getSimpleApSsid(myPrinter);
                    ScanSearchActivity.this.startGetCapabilityAndFinishTask(myPrinter);
                }
            }
        });
        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long j) {
                if (ScanSearchActivity.this.scanner_location == 1) {
                    return true;
                }
                ScanSearchActivity scanSearchActivity = ScanSearchActivity.this;
                scanSearchActivity.mDeletePos = i;
                scanSearchActivity.registerForContextMenu(scanSearchActivity.mListView);
                return false;
            }
        });
        mAddButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (ScanSearchActivity.this.scanner_location == 3) {
                    if (ScanSearchActivity.mBuilder.getData().size() >= 32) {
                        new AlertDialog.Builder(ScanSearchActivity.mContext).setCancelable(false).setTitle(ScanSearchActivity.this.getString(R.string.epsonconnect_err_regist_remote_printer_max_size_title)).setMessage(ScanSearchActivity.this.getString(R.string.epsonconnect_err_regist_remote_printer_max_size)).setPositiveButton(ScanSearchActivity.this.getString(R.string.str_yes), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        }).show();
                        return;
                    }
                    Message message = new Message();
                    message.what = 10;
                    message.obj = null;
                    ScanSearchActivity.mHandler.sendMessage(message);
                }
            }
        });
        mSearchButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ScanSearchActivity.mHandler.sendEmptyMessageDelayed(1, 100);
            }
        });
        mLocalButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (ScanSearchActivity.this.scanner_location != 1) {
                    int unused = ScanSearchActivity.this.scanner_location = 1;
                    ScanSearchActivity.this.buildElements();
                    ScanSearchActivity.mHandler.sendEmptyMessageDelayed(1, 100);
                }
            }
        });
        mIpButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (ScanSearchActivity.this.scanner_location != 3) {
                    ScanSearchActivity.this.interruptSearch();
                    int unused = ScanSearchActivity.this.scanner_location = 3;
                    ScanSearchActivity.this.buildElements();
                }
            }
        });
    }

    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
        contextMenu.setHeaderTitle(R.string.str_edit_bookmark);
        contextMenu.add(0, 1, 0, R.string.str_delete);
        contextMenu.add(0, 2, 0, R.string.str_edit_bookmark);
    }

    public boolean onContextItemSelected(MenuItem menuItem) {
        listItemIndex = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo();
        if (this.listItemIndex == null) {
            return true;
        }
        switch (menuItem.getItemId()) {
            case 1:
                new AlertDialog.Builder(mContext).setCancelable(false).setTitle(getString(R.string.delete_file)).setIcon(getResources().getDrawable(R.drawable.delete_file)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Message obtainMessage = ScanSearchActivity.mHandler.obtainMessage();
                        obtainMessage.what = 9;
                        ScanSearchActivity.mHandler.sendMessageDelayed(obtainMessage, 100);
                    }
                }).setPositiveButton(getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                }).show();
                break;
            case 2:
                Message message = new Message();
                if (this.scanner_location == 3) {
                    message.what = 10;
                }
                message.obj = mBuilder.getData().elementAt(mDeletePos);
                mHandler.sendMessage(message);
                break;
        }
        return true;
    }

    protected void onDestroy() {
        EPLog.i("epson.scan.activity.ScanSearchActivity", "Destroy search screen");
        interruptSearch();
        mBuilder.destructor();
        super.onDestroy();
    }

    public void onBackPressed() {
        if (!mProgress) {
            mActivityBackRequested = true;
            if (sEscanLibSemaphore.availablePermits() <= 0) {
                interruptSearch();
                new CancelWaitTask().execute(new Void[0]);
                return;
            }
            releaseAndFinish();
        }
    }

    private void releaseAndFinish() {
        mEscanLib.escanWrapperReleaseDriver();
        setResult(0);
        finish();
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i != 7) {
            if (i == 10 && i2 == -1) {
                mBuilder.refresh();
                MyPrinter myPrinter = new MyPrinter(intent.getStringExtra(Constants.PRINTER_NAME), intent.getStringExtra(Constants.PRINTER_IP), intent.getStringExtra(Constants.SCAN_REFS_SCANNER_ID), (String) null);
                myPrinter.setLocation(3);
                startGetCapabilityAndFinishTask(myPrinter);
            }
        } else if (i2 == -1) {
            isSelected = true;
            intent.getBooleanExtra(ActivityiPrintConnect.FINISH_EAYSETUP, false);
            MyPrinter myPrinter2 = new MyPrinter(intent.getStringExtra("name"), intent.getStringExtra(escprLib.PRINTER_IP), intent.getStringExtra("id"), (String) null);
            myPrinter2.setLocation(this.scanner_location);
            mSimpleApSsid = getSimpleApSsid(myPrinter2);
            startGetCapabilityAndFinishTask(myPrinter2);
        } else {
            forceSetPrinterSearchEnd();
        }
    }

    /* access modifiers changed from: private */
    @UiThread
    public void search() {
        interruptSearch();
        mScannerFindCancelRequested = false;
        mEscanLib.resetEscanLib();
        mBuilder.refresh();
        mBuilder.destructor();
        wiFiDirectPrinterListUtils.clearPrinterInfoList();
        if (WiFiDirectManager.getCurSSID(this) != null) {
            mHandler.sendEmptyMessageDelayed(12, 5000);
            isSearchSimpleAp = true;
        } else if (WiFiDirectManager.isWifiEnabled(this)) {
            mHandler.sendEmptyMessage(12);
            isSearchSimpleAp = true;
        } else {
            boolean z = bCheckWiFiStatus;
            if (!z && !z) {
                bCheckWiFiStatus = true;
                WiFiDirectManager.enableWiFi(this, -1);
                return;
            }
        }
        WiFiDirectManager.setPriorityToSimpleAP(this, true);
        searchButtonSetEnabled(false);
        mProgressBar.setVisibility(View.VISIBLE);
        tvResult.setText(getString(R.string.lbl_scanner_search_processing));
        isFinishSearchScanner = false;
        mEscanLib.setScanHandler(mHandler);
        mFindScannerTask = new FindScannerTask(mEscanLib);
        mFindScannerTask.execute(new Void[0]);
    }

    /* access modifiers changed from: private */
    @UiThread
    public void interruptSearch() {
        mScannerFindCancelRequested = true;
        mHandler.removeMessages(1);
        mHandler.removeMessages(12);
        EPLog.i("epson.scan.activity.ScanSettingsActivity", "Cancel search scanner");
        mHandler.removeMessages(8);
        mHandler.removeMessages(6);
        SearchWiFiDirectPrinterTask searchWiFiDirectPrinterTask = searchWiFiDirectPrinter;
        if (searchWiFiDirectPrinterTask != null) {
            searchWiFiDirectPrinterTask.interrupt();
            searchWiFiDirectPrinter = null;
        }
        isSearchSimpleAp = false;
        FindScannerTask findScannerTask = mFindScannerTask;
        if (findScannerTask != null) {
            findScannerTask.cancelFind();
        }
    }

    private void updateFindScannerResult() {
        if (!isFinishing()) {
            mProgressBar.setVisibility(View.GONE);
            searchButtonSetEnabled(true);
            int size = mBuilder.getData().size();
            if (size <= 0) {
                tvResult.setText(R.string.lbl_scanner_search_result_none);
                if (mActivityForegroundLifetime) {
                    showPrinterNotFoundDialog(getPrinterNotFoundDialog(this.isSearchSimpleAp));
                }
            } else if (Locale.getDefault().getLanguage().equalsIgnoreCase(Locale.JAPAN.getLanguage()) || Locale.getDefault().getLanguage().equalsIgnoreCase(Locale.JAPANESE.getLanguage())) {
                tvResult.setText(getString(R.string.lbl_scanner_search_result));
            } else {
                tvResult.setText(String.format(getString(R.string.lbl_scanner_search_result), new Object[]{Integer.valueOf(size)}));
            }
        }
    }

    private DialogFragment getPrinterNotFoundDialog(boolean z) {
        String str;
        String str2;
        if (z) {
            str = getString(R.string.EPS_PRNERR_COMM1_NOWIFI);
            str2 = getString(R.string.str_err_msg_scan_not_found_title);
        } else {
            str2 = getString(R.string.str_err_msg_scan_not_found_title);
            if (Utils.getPrefBoolean(getApplicationContext(), Constants.SCAN_REFS_USED_SCANNER_PATH, CommonDefine.RE_SEARCH)) {
                str = getString(R.string.str_err_msg_scan_not_found_research);
                String ssid = Utils.getSSID(this);
                if (!Utils.isConnectedWifi(this) || ssid == null) {
                    return CustomLayoutDialogFragment.newInstance(1, str, str2, 0, R.string.str_btn_close);
                }
                str2 = str2 + "\nSSID: " + ssid;
            } else {
                str = getString(R.string.str_err_msg_scan_not_found);
            }
        }
        return CustomLayoutDialogFragment.newInstance(1, str + "\n\n" + getString(R.string.confirm_browse_wlan_setup_info), str2, R.string.browse_site, R.string.str_btn_close);
    }

    private void showPrinterNotFoundDialog(DialogFragment dialogFragment) {
        if (!isFinishing()) {
            dialogFragment.show(getSupportFragmentManager(), DIALOG_TAG_PRINTER_NOT_FOUND);
        }
    }

    protected Dialog onCreateDialog(int i) {
        Dialog onCreateDialog = super.onCreateDialog(i);
        if (i == R.string.EPS_PRNERR_COMM1_NOWIFI) {
            return Utils.makeMessageBox(this, getString(R.string.str_err_msg_scan_not_found_title), getString(R.string.EPS_PRNERR_COMM1_NOWIFI), getString(R.string.str_ok));
        }
        switch (i) {
            case R.string.str_err_msg_scan_not_found /*2131625133*/:
            case R.string.str_err_msg_scan_not_found_research /*2131625134*/:
                String str = "";
                String ssid = Utils.getSSID(this);
                if (Utils.isConnectedWifi(this) && ssid != null) {
                    str = "\nSSID: " + ssid;
                }
                return Utils.makeMessageBox(this, getString(R.string.str_err_msg_scan_not_found_title) + str, getString(i), getString(R.string.str_ok));
            default:
                return onCreateDialog;
        }
    }

    /* access modifiers changed from: package-private */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:11:0x0012 */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void removeAllDialog() {
        /*
            r1 = this;
            r0 = 2131625133(0x7f0e04ad, float:1.8877465E38)
            r1.removeDialog(r0)     // Catch:{ IllegalArgumentException -> 0x0006 }
        L_0x0006:
            r0 = 2131625134(0x7f0e04ae, float:1.8877467E38)
            r1.removeDialog(r0)     // Catch:{ IllegalArgumentException -> 0x000c }
        L_0x000c:
            r0 = 2131624361(0x7f0e01a9, float:1.88759E38)
            r1.removeDialog(r0)     // Catch:{ IllegalArgumentException -> 0x0012 }
        L_0x0012:
            r1.dismissPrinterNotFoundDialog()     // Catch:{ Exception -> 0x0015 }
        L_0x0015:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.scan.activity.ScanSearchActivity.removeAllDialog():void");
    }

    private void dismissPrinterNotFoundDialog() {
        DialogFragment dialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(DIALOG_TAG_PRINTER_NOT_FOUND);
        if (dialogFragment != null) {
            dialogFragment.dismiss();
        }
    }

    public void searchButtonSetEnabled(boolean z) {
        mSearchButton.setVisibility(View.VISIBLE);
        if (z) {
            mSearchButton.setEnabled(true);
        } else {
            mSearchButton.setEnabled(false);
        }
    }

    protected void onResume() {
        super.onResume();
        EPLog.e("Epson", "ScanSearchActivity.java call onResume()");
        mActivityForegroundLifetime = true;
        dismissPrinterNotFoundDialog();
        if (this.scanner_location != 3) {
            if (!this.isSelected) {
                mHandler.removeMessages(1);
                mHandler.sendEmptyMessageDelayed(1, 100);
            }
            EPLog.d("ScanSearchActivity", "Send CHECK_PRINTER Message.");
        }
    }

    protected void onPause() {
        EPLog.d("SearchPrinterScr", "onPause");
        super.onPause();
        mActivityForegroundLifetime = false;
        interruptSearch();
        if (!this.isSelected) {
            removeAllDialog();
        }
        GetScannerCapabilityAndFinishTask getScannerCapabilityAndFinishTask = mGetScannerCapabilityAndFinishTask;
        if (getScannerCapabilityAndFinishTask != null) {
            getScannerCapabilityAndFinishTask.cancelTask();
        }
    }

    /* access modifiers changed from: package-private */
    @UiThread
    public void showErrorDialog(EscanLibException escanLibException) {
        int[] iArr;
        if (!isFinishing()) {
            if (escanLibException.getEscIVersion() == 1) {
                iArr = getEscanI1ErrorTitleAndMessageId(escanLibException.getEscanLibErrorCode());
            } else {
                iArr = getEscanI2ErrorTitleAndMessageId(escanLibException.getEscanLibErrorCode());
            }
            errorDialog = Utils.makeMessageBox(this, getString(iArr[0]), getString(iArr[1]), getString(R.string.str_ok));
            errorDialog.show();
        }
    }

    @NonNull
    private static int[] getEscanI1ErrorTitleAndMessageId(int i) {
        if (i == -500001) {
            return new int[]{R.string.EPS_PRNERR_COMM_TITLE2, R.string.str_ipprinter_comerror};
        }
        if (i != -1501) {
            if (i == -1300) {
                return new int[]{R.string.str_err_msg_scan_not_found_title, R.string.str_err_msg_scan_not_found_research};
            }
            if (i != -1100) {
                switch (i) {
                    case 5:
                        break;
                    case 6:
                        break;
                    default:
                        return new int[]{R.string.str_err_msg_scan_generic_internal_title, R.string.str_err_msg_scan_generic_internal};
                }
            }
            return new int[]{R.string.str_err_msg_scan_comm_title, R.string.str_err_msg_scan_comm};
        }
        return new int[]{R.string.str_err_msg_scan_busy_title, R.string.str_err_msg_scan_busy};
    }

    @NonNull
    private static int[] getEscanI2ErrorTitleAndMessageId(int i) {
        int[] iArr = new int[2];
        switch (i) {
            case Constants.ESCAN_ERR_SCANNER_OCCUPIED:
            case -213:
                iArr[0] = R.string.str_err_msg_scan_busy_title;
                iArr[1] = R.string.str_err_msg_scan_busy;
                break;
            case -1300:
                iArr[0] = R.string.str_err_msg_scan_not_found_title;
                iArr[1] = R.string.str_err_msg_scan_not_found_research;
                break;
            case -1114:
            case -1100:
            case -214:
                iArr[0] = R.string.str_err_msg_scan_comm_title;
                iArr[1] = R.string.str_err_msg_scan_comm;
                break;
            case -202:
            case -201:
            case -102:
            case -101:
                iArr[0] = R.string.str_blank;
                iArr[1] = R.string.str_err_msg_out_of_memory_title;
                break;
            default:
                iArr[0] = R.string.str_err_msg_scan_generic_internal_title;
                iArr[1] = R.string.str_err_msg_scan_generic_internal;
                break;
        }
        return iArr;
    }

    private void forceSetPrinterSearchEnd() {
        isFocused = true;
        isSelected = false;
    }

    private void localFinish(ScannerPropertyWrapper scannerPropertyWrapper) {
        scannerPropertyWrapper.setSimpleApSsid(mSimpleApSsid);
        setResult(-1, SettingActivityParams.getScanSearchReturnIntent(scannerPropertyWrapper));
        finish();
    }

    private String getSimpleApSsid(MyPrinter myPrinter) {
        String macAddressFromScannerId = MacAddrUtils.getMacAddressFromScannerId(myPrinter.getScannerId());
        if (macAddressFromScannerId == null) {
            macAddressFromScannerId = MacAddrUtils.getMacAddressFromPrinterId(myPrinter.getPrinterId());
        }
        String curConnectInfo = WiFiDirectManager.getCurConnectInfo(this, macAddressFromScannerId);
        return curConnectInfo != null ? curConnectInfo : "";
    }

    public void onLocalPositiveCallback(int i) {
        if (i == 1) {
            startActivity(PrinterNotFoundDialogCreator.getStartIntent(this));
        }
    }

    private void showProgress() {
        mProgress = true;
        mProgressDialog = new WorkingDialog(this);
        mProgressDialog.show();
    }

    private void dismissProgress() {
        if (mProgressBar != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
        mProgress = false;
    }

    private void startGetCapabilityAndFinishTask(MyPrinter myPrinter) {
        mMyPrinterForSimpleApDisconnect = myPrinter.getIp();
        interruptSearch();
        mGetScannerCapabilityAndFinishTask = new GetScannerCapabilityAndFinishTask(mEscanLib, myPrinter);
        mGetScannerCapabilityAndFinishTask.execute(new Void[0]);
    }

    private void disconnectSimpleAp() {
        WiFiDirectManager.disconnect(getApplicationContext(), WiFiDirectManager.DEVICE_TYPE_SCANNER, mMyPrinterForSimpleApDisconnect);
        WiFiDirectManager.disconnectSimpleAP(getApplicationContext(), WiFiDirectManager.getCurSSID(this), mMyPrinterForSimpleApDisconnect);
    }

    public static Intent getStartIntent(Context context, String str, int i, String str2) {
        Intent intent = new Intent(context, ScanSearchActivity.class);
        intent.putExtra(PARAM_KEY_SCANNER_LOCATION, i);
        intent.putExtra(PARAM_KEY_SCANNER_ID, str);
        intent.putExtra(PARAM_KEY_SCANNER_SSID, str2);
        return intent;
    }

    private class FindScannerTask extends AsyncTask<Void, Void, Boolean> {
        private CancelableFind mCancelableFind = new CancelableFind();

        protected void onCancelled(Boolean bool) {
        }

        FindScannerTask(escanLib escanlib) {
            mCancelableFind.setEscanLib(escanlib);
        }

        protected Boolean doInBackground(Void... voidArr) {
            if (ScanSearchActivity.mScannerFindCancelRequested) {
                return true;
            }
            try {
                ScanSearchActivity.sEscanLibSemaphore.acquire();
                mCancelableFind.setEscanLib(ScanSearchActivity.mEscanLib);
                mCancelableFind.findScanner(ScanSearchActivity.this.getApplicationContext(), ScanSearchActivity.mHandler);
            } catch (InterruptedException unused) {
            } catch (Throwable th) {
                ScanSearchActivity.sEscanLibSemaphore.release();
                throw th;
            }
            ScanSearchActivity.sEscanLibSemaphore.release();
            return true;
        }

        protected void onPostExecute(Boolean bool) {
            boolean unused = ScanSearchActivity.this.isFinishSearchScanner = true;
            if (!mCancelableFind.isCanceled() && !ScanSearchActivity.this.isSearchSimpleAp) {
                ScanSearchActivity.this.updateFindScannerResult();
            }
        }

        public void cancelFind() {
            mCancelableFind.cancel();
        }
    }

    private class GetScannerCapabilityAndFinishTask extends AsyncTask<Void, Void, Boolean> {
        private boolean mCancelRequested;
        private CancelablePropertyTaker mCancelablePropertyTaker;
        private EscanLibException mEscanErrorCode = null;
        private escanLib mEscanLib;
        private MyPrinter mMyPrinter;
        private ScannerPropertyWrapper mScannerPropertyWrapper;

        public GetScannerCapabilityAndFinishTask(escanLib escanlib, MyPrinter myPrinter) {
            mEscanLib = escanlib;
            mMyPrinter = myPrinter;
        }

        protected Boolean doInBackground(Void... voidArr) {
            if (ScanSearchActivity.mActivityBackRequested) {
                return false;
            }
            try {
                ScanSearchActivity.sEscanLibSemaphore.acquire();
                if (!ScanSearchActivity.mActivityBackRequested) {
                    if (!mCancelRequested) {
                        synchronized (this) {
                            mCancelablePropertyTaker = new CancelablePropertyTaker(mEscanLib);
                        }
                        mScannerPropertyWrapper = mCancelablePropertyTaker.getScannerProperty(mMyPrinter);
                        if (!ScanSearchActivity.mActivityBackRequested) {
                            if (!mCancelRequested) {
                                if (mScannerPropertyWrapper == null) {
                                    mEscanErrorCode = new EscanLibException(1, -1300);
                                    ScanSearchActivity.this.disconnectSimpleAp();
                                    ScanSearchActivity.sEscanLibSemaphore.release();
                                    return false;
                                }
                                mEscanLib.escanWrapperReleaseDriver();
                                ScanSearchActivity.this.disconnectSimpleAp();
                                ScanSearchActivity.sEscanLibSemaphore.release();
                                return true;
                            }
                        }
                        ScanSearchActivity.this.disconnectSimpleAp();
                        ScanSearchActivity.sEscanLibSemaphore.release();
                        return false;
                    }
                }
                ScanSearchActivity.this.disconnectSimpleAp();
                ScanSearchActivity.sEscanLibSemaphore.release();
                return false;
            } catch (EscanLibException e) {
                try {
                    mEscanErrorCode = e;
                    return false;
                } finally {
                    ScanSearchActivity.this.disconnectSimpleAp();
                    ScanSearchActivity.sEscanLibSemaphore.release();
                }
            } catch (InterruptedException unused) {
            }
        }

        protected void onPostExecute(Boolean bool) {
            EscanLibException escanLibException;
            if (!ScanSearchActivity.this.isFinishing()) {
                ScanSearchActivity.this.dismissProgress();
                if (!bool.booleanValue()) {
                    ScanSearchActivity.this.forceSetPrinterSearchEnd();
                    if (!mCancelRequested && (escanLibException = mEscanErrorCode) != null) {
                        ScanSearchActivity.this.showErrorDialog(escanLibException);
                        return;
                    }
                    return;
                }
                ScanSearchActivity.this.localFinish(mScannerPropertyWrapper);
            }
        }

        protected void onPreExecute() {
            ScanSearchActivity.this.showProgress();
        }

        private void cancelTask() {
            mCancelRequested = true;
            synchronized (this) {
                if (mCancelablePropertyTaker != null) {
                    mCancelablePropertyTaker.cancel();
                }
            }
        }
    }

    class CancelWaitTask extends AsyncTask<Void, Void, Void> {
        CancelWaitTask() {
        }

        protected Void doInBackground(Void... voidArr) {
            try {
                ScanSearchActivity.sEscanLibSemaphore.acquire();
                ScanSearchActivity.this.disconnectSimpleAp();
                ScanSearchActivity.sEscanLibSemaphore.release();
                return null;
            } catch (InterruptedException unused) {
                return null;
            }
        }

        protected void onPostExecute(Void voidR) {
            ScanSearchActivity.this.releaseAndFinish();
        }
    }
}
