package com.epson.mobilephone.common.wifidirect;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;

public class ActivityControlWiFi extends Activity {
    public static final int SCANNING_TIMEOUT = 10000;
    public static final String SHOWPROGRESS = "com.epson.iprint.wifidirect.progress";
    public static final String SHOWWIFISETTINGS = "com.epson.iprint.wifidirect.wifisettings";
    public static final String TAG = "ActivityControlWiFi";
    final int IDD_NO_WIFI = 0;
    final int ID_SHOWWIFISETTINGS = 0;
    final int MAX_RETRY_SCANNING = 5;
    WifiManager mWifiManager = null;
    NetworkStateChangeReciever networkStateChangeReciever = null;
    int scancount = 0;
    ScanningObserver scannigObserver = new ScanningObserver();
    boolean showProgress = false;
    boolean showWifiSettings = false;

    protected void onCreate(Bundle bundle) {
        EPLog.m86d(TAG, "onCreate()");
        super.onCreate(bundle);
        mWifiManager = (WifiManager) getApplicationContext().getSystemService("wifi");
        requestWindowFeature(1);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            showProgress = extras.getBoolean(SHOWPROGRESS, false);
            showWifiSettings = extras.getBoolean(SHOWWIFISETTINGS, false);
        }
        if (this.showProgress) {
            setContentView(C1113R.layout.progress);
        }
        if (this.showWifiSettings) {
            WiFiUtils.showOsWifiSettings(this, 0);
        } else if (!mWifiManager.isWifiEnabled()) {
            showDialog(0);
        } else {
            EPLog.m86d(TAG, "Already WiFi Enabled");
            onOK();
        }
    }

    protected void onResume() {
        EPLog.m86d(TAG, "onResume()");
        super.onResume();
        NfcDispatchUtils.enableForegroundDispatch(this, (IntentFilter[]) null, (String[][]) null);
    }

    protected void onPause() {
        super.onPause();
        NfcDispatchUtils.disableForegroundDispatch(this);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        EPLog.m86d(TAG, "onActivityResult()");
        super.onActivityResult(i, i2, intent);
        if (i == 0) {
            switch (mWifiManager.getWifiState()) {
                case 2:
                case 3:
                    registerReciever();
                    mWifiManager.startScan();
                    return;
                default:
                    onCancel();
                    return;
            }
        }
    }

    protected void onDestroy() {
        EPLog.m86d(TAG, "onDestroy()");
        super.onDestroy();
        if (this.networkStateChangeReciever != null) {
            EPLog.m86d(TAG, "unregisterReceiver()");
            unregisterReceiver(this.networkStateChangeReciever);
            networkStateChangeReciever = null;
        }
        scannigObserver.interrunpt();
    }

    protected Dialog onCreateDialog(int i) {
        return i != 0 ? super.onCreateDialog(i) : new AlertDialog.Builder(this).setPositiveButton(getString(17039370), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                ActivityControlWiFi.this.registerReciever();
                boolean wifiEnabled = ActivityControlWiFi.mWifiManager.setWifiEnabled(true);
                EPLog.m86d(ActivityControlWiFi.TAG, "setWifiEnabled return " + wifiEnabled);
                if (!wifiEnabled) {
                    WiFiUtils.showOsWifiSettings(ActivityControlWiFi.this, 0);
                }
            }
        }).setNegativeButton(getString(17039360), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                ActivityControlWiFi.this.onCancel();
            }
        }).setMessage(getString(C1113R.string.str_no_wifi)).setCancelable(false).create();
    }

    private void onOK() {
        EPLog.m86d(TAG, "finish():RESULT_OK");
        setResult(-1);
        finish();
    }

    private void onCancel() {
        EPLog.m86d(TAG, "finish():RESULT_CANCELED");
        setResult(0);
        finish();
    }

    private void registerReciever() {
        if (this.networkStateChangeReciever == null) {
            EPLog.m86d(TAG, "registerReceiver()");
            networkStateChangeReciever = new NetworkStateChangeReciever();
            registerReceiver(this.networkStateChangeReciever, new IntentFilter("android.net.wifi.STATE_CHANGE"));
            registerReceiver(this.networkStateChangeReciever, new IntentFilter("android.net.wifi.WIFI_STATE_CHANGED"));
            registerReceiver(this.networkStateChangeReciever, new IntentFilter("android.net.wifi.supplicant.STATE_CHANGE"));
        }
    }

    class NetworkStateChangeReciever extends BroadcastReceiver {
        NetworkStateChangeReciever() {
        }

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("android.net.wifi.WIFI_STATE_CHANGED")) {
                EPLog.m86d(ActivityControlWiFi.TAG, String.format("WiFi State Change : wifiState = %d", new Object[]{Integer.valueOf(intent.getIntExtra("wifi_state", 4))}));
            } else if (action.equals("android.net.wifi.STATE_CHANGE")) {
                NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra("networkInfo");
                EPLog.m86d(ActivityControlWiFi.TAG, "Network State Changed:" + networkInfo.toString());
                if (ActivityControlWiFi.mWifiManager.isWifiEnabled()) {
                    switch (C10903.$SwitchMap$android$net$NetworkInfo$State[networkInfo.getState().ordinal()]) {
                        case 1:
                            ActivityControlWiFi.this.scannigObserver.interrunpt();
                            ActivityControlWiFi.this.onOK();
                            return;
                        case 2:
                            ActivityControlWiFi.this.scannigObserver.interrunpt();
                            return;
                        default:
                            return;
                    }
                }
            } else if (action.equals("android.net.wifi.supplicant.STATE_CHANGE")) {
                SupplicantState supplicantState = (SupplicantState) intent.getParcelableExtra("newState");
                EPLog.m86d(ActivityControlWiFi.TAG, "Supplicant State Changed: State = " + supplicantState.toString());
                switch (C10903.$SwitchMap$android$net$wifi$SupplicantState[supplicantState.ordinal()]) {
                    case 1:
                        ActivityControlWiFi.this.scancount++;
                        if (ActivityControlWiFi.this.scancount > 5) {
                            ActivityControlWiFi.this.onOK();
                            return;
                        } else {
                            ActivityControlWiFi.this.scannigObserver.start();
                            return;
                        }
                    case 2:
                        ActivityControlWiFi.this.scannigObserver.interrunpt();
                        return;
                    case 3:
                    case 4:
                        ActivityControlWiFi.this.onOK();
                        return;
                    case 5:
                        ActivityControlWiFi.this.scannigObserver.start();
                        return;
                    default:
                        return;
                }
            }
        }
    }

    /* renamed from: com.epson.mobilephone.common.wifidirect.ActivityControlWiFi$3 */
    static /* synthetic */ class C10903 {
        static final /* synthetic */ int[] $SwitchMap$android$net$NetworkInfo$State = new int[NetworkInfo.State.values().length];
        static final /* synthetic */ int[] $SwitchMap$android$net$wifi$SupplicantState = new int[SupplicantState.values().length];

        /* JADX WARNING: Can't wrap try/catch for region: R(17:0|(2:1|2)|3|5|6|7|8|9|10|11|12|13|15|16|17|18|20) */
        /* JADX WARNING: Can't wrap try/catch for region: R(18:0|1|2|3|5|6|7|8|9|10|11|12|13|15|16|17|18|20) */
        /* JADX WARNING: Code restructure failed: missing block: B:21:?, code lost:
            return;
         */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:11:0x0035 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:17:0x0053 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x001f */
        /* JADX WARNING: Missing exception handler attribute for start block: B:9:0x002a */
        static {
            /*
                android.net.wifi.SupplicantState[] r0 = android.net.wifi.SupplicantState.values()
                int r0 = r0.length
                int[] r0 = new int[r0]
                $SwitchMap$android$net$wifi$SupplicantState = r0
                r0 = 1
                int[] r1 = $SwitchMap$android$net$wifi$SupplicantState     // Catch:{ NoSuchFieldError -> 0x0014 }
                android.net.wifi.SupplicantState r2 = android.net.wifi.SupplicantState.SCANNING     // Catch:{ NoSuchFieldError -> 0x0014 }
                int r2 = r2.ordinal()     // Catch:{ NoSuchFieldError -> 0x0014 }
                r1[r2] = r0     // Catch:{ NoSuchFieldError -> 0x0014 }
            L_0x0014:
                r1 = 2
                int[] r2 = $SwitchMap$android$net$wifi$SupplicantState     // Catch:{ NoSuchFieldError -> 0x001f }
                android.net.wifi.SupplicantState r3 = android.net.wifi.SupplicantState.ASSOCIATING     // Catch:{ NoSuchFieldError -> 0x001f }
                int r3 = r3.ordinal()     // Catch:{ NoSuchFieldError -> 0x001f }
                r2[r3] = r1     // Catch:{ NoSuchFieldError -> 0x001f }
            L_0x001f:
                int[] r2 = $SwitchMap$android$net$wifi$SupplicantState     // Catch:{ NoSuchFieldError -> 0x002a }
                android.net.wifi.SupplicantState r3 = android.net.wifi.SupplicantState.INACTIVE     // Catch:{ NoSuchFieldError -> 0x002a }
                int r3 = r3.ordinal()     // Catch:{ NoSuchFieldError -> 0x002a }
                r4 = 3
                r2[r3] = r4     // Catch:{ NoSuchFieldError -> 0x002a }
            L_0x002a:
                int[] r2 = $SwitchMap$android$net$wifi$SupplicantState     // Catch:{ NoSuchFieldError -> 0x0035 }
                android.net.wifi.SupplicantState r3 = android.net.wifi.SupplicantState.DORMANT     // Catch:{ NoSuchFieldError -> 0x0035 }
                int r3 = r3.ordinal()     // Catch:{ NoSuchFieldError -> 0x0035 }
                r4 = 4
                r2[r3] = r4     // Catch:{ NoSuchFieldError -> 0x0035 }
            L_0x0035:
                int[] r2 = $SwitchMap$android$net$wifi$SupplicantState     // Catch:{ NoSuchFieldError -> 0x0040 }
                android.net.wifi.SupplicantState r3 = android.net.wifi.SupplicantState.DISCONNECTED     // Catch:{ NoSuchFieldError -> 0x0040 }
                int r3 = r3.ordinal()     // Catch:{ NoSuchFieldError -> 0x0040 }
                r4 = 5
                r2[r3] = r4     // Catch:{ NoSuchFieldError -> 0x0040 }
            L_0x0040:
                android.net.NetworkInfo$State[] r2 = android.net.NetworkInfo.State.values()
                int r2 = r2.length
                int[] r2 = new int[r2]
                $SwitchMap$android$net$NetworkInfo$State = r2
                int[] r2 = $SwitchMap$android$net$NetworkInfo$State     // Catch:{ NoSuchFieldError -> 0x0053 }
                android.net.NetworkInfo$State r3 = android.net.NetworkInfo.State.CONNECTED     // Catch:{ NoSuchFieldError -> 0x0053 }
                int r3 = r3.ordinal()     // Catch:{ NoSuchFieldError -> 0x0053 }
                r2[r3] = r0     // Catch:{ NoSuchFieldError -> 0x0053 }
            L_0x0053:
                int[] r0 = $SwitchMap$android$net$NetworkInfo$State     // Catch:{ NoSuchFieldError -> 0x005d }
                android.net.NetworkInfo$State r2 = android.net.NetworkInfo.State.CONNECTING     // Catch:{ NoSuchFieldError -> 0x005d }
                int r2 = r2.ordinal()     // Catch:{ NoSuchFieldError -> 0x005d }
                r0[r2] = r1     // Catch:{ NoSuchFieldError -> 0x005d }
            L_0x005d:
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.wifidirect.ActivityControlWiFi.C10903.<clinit>():void");
        }
    }

    class ScanningObserver {
        AsyncTask<Void, Void, Void> observerTask = null;

        ScanningObserver() {
        }

        private void start() {
            interrunpt();
            EPLog.m86d(ActivityControlWiFi.TAG, "Start ScanningObserver");
            observerTask = new AsyncTask<Void, Void, Void>() {
                protected Void doInBackground(Void... voidArr) {
                    int i = 0;
                    while (i < 10000) {
                        try {
                            Thread.sleep(100);
                            i += 100;
                            if (isCancelled()) {
                                return null;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                    return null;
                }

                protected void onPostExecute(Void voidR) {
                    EPLog.m88i(ActivityControlWiFi.TAG, "Timeout ScanningObserver");
                    ActivityControlWiFi.this.onOK();
                }
            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        }

        private void interrunpt() {
            AsyncTask<Void, Void, Void> asyncTask = observerTask;
            if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING && !this.observerTask.isCancelled()) {
                EPLog.m86d(ActivityControlWiFi.TAG, "Stop ScanningObserver");
                observerTask.cancel(false);
            }
        }
    }
}
