package J;

import A1.y;
import P2.w;
import android.app.Service;
import android.app.job.JobParameters;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.internal.measurement.C0611b;
import com.google.android.gms.internal.measurement.D;
import com.google.android.gms.internal.measurement.G0;
import com.google.android.gms.internal.measurement.L;
import com.google.android.gms.internal.measurement.N;
import d3.BinderC0841z0;
import d3.C0773c0;
import d3.C0775d;
import d3.C0778e;
import d3.C0784g;
import d3.C0785g0;
import d3.C0803m0;
import d3.C0805n;
import d3.C0817r0;
import d3.C0824t1;
import d3.C0825u;
import d3.C0828v;
import d3.E0;
import d3.H;
import d3.I;
import d3.InterfaceC0833w1;
import d3.K;
import d3.P1;
import d3.Q1;
import d3.R1;
import d3.T1;
import d3.V0;
import d3.W1;
import d3.X1;
import d3.Y0;
import d3.Z;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import k4.C1099b;
import n4.u0;
import r4.C1495b;

/* JADX INFO: loaded from: classes.dex */
public final class m implements Runnable {

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public Object f3442r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public Object f3443s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public Object f3444t;

    public /* synthetic */ m() {
        this.f3441q = 0;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:179:0x04fc  */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    @Override // java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void run() throws Throwable {
        Object objCall;
        Cursor cursor;
        C0775d c0775d;
        C0817r0 c0817r0;
        int i;
        Cursor cursorQuery;
        R1 r1H0;
        Cursor cursor2;
        String string;
        byte[] blob;
        String string2;
        String string3;
        int i5;
        int i8;
        G0 g02;
        C0825u c0825u;
        AtomicReference atomicReference;
        C0824t1 c0824t1;
        C0817r0 c0817r02;
        C0785g0 c0785g0;
        W1 w12;
        boolean zK;
        C0785g0 c0785g02;
        Z z8;
        int i9 = 0;
        strF = null;
        String strF = null;
        switch (this.f3441q) {
            case u0.f15495q /* 0 */:
                try {
                    objCall = ((f) this.f3442r).call();
                    break;
                } catch (Exception unused) {
                    objCall = null;
                }
                ((Handler) this.f3444t).post(new G3.b((g) this.f3443s, 3, objCall));
                return;
            case 1:
                Q1 q12 = ((BinderC0841z0) this.f3442r).f12234e;
                q12.j();
                String str = ((X1) this.f3443s).f11831q;
                w.f(str);
                C0784g c0784gH0 = q12.h0();
                H h8 = I.f11517Q0;
                if (c0784gH0.R(null, h8)) {
                    I1.a.u(q12);
                    C0805n c0805n = q12.f11683s;
                    Q1.L(c0805n);
                    C0775d c0775d2 = (C0775d) this.f3444t;
                    long j8 = c0775d2.f11910q;
                    C0817r0 c0817r03 = (C0817r0) c0805n.f5146q;
                    if (c0817r03.f12148w.R(null, h8)) {
                        c0805n.E();
                        c0805n.F();
                        try {
                            cursorQuery = c0805n.A0().query("upload_queue", new String[]{"rowId", "app_id", "measurement_batch", "upload_uri", "upload_headers", "upload_type", "retry_count", "creation_timestamp", "associated_row_id", "last_upload_timestamp"}, "rowId=?", new String[]{String.valueOf(j8)}, null, null, null, "1");
                            try {
                                try {
                                } catch (Throwable th) {
                                    th = th;
                                    cursor2 = cursorQuery;
                                }
                            } catch (SQLiteException e8) {
                                e = e8;
                                c0775d = c0775d2;
                                c0817r0 = c0817r03;
                                i = 4;
                                cursor2 = cursorQuery;
                            }
                            break;
                        } catch (SQLiteException e9) {
                            e = e9;
                            c0775d = c0775d2;
                            c0817r0 = c0817r03;
                            i = 4;
                            cursorQuery = null;
                        } catch (Throwable th2) {
                            th = th2;
                            cursor = null;
                        }
                        if (cursorQuery.moveToFirst()) {
                            try {
                                string = cursorQuery.getString(1);
                                w.f(string);
                                blob = cursorQuery.getBlob(2);
                                string2 = cursorQuery.getString(3);
                                string3 = cursorQuery.getString(4);
                                try {
                                    i5 = cursorQuery.getInt(5);
                                    i8 = cursorQuery.getInt(6);
                                    cursorQuery.getLong(7);
                                    c0817r0 = c0817r03;
                                    c0775d = c0775d2;
                                    cursor2 = cursorQuery;
                                    i = 4;
                                } catch (SQLiteException e10) {
                                    e = e10;
                                    c0817r0 = c0817r03;
                                    cursor2 = cursorQuery;
                                    c0775d = c0775d2;
                                    i = 4;
                                }
                            } catch (SQLiteException e11) {
                                e = e11;
                                c0817r0 = c0817r03;
                                i = 4;
                                cursor2 = cursorQuery;
                                c0775d = c0775d2;
                            }
                            try {
                                r1H0 = c0805n.h0(string, j8, blob, string2, string3, i5, i8, cursorQuery.getLong(8), cursorQuery.getLong(9));
                                cursor2.close();
                            } catch (SQLiteException e12) {
                                e = e12;
                                cursorQuery = cursor2;
                                try {
                                    Z z9 = c0817r0.f12150y;
                                    C0817r0.k(z9);
                                    z9.f11875v.d("Error to querying MeasurementBatch from upload_queue. rowId", Long.valueOf(j8), e);
                                    if (cursorQuery != null) {
                                    }
                                    r1H0 = null;
                                } catch (Throwable th3) {
                                    th = th3;
                                    cursor = cursorQuery;
                                }
                            } catch (Throwable th4) {
                                th = th4;
                            }
                        } else {
                            c0775d = c0775d2;
                            i = 4;
                            if (cursorQuery != null) {
                                cursorQuery.close();
                            }
                            r1H0 = null;
                        }
                        cursor = cursor2;
                        if (cursor != null) {
                            cursor.close();
                        }
                        throw th;
                    }
                    r1H0 = null;
                    c0775d = c0775d2;
                    i = 4;
                    if (r1H0 == null) {
                        q12.e().f11878y.d("[sgtm] Queued batch doesn't exist. appId, rowId", str, Long.valueOf(j8));
                        return;
                    }
                    HashMap map = q12.f11676U;
                    String str2 = r1H0.f11696c;
                    int i10 = c0775d.f11911r;
                    if (i10 != 1) {
                        if (i10 == 3) {
                            P1 p1 = (P1) map.get(str2);
                            if (p1 == null) {
                                p1 = new P1(q12);
                                map.put(str2, p1);
                            } else {
                                p1.f11650b++;
                                p1.f11651c = p1.a();
                            }
                            long j9 = p1.f11651c;
                            q12.d().getClass();
                            q12.e().D.e("[sgtm] Putting sGTM server in backoff mode. appId, destination, nextRetryInSeconds", str, str2, Long.valueOf((j9 - System.currentTimeMillis()) / 1000));
                        }
                        C0805n c0805n2 = q12.f11683s;
                        Q1.L(c0805n2);
                        Long lValueOf = Long.valueOf(c0775d.f11910q);
                        c0805n2.V(lValueOf);
                        q12.e().D.d("[sgtm] increased batch retry count after failed client upload. appId, rowId", str, lValueOf);
                        return;
                    }
                    if (map.containsKey(str2)) {
                        map.remove(str2);
                    }
                    C0805n c0805n3 = q12.f11683s;
                    Q1.L(c0805n3);
                    Long lValueOf2 = Long.valueOf(j8);
                    c0805n3.S(lValueOf2);
                    q12.e().D.d("[sgtm] queued batch deleted after successful client upload. appId, rowId", str, lValueOf2);
                    long j10 = c0775d.f11912s;
                    if (j10 > 0) {
                        C0805n c0805n4 = q12.f11683s;
                        Q1.L(c0805n4);
                        C0817r0 c0817r04 = (C0817r0) c0805n4.f5146q;
                        C0784g c0784g = c0817r04.f12148w;
                        Z z10 = c0817r04.f12150y;
                        if (c0784g.R(null, h8)) {
                            c0805n4.E();
                            c0805n4.F();
                            Long lValueOf3 = Long.valueOf(j10);
                            ContentValues contentValues = new ContentValues();
                            contentValues.put("upload_type", (Integer) 1);
                            c0817r04.D.getClass();
                            contentValues.put("creation_timestamp", Long.valueOf(System.currentTimeMillis()));
                            try {
                                if (c0805n4.A0().update("upload_queue", contentValues, "rowid=? AND app_id=? AND upload_type=?", new String[]{String.valueOf(j10), str, String.valueOf(i)}) != 1) {
                                    C0817r0.k(z10);
                                    z10.f11878y.d("Google Signal pending batch not updated. appId, rowId", str, lValueOf3);
                                }
                            } catch (SQLiteException e13) {
                                C0817r0.k(z10);
                                z10.f11875v.e("Failed to update google Signal pending batch. appid, rowId", str, Long.valueOf(j10), e13);
                                throw e13;
                            }
                            break;
                        }
                        q12.e().D.d("[sgtm] queued Google Signal batch updated. appId, signalRowId", str, Long.valueOf(j10));
                        q12.Z(str);
                        return;
                    }
                    return;
                }
                return;
            case 2:
                BinderC0841z0 binderC0841z0 = (BinderC0841z0) this.f3444t;
                binderC0841z0.f12234e.j();
                C0778e c0778e = (C0778e) this.f3442r;
                Object objA = c0778e.f11920s.a();
                X1 x12 = (X1) this.f3443s;
                Q1 q13 = binderC0841z0.f12234e;
                if (objA == null) {
                    q13.P(c0778e, x12);
                    return;
                } else {
                    q13.U(c0778e, x12);
                    return;
                }
            case 3:
                BinderC0841z0 binderC0841z02 = (BinderC0841z0) this.f3444t;
                binderC0841z02.getClass();
                C0828v c0828v = (C0828v) this.f3442r;
                boolean zEquals = "_cmp".equals(c0828v.f12193q);
                Q1 q14 = binderC0841z02.f12234e;
                if (zEquals && (c0825u = c0828v.f12194r) != null) {
                    Bundle bundle = c0825u.f12186q;
                    if (bundle.size() != 0) {
                        String string4 = bundle.getString("_cis");
                        if ("referrer broadcast".equals(string4) || "referrer API".equals(string4)) {
                            q14.e().f11870B.c(c0828v.toString(), "Event has been filtered ");
                            c0828v = new C0828v("_cmpx", c0825u, c0828v.f12195s, c0828v.f12196t);
                        }
                    }
                }
                String str3 = c0828v.f12193q;
                boolean zBooleanValue = ((Boolean) I.f11576o1.a(null)).booleanValue();
                X1 x13 = (X1) this.f3443s;
                if (!zBooleanValue) {
                    C0803m0 c0803m0 = q14.f11681q;
                    Q1.L(c0803m0);
                    String str4 = x13.f11831q;
                    if (TextUtils.isEmpty(str4) || (g02 = (G0) c0803m0.f12056x.get(str4)) == null || g02.n() == 0) {
                        binderC0841z02.M(c0828v, x13);
                        return;
                    }
                    q14.e().D.c(str4, "EES config found for");
                }
                C0803m0 c0803m02 = q14.f11681q;
                C0773c0 c0773c0 = q14.f11687w;
                Q1.L(c0803m02);
                String str5 = x13.f11831q;
                D d8 = TextUtils.isEmpty(str5) ? null : (D) c0803m02.f12058z.h(str5);
                if (d8 == null) {
                    q14.e().D.c(x13.f11831q, "EES not loaded for");
                    binderC0841z02.M(c0828v, x13);
                    return;
                }
                try {
                    y yVar = d8.f9579c;
                    Q1.L(c0773c0);
                    HashMap mapP0 = C0773c0.p0(c0828v.f12194r.a(), true);
                    String strD = d3.G0.d(str3, d3.G0.f11465c, d3.G0.f11463a);
                    if (strD == null) {
                        strD = str3;
                    }
                    if (d8.b(new C0611b(strD, c0828v.f12196t, mapP0))) {
                        if (((C0611b) yVar.f364s).equals((C0611b) yVar.f363r)) {
                            binderC0841z02.M(c0828v, x13);
                        } else {
                            q14.e().D.c(str3, "EES edited event");
                            Q1.L(c0773c0);
                            binderC0841z02.M(C0773c0.h0((C0611b) yVar.f364s), x13);
                        }
                        if (((ArrayList) yVar.f365t).isEmpty()) {
                            return;
                        }
                        ArrayList arrayList = (ArrayList) yVar.f365t;
                        int size = arrayList.size();
                        while (i9 < size) {
                            Object obj = arrayList.get(i9);
                            i9++;
                            C0611b c0611b = (C0611b) obj;
                            q14.e().D.c(c0611b.f9812a, "EES logging created event");
                            Q1.L(c0773c0);
                            binderC0841z02.M(C0773c0.h0(c0611b), x13);
                        }
                        return;
                    }
                } catch (N unused2) {
                    q14.e().f11875v.d("EES error. appId, eventName", x13.f11832r, str3);
                }
                q14.e().D.c(str3, "EES was not applied to event");
                binderC0841z02.M(c0828v, x13);
                return;
            case 4:
                BinderC0841z0 binderC0841z03 = (BinderC0841z0) this.f3444t;
                binderC0841z03.f12234e.j();
                binderC0841z03.f12234e.r((C0828v) this.f3442r, (String) this.f3443s);
                return;
            case 5:
                BinderC0841z0 binderC0841z04 = (BinderC0841z0) this.f3444t;
                binderC0841z04.f12234e.j();
                T1 t12 = (T1) this.f3442r;
                Object objA2 = t12.a();
                X1 x14 = (X1) this.f3443s;
                Q1 q15 = binderC0841z04.f12234e;
                if (objA2 == null) {
                    q15.Q(t12.f11723r, x14);
                    return;
                } else {
                    q15.W(t12, x14);
                    return;
                }
            case Z.g.STRING_SET_FIELD_NUMBER /* 6 */:
                X1 x15 = (X1) this.f3443s;
                C0775d c0775d3 = (C0775d) this.f3444t;
                C0824t1 c0824t12 = (C0824t1) this.f3442r;
                K k8 = c0824t12.f12179t;
                C0817r0 c0817r05 = (C0817r0) c0824t12.f5146q;
                if (k8 == null) {
                    Z z11 = c0817r05.f12150y;
                    C0817r0.k(z11);
                    z11.f11875v.b("[sgtm] Discarding data. Failed to update batch upload status.");
                    return;
                }
                try {
                    k8.s(x15, c0775d3);
                    c0824t12.U();
                    return;
                } catch (RemoteException e14) {
                    Z z12 = c0817r05.f12150y;
                    C0817r0.k(z12);
                    z12.f11875v.d("[sgtm] Failed to update batch upload status, rowId, exception", Long.valueOf(c0775d3.f11910q), e14);
                    return;
                }
            case Z.g.DOUBLE_FIELD_NUMBER /* 7 */:
                AtomicReference atomicReference2 = (AtomicReference) this.f3442r;
                synchronized (atomicReference2) {
                    try {
                        try {
                            c0824t1 = (C0824t1) this.f3444t;
                            c0817r02 = (C0817r0) c0824t1.f5146q;
                            c0785g0 = c0817r02.f12149x;
                            C0817r0.i(c0785g0);
                        } catch (RemoteException e15) {
                            Z z13 = ((C0817r0) ((C0824t1) this.f3444t).f5146q).f12150y;
                            C0817r0.k(z13);
                            z13.f11875v.c(e15, "Failed to get app instance id");
                            atomicReference = (AtomicReference) this.f3442r;
                        }
                        if (c0785g0.L().k(E0.f11448s)) {
                            K k9 = c0824t1.f12179t;
                            if (k9 != null) {
                                atomicReference2.set(k9.f((X1) this.f3443s));
                                String str6 = (String) atomicReference2.get();
                                if (str6 != null) {
                                    Y0 y02 = ((C0817r0) c0824t1.f5146q).f12125F;
                                    C0817r0.j(y02);
                                    y02.f11857w.set(str6);
                                    C0785g0 c0785g03 = c0817r02.f12149x;
                                    C0817r0.i(c0785g03);
                                    c0785g03.f11976x.g(str6);
                                }
                                c0824t1.U();
                                atomicReference = (AtomicReference) this.f3442r;
                                atomicReference.notify();
                                return;
                            }
                            Z z14 = c0817r02.f12150y;
                            C0817r0.k(z14);
                            z14.f11875v.b("Failed to get app instance id");
                            atomicReference2.notify();
                        } else {
                            Z z15 = c0817r02.f12150y;
                            C0817r0.k(z15);
                            z15.f11869A.b("Analytics storage consent denied; will not get app instance id");
                            Y0 y03 = ((C0817r0) c0824t1.f5146q).f12125F;
                            C0817r0.j(y03);
                            y03.f11857w.set(null);
                            C0785g0 c0785g04 = c0817r02.f12149x;
                            C0817r0.i(c0785g04);
                            c0785g04.f11976x.g(null);
                            atomicReference2.set(null);
                            atomicReference2.notify();
                        }
                        return;
                    } catch (Throwable th5) {
                        ((AtomicReference) this.f3442r).notify();
                        throw th5;
                    }
                }
            case Z.g.BYTES_FIELD_NUMBER /* 8 */:
                L l8 = (L) this.f3443s;
                C0824t1 c0824t13 = (C0824t1) this.f3444t;
                C0817r0 c0817r06 = (C0817r0) c0824t13.f5146q;
                try {
                    try {
                        C0785g0 c0785g05 = c0817r06.f12149x;
                        C0817r0.i(c0785g05);
                        zK = c0785g05.L().k(E0.f11448s);
                        c0785g02 = c0817r06.f12149x;
                        z8 = c0817r06.f12150y;
                    } catch (RemoteException e16) {
                        Z z16 = c0817r06.f12150y;
                        C0817r0.k(z16);
                        z16.f11875v.c(e16, "Failed to get app instance id");
                    }
                    if (zK) {
                        K k10 = c0824t13.f12179t;
                        if (k10 != null) {
                            strF = k10.f((X1) this.f3442r);
                            if (strF != null) {
                                Y0 y04 = c0817r06.f12125F;
                                C0817r0.j(y04);
                                y04.f11857w.set(strF);
                                C0817r0.i(c0785g02);
                                c0785g02.f11976x.g(strF);
                            }
                            c0824t13.U();
                            w12 = c0817r06.f12122B;
                            C0817r0.i(w12);
                            w12.g0(strF, l8);
                            return;
                        }
                        C0817r0.k(z8);
                        z8.f11875v.b("Failed to get app instance id");
                    } else {
                        C0817r0.k(z8);
                        z8.f11869A.b("Analytics storage consent denied; will not get app instance id");
                        Y0 y05 = c0817r06.f12125F;
                        C0817r0.j(y05);
                        y05.f11857w.set(null);
                        C0817r0.i(c0785g02);
                        c0785g02.f11976x.g(null);
                    }
                    w12 = c0817r06.f12122B;
                    C0817r0.i(w12);
                    w12.g0(strF, l8);
                    return;
                } catch (Throwable th6) {
                    W1 w13 = c0817r06.f12122B;
                    C0817r0.i(w13);
                    w13.g0(null, l8);
                    throw th6;
                }
            case 9:
                ((Z) this.f3443s).D.b("AppMeasurementJobService processed last upload request.");
                ((InterfaceC0833w1) ((Service) ((V0) this.f3442r).f11784r)).c((JobParameters) this.f3444t);
                return;
            default:
                C1495b c1495b = (C1495b) this.f3444t;
                C1099b c1099b = (C1099b) this.f3442r;
                c1495b.b(c1099b, (g3.h) this.f3443s);
                ((AtomicInteger) c1495b.i.f12398s).set(0);
                double dMin = Math.min(3600000.0d, Math.pow(c1495b.f16445b, c1495b.a()) * (60000.0d / c1495b.f16444a));
                String str7 = "Delay for: " + String.format(Locale.US, "%.2f", Double.valueOf(dMin / 1000.0d)) + " s for report: " + c1099b.f13846b;
                if (Log.isLoggable("FirebaseCrashlytics", 3)) {
                    Log.d("FirebaseCrashlytics", str7, null);
                }
                try {
                    Thread.sleep((long) dMin);
                    return;
                } catch (InterruptedException unused3) {
                    return;
                }
        }
    }

    public /* synthetic */ m(Object obj, Object obj2, Parcelable parcelable, int i) {
        this.f3441q = i;
        this.f3442r = obj;
        this.f3443s = obj2;
        this.f3444t = parcelable;
    }

    public /* synthetic */ m(Object obj, Object obj2, Object obj3, int i) {
        this.f3441q = i;
        this.f3442r = obj2;
        this.f3443s = obj3;
        this.f3444t = obj;
    }
}
