package XnFJ5_9.Ft6VRog_36.KNBe_11;

import XnFJ5_9.XuVgy_28;
import android.os.Build;
import android.util.Log;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.TrustAnchor;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.X509TrustManager;
/* compiled from: AndroidPlatform.java */
/* loaded from: classes.dex */
public class KQgRr5e_0 extends X6FRs_4 {
    public final QxJ9d_1 KQgRr5e_0 = QxJ9d_1.KQgRr5e_0();

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final Class<?> f1512KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final Method f1513KQgRr5e_0;

    /* renamed from: Mqv2r_3  reason: collision with root package name */
    public final Method f3375Mqv2r_3;
    public final Method PGPF6hQ_2;
    public final Method QxJ9d_1;

    /* compiled from: AndroidPlatform.java */
    /* renamed from: XnFJ5_9.Ft6VRog_36.KNBe_11.KQgRr5e_0$KQgRr5e_0  reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public static final class C0081KQgRr5e_0 extends XnFJ5_9.Ft6VRog_36.WwEq_13.PGPF6hQ_2 {
        public final Object KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final Method f1514KQgRr5e_0;

        public C0081KQgRr5e_0(Object obj, Method method) {
            this.KQgRr5e_0 = obj;
            this.f1514KQgRr5e_0 = method;
        }

        @Override // XnFJ5_9.Ft6VRog_36.WwEq_13.PGPF6hQ_2
        public List<Certificate> KQgRr5e_0(List<Certificate> list, String str) throws SSLPeerUnverifiedException {
            try {
                return (List) this.f1514KQgRr5e_0.invoke(this.KQgRr5e_0, (X509Certificate[]) list.toArray(new X509Certificate[list.size()]), "RSA", str);
            } catch (IllegalAccessException e) {
                throw new AssertionError(e);
            } catch (InvocationTargetException e2) {
                SSLPeerUnverifiedException sSLPeerUnverifiedException = new SSLPeerUnverifiedException(e2.getMessage());
                sSLPeerUnverifiedException.initCause(e2);
                throw sSLPeerUnverifiedException;
            }
        }

        public boolean equals(Object obj) {
            return obj instanceof C0081KQgRr5e_0;
        }

        public int hashCode() {
            return 0;
        }
    }

    /* compiled from: AndroidPlatform.java */
    /* loaded from: classes.dex */
    public static final class PGPF6hQ_2 implements XnFJ5_9.Ft6VRog_36.WwEq_13.X6FRs_4 {
        public final Method KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final X509TrustManager f1515KQgRr5e_0;

        public PGPF6hQ_2(X509TrustManager x509TrustManager, Method method) {
            this.KQgRr5e_0 = method;
            this.f1515KQgRr5e_0 = x509TrustManager;
        }

        @Override // XnFJ5_9.Ft6VRog_36.WwEq_13.X6FRs_4
        public X509Certificate KQgRr5e_0(X509Certificate x509Certificate) {
            try {
                TrustAnchor trustAnchor = (TrustAnchor) this.KQgRr5e_0.invoke(this.f1515KQgRr5e_0, x509Certificate);
                if (trustAnchor != null) {
                    return trustAnchor.getTrustedCert();
                }
                return null;
            } catch (IllegalAccessException unused) {
                throw new AssertionError("unable to get issues and signature");
            } catch (InvocationTargetException unused2) {
                return null;
            }
        }

        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (obj instanceof PGPF6hQ_2) {
                PGPF6hQ_2 pGPF6hQ_2 = (PGPF6hQ_2) obj;
                return this.f1515KQgRr5e_0.equals(pGPF6hQ_2.f1515KQgRr5e_0) && this.KQgRr5e_0.equals(pGPF6hQ_2.KQgRr5e_0);
            }
            return false;
        }

        public int hashCode() {
            return this.f1515KQgRr5e_0.hashCode() + (this.KQgRr5e_0.hashCode() * 31);
        }
    }

    public KQgRr5e_0(Class<?> cls, Class<?> cls2, Method method, Method method2, Method method3, Method method4) {
        this.f1512KQgRr5e_0 = cls2;
        this.f1513KQgRr5e_0 = method;
        this.QxJ9d_1 = method2;
        this.PGPF6hQ_2 = method3;
        this.f3375Mqv2r_3 = method4;
    }

    @Nullable
    public static X6FRs_4 PGPF6hQ_2() {
        try {
            Class<?> cls = Class.forName("com.android.org.conscrypt.SSLParametersImpl");
            Class<?> cls2 = Class.forName("com.android.org.conscrypt.OpenSSLSocketImpl");
            if (Build.VERSION.SDK_INT >= 21) {
                try {
                    return new KQgRr5e_0(cls, cls2, cls2.getDeclaredMethod("setUseSessionTickets", Boolean.TYPE), cls2.getMethod("setHostname", String.class), cls2.getMethod("getAlpnSelectedProtocol", new Class[0]), cls2.getMethod("setAlpnProtocols", byte[].class));
                } catch (NoSuchMethodException unused) {
                }
            }
            throw new IllegalStateException("Expected Android API level 21+ but was " + Build.VERSION.SDK_INT);
        } catch (ClassNotFoundException unused2) {
            return null;
        }
    }

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

    public final boolean QxJ9d_1(String str, Class<?> cls, Object obj) throws InvocationTargetException, IllegalAccessException {
        try {
            return ((Boolean) cls.getMethod("isCleartextTrafficPermitted", String.class).invoke(obj, str)).booleanValue();
        } catch (NoSuchMethodException unused) {
            return KQgRr5e_0(str, cls, obj);
        }
    }

    /* compiled from: AndroidPlatform.java */
    /* loaded from: classes.dex */
    public static final class QxJ9d_1 {
        public final Method KQgRr5e_0;
        public final Method PGPF6hQ_2;
        public final Method QxJ9d_1;

        public QxJ9d_1(Method method, Method method2, Method method3) {
            this.KQgRr5e_0 = method;
            this.QxJ9d_1 = method2;
            this.PGPF6hQ_2 = method3;
        }

        public Object KQgRr5e_0(String str) {
            Method method = this.KQgRr5e_0;
            if (method != null) {
                try {
                    Object invoke = method.invoke(null, new Object[0]);
                    this.QxJ9d_1.invoke(invoke, str);
                    return invoke;
                } catch (Exception unused) {
                }
            }
            return null;
        }

        public boolean KQgRr5e_0(Object obj) {
            if (obj != null) {
                try {
                    this.PGPF6hQ_2.invoke(obj, new Object[0]);
                    return true;
                } catch (Exception unused) {
                    return false;
                }
            }
            return false;
        }

        public static QxJ9d_1 KQgRr5e_0() {
            Method method;
            Method method2;
            Method method3 = null;
            try {
                Class<?> cls = Class.forName("dalvik.system.CloseGuard");
                Method method4 = cls.getMethod("get", new Class[0]);
                method2 = cls.getMethod("open", String.class);
                method = cls.getMethod("warnIfOpen", new Class[0]);
                method3 = method4;
            } catch (Exception unused) {
                method = null;
                method2 = null;
            }
            return new QxJ9d_1(method3, method2, method);
        }
    }

    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    public void KQgRr5e_0(SSLSocket sSLSocket, String str, List<XuVgy_28> list) {
        if (this.f1512KQgRr5e_0.isInstance(sSLSocket)) {
            if (str != null) {
                try {
                    this.f1513KQgRr5e_0.invoke(sSLSocket, true);
                    this.QxJ9d_1.invoke(sSLSocket, str);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new AssertionError(e);
                }
            }
            this.f3375Mqv2r_3.invoke(sSLSocket, X6FRs_4.m696KQgRr5e_0(list));
        }
    }

    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    @Nullable
    public String KQgRr5e_0(SSLSocket sSLSocket) {
        if (this.f1512KQgRr5e_0.isInstance(sSLSocket)) {
            try {
                byte[] bArr = (byte[]) this.PGPF6hQ_2.invoke(sSLSocket, new Object[0]);
                if (bArr != null) {
                    return new String(bArr, StandardCharsets.UTF_8);
                }
                return null;
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new AssertionError(e);
            }
        }
        return null;
    }

    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    public void KQgRr5e_0(int i, String str, @Nullable Throwable th) {
        int min;
        int i2 = i != 5 ? 3 : 5;
        if (th != null) {
            str = str + '\n' + Log.getStackTraceString(th);
        }
        int i3 = 0;
        int length = str.length();
        while (i3 < length) {
            int indexOf = str.indexOf(10, i3);
            if (indexOf == -1) {
                indexOf = length;
            }
            while (true) {
                min = Math.min(indexOf, i3 + 4000);
                Log.println(i2, "OkHttp", str.substring(i3, min));
                if (min >= indexOf) {
                    break;
                }
                i3 = min;
            }
            i3 = min + 1;
        }
    }

    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    @Nullable
    public Object KQgRr5e_0(String str) {
        return this.KQgRr5e_0.KQgRr5e_0(str);
    }

    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    public void KQgRr5e_0(String str, Object obj) {
        if (this.KQgRr5e_0.KQgRr5e_0(obj)) {
            return;
        }
        KQgRr5e_0(5, str, (Throwable) null);
    }

    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    public boolean KQgRr5e_0(String str) {
        try {
            Class<?> cls = Class.forName("android.security.NetworkSecurityPolicy");
            return QxJ9d_1(str, cls, cls.getMethod("getInstance", new Class[0]).invoke(null, new Object[0]));
        } catch (ClassNotFoundException | NoSuchMethodException unused) {
            return super.m700KQgRr5e_0(str);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException unused2) {
            throw new AssertionError("unable to determine cleartext support");
        }
    }

    public final boolean KQgRr5e_0(String str, Class<?> cls, Object obj) throws InvocationTargetException, IllegalAccessException {
        try {
            return ((Boolean) cls.getMethod("isCleartextTrafficPermitted", new Class[0]).invoke(obj, new Object[0])).booleanValue();
        } catch (NoSuchMethodException unused) {
            return super.m700KQgRr5e_0(str);
        }
    }

    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    public XnFJ5_9.Ft6VRog_36.WwEq_13.PGPF6hQ_2 KQgRr5e_0(X509TrustManager x509TrustManager) {
        try {
            Class<?> cls = Class.forName("android.net.http.X509TrustManagerExtensions");
            return new C0081KQgRr5e_0(cls.getConstructor(X509TrustManager.class).newInstance(x509TrustManager), cls.getMethod("checkServerTrusted", X509Certificate[].class, String.class, String.class));
        } catch (Exception unused) {
            return super.KQgRr5e_0(x509TrustManager);
        }
    }

    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    public XnFJ5_9.Ft6VRog_36.WwEq_13.X6FRs_4 KQgRr5e_0(X509TrustManager x509TrustManager) {
        try {
            Method declaredMethod = x509TrustManager.getClass().getDeclaredMethod("findTrustAnchorByIssuerAndSignature", X509Certificate.class);
            declaredMethod.setAccessible(true);
            return new PGPF6hQ_2(x509TrustManager, declaredMethod);
        } catch (NoSuchMethodException unused) {
            return super.m697KQgRr5e_0(x509TrustManager);
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:7:0x000b, code lost:
        if (android.os.Build.VERSION.SDK_INT < 22) goto L8;
     */
    @Override // XnFJ5_9.Ft6VRog_36.KNBe_11.X6FRs_4
    /* renamed from: KQgRr5e_0 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public SSLContext mo698KQgRr5e_0() {
        boolean z = true;
        try {
            if (Build.VERSION.SDK_INT >= 16) {
            }
            z = false;
        } catch (NoClassDefFoundError unused) {
        }
        if (z) {
            try {
                return SSLContext.getInstance("TLSv1.2");
            } catch (NoSuchAlgorithmException unused2) {
            }
        }
        try {
            return SSLContext.getInstance("TLS");
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("No TLS provider", e);
        }
    }
}
