package s7;

import android.net.http.X509TrustManagerExtensions;
import android.os.Build;
import android.security.NetworkSecurityPolicy;
import d3.C;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.X509TrustManager;

/* JADX INFO: loaded from: classes.dex */
public final class c extends n {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final boolean f16912e;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ArrayList f16913c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final G6.f f16914d;

    static {
        boolean z8 = false;
        if (C.n() && Build.VERSION.SDK_INT < 30) {
            z8 = true;
        }
        f16912e = z8;
    }

    public c() throws NoSuchMethodException {
        t7.n nVar;
        Method method;
        Method method2;
        Method method3 = null;
        try {
            Class<?> cls = Class.forName("com.android.org.conscrypt".concat(".OpenSSLSocketImpl"));
            Class.forName("com.android.org.conscrypt".concat(".OpenSSLSocketFactoryImpl"));
            Class.forName("com.android.org.conscrypt".concat(".SSLParametersImpl"));
            nVar = new t7.n(cls);
        } catch (Exception e8) {
            n.f16942a.getClass();
            n.i("unable to load android socket classes", 5, e8);
            nVar = null;
        }
        ArrayList arrayListN = B6.j.N(new t7.m[]{nVar, new t7.l(t7.f.f17032f), new t7.l(t7.j.f17039a), new t7.l(t7.h.f17038a)});
        ArrayList arrayList = new ArrayList();
        int size = arrayListN.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayListN.get(i);
            i++;
            if (((t7.m) obj).c()) {
                arrayList.add(obj);
            }
        }
        this.f16913c = arrayList;
        try {
            Class<?> cls2 = Class.forName("dalvik.system.CloseGuard");
            Method method4 = cls2.getMethod("get", null);
            method = cls2.getMethod("open", String.class);
            method2 = cls2.getMethod("warnIfOpen", null);
            method3 = method4;
        } catch (Exception unused) {
            method = null;
            method2 = null;
        }
        this.f16914d = new G6.f(method3, method, method2);
    }

    @Override // s7.n
    public final W3.b b(X509TrustManager x509TrustManager) {
        X509TrustManagerExtensions x509TrustManagerExtensions;
        try {
            x509TrustManagerExtensions = new X509TrustManagerExtensions(x509TrustManager);
        } catch (IllegalArgumentException unused) {
            x509TrustManagerExtensions = null;
        }
        t7.b bVar = x509TrustManagerExtensions != null ? new t7.b(x509TrustManager, x509TrustManagerExtensions) : null;
        return bVar != null ? bVar : new w7.a(c(x509TrustManager));
    }

    @Override // s7.n
    public final w7.d c(X509TrustManager x509TrustManager) {
        try {
            Method declaredMethod = x509TrustManager.getClass().getDeclaredMethod("findTrustAnchorByIssuerAndSignature", X509Certificate.class);
            declaredMethod.setAccessible(true);
            return new b(x509TrustManager, declaredMethod);
        } catch (NoSuchMethodException unused) {
            return super.c(x509TrustManager);
        }
    }

    @Override // s7.n
    public final void d(SSLSocket sSLSocket, String str, List list) {
        Object obj;
        P6.g.e(list, "protocols");
        ArrayList arrayList = this.f16913c;
        int size = arrayList.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                obj = null;
                break;
            }
            obj = arrayList.get(i);
            i++;
            if (((t7.m) obj).a(sSLSocket)) {
                break;
            }
        }
        t7.m mVar = (t7.m) obj;
        if (mVar != null) {
            mVar.d(sSLSocket, str, list);
        }
    }

    @Override // s7.n
    public final void e(Socket socket, InetSocketAddress inetSocketAddress, int i) throws IOException {
        P6.g.e(inetSocketAddress, "address");
        try {
            socket.connect(inetSocketAddress, i);
        } catch (ClassCastException e8) {
            if (Build.VERSION.SDK_INT != 26) {
                throw e8;
            }
            throw new IOException("Exception in connect", e8);
        }
    }

    @Override // s7.n
    public final String f(SSLSocket sSLSocket) {
        Object obj;
        ArrayList arrayList = this.f16913c;
        int size = arrayList.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                obj = null;
                break;
            }
            obj = arrayList.get(i);
            i++;
            if (((t7.m) obj).a(sSLSocket)) {
                break;
            }
        }
        t7.m mVar = (t7.m) obj;
        if (mVar != null) {
            return mVar.b(sSLSocket);
        }
        return null;
    }

    @Override // s7.n
    public final Object g() {
        G6.f fVar = this.f16914d;
        fVar.getClass();
        Method method = fVar.f3018a;
        if (method != null) {
            try {
                Object objInvoke = method.invoke(null, null);
                Method method2 = fVar.f3019b;
                P6.g.b(method2);
                method2.invoke(objInvoke, "response.body().close()");
                return objInvoke;
            } catch (Exception unused) {
            }
        }
        return null;
    }

    @Override // s7.n
    public final boolean h(String str) {
        P6.g.e(str, "hostname");
        return Build.VERSION.SDK_INT >= 24 ? NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(str) : NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted();
    }

    @Override // s7.n
    public final void j(Object obj, String str) {
        P6.g.e(str, "message");
        G6.f fVar = this.f16914d;
        fVar.getClass();
        if (obj != null) {
            try {
                Method method = fVar.f3020c;
                P6.g.b(method);
                method.invoke(obj, null);
                return;
            } catch (Exception unused) {
            }
        }
        n.i(str, 5, null);
    }
}
