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

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.SparseArray;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import androidx.privacysandbox.ads.adservices.java.measurement.MeasurementManagerFutures$Api33Ext5JavaImpl;
import com.google.android.gms.internal.measurement.y7;
import com.google.android.gms.internal.measurement.z9;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes2.dex */
public final class g2 extends x0 {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public q2 f21618c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public a f21619d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f21621f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final AtomicReference f21622g;

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public boolean f21624i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f21625j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public m2 f21626k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public PriorityQueue f21627l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public boolean f21628m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public b2 f21629n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final AtomicLong f21630o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public long f21631p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final p1 f21632q;

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

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

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public m2 f21636u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final h f21637v;

    public g2(m1 m1Var) {
        super(m1Var);
        this.f21620e = new CopyOnWriteArraySet();
        this.f21623h = new Object();
        this.f21624i = false;
        this.f21625j = 1;
        this.f21633r = true;
        this.f21637v = new h(this, 4);
        this.f21622g = new AtomicReference();
        this.f21629n = b2.f21485c;
        this.f21631p = -1L;
        this.f21630o = new AtomicLong(0L);
        this.f21632q = new p1(m1Var);
    }

    public static void L(g2 g2Var, b2 b2Var, long j10, boolean z10, boolean z11) {
        g2Var.w();
        g2Var.D();
        b2 b2VarH = g2Var.u().H();
        if (j10 <= g2Var.f21631p) {
            if (b2.h(b2VarH.f21487b, b2Var.f21487b)) {
                g2Var.zzj().f21840l.b(b2Var, "Dropped out-of-date consent setting, proposed settings");
                return;
            }
        }
        y0 y0VarU = g2Var.u();
        y0VarU.w();
        int i10 = b2Var.f21487b;
        if (!b2.h(i10, y0VarU.F().getInt("consent_source", 100))) {
            p0 p0VarZzj = g2Var.zzj();
            p0VarZzj.f21840l.b(Integer.valueOf(b2Var.f21487b), "Lower precedence consent source ignored, proposed source");
            return;
        }
        SharedPreferences.Editor editorEdit = y0VarU.F().edit();
        editorEdit.putString("consent_settings", b2Var.n());
        editorEdit.putInt("consent_source", i10);
        editorEdit.apply();
        g2Var.zzj().f21842n.b(b2Var, "Setting storage consent(FE)");
        g2Var.f21631p = j10;
        a3 a3VarB = g2Var.B();
        a3VarB.w();
        a3VarB.D();
        if (a3VarB.P() && a3VarB.v().z0() < 241200) {
            g2Var.B().K(z10);
        } else {
            a3 a3VarB2 = g2Var.B();
            a3VarB2.w();
            a3VarB2.D();
            y7.a();
            if (!a3VarB2.s().J(null, z.W0) && z10) {
                a3VarB2.y().I();
            }
            a3VarB2.I(new z2(a3VarB2, 0));
        }
        if (z11) {
            g2Var.B().J(new AtomicReference());
        }
    }

    public static void M(g2 g2Var, b2 b2Var, b2 b2Var2) {
        y7.a();
        if (g2Var.s().J(null, z.W0)) {
            return;
        }
        zzje$zza zzje_zza = zzje$zza.ANALYTICS_STORAGE;
        zzje$zza zzje_zza2 = zzje$zza.AD_STORAGE;
        zzje$zza[] zzje_zzaArr = {zzje_zza, zzje_zza2};
        b2Var.getClass();
        boolean z10 = false;
        int i10 = 0;
        while (true) {
            if (i10 >= 2) {
                break;
            }
            zzje$zza zzje_zza3 = zzje_zzaArr[i10];
            if (!b2Var2.i(zzje_zza3) && b2Var.i(zzje_zza3)) {
                z10 = true;
                break;
            }
            i10++;
        }
        boolean zL = b2Var.l(b2Var2, zzje_zza, zzje_zza2);
        if (z10 || zL) {
            g2Var.x().I();
        }
    }

    @Override // com.google.android.gms.measurement.internal.x0
    public final boolean F() {
        return false;
    }

    public final void G(long j10, boolean z10) {
        w();
        D();
        zzj().f21841m.c("Resetting analytics data (FE)");
        m3 m3VarC = C();
        m3VarC.w();
        xyz.stream.utils.k0 k0Var = m3VarC.f21780f;
        ((p) k0Var.f36157a).a();
        if (((m3) k0Var.f36160d).s().J(null, z.a1)) {
            ((i8.d) ((m3) k0Var.f36160d).zzb()).getClass();
            k0Var.f36158b = SystemClock.elapsedRealtime();
        } else {
            k0Var.f36158b = 0L;
        }
        k0Var.f36159c = k0Var.f36158b;
        x().I();
        boolean zE = ((m1) this.f31277a).e();
        y0 y0VarU = u();
        y0VarU.f21989g.b(j10);
        if (!TextUtils.isEmpty(y0VarU.u().f22005w.f())) {
            y0VarU.f22005w.g(null);
        }
        y0VarU.f21999q.b(0L);
        y0VarU.f22000r.b(0L);
        Boolean boolH = y0VarU.s().H("firebase_analytics_collection_deactivated");
        if (boolH == null || !boolH.booleanValue()) {
            y0VarU.D(!zE);
        }
        y0VarU.f22006x.g(null);
        y0VarU.f22007y.b(0L);
        y0VarU.f22008z.u(null);
        if (z10) {
            a3 a3VarB = B();
            a3VarB.w();
            a3VarB.D();
            d4 d4VarS = a3VarB.S(false);
            a3VarB.y().I();
            a3VarB.I(new d3(a3VarB, d4VarS, 0));
        }
        C().f21779e.f();
        this.f21633r = !zE;
    }

    public final void H(Bundle bundle, int i10, long j10) {
        String string;
        zzjh zzjhVar;
        D();
        b2 b2Var = b2.f21485c;
        zzje$zza[] zzje_zzaArr = zzjg.STORAGE.zzd;
        int length = zzje_zzaArr.length;
        int i11 = 0;
        while (true) {
            if (i11 >= length) {
                string = null;
                break;
            }
            zzje$zza zzje_zza = zzje_zzaArr[i11];
            if (bundle.containsKey(zzje_zza.zze) && (string = bundle.getString(zzje_zza.zze)) != null) {
                if ((string.equals("granted") ? Boolean.TRUE : string.equals("denied") ? Boolean.FALSE : null) == null) {
                    break;
                }
            }
            i11++;
        }
        if (string != null) {
            zzj().f21839k.b(string, "Ignoring invalid consent setting");
            zzj().f21839k.c("Valid consent values are 'granted', 'denied'");
        }
        boolean zK = zzl().K();
        b2 b2VarB = b2.b(i10, bundle);
        Iterator it = b2VarB.f21486a.values().iterator();
        while (true) {
            boolean zHasNext = it.hasNext();
            zzjhVar = zzjh.UNINITIALIZED;
            if (!zHasNext) {
                break;
            } else if (((zzjh) it.next()) != zzjhVar) {
                K(b2VarB, j10, zK);
                break;
            }
        }
        q qVarA = q.a(i10, bundle);
        Iterator it2 = qVarA.f21866e.values().iterator();
        while (true) {
            if (!it2.hasNext()) {
                break;
            } else if (((zzjh) it2.next()) != zzjhVar) {
                I(qVarA, zK);
                break;
            }
        }
        Boolean boolC = q.c(bundle);
        if (boolC != null) {
            String str = i10 == -30 ? "tcf" : "app";
            if (s().J(null, z.R0) && zK) {
                S(str, "allow_personalized_ads", boolC.toString(), j10);
            } else {
                T(str, "allow_personalized_ads", boolC.toString(), false, j10);
            }
        }
    }

    public final void I(q qVar, boolean z10) {
        s1 s1Var = new s1((Object) this, (Object) qVar, 7);
        if (!z10) {
            zzl().I(s1Var);
        } else {
            w();
            s1Var.run();
        }
    }

    public final void J(b2 b2Var) {
        w();
        boolean z10 = (b2Var.i(zzje$zza.ANALYTICS_STORAGE) && b2Var.i(zzje$zza.AD_STORAGE)) || B().O();
        m1 m1Var = (m1) this.f31277a;
        j1 j1Var = m1Var.f21758j;
        m1.d(j1Var);
        j1Var.w();
        if (z10 != m1Var.D) {
            m1 m1Var2 = (m1) this.f31277a;
            j1 j1Var2 = m1Var2.f21758j;
            m1.d(j1Var2);
            j1Var2.w();
            m1Var2.D = z10;
            y0 y0VarU = u();
            y0VarU.w();
            Boolean boolValueOf = y0VarU.F().contains("measurement_enabled_from_api") ? Boolean.valueOf(y0VarU.F().getBoolean("measurement_enabled_from_api", true)) : null;
            if (!z10 || boolValueOf == null || boolValueOf.booleanValue()) {
                N(Boolean.valueOf(z10), false);
            }
        }
    }

    public final void K(b2 b2Var, long j10, boolean z10) {
        b2 b2Var2;
        boolean z11;
        boolean zL;
        boolean z12;
        b2 b2VarJ = b2Var;
        D();
        int i10 = b2VarJ.f21487b;
        if (i10 != -10) {
            zzjh zzjhVar = (zzjh) b2VarJ.f21486a.get(zzje$zza.AD_STORAGE);
            if (zzjhVar == null) {
                zzjhVar = zzjh.UNINITIALIZED;
            }
            zzjh zzjhVar2 = zzjh.UNINITIALIZED;
            if (zzjhVar == zzjhVar2) {
                zzjh zzjhVar3 = (zzjh) b2VarJ.f21486a.get(zzje$zza.ANALYTICS_STORAGE);
                if (zzjhVar3 == null) {
                    zzjhVar3 = zzjhVar2;
                }
                if (zzjhVar3 == zzjhVar2) {
                    zzj().f21839k.c("Ignoring empty consent settings");
                    return;
                }
            }
        }
        synchronized (this.f21623h) {
            try {
                b2Var2 = this.f21629n;
                z11 = false;
                if (b2.h(i10, b2Var2.f21487b)) {
                    zL = b2Var.l(this.f21629n, (zzje$zza[]) b2VarJ.f21486a.keySet().toArray(new zzje$zza[0]));
                    zzje$zza zzje_zza = zzje$zza.ANALYTICS_STORAGE;
                    if (b2Var.i(zzje_zza) && !this.f21629n.i(zzje_zza)) {
                        z11 = true;
                    }
                    b2VarJ = b2Var.j(this.f21629n);
                    this.f21629n = b2VarJ;
                    z12 = z11;
                    z11 = true;
                } else {
                    zL = false;
                    z12 = false;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        if (!z11) {
            zzj().f21840l.b(b2VarJ, "Ignoring lower-priority consent settings, proposed settings");
            return;
        }
        long andIncrement = this.f21630o.getAndIncrement();
        if (zL) {
            c0(null);
            p2 p2Var = new p2(this, b2VarJ, j10, andIncrement, z12, b2Var2);
            if (!z10) {
                zzl().J(p2Var);
                return;
            } else {
                w();
                p2Var.run();
                return;
            }
        }
        r2 r2Var = new r2(this, b2VarJ, andIncrement, z12, b2Var2);
        if (z10) {
            w();
            r2Var.run();
        } else if (i10 == 30 || i10 == -10) {
            zzl().J(r2Var);
        } else {
            zzl().I(r2Var);
        }
    }

    public final void N(Boolean bool, boolean z10) {
        w();
        D();
        zzj().f21841m.b(bool, "Setting app measurement enabled (FE)");
        y0 y0VarU = u();
        y0VarU.w();
        SharedPreferences.Editor editorEdit = y0VarU.F().edit();
        if (bool != null) {
            editorEdit.putBoolean("measurement_enabled", bool.booleanValue());
        } else {
            editorEdit.remove("measurement_enabled");
        }
        editorEdit.apply();
        if (z10) {
            y0 y0VarU2 = u();
            y0VarU2.w();
            SharedPreferences.Editor editorEdit2 = y0VarU2.F().edit();
            if (bool != null) {
                editorEdit2.putBoolean("measurement_enabled_from_api", bool.booleanValue());
            } else {
                editorEdit2.remove("measurement_enabled_from_api");
            }
            editorEdit2.apply();
        }
        m1 m1Var = (m1) this.f31277a;
        j1 j1Var = m1Var.f21758j;
        m1.d(j1Var);
        j1Var.w();
        if (m1Var.D || !(bool == null || bool.booleanValue())) {
            a0();
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r15v16 */
    /* JADX WARN: Type inference failed for: r15v3 */
    /* JADX WARN: Type inference failed for: r15v4 */
    /* JADX WARN: Type inference failed for: r31v2 */
    public final void O(String str, String str2, long j10, Bundle bundle, boolean z10, boolean z11, boolean z12, String str3) {
        boolean z13;
        boolean zB;
        ArrayList arrayList;
        long j11;
        Bundle[] bundleArr;
        String str4;
        String str5;
        boolean zH;
        Bundle[] bundleArr2;
        com.google.android.gms.common.internal.w.checkNotEmpty(str);
        com.google.android.gms.common.internal.w.checkNotNull(bundle);
        w();
        D();
        Object obj = this.f31277a;
        m1 m1Var = (m1) obj;
        if (!m1Var.e()) {
            zzj().f21841m.c("Event not sent since app measurement is disabled");
            return;
        }
        List list = x().f21725i;
        if (list != null && !list.contains(str2)) {
            zzj().f21841m.a(str2, str, "Dropping non-safelisted event. event name, origin");
            return;
        }
        if (!this.f21621f) {
            this.f21621f = true;
            try {
                try {
                    (!((m1) obj).f21753e ? Class.forName("com.google.android.gms.tagmanager.TagManagerService", true, zza().getClassLoader()) : Class.forName("com.google.android.gms.tagmanager.TagManagerService")).getDeclaredMethod("initialize", Context.class).invoke(null, zza());
                } catch (Exception e10) {
                    zzj().f21837i.b(e10, "Failed to invoke Tag Manager's initialize() method");
                }
            } catch (ClassNotFoundException unused) {
                zzj().f21840l.c("Tag Manager is not found and thus will not be used");
            }
        }
        if ("_cmp".equals(str2) && bundle.containsKey("gclid")) {
            String string = bundle.getString("gclid");
            ((i8.d) zzb()).getClass();
            z13 = false;
            S("auto", "_lgclid", string, System.currentTimeMillis());
        } else {
            z13 = false;
        }
        if (z10 && (!n4.f21793j[z13 ? 1 : 0].equals(str2))) {
            v().O(bundle, u().f22008z.o());
        }
        h hVar = this.f21637v;
        if (!z12 && !"_iap".equals(str2)) {
            n4 n4Var = m1Var.f21760l;
            m1.c(n4Var);
            char c6 = 2;
            c6 = 2;
            if (n4Var.v0("event", str2)) {
                if (!n4Var.k0("event", c2.f21498a, c2.f21499b, str2)) {
                    c6 = '\r';
                } else if (n4Var.c0(40, "event", str2)) {
                    c6 = z13 ? 1 : 0;
                }
            }
            if (c6 != 0) {
                zzj().f21836h.b(t().c(str2), "Invalid public event name. Event will not be logged (FE)");
                m1Var.m();
                String strL = n4.L(40, str2, true);
                ?? length = z13;
                if (str2 != null) {
                    length = str2.length();
                }
                m1Var.m();
                n4.Y(hVar, null, c6 == true ? 1 : 0, "_ev", strL, length == true ? 1 : 0);
                return;
            }
        }
        x2 x2VarG = A().G(z13);
        if (x2VarG != null && !bundle.containsKey("_sc")) {
            x2VarG.f21978b = true;
        }
        n4.X(x2VarG, bundle, (!z10 || z12) ? z13 ? 1 : 0 : true);
        boolean zEquals = "am".equals(str);
        boolean zA0 = n4.A0(str2);
        if (z10 && this.f21619d != null && !zA0 && !zEquals) {
            zzj().f21841m.a(t().c(str2), t().a(bundle), "Passing event to registered event handler (FE)");
            com.google.android.gms.common.internal.w.checkNotNull(this.f21619d);
            a aVar = this.f21619d;
            aVar.getClass();
            try {
                ((com.google.android.gms.internal.measurement.y0) aVar.f21466b).C1(str, str2, bundle, j10);
                return;
            } catch (RemoteException e11) {
                m1 m1Var2 = ((AppMeasurementDynamiteService) aVar.f21467c).f21463a;
                if (m1Var2 != null) {
                    p0 p0Var = m1Var2.f21757i;
                    m1.d(p0Var);
                    p0Var.f21837i.b(e11, "Event interceptor threw exception");
                    return;
                }
                return;
            }
        }
        if (m1Var.f()) {
            int iB = v().B(str2);
            if (iB != 0) {
                zzj().f21836h.b(t().c(str2), "Invalid event name. Event will not be logged (FE)");
                v();
                String strL2 = n4.L(40, str2, true);
                int length2 = str2 != null ? str2.length() : z13 ? 1 : 0;
                m1Var.m();
                n4.Y(hVar, str3, iB, "_ev", strL2, length2 == true ? 1 : 0);
                return;
            }
            Bundle bundleH = v().H(str2, bundle, Collections.unmodifiableList(Arrays.asList("_o", "_sn", "_sc", "_si")), z12);
            com.google.android.gms.common.internal.w.checkNotNull(bundleH);
            if (A().G(z13) != null && "_ae".equals(str2)) {
                xyz.stream.utils.k0 k0Var = C().f21780f;
                ((i8.d) ((m3) k0Var.f36160d).zzb()).getClass();
                long jElapsedRealtime = SystemClock.elapsedRealtime();
                long j12 = jElapsedRealtime - k0Var.f36159c;
                k0Var.f36159c = jElapsedRealtime;
                if (j12 > 0) {
                    v().N(bundleH, j12);
                }
            }
            if (!"auto".equals(str) && "_ssr".equals(str2)) {
                n4 n4VarV = v();
                String string2 = bundleH.getString("_ffr");
                if (i8.h.isEmptyOrWhitespace(string2)) {
                    string2 = null;
                } else if (string2 != null) {
                    string2 = string2.trim();
                }
                if (Objects.equals(string2, n4VarV.u().f22005w.f())) {
                    n4VarV.zzj().f21841m.c("Not logging duplicate session_start_with_rollout event");
                    return;
                }
                n4VarV.u().f22005w.g(string2);
            } else if ("_ae".equals(str2)) {
                String strF = v().u().f22005w.f();
                if (!TextUtils.isEmpty(strF)) {
                    bundleH.putString("_ffr", strF);
                }
            }
            ArrayList arrayList2 = new ArrayList();
            arrayList2.add(bundleH);
            if (s().J(null, z.N0)) {
                m3 m3VarC = C();
                m3VarC.w();
                zB = m3VarC.f21778d;
            } else {
                zB = u().f22002t.b();
            }
            if (u().f21999q.a() > 0 && u().B(j10) && zB) {
                zzj().f21842n.c("Current session is expired, remove the session number, ID, and engagement time");
                ((i8.d) zzb()).getClass();
                arrayList = arrayList2;
                j11 = 0;
                bundleArr = null;
                S("auto", "_sid", null, System.currentTimeMillis());
                ((i8.d) zzb()).getClass();
                S("auto", "_sno", null, System.currentTimeMillis());
                ((i8.d) zzb()).getClass();
                S("auto", "_se", null, System.currentTimeMillis());
                u().f22000r.b(0L);
            } else {
                arrayList = arrayList2;
                j11 = 0;
                bundleArr = null;
            }
            if (bundleH.getLong("extend_session", j11) == 1) {
                zzj().f21842n.c("EXTEND_SESSION param attached: initiate a new session or extend the current active session");
                m3 m3Var = m1Var.f21759k;
                m1.b(m3Var);
                m3Var.f21779e.a(j10, true);
            }
            ArrayList arrayList3 = new ArrayList(bundleH.keySet());
            Collections.sort(arrayList3);
            int size = arrayList3.size();
            int i10 = z13 ? 1 : 0;
            while (i10 < size) {
                Object obj2 = arrayList3.get(i10);
                i10++;
                String str6 = (String) obj2;
                if (str6 != null) {
                    v();
                    Object obj3 = bundleH.get(str6);
                    if (obj3 instanceof Bundle) {
                        Bundle[] bundleArr3 = new Bundle[1];
                        bundleArr3[z13 ? 1 : 0] = (Bundle) obj3;
                        bundleArr2 = bundleArr3;
                    } else if (obj3 instanceof Parcelable[]) {
                        Parcelable[] parcelableArr = (Parcelable[]) obj3;
                        bundleArr2 = (Bundle[]) Arrays.copyOf(parcelableArr, parcelableArr.length, Bundle[].class);
                    } else if (obj3 instanceof ArrayList) {
                        ArrayList arrayList4 = (ArrayList) obj3;
                        bundleArr2 = (Bundle[]) arrayList4.toArray(new Bundle[arrayList4.size()]);
                    } else {
                        bundleArr2 = bundleArr;
                    }
                    if (bundleArr2 != null) {
                        bundleH.putParcelableArray(str6, bundleArr2);
                    }
                }
            }
            int i11 = z13 ? 1 : 0;
            while (i11 < arrayList.size()) {
                ArrayList arrayList5 = arrayList;
                Bundle bundleG = (Bundle) arrayList5.get(i11);
                if (i11 != 0) {
                    str5 = "_ep";
                    str4 = str;
                } else {
                    str4 = str;
                    str5 = str2;
                }
                bundleG.putString("_o", str4);
                if (z11) {
                    bundleG = v().G(bundleG);
                }
                Bundle bundle2 = bundleG;
                y yVar = new y(str5, new x(bundle2), str, j10);
                a3 a3VarB = B();
                a3VarB.getClass();
                com.google.android.gms.common.internal.w.checkNotNull(yVar);
                a3VarB.w();
                a3VarB.D();
                k0 k0VarY = a3VarB.y();
                k0VarY.getClass();
                Parcel parcelObtain = Parcel.obtain();
                yVar.writeToParcel(parcelObtain, z13 ? 1 : 0);
                byte[] bArrMarshall = parcelObtain.marshall();
                parcelObtain.recycle();
                if (bArrMarshall.length > 131072) {
                    k0VarY.zzj().f21835g.c("Event is too long for local database. Sending event directly to service");
                    zH = z13 ? 1 : 0;
                } else {
                    zH = k0VarY.H(z13 ? 1 : 0, bArrMarshall);
                }
                a3VarB.I(new e3(a3VarB, a3VarB.S(true), zH, yVar, str3));
                if (!zEquals) {
                    Iterator it = this.f21620e.iterator();
                    while (it.hasNext()) {
                        ((d2) it.next()).a(str, str2, new Bundle(bundle2), j10);
                    }
                }
                i11++;
                arrayList = arrayList5;
            }
            if (A().G(z13) == null || !"_ae".equals(str2)) {
                return;
            }
            m3 m3VarC2 = C();
            ((i8.d) zzb()).getClass();
            m3VarC2.f21780f.d(SystemClock.elapsedRealtime(), true, true);
        }
    }

    public final void P(String str, String str2, Bundle bundle) {
        ((i8.d) zzb()).getClass();
        long jCurrentTimeMillis = System.currentTimeMillis();
        com.google.android.gms.common.internal.w.checkNotEmpty(str);
        Bundle bundle2 = new Bundle();
        bundle2.putString("name", str);
        bundle2.putLong("creation_timestamp", jCurrentTimeMillis);
        if (str2 != null) {
            bundle2.putString("expired_event_name", str2);
            bundle2.putBundle("expired_event_params", bundle);
        }
        zzl().I(new i2(this, bundle2, 1));
    }

    public final void Q(String str, String str2, Bundle bundle, long j10) {
        w();
        O(str, str2, j10, bundle, true, this.f21619d == null || n4.A0(str2), true, null);
    }

    public final void R(String str, String str2, Bundle bundle, boolean z10, boolean z11, long j10) {
        String strH;
        String str3 = str == null ? "app" : str;
        Bundle bundle2 = bundle == null ? new Bundle() : bundle;
        if (!Objects.equals(str2, "screen_view")) {
            boolean z12 = !z11 || this.f21619d == null || n4.A0(str2);
            Bundle bundle3 = new Bundle(bundle2);
            for (String str4 : bundle3.keySet()) {
                Object obj = bundle3.get(str4);
                if (obj instanceof Bundle) {
                    bundle3.putBundle(str4, new Bundle((Bundle) obj));
                } else if (obj instanceof Parcelable[]) {
                    Parcelable[] parcelableArr = (Parcelable[]) obj;
                    for (int i10 = 0; i10 < parcelableArr.length; i10++) {
                        if (parcelableArr[i10] instanceof Bundle) {
                            parcelableArr[i10] = new Bundle((Bundle) parcelableArr[i10]);
                        }
                    }
                } else if (obj instanceof List) {
                    List list = (List) obj;
                    for (int i11 = 0; i11 < list.size(); i11++) {
                        Object obj2 = list.get(i11);
                        if (obj2 instanceof Bundle) {
                            list.set(i11, new Bundle((Bundle) obj2));
                        }
                    }
                }
            }
            zzl().I(new o2(this, str3, str2, j10, bundle3, z11, z12, z10));
            return;
        }
        w2 w2VarA = A();
        synchronized (w2VarA.f21968l) {
            try {
                if (!w2VarA.f21967k) {
                    w2VarA.zzj().f21839k.c("Cannot log screen view event when the app is in the background.");
                    return;
                }
                String string = bundle2.getString("screen_name");
                if (string != null && (string.length() <= 0 || string.length() > w2VarA.s().y(null, false))) {
                    w2VarA.zzj().f21839k.b(Integer.valueOf(string.length()), "Invalid screen name length for screen view. Length");
                    return;
                }
                String string2 = bundle2.getString("screen_class");
                if (string2 != null && (string2.length() <= 0 || string2.length() > w2VarA.s().y(null, false))) {
                    w2VarA.zzj().f21839k.b(Integer.valueOf(string2.length()), "Invalid screen class length for screen view. Length");
                    return;
                }
                if (string2 == null) {
                    Activity activity = w2VarA.f21963g;
                    strH = activity != null ? w2VarA.H(activity.getClass()) : "Activity";
                } else {
                    strH = string2;
                }
                x2 x2Var = w2VarA.f21959c;
                if (w2VarA.f21964h && x2Var != null) {
                    w2VarA.f21964h = false;
                    boolean zEquals = Objects.equals(x2Var.zzb, strH);
                    boolean zEquals2 = Objects.equals(x2Var.zza, string);
                    if (zEquals && zEquals2) {
                        w2VarA.zzj().f21839k.c("Ignoring call to log screen view event with duplicate parameters.");
                        return;
                    }
                }
                w2VarA.zzj().f21842n.a(string == null ? "null" : string, strH == null ? "null" : strH, "Logging screen view with name, class");
                x2 x2Var2 = w2VarA.f21959c == null ? w2VarA.f21960d : w2VarA.f21959c;
                x2 x2Var3 = new x2(string, strH, w2VarA.v().F0(), true, j10);
                w2VarA.f21959c = x2Var3;
                w2VarA.f21960d = x2Var2;
                w2VarA.f21965i = x2Var3;
                ((i8.d) w2VarA.zzb()).getClass();
                w2VarA.zzl().I(new w1(w2VarA, bundle2, x2Var3, x2Var2, SystemClock.elapsedRealtime(), 2));
            } finally {
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0051  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void S(String str, String str2, Object obj, long j10) {
        com.google.android.gms.common.internal.w.checkNotEmpty(str);
        com.google.android.gms.common.internal.w.checkNotEmpty(str2);
        w();
        D();
        if ("allow_personalized_ads".equals(str2)) {
            if (obj instanceof String) {
                String str3 = (String) obj;
                if (TextUtils.isEmpty(str3)) {
                    if (obj == null) {
                        u().f21996n.g("unset");
                    }
                    zzj().f21842n.a("non_personalized_ads(_npa)", obj, "Setting user property(FE)");
                } else {
                    Long lValueOf = Long.valueOf("false".equals(str3.toLowerCase(Locale.ENGLISH)) ? 1L : 0L);
                    u().f21996n.g(lValueOf.longValue() == 1 ? "true" : "false");
                    obj = lValueOf;
                }
                str2 = "_npa";
                zzj().f21842n.a("non_personalized_ads(_npa)", obj, "Setting user property(FE)");
            }
        }
        String str4 = str2;
        Object obj2 = obj;
        Object obj3 = this.f31277a;
        if (!((m1) obj3).e()) {
            zzj().f21842n.c("User property not set since app measurement is disabled");
            return;
        }
        if (((m1) obj3).f()) {
            i4 i4Var = new i4(str4, j10, obj2, str);
            a3 a3VarB = B();
            a3VarB.w();
            a3VarB.D();
            k0 k0VarY = a3VarB.y();
            k0VarY.getClass();
            Parcel parcelObtain = Parcel.obtain();
            boolean zH = false;
            i4Var.writeToParcel(parcelObtain, 0);
            byte[] bArrMarshall = parcelObtain.marshall();
            parcelObtain.recycle();
            if (bArrMarshall.length > 131072) {
                k0VarY.zzj().f21835g.c("User property too long for local database. Sending directly to service");
            } else {
                zH = k0VarY.H(1, bArrMarshall);
            }
            a3VarB.I(new c3(a3VarB, a3VarB.S(true), zH, i4Var));
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x0042  */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0063  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void T(String str, String str2, Object obj, boolean z10, long j10) {
        int i10;
        int iO0;
        int length;
        String str3 = str == null ? "app" : str;
        if (!z10) {
            n4 n4VarV = v();
            i10 = 6;
            if (n4VarV.v0("user property", str2)) {
                if (!n4VarV.k0("user property", c2.f21502e, null, str2)) {
                    iO0 = 15;
                } else if (n4VarV.c0(24, "user property", str2)) {
                    i10 = 0;
                }
            }
            h hVar = this.f21637v;
            Object obj2 = this.f31277a;
            if (i10 == 0) {
                v();
                String strL = n4.L(24, str2, true);
                length = str2 != null ? str2.length() : 0;
                ((m1) obj2).m();
                n4.Y(hVar, null, i10, "_ev", strL, length);
                return;
            }
            if (obj == null) {
                zzl().I(new w1(this, str3, str2, null, j10, 1));
                return;
            }
            int iA = v().A(obj, str2);
            if (iA == 0) {
                Object objU0 = v().u0(obj, str2);
                if (objU0 != null) {
                    zzl().I(new w1(this, str3, str2, objU0, j10, 1));
                    return;
                }
                return;
            }
            v();
            String strL2 = n4.L(24, str2, true);
            length = ((obj instanceof String) || (obj instanceof CharSequence)) ? String.valueOf(obj).length() : 0;
            ((m1) obj2).m();
            n4.Y(hVar, null, iA, "_ev", strL2, length);
            return;
        }
        iO0 = v().o0(str2);
        i10 = iO0;
        h hVar2 = this.f21637v;
        Object obj22 = this.f31277a;
        if (i10 == 0) {
        }
    }

    public final PriorityQueue U() {
        if (this.f21627l == null) {
            this.f21627l = new PriorityQueue(Comparator.comparing(new f2(), new d0.g(11)));
        }
        return this.f21627l;
    }

    public final void V() {
        w();
        D();
        Object obj = this.f31277a;
        if (((m1) obj).f()) {
            Boolean boolH = s().H("google_analytics_deferred_deep_link_enabled");
            int i10 = 1;
            if (boolH != null && boolH.booleanValue()) {
                zzj().f21841m.c("Deferred Deep Link feature enabled.");
                zzl().I(new q1(this, i10));
            }
            a3 a3VarB = B();
            a3VarB.w();
            a3VarB.D();
            d4 d4VarS = a3VarB.S(true);
            a3VarB.y().H(3, new byte[0]);
            a3VarB.I(new d3(a3VarB, d4VarS, i10));
            this.f21633r = false;
            y0 y0VarU = u();
            y0VarU.w();
            String string = y0VarU.F().getString("previous_os_version", null);
            ((m1) y0VarU.f31277a).i().x();
            String str = Build.VERSION.RELEASE;
            if (!TextUtils.isEmpty(str) && !str.equals(string)) {
                SharedPreferences.Editor editorEdit = y0VarU.F().edit();
                editorEdit.putString("previous_os_version", str);
                editorEdit.apply();
            }
            if (TextUtils.isEmpty(string)) {
                return;
            }
            ((m1) obj).i().x();
            if (string.equals(str)) {
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putString("_po", string);
            d0("auto", "_ou", bundle);
        }
    }

    public final void W() {
        if (!(zza().getApplicationContext() instanceof Application) || this.f21618c == null) {
            return;
        }
        ((Application) zza().getApplicationContext()).unregisterActivityLifecycleCallbacks(this.f21618c);
    }

    public final void X() {
        z9.a();
        if (s().J(null, z.H0)) {
            if (zzl().K()) {
                zzj().f21834f.c("Cannot get trigger URIs from analytics worker thread");
                return;
            }
            if (l8.g.a()) {
                zzj().f21834f.c("Cannot get trigger URIs from main thread");
                return;
            }
            D();
            zzj().f21842n.c("Getting trigger URIs (FE)");
            AtomicReference atomicReference = new AtomicReference();
            zzl().zza(atomicReference, 5000L, "get trigger URIs", new h2(this, atomicReference, 0));
            List list = (List) atomicReference.get();
            if (list == null) {
                zzj().f21834f.c("Timed out waiting for get trigger URIs");
            } else {
                zzl().I(new s1(this, (Object) list, 3));
            }
        }
    }

    public final void Y() {
        String string;
        int i10;
        int i11;
        int i12;
        String string2;
        int i13;
        int i14;
        int i15;
        Bundle bundle;
        int i16;
        String str;
        w();
        zzj().f21841m.c("Handle tcf update.");
        SharedPreferences sharedPreferencesE = u().E();
        HashMap map = new HashMap();
        try {
            string = sharedPreferencesE.getString("IABTCF_VendorConsents", "\u0000");
        } catch (ClassCastException unused) {
            string = "\u0000";
        }
        if (!"\u0000".equals(string) && string.length() > 754) {
            map.put("GoogleConsent", String.valueOf(string.charAt(754)));
        }
        try {
            i10 = sharedPreferencesE.getInt("IABTCF_gdprApplies", -1);
        } catch (ClassCastException unused2) {
            i10 = -1;
        }
        if (i10 != -1) {
            map.put("gdprApplies", String.valueOf(i10));
        }
        try {
            i11 = sharedPreferencesE.getInt("IABTCF_EnableAdvertiserConsentMode", -1);
        } catch (ClassCastException unused3) {
            i11 = -1;
        }
        if (i11 != -1) {
            map.put("EnableAdvertiserConsentMode", String.valueOf(i11));
        }
        try {
            i12 = sharedPreferencesE.getInt("IABTCF_PolicyVersion", -1);
        } catch (ClassCastException unused4) {
            i12 = -1;
        }
        if (i12 != -1) {
            map.put("PolicyVersion", String.valueOf(i12));
        }
        try {
            string2 = sharedPreferencesE.getString("IABTCF_PurposeConsents", "\u0000");
        } catch (ClassCastException unused5) {
            string2 = "\u0000";
        }
        if (!"\u0000".equals(string2)) {
            map.put("PurposeConsents", string2);
        }
        try {
            i13 = sharedPreferencesE.getInt("IABTCF_CmpSdkID", -1);
        } catch (ClassCastException unused6) {
            i13 = -1;
        }
        if (i13 != -1) {
            map.put("CmpSdkID", String.valueOf(i13));
        }
        s3 s3Var = new s3(map);
        zzj().f21842n.b(s3Var, "Tcf preferences read");
        y0 y0VarU = u();
        y0VarU.w();
        String string3 = y0VarU.F().getString("stored_tcf_param", "");
        String strA = s3Var.a();
        if (strA.equals(string3)) {
            return;
        }
        SharedPreferences.Editor editorEdit = y0VarU.F().edit();
        editorEdit.putString("stored_tcf_param", strA);
        editorEdit.apply();
        HashMap map2 = s3Var.f21901a;
        if ("1".equals(map2.get("GoogleConsent")) && "1".equals(map2.get("gdprApplies")) && "1".equals(map2.get("EnableAdvertiserConsentMode"))) {
            int iB = s3Var.b();
            if (iB < 0) {
                bundle = Bundle.EMPTY;
            } else {
                String str2 = (String) map2.get("PurposeConsents");
                if (TextUtils.isEmpty(str2)) {
                    bundle = Bundle.EMPTY;
                } else {
                    Bundle bundle2 = new Bundle();
                    if (str2.length() > 0) {
                        bundle2.putString(zzje$zza.AD_STORAGE.zze, str2.charAt(0) == '1' ? "granted" : "denied");
                    }
                    if (str2.length() > 3) {
                        str = "denied";
                        i15 = 2;
                        bundle2.putString(zzje$zza.AD_PERSONALIZATION.zze, (str2.charAt(2) == '1' && str2.charAt(3) == '1') ? "granted" : str);
                    } else {
                        str = "denied";
                        i15 = 2;
                    }
                    if (str2.length() <= 6 || iB < 4) {
                        i14 = 0;
                    } else {
                        i14 = 0;
                        bundle2.putString(zzje$zza.AD_USER_DATA.zze, (str2.charAt(0) == '1' && str2.charAt(6) == '1') ? "granted" : str);
                    }
                    bundle = bundle2;
                }
            }
            i14 = 0;
            i15 = 2;
        } else {
            i14 = 0;
            i15 = 2;
            bundle = Bundle.EMPTY;
        }
        zzj().f21842n.b(bundle, "Consent generated from Tcf");
        if (bundle != Bundle.EMPTY) {
            ((i8.d) zzb()).getClass();
            H(bundle, -30, System.currentTimeMillis());
        }
        Bundle bundle3 = new Bundle();
        StringBuilder sb = new StringBuilder("1");
        try {
            String str3 = (String) map2.get("CmpSdkID");
            i16 = !TextUtils.isEmpty(str3) ? Integer.parseInt(str3) : -1;
        } catch (NumberFormatException unused7) {
            i16 = -1;
        }
        if (i16 < 0 || i16 > 4095) {
            sb.append("00");
        } else {
            sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".charAt((i16 >> 6) & 63));
            sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".charAt(i16 & 63));
        }
        int iB2 = s3Var.b();
        if (iB2 < 0 || iB2 > 63) {
            sb.append(SessionDescription.SUPPORTED_SDP_VERSION);
        } else {
            sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".charAt(iB2));
        }
        if ("1".equals(map2.get("gdprApplies"))) {
            i14 = i15;
        }
        int i17 = i14 | 4;
        if ("1".equals(map2.get("EnableAdvertiserConsentMode"))) {
            i17 = i14 | 12;
        }
        sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".charAt(i17));
        bundle3.putString("_tcfd", sb.toString());
        d0("auto", "_tcf", bundle3);
    }

    public final void Z() {
        t3 t3Var;
        w();
        this.f21628m = false;
        if (U().isEmpty() || this.f21624i || (t3Var = (t3) U().poll()) == null) {
            return;
        }
        n4 n4VarV = v();
        if (n4VarV.f21797f == null) {
            n4VarV.f21797f = androidx.privacysandbox.ads.adservices.java.measurement.a.a(n4VarV.zza());
        }
        MeasurementManagerFutures$Api33Ext5JavaImpl measurementManagerFutures$Api33Ext5JavaImpl = n4VarV.f21797f;
        if (measurementManagerFutures$Api33Ext5JavaImpl == null) {
            return;
        }
        this.f21624i = true;
        r0 r0Var = zzj().f21842n;
        String str = t3Var.f21911a;
        r0Var.b(str, "Registering trigger URI");
        com.google.common.util.concurrent.w wVarE = measurementManagerFutures$Api33Ext5JavaImpl.e(Uri.parse(str));
        if (wVarE == null) {
            this.f21624i = false;
            U().add(t3Var);
            return;
        }
        if (!s().J(null, z.M0)) {
            SparseArray sparseArrayG = u().G();
            sparseArrayG.put(t3Var.f21913c, Long.valueOf(t3Var.f21912b));
            u().A(sparseArrayG);
        }
        com.google.common.util.concurrent.c0.a(wVarE, new a(this, t3Var, 2), new l2(this));
    }

    public final void a0() {
        w();
        String strF = u().f21996n.f();
        if (strF != null) {
            if ("unset".equals(strF)) {
                ((i8.d) zzb()).getClass();
                S("app", "_npa", null, System.currentTimeMillis());
            } else {
                Long lValueOf = Long.valueOf("true".equals(strF) ? 1L : 0L);
                ((i8.d) zzb()).getClass();
                S("app", "_npa", lValueOf, System.currentTimeMillis());
            }
        }
        if (((m1) this.f31277a).e() && this.f21633r) {
            zzj().f21841m.c("Recording app launch after enabling measurement for the first time (FE)");
            V();
            C().f21779e.f();
            zzl().I(new q1(this, 2));
            return;
        }
        zzj().f21841m.c("Updating Scion state (FE)");
        a3 a3VarB = B();
        a3VarB.w();
        a3VarB.D();
        a3VarB.I(new d3(a3VarB, a3VarB.S(true), 3));
    }

    public final void b0(Bundle bundle, long j10) {
        com.google.android.gms.common.internal.w.checkNotNull(bundle);
        Bundle bundle2 = new Bundle(bundle);
        if (!TextUtils.isEmpty(bundle2.getString("app_id"))) {
            zzj().f21837i.c("Package name should be null when calling setConditionalUserProperty");
        }
        bundle2.remove("app_id");
        com.google.android.gms.common.internal.w.checkNotNull(bundle2);
        c2.q(bundle2, "app_id", String.class, null);
        c2.q(bundle2, "origin", String.class, null);
        c2.q(bundle2, "name", String.class, null);
        c2.q(bundle2, "value", Object.class, null);
        c2.q(bundle2, "trigger_event_name", String.class, null);
        c2.q(bundle2, "trigger_timeout", Long.class, 0L);
        c2.q(bundle2, "timed_out_event_name", String.class, null);
        c2.q(bundle2, "timed_out_event_params", Bundle.class, null);
        c2.q(bundle2, "triggered_event_name", String.class, null);
        c2.q(bundle2, "triggered_event_params", Bundle.class, null);
        c2.q(bundle2, "time_to_live", Long.class, 0L);
        c2.q(bundle2, "expired_event_name", String.class, null);
        c2.q(bundle2, "expired_event_params", Bundle.class, null);
        com.google.android.gms.common.internal.w.checkNotEmpty(bundle2.getString("name"));
        com.google.android.gms.common.internal.w.checkNotEmpty(bundle2.getString("origin"));
        com.google.android.gms.common.internal.w.checkNotNull(bundle2.get("value"));
        bundle2.putLong("creation_timestamp", j10);
        String string = bundle2.getString("name");
        Object obj = bundle2.get("value");
        if (v().o0(string) != 0) {
            p0 p0VarZzj = zzj();
            p0VarZzj.f21834f.b(t().g(string), "Invalid conditional user property name");
            return;
        }
        if (v().A(obj, string) != 0) {
            p0 p0VarZzj2 = zzj();
            p0VarZzj2.f21834f.a(t().g(string), obj, "Invalid conditional user property value");
            return;
        }
        Object objU0 = v().u0(obj, string);
        if (objU0 == null) {
            p0 p0VarZzj3 = zzj();
            p0VarZzj3.f21834f.a(t().g(string), obj, "Unable to normalize conditional user property value");
            return;
        }
        c2.r(bundle2, objU0);
        long j11 = bundle2.getLong("trigger_timeout");
        if (!TextUtils.isEmpty(bundle2.getString("trigger_event_name")) && (j11 > 15552000000L || j11 < 1)) {
            p0 p0VarZzj4 = zzj();
            p0VarZzj4.f21834f.a(t().g(string), Long.valueOf(j11), "Invalid conditional user property timeout");
            return;
        }
        long j12 = bundle2.getLong("time_to_live");
        if (j12 <= 15552000000L && j12 >= 1) {
            zzl().I(new i2(this, bundle2, 2));
            return;
        }
        p0 p0VarZzj5 = zzj();
        p0VarZzj5.f21834f.a(t().g(string), Long.valueOf(j12), "Invalid conditional user property time to live");
    }

    public final void c0(String str) {
        this.f21622g.set(str);
    }

    public final void d0(String str, String str2, Bundle bundle) {
        w();
        ((i8.d) zzb()).getClass();
        Q(str, str2, bundle, System.currentTimeMillis());
    }
}
