package n4;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;

/* JADX INFO: loaded from: classes3.dex */
public final class p extends q {

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final o4.f f21751e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final o4.f f21752f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final o4.f f21753g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final o4.f f21754h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final o4.f f21755i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final Method f21756j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final Method f21757k;
    public static final Method l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final Method f21758m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public static final Method f21759n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public static final Method f21760o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public static final Constructor f21761p;

    static {
        Method method;
        Method method2;
        Method method3;
        Method method4;
        Method method5;
        Method method6;
        Logger logger = q.f21762b;
        Class cls = Boolean.TYPE;
        Constructor<?> constructor = null;
        f21750d = new o4.f(null, "setUseSessionTickets", new Class[]{cls}, 0);
        f21751e = new o4.f(null, "setHostname", new Class[]{String.class}, 0);
        f21752f = new o4.f(byte[].class, "getAlpnSelectedProtocol", new Class[0], 0);
        f21753g = new o4.f(null, "setAlpnProtocols", new Class[]{byte[].class}, 0);
        f21754h = new o4.f(byte[].class, "getNpnSelectedProtocol", new Class[0], 0);
        f21755i = new o4.f(null, "setNpnProtocols", new Class[]{byte[].class}, 0);
        try {
            method = SSLParameters.class.getMethod("setApplicationProtocols", String[].class);
            try {
                method3 = SSLParameters.class.getMethod("getApplicationProtocols", new Class[0]);
                try {
                    method2 = SSLSocket.class.getMethod("getApplicationProtocol", new Class[0]);
                    try {
                        Class<?> cls2 = Class.forName("android.net.ssl.SSLSockets");
                        method4 = cls2.getMethod("isSupportedSocket", SSLSocket.class);
                        try {
                            method5 = cls2.getMethod("setUseSessionTickets", SSLSocket.class, cls);
                        } catch (ClassNotFoundException e6) {
                            e = e6;
                            logger.log(Level.FINER, "Failed to find Android 10.0+ APIs", (Throwable) e);
                            method5 = null;
                        } catch (NoSuchMethodException e7) {
                            e = e7;
                            logger.log(Level.FINER, "Failed to find Android 10.0+ APIs", (Throwable) e);
                            method5 = null;
                        }
                    } catch (ClassNotFoundException e8) {
                        e = e8;
                        method4 = null;
                    } catch (NoSuchMethodException e9) {
                        e = e9;
                        method4 = null;
                    }
                } catch (ClassNotFoundException e10) {
                    e = e10;
                    method2 = null;
                    method4 = null;
                } catch (NoSuchMethodException e11) {
                    e = e11;
                    method2 = null;
                    method4 = null;
                }
            } catch (ClassNotFoundException e12) {
                e = e12;
                method2 = null;
                method3 = method2;
                method4 = method3;
                logger.log(Level.FINER, "Failed to find Android 10.0+ APIs", (Throwable) e);
                method5 = null;
                l = method;
                f21758m = method3;
                f21759n = method2;
                f21756j = method4;
                f21757k = method5;
                method6 = SSLParameters.class.getMethod("setServerNames", List.class);
                try {
                    constructor = Class.forName("javax.net.ssl.SNIHostName").getConstructor(String.class);
                } catch (ClassNotFoundException e13) {
                    e = e13;
                    logger.log(Level.FINER, "Failed to find Android 7.0+ APIs", (Throwable) e);
                } catch (NoSuchMethodException e14) {
                    e = e14;
                    logger.log(Level.FINER, "Failed to find Android 7.0+ APIs", (Throwable) e);
                }
                f21760o = method6;
                f21761p = constructor;
            } catch (NoSuchMethodException e15) {
                e = e15;
                method2 = null;
                method3 = method2;
                method4 = method3;
                logger.log(Level.FINER, "Failed to find Android 10.0+ APIs", (Throwable) e);
                method5 = null;
                l = method;
                f21758m = method3;
                f21759n = method2;
                f21756j = method4;
                f21757k = method5;
                method6 = SSLParameters.class.getMethod("setServerNames", List.class);
                constructor = Class.forName("javax.net.ssl.SNIHostName").getConstructor(String.class);
                f21760o = method6;
                f21761p = constructor;
            }
        } catch (ClassNotFoundException e16) {
            e = e16;
            method = null;
            method2 = null;
        } catch (NoSuchMethodException e17) {
            e = e17;
            method = null;
            method2 = null;
        }
        l = method;
        f21758m = method3;
        f21759n = method2;
        f21756j = method4;
        f21757k = method5;
        try {
            method6 = SSLParameters.class.getMethod("setServerNames", List.class);
            constructor = Class.forName("javax.net.ssl.SNIHostName").getConstructor(String.class);
        } catch (ClassNotFoundException e18) {
            e = e18;
            method6 = null;
        } catch (NoSuchMethodException e19) {
            e = e19;
            method6 = null;
        }
        f21760o = method6;
        f21761p = constructor;
    }

    @Override // n4.q
    public final void a(SSLSocket sSLSocket, String str, List list) {
        Constructor constructor;
        boolean z6;
        Method method;
        ArrayList arrayList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            arrayList.add(((o4.m) it.next()).f21877a);
        }
        String[] strArr = (String[]) arrayList.toArray(new String[0]);
        SSLParameters sSLParameters = sSLSocket.getSSLParameters();
        if (str != null) {
            try {
                try {
                    if (q.c(str)) {
                        Method method2 = f21756j;
                        if (method2 == null || !((Boolean) method2.invoke(null, sSLSocket)).booleanValue()) {
                            f21750d.h(sSLSocket, Boolean.TRUE);
                        } else {
                            f21757k.invoke(null, sSLSocket, Boolean.TRUE);
                        }
                        Method method3 = f21760o;
                        if (method3 == null || (constructor = f21761p) == null) {
                            f21751e.h(sSLSocket, str);
                        } else {
                            method3.invoke(sSLParameters, Collections.singletonList(constructor.newInstance(str)));
                        }
                    }
                } catch (InvocationTargetException e6) {
                    throw new RuntimeException(e6);
                }
            } catch (IllegalAccessException e7) {
                throw new RuntimeException(e7);
            } catch (InstantiationException e8) {
                throw new RuntimeException(e8);
            }
        }
        Method method4 = f21759n;
        if (method4 != null) {
            try {
                method4.invoke(sSLSocket, new Object[0]);
                l.invoke(sSLParameters, strArr);
                z6 = true;
            } catch (InvocationTargetException e9) {
                if (!(e9.getTargetException() instanceof UnsupportedOperationException)) {
                    throw e9;
                }
                q.f21762b.log(Level.FINER, "setApplicationProtocol unsupported, will try old methods");
                z6 = false;
            }
        } else {
            z6 = false;
        }
        sSLSocket.setSSLParameters(sSLParameters);
        if (z6 && (method = f21758m) != null && Arrays.equals(strArr, (String[]) method.invoke(sSLSocket.getSSLParameters(), new Object[0]))) {
            return;
        }
        Object[] objArr = {o4.l.b(list)};
        o4.l lVar = this.f21764a;
        if (lVar.e() == 1) {
            f21753g.i(sSLSocket, objArr);
        }
        if (lVar.e() == 3) {
            throw new RuntimeException("We can not do TLS handshake on this Android version, please install the Google Play Services Dynamic Security Provider to use TLS");
        }
        f21755i.i(sSLSocket, objArr);
    }

    @Override // n4.q
    public final String b(SSLSocket sSLSocket) {
        Logger logger = q.f21762b;
        Method method = f21759n;
        if (method != null) {
            try {
                return (String) method.invoke(sSLSocket, new Object[0]);
            } catch (IllegalAccessException e6) {
                throw new RuntimeException(e6);
            } catch (InvocationTargetException e7) {
                if (!(e7.getTargetException() instanceof UnsupportedOperationException)) {
                    throw new RuntimeException(e7);
                }
                logger.log(Level.FINER, "Socket unsupported for getApplicationProtocol, will try old methods");
            }
        }
        o4.l lVar = this.f21764a;
        if (lVar.e() == 1) {
            try {
                byte[] bArr = (byte[]) f21752f.i(sSLSocket, new Object[0]);
                if (bArr != null) {
                    return new String(bArr, o4.o.f21886b);
                }
            } catch (Exception e8) {
                logger.log(Level.FINE, "Failed calling getAlpnSelectedProtocol()", (Throwable) e8);
            }
        }
        if (lVar.e() == 3) {
            return null;
        }
        try {
            byte[] bArr2 = (byte[]) f21754h.i(sSLSocket, new Object[0]);
            if (bArr2 != null) {
                return new String(bArr2, o4.o.f21886b);
            }
            return null;
        } catch (Exception e9) {
            logger.log(Level.FINE, "Failed calling getNpnSelectedProtocol()", (Throwable) e9);
            return null;
        }
    }

    @Override // n4.q
    public final String d(SSLSocket sSLSocket, String str, List list) {
        String strB = b(sSLSocket);
        return strB == null ? super.d(sSLSocket, str, list) : strB;
    }
}
