package com.dowse.base.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.ResultReceiver;
import android.text.TextUtils;
import com.dowse.base.log.DSLog;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/WifiUtil.class */
public class WifiUtil {
    private static final String TAG = "WifiUtil";

    /* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/WifiUtil$CallBack.class */
    public interface CallBack {
        void connnectResult(boolean z);
    }

    public static void enableAP(Context context, String name, String password) {
        DSLog.d(TAG, "Enable Wi-Fi AP,name=" + name + ",password=" + password);
        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            WifiConfiguration wifiConfiguration = new WifiConfiguration();
            wifiConfiguration.SSID = name;
            wifiConfiguration.allowedKeyManagement.set(4);
            wifiConfiguration.allowedAuthAlgorithms.set(0);
            wifiConfiguration.preSharedKey = password;
            if (wifiManager != null) {
                Method setWifiApConfigurationMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
                setWifiApConfigurationMethod.invoke(wifiManager, wifiConfiguration);
            }
            if (connectivityManager != null) {
                ResultReceiver dummyResultReceiver = new ResultReceiver(null);
                Field internalConnectivityManagerField = ConnectivityManager.class.getDeclaredField("mService");
                internalConnectivityManagerField.setAccessible(true);
                Object internalConnectivityManagerObject = internalConnectivityManagerField.get(connectivityManager);
                if (internalConnectivityManagerObject != null) {
                    String className = internalConnectivityManagerObject.getClass().getName();
                    Class internalConnectivityManagerClass = Class.forName(className);
                    Method startTetheringMethod = internalConnectivityManagerClass.getDeclaredMethod("startTethering", Integer.TYPE, ResultReceiver.class, Boolean.TYPE, String.class);
                    startTetheringMethod.invoke(internalConnectivityManagerObject, 0, dummyResultReceiver, false, context.getPackageName());
                }
            }
        } catch (Exception e) {
            DSLog.e(TAG, "Enable Wi-Fi AP : Exception " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void disableAp(Context context) {
        DSLog.d(TAG, "Disable Wi-Fi AP ");
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager != null) {
                Field internalConnectivityManagerField = ConnectivityManager.class.getDeclaredField("mService");
                internalConnectivityManagerField.setAccessible(true);
                Object internalConnectivityManagerObject = internalConnectivityManagerField.get(connectivityManager);
                if (internalConnectivityManagerObject != null) {
                    String className = internalConnectivityManagerObject.getClass().getName();
                    Class internalConnectivityManagerClass = Class.forName(className);
                    Method stopTethering = internalConnectivityManagerClass.getDeclaredMethod("stopTethering", Integer.TYPE, String.class);
                    stopTethering.invoke(internalConnectivityManagerObject, 0, context.getPackageName());
                }
            }
        } catch (Exception e) {
            DSLog.e(TAG, "DisableAp: Exception " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static WifiConfiguration createWifiInfo(String ssid, String password) {
        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.SSID = "\"" + ssid + "\"";
        if (TextUtils.isEmpty(password)) {
            config.allowedKeyManagement.set(0);
            DSLog.i(TAG, "password is ''");
            return config;
        }
        config.preSharedKey = "\"" + password + "\"";
        config.allowedAuthAlgorithms.set(0);
        config.allowedGroupCiphers.set(2);
        config.allowedGroupCiphers.set(3);
        config.allowedKeyManagement.set(1);
        config.allowedPairwiseCiphers.set(1);
        config.allowedPairwiseCiphers.set(2);
        config.allowedProtocols.set(1);
        config.allowedProtocols.set(0);
        config.status = 2;
        return config;
    }

    public static void connectWIFI(Context context, String wifiApName, String password, CallBack callBack) {
        if (context == null || callBack == null) {
            DSLog.i(TAG, "context == null || callBack == null");
            return;
        }
        WifiManager mWifiManager = (WifiManager) context.getSystemService("wifi");
        WifiConfiguration wifiNewConfiguration = createWifiInfo(wifiApName, password);
        int newNetworkId = mWifiManager.addNetwork(wifiNewConfiguration);
        if (newNetworkId == -1) {
            DSLog.e(TAG, "操作失败,需要将wifi列表中取消对设备连接的保存");
            callBack.connnectResult(false);
            return;
        }
        DSLog.d(TAG, "newNetworkId is:" + newNetworkId);
        if (!mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(true);
        }
        boolean enableNetwork = mWifiManager.enableNetwork(newNetworkId, true);
        if (!enableNetwork) {
            DSLog.e(TAG, "切换到指定wifi失败");
            callBack.connnectResult(false);
            return;
        }
        DSLog.e(TAG, "切换到指定wifi成功");
        callBack.connnectResult(true);
    }

    public static void disconnectWIFI(Context context) {
        WifiManager mWifiManager = (WifiManager) context.getSystemService("wifi");
        mWifiManager.disconnect();
        mWifiManager.setWifiEnabled(false);
    }

    public static boolean isWifiOpen(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        return wifiManager.isWifiEnabled();
    }

    public static WifiInfo getWifInfo(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        if (!wifiManager.isWifiEnabled()) {
            return null;
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        return wifiInfo;
    }

    public static String intToIp(int paramInt) {
        return (paramInt & 255) + "." + (255 & (paramInt >> 8)) + "." + (255 & (paramInt >> 16)) + "." + (255 & (paramInt >> 24));
    }

    public static boolean isWifiApEnabled(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        try {
            Method method = wifiManager.getClass().getMethod("isWifiApEnabled", new Class[0]);
            method.setAccessible(true);
            return ((Boolean) method.invoke(wifiManager, new Object[0])).booleanValue();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public static String getApSSID(Context context) {
        Object localObject1;
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        try {
            Method localMethod = wifiManager.getClass().getDeclaredMethod("getWifiApConfiguration", new Class[0]);
            if (localMethod == null || (localObject1 = localMethod.invoke(wifiManager, new Object[0])) == null) {
                return null;
            }
            WifiConfiguration localWifiConfiguration = (WifiConfiguration) localObject1;
            if (localWifiConfiguration.SSID != null) {
                return localWifiConfiguration.SSID;
            }
            Field localField1 = WifiConfiguration.class.getDeclaredField("mWifiApProfile");
            if (localField1 == null) {
                return null;
            }
            localField1.setAccessible(true);
            Object localObject2 = localField1.get(localWifiConfiguration);
            localField1.setAccessible(false);
            if (localObject2 == null) {
                return null;
            }
            Field localField2 = localObject2.getClass().getDeclaredField("SSID");
            localField2.setAccessible(true);
            Object localObject3 = localField2.get(localObject2);
            if (localObject3 == null) {
                return null;
            }
            localField2.setAccessible(false);
            String str = (String) localObject3;
            return str;
        } catch (Exception e) {
            return null;
        }
    }

    public static int getWifiApState(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        try {
            int i = ((Integer) wifiManager.getClass().getMethod("getWifiApState", new Class[0]).invoke(wifiManager, new Object[0])).intValue();
            return i;
        } catch (Exception e) {
            return 4;
        }
    }
}
