package o;

import android.net.http.X509TrustManagerExtensions;
import android.os.Build;
import android.security.NetworkSecurityPolicy;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class S4 extends ZW {
    public static final boolean e;
    public final ArrayList c;
    public final C1883mi d;

    static {
        boolean z = false;
        if (C2616uQ.v() && Build.VERSION.SDK_INT < 30) {
            z = true;
        }
        e = z;
    }

    public S4() throws NoSuchMethodException, ClassNotFoundException, SecurityException {
        C2153pa0 c2153pa0;
        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"));
            c2153pa0 = new C2153pa0(cls);
        } catch (Exception e2) {
            ZW.a.getClass();
            ZW.i("unable to load android socket classes", 5, e2);
            c2153pa0 = null;
        }
        int i = 0;
        ArrayList arrayListQ = AbstractC2219q9.Q(new K90[]{c2153pa0, new C0353Nq(C2305r5.f), new C0353Nq(C2838wl.a), new C0353Nq(C2824we.a)});
        ArrayList arrayList = new ArrayList();
        int size = arrayListQ.size();
        while (i < size) {
            Object obj = arrayListQ.get(i);
            i++;
            if (((K90) obj).c()) {
                arrayList.add(obj);
            }
        }
        this.c = arrayList;
        try {
            Class<?> cls2 = Class.forName("dalvik.system.CloseGuard");
            Method method4 = cls2.getMethod("get", null);
            method2 = cls2.getMethod("open", String.class);
            method = cls2.getMethod("warnIfOpen", null);
            method3 = method4;
        } catch (Exception unused) {
            method = null;
            method2 = null;
        }
        this.d = new C1883mi(method3, method2, method);
    }

    @Override // o.ZW
    public final AbstractC1155f00 b(X509TrustManager x509TrustManager) {
        X509TrustManagerExtensions x509TrustManagerExtensions;
        try {
            x509TrustManagerExtensions = new X509TrustManagerExtensions(x509TrustManager);
        } catch (IllegalArgumentException unused) {
            x509TrustManagerExtensions = null;
        }
        V2 v2 = x509TrustManagerExtensions != null ? new V2(x509TrustManager, x509TrustManagerExtensions) : null;
        return v2 != null ? v2 : super.b(x509TrustManager);
    }

    @Override // o.ZW
    public final InterfaceC1215fh0 c(X509TrustManager x509TrustManager) throws NoSuchMethodException, SecurityException {
        try {
            Method declaredMethod = x509TrustManager.getClass().getDeclaredMethod("findTrustAnchorByIssuerAndSignature", X509Certificate.class);
            declaredMethod.setAccessible(true);
            return new R4(x509TrustManager, declaredMethod);
        } catch (NoSuchMethodException unused) {
            return super.c(x509TrustManager);
        }
    }

    @Override // o.ZW
    public final void d(SSLSocket sSLSocket, String str, List list) {
        Object obj;
        ArrayList arrayList = this.c;
        int size = arrayList.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                obj = null;
                break;
            }
            obj = arrayList.get(i);
            i++;
            if (((K90) obj).b(sSLSocket)) {
                break;
            }
        }
        K90 k90 = (K90) obj;
        if (k90 != null) {
            k90.d(sSLSocket, str, list);
        }
    }

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

    @Override // o.ZW
    public final String f(SSLSocket sSLSocket) {
        Object obj;
        ArrayList arrayList = this.c;
        int size = arrayList.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                obj = null;
                break;
            }
            obj = arrayList.get(i);
            i++;
            if (((K90) obj).b(sSLSocket)) {
                break;
            }
        }
        K90 k90 = (K90) obj;
        if (k90 != null) {
            return k90.a(sSLSocket);
        }
        return null;
    }

    @Override // o.ZW
    public final Object g() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        C1883mi c1883mi = this.d;
        Method method = c1883mi.a;
        if (method != null) {
            try {
                Object objInvoke = method.invoke(null, null);
                c1883mi.b.invoke(objInvoke, "response.body().close()");
                return objInvoke;
            } catch (Exception unused) {
            }
        }
        return null;
    }

    @Override // o.ZW
    public final boolean h(String str) {
        return NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(str);
    }

    @Override // o.ZW
    public final void j(Object obj, String str) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        C1883mi c1883mi = this.d;
        c1883mi.getClass();
        if (obj != null) {
            try {
                c1883mi.c.invoke(obj, null);
                return;
            } catch (Exception unused) {
            }
        }
        ZW.i(str, 5, null);
    }
}
