package com.epson.mobilephone.common.wifidirect;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Handler;

import epson.common.Constants;

public class WiFiDirectManager {
    public static final String DEVICE_TYPE_OTHER = "other";
    public static final String DEVICE_TYPE_PRINTER = "printer";
    public static final String DEVICE_TYPE_SCANNER = "scanner";
    private static final String TAG = "WiFiDirectManager";
    public static final int TIMEOUT_CONNECTING = 90;
    public static final int TIMEOUT_WIFIDIRECTMODE = 120;
    static boolean bCheckDebuggable = false;
    static boolean bSimpleAPCreated = false;

    static void checkDebuggable(Context context) {
        if (!bCheckDebuggable) {
            EPLog.isDebuggable(context);
            bCheckDebuggable = true;
        }
    }

    public static boolean isWifiEnabled(Context context) {
        return ((WifiManager) context.getSystemService(Service.WIFI_SERVICE)).isWifiEnabled();
    }

    public static boolean isSimpleAP(Context context) {
        checkDebuggable(context);
        return WiFiControl.isSimpleAP(WiFiUtils.getInstance(context).getCurSSID());
    }

    public static boolean isWifiDirectFY13(Context context) {
        checkDebuggable(context);
        return WiFiControl.isSimpleAPFY13WiFiDirect(WiFiUtils.getInstance(context).getCurSSID());
    }

    public static boolean isWifiDirectP2P(Context context) {
        return WifiP2pUtils.getInstance(context).isConnectedWiFiP2P();
    }

    public static String getCurSSID(Context context) {
        return WiFiUtils.getInstance(context).getCurSSID();
    }

    public static void enableWiFi(Activity activity, int i) {
        Intent intent = new Intent(activity, ActivityControlWiFi.class);
        intent.putExtra(ActivityControlWiFi.SHOWPROGRESS, true);
        intent.putExtra(ActivityControlWiFi.SHOWWIFISETTINGS, false);
        activity.startActivityForResult(intent, i);
    }

    public static void setPriorityToSimpleAP(Context context, boolean z) {
        if (z) {
            ManageDefaultNetwork.getInstance(context).setDefaultNetworkSimpleAp();
        } else {
            ManageDefaultNetwork.getInstance(context).resetDefaultNetwork();
        }
    }

    public static boolean isNeedConnect(Context context, String str) {
        checkDebuggable(context);
        String connectInfo = getConnectInfo(context, str);
        if (connectInfo == null) {
            return false;
        }
        return WiFiControl.getInstance(context).isNeedConnect(connectInfo);
    }

    public static WiFiControl.ConnectType getConnectType(Context context, String str) {
        return WiFiControl.getSSIDType(str);
    }

    public static String getCurConnectInfo(Context context, String str) {
        return WiFiControl.getInstance(context).getCurConnectInfo(str);
    }

    public static String getConnectString(Context context, String str, String str2) {
        return WiFiControl.getInstance(context).getEstimateSSID(str, str2);
    }

    public static boolean connect(Activity activity, String str, int i) {
        Intent intent;
        checkDebuggable(activity);
        EPLog.d(TAG, "connect()");
        String removeSSIDPrefix = WiFiControl.removeSSIDPrefix(str);
        if (C11201.f154x793e0ec3[WiFiControl.getSSIDType(str).ordinal()] != 1) {
            intent = new Intent(activity, ActivityConnectSimpleAP.class);
        } else {
            intent = new Intent(activity, ActivityConnectP2P.class);
        }
        intent.putExtra(ActivityConnectBase.APNAME, removeSSIDPrefix);
        intent.putExtra(ActivityConnectBase.CREATE, false);
        intent.putExtra(ActivityConnectBase.SHOWERROR, true);
        intent.putExtra(ActivityConnectBase.TIMEOUT, 90);
        intent.putExtra(ActivityConnectBase.NEEDINFO, true);
        activity.startActivityForResult(intent, i);
        return true;
    }

    /* renamed from: com.epson.mobilephone.common.wifidirect.WiFiDirectManager$1 */
    static /* synthetic */ class C11201 {

        /* renamed from: $SwitchMap$com$epson$mobilephone$common$wifidirect$WiFiControl$ConnectType */
        static final /* synthetic */ int[] f154x793e0ec3 = new int[WiFiControl.ConnectType.values().length];

        /* JADX WARNING: Can't wrap try/catch for region: R(6:0|1|2|3|4|6) */
        /* JADX WARNING: Code restructure failed: missing block: B:7:?, code lost:
            return;
         */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x0014 */
        static {
            /*
                com.epson.mobilephone.common.wifidirect.WiFiControl$ConnectType[] r0 = com.epson.mobilephone.common.wifidirect.WiFiControl.ConnectType.values()
                int r0 = r0.length
                int[] r0 = new int[r0]
                f154x793e0ec3 = r0
                int[] r0 = f154x793e0ec3     // Catch:{ NoSuchFieldError -> 0x0014 }
                com.epson.mobilephone.common.wifidirect.WiFiControl$ConnectType r1 = com.epson.mobilephone.common.wifidirect.WiFiControl.ConnectType.WiFiP2P     // Catch:{ NoSuchFieldError -> 0x0014 }
                int r1 = r1.ordinal()     // Catch:{ NoSuchFieldError -> 0x0014 }
                r2 = 1
                r0[r1] = r2     // Catch:{ NoSuchFieldError -> 0x0014 }
            L_0x0014:
                int[] r0 = f154x793e0ec3     // Catch:{ NoSuchFieldError -> 0x001f }
                com.epson.mobilephone.common.wifidirect.WiFiControl$ConnectType r1 = com.epson.mobilephone.common.wifidirect.WiFiControl.ConnectType.SimpleAP     // Catch:{ NoSuchFieldError -> 0x001f }
                int r1 = r1.ordinal()     // Catch:{ NoSuchFieldError -> 0x001f }
                r2 = 2
                r0[r1] = r2     // Catch:{ NoSuchFieldError -> 0x001f }
            L_0x001f:
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.wifidirect.WiFiDirectManager.C11201.<clinit>():void");
        }
    }

    public static boolean reconnect(Activity activity, String str, int i) {
        Intent intent;
        checkDebuggable(activity);
        EPLog.d(TAG, "reconnect()");
        String connectInfo = getConnectInfo(activity, str);
        if (connectInfo == null) {
            EPLog.d(TAG, "getConnectInfo == null");
            return false;
        }
        String removeSSIDPrefix = WiFiControl.removeSSIDPrefix(connectInfo);
        if (C11201.f154x793e0ec3[WiFiControl.getSSIDType(connectInfo).ordinal()] != 1) {
            intent = new Intent(activity, ActivityConnectSimpleAP.class);
        } else {
            intent = new Intent(activity, ActivityConnectP2P.class);
        }
        intent.putExtra(ActivityConnectBase.APNAME, removeSSIDPrefix);
        intent.putExtra(ActivityConnectBase.CREATE, false);
        intent.putExtra(ActivityConnectBase.SHOWERROR, true);
        intent.putExtra(ActivityConnectBase.TIMEOUT, 90);
        activity.startActivityForResult(intent, i);
        return true;
    }

    public static boolean disconnect(Context context, String str, String str2) {
        int networkId;
        checkDebuggable(context);
        String connectInfo = getConnectInfo(context, str);
        if (connectInfo == null) {
            return false;
        }
        if (C11201.f154x793e0ec3[WiFiControl.getSSIDType(connectInfo).ordinal()] == 1) {
            return true;
        }
        String removeSSIDPrefix = WiFiControl.removeSSIDPrefix(connectInfo);
        if (!WiFiControl.isSimpleAP(removeSSIDPrefix)) {
            return false;
        }
        WiFiUtils instance = WiFiUtils.getInstance(context);
        if (!removeSSIDPrefix.equals(instance.getCurSSID()) || (networkId = instance.getNetworkId(removeSSIDPrefix)) == -1) {
            return false;
        }
        EPLog.d(TAG, "disconnect() deviceType = " + str + " printerIp = " + str2);
        WiFiUtils.setAutoGoTimeout(str2, 120);
        if (bSimpleAPCreated) {
            bSimpleAPCreated = false;
            if (!instance.invalidateSimpleAP(context, networkId)) {
                return false;
            }
        } else if (!instance.disableSimpleAP(context, networkId)) {
            return false;
        }
        return true;
    }

    public static SearchWiFiDirectPrinterTask searchWiFiDirectPrinter(Context context, Handler handler, int i, int i2) {
        SearchWiFiDirectPrinterTask instance = SearchWiFiDirectPrinterTask.getInstance(context);
        if (instance.start(3, handler, i, i2)) {
            return instance;
        }
        return null;
    }

    public static String getConnectInfo(Context context, String str) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.PREFS_WIFI, 0);
        if (str == null || str.length() == 0) {
            EPLog.e(TAG, "getConnectInfo Ilegal param");
            return null;
        }
        String string = sharedPreferences.getString(Constants.PREFS_WIFI_AUTOCONNECT_SSID + str, (String) null);
        if (string == null) {
            if (DEVICE_TYPE_PRINTER.equals(str)) {
                string = sharedPreferences.getString("AUTOCONNECT_SSID.other", (String) null);
            } else if (DEVICE_TYPE_OTHER.equals(str)) {
                string = sharedPreferences.getString("AUTOCONNECT_SSID.printer", (String) null);
            }
        }
        if (string != null) {
            return WiFiControl.rebuildSSID(string);
        }
        return string;
    }

    public static void setConnectInfo(Context context, String str, String str2, String str3) {
        if (str == null) {
            EPLog.e(TAG, "SSID Ilegal param");
            return;
        }
        String rebuildSSID = WiFiControl.rebuildSSID(str);
        EPLog.d(TAG, "setConnectInfo :" + rebuildSSID);
        if (str2 == null || str2.length() == 0) {
            EPLog.e(TAG, "setConnectInfo Ilegal param");
            return;
        }
        SharedPreferences.Editor edit = context.getSharedPreferences(Constants.PREFS_WIFI, 0).edit();
        edit.putString(Constants.PREFS_WIFI_AUTOCONNECT_SSID + str2, rebuildSSID);
        edit.commit();
        SimpleAPInfoDB.SimpleAPInfo simpleAPInfo = new SimpleAPInfoDB.SimpleAPInfo();
        simpleAPInfo.ssid = rebuildSSID;
        simpleAPInfo.printerName = str3;
        SimpleAPInfoDB.updateSimpleAPInfoDB(context, simpleAPInfo);
    }

    public static void resetConnectInfo(Context context, String str) {
        EPLog.d(TAG, "resetConnectInfo :" + str);
        if (str == null || str.length() == 0) {
            EPLog.e(TAG, "resetConnectInfo Ilegal param");
            return;
        }
        SharedPreferences.Editor edit = context.getSharedPreferences(Constants.PREFS_WIFI, 0).edit();
        edit.remove(Constants.PREFS_WIFI_AUTOCONNECT_SSID + str);
        edit.commit();
    }

    public static boolean connectSimpleAP(Activity activity, String str, String str2, int i) {
        checkDebuggable(activity);
        EPLog.d(TAG, "connectSimpleAP()");
        String removeSSIDPrefix = WiFiControl.removeSSIDPrefix(str);
        Intent intent = new Intent(activity, ActivityConnectSimpleAP.class);
        intent.putExtra(ActivityConnectBase.APNAME, removeSSIDPrefix);
        intent.putExtra(ActivityConnectBase.APPASS, str2);
        intent.putExtra(ActivityConnectBase.CREATE, true);
        intent.putExtra(ActivityConnectBase.SHOWERROR, false);
        intent.putExtra(ActivityConnectBase.TIMEOUT, 90);
        intent.putExtra(ActivityConnectBase.NEEDINFO, false);
        activity.startActivityForResult(intent, i);
        return true;
    }

    public static boolean disconnectSimpleAP(Context context, String str, String str2) {
        int networkId;
        checkDebuggable(context);
        if (str == null) {
            return false;
        }
        String removeSSIDPrefix = WiFiControl.removeSSIDPrefix(str);
        if (!WiFiControl.isSimpleAP(removeSSIDPrefix)) {
            return false;
        }
        WiFiUtils instance = WiFiUtils.getInstance(context);
        if (!removeSSIDPrefix.equals(instance.getCurSSID()) || (networkId = instance.getNetworkId(removeSSIDPrefix)) == -1) {
            return false;
        }
        EPLog.d(TAG, "disconnectSimpleAP() ssid = " + removeSSIDPrefix + " printerIp = " + str2);
        WiFiUtils.setAutoGoTimeout(str2, 120);
        if (!instance.disableSimpleAP(context, networkId)) {
            return false;
        }
        return true;
    }

    public static void setFlagSimpleAPCreated() {
        bSimpleAPCreated = true;
    }

    public static int getLastDetailError() {
        return ActivityConnectSimpleAP.getLastDetailResult();
    }
}
