package O1;

import Z6.AbstractC0385z;
import Z6.C0369i;
import android.content.ContentValues;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import com.google.android.gms.internal.measurement.C0613b1;
import com.google.android.gms.internal.measurement.C0618c1;
import com.google.android.gms.internal.measurement.C0623d1;
import com.google.android.gms.internal.measurement.C0628e1;
import com.google.android.gms.measurement.internal.AppMeasurementDynamiteService;
import d3.BinderC0804m1;
import d3.BinderC0807n1;
import d3.BinderC0841z0;
import d3.C0773c0;
import d3.C0805n;
import d3.C0817r0;
import d3.C0824t1;
import d3.C0828v;
import d3.H1;
import d3.I1;
import d3.J1;
import d3.N1;
import d3.Q1;
import d3.R1;
import d3.RunnableC0820s0;
import d3.W1;
import d3.X1;
import d3.Y0;
import d3.Z;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import l6.C1139a;
import m.C1143d;
import m.MenuC1147h;
import m.MenuItemC1148i;
import m.ViewOnKeyListenerC1144e;
import n4.u0;

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

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

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

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

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

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

    public /* synthetic */ C(Object obj, Object obj2, Object obj3, Object obj4, int i) {
        this.f4928q = i;
        this.f4929r = obj;
        this.f4930s = obj2;
        this.f4931t = obj3;
        this.f4932u = obj4;
    }

    private final void a() {
        d3.K k8;
        C0824t1 c0824t1 = (C0824t1) this.f4929r;
        AtomicReference atomicReference = (AtomicReference) this.f4930s;
        X1 x12 = (X1) this.f4931t;
        I1 i12 = (I1) this.f4932u;
        synchronized (atomicReference) {
            try {
                k8 = c0824t1.f12179t;
            } catch (RemoteException e8) {
                Z z8 = ((C0817r0) c0824t1.f5146q).f12150y;
                C0817r0.k(z8);
                z8.f11875v.c(e8, "[sgtm] Failed to get upload batches; remote exception");
                atomicReference.notifyAll();
            }
            if (k8 != null) {
                k8.F(x12, i12, new BinderC0807n1(c0824t1, atomicReference));
                c0824t1.U();
            } else {
                Z z9 = ((C0817r0) c0824t1.f5146q).f12150y;
                C0817r0.k(z9);
                z9.f11875v.b("[sgtm] Failed to get upload batches; not connected to service");
            }
        }
    }

    /* JADX WARN: Can't wrap try/catch for region: R(16:153|100|(1:102)(5:103|(2:106|(1:124))|125|122|156)|108|(2:111|109)|158|112|148|113|(2:116|114)|159|118|(1:120)|121|122|156) */
    /* JADX WARN: Code restructure failed: missing block: B:117:0x0486, code lost:
    
        r6 = r16;
     */
    /* JADX WARN: Code restructure failed: missing block: B:123:0x04bd, code lost:
    
        r2.e().f11878y.c(r4, "Failed to parse queued batch. appId");
     */
    @Override // java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void run() throws Throwable {
        J1 j12;
        ArrayList arrayList;
        d3.K k8;
        d3.K k9;
        switch (this.f4928q) {
            case u0.f15495q /* 0 */:
                C0369i c0369i = (C0369i) this.f4930s;
                try {
                    AbstractC0385z.v(((E6.i) this.f4929r).n(E6.e.f2225q), new B((A) this.f4931t, c0369i, (D) this.f4932u, null));
                    return;
                } catch (Throwable th) {
                    c0369i.q(th);
                    return;
                }
            case 1:
                d3.O o8 = (d3.O) this.f4932u;
                Q1 q12 = ((BinderC0841z0) this.f4929r).f12234e;
                q12.j();
                Object obj = null;
                boolean zR = q12.h0().R(null, d3.I.f11517Q0);
                String str = (String) this.f4930s;
                if (zR) {
                    I1.a.u(q12);
                    C0805n c0805n = q12.f11683s;
                    Q1.L(c0805n);
                    List<R1> listL = c0805n.L(str, (I1) this.f4931t, ((Integer) d3.I.f11489B.a(null)).intValue());
                    ArrayList arrayList2 = new ArrayList();
                    for (R1 r12 : listL) {
                        boolean zC0 = q12.c0(str, r12.f11696c);
                        long j8 = r12.f11694a;
                        if (zC0) {
                            int i = r12.f11701h;
                            if (i <= 0) {
                                arrayList = arrayList2;
                            } else {
                                int iIntValue = ((Integer) d3.I.f11599z.a(obj)).intValue();
                                long j9 = r12.f11700g;
                                if (i <= iIntValue) {
                                    arrayList = arrayList2;
                                    long jMin = Math.min(((Long) d3.I.f11595x.a(obj)).longValue() * (1 << (i - 1)), ((Long) d3.I.f11597y.a(obj)).longValue());
                                    q12.d().getClass();
                                    if (System.currentTimeMillis() < jMin + j9) {
                                        arrayList2 = arrayList;
                                    }
                                }
                                q12.e().D.e("[sgtm] batch skipped waiting for next retry. appId, rowId, lastUploadMillis", str, Long.valueOf(j8), Long.valueOf(j9));
                                obj = null;
                            }
                            Bundle bundle = new Bundle();
                            for (Map.Entry entry : r12.f11697d.entrySet()) {
                                bundle.putString((String) entry.getKey(), (String) entry.getValue());
                            }
                            H1 h12 = new H1(r12.f11694a, r12.f11695b.c(), r12.f11696c, bundle, r12.f11698e.f11951q, r12.f11699f, "");
                            C0613b1 c0613b1 = (C0613b1) C0773c0.k0(C0618c1.o(), h12.f11481r);
                            for (int i5 = 0; i5 < ((C0618c1) c0613b1.f9801r).n(); i5++) {
                                C0623d1 c0623d1 = (C0623d1) ((C0618c1) c0613b1.f9801r).q(i5).f();
                                q12.d().getClass();
                                long jCurrentTimeMillis = System.currentTimeMillis();
                                c0623d1.k();
                                C0628e1.B1((C0628e1) c0623d1.f9801r, jCurrentTimeMillis);
                                c0613b1.k();
                                C0618c1.x((C0618c1) c0613b1.f9801r, i5, (C0628e1) c0623d1.h());
                                break;
                            }
                            h12.f11481r = ((C0618c1) c0613b1.h()).c();
                            if (Log.isLoggable(q12.e().P(), 2)) {
                                C0773c0 c0773c0 = q12.f11687w;
                                Q1.L(c0773c0);
                                h12.f11486w = c0773c0.l0((C0618c1) c0613b1.h());
                            }
                            arrayList2 = arrayList;
                            arrayList2.add(h12);
                            obj = null;
                        } else {
                            q12.e().D.e("[sgtm] batch skipped due to destination in backoff. appId, rowId, url", str, Long.valueOf(j8), r12.f11696c);
                        }
                    }
                    j12 = new J1(arrayList2);
                } else {
                    j12 = new J1(Collections.EMPTY_LIST);
                }
                try {
                    o8.A(j12);
                    q12.e().D.d("[sgtm] Sending queued upload batches to client. appId, count", str, Integer.valueOf(j12.f11611q.size()));
                    return;
                } catch (RemoteException e8) {
                    q12.e().f11875v.d("[sgtm] Failed to return upload batches for app", str, e8);
                    return;
                }
            case 2:
                BinderC0841z0 binderC0841z0 = (BinderC0841z0) this.f4929r;
                Q1 q13 = binderC0841z0.f12234e;
                boolean zR2 = q13.h0().R(null, d3.I.f11544d1);
                boolean zR3 = q13.h0().R(null, d3.I.f11550f1);
                Bundle bundle2 = (Bundle) this.f4930s;
                boolean zIsEmpty = bundle2.isEmpty();
                String str2 = (String) this.f4931t;
                Q1 q14 = binderC0841z0.f12234e;
                if (zIsEmpty && zR2) {
                    C0805n c0805n2 = q14.f11683s;
                    Q1.L(c0805n2);
                    c0805n2.E();
                    c0805n2.F();
                    try {
                        c0805n2.A0().execSQL("delete from default_event_params where app_id=?", new String[]{str2});
                        return;
                    } catch (SQLiteException e9) {
                        Z z8 = ((C0817r0) c0805n2.f5146q).f12150y;
                        C0817r0.k(z8);
                        z8.f11875v.c(e9, "Error clearing default event params");
                        return;
                    }
                }
                C0805n c0805n3 = q13.f11683s;
                Q1.L(c0805n3);
                c0805n3.E();
                c0805n3.F();
                d3.r rVar = new d3.r((C0817r0) c0805n3.f5146q, "", str2, "dep", 0L, 0L, bundle2);
                C0773c0 c0773c02 = c0805n3.f11473r.f11687w;
                Q1.L(c0773c02);
                byte[] bArrC = c0773c02.j0(rVar).c();
                Z z9 = ((C0817r0) c0805n3.f5146q).f12150y;
                C0817r0.k(z9);
                z9.D.d("Saving default event parameters, appId, data size", str2, Integer.valueOf(bArrC.length));
                ContentValues contentValues = new ContentValues();
                contentValues.put("app_id", str2);
                contentValues.put("parameters", bArrC);
                try {
                    if (c0805n3.A0().insertWithOnConflict("default_event_params", null, contentValues, 5) == -1) {
                        C0817r0.k(z9);
                        z9.f11875v.c(Z.M(str2), "Failed to insert default event parameters (got -1). appId");
                    }
                } catch (SQLiteException e10) {
                    C0817r0.k(z9);
                    z9.f11875v.d("Error storing default event parameters. appId", Z.M(str2), e10);
                }
                C0805n c0805n4 = q14.f11683s;
                Q1.L(c0805n4);
                long j10 = ((X1) this.f4932u).f11829V;
                C0817r0 c0817r0 = (C0817r0) c0805n4.f5146q;
                if (!c0817r0.f12148w.R(null, d3.I.f11550f1)) {
                    c0817r0.D.getClass();
                    if (System.currentTimeMillis() > 15000 + j10) {
                        return;
                    }
                }
                try {
                    if (c0805n4.w0(0L, "select count(*) from raw_events where app_id=? and timestamp >= ? and name not like '!_%' escape '!' limit 1;", new String[]{str2, String.valueOf(j10)}) > 0) {
                        return;
                    }
                    if (c0805n4.w0(0L, "select count(*) from raw_events where app_id=? and timestamp >= ? and name like '!_%' escape '!' limit 1;", new String[]{str2, String.valueOf(j10)}) > 0) {
                        if (zR3) {
                            C0805n c0805n5 = q14.f11683s;
                            Q1.L(c0805n5);
                            c0805n5.O(str2, Long.valueOf(j10), null, bundle2);
                            return;
                        } else {
                            C0805n c0805n6 = q14.f11683s;
                            Q1.L(c0805n6);
                            c0805n6.O(str2, null, null, bundle2);
                            return;
                        }
                    }
                    return;
                } catch (SQLiteException e11) {
                    Z z10 = c0817r0.f12150y;
                    C0817r0.k(z10);
                    z10.f11875v.c(e11, "Error checking backfill conditions");
                    return;
                }
            case 3:
                C0824t1 c0824t1R = ((AppMeasurementDynamiteService) this.f4932u).f10093e.r();
                c0824t1R.E();
                c0824t1R.F();
                C0817r0 c0817r02 = (C0817r0) c0824t1R.f5146q;
                W1 w12 = c0817r02.f12122B;
                C0817r0.i(w12);
                int iB = M2.f.f4167b.b(((C0817r0) w12.f5146q).f12142q, 12451000);
                com.google.android.gms.internal.measurement.L l8 = (com.google.android.gms.internal.measurement.L) this.f4929r;
                if (iB == 0) {
                    c0824t1R.V(new C(c0824t1R, (C0828v) this.f4930s, (String) this.f4931t, l8, 8, false));
                    return;
                }
                Z z11 = c0817r02.f12150y;
                C0817r0.k(z11);
                z11.f11878y.b("Not bundling data. Service unavailable or out of date");
                W1 w13 = c0817r02.f12122B;
                C0817r0.i(w13);
                w13.d0(l8, new byte[0]);
                return;
            case 4:
                C0824t1 c0824t1R2 = ((C0817r0) ((Y0) this.f4932u).f5146q).r();
                c0824t1R2.E();
                c0824t1R2.F();
                c0824t1R2.V(new RunnableC0820s0(c0824t1R2, (AtomicReference) this.f4929r, (String) this.f4930s, (String) this.f4931t, c0824t1R2.S(false)));
                return;
            case 5:
                C0824t1 c0824t1 = (C0824t1) this.f4929r;
                AtomicReference atomicReference = (AtomicReference) this.f4930s;
                X1 x12 = (X1) this.f4931t;
                Bundle bundle3 = (Bundle) this.f4932u;
                synchronized (atomicReference) {
                    try {
                        k8 = c0824t1.f12179t;
                    } catch (RemoteException e12) {
                        Z z12 = ((C0817r0) c0824t1.f5146q).f12150y;
                        C0817r0.k(z12);
                        z12.f11875v.c(e12, "Failed to request trigger URIs; remote exception");
                        atomicReference.notifyAll();
                    }
                    if (k8 != null) {
                        k8.k(x12, bundle3, new BinderC0804m1(atomicReference));
                        c0824t1.U();
                        return;
                    } else {
                        Z z13 = ((C0817r0) c0824t1.f5146q).f12150y;
                        C0817r0.k(z13);
                        z13.f11875v.b("Failed to request trigger URIs; not connected to service");
                        return;
                    }
                }
            case Z.g.STRING_SET_FIELD_NUMBER /* 6 */:
                a();
                return;
            case Z.g.DOUBLE_FIELD_NUMBER /* 7 */:
                C0824t1 c0824t1R3 = ((AppMeasurementDynamiteService) this.f4932u).f10093e.r();
                c0824t1R3.E();
                c0824t1R3.F();
                c0824t1R3.V(new RunnableC0820s0(c0824t1R3, (String) this.f4930s, (String) this.f4931t, c0824t1R3.S(false), (com.google.android.gms.internal.measurement.L) this.f4929r));
                return;
            case Z.g.BYTES_FIELD_NUMBER /* 8 */:
                com.google.android.gms.internal.measurement.L l9 = (com.google.android.gms.internal.measurement.L) this.f4931t;
                C0824t1 c0824t12 = (C0824t1) this.f4932u;
                C0817r0 c0817r03 = (C0817r0) c0824t12.f5146q;
                byte[] bArrB = null;
                try {
                    try {
                        k9 = c0824t12.f12179t;
                    } finally {
                        W1 w14 = c0817r03.f12122B;
                        C0817r0.i(w14);
                        w14.d0(l9, null);
                    }
                } catch (RemoteException e13) {
                    Z z14 = c0817r03.f12150y;
                    C0817r0.k(z14);
                    z14.f11875v.c(e13, "Failed to send event to the service to bundle");
                }
                if (k9 != null) {
                    bArrB = k9.B((C0828v) this.f4929r, (String) this.f4930s);
                    c0824t12.U();
                    return;
                } else {
                    Z z15 = c0817r03.f12150y;
                    C0817r0.k(z15);
                    z15.f11875v.b("Discarding data. Failed to send event to service to bundle");
                    return;
                }
            case 9:
                Q1 q15 = ((N1) this.f4932u).f11629q;
                W1 w1G = q15.g();
                q15.d().getClass();
                C0828v c0828vK = w1G.K((String) this.f4930s, (Bundle) this.f4931t, "auto", System.currentTimeMillis(), false);
                P2.w.f(c0828vK);
                q15.r(c0828vK, (String) this.f4929r);
                return;
            default:
                C1143d c1143d = (C1143d) this.f4929r;
                if (c1143d != null) {
                    C1139a c1139a = (C1139a) this.f4932u;
                    ((ViewOnKeyListenerC1144e) c1139a.f14409q).f14448P = true;
                    c1143d.f14433b.c(false);
                    ((ViewOnKeyListenerC1144e) c1139a.f14409q).f14448P = false;
                }
                MenuItemC1148i menuItemC1148i = (MenuItemC1148i) this.f4930s;
                if (menuItemC1148i.isEnabled() && menuItemC1148i.hasSubMenu()) {
                    ((MenuC1147h) this.f4931t).p(menuItemC1148i, null, 4);
                    return;
                }
                return;
        }
    }

    public /* synthetic */ C(Object obj, Object obj2, Object obj3, Object obj4, int i, boolean z8) {
        this.f4928q = i;
        this.f4929r = obj2;
        this.f4930s = obj3;
        this.f4931t = obj4;
        this.f4932u = obj;
    }
}
