package com.google.android.gms.internal.ads;

import android.os.Bundle;
import android.os.RemoteException;
import android.util.Pair;
import androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes2.dex */
public final /* synthetic */ class qk1 implements Runnable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f15769a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final /* synthetic */ boolean f15770b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ Object f15771c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final /* synthetic */ Object f15772d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final /* synthetic */ Object f15773e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ Object f15774f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final /* synthetic */ Object f15775g;

    public /* synthetic */ qk1(sk1 sk1Var, String str, androidx.concurrent.futures.h hVar, boolean z10, String str2, byte[] bArr) {
        this.f15769a = 0;
        this.f15771c = sk1Var;
        this.f15772d = str;
        this.f15774f = hVar;
        this.f15770b = z10;
        this.f15773e = str2;
        this.f15775g = bArr;
    }

    /* JADX WARN: Removed duplicated region for block: B:120:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:95:0x01e4  */
    @Override // java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void run() {
        HttpURLConnection httpURLConnection;
        byte[] byteArray;
        int i10 = this.f15769a;
        boolean z10 = this.f15770b;
        Object obj = this.f15772d;
        Object obj2 = this.f15773e;
        Object obj3 = this.f15775g;
        Object obj4 = this.f15774f;
        Object obj5 = this.f15771c;
        switch (i10) {
            case 0:
                sk1 sk1Var = (sk1) obj5;
                String str = (String) obj;
                androidx.concurrent.futures.h hVar = (androidx.concurrent.futures.h) obj4;
                String str2 = (String) obj2;
                byte[] bArr = (byte[]) obj3;
                sk1Var.getClass();
                HttpURLConnection httpURLConnection2 = null;
                try {
                    try {
                        httpURLConnection = (HttpURLConnection) URI.create(str).toURL().openConnection();
                    } finally {
                        if (httpURLConnection2 != null) {
                            httpURLConnection2.disconnect();
                        }
                    }
                    break;
                } catch (SocketTimeoutException e10) {
                    e = e10;
                } catch (Throwable th) {
                    th = th;
                }
                try {
                    Objects.requireNonNull(httpURLConnection);
                    na1 na1Var = new na1(httpURLConnection, 8);
                    ExecutorService executorService = sk1Var.f16464a;
                    androidx.concurrent.futures.l lVar = hVar.f993c;
                    if (lVar != null) {
                        lVar.g(na1Var, executorService);
                    }
                    httpURLConnection.setRequestProperty(RtspHeaders.USER_AGENT, sk1Var.f16465b);
                    int i11 = (int) sk1Var.f16466c;
                    httpURLConnection.setConnectTimeout(i11);
                    httpURLConnection.setReadTimeout(i11);
                    if (z10) {
                        httpURLConnection.setDoOutput(true);
                        httpURLConnection.setRequestMethod(HlsMediaPlaylist.Interstitial.CUE_TRIGGER_POST);
                        if (str2 != null) {
                            httpURLConnection.setRequestProperty(RtspHeaders.CONTENT_TYPE, str2);
                        }
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
                        try {
                            bufferedOutputStream.write(bArr);
                            bufferedOutputStream.close();
                        } finally {
                            try {
                                break;
                            } catch (Throwable th2) {
                            }
                        }
                    }
                    int responseCode = httpURLConnection.getResponseCode();
                    InputStream inputStream = responseCode < 400 ? httpURLConnection.getInputStream() : httpURLConnection.getErrorStream();
                    try {
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        try {
                            if (inputStream == null) {
                                byteArray = new byte[0];
                                byteArrayOutputStream.close();
                            } else {
                                byte[] bArr2 = new byte[4096];
                                while (true) {
                                    int i12 = inputStream.read(bArr2);
                                    if (i12 != -1) {
                                        byteArrayOutputStream.write(bArr2, 0, i12);
                                    } else {
                                        byteArray = byteArrayOutputStream.toByteArray();
                                        byteArrayOutputStream.close();
                                        inputStream.close();
                                    }
                                }
                            }
                            hVar.a(new rk1(responseCode, byteArray));
                        } finally {
                            try {
                                break;
                            } catch (Throwable th3) {
                            }
                        }
                    } finally {
                        if (inputStream != null) {
                            try {
                                break;
                            } catch (Throwable th4) {
                            }
                        }
                    }
                } catch (SocketTimeoutException e11) {
                    e = e11;
                    httpURLConnection2 = httpURLConnection;
                    String message = e.getMessage();
                    StringBuilder sb = new StringBuilder(String.valueOf(message).length() + 9);
                    sb.append("Timeout: ");
                    sb.append(message);
                    hVar.b(new TimeoutException(sb.toString()));
                    httpURLConnection = httpURLConnection2;
                    if (httpURLConnection != null) {
                    }
                } catch (Throwable th5) {
                    th = th5;
                    httpURLConnection2 = httpURLConnection;
                    hVar.b(th);
                    httpURLConnection = httpURLConnection2;
                    if (httpURLConnection != null) {
                    }
                }
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                    return;
                }
                return;
            case 1:
                Pair pair = (Pair) obj;
                ((ek2) ((pi2) obj5).f15425b.f17646h).zzal(((Integer) pair.first).intValue(), (cp2) pair.second, (to2) obj2, (yo2) obj4, (IOException) obj3, this.f15770b);
                return;
            case 2:
                com.google.android.gms.measurement.internal.a3 a3VarL = ((com.google.android.gms.measurement.internal.m1) ((com.google.android.gms.measurement.internal.g2) obj3).f31277a).l();
                boolean z11 = this.f15770b;
                a3VarL.w();
                a3VarL.D();
                a3VarL.I(new com.google.android.gms.measurement.internal.f3(a3VarL, (AtomicReference) obj5, (String) obj2, (String) obj4, a3VarL.S(false), z11));
                return;
            default:
                Bundle bundle = new Bundle();
                try {
                    com.google.android.gms.measurement.internal.h0 h0Var = ((com.google.android.gms.measurement.internal.a3) obj3).f21472d;
                    if (h0Var == null) {
                        ((com.google.android.gms.measurement.internal.a3) obj3).zzj().f21834f.a((String) obj, (String) obj2, "Failed to get user properties; not connected to service");
                    } else {
                        com.google.android.gms.common.internal.w.checkNotNull((com.google.android.gms.measurement.internal.d4) obj5);
                        bundle = com.google.android.gms.measurement.internal.n4.I(h0Var.zza((String) obj, (String) obj2, z10, (com.google.android.gms.measurement.internal.d4) obj5));
                        ((com.google.android.gms.measurement.internal.a3) obj3).R();
                        com.google.android.gms.measurement.internal.a3 a3Var = (com.google.android.gms.measurement.internal.a3) obj3;
                        com.google.android.gms.internal.measurement.x0 x0Var = (com.google.android.gms.internal.measurement.x0) obj4;
                        a3Var.v().S(x0Var, bundle);
                        obj3 = a3Var;
                        obj4 = x0Var;
                    }
                    return;
                } catch (RemoteException e12) {
                    ((com.google.android.gms.measurement.internal.a3) obj3).zzj().f21834f.a((String) obj, e12, "Failed to get user properties; remote exception");
                    return;
                } finally {
                    ((com.google.android.gms.measurement.internal.a3) obj3).v().S((com.google.android.gms.internal.measurement.x0) obj4, bundle);
                }
        }
    }

    public /* synthetic */ qk1(pi2 pi2Var, Pair pair, to2 to2Var, yo2 yo2Var, IOException iOException, boolean z10) {
        this.f15769a = 1;
        this.f15771c = pi2Var;
        this.f15772d = pair;
        this.f15773e = to2Var;
        this.f15774f = yo2Var;
        this.f15775g = iOException;
        this.f15770b = z10;
    }

    public qk1(com.google.android.gms.measurement.internal.g2 g2Var, AtomicReference atomicReference, String str, String str2, boolean z10) {
        this.f15769a = 2;
        this.f15771c = atomicReference;
        this.f15772d = null;
        this.f15773e = str;
        this.f15774f = str2;
        this.f15770b = z10;
        this.f15775g = g2Var;
    }

    public qk1(com.google.android.gms.measurement.internal.a3 a3Var, String str, String str2, com.google.android.gms.measurement.internal.d4 d4Var, boolean z10, com.google.android.gms.internal.measurement.x0 x0Var) {
        this.f15769a = 3;
        this.f15772d = str;
        this.f15773e = str2;
        this.f15771c = d4Var;
        this.f15770b = z10;
        this.f15774f = x0Var;
        this.f15775g = a3Var;
    }
}
