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

import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object f8504b;

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

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

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

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

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

    public /* synthetic */ a61(c61 c61Var, ex exVar, Bundle bundle, List list, j01 j01Var, e30 e30Var) {
        this.f8503a = 0;
        this.f8504b = c61Var;
        this.f8505c = exVar;
        this.f8506d = bundle;
        this.f8507e = list;
        this.f8508f = j01Var;
        this.f8509g = e30Var;
    }

    /* JADX WARN: Removed duplicated region for block: B:103:0x01f2 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:107:0x0225 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:85:0x020a  */
    /* JADX WARN: Removed duplicated region for block: B:94:0x023d  */
    @Override // java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void run() throws Throwable {
        int i10;
        HttpURLConnection httpURLConnection;
        Map<String, List<String>> map;
        IOException iOException;
        int i11;
        Map<String, List<String>> map2;
        Throwable th;
        Map<String, List<String>> headerFields;
        Object obj;
        com.google.android.gms.measurement.internal.h0 h0Var;
        switch (this.f8503a) {
            case 0:
                c61 c61Var = (c61) this.f8504b;
                ex exVar = (ex) this.f8505c;
                Bundle bundle = (Bundle) this.f8506d;
                List list = (List) this.f8507e;
                j01 j01Var = (j01) this.f8508f;
                e30 e30Var = (e30) this.f8509g;
                c61Var.getClass();
                try {
                    exVar.A1(new k8.b(c61Var.f9768d), c61Var.f9774j, bundle, (Bundle) list.get(0), c61Var.f9769e.f12454c, j01Var);
                    return;
                } catch (RemoteException e10) {
                    e30Var.a(e10);
                    return;
                }
            case 1:
                Object obj2 = this.f8505c;
                Object obj3 = this.f8506d;
                Object obj4 = this.f8507e;
                Object obj5 = this.f8509g;
                com.google.android.gms.measurement.internal.s0 s0Var = (com.google.android.gms.measurement.internal.s0) obj5;
                com.google.android.gms.measurement.internal.j1 j1Var = ((com.google.android.gms.measurement.internal.m1) s0Var.f31277a).f21758j;
                com.google.android.gms.measurement.internal.m1.d(j1Var);
                j1Var.L();
                OutputStream outputStream = null;
                try {
                    URL url = (URL) this.f8504b;
                    synchronized (com.google.android.gms.internal.measurement.s0.class) {
                    }
                    URLConnection uRLConnectionOpenConnection = url.openConnection();
                    if (!(uRLConnectionOpenConnection instanceof HttpURLConnection)) {
                        throw new IOException("Failed to obtain HTTP connection");
                    }
                    httpURLConnection = (HttpURLConnection) uRLConnectionOpenConnection;
                    httpURLConnection.setDefaultUseCaches(false);
                    httpURLConnection.setConnectTimeout(60000);
                    httpURLConnection.setReadTimeout(61000);
                    httpURLConnection.setInstanceFollowRedirects(false);
                    httpURLConnection.setDoInput(true);
                    try {
                        Object obj6 = this.f8508f;
                        if (((Map) obj6) != null) {
                            for (Map.Entry entry : ((Map) obj6).entrySet()) {
                                httpURLConnection.addRequestProperty((String) entry.getKey(), (String) entry.getValue());
                            }
                        }
                        if (((byte[]) obj2) != null) {
                            byte[] bArrG0 = ((com.google.android.gms.measurement.internal.s0) obj5).x().g0((byte[]) obj2);
                            ((com.google.android.gms.measurement.internal.s0) obj5).zzj().f21842n.b(Integer.valueOf(bArrG0.length), "Uploading data. size");
                            httpURLConnection.setDoOutput(true);
                            httpURLConnection.addRequestProperty(RtspHeaders.CONTENT_ENCODING, "gzip");
                            httpURLConnection.setFixedLengthStreamingMode(bArrG0.length);
                            httpURLConnection.connect();
                            OutputStream outputStream2 = httpURLConnection.getOutputStream();
                            try {
                                outputStream2.write(bArrG0);
                                outputStream2.close();
                            } catch (IOException e11) {
                                iOException = e11;
                                i11 = 0;
                                map2 = null;
                                outputStream = outputStream2;
                                if (outputStream != null) {
                                }
                                if (httpURLConnection != null) {
                                }
                                s0Var.zzl().I(new androidx.fragment.app.y1((String) obj4, (com.google.android.gms.measurement.internal.u0) obj3, i11, iOException, null, map2));
                                return;
                            } catch (Throwable th2) {
                                i10 = 0;
                                map = null;
                                th = th2;
                                outputStream = outputStream2;
                                if (outputStream != null) {
                                }
                                if (httpURLConnection != null) {
                                }
                                s0Var.zzl().I(new androidx.fragment.app.y1((String) obj4, (com.google.android.gms.measurement.internal.u0) obj3, i10, null, null, map));
                                throw th;
                            }
                        }
                        int responseCode = httpURLConnection.getResponseCode();
                        try {
                            headerFields = httpURLConnection.getHeaderFields();
                        } catch (IOException e12) {
                            e = e12;
                            map2 = null;
                            i11 = responseCode;
                        } catch (Throwable th3) {
                            th = th3;
                            map = null;
                            i10 = responseCode;
                        }
                        try {
                            byte[] bArrE = com.google.android.gms.measurement.internal.s0.E(httpURLConnection);
                            httpURLConnection.disconnect();
                            s0Var.zzl().I(new androidx.fragment.app.y1((String) obj4, (com.google.android.gms.measurement.internal.u0) obj3, responseCode, null, bArrE, headerFields));
                            return;
                        } catch (IOException e13) {
                            e = e13;
                            i11 = responseCode;
                            map2 = headerFields;
                            iOException = e;
                            if (outputStream != null) {
                                try {
                                    outputStream.close();
                                } catch (IOException e14) {
                                    s0Var.zzj().f21834f.a(com.google.android.gms.measurement.internal.p0.A((String) obj4), e14, "Error closing HTTP compressed POST connection output stream. appId");
                                }
                                break;
                            }
                            if (httpURLConnection != null) {
                                httpURLConnection.disconnect();
                            }
                            s0Var.zzl().I(new androidx.fragment.app.y1((String) obj4, (com.google.android.gms.measurement.internal.u0) obj3, i11, iOException, null, map2));
                            return;
                        } catch (Throwable th4) {
                            th = th4;
                            i10 = responseCode;
                            map = headerFields;
                            if (outputStream != null) {
                                try {
                                    outputStream.close();
                                } catch (IOException e15) {
                                    s0Var.zzj().f21834f.a(com.google.android.gms.measurement.internal.p0.A((String) obj4), e15, "Error closing HTTP compressed POST connection output stream. appId");
                                }
                                break;
                            }
                            if (httpURLConnection != null) {
                                httpURLConnection.disconnect();
                            }
                            s0Var.zzl().I(new androidx.fragment.app.y1((String) obj4, (com.google.android.gms.measurement.internal.u0) obj3, i10, null, null, map));
                            throw th;
                        }
                    } catch (IOException e16) {
                        iOException = e16;
                        i11 = 0;
                        map2 = null;
                    } catch (Throwable th5) {
                        th = th5;
                        i10 = 0;
                        map = null;
                        th = th;
                        if (outputStream != null) {
                        }
                        if (httpURLConnection != null) {
                        }
                        s0Var.zzl().I(new androidx.fragment.app.y1((String) obj4, (com.google.android.gms.measurement.internal.u0) obj3, i10, null, null, map));
                        throw th;
                    }
                } catch (IOException e17) {
                    iOException = e17;
                    i11 = 0;
                    httpURLConnection = null;
                    map2 = null;
                } catch (Throwable th6) {
                    th = th6;
                    i10 = 0;
                    httpURLConnection = null;
                    map = null;
                }
                break;
            default:
                synchronized (((AtomicReference) this.f8504b)) {
                    try {
                        try {
                            obj = this.f8509g;
                            h0Var = ((com.google.android.gms.measurement.internal.a3) obj).f21472d;
                        } catch (RemoteException e18) {
                            ((com.google.android.gms.measurement.internal.a3) this.f8509g).zzj().f21834f.d("(legacy) Failed to get conditional properties; remote exception", com.google.android.gms.measurement.internal.p0.A((String) this.f8505c), (String) this.f8506d, e18);
                            ((AtomicReference) this.f8504b).set(Collections.emptyList());
                        }
                        if (h0Var == null) {
                            ((com.google.android.gms.measurement.internal.a3) obj).zzj().f21834f.d("(legacy) Failed to get conditional properties; not connected to service", com.google.android.gms.measurement.internal.p0.A((String) this.f8505c), (String) this.f8506d, (String) this.f8507e);
                            ((AtomicReference) this.f8504b).set(Collections.emptyList());
                            return;
                        }
                        if (TextUtils.isEmpty((String) this.f8505c)) {
                            com.google.android.gms.common.internal.w.checkNotNull((com.google.android.gms.measurement.internal.d4) this.f8508f);
                            ((AtomicReference) this.f8504b).set(h0Var.zza((String) this.f8506d, (String) this.f8507e, (com.google.android.gms.measurement.internal.d4) this.f8508f));
                        } else {
                            ((AtomicReference) this.f8504b).set(h0Var.zza((String) this.f8505c, (String) this.f8506d, (String) this.f8507e));
                        }
                        ((com.google.android.gms.measurement.internal.a3) this.f8509g).R();
                        ((AtomicReference) this.f8504b).notify();
                        return;
                    } finally {
                        ((AtomicReference) this.f8504b).notify();
                    }
                }
        }
    }

    public a61(com.google.android.gms.measurement.internal.s0 s0Var, String str, URL url, byte[] bArr, Map map, com.google.android.gms.measurement.internal.u0 u0Var) {
        this.f8503a = 1;
        this.f8509g = s0Var;
        com.google.android.gms.common.internal.w.checkNotEmpty(str);
        com.google.android.gms.common.internal.w.checkNotNull(url);
        com.google.android.gms.common.internal.w.checkNotNull(u0Var);
        this.f8504b = url;
        this.f8505c = bArr;
        this.f8506d = u0Var;
        this.f8507e = str;
        this.f8508f = map;
    }

    public a61(com.google.android.gms.measurement.internal.a3 a3Var, AtomicReference atomicReference, String str, String str2, com.google.android.gms.measurement.internal.d4 d4Var) {
        this.f8503a = 2;
        this.f8504b = atomicReference;
        this.f8505c = null;
        this.f8506d = str;
        this.f8507e = str2;
        this.f8508f = d4Var;
        this.f8509g = a3Var;
    }
}
