package com.seewo.vcommons.helper;

import android.content.ContentResolver;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.ResultReceiver;
import android.os.SystemProperties;
import android.provider.Settings;
import android.util.Log;
import com.seewo.vcommons.data.PreferencesUtils;
import com.seewo.vcommons.data.PropertiesUtils;
import com.seewo.vcommons.utils.RLog;
import com.seewo.vcommons.utils.reflect.WifiManagerInsider;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes.dex */
public class NetHelper {
    private static final String CAT_ARP = "cat /proc/net/arp";
    private static final String KEY_ETHERNET_IP = "dhcp.eth0.ipaddress";
    private static final String KEY_WIFI_IP = "dhcp.wlan0.ipaddress";
    private static final String PATTERN_IP_ADDRESS = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
    private static final String TAG = "NetHelper";
    private static final String TARGET_DEV = "eth1";
    private static final String TARGET_IP_MASK = "192.168.";

    private NetHelper() {
    }

    private static Object doMethod(Object obj, String str, Class[] clsArr, Object... objArr) {
        try {
            return obj.getClass().getMethod(str, clsArr).invoke(obj, objArr);
        } catch (Exception e2) {
            RLog.e(TAG, e2.toString());
            return null;
        }
    }

    private static List<String> execCommand(String str) {
        LinkedList linkedList = new LinkedList();
        Process processExec = null;
        try {
            try {
                try {
                    processExec = Runtime.getRuntime().exec(str);
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(processExec.getInputStream()));
                    while (true) {
                        String line = bufferedReader.readLine();
                        if (line == null) {
                            break;
                        }
                        linkedList.add(line);
                    }
                    processExec.destroy();
                } catch (Exception e2) {
                    Log.e(TAG, "close stream error", e2);
                }
            } catch (IOException e3) {
                Log.e(TAG, "execute shell command error", e3);
                if (processExec != null) {
                    processExec.destroy();
                }
            } catch (Exception e4) {
                Log.e(TAG, "process.waitFor exception", e4);
                if (processExec != null) {
                    processExec.destroy();
                }
            }
            return linkedList;
        } catch (Throwable th) {
            if (0 != 0) {
                try {
                    processExec.destroy();
                } catch (Exception e5) {
                    Log.e(TAG, "close stream error", e5);
                }
            }
            throw th;
        }
    }

    public static String getAndroidSlotIp() {
        return PropertiesUtils.getCurrentAndroidSlotIp();
    }

    public static String getPCIp() {
        return PropertiesUtils.getCurrentPCIp();
    }

    public static String getPlatformIp(Context context) {
        String str;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (PreferencesUtils.getNetworkState(context)) {
            str = KEY_ETHERNET_IP;
        } else {
            if (connectivityManager.getNetworkInfo(1).getState() != NetworkInfo.State.CONNECTED) {
                return null;
            }
            str = KEY_WIFI_IP;
        }
        return SystemProperties.get(str);
    }

    public static String getSlotIp() {
        String strRegexMatch = null;
        for (String str : execCommand(CAT_ARP)) {
            if (str.contains(TARGET_DEV) && str.contains(TARGET_IP_MASK)) {
                strRegexMatch = regexMatch(PATTERN_IP_ADDRESS, str);
            }
        }
        return strRegexMatch;
    }

    private static String regexMatch(String str, String str2) {
        Matcher matcher = Pattern.compile(str).matcher(str2);
        String strGroup = null;
        while (matcher.find()) {
            strGroup = matcher.group(0);
            if (str2.indexOf(str2) <= str2.lastIndexOf(matcher.group(1))) {
                break;
            }
        }
        return strGroup;
    }

    private static void startHightVersionNativeHotSpot(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        ResultReceiver resultReceiver = new ResultReceiver(new Handler());
        try {
            Field declaredField = ConnectivityManager.class.getDeclaredField("mService");
            declaredField.setAccessible(true);
            doMethod(declaredField.get(connectivityManager), "startTethering", new Class[]{Integer.TYPE, ResultReceiver.class, Boolean.TYPE}, 0, resultReceiver, Boolean.TRUE);
        } catch (Exception e2) {
            Log.e(TAG, "start AP error", e2);
        }
    }

    private static void startLowVersionNativeHotSpot(Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        int wifiState = wifiManager.getWifiState();
        if (wifiState == 2 || wifiState == 3) {
            wifiManager.setWifiEnabled(false);
            Settings.Global.putInt(contentResolver, "wifi_saved_state", 1);
        }
        WifiManagerInsider.setWifiApEnabled(wifiManager, null, true);
    }

    public static void startNativeHotSpot(Context context) {
        startHightVersionNativeHotSpot(context);
    }

    public static void stopNativeHotSpot(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService("wifi");
        if (WifiManagerInsider.getWifiApState(wifiManager) == WifiManagerInsider.WIFI_AP_STATE_ENABLED || WifiManagerInsider.getWifiApState(wifiManager) == WifiManagerInsider.WIFI_AP_STATE_ENABLING) {
            doMethod(context.getSystemService("connectivity"), "stopTethering", new Class[]{Integer.TYPE}, 0);
        }
    }
}
