package com.epson.mobilephone.common.ble;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;

import com.epson.mobilephone.common.ble.util.BLEUtility;
import com.epson.mobilephone.common.ble.util.ScannedDevice;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class BleAsyncTask extends AsyncTask<Void, Void, Boolean> {
    private static AlertDialog dialog = null;
    private static HandlerThread handlerThread = null;
    private static boolean mStop = false;
    private static BleAsyncTask mTask;
    private static Runnable mTimeOutTask;
    private BleScanWork mBleWork = null;
    private BLEUtility.BleWorkCallback mCallBack = null;
    private List<ScannedDevice> mDevicelist = null;
    private Boolean mFind = false;
    private long mTimeOut = -1;
    private long mTimeOutScan = -1;
    private Activity m_Activity;

    public static void setStop(boolean z) {
        EpLog.d("mStop   " + z);
        mStop = z;
        if (z) {
            EpLog.d("★ stopScan");
            BleScanWork.getInstace().stopScan();
            if (mTimeOutTask != null) {
                mTimeOutTask = null;
                BleAsyncTask bleAsyncTask = mTask;
                if (bleAsyncTask != null) {
                    bleAsyncTask.cancel(true);
                }
            }
        }
    }

    public BleAsyncTask(Activity activity) {
        mTimeOutTask = new Runnable() {
            public void run() {
                try {
                    BleAsyncTask.this.get(BleAsyncTask.mTimeOut, TimeUnit.MILLISECONDS);
                } catch (InterruptedException unused) {
                    EpLog.e("InterruptedException");
                } catch (ExecutionException unused2) {
                    EpLog.e("ExecutionException");
                } catch (TimeoutException unused3) {
                    EpLog.m77i("Running task has timed out!!");
                    if (BleAsyncTask.mBleWork != null) {
                        BleAsyncTask.mBleWork.stopScan();
                    }
                    BleAsyncTask.mTask.cancel(true);
                } catch (CancellationException unused4) {
                    EpLog.e("CancellationException");
                }
            }
        };
        m_Activity = activity;
        mTimeOut = (long) (BleWork.getScanAdvertisingPacketTimeout() + 2000);
        mTimeOutScan = -1;
    }

    public BleAsyncTask(Activity activity, BLEUtility.BleWorkCallback bleWorkCallback, int i) {
        mTimeOutTask = new Runnable() {
            public void run() {
                try {
                    BleAsyncTask.this.get(BleAsyncTask.mTimeOut, TimeUnit.MILLISECONDS);
                } catch (InterruptedException unused) {
                    EpLog.e("InterruptedException");
                } catch (ExecutionException unused2) {
                    EpLog.e("ExecutionException");
                } catch (TimeoutException unused3) {
                    EpLog.m77i("Running task has timed out!!");
                    if (BleAsyncTask.mBleWork != null) {
                        BleAsyncTask.mBleWork.stopScan();
                    }
                    BleAsyncTask.mTask.cancel(true);
                } catch (CancellationException unused4) {
                    EpLog.e("CancellationException");
                }
            }
        };
        m_Activity = activity;
        mTimeOut = (long) (i + 2000);
        mTimeOutScan = (long) i;
        mCallBack = bleWorkCallback;
    }

    protected void onPreExecute() {
        super.onPreExecute();
        mBleWork = BleScanWork.getInstace();
    }

    /* access modifiers changed from: protected */
    /* JADX WARNING: Removed duplicated region for block: B:14:0x0072  */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x00ac A[EDGE_INSN: B:26:0x00ac->B:24:0x00ac ?: BREAK  , SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public java.lang.Boolean doInBackground(java.lang.Void... r5) {
        /*
            r4 = this;
            java.lang.StringBuilder r5 = new java.lang.StringBuilder
            r5.<init>()
            java.lang.String r0 = "mStop  "
            r5.append(r0)
            boolean r0 = mStop
            r5.append(r0)
            java.lang.String r5 = r5.toString()
            com.epson.mobilephone.common.EpLog.m77i(r5)
            boolean r5 = mStop
            if (r5 == 0) goto L_0x001d
            java.lang.Boolean r5 = r4.mFind
            return r5
        L_0x001d:
            java.lang.Thread r5 = java.lang.Thread.currentThread()
            java.lang.String r0 = "BleAsyncTask"
            r5.setName(r0)
            com.epson.mobilephone.common.EpLog.m68d()
            java.lang.Thread r5 = java.lang.Thread.currentThread()
            java.lang.String r5 = r5.getName()
            com.epson.mobilephone.common.EpLog.m77i(r5)
            mTask = r4
            long r0 = r4.mTimeOut
            r2 = 0
            int r5 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
            if (r5 <= 0) goto L_0x0048
            java.lang.Thread r5 = new java.lang.Thread
            java.lang.Runnable r0 = mTimeOutTask
            r5.<init>(r0)
            r5.start()
        L_0x0048:
            com.epson.mobilephone.common.ble.BleScanWork r5 = r4.mBleWork
            android.app.Activity r0 = r4.m_Activity
            boolean r5 = r5.init_scan(r0)
            if (r5 != 0) goto L_0x005a
            java.lang.String r5 = "BLE is not available !!"
            com.epson.mobilephone.common.EpLog.m82w(r5)
            java.lang.Boolean r5 = r4.mFind
            return r5
        L_0x005a:
            java.lang.String r5 = "BLE is available!!"
            com.epson.mobilephone.common.EpLog.m77i(r5)
            com.epson.mobilephone.common.ble.BleScanWork r5 = r4.mBleWork
            com.epson.mobilephone.common.ble.BleAsyncTask$2 r0 = new com.epson.mobilephone.common.ble.BleAsyncTask$2
            r0.<init>()
            com.epson.mobilephone.common.ble.BleAsyncTask$3 r1 = new com.epson.mobilephone.common.ble.BleAsyncTask$3
            r1.<init>()
            r5.search(r0, r1)
        L_0x006e:
            boolean r5 = mStop
            if (r5 != 0) goto L_0x00ac
            java.lang.StringBuilder r5 = new java.lang.StringBuilder
            r5.<init>()
            java.lang.String r0 = "生きてるんです. mStop = "
            r5.append(r0)
            boolean r0 = mStop
            r5.append(r0)
            java.lang.String r5 = r5.toString()
            com.epson.mobilephone.common.EpLog.d(r5)
            r0 = 2000(0x7d0, double:9.88E-321)
            java.lang.Thread.sleep(r0)     // Catch:{ InterruptedException -> 0x008e }
            goto L_0x0093
        L_0x008e:
            java.lang.String r5 = "InterruptedException"
            com.epson.mobilephone.common.EpLog.e((java.lang.String) r5)
        L_0x0093:
            boolean r5 = r4.isCancelled()
            if (r5 != 0) goto L_0x009d
            boolean r5 = mStop
            if (r5 == 0) goto L_0x006e
        L_0x009d:
            java.lang.String r5 = "死にます"
            com.epson.mobilephone.common.EpLog.d(r5)
            java.lang.String r5 = "★ stopScan"
            com.epson.mobilephone.common.EpLog.d(r5)
            com.epson.mobilephone.common.ble.BleScanWork r5 = r4.mBleWork
            r5.stopScan()
        L_0x00ac:
            java.lang.StringBuilder r5 = new java.lang.StringBuilder
            r5.<init>()
            java.lang.String r0 = "見つかった？ "
            r5.append(r0)
            java.lang.Boolean r0 = r4.mFind
            r5.append(r0)
            java.lang.String r5 = r5.toString()
            com.epson.mobilephone.common.EpLog.d(r5)
            java.lang.Boolean r5 = r4.mFind
            return r5
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.ble.BleAsyncTask.doInBackground(java.lang.Void[]):java.lang.Boolean");
    }

    protected void onPostExecute(Boolean bool) {
        super.onPostExecute(bool);
        EpLog.d("" + bool);
    }

    private void showAdvanceToSettingDialog() {
        if (mCallBack != null) {
            mDevicelist = mBleWork.getScannedDeviceList();
            ArrayList arrayList = new ArrayList();
            for (ScannedDevice next : mDevicelist) {
                arrayList.add(new ScannedDevice(next.getDevice(), next.getRssi(), next.getDisplayName(), next.getHardwareType()));
            }
            EpLog.d(" ★　mDevicelist " + arrayList.size());
            mCallBack.call(arrayList);
            return;
        }
        dialog = new AlertDialog.Builder(m_Activity).setCancelable(false).setMessage(m_Activity.getString(R.string.BLE_find_unconfigured_printer_title)).setPositiveButton(m_Activity.getString(R.string.BLE_find_unconfigured_printer_setup_button), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                EpLog.d("★ stopScan");
                BleAsyncTask.mBleWork.stopScan();
                BleAsyncTask bleAsyncTask = BleAsyncTask.this;
                List unused = bleAsyncTask.mDevicelist = bleAsyncTask.mBleWork.getScannedDeviceList();
                ArrayList arrayList = new ArrayList();
                for (ScannedDevice scannedDevice : BleAsyncTask.mDevicelist) {
                    arrayList.add(new ScannedDevice(scannedDevice.getDevice(), scannedDevice.getRssi(), scannedDevice.getDisplayName(), scannedDevice.getHardwareType()));
                }
                Intent intent = new Intent(BleAsyncTask.m_Activity.getApplicationContext(), BleWorkActivity.class);
                intent.putParcelableArrayListExtra(ScannedDevice.TYPE, arrayList);
                BleAsyncTask.m_Activity.startActivity(intent);
            }
        }).setNegativeButton(m_Activity.getString(R.string.BLE_find_unconfigured_printer_not_now_button), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                EpLog.d("★ stopScan");
                BleAsyncTask.mBleWork.stopScan();
                BleAsyncTask.setStop(true);
            }
        }).create();
        dialog.show();
    }

    protected void onCancelled(Boolean bool) {
        super.onCancelled(bool);
        EpLog.d("死にました。  " + bool);
    }

    protected void onCancelled() {
        super.onCancelled();
    }

    public BleAsyncTask setTimeOut(long j) {
        mTimeOut = j;
        return this;
    }

    public static void blePrinterCheck(Context context) {
        setStop(false);
        EpLog.i();
        AlertDialog alertDialog = dialog;
        if (alertDialog == null || !alertDialog.isShowing()) {
            final Activity activity = (Activity) context;
            if (BLEUtility.isThreadAlive("bleCheck")) {
                EpLog.d("isAlive bleCheck");
            } else if (handlerThread != null) {
                EpLog.e("handlerThread != null   ");
            } else {
                EpLog.m77i("★　　　　　handlerThread　start   ");
                handlerThread = new HandlerThread("bleCheck");
                handlerThread.start();
                new Handler(handlerThread.getLooper()).postDelayed(new Runnable() {
                    public void run() {
                        EpLog.m77i(" 0 handlerThread run !!!!");
                        BleAsyncTask.handlerThread.quitSafely();
                        new BleAsyncTask(activity).execute(new Void[0]);
                    }
                }, 500);
            }
        }
    }

    public static void blePrinterCheck(Context context, final BLEUtility.BleWorkCallback bleWorkCallback) {
        setStop(false);
        EpLog.i();
        final Activity activity = (Activity) context;
        if (BLEUtility.isThreadAlive("bleCheck")) {
            EpLog.d("　　　‼　isAlive bleCheck");
            return;
        }
        EpLog.m77i("handlerThread");
        handlerThread = new HandlerThread("bleCheck");
        handlerThread.start();
        new Handler(handlerThread.getLooper()).postDelayed(new Runnable() {
            public void run() {
                EpLog.m77i(" handlerThread run !!!!");
                BleAsyncTask.handlerThread.quitSafely();
                new BleAsyncTask(activity, bleWorkCallback, BleWork.getScanAdvertisingPacketTimeout()).execute(new Void[0]);
            }
        }, 1000);
    }

    public static void blePrinterCheckStop() {
        EpLog.i();
        if (handlerThread != null) {
            setStop(true);
            handlerThread.quitSafely();
            handlerThread = null;
            AlertDialog alertDialog = dialog;
            if (alertDialog != null && alertDialog.isShowing()) {
                dialog.dismiss();
            }
            Thread thread = BLEUtility.getThread("bleCheck");
            if (thread != null && thread.isAlive()) {
                EpLog.m77i("interrupt");
                thread.interrupt();
                try {
                    EpLog.m77i("join");
                    thread.join(50);
                } catch (InterruptedException e) {
                    EpLog.e(e.getMessage());
                }
            }
        } else {
            EpLog.m77i("handlerThread NULL return !!");
        }
    }
}
