package com.seewo.remote.g;

import android.net.ConnectivityManager;
import android.net.IpConfiguration;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkCapabilities;
import android.os.Build;
import android.os.INetworkManagementService;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import com.seewo.remote.base.App;
import java.net.Inet4Address;
import java.util.Arrays;
import java.util.List;
import org.snmp4j.agent.mo.snmp.DisplayString;
import org.snmp4j.smi.Counter32;

/* JADX INFO: compiled from: EthHelper.kt */
/* JADX INFO: loaded from: classes.dex */
public final class i {
    public static final i a = new i();

    private i() {
    }

    private final String a() {
        Object systemService = com.seewo.remote.d.b.b().a().getSystemService("connectivity");
        if (systemService == null) {
            throw new NullPointerException("null cannot be cast to non-null type android.net.ConnectivityManager");
        }
        LinkProperties linkProperties = (LinkProperties) e.c.e.f.g.c.l((ConnectivityManager) systemService).c("getLinkProperties", 9).g();
        if (linkProperties == null) {
            Log.w("EthHelper", "getCurrentInterface: linkProperties is null!");
            return "eth0";
        }
        Object objG = e.c.e.f.g.c.l(linkProperties).b("getInterfaceName").g();
        h.x.d.k.c(objG, "{\n            Reflect.on…aceName\").get()\n        }");
        return (String) objG;
    }

    private final IpConfiguration.IpAssignment b(String str) {
        Object systemService = com.seewo.remote.d.b.b().a().getSystemService("ethernet");
        String strA = a();
        IpConfiguration ipConfiguration = (IpConfiguration) e.c.e.f.g.c.l(systemService).c("getConfiguration", a()).g();
        Log.v("EthHelper", "interfaceName:" + strA + ",  ipConfiguration:" + ipConfiguration);
        IpConfiguration.IpAssignment ipAssignment = ipConfiguration != null ? ipConfiguration.ipAssignment : IpConfiguration.IpAssignment.DHCP;
        Log.d("EthHelper", "getEthernetMode: " + ipAssignment + " reason=" + str);
        h.x.d.k.c(ipAssignment, "mode");
        return ipAssignment;
    }

    static /* synthetic */ IpConfiguration.IpAssignment c(i iVar, String str, int i2, Object obj) {
        if ((i2 & 1) != 0) {
            str = "";
        }
        return iVar.b(str);
    }

    private final String d(String str) {
        try {
            String hardwareAddress = INetworkManagementService.Stub.asInterface(ServiceManager.getService("network_management")).getInterfaceConfig(str).getHardwareAddress();
            h.x.d.k.c(hardwareAddress, "{\n            mNwService…hardwareAddress\n        }");
            return hardwareAddress;
        } catch (RemoteException unused) {
            return "";
        }
    }

    public static /* synthetic */ String f(i iVar, String str, int i2, Object obj) {
        if ((i2 & 1) != 0) {
            str = "";
        }
        return iVar.e(str);
    }

    private final String k(int i2) {
        int i3 = (int) (Counter32.MAX_COUNTER32_VALUE << (32 - i2));
        h.x.d.x xVar = h.x.d.x.a;
        String str = String.format("%d.%d.%d.%d", Arrays.copyOf(new Object[]{Integer.valueOf((i3 >>> 24) & DisplayString.MAX_SIZE), Integer.valueOf((i3 >>> 16) & DisplayString.MAX_SIZE), Integer.valueOf((i3 >>> 8) & DisplayString.MAX_SIZE), Integer.valueOf(i3 & DisplayString.MAX_SIZE)}, 4));
        h.x.d.k.c(str, "format(format, *args)");
        return str;
    }

    public final String e(String str) {
        String strD;
        h.x.d.k.d(str, "reason");
        try {
            strD = d(a());
        } catch (Exception e2) {
            Log.e("EthHelper", "getMacAddress: ", e2);
            strD = "";
        }
        Log.d("EthHelper", "getMacAddress: " + strD + " reason=" + str);
        return strD;
    }

    public final String g() {
        if (Build.VERSION.SDK_INT < 21) {
            return "0.0.0.0";
        }
        Object systemService = com.seewo.remote.d.b.b().a().getSystemService("connectivity");
        if (systemService == null) {
            throw new NullPointerException("null cannot be cast to non-null type android.net.ConnectivityManager");
        }
        LinkProperties linkProperties = (LinkProperties) e.c.e.f.g.c.l((ConnectivityManager) systemService).c("getLinkProperties", 1).g();
        if (linkProperties == null) {
            return "0.0.0.0";
        }
        List<LinkAddress> list = (List) e.c.e.f.g.c.l(linkProperties).b("getLinkAddresses").g();
        h.x.d.k.c(list, "linkAddresses");
        if (!(true ^ list.isEmpty())) {
            return "0.0.0.0";
        }
        for (LinkAddress linkAddress : list) {
            if (linkAddress.getAddress() instanceof Inet4Address) {
                return k(linkAddress.getPrefixLength());
            }
        }
        return "0.0.0.0";
    }

    public final boolean h() {
        return c(this, null, 1, null) != IpConfiguration.IpAssignment.STATIC;
    }

    public final boolean i() {
        try {
            Object systemService = com.seewo.remote.d.b.b().a().getSystemService("connectivity");
            if (systemService == null) {
                throw new NullPointerException("null cannot be cast to non-null type android.net.ConnectivityManager");
            }
            ConnectivityManager connectivityManager = (ConnectivityManager) systemService;
            if (Build.VERSION.SDK_INT < 23) {
                return j();
            }
            NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
            boolean z = networkCapabilities != null && networkCapabilities.hasTransport(3);
            boolean z2 = networkCapabilities != null && networkCapabilities.hasCapability(12);
            Log.d("EthHelper", "isEthernetConnected: isEthernet=" + z + ", isConnected=" + z2);
            return z && z2;
        } catch (Exception e2) {
            Log.e("EthHelper", "isEthernetConnected error: ", e2);
            return false;
        }
    }

    public final boolean j() {
        Object systemService = App.b.getSystemService("ethernet");
        if (Build.VERSION.SDK_INT < 33) {
            Object objG = e.c.e.f.g.c.l(systemService).b("getState").g();
            h.x.d.k.c(objG, "on(ethernetManager).call(\"getState\").get()");
            return 1 == ((Number) objG).intValue();
        }
        Object objG2 = e.c.e.f.g.c.l(systemService).c("isAvailable", a()).g();
        if (objG2 != null) {
            return ((Boolean) objG2).booleanValue();
        }
        throw new NullPointerException("null cannot be cast to non-null type kotlin.Boolean");
    }

    public final void l(boolean z) {
        try {
            Object systemService = App.b.getSystemService("ethernet");
            if (Build.VERSION.SDK_INT >= 33) {
                e.c.e.f.g.c.l(systemService).c("setEthernetEnabled", Boolean.valueOf(z));
            } else {
                e.c.e.f.g.c.l(systemService).c("enableEthernet", Boolean.valueOf(z));
            }
        } catch (Exception e2) {
            String message = e2.getMessage();
            if (message == null) {
                message = "";
            }
            Log.e("SmartProcessor", message);
        }
    }
}
