package d3;

import N1.C0207h;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.SystemClock;
import android.text.TextUtils;
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.internal.measurement.C0673n1;
import com.google.android.gms.internal.measurement.InterfaceC0639g2;
import com.google.android.gms.internal.measurement.J3;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/* JADX INFO: renamed from: d3.n, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0805n extends K1 {

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final C0802m f12070t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final C0207h f12071u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public static final String[] f12065v = {"last_bundled_timestamp", "ALTER TABLE events ADD COLUMN last_bundled_timestamp INTEGER;", "last_bundled_day", "ALTER TABLE events ADD COLUMN last_bundled_day INTEGER;", "last_sampled_complex_event_id", "ALTER TABLE events ADD COLUMN last_sampled_complex_event_id INTEGER;", "last_sampling_rate", "ALTER TABLE events ADD COLUMN last_sampling_rate INTEGER;", "last_exempt_from_sampling", "ALTER TABLE events ADD COLUMN last_exempt_from_sampling INTEGER;", "current_session_count", "ALTER TABLE events ADD COLUMN current_session_count INTEGER;"};

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public static final String[] f12066w = {"associated_row_id", "ALTER TABLE upload_queue ADD COLUMN associated_row_id INTEGER;", "last_upload_timestamp", "ALTER TABLE upload_queue ADD COLUMN last_upload_timestamp INTEGER;"};

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public static final String[] f12067x = {"origin", "ALTER TABLE user_attributes ADD COLUMN origin TEXT;"};

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public static final String[] f12068y = {"app_version", "ALTER TABLE apps ADD COLUMN app_version TEXT;", "app_store", "ALTER TABLE apps ADD COLUMN app_store TEXT;", "gmp_version", "ALTER TABLE apps ADD COLUMN gmp_version INTEGER;", "dev_cert_hash", "ALTER TABLE apps ADD COLUMN dev_cert_hash INTEGER;", "measurement_enabled", "ALTER TABLE apps ADD COLUMN measurement_enabled INTEGER;", "last_bundle_start_timestamp", "ALTER TABLE apps ADD COLUMN last_bundle_start_timestamp INTEGER;", "day", "ALTER TABLE apps ADD COLUMN day INTEGER;", "daily_public_events_count", "ALTER TABLE apps ADD COLUMN daily_public_events_count INTEGER;", "daily_events_count", "ALTER TABLE apps ADD COLUMN daily_events_count INTEGER;", "daily_conversions_count", "ALTER TABLE apps ADD COLUMN daily_conversions_count INTEGER;", "remote_config", "ALTER TABLE apps ADD COLUMN remote_config BLOB;", "config_fetched_time", "ALTER TABLE apps ADD COLUMN config_fetched_time INTEGER;", "failed_config_fetch_time", "ALTER TABLE apps ADD COLUMN failed_config_fetch_time INTEGER;", "app_version_int", "ALTER TABLE apps ADD COLUMN app_version_int INTEGER;", "firebase_instance_id", "ALTER TABLE apps ADD COLUMN firebase_instance_id TEXT;", "daily_error_events_count", "ALTER TABLE apps ADD COLUMN daily_error_events_count INTEGER;", "daily_realtime_events_count", "ALTER TABLE apps ADD COLUMN daily_realtime_events_count INTEGER;", "health_monitor_sample", "ALTER TABLE apps ADD COLUMN health_monitor_sample TEXT;", "android_id", "ALTER TABLE apps ADD COLUMN android_id INTEGER;", "adid_reporting_enabled", "ALTER TABLE apps ADD COLUMN adid_reporting_enabled INTEGER;", "ssaid_reporting_enabled", "ALTER TABLE apps ADD COLUMN ssaid_reporting_enabled INTEGER;", "admob_app_id", "ALTER TABLE apps ADD COLUMN admob_app_id TEXT;", "linked_admob_app_id", "ALTER TABLE apps ADD COLUMN linked_admob_app_id TEXT;", "dynamite_version", "ALTER TABLE apps ADD COLUMN dynamite_version INTEGER;", "safelisted_events", "ALTER TABLE apps ADD COLUMN safelisted_events TEXT;", "ga_app_id", "ALTER TABLE apps ADD COLUMN ga_app_id TEXT;", "config_last_modified_time", "ALTER TABLE apps ADD COLUMN config_last_modified_time TEXT;", "e_tag", "ALTER TABLE apps ADD COLUMN e_tag TEXT;", "session_stitching_token", "ALTER TABLE apps ADD COLUMN session_stitching_token TEXT;", "sgtm_upload_enabled", "ALTER TABLE apps ADD COLUMN sgtm_upload_enabled INTEGER;", "target_os_version", "ALTER TABLE apps ADD COLUMN target_os_version INTEGER;", "session_stitching_token_hash", "ALTER TABLE apps ADD COLUMN session_stitching_token_hash INTEGER;", "ad_services_version", "ALTER TABLE apps ADD COLUMN ad_services_version INTEGER;", "unmatched_first_open_without_ad_id", "ALTER TABLE apps ADD COLUMN unmatched_first_open_without_ad_id INTEGER;", "npa_metadata_value", "ALTER TABLE apps ADD COLUMN npa_metadata_value INTEGER;", "attribution_eligibility_status", "ALTER TABLE apps ADD COLUMN attribution_eligibility_status INTEGER;", "sgtm_preview_key", "ALTER TABLE apps ADD COLUMN sgtm_preview_key TEXT;", "dma_consent_state", "ALTER TABLE apps ADD COLUMN dma_consent_state INTEGER;", "daily_realtime_dcu_count", "ALTER TABLE apps ADD COLUMN daily_realtime_dcu_count INTEGER;", "bundle_delivery_index", "ALTER TABLE apps ADD COLUMN bundle_delivery_index INTEGER;", "serialized_npa_metadata", "ALTER TABLE apps ADD COLUMN serialized_npa_metadata TEXT;", "unmatched_pfo", "ALTER TABLE apps ADD COLUMN unmatched_pfo INTEGER;", "unmatched_uwa", "ALTER TABLE apps ADD COLUMN unmatched_uwa INTEGER;", "ad_campaign_info", "ALTER TABLE apps ADD COLUMN ad_campaign_info BLOB;", "daily_registered_triggers_count", "ALTER TABLE apps ADD COLUMN daily_registered_triggers_count INTEGER;", "client_upload_eligibility", "ALTER TABLE apps ADD COLUMN client_upload_eligibility INTEGER;"};

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public static final String[] f12069z = {"realtime", "ALTER TABLE raw_events ADD COLUMN realtime INTEGER;"};

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public static final String[] f12060A = {"has_realtime", "ALTER TABLE queue ADD COLUMN has_realtime INTEGER;", "retry_count", "ALTER TABLE queue ADD COLUMN retry_count INTEGER;"};

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public static final String[] f12061B = {"session_scoped", "ALTER TABLE event_filters ADD COLUMN session_scoped BOOLEAN;"};

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public static final String[] f12062C = {"session_scoped", "ALTER TABLE property_filters ADD COLUMN session_scoped BOOLEAN;"};
    public static final String[] D = {"previous_install_count", "ALTER TABLE app2 ADD COLUMN previous_install_count INTEGER;"};

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public static final String[] f12063E = {"consent_source", "ALTER TABLE consent_settings ADD COLUMN consent_source INTEGER;", "dma_consent_settings", "ALTER TABLE consent_settings ADD COLUMN dma_consent_settings TEXT;", "storage_consent_at_bundling", "ALTER TABLE consent_settings ADD COLUMN storage_consent_at_bundling TEXT;"};

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public static final String[] f12064F = {"idempotent", "CREATE INDEX IF NOT EXISTS trigger_uris_index ON trigger_uris (app_id);"};

    public C0805n(Q1 q12) {
        super(q12);
        this.f12071u = new C0207h(((C0817r0) this.f5146q).D);
        ((C0817r0) this.f5146q).getClass();
        this.f12070t = new C0802m(this, ((C0817r0) this.f5146q).f12142q);
    }

    public static final String n0(ArrayList arrayList) {
        return arrayList.isEmpty() ? "" : I1.a.p(" AND (upload_type IN (", TextUtils.join(", ", arrayList), "))");
    }

    public static final void u0(ContentValues contentValues, Object obj) {
        P2.w.c("value");
        P2.w.f(obj);
        if (obj instanceof String) {
            contentValues.put("value", (String) obj);
        } else if (obj instanceof Long) {
            contentValues.put("value", (Long) obj);
        } else {
            if (!(obj instanceof Double)) {
                throw new IllegalArgumentException("Invalid value type");
            }
            contentValues.put("value", (Double) obj);
        }
    }

    public final SQLiteDatabase A0() {
        E();
        try {
            return this.f12070t.getWritableDatabase();
        } catch (SQLiteException e8) {
            Z z8 = ((C0817r0) this.f5146q).f12150y;
            C0817r0.k(z8);
            z8.f11878y.c(e8, "Error opening database");
            throw e8;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:128:0x0400  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final V B0(String str) {
        Cursor cursorQuery;
        Boolean boolValueOf;
        String string;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str);
        E();
        F();
        Cursor cursor = null;
        try {
            cursorQuery = A0().query("apps", new String[]{"app_instance_id", "gmp_app_id", "resettable_device_id_hash", "last_bundle_index", "last_bundle_start_timestamp", "last_bundle_end_timestamp", "app_version", "app_store", "gmp_version", "dev_cert_hash", "measurement_enabled", "day", "daily_public_events_count", "daily_events_count", "daily_conversions_count", "config_fetched_time", "failed_config_fetch_time", "app_version_int", "firebase_instance_id", "daily_error_events_count", "daily_realtime_events_count", "health_monitor_sample", "android_id", "adid_reporting_enabled", "admob_app_id", "dynamite_version", "safelisted_events", "ga_app_id", "session_stitching_token", "sgtm_upload_enabled", "target_os_version", "session_stitching_token_hash", "ad_services_version", "unmatched_first_open_without_ad_id", "npa_metadata_value", "attribution_eligibility_status", "sgtm_preview_key", "dma_consent_state", "daily_realtime_dcu_count", "bundle_delivery_index", "serialized_npa_metadata", "unmatched_pfo", "unmatched_uwa", "ad_campaign_info", "client_upload_eligibility"}, "app_id=?", new String[]{str}, null, null, null);
            try {
                try {
                    if (cursorQuery.moveToFirst()) {
                        Q1 q12 = this.f11473r;
                        V v7 = new V(q12.f11659B, str);
                        C0817r0 c0817r02 = v7.f11758a;
                        F0 f0N0 = q12.n0(str);
                        E0 e02 = E0.f11448s;
                        if (f0N0.k(e02)) {
                            v7.l(cursorQuery.getString(0));
                        }
                        boolean z8 = true;
                        v7.H(cursorQuery.getString(1));
                        if (q12.n0(str).k(E0.f11447r)) {
                            v7.P(cursorQuery.getString(2));
                        }
                        v7.M(cursorQuery.getLong(3));
                        v7.N(cursorQuery.getLong(4));
                        v7.L(cursorQuery.getLong(5));
                        v7.n(cursorQuery.getString(6));
                        v7.m(cursorQuery.getString(7));
                        v7.I(cursorQuery.getLong(8));
                        v7.C(cursorQuery.getLong(9));
                        v7.O(cursorQuery.isNull(10) || cursorQuery.getInt(10) != 0);
                        v7.B(cursorQuery.getLong(11));
                        v7.y(cursorQuery.getLong(12));
                        v7.x(cursorQuery.getLong(13));
                        v7.v(cursorQuery.getLong(14));
                        v7.u(cursorQuery.getLong(15));
                        v7.F(cursorQuery.getLong(16));
                        v7.o(cursorQuery.isNull(17) ? -2147483648L : cursorQuery.getInt(17));
                        v7.G(cursorQuery.getString(18));
                        v7.w(cursorQuery.getLong(19));
                        v7.A(cursorQuery.getLong(20));
                        v7.J(cursorQuery.getString(21));
                        boolean z9 = cursorQuery.isNull(23) || cursorQuery.getInt(23) != 0;
                        C0815q0 c0815q0 = c0817r02.f12151z;
                        C0817r0.k(c0815q0);
                        c0815q0.E();
                        v7.f11755R |= v7.f11772p != z9;
                        v7.f11772p = z9;
                        v7.k(cursorQuery.getString(24));
                        v7.E(cursorQuery.isNull(25) ? 0L : cursorQuery.getLong(25));
                        if (!cursorQuery.isNull(26)) {
                            v7.Q(Arrays.asList(cursorQuery.getString(26).split(",", -1)));
                        }
                        if (q12.n0(str).k(e02)) {
                            String string2 = cursorQuery.getString(28);
                            C0815q0 c0815q02 = c0817r02.f12151z;
                            C0817r0.k(c0815q02);
                            c0815q02.E();
                            v7.f11755R |= !Objects.equals(v7.f11777u, string2);
                            v7.f11777u = string2;
                        }
                        boolean z10 = (cursorQuery.isNull(29) || cursorQuery.getInt(29) == 0) ? false : true;
                        C0815q0 c0815q03 = c0817r02.f12151z;
                        C0817r0.k(c0815q03);
                        c0815q03.E();
                        v7.f11755R |= v7.f11778v != z10;
                        v7.f11778v = z10;
                        v7.K(cursorQuery.getLong(39));
                        String string3 = cursorQuery.getString(36);
                        C0815q0 c0815q04 = c0817r02.f12151z;
                        C0817r0.k(c0815q04);
                        c0815q04.E();
                        v7.f11755R |= v7.D != string3;
                        v7.D = string3;
                        v7.r(cursorQuery.getLong(30));
                        v7.q(cursorQuery.getLong(31));
                        J3.b();
                        if (c0817r0.f12148w.R(str, I.W0)) {
                            int i = cursorQuery.getInt(32);
                            C0815q0 c0815q05 = c0817r02.f12151z;
                            C0817r0.k(c0815q05);
                            c0815q05.E();
                            v7.f11755R |= v7.f11781y != i;
                            v7.f11781y = i;
                            v7.p(cursorQuery.getLong(35));
                        }
                        boolean z11 = (cursorQuery.isNull(33) || cursorQuery.getInt(33) == 0) ? false : true;
                        C0815q0 c0815q06 = c0817r02.f12151z;
                        C0817r0.k(c0815q06);
                        c0815q06.E();
                        v7.f11755R |= v7.f11782z != z11;
                        v7.f11782z = z11;
                        if (cursorQuery.isNull(34)) {
                            boolValueOf = null;
                        } else {
                            boolValueOf = Boolean.valueOf(cursorQuery.getInt(34) != 0);
                        }
                        C0815q0 c0815q07 = c0817r02.f12151z;
                        C0817r0.k(c0815q07);
                        c0815q07.E();
                        v7.f11755R |= !Objects.equals(v7.f11774r, boolValueOf);
                        v7.f11774r = boolValueOf;
                        v7.D(cursorQuery.getInt(37));
                        v7.z(cursorQuery.getInt(38));
                        if (cursorQuery.isNull(40)) {
                            string = "";
                        } else {
                            string = cursorQuery.getString(40);
                            P2.w.f(string);
                        }
                        C0815q0 c0815q08 = c0817r02.f12151z;
                        C0817r0.k(c0815q08);
                        c0815q08.E();
                        v7.f11755R |= v7.H != string;
                        v7.H = string;
                        if (!cursorQuery.isNull(41)) {
                            Long lValueOf = Long.valueOf(cursorQuery.getLong(41));
                            C0815q0 c0815q09 = c0817r02.f12151z;
                            C0817r0.k(c0815q09);
                            c0815q09.E();
                            v7.f11755R |= !Objects.equals(v7.f11740A, lValueOf);
                            v7.f11740A = lValueOf;
                        }
                        if (!cursorQuery.isNull(42)) {
                            Long lValueOf2 = Long.valueOf(cursorQuery.getLong(42));
                            C0815q0 c0815q010 = c0817r02.f12151z;
                            C0817r0.k(c0815q010);
                            c0815q010.E();
                            v7.f11755R |= !Objects.equals(v7.f11741B, lValueOf2);
                            v7.f11741B = lValueOf2;
                        }
                        byte[] blob = cursorQuery.getBlob(43);
                        C0815q0 c0815q011 = c0817r02.f12151z;
                        C0817r0.k(c0815q011);
                        c0815q011.E();
                        v7.f11755R |= v7.f11746I != blob;
                        v7.f11746I = blob;
                        if (c0817r0.f12148w.R(str, I.f11517Q0) && !cursorQuery.isNull(44)) {
                            int i5 = cursorQuery.getInt(44);
                            C0815q0 c0815q012 = c0817r02.f12151z;
                            C0817r0.k(c0815q012);
                            c0815q012.E();
                            boolean z12 = v7.f11755R;
                            if (v7.f11747J == i5) {
                                z8 = false;
                            }
                            v7.f11755R = z8 | z12;
                            v7.f11747J = i5;
                        }
                        C0815q0 c0815q013 = c0817r02.f12151z;
                        C0817r0.k(c0815q013);
                        c0815q013.E();
                        v7.f11755R = false;
                        if (cursorQuery.moveToNext()) {
                            Z z13 = c0817r0.f12150y;
                            C0817r0.k(z13);
                            z13.f11875v.c(Z.M(str), "Got multiple records for app, expected one. appId");
                        }
                        cursorQuery.close();
                        return v7;
                    }
                } catch (SQLiteException e8) {
                    e = e8;
                    Z z14 = c0817r0.f12150y;
                    C0817r0.k(z14);
                    z14.f11875v.d("Error querying app. appId", Z.M(str), e);
                }
            } catch (Throwable th) {
                th = th;
                cursor = cursorQuery;
                if (cursor != null) {
                    cursor.close();
                }
                throw th;
            }
        } catch (SQLiteException e9) {
            e = e9;
            cursorQuery = null;
        } catch (Throwable th2) {
            th = th2;
            if (cursor != null) {
            }
            throw th;
        }
        if (cursorQuery != null) {
            cursorQuery.close();
        }
        return null;
    }

    /* JADX WARN: Not initialized variable reg: 10, insn: 0x00f6: MOVE (r9 I:??[OBJECT, ARRAY]) = (r10 I:??[OBJECT, ARRAY]) (LINE:247), block:B:29:0x00f6 */
    /* JADX WARN: Removed duplicated region for block: B:36:0x0116  */
    /* JADX WARN: Removed duplicated region for block: B:39:0x011c  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final C0778e C0(String str, String str2) throws Throwable {
        String str3;
        Cursor cursorQuery;
        Cursor cursor;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str);
        P2.w.c(str2);
        E();
        F();
        Cursor cursor2 = null;
        try {
            try {
                cursorQuery = A0().query("conditional_properties", new String[]{"origin", "value", "active", "trigger_event_name", "trigger_timeout", "timed_out_event", "creation_timestamp", "triggered_event", "triggered_timestamp", "time_to_live", "expired_event"}, "app_id=? and name=?", new String[]{str, str2}, null, null, null);
                try {
                } catch (SQLiteException e8) {
                    e = e8;
                    str3 = str2;
                }
            } catch (Throwable th) {
                th = th;
                cursor2 = cursor;
                if (cursor2 != null) {
                    cursor2.close();
                }
                throw th;
            }
        } catch (SQLiteException e9) {
            e = e9;
            str3 = str2;
            cursorQuery = null;
        } catch (Throwable th2) {
            th = th2;
            if (cursor2 != null) {
            }
            throw th;
        }
        if (!cursorQuery.moveToFirst()) {
            if (cursorQuery != null) {
                cursorQuery.close();
            }
            return null;
        }
        String string = cursorQuery.getString(0);
        if (string == null) {
            string = "";
        }
        String str4 = string;
        Object objL0 = L0(cursorQuery, 1);
        boolean z8 = cursorQuery.getInt(2) != 0;
        String string2 = cursorQuery.getString(3);
        long j8 = cursorQuery.getLong(4);
        C0773c0 c0773c0 = this.f11473r.f11687w;
        Q1.L(c0773c0);
        byte[] blob = cursorQuery.getBlob(5);
        Parcelable.Creator<C0828v> creator = C0828v.CREATOR;
        C0828v c0828v = (C0828v) c0773c0.g0(blob, creator);
        long j9 = cursorQuery.getLong(6);
        Q1.L(c0773c0);
        C0828v c0828v2 = (C0828v) c0773c0.g0(cursorQuery.getBlob(7), creator);
        long j10 = cursorQuery.getLong(8);
        long j11 = cursorQuery.getLong(9);
        Q1.L(c0773c0);
        str3 = str2;
        try {
            C0778e c0778e = new C0778e(str, str4, new T1(j10, objL0, str3, str4), j9, z8, string2, c0828v, j8, c0828v2, j11, (C0828v) c0773c0.g0(cursorQuery.getBlob(10), creator));
            if (cursorQuery.moveToNext()) {
                Z z9 = c0817r0.f12150y;
                C0817r0.k(z9);
                z9.f11875v.d("Got multiple records for conditional property, expected one", Z.M(str), c0817r0.f12123C.f(str3));
            }
            cursorQuery.close();
            return c0778e;
        } catch (SQLiteException e10) {
            e = e10;
        }
        Z z10 = c0817r0.f12150y;
        C0817r0.k(z10);
        z10.f11875v.e("Error querying conditional property", Z.M(str), c0817r0.f12123C.f(str3), e);
        if (cursorQuery != null) {
        }
        return null;
    }

    /* JADX WARN: Not initialized variable reg: 3, insn: 0x006b: MOVE (r2 I:??[OBJECT, ARRAY]) = (r3 I:??[OBJECT, ARRAY]) (LINE:108), block:B:19:0x006b */
    /* JADX WARN: Removed duplicated region for block: B:29:0x008a  */
    /* JADX WARN: Removed duplicated region for block: B:35:? A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final com.google.android.material.datepicker.c D0(String str) {
        Throwable th;
        Cursor cursorQuery;
        Cursor cursor;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str);
        E();
        F();
        Cursor cursor2 = null;
        try {
            try {
                cursorQuery = A0().query("apps", new String[]{"remote_config", "config_last_modified_time", "e_tag"}, "app_id=?", new String[]{str}, null, null, null);
                try {
                    if (cursorQuery.moveToFirst()) {
                        byte[] blob = cursorQuery.getBlob(0);
                        String string = cursorQuery.getString(1);
                        String string2 = cursorQuery.getString(2);
                        if (cursorQuery.moveToNext()) {
                            Z z8 = c0817r0.f12150y;
                            C0817r0.k(z8);
                            z8.f11875v.c(Z.M(str), "Got multiple records for app config, expected one. appId");
                        }
                        if (blob != null) {
                            com.google.android.material.datepicker.c cVar = new com.google.android.material.datepicker.c(blob, string, string2, 1);
                            cursorQuery.close();
                            return cVar;
                        }
                    }
                } catch (SQLiteException e8) {
                    e = e8;
                    Z z9 = c0817r0.f12150y;
                    C0817r0.k(z9);
                    z9.f11875v.d("Error querying remote config. appId", Z.M(str), e);
                }
            } catch (Throwable th2) {
                th = th2;
                cursor2 = cursor;
                if (cursor2 != null) {
                    throw th;
                }
                cursor2.close();
                throw th;
            }
        } catch (SQLiteException e9) {
            e = e9;
            cursorQuery = null;
        } catch (Throwable th3) {
            th = th3;
            if (cursor2 != null) {
            }
        }
        if (cursorQuery != null) {
            cursorQuery.close();
        }
        return null;
    }

    public final C0793j E0(long j8, String str, boolean z8, boolean z9, boolean z10, boolean z11) {
        return F0(j8, str, 1L, false, false, z8, false, z9, z10, z11);
    }

    public final C0793j F0(long j8, String str, long j9, boolean z8, boolean z9, boolean z10, boolean z11, boolean z12, boolean z13, boolean z14) {
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str);
        E();
        F();
        String[] strArr = {str};
        C0793j c0793j = new C0793j();
        Cursor cursorQuery = null;
        try {
            try {
                SQLiteDatabase sQLiteDatabaseA0 = A0();
                cursorQuery = sQLiteDatabaseA0.query("apps", new String[]{"day", "daily_events_count", "daily_public_events_count", "daily_conversions_count", "daily_error_events_count", "daily_realtime_events_count", "daily_realtime_dcu_count", "daily_registered_triggers_count"}, "app_id=?", new String[]{str}, null, null, null);
                if (cursorQuery.moveToFirst()) {
                    if (cursorQuery.getLong(0) == j8) {
                        c0793j.f12009b = cursorQuery.getLong(1);
                        c0793j.f12008a = cursorQuery.getLong(2);
                        c0793j.f12010c = cursorQuery.getLong(3);
                        c0793j.f12011d = cursorQuery.getLong(4);
                        c0793j.f12012e = cursorQuery.getLong(5);
                        c0793j.f12013f = cursorQuery.getLong(6);
                        c0793j.f12014g = cursorQuery.getLong(7);
                    }
                    if (z8) {
                        c0793j.f12009b += j9;
                    }
                    if (z9) {
                        c0793j.f12008a += j9;
                    }
                    if (z10) {
                        c0793j.f12010c += j9;
                    }
                    if (z11) {
                        c0793j.f12011d += j9;
                    }
                    if (z12) {
                        c0793j.f12012e += j9;
                    }
                    if (z13) {
                        c0793j.f12013f += j9;
                    }
                    if (z14) {
                        c0793j.f12014g += j9;
                    }
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("day", Long.valueOf(j8));
                    contentValues.put("daily_public_events_count", Long.valueOf(c0793j.f12008a));
                    contentValues.put("daily_events_count", Long.valueOf(c0793j.f12009b));
                    contentValues.put("daily_conversions_count", Long.valueOf(c0793j.f12010c));
                    contentValues.put("daily_error_events_count", Long.valueOf(c0793j.f12011d));
                    contentValues.put("daily_realtime_events_count", Long.valueOf(c0793j.f12012e));
                    contentValues.put("daily_realtime_dcu_count", Long.valueOf(c0793j.f12013f));
                    contentValues.put("daily_registered_triggers_count", Long.valueOf(c0793j.f12014g));
                    sQLiteDatabaseA0.update("apps", contentValues, "app_id=?", strArr);
                } else {
                    Z z15 = c0817r0.f12150y;
                    C0817r0.k(z15);
                    z15.f11878y.c(Z.M(str), "Not updating daily counts, app is not known. appId");
                }
            } catch (SQLiteException e8) {
                Z z16 = c0817r0.f12150y;
                C0817r0.k(z16);
                z16.f11875v.d("Error updating daily counts. appId", Z.M(str), e8);
            }
            if (cursorQuery != null) {
                cursorQuery.close();
            }
            return c0793j;
        } catch (Throwable th) {
            if (0 != 0) {
                cursorQuery.close();
            }
            throw th;
        }
    }

    public final C0819s G0(String str, com.google.android.gms.internal.measurement.W0 w02, String str2) {
        C0819s c0819sG0 = g0("events", str, w02.t());
        if (c0819sG0 != null) {
            long j8 = c0819sG0.f12159e + 1;
            long j9 = c0819sG0.f12158d + 1;
            return new C0819s(c0819sG0.f12155a, c0819sG0.f12156b, c0819sG0.f12157c + 1, j9, j8, c0819sG0.f12160f, c0819sG0.f12161g, c0819sG0.f12162h, c0819sG0.i, c0819sG0.f12163j, c0819sG0.f12164k);
        }
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        Z z8 = c0817r0.f12150y;
        C0817r0.k(z8);
        z8.f11878y.d("Event aggregate wasn't created during raw event logging. appId, event", Z.M(str), c0817r0.f12123C.d(str2));
        return new C0819s(str, w02.t(), 1L, 1L, 1L, w02.q(), 0L, null, null, null, null);
    }

    public final F0 H0(String str) {
        P2.w.f(str);
        E();
        F();
        return F0.e(100, j0("select storage_consent_at_bundling from consent_settings where app_id=? limit 1;", new String[]{str}));
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:23:0x003f  */
    /* JADX WARN: Type inference failed for: r1v0 */
    /* JADX WARN: Type inference failed for: r1v1, types: [android.database.Cursor] */
    /* JADX WARN: Type inference failed for: r1v3 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String I() throws Throwable {
        SQLiteException e8;
        Cursor cursorRawQuery;
        SQLiteDatabase sQLiteDatabaseA0 = A0();
        ?? r12 = 0;
        try {
            try {
                cursorRawQuery = sQLiteDatabaseA0.rawQuery("select app_id from queue order by has_realtime desc, rowid asc limit 1;", null);
                try {
                    if (cursorRawQuery.moveToFirst()) {
                        String string = cursorRawQuery.getString(0);
                        cursorRawQuery.close();
                        return string;
                    }
                } catch (SQLiteException e9) {
                    e8 = e9;
                    Z z8 = ((C0817r0) this.f5146q).f12150y;
                    C0817r0.k(z8);
                    z8.f11875v.c(e8, "Database error getting next bundle app id");
                }
            } catch (Throwable th) {
                r12 = sQLiteDatabaseA0;
                th = th;
                if (r12 != 0) {
                    r12.close();
                }
                throw th;
            }
        } catch (SQLiteException e10) {
            e8 = e10;
            cursorRawQuery = null;
        } catch (Throwable th2) {
            th = th2;
            if (r12 != 0) {
            }
            throw th;
        }
        if (cursorRawQuery != null) {
            cursorRawQuery.close();
        }
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:24:0x005e  */
    /* JADX WARN: Removed duplicated region for block: B:26:0x0061 A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:28:0x0064  */
    /* JADX WARN: Type inference failed for: r2v0 */
    /* JADX WARN: Type inference failed for: r2v1, types: [android.database.Cursor] */
    /* JADX WARN: Type inference failed for: r2v2 */
    /* JADX WARN: Type inference failed for: r3v1, types: [android.database.sqlite.SQLiteDatabase] */
    /* JADX WARN: Type inference failed for: r5v1, types: [java.lang.String[]] */
    /* JADX WARN: Type inference failed for: r5v4 */
    /* JADX WARN: Type inference failed for: r5v5 */
    /* JADX WARN: Type inference failed for: r5v6 */
    /* JADX WARN: Type inference failed for: r5v8, types: [android.database.Cursor] */
    /* JADX WARN: Type inference failed for: r5v9, types: [android.database.Cursor] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final F0 I0(String str) {
        Throwable th;
        SQLiteException e8;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.f(str);
        E();
        F();
        ?? RawQuery = {str};
        ?? r22 = 0;
        f0E = null;
        f0E = null;
        F0 f0E = null;
        try {
            try {
                RawQuery = A0().rawQuery("select consent_state, consent_source from consent_settings where app_id=? limit 1;", RawQuery);
                try {
                    if (RawQuery.moveToFirst()) {
                        f0E = F0.e(RawQuery.getInt(1), RawQuery.getString(0));
                    } else {
                        Z z8 = c0817r0.f12150y;
                        C0817r0.k(z8);
                        z8.D.b("No data found");
                    }
                } catch (SQLiteException e9) {
                    e8 = e9;
                    Z z9 = c0817r0.f12150y;
                    C0817r0.k(z9);
                    z9.f11875v.c(e8, "Error querying database.");
                    if (RawQuery != 0) {
                    }
                    if (f0E != null) {
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                r22 = RawQuery;
                if (r22 != 0) {
                    r22.close();
                }
                throw th;
            }
        } catch (SQLiteException e10) {
            e8 = e10;
            RawQuery = 0;
        } catch (Throwable th3) {
            th = th3;
            if (r22 != 0) {
            }
            throw th;
        }
        RawQuery.close();
        return f0E != null ? F0.f11457c : f0E;
    }

    public final List J(String str, String str2, String str3) {
        P2.w.c(str);
        E();
        F();
        ArrayList arrayList = new ArrayList(3);
        arrayList.add(str);
        StringBuilder sb = new StringBuilder("app_id=?");
        if (!TextUtils.isEmpty(str2)) {
            arrayList.add(str2);
            sb.append(" and origin=?");
        }
        if (!TextUtils.isEmpty(str3)) {
            arrayList.add(String.valueOf(str3).concat("*"));
            sb.append(" and name glob ?");
        }
        return K(sb.toString(), (String[]) arrayList.toArray(new String[arrayList.size()]));
    }

    /* JADX WARN: Removed duplicated region for block: B:36:0x00fe  */
    /* JADX WARN: Removed duplicated region for block: B:40:0x0106  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final R1 J0(String str) throws Throwable {
        Cursor cursorQuery;
        Cursor cursor;
        P2.w.c(str);
        E();
        F();
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        Cursor cursor2 = null;
        if (c0817r0.f12148w.R(null, I.f11511N0)) {
            if (c0817r0.f12148w.R(null, I.f11517Q0)) {
                EnumC0783f1[] enumC0783f1Arr = {EnumC0783f1.f11945s};
                ArrayList arrayList = new ArrayList(1);
                arrayList.add(Integer.valueOf(enumC0783f1Arr[0].f11951q));
                List listL = L(str, new I1(arrayList), 1);
                if (!listL.isEmpty()) {
                    return (R1) listL.get(0);
                }
            } else {
                try {
                    cursorQuery = 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"}, "app_id=? AND NOT " + i0(), new String[]{str}, null, null, "creation_timestamp ASC", "1");
                    try {
                    } catch (SQLiteException e8) {
                        e = e8;
                        cursor = cursorQuery;
                    } catch (Throwable th) {
                        th = th;
                        cursor = cursorQuery;
                    }
                } catch (SQLiteException e9) {
                    e = e9;
                    cursorQuery = null;
                } catch (Throwable th2) {
                    th = th2;
                }
                try {
                    if (cursorQuery.moveToFirst()) {
                        long j8 = cursorQuery.getLong(0);
                        byte[] blob = cursorQuery.getBlob(2);
                        String string = cursorQuery.getString(3);
                        String string2 = cursorQuery.getString(4);
                        int i = cursorQuery.getInt(5);
                        int i5 = cursorQuery.getInt(6);
                        cursorQuery.getLong(7);
                        cursor = cursorQuery;
                        try {
                            R1 r1H0 = h0(str, j8, blob, string, string2, i, i5, cursorQuery.getLong(8), cursorQuery.getLong(9));
                            cursor.close();
                            return r1H0;
                        } catch (SQLiteException e10) {
                            e = e10;
                        } catch (Throwable th3) {
                            th = th3;
                            cursor2 = cursor;
                            if (cursor2 != null) {
                            }
                            throw th;
                        }
                    } else if (cursorQuery != null) {
                        cursorQuery.close();
                    }
                    Z z8 = c0817r0.f12150y;
                    C0817r0.k(z8);
                    z8.f11875v.d("Error to querying MeasurementBatch from upload_queue. appId", str, e);
                    if (cursorQuery != null) {
                    }
                } catch (Throwable th4) {
                    th = th4;
                    cursor2 = cursorQuery;
                    if (cursor2 != null) {
                        cursor2.close();
                    }
                    throw th;
                }
                cursorQuery = cursor;
            }
        }
        return null;
    }

    /* JADX WARN: Code restructure failed: missing block: B:7:0x0056, code lost:
    
        r3 = r2.f12150y;
        d3.C0817r0.k(r3);
        r3.f11875v.c(1000, "Read more than the max allowed conditional properties, ignoring extra");
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v1, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r0v3, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v5, types: [java.util.List] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final List K(String str, String[] strArr) {
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        E();
        F();
        ?? arrayList = new ArrayList();
        Cursor cursorQuery = null;
        try {
            try {
                c0817r0.getClass();
                cursorQuery = A0().query("conditional_properties", new String[]{"app_id", "origin", "name", "value", "active", "trigger_event_name", "trigger_timeout", "timed_out_event", "creation_timestamp", "triggered_event", "triggered_timestamp", "time_to_live", "expired_event"}, str, strArr, null, null, "rowid", "1001");
                if (cursorQuery.moveToFirst()) {
                    while (true) {
                        if (arrayList.size() >= 1000) {
                            break;
                        }
                        String string = cursorQuery.getString(0);
                        String string2 = cursorQuery.getString(1);
                        String string3 = cursorQuery.getString(2);
                        Object objL0 = L0(cursorQuery, 3);
                        boolean z8 = cursorQuery.getInt(4) != 0;
                        String string4 = cursorQuery.getString(5);
                        long j8 = cursorQuery.getLong(6);
                        C0773c0 c0773c0 = this.f11473r.f11687w;
                        Q1.L(c0773c0);
                        byte[] blob = cursorQuery.getBlob(7);
                        Parcelable.Creator<C0828v> creator = C0828v.CREATOR;
                        C0828v c0828v = (C0828v) c0773c0.g0(blob, creator);
                        long j9 = cursorQuery.getLong(8);
                        Q1.L(c0773c0);
                        C0828v c0828v2 = (C0828v) c0773c0.g0(cursorQuery.getBlob(9), creator);
                        long j10 = cursorQuery.getLong(10);
                        long j11 = cursorQuery.getLong(11);
                        Q1.L(c0773c0);
                        arrayList.add(new C0778e(string, string2, new T1(j10, objL0, string3, string2), j9, z8, string4, c0828v, j8, c0828v2, j11, (C0828v) c0773c0.g0(cursorQuery.getBlob(12), creator)));
                        if (!cursorQuery.moveToNext()) {
                            break;
                        }
                    }
                }
            } catch (SQLiteException e8) {
                Z z9 = c0817r0.f12150y;
                C0817r0.k(z9);
                z9.f11875v.c(e8, "Error querying conditional user property value");
                arrayList = Collections.EMPTY_LIST;
            }
            if (cursorQuery != null) {
                cursorQuery.close();
            }
            return arrayList;
        } catch (Throwable th) {
            if (cursorQuery != null) {
                cursorQuery.close();
            }
            throw th;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:34:0x009d  */
    /* JADX WARN: Removed duplicated region for block: B:43:? A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final U1 K0(String str, String str2) {
        Throwable th;
        String str3;
        String str4;
        SQLiteException sQLiteException;
        Cursor cursorQuery;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str);
        P2.w.c(str2);
        E();
        F();
        Cursor cursor = null;
        try {
            cursorQuery = A0().query("user_attributes", new String[]{"set_timestamp", "value", "origin"}, "app_id=? and name=?", new String[]{str, str2}, null, null, null);
            try {
                try {
                } catch (Throwable th2) {
                    th = th2;
                    cursor = cursorQuery;
                    if (cursor != null) {
                        throw th;
                    }
                    cursor.close();
                    throw th;
                }
            } catch (SQLiteException e8) {
                e = e8;
                str3 = str;
                str4 = str2;
            }
        } catch (SQLiteException e9) {
            str3 = str;
            str4 = str2;
            sQLiteException = e9;
            cursorQuery = null;
        } catch (Throwable th3) {
            th = th3;
            if (cursor != null) {
            }
        }
        if (cursorQuery.moveToFirst()) {
            long j8 = cursorQuery.getLong(0);
            Object objL0 = L0(cursorQuery, 1);
            if (objL0 != null) {
                str3 = str;
                str4 = str2;
                try {
                    U1 u12 = new U1(str3, cursorQuery.getString(2), str4, j8, objL0);
                    if (cursorQuery.moveToNext()) {
                        Z z8 = c0817r0.f12150y;
                        C0817r0.k(z8);
                        z8.f11875v.c(Z.M(str3), "Got multiple records for user property, expected one. appId");
                    }
                    cursorQuery.close();
                    return u12;
                } catch (SQLiteException e10) {
                    e = e10;
                }
            }
            sQLiteException = e;
            Z z9 = c0817r0.f12150y;
            C0817r0.k(z9);
            z9.f11875v.e("Error querying user property. appId", Z.M(str3), c0817r0.f12123C.f(str4), sQLiteException);
        }
        if (cursorQuery != null) {
            cursorQuery.close();
        }
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v10, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r0v5, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v6, types: [java.util.List] */
    public final List L(String str, I1 i12, int i) {
        ?? arrayList;
        Cursor cursorQuery;
        C0805n c0805n = this;
        C0817r0 c0817r0 = (C0817r0) c0805n.f5146q;
        Cursor cursor = null;
        if (!c0817r0.f12148w.R(null, I.f11517Q0)) {
            return Collections.EMPTY_LIST;
        }
        P2.w.c(str);
        c0805n.E();
        c0805n.F();
        try {
            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"}, "app_id=?" + n0(i12.f11610q) + " AND NOT " + c0805n.i0(), new String[]{str}, null, null, "creation_timestamp ASC", i > 0 ? String.valueOf(i) : null);
            } catch (Throwable th) {
                th = th;
            }
        } catch (SQLiteException e8) {
            e = e8;
        }
        try {
            arrayList = new ArrayList();
            while (cursorQuery.moveToNext()) {
                long j8 = cursorQuery.getLong(0);
                byte[] blob = cursorQuery.getBlob(2);
                String string = cursorQuery.getString(3);
                String string2 = cursorQuery.getString(4);
                int i5 = cursorQuery.getInt(5);
                int i8 = cursorQuery.getInt(6);
                cursorQuery.getLong(7);
                R1 r1H0 = c0805n.h0(str, j8, blob, string, string2, i5, i8, cursorQuery.getLong(8), cursorQuery.getLong(9));
                if (r1H0 != null) {
                    arrayList.add(r1H0);
                }
                c0805n = this;
            }
        } catch (SQLiteException e9) {
            e = e9;
            cursor = cursorQuery;
            Z z8 = c0817r0.f12150y;
            C0817r0.k(z8);
            z8.f11875v.d("Error to querying MeasurementBatch from upload_queue. appId", str, e);
            arrayList = Collections.EMPTY_LIST;
            cursorQuery = cursor;
        } catch (Throwable th2) {
            th = th2;
            cursor = cursorQuery;
            if (cursor != null) {
                cursor.close();
            }
            throw th;
        }
        if (cursorQuery != null) {
            cursorQuery.close();
        }
        return arrayList;
    }

    public final Object L0(Cursor cursor, int i) {
        int type = cursor.getType(i);
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        if (type == 0) {
            Z z8 = c0817r0.f12150y;
            C0817r0.k(z8);
            z8.f11875v.b("Loaded invalid null value from database");
            return null;
        }
        if (type == 1) {
            return Long.valueOf(cursor.getLong(i));
        }
        if (type == 2) {
            return Double.valueOf(cursor.getDouble(i));
        }
        if (type == 3) {
            return cursor.getString(i);
        }
        if (type == 4) {
            Z z9 = c0817r0.f12150y;
            C0817r0.k(z9);
            z9.f11875v.b("Loaded invalid blob type value, ignoring it");
            return null;
        }
        Z z10 = c0817r0.f12150y;
        C0817r0.k(z10);
        z10.f11875v.c(Integer.valueOf(type), "Loaded invalid unknown value type, ignoring it");
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v1, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r0v4, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v6, types: [java.util.List] */
    public final List M(String str) {
        String str2;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str);
        E();
        F();
        ?? arrayList = new ArrayList();
        Cursor cursorQuery = null;
        try {
            try {
                c0817r0.getClass();
                cursorQuery = A0().query("user_attributes", new String[]{"name", "origin", "set_timestamp", "value"}, "app_id=?", new String[]{str}, null, null, "rowid", "1000");
                try {
                    if (cursorQuery.moveToFirst()) {
                        while (true) {
                            String string = cursorQuery.getString(0);
                            String string2 = cursorQuery.getString(1);
                            if (string2 == null) {
                                string2 = "";
                            }
                            String str3 = string2;
                            long j8 = cursorQuery.getLong(2);
                            Object objL0 = L0(cursorQuery, 3);
                            if (objL0 == null) {
                                Z z8 = c0817r0.f12150y;
                                C0817r0.k(z8);
                                z8.f11875v.c(Z.M(str), "Read invalid user property value, ignoring it. appId");
                                str2 = str;
                            } else {
                                str2 = str;
                                try {
                                    arrayList.add(new U1(str2, str3, string, j8, objL0));
                                } catch (SQLiteException e8) {
                                    e = e8;
                                    Z z9 = c0817r0.f12150y;
                                    C0817r0.k(z9);
                                    z9.f11875v.d("Error querying user properties. appId", Z.M(str2), e);
                                    arrayList = Collections.EMPTY_LIST;
                                }
                            }
                            if (!cursorQuery.moveToNext()) {
                                break;
                            }
                            str = str2;
                        }
                    }
                } catch (SQLiteException e9) {
                    e = e9;
                    str2 = str;
                }
            } finally {
            }
        } catch (SQLiteException e10) {
            e = e10;
            str2 = str;
        }
        if (cursorQuery != null) {
            cursorQuery.close();
        }
        return arrayList;
    }

    /* JADX WARN: Code restructure failed: missing block: B:22:0x00a6, code lost:
    
        d3.C0817r0.k(r7);
        r7.f11875v.c(1000, "Read more than the max allowed user properties, ignoring excess");
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:50:0x011e  */
    /* JADX WARN: Type inference failed for: r3v0, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r3v1, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r3v3, types: [java.util.List] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final List N(String str, String str2, String str3) throws Throwable {
        Cursor cursorQuery;
        String str4;
        String str5;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str);
        E();
        F();
        ?? arrayList = new ArrayList();
        try {
            ArrayList arrayList2 = new ArrayList(3);
            String str6 = str;
            arrayList2.add(str6);
            StringBuilder sb = new StringBuilder("app_id=?");
            if (!TextUtils.isEmpty(str2)) {
                arrayList2.add(str2);
                sb.append(" and origin=?");
            }
            if (!TextUtils.isEmpty(str3)) {
                arrayList2.add(str3 + "*");
                sb.append(" and name glob ?");
            }
            String[] strArr = (String[]) arrayList2.toArray(new String[arrayList2.size()]);
            String string = sb.toString();
            c0817r0.getClass();
            cursorQuery = A0().query("user_attributes", new String[]{"name", "set_timestamp", "value", "origin"}, string, strArr, null, null, "rowid", "1001");
            try {
                try {
                    if (cursorQuery.moveToFirst()) {
                        str4 = str2;
                        while (true) {
                            try {
                                int size = arrayList.size();
                                Z z8 = c0817r0.f12150y;
                                if (size >= 1000) {
                                    break;
                                }
                                String string2 = cursorQuery.getString(0);
                                long j8 = cursorQuery.getLong(1);
                                Object objL0 = L0(cursorQuery, 2);
                                String string3 = cursorQuery.getString(3);
                                if (objL0 == null) {
                                    try {
                                        C0817r0.k(z8);
                                        z8.f11875v.e("(2)Read invalid user property value, ignoring it", Z.M(str6), string3, str3);
                                        str5 = string3;
                                    } catch (SQLiteException e8) {
                                        e = e8;
                                        str5 = string3;
                                        str4 = str5;
                                        Z z9 = c0817r0.f12150y;
                                        C0817r0.k(z9);
                                        z9.f11875v.e("(2)Error querying user properties", Z.M(str), str4, e);
                                        arrayList = Collections.EMPTY_LIST;
                                    }
                                } else {
                                    str5 = string3;
                                    try {
                                        arrayList.add(new U1(str6, str5, string2, j8, objL0));
                                    } catch (SQLiteException e9) {
                                        e = e9;
                                        str4 = str5;
                                        Z z92 = c0817r0.f12150y;
                                        C0817r0.k(z92);
                                        z92.f11875v.e("(2)Error querying user properties", Z.M(str), str4, e);
                                        arrayList = Collections.EMPTY_LIST;
                                    }
                                }
                                if (!cursorQuery.moveToNext()) {
                                    break;
                                }
                                str6 = str;
                                str4 = str5;
                            } catch (SQLiteException e10) {
                                e = e10;
                            }
                        }
                    }
                } catch (Throwable th) {
                    th = th;
                    if (cursorQuery != null) {
                        cursorQuery.close();
                    }
                    throw th;
                }
            } catch (SQLiteException e11) {
                e = e11;
                str4 = str2;
            }
        } catch (SQLiteException e12) {
            e = e12;
            str4 = str2;
            cursorQuery = null;
        } catch (Throwable th2) {
            th = th2;
            cursorQuery = null;
            if (cursorQuery != null) {
            }
            throw th;
        }
        if (cursorQuery != null) {
            cursorQuery.close();
        }
        return arrayList;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:111:0x0117 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:49:0x00f5  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x013c  */
    /* JADX WARN: Removed duplicated region for block: B:81:0x01cb  */
    /* JADX WARN: Removed duplicated region for block: B:85:0x0264 A[Catch: SQLiteException -> 0x0281, TRY_LEAVE, TryCatch #6 {SQLiteException -> 0x0281, blocks: (B:83:0x0249, B:85:0x0264), top: B:104:0x0249 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void O(String str, Long l8, String str2, Bundle bundle) {
        String string;
        Z z8;
        long jUpdate;
        C0799l c0799l;
        Cursor cursorQuery;
        Object obj;
        Object obj2;
        C0628e1 c0628e1;
        C0628e1 c0628e12;
        String str3 = str;
        P2.w.f(bundle);
        E();
        F();
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        Cursor cursor = null;
        C0799l c0799l2 = (!c0817r0.f12148w.R(null, I.f11550f1) || l8 == null) ? new C0799l(this, str3) : new C0799l(this, str3, l8.longValue());
        List<C0796k> listA = c0799l2.a();
        while (!listA.isEmpty()) {
            for (C0796k c0796k : listA) {
                boolean zIsEmpty = TextUtils.isEmpty(str2);
                Z z9 = c0817r0.f12150y;
                if (!zIsEmpty) {
                    try {
                        cursorQuery = A0().query("raw_events_metadata", new String[]{"metadata"}, "app_id = ? and metadata_fingerprint = ?", new String[]{str3, Long.toString(c0796k.f12029b)}, null, null, "rowid", "2");
                        try {
                            try {
                            } catch (Throwable th) {
                                th = th;
                                cursor = cursorQuery;
                                if (cursor != null) {
                                    cursor.close();
                                }
                                throw th;
                            }
                        } catch (SQLiteException e8) {
                            e = e8;
                            obj = cursor;
                        }
                    } catch (SQLiteException e9) {
                        e = e9;
                        cursorQuery = cursor;
                        obj = cursorQuery;
                    } catch (Throwable th2) {
                        th = th2;
                    }
                    if (cursorQuery.moveToFirst()) {
                        try {
                            c0628e12 = (C0628e1) ((C0623d1) C0773c0.k0(C0628e1.k2(), cursorQuery.getBlob(0))).h();
                        } catch (IOException e10) {
                            C0817r0.k(z9);
                            z9.f11875v.d("Data loss. Failed to merge raw event metadata. appId", Z.M(str3), e10);
                            cursorQuery.close();
                            c0628e1 = cursor;
                        }
                        try {
                            if (cursorQuery.moveToNext()) {
                                C0817r0.k(z9);
                                z9.f11878y.c(Z.M(str3), "Get multiple raw event metadata records, expected one. appId");
                            }
                            cursorQuery.close();
                            obj2 = c0628e12;
                        } catch (SQLiteException e11) {
                            e = e11;
                            obj = c0628e12;
                            C0817r0.k(z9);
                            z9.f11875v.d("Data loss. Error selecting raw event. appId", Z.M(str3), e);
                            c0628e1 = obj;
                            obj2 = obj;
                            if (cursorQuery != null) {
                            }
                            if (c0628e1 != 0) {
                            }
                            Q1 q12 = this.f11473r;
                            C0773c0 c0773c0 = q12.f11687w;
                            Q1.L(c0773c0);
                            com.google.android.gms.internal.measurement.W0 w02 = c0796k.f12031d;
                            Bundle bundle2 = new Bundle();
                            while (r8.hasNext()) {
                            }
                            C0799l c0799l3 = c0799l2;
                            string = bundle2.getString("_o");
                            bundle2.remove("_o");
                            if (string == null) {
                            }
                            String str4 = string;
                            W1 w12 = c0817r0.f12122B;
                            C0817r0.i(w12);
                            w12.V(bundle2, bundle);
                            r rVar = new r((C0817r0) this.f5146q, str4, str3, w02.t(), w02.q(), w02.p(), bundle2);
                            long j8 = c0796k.f12028a;
                            E();
                            F();
                            String str5 = rVar.f12115a;
                            P2.w.c(str5);
                            C0773c0 c0773c02 = q12.f11687w;
                            Q1.L(c0773c02);
                            byte[] bArrC = c0773c02.j0(rVar).c();
                            ContentValues contentValues = new ContentValues();
                            contentValues.put("app_id", str5);
                            contentValues.put("name", rVar.f12116b);
                            contentValues.put("timestamp", Long.valueOf(rVar.f12118d));
                            contentValues.put("metadata_fingerprint", Long.valueOf(c0796k.f12029b));
                            contentValues.put("data", bArrC);
                            contentValues.put("realtime", Integer.valueOf(c0796k.f12030c ? 1 : 0));
                            jUpdate = A0().update("raw_events", contentValues, "rowid = ?", new String[]{String.valueOf(j8)});
                            if (jUpdate != 1) {
                            }
                            str3 = str;
                            c0799l2 = c0799l3;
                            cursor = null;
                        }
                        cursorQuery.close();
                        c0628e1 = obj2;
                        if (c0628e1 != 0) {
                            Iterator it = c0628e1.K().iterator();
                            while (it.hasNext()) {
                                if (((C0673n1) it.next()).s().equals(str2)) {
                                    break;
                                }
                            }
                        }
                    } else {
                        C0817r0.k(z9);
                        z9.f11875v.c(Z.M(str3), "Raw event metadata record is missing. appId");
                    }
                    cursorQuery.close();
                    c0628e1 = cursor;
                    if (c0628e1 != 0) {
                    }
                }
                Q1 q122 = this.f11473r;
                C0773c0 c0773c03 = q122.f11687w;
                Q1.L(c0773c03);
                com.google.android.gms.internal.measurement.W0 w022 = c0796k.f12031d;
                Bundle bundle22 = new Bundle();
                for (com.google.android.gms.internal.measurement.Z0 z02 : w022.u()) {
                    if (z02.F()) {
                        c0799l = c0799l2;
                        bundle22.putDouble(z02.s(), z02.n());
                    } else {
                        c0799l = c0799l2;
                        if (z02.G()) {
                            bundle22.putFloat(z02.s(), z02.o());
                        } else if (z02.H()) {
                            bundle22.putLong(z02.s(), z02.q());
                        } else if (z02.J()) {
                            bundle22.putString(z02.s(), z02.t());
                        } else if (z02.u().isEmpty()) {
                            Z z10 = ((C0817r0) c0773c03.f5146q).f12150y;
                            C0817r0.k(z10);
                            z10.f11875v.c(z02, "Unexpected parameter type for parameter");
                        } else {
                            bundle22.putParcelableArray(z02.s(), C0773c0.K((InterfaceC0639g2) z02.u()));
                        }
                    }
                    c0799l2 = c0799l;
                }
                C0799l c0799l32 = c0799l2;
                string = bundle22.getString("_o");
                bundle22.remove("_o");
                if (string == null) {
                    string = "";
                }
                String str42 = string;
                W1 w122 = c0817r0.f12122B;
                C0817r0.i(w122);
                w122.V(bundle22, bundle);
                r rVar2 = new r((C0817r0) this.f5146q, str42, str3, w022.t(), w022.q(), w022.p(), bundle22);
                long j82 = c0796k.f12028a;
                E();
                F();
                String str52 = rVar2.f12115a;
                P2.w.c(str52);
                C0773c0 c0773c022 = q122.f11687w;
                Q1.L(c0773c022);
                byte[] bArrC2 = c0773c022.j0(rVar2).c();
                ContentValues contentValues2 = new ContentValues();
                contentValues2.put("app_id", str52);
                contentValues2.put("name", rVar2.f12116b);
                contentValues2.put("timestamp", Long.valueOf(rVar2.f12118d));
                contentValues2.put("metadata_fingerprint", Long.valueOf(c0796k.f12029b));
                contentValues2.put("data", bArrC2);
                contentValues2.put("realtime", Integer.valueOf(c0796k.f12030c ? 1 : 0));
                try {
                    jUpdate = A0().update("raw_events", contentValues2, "rowid = ?", new String[]{String.valueOf(j82)});
                    if (jUpdate != 1) {
                        C0817r0.k(z9);
                        z8 = z9;
                        try {
                            z8.f11875v.d("Failed to update raw event. appId, updatedRows", Z.M(str52), Long.valueOf(jUpdate));
                        } catch (SQLiteException e12) {
                            e = e12;
                            C0817r0.k(z8);
                            z8.f11875v.d("Error updating raw event. appId", Z.M(str52), e);
                        }
                    }
                } catch (SQLiteException e13) {
                    e = e13;
                    z8 = z9;
                }
                str3 = str;
                c0799l2 = c0799l32;
                cursor = null;
            }
            listA = c0799l2.a();
            str3 = str;
            cursor = null;
        }
    }

    public final void P() {
        F();
        A0().beginTransaction();
    }

    public final void Q(String str) {
        C0819s c0819sG0;
        k0("events_snapshot", str);
        Cursor cursorQuery = null;
        try {
            try {
                cursorQuery = A0().query("events", (String[]) Collections.singletonList("name").toArray(new String[0]), "app_id=?", new String[]{str}, null, null, null);
                if (cursorQuery.moveToFirst()) {
                    do {
                        String string = cursorQuery.getString(0);
                        if (string != null && (c0819sG0 = g0("events", str, string)) != null) {
                            l0("events_snapshot", c0819sG0);
                        }
                    } while (cursorQuery.moveToNext());
                }
            } catch (SQLiteException e8) {
                Z z8 = ((C0817r0) this.f5146q).f12150y;
                C0817r0.k(z8);
                z8.f11875v.d("Error creating snapshot. appId", Z.M(str), e8);
            }
            if (cursorQuery != null) {
                cursorQuery.close();
            }
        } finally {
        }
    }

    public final void R(ArrayList arrayList) {
        P2.w.f(arrayList);
        E();
        F();
        StringBuilder sb = new StringBuilder("rowid in (");
        for (int i = 0; i < arrayList.size(); i++) {
            if (i != 0) {
                sb.append(",");
            }
            sb.append(((Long) arrayList.get(i)).longValue());
        }
        sb.append(")");
        int iDelete = A0().delete("raw_events", sb.toString(), null);
        if (iDelete != arrayList.size()) {
            Z z8 = ((C0817r0) this.f5146q).f12150y;
            C0817r0.k(z8);
            z8.f11875v.d("Deleted fewer rows from raw events table than expected", Integer.valueOf(iDelete), Integer.valueOf(arrayList.size()));
        }
    }

    public final void S(Long l8) {
        E();
        F();
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        C0784g c0784g = c0817r0.f12148w;
        Z z8 = c0817r0.f12150y;
        if (c0784g.R(null, I.f11511N0)) {
            try {
                if (A0().delete("upload_queue", "rowid=?", new String[]{l8.toString()}) != 1) {
                    C0817r0.k(z8);
                    z8.f11878y.b("Deleted fewer rows from upload_queue than expected");
                }
            } catch (SQLiteException e8) {
                C0817r0.k(z8);
                z8.f11875v.c(e8, "Failed to delete a MeasurementBatch in a upload_queue table");
                throw e8;
            }
        }
    }

    public final void T() {
        F();
        A0().endTransaction();
    }

    public final void U(ArrayList arrayList) {
        E();
        F();
        P2.w.f(arrayList);
        if (arrayList.size() == 0) {
            throw new IllegalArgumentException("Given Integer is zero");
        }
        if (p0()) {
            String strP = I1.a.p("(", TextUtils.join(",", arrayList), ")");
            long jV0 = v0("SELECT COUNT(1) FROM queue WHERE rowid IN " + strP + " AND retry_count =  2147483647 LIMIT 1", null);
            C0817r0 c0817r0 = (C0817r0) this.f5146q;
            if (jV0 > 0) {
                Z z8 = c0817r0.f12150y;
                C0817r0.k(z8);
                z8.f11878y.b("The number of upload retries exceeds the limit. Will remain unchanged.");
            }
            try {
                A0().execSQL("UPDATE queue SET retry_count = IFNULL(retry_count, 0) + 1 WHERE rowid IN " + strP + " AND (retry_count IS NULL OR retry_count < 2147483647)");
            } catch (SQLiteException e8) {
                Z z9 = c0817r0.f12150y;
                C0817r0.k(z9);
                z9.f11875v.c(e8, "Error incrementing retry count. error");
            }
        }
    }

    public final void V(Long l8) {
        String str;
        E();
        F();
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        if (c0817r0.f12148w.R(null, I.f11511N0) && p0()) {
            long jV0 = v0("SELECT COUNT(1) FROM upload_queue WHERE rowid = " + l8 + " AND retry_count =  2147483647 LIMIT 1", null);
            Z z8 = c0817r0.f12150y;
            if (jV0 > 0) {
                C0817r0.k(z8);
                z8.f11878y.b("The number of upload retries exceeds the limit. Will remain unchanged.");
            }
            try {
                SQLiteDatabase sQLiteDatabaseA0 = A0();
                if (c0817r0.f12148w.R(null, I.f11517Q0)) {
                    c0817r0.D.getClass();
                    str = " SET retry_count = retry_count + 1, last_upload_timestamp = " + System.currentTimeMillis();
                } else {
                    str = " SET retry_count = retry_count + 1 ";
                }
                sQLiteDatabaseA0.execSQL("UPDATE upload_queue" + str + " WHERE rowid = " + l8 + " AND retry_count < 2147483647");
            } catch (SQLiteException e8) {
                C0817r0.k(z8);
                z8.f11875v.c(e8, "Error incrementing retry count. error");
            }
        }
    }

    public final void W() {
        E();
        F();
        if (p0()) {
            Q1 q12 = this.f11473r;
            long jG = q12.f11689y.f12204u.g();
            C0817r0 c0817r0 = (C0817r0) this.f5146q;
            c0817r0.D.getClass();
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            if (Math.abs(jElapsedRealtime - jG) > ((Long) I.f11508M.a(null)).longValue()) {
                q12.f11689y.f12204u.h(jElapsedRealtime);
                E();
                F();
                if (p0()) {
                    SQLiteDatabase sQLiteDatabaseA0 = A0();
                    c0817r0.D.getClass();
                    int iDelete = sQLiteDatabaseA0.delete("queue", "abs(bundle_end_timestamp - ?) > cast(? as integer)", new String[]{String.valueOf(System.currentTimeMillis()), String.valueOf(((Long) I.f11518R.a(null)).longValue())});
                    if (iDelete > 0) {
                        Z z8 = c0817r0.f12150y;
                        C0817r0.k(z8);
                        z8.D.c(Integer.valueOf(iDelete), "Deleted stale rows. rowsDeleted");
                    }
                }
            }
        }
    }

    public final void X(String str, String str2) {
        P2.w.c(str);
        P2.w.c(str2);
        E();
        F();
        try {
            A0().delete("user_attributes", "app_id=? and name=?", new String[]{str, str2});
        } catch (SQLiteException e8) {
            C0817r0 c0817r0 = (C0817r0) this.f5146q;
            Z z8 = c0817r0.f12150y;
            C0817r0.k(z8);
            z8.f11875v.e("Error deleting user property. appId", Z.M(str), c0817r0.f12123C.f(str2), e8);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:50:0x00cb  */
    /* JADX WARN: Removed duplicated region for block: B:55:0x00d7  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0054  */
    /* JADX WARN: Removed duplicated region for block: B:9:0x005b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void Y(String str) throws Throwable {
        boolean z8;
        C0819s c0819sG0;
        ArrayList arrayList = new ArrayList(Arrays.asList("name", "lifetime_count"));
        C0819s c0819sG02 = g0("events", str, "_f");
        C0819s c0819sG03 = g0("events", str, "_v");
        k0("events", str);
        Cursor cursorQuery = null;
        boolean z9 = false;
        try {
            cursorQuery = A0().query("events_snapshot", (String[]) arrayList.toArray(new String[0]), "app_id=?", new String[]{str}, null, null, null);
        } catch (SQLiteException e8) {
            e = e8;
            z8 = false;
        } catch (Throwable th) {
            th = th;
            z8 = false;
        }
        if (!cursorQuery.moveToFirst()) {
            cursorQuery.close();
            if (c0819sG02 != null) {
                l0("events", c0819sG02);
            } else if (c0819sG03 != null) {
                l0("events", c0819sG03);
            }
            k0("events_snapshot", str);
        }
        boolean z10 = false;
        z8 = false;
        do {
            try {
                String string = cursorQuery.getString(0);
                if (cursorQuery.getLong(1) >= 1) {
                    if ("_f".equals(string)) {
                        z10 = true;
                    } else if ("_v".equals(string)) {
                        z8 = true;
                    }
                }
                if (string != null && (c0819sG0 = g0("events_snapshot", str, string)) != null) {
                    l0("events", c0819sG0);
                }
            } catch (SQLiteException e9) {
                e = e9;
                z9 = z10;
                try {
                    Z z11 = ((C0817r0) this.f5146q).f12150y;
                    C0817r0.k(z11);
                    z11.f11875v.d("Error querying snapshot. appId", Z.M(str), e);
                    z10 = z9;
                } catch (Throwable th2) {
                    th = th2;
                    if (cursorQuery != null) {
                        cursorQuery.close();
                    }
                    if (z9 && c0819sG02 != null) {
                        l0("events", c0819sG02);
                    } else if (!z8 && c0819sG03 != null) {
                        l0("events", c0819sG03);
                    }
                    k0("events_snapshot", str);
                    throw th;
                }
            } catch (Throwable th3) {
                th = th3;
                z9 = z10;
                if (cursorQuery != null) {
                }
                if (z9) {
                    if (!z8) {
                        l0("events", c0819sG03);
                    }
                }
                k0("events_snapshot", str);
                throw th;
            }
        } while (cursorQuery.moveToNext());
        if (cursorQuery != null) {
            cursorQuery.close();
        }
        if (z10 || c0819sG02 == null) {
            if (!z8 && c0819sG03 != null) {
            }
        }
        k0("events_snapshot", str);
    }

    public final void Z() {
        F();
        A0().setTransactionSuccessful();
    }

    public final void a0(V v7, boolean z8) {
        E();
        F();
        String strC = v7.c();
        P2.w.f(strC);
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id", strC);
        E0 e02 = E0.f11448s;
        Q1 q12 = this.f11473r;
        if (z8) {
            contentValues.put("app_instance_id", (String) null);
        } else if (q12.n0(strC).k(e02)) {
            contentValues.put("app_instance_id", v7.d());
        }
        contentValues.put("gmp_app_id", v7.g());
        boolean zK = q12.n0(strC).k(E0.f11447r);
        C0817r0 c0817r0 = v7.f11758a;
        if (zK) {
            C0815q0 c0815q0 = c0817r0.f12151z;
            C0817r0.k(c0815q0);
            c0815q0.E();
            contentValues.put("resettable_device_id_hash", v7.f11762e);
        }
        C0815q0 c0815q02 = c0817r0.f12151z;
        C0817r0.k(c0815q02);
        c0815q02.E();
        contentValues.put("last_bundle_index", Long.valueOf(v7.f11764g));
        C0815q0 c0815q03 = c0817r0.f12151z;
        C0817r0.k(c0815q03);
        c0815q03.E();
        contentValues.put("last_bundle_start_timestamp", Long.valueOf(v7.f11765h));
        C0815q0 c0815q04 = c0817r0.f12151z;
        C0817r0.k(c0815q04);
        c0815q04.E();
        contentValues.put("last_bundle_end_timestamp", Long.valueOf(v7.i));
        contentValues.put("app_version", v7.e());
        C0815q0 c0815q05 = c0817r0.f12151z;
        C0817r0.k(c0815q05);
        c0815q05.E();
        contentValues.put("app_store", v7.f11768l);
        C0815q0 c0815q06 = c0817r0.f12151z;
        C0817r0.k(c0815q06);
        c0815q06.E();
        contentValues.put("gmp_version", Long.valueOf(v7.f11769m));
        C0815q0 c0815q07 = c0817r0.f12151z;
        C0817r0.k(c0815q07);
        c0815q07.E();
        contentValues.put("dev_cert_hash", Long.valueOf(v7.f11770n));
        C0815q0 c0815q08 = c0817r0.f12151z;
        C0817r0.k(c0815q08);
        c0815q08.E();
        contentValues.put("measurement_enabled", Boolean.valueOf(v7.f11771o));
        C0815q0 c0815q09 = c0817r0.f12151z;
        C0817r0.k(c0815q09);
        c0815q09.E();
        contentValues.put("day", Long.valueOf(v7.f11748K));
        C0815q0 c0815q010 = c0817r0.f12151z;
        C0817r0.k(c0815q010);
        c0815q010.E();
        contentValues.put("daily_public_events_count", Long.valueOf(v7.f11749L));
        C0817r0.k(c0815q010);
        c0815q010.E();
        contentValues.put("daily_events_count", Long.valueOf(v7.f11750M));
        C0817r0.k(c0815q010);
        c0815q010.E();
        contentValues.put("daily_conversions_count", Long.valueOf(v7.f11751N));
        C0815q0 c0815q011 = c0817r0.f12151z;
        C0817r0.k(c0815q011);
        c0815q011.E();
        contentValues.put("config_fetched_time", Long.valueOf(v7.f11756S));
        C0815q0 c0815q012 = c0817r0.f12151z;
        C0817r0.k(c0815q012);
        c0815q012.E();
        contentValues.put("failed_config_fetch_time", Long.valueOf(v7.f11757T));
        contentValues.put("app_version_int", Long.valueOf(v7.S()));
        contentValues.put("firebase_instance_id", v7.f());
        C0817r0.k(c0815q010);
        c0815q010.E();
        contentValues.put("daily_error_events_count", Long.valueOf(v7.f11752O));
        C0817r0.k(c0815q010);
        c0815q010.E();
        contentValues.put("daily_realtime_events_count", Long.valueOf(v7.f11753P));
        C0817r0.k(c0815q010);
        c0815q010.E();
        contentValues.put("health_monitor_sample", v7.f11754Q);
        contentValues.put("android_id", (Long) 0L);
        C0815q0 c0815q013 = c0817r0.f12151z;
        C0817r0.k(c0815q013);
        c0815q013.E();
        contentValues.put("adid_reporting_enabled", Boolean.valueOf(v7.f11772p));
        contentValues.put("admob_app_id", v7.a());
        contentValues.put("dynamite_version", Long.valueOf(v7.T()));
        if (q12.n0(strC).k(e02)) {
            C0815q0 c0815q014 = c0817r0.f12151z;
            C0817r0.k(c0815q014);
            c0815q014.E();
            contentValues.put("session_stitching_token", v7.f11777u);
        }
        contentValues.put("sgtm_upload_enabled", Boolean.valueOf(v7.t()));
        C0815q0 c0815q015 = c0817r0.f12151z;
        C0817r0.k(c0815q015);
        c0815q015.E();
        contentValues.put("target_os_version", Long.valueOf(v7.f11779w));
        C0815q0 c0815q016 = c0817r0.f12151z;
        C0817r0.k(c0815q016);
        c0815q016.E();
        contentValues.put("session_stitching_token_hash", Long.valueOf(v7.f11780x));
        J3.b();
        C0817r0 c0817r02 = (C0817r0) this.f5146q;
        if (c0817r02.f12148w.R(strC, I.W0)) {
            C0815q0 c0815q017 = c0817r0.f12151z;
            C0817r0.k(c0815q017);
            c0815q017.E();
            contentValues.put("ad_services_version", Integer.valueOf(v7.f11781y));
            C0815q0 c0815q018 = c0817r0.f12151z;
            C0817r0.k(c0815q018);
            c0815q018.E();
            contentValues.put("attribution_eligibility_status", Long.valueOf(v7.f11742C));
        }
        C0815q0 c0815q019 = c0817r0.f12151z;
        C0817r0.k(c0815q019);
        c0815q019.E();
        contentValues.put("unmatched_first_open_without_ad_id", Boolean.valueOf(v7.f11782z));
        contentValues.put("npa_metadata_value", v7.U());
        C0815q0 c0815q020 = c0817r0.f12151z;
        C0817r0.k(c0815q020);
        c0815q020.E();
        contentValues.put("bundle_delivery_index", Long.valueOf(v7.f11745G));
        contentValues.put("sgtm_preview_key", v7.i());
        C0817r0.k(c0815q010);
        c0815q010.E();
        contentValues.put("dma_consent_state", Integer.valueOf(v7.f11743E));
        C0817r0.k(c0815q010);
        c0815q010.E();
        contentValues.put("daily_realtime_dcu_count", Integer.valueOf(v7.f11744F));
        contentValues.put("serialized_npa_metadata", v7.h());
        H h8 = I.f11517Q0;
        C0784g c0784g = c0817r02.f12148w;
        if (c0784g.R(strC, h8)) {
            contentValues.put("client_upload_eligibility", Integer.valueOf(v7.R()));
        }
        C0815q0 c0815q021 = c0817r0.f12151z;
        C0817r0.k(c0815q021);
        c0815q021.E();
        ArrayList arrayList = v7.f11776t;
        Z z9 = c0817r02.f12150y;
        if (arrayList != null) {
            if (arrayList.isEmpty()) {
                C0817r0.k(z9);
                z9.f11878y.c(strC, "Safelisted events should not be an empty list. appId");
            } else {
                contentValues.put("safelisted_events", TextUtils.join(",", arrayList));
            }
        }
        if (c0784g.R(null, I.K0) && !contentValues.containsKey("safelisted_events")) {
            contentValues.put("safelisted_events", (String) null);
        }
        C0815q0 c0815q022 = c0817r0.f12151z;
        C0817r0.k(c0815q022);
        c0815q022.E();
        contentValues.put("unmatched_pfo", v7.f11740A);
        C0815q0 c0815q023 = c0817r0.f12151z;
        C0817r0.k(c0815q023);
        c0815q023.E();
        contentValues.put("unmatched_uwa", v7.f11741B);
        C0815q0 c0815q024 = c0817r0.f12151z;
        C0817r0.k(c0815q024);
        c0815q024.E();
        contentValues.put("ad_campaign_info", v7.f11746I);
        try {
            SQLiteDatabase sQLiteDatabaseA0 = A0();
            if (sQLiteDatabaseA0.update("apps", contentValues, "app_id = ?", new String[]{strC}) == 0 && sQLiteDatabaseA0.insertWithOnConflict("apps", null, contentValues, 5) == -1) {
                C0817r0.k(z9);
                z9.f11875v.c(Z.M(strC), "Failed to insert/update app (got -1). appId");
            }
        } catch (SQLiteException e8) {
            C0817r0.k(z9);
            z9.f11875v.d("Error storing app. appId", Z.M(strC), e8);
        }
    }

    public final void b0(String str, F0 f02) {
        P2.w.f(str);
        E();
        F();
        c0(str, I0(str));
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id", str);
        contentValues.put("storage_consent_at_bundling", f02.j());
        m0(contentValues);
    }

    public final void c0(String str, F0 f02) {
        P2.w.f(str);
        P2.w.f(f02);
        E();
        F();
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id", str);
        contentValues.put("consent_state", f02.j());
        contentValues.put("consent_source", Integer.valueOf(f02.f11459b));
        m0(contentValues);
    }

    /* JADX WARN: Removed duplicated region for block: B:11:0x0074 A[RETURN] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean d0(String str) {
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        if (c0817r0.f12148w.R(null, I.f11511N0)) {
            if (c0817r0.f12148w.R(null, I.f11517Q0)) {
                EnumC0783f1[] enumC0783f1Arr = {EnumC0783f1.f11945s};
                ArrayList arrayList = new ArrayList(1);
                arrayList.add(Integer.valueOf(enumC0783f1Arr[0].f11951q));
                if (v0("SELECT COUNT(1) > 0 FROM upload_queue WHERE app_id=?" + n0(arrayList) + " AND NOT " + i0(), new String[]{str}) != 0) {
                    return true;
                }
            } else if (v0("SELECT COUNT(1) > 0 FROM upload_queue WHERE app_id=? AND NOT ".concat(i0()), new String[]{str}) != 0) {
            }
        }
        return false;
    }

    public final boolean e0(String str, String str2) {
        return v0("select count(1) from raw_events where app_id = ? and name = ?", new String[]{str, str2}) > 0;
    }

    public final void f0(String str, String str2) {
        P2.w.c(str);
        P2.w.c(str2);
        E();
        F();
        try {
            A0().delete("conditional_properties", "app_id=? and name=?", new String[]{str, str2});
        } catch (SQLiteException e8) {
            C0817r0 c0817r0 = (C0817r0) this.f5146q;
            Z z8 = c0817r0.f12150y;
            C0817r0.k(z8);
            z8.f11875v.e("Error deleting conditional property", Z.M(str), c0817r0.f12123C.f(str2), e8);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:54:0x012d  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final C0819s g0(String str, String str2, String str3) {
        Cursor cursorQuery;
        Boolean boolValueOf;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str2);
        P2.w.c(str3);
        E();
        F();
        Cursor cursor = null;
        try {
            cursorQuery = A0().query(str, (String[]) new ArrayList(Arrays.asList("lifetime_count", "current_bundle_count", "last_fire_timestamp", "last_bundled_timestamp", "last_bundled_day", "last_sampled_complex_event_id", "last_sampling_rate", "last_exempt_from_sampling", "current_session_count")).toArray(new String[0]), "app_id=? and name=?", new String[]{str2, str3}, null, null, null);
            try {
                try {
                    if (cursorQuery.moveToFirst()) {
                        long j8 = cursorQuery.getLong(0);
                        long j9 = cursorQuery.getLong(1);
                        long j10 = cursorQuery.getLong(2);
                        long j11 = cursorQuery.isNull(3) ? 0L : cursorQuery.getLong(3);
                        Long lValueOf = cursorQuery.isNull(4) ? null : Long.valueOf(cursorQuery.getLong(4));
                        Long lValueOf2 = cursorQuery.isNull(5) ? null : Long.valueOf(cursorQuery.getLong(5));
                        Long lValueOf3 = cursorQuery.isNull(6) ? null : Long.valueOf(cursorQuery.getLong(6));
                        if (cursorQuery.isNull(7)) {
                            boolValueOf = null;
                        } else {
                            boolValueOf = Boolean.valueOf(cursorQuery.getLong(7) == 1);
                        }
                        C0819s c0819s = new C0819s(str2, str3, j8, j9, cursorQuery.isNull(8) ? 0L : cursorQuery.getLong(8), j10, j11, lValueOf, lValueOf2, lValueOf3, boolValueOf);
                        if (cursorQuery.moveToNext()) {
                            Z z8 = c0817r0.f12150y;
                            C0817r0.k(z8);
                            z8.f11875v.c(Z.M(str2), "Got multiple records for event aggregates, expected one. appId");
                        }
                        cursorQuery.close();
                        return c0819s;
                    }
                } catch (SQLiteException e8) {
                    e = e8;
                    Z z9 = c0817r0.f12150y;
                    C0817r0.k(z9);
                    z9.f11875v.e("Error querying events. appId", Z.M(str2), c0817r0.f12123C.d(str3), e);
                }
            } catch (Throwable th) {
                th = th;
                cursor = cursorQuery;
                if (cursor != null) {
                    cursor.close();
                }
                throw th;
            }
        } catch (SQLiteException e9) {
            e = e9;
            cursorQuery = null;
        } catch (Throwable th2) {
            th = th2;
            if (cursor != null) {
            }
            throw th;
        }
        if (cursorQuery != null) {
            cursorQuery.close();
        }
        return null;
    }

    public final R1 h0(String str, long j8, byte[] bArr, String str2, String str3, int i, int i5, long j9, long j10) {
        EnumC0783f1 enumC0783f1;
        boolean zIsEmpty = TextUtils.isEmpty(str2);
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        if (zIsEmpty) {
            Z z8 = c0817r0.f12150y;
            C0817r0.k(z8);
            z8.f11871C.b("Upload uri is null or empty. Destination is unknown. Dropping batch. ");
            return null;
        }
        try {
            C0613b1 c0613b1 = (C0613b1) C0773c0.k0(C0618c1.o(), bArr);
            EnumC0783f1[] enumC0783f1ArrValues = EnumC0783f1.values();
            int length = enumC0783f1ArrValues.length;
            int i8 = 0;
            while (true) {
                if (i8 >= length) {
                    enumC0783f1 = EnumC0783f1.f11949w;
                    break;
                }
                enumC0783f1 = enumC0783f1ArrValues[i8];
                if (enumC0783f1.f11951q == i) {
                    break;
                }
                i8++;
            }
            if (enumC0783f1 != EnumC0783f1.f11945s && enumC0783f1 != EnumC0783f1.f11948v && i5 > 0) {
                ArrayList arrayList = new ArrayList();
                Iterator it = Collections.unmodifiableList(((C0618c1) c0613b1.f9801r).t()).iterator();
                while (it.hasNext()) {
                    C0623d1 c0623d1 = (C0623d1) ((C0628e1) it.next()).f();
                    c0623d1.k();
                    C0628e1.u1((C0628e1) c0623d1.f9801r, i5);
                    arrayList.add((C0628e1) c0623d1.h());
                }
                c0613b1.k();
                C0618c1.w((C0618c1) c0613b1.f9801r);
                c0613b1.k();
                C0618c1.u((C0618c1) c0613b1.f9801r, arrayList);
            }
            HashMap map = new HashMap();
            if (str3 != null) {
                String[] strArrSplit = str3.split("\r\n");
                int length2 = strArrSplit.length;
                int i9 = 0;
                while (true) {
                    if (i9 >= length2) {
                        break;
                    }
                    String str4 = strArrSplit[i9];
                    if (str4.isEmpty()) {
                        break;
                    }
                    String[] strArrSplit2 = str4.split("=", 2);
                    if (strArrSplit2.length != 2) {
                        Z z9 = c0817r0.f12150y;
                        C0817r0.k(z9);
                        z9.f11875v.c(str4, "Invalid upload header: ");
                        break;
                    }
                    map.put(strArrSplit2[0], strArrSplit2[1]);
                    i9++;
                }
            }
            return new R1(j8, (C0618c1) c0613b1.h(), str2, map, enumC0783f1, j9, j10, i5);
        } catch (IOException e8) {
            Z z10 = c0817r0.f12150y;
            C0817r0.k(z10);
            z10.f11875v.d("Failed to queued MeasurementBatch from upload_queue. appId", str, e8);
            return null;
        }
    }

    public final String i0() {
        ((C0817r0) this.f5146q).D.getClass();
        long jCurrentTimeMillis = System.currentTimeMillis();
        Locale locale = Locale.US;
        Long l8 = (Long) I.f11520S.a(null);
        l8.getClass();
        String str = "(upload_type = 1 AND ABS(creation_timestamp - " + jCurrentTimeMillis + ") > " + l8 + ")";
        long jLongValue = ((Long) I.f11518R.a(null)).longValue();
        StringBuilder sb = new StringBuilder("(upload_type != 1 AND ABS(creation_timestamp - ");
        sb.append(jCurrentTimeMillis);
        sb.append(") > ");
        return "(" + str + " OR " + I1.a.q(sb, jLongValue, ")") + ")";
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0035  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String j0(String str, String[] strArr) {
        Cursor cursorRawQuery = null;
        try {
            try {
                cursorRawQuery = A0().rawQuery(str, strArr);
                if (!cursorRawQuery.moveToFirst()) {
                    cursorRawQuery.close();
                    return "";
                }
                String string = cursorRawQuery.getString(0);
                cursorRawQuery.close();
                return string;
            } catch (SQLiteException e8) {
                Z z8 = ((C0817r0) this.f5146q).f12150y;
                C0817r0.k(z8);
                z8.f11875v.d("Database error", str, e8);
                throw e8;
            }
        } catch (Throwable th) {
            if (cursorRawQuery != null) {
            }
            throw th;
        }
        if (cursorRawQuery != null) {
            cursorRawQuery.close();
        }
        throw th;
    }

    public final void k0(String str, String str2) {
        P2.w.c(str2);
        E();
        F();
        try {
            A0().delete(str, "app_id=?", new String[]{str2});
        } catch (SQLiteException e8) {
            Z z8 = ((C0817r0) this.f5146q).f12150y;
            C0817r0.k(z8);
            z8.f11875v.d("Error deleting snapshot. appId", Z.M(str2), e8);
        }
    }

    public final void l0(String str, C0819s c0819s) {
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.f(c0819s);
        E();
        F();
        ContentValues contentValues = new ContentValues();
        String str2 = c0819s.f12155a;
        contentValues.put("app_id", str2);
        contentValues.put("name", c0819s.f12156b);
        contentValues.put("lifetime_count", Long.valueOf(c0819s.f12157c));
        contentValues.put("current_bundle_count", Long.valueOf(c0819s.f12158d));
        contentValues.put("last_fire_timestamp", Long.valueOf(c0819s.f12160f));
        contentValues.put("last_bundled_timestamp", Long.valueOf(c0819s.f12161g));
        contentValues.put("last_bundled_day", c0819s.f12162h);
        contentValues.put("last_sampled_complex_event_id", c0819s.i);
        contentValues.put("last_sampling_rate", c0819s.f12163j);
        contentValues.put("current_session_count", Long.valueOf(c0819s.f12159e));
        Boolean bool = c0819s.f12164k;
        contentValues.put("last_exempt_from_sampling", (bool == null || !bool.booleanValue()) ? null : 1L);
        try {
            if (A0().insertWithOnConflict(str, null, contentValues, 5) == -1) {
                Z z8 = c0817r0.f12150y;
                C0817r0.k(z8);
                z8.f11875v.c(Z.M(str2), "Failed to insert/update event aggregates (got -1). appId");
            }
        } catch (SQLiteException e8) {
            Z z9 = c0817r0.f12150y;
            C0817r0.k(z9);
            z9.f11875v.d("Error storing event aggregates. appId", Z.M(str2), e8);
        }
    }

    public final void m0(ContentValues contentValues) {
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        try {
            SQLiteDatabase sQLiteDatabaseA0 = A0();
            if (contentValues.getAsString("app_id") == null) {
                Z z8 = c0817r0.f12150y;
                C0817r0.k(z8);
                z8.f11877x.c(Z.M("app_id"), "Value of the primary key is not set.");
            } else if (sQLiteDatabaseA0.update("consent_settings", contentValues, "app_id = ?", new String[]{r4}) == 0 && sQLiteDatabaseA0.insertWithOnConflict("consent_settings", null, contentValues, 5) == -1) {
                Z z9 = c0817r0.f12150y;
                C0817r0.k(z9);
                z9.f11875v.d("Failed to insert/update table (got -1). key", Z.M("consent_settings"), Z.M("app_id"));
            }
        } catch (SQLiteException e8) {
            Z z10 = c0817r0.f12150y;
            C0817r0.k(z10);
            z10.f11875v.e("Error storing into table. key", Z.M("consent_settings"), Z.M("app_id"), e8);
        }
    }

    public final void o0(String str, E1 e12) {
        E();
        F();
        P2.w.c(str);
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        c0817r0.D.getClass();
        long jCurrentTimeMillis = System.currentTimeMillis();
        H h8 = I.f11592v0;
        long jLongValue = jCurrentTimeMillis - ((Long) h8.a(null)).longValue();
        long j8 = e12.f11454r;
        Z z8 = c0817r0.f12150y;
        if (j8 < jLongValue || j8 > ((Long) h8.a(null)).longValue() + jCurrentTimeMillis) {
            C0817r0.k(z8);
            z8.f11878y.e("Storing trigger URI outside of the max retention time span. appId, now, timestamp", Z.M(str), Long.valueOf(jCurrentTimeMillis), Long.valueOf(j8));
        }
        C0817r0.k(z8);
        z8.D.b("Saving trigger URI");
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id", str);
        contentValues.put("trigger_uri", e12.f11453q);
        contentValues.put("source", Integer.valueOf(e12.f11455s));
        contentValues.put("timestamp_millis", Long.valueOf(j8));
        try {
            if (A0().insert("trigger_uris", null, contentValues) == -1) {
                C0817r0.k(z8);
                z8.f11875v.c(Z.M(str), "Failed to insert trigger URI (got -1). appId");
            }
        } catch (SQLiteException e8) {
            C0817r0.k(z8);
            z8.f11875v.d("Error storing trigger URI. appId", Z.M(str), e8);
        }
    }

    public final boolean p0() {
        return ((C0817r0) this.f5146q).f12142q.getDatabasePath("google_app_measurement.db").exists();
    }

    public final void q0(String str, Long l8, long j8, com.google.android.gms.internal.measurement.W0 w02) {
        E();
        F();
        P2.w.f(w02);
        P2.w.c(str);
        byte[] bArrC = w02.c();
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        Z z8 = c0817r0.f12150y;
        Z z9 = c0817r0.f12150y;
        C0817r0.k(z8);
        z8.D.d("Saving complex main event, appId, data size", c0817r0.f12123C.d(str), Integer.valueOf(bArrC.length));
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id", str);
        contentValues.put("event_id", l8);
        contentValues.put("children_to_process", Long.valueOf(j8));
        contentValues.put("main_event", bArrC);
        try {
            if (A0().insertWithOnConflict("main_event_params", null, contentValues, 5) == -1) {
                C0817r0.k(z9);
                z9.f11875v.c(Z.M(str), "Failed to insert complex main event (got -1). appId");
            }
        } catch (SQLiteException e8) {
            C0817r0.k(z9);
            z9.f11875v.d("Error storing complex main event. appId", Z.M(str), e8);
        }
    }

    public final boolean r0(C0778e c0778e) {
        E();
        F();
        String str = c0778e.f11918q;
        P2.w.f(str);
        U1 u1K0 = K0(str, c0778e.f11920s.f11723r);
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        if (u1K0 == null) {
            long jV0 = v0("SELECT COUNT(1) FROM conditional_properties WHERE app_id=?", new String[]{str});
            c0817r0.getClass();
            if (jV0 >= 1000) {
                return false;
            }
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id", str);
        contentValues.put("origin", c0778e.f11919r);
        contentValues.put("name", c0778e.f11920s.f11723r);
        Object objA = c0778e.f11920s.a();
        P2.w.f(objA);
        u0(contentValues, objA);
        contentValues.put("active", Boolean.valueOf(c0778e.f11922u));
        contentValues.put("trigger_event_name", c0778e.f11923v);
        contentValues.put("trigger_timeout", Long.valueOf(c0778e.f11925x));
        W1 w12 = c0817r0.f12122B;
        Z z8 = c0817r0.f12150y;
        C0817r0.i(w12);
        contentValues.put("timed_out_event", W1.F0(c0778e.f11924w));
        contentValues.put("creation_timestamp", Long.valueOf(c0778e.f11921t));
        C0817r0.i(c0817r0.f12122B);
        contentValues.put("triggered_event", W1.F0(c0778e.f11926y));
        contentValues.put("triggered_timestamp", Long.valueOf(c0778e.f11920s.f11724s));
        contentValues.put("time_to_live", Long.valueOf(c0778e.f11927z));
        contentValues.put("expired_event", W1.F0(c0778e.f11917A));
        try {
            if (A0().insertWithOnConflict("conditional_properties", null, contentValues, 5) != -1) {
                return true;
            }
            C0817r0.k(z8);
            z8.f11875v.c(Z.M(str), "Failed to insert/update conditional user property (got -1)");
            return true;
        } catch (SQLiteException e8) {
            C0817r0.k(z8);
            z8.f11875v.d("Error storing conditional user property", Z.M(str), e8);
            return true;
        }
    }

    public final boolean s0(U1 u12) {
        E();
        F();
        String str = u12.f11735a;
        String str2 = u12.f11737c;
        U1 u1K0 = K0(str, str2);
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        String str3 = u12.f11736b;
        if (u1K0 == null) {
            if (W1.z0(str2)) {
                if (v0("select count(1) from user_attributes where app_id=? and name not like '!_%' escape '!'", new String[]{str}) >= Math.max(Math.min(c0817r0.f12148w.K(str, I.f11526V), 100), 25)) {
                    return false;
                }
            } else if (!"_npa".equals(str2)) {
                long jV0 = v0("select count(1) from user_attributes where app_id=? and origin=? AND name like '!_%' escape '!'", new String[]{str, str3});
                c0817r0.getClass();
                if (jV0 >= 25) {
                    return false;
                }
            }
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id", str);
        contentValues.put("origin", str3);
        contentValues.put("name", str2);
        contentValues.put("set_timestamp", Long.valueOf(u12.f11738d));
        u0(contentValues, u12.f11739e);
        try {
            if (A0().insertWithOnConflict("user_attributes", null, contentValues, 5) != -1) {
                return true;
            }
            Z z8 = c0817r0.f12150y;
            C0817r0.k(z8);
            z8.f11875v.c(Z.M(str), "Failed to insert/update user property (got -1). appId");
            return true;
        } catch (SQLiteException e8) {
            Z z9 = c0817r0.f12150y;
            C0817r0.k(z9);
            z9.f11875v.d("Error storing user property. appId", Z.M(str), e8);
            return true;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:42:0x00dd A[Catch: all -> 0x006f, SQLiteException -> 0x0072, TryCatch #3 {SQLiteException -> 0x0072, blocks: (B:19:0x0067, B:40:0x00b8, B:42:0x00dd, B:43:0x00ef, B:44:0x00f3, B:45:0x0103, B:47:0x0109, B:48:0x0119, B:50:0x0122, B:53:0x0136, B:55:0x0153, B:56:0x015c, B:58:0x0167, B:63:0x01a3, B:62:0x0192, B:66:0x01aa, B:52:0x012f, B:68:0x01bc), top: B:84:0x0067 }] */
    /* JADX WARN: Removed duplicated region for block: B:43:0x00ef A[Catch: all -> 0x006f, SQLiteException -> 0x0072, TRY_LEAVE, TryCatch #3 {SQLiteException -> 0x0072, blocks: (B:19:0x0067, B:40:0x00b8, B:42:0x00dd, B:43:0x00ef, B:44:0x00f3, B:45:0x0103, B:47:0x0109, B:48:0x0119, B:50:0x0122, B:53:0x0136, B:55:0x0153, B:56:0x015c, B:58:0x0167, B:63:0x01a3, B:62:0x0192, B:66:0x01aa, B:52:0x012f, B:68:0x01bc), top: B:84:0x0067 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void t0(String str, long j8, long j9, C0782f0 c0782f0) {
        String str2;
        String string;
        SQLiteDatabase sQLiteDatabaseA0;
        String[] strArr;
        String string2;
        String str3;
        String[] strArr2;
        com.google.android.gms.internal.measurement.V0 v02;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        E();
        F();
        Cursor cursorRawQuery = null;
        try {
            try {
                sQLiteDatabaseA0 = A0();
            } catch (SQLiteException e8) {
                e = e8;
                str2 = str;
            }
            if (TextUtils.isEmpty(str)) {
                cursorRawQuery = sQLiteDatabaseA0.rawQuery("select app_id, metadata_fingerprint from raw_events where " + (j9 != -1 ? "rowid <= ? and " : "") + "app_id in (select app_id from apps where config_fetched_time >= ?) order by rowid limit 1;", j9 != -1 ? new String[]{String.valueOf(j9), String.valueOf(j8)} : new String[]{String.valueOf(j8)});
                try {
                    if (cursorRawQuery.moveToFirst()) {
                        string = cursorRawQuery.getString(0);
                        try {
                            string2 = cursorRawQuery.getString(1);
                            cursorRawQuery.close();
                            cursorRawQuery = sQLiteDatabaseA0.query("raw_events_metadata", new String[]{"metadata"}, "app_id = ? and metadata_fingerprint = ?", new String[]{string, string2}, null, null, "rowid", "2");
                            if (cursorRawQuery.moveToFirst()) {
                                Z z8 = c0817r0.f12150y;
                                C0817r0.k(z8);
                                z8.f11875v.c(Z.M(string), "Raw event metadata record is missing. appId");
                            } else {
                                try {
                                    C0628e1 c0628e1 = (C0628e1) ((C0623d1) C0773c0.k0(C0628e1.k2(), cursorRawQuery.getBlob(0))).h();
                                    if (cursorRawQuery.moveToNext()) {
                                        Z z9 = c0817r0.f12150y;
                                        C0817r0.k(z9);
                                        z9.f11878y.c(Z.M(string), "Get multiple raw event metadata records, expected one. appId");
                                    }
                                    cursorRawQuery.close();
                                    c0782f0.f11940b = c0628e1;
                                    if (j9 != -1) {
                                        str3 = "app_id = ? and metadata_fingerprint = ? and rowid <= ?";
                                        strArr2 = new String[]{string, string2, String.valueOf(j9)};
                                    } else {
                                        str3 = "app_id = ? and metadata_fingerprint = ?";
                                        strArr2 = new String[]{string, string2};
                                    }
                                    cursorRawQuery = sQLiteDatabaseA0.query("raw_events", new String[]{"rowid", "name", "timestamp", "data"}, str3, strArr2, null, null, "rowid", null);
                                    if (cursorRawQuery.moveToFirst()) {
                                        do {
                                            long j10 = cursorRawQuery.getLong(0);
                                            try {
                                                v02 = (com.google.android.gms.internal.measurement.V0) C0773c0.k0(com.google.android.gms.internal.measurement.W0.r(), cursorRawQuery.getBlob(3));
                                                String string3 = cursorRawQuery.getString(1);
                                                v02.k();
                                                com.google.android.gms.internal.measurement.W0.z((com.google.android.gms.internal.measurement.W0) v02.f9801r, string3);
                                                long j11 = cursorRawQuery.getLong(2);
                                                v02.k();
                                                com.google.android.gms.internal.measurement.W0.C(j11, (com.google.android.gms.internal.measurement.W0) v02.f9801r);
                                            } catch (IOException e9) {
                                                Z z10 = c0817r0.f12150y;
                                                C0817r0.k(z10);
                                                z10.f11875v.d("Data loss. Failed to merge raw event. appId", Z.M(string), e9);
                                            }
                                            if (!c0782f0.a(j10, (com.google.android.gms.internal.measurement.W0) v02.h())) {
                                                break;
                                            }
                                        } while (cursorRawQuery.moveToNext());
                                    } else {
                                        Z z11 = c0817r0.f12150y;
                                        C0817r0.k(z11);
                                        z11.f11878y.c(Z.M(string), "Raw event data disappeared while in transaction. appId");
                                    }
                                } catch (IOException e10) {
                                    Z z12 = c0817r0.f12150y;
                                    C0817r0.k(z12);
                                    z12.f11875v.d("Data loss. Failed to merge raw event metadata. appId", Z.M(string), e10);
                                }
                            }
                        } catch (SQLiteException e11) {
                            e = e11;
                            Z z13 = c0817r0.f12150y;
                            C0817r0.k(z13);
                            z13.f11875v.d("Data loss. Error selecting raw event. appId", Z.M(string), e);
                        }
                    }
                } catch (SQLiteException e12) {
                    e = e12;
                    string = str;
                }
                Z z132 = c0817r0.f12150y;
                C0817r0.k(z132);
                z132.f11875v.d("Data loss. Error selecting raw event. appId", Z.M(string), e);
            } else {
                try {
                    if (j9 != -1) {
                        str2 = str;
                        strArr = new String[]{str2, String.valueOf(j9)};
                    } else {
                        str2 = str;
                        strArr = new String[]{str2};
                    }
                    cursorRawQuery = sQLiteDatabaseA0.rawQuery("select metadata_fingerprint from raw_events where app_id = ?" + (j9 != -1 ? " and rowid <= ?" : "") + " order by rowid limit 1;", strArr);
                    if (cursorRawQuery.moveToFirst()) {
                        string2 = cursorRawQuery.getString(0);
                        cursorRawQuery.close();
                        string = str2;
                        cursorRawQuery = sQLiteDatabaseA0.query("raw_events_metadata", new String[]{"metadata"}, "app_id = ? and metadata_fingerprint = ?", new String[]{string, string2}, null, null, "rowid", "2");
                        if (cursorRawQuery.moveToFirst()) {
                        }
                    }
                } catch (SQLiteException e13) {
                    e = e13;
                    string = str2;
                    Z z1322 = c0817r0.f12150y;
                    C0817r0.k(z1322);
                    z1322.f11875v.d("Data loss. Error selecting raw event. appId", Z.M(string), e);
                }
            }
        } finally {
            if (0 != 0) {
                cursorRawQuery.close();
            }
        }
    }

    public final long v0(String str, String[] strArr) {
        Cursor cursor = null;
        try {
            try {
                Cursor cursorRawQuery = A0().rawQuery(str, strArr);
                if (!cursorRawQuery.moveToFirst()) {
                    throw new SQLiteException("Database returned empty set");
                }
                long j8 = cursorRawQuery.getLong(0);
                cursorRawQuery.close();
                return j8;
            } catch (SQLiteException e8) {
                Z z8 = ((C0817r0) this.f5146q).f12150y;
                C0817r0.k(z8);
                z8.f11875v.d("Database error", str, e8);
                throw e8;
            }
        } catch (Throwable th) {
            if (0 != 0) {
                cursor.close();
            }
            throw th;
        }
    }

    public final long w0(long j8, String str, String[] strArr) {
        Cursor cursorRawQuery = null;
        try {
            try {
                cursorRawQuery = A0().rawQuery(str, strArr);
                if (cursorRawQuery.moveToFirst()) {
                    j8 = cursorRawQuery.getLong(0);
                }
                cursorRawQuery.close();
                return j8;
            } catch (SQLiteException e8) {
                Z z8 = ((C0817r0) this.f5146q).f12150y;
                C0817r0.k(z8);
                z8.f11875v.d("Database error", str, e8);
                throw e8;
            }
        } catch (Throwable th) {
            if (cursorRawQuery != null) {
                cursorRawQuery.close();
            }
            throw th;
        }
    }

    public final long x0(String str, C0618c1 c0618c1, String str2, Map map, EnumC0783f1 enumC0783f1, Long l8) {
        long j8;
        int iDelete;
        E();
        F();
        P2.w.f(c0618c1);
        P2.w.c(str);
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        if (!c0817r0.f12148w.R(null, I.f11511N0)) {
            return -1L;
        }
        E();
        F();
        boolean zP0 = p0();
        T2.a aVar = c0817r0.D;
        Z z8 = c0817r0.f12150y;
        if (zP0) {
            Q1 q12 = this.f11473r;
            long jG = q12.f11689y.f12205v.g();
            aVar.getClass();
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            j8 = -1;
            if (Math.abs(jElapsedRealtime - jG) > ((Long) I.f11508M.a(null)).longValue()) {
                q12.f11689y.f12205v.h(jElapsedRealtime);
                E();
                F();
                if (p0() && (iDelete = A0().delete("upload_queue", i0(), new String[0])) > 0) {
                    C0817r0.k(z8);
                    z8.D.c(Integer.valueOf(iDelete), "Deleted stale MeasurementBatch rows from upload_queue. rowsDeleted");
                }
                H h8 = I.f11517Q0;
                C0784g c0784g = c0817r0.f12148w;
                if (c0784g.R(null, h8)) {
                    P2.w.c(str);
                    E();
                    F();
                    try {
                        int iK = c0784g.K(str, I.f11487A);
                        if (iK > 0) {
                            A0().delete("upload_queue", "rowid in (SELECT rowid FROM upload_queue WHERE app_id=? ORDER BY rowid DESC LIMIT -1 OFFSET ?)", new String[]{str, String.valueOf(iK)});
                        }
                    } catch (SQLiteException e8) {
                        C0817r0.k(z8);
                        z8.f11875v.d("Error deleting over the limit queued batches. appId", Z.M(str), e8);
                    }
                }
            }
        } else {
            j8 = -1;
        }
        ArrayList arrayList = new ArrayList();
        for (Map.Entry entry : map.entrySet()) {
            arrayList.add(((String) entry.getKey()) + "=" + ((String) entry.getValue()));
        }
        byte[] bArrC = c0618c1.c();
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id", str);
        contentValues.put("measurement_batch", bArrC);
        contentValues.put("upload_uri", str2);
        StringBuilder sb = new StringBuilder();
        Iterator it = arrayList.iterator();
        if (it.hasNext()) {
            while (true) {
                sb.append((CharSequence) it.next());
                if (!it.hasNext()) {
                    break;
                }
                sb.append((CharSequence) "\r\n");
            }
        }
        contentValues.put("upload_headers", sb.toString());
        contentValues.put("upload_type", Integer.valueOf(enumC0783f1.f11951q));
        aVar.getClass();
        contentValues.put("creation_timestamp", Long.valueOf(System.currentTimeMillis()));
        contentValues.put("retry_count", (Integer) 0);
        if (l8 != null) {
            contentValues.put("associated_row_id", l8);
        }
        try {
            long jInsert = A0().insert("upload_queue", null, contentValues);
            if (jInsert != j8) {
                return jInsert;
            }
            C0817r0.k(z8);
            z8.f11875v.c(str, "Failed to insert MeasurementBatch (got -1) to upload_queue. appId");
            return j8;
        } catch (SQLiteException e9) {
            C0817r0.k(z8);
            z8.f11875v.d("Error storing MeasurementBatch to upload_queue. appId", str, e9);
            return j8;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0087 A[Catch: all -> 0x0060, SQLiteException -> 0x0098, TryCatch #0 {SQLiteException -> 0x0098, blocks: (B:15:0x0066, B:17:0x0087, B:20:0x009a), top: B:30:0x0066 }] */
    /* JADX WARN: Removed duplicated region for block: B:20:0x009a A[Catch: all -> 0x0060, SQLiteException -> 0x0098, TRY_LEAVE, TryCatch #0 {SQLiteException -> 0x0098, blocks: (B:15:0x0066, B:17:0x0087, B:20:0x009a), top: B:30:0x0066 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long y0(String str) {
        long j8;
        long jW0;
        ContentValues contentValues;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        P2.w.c(str);
        P2.w.c("first_open_count");
        E();
        F();
        SQLiteDatabase sQLiteDatabaseA0 = A0();
        sQLiteDatabaseA0.beginTransaction();
        long j9 = 0;
        try {
            try {
                j8 = -1;
                jW0 = w0(-1L, "select first_open_count from app2 where app_id=?", new String[]{str});
            } finally {
                sQLiteDatabaseA0.endTransaction();
            }
        } catch (SQLiteException e8) {
            e = e8;
        }
        if (jW0 == -1) {
            ContentValues contentValues2 = new ContentValues();
            contentValues2.put("app_id", str);
            contentValues2.put("first_open_count", (Integer) 0);
            contentValues2.put("previous_install_count", (Integer) 0);
            if (sQLiteDatabaseA0.insertWithOnConflict("app2", null, contentValues2, 5) == -1) {
                Z z8 = c0817r0.f12150y;
                C0817r0.k(z8);
                z8.f11875v.d("Failed to insert column (got -1). appId", Z.M(str), "first_open_count");
            } else {
                jW0 = 0;
                try {
                    contentValues = new ContentValues();
                    contentValues.put("app_id", str);
                    contentValues.put("first_open_count", Long.valueOf(1 + jW0));
                    if (sQLiteDatabaseA0.update("app2", contentValues, "app_id = ?", new String[]{str}) != 0) {
                        Z z9 = c0817r0.f12150y;
                        C0817r0.k(z9);
                        z9.f11875v.d("Failed to update column (got 0). appId", Z.M(str), "first_open_count");
                    } else {
                        sQLiteDatabaseA0.setTransactionSuccessful();
                        j8 = jW0;
                    }
                } catch (SQLiteException e9) {
                    e = e9;
                    j9 = jW0;
                    Z z10 = c0817r0.f12150y;
                    C0817r0.k(z10);
                    z10.f11875v.e("Error inserting column. appId", Z.M(str), "first_open_count", e);
                    j8 = j9;
                }
            }
            Z z102 = c0817r0.f12150y;
            C0817r0.k(z102);
            z102.f11875v.e("Error inserting column. appId", Z.M(str), "first_open_count", e);
            j8 = j9;
        } else {
            contentValues = new ContentValues();
            contentValues.put("app_id", str);
            contentValues.put("first_open_count", Long.valueOf(1 + jW0));
            if (sQLiteDatabaseA0.update("app2", contentValues, "app_id = ?", new String[]{str}) != 0) {
            }
        }
        return j8;
    }

    public final long z0(String str) {
        P2.w.c(str);
        return w0(0L, "select count(1) from events where app_id=? and name not like '!_%' escape '!'", new String[]{str});
    }

    @Override // d3.K1
    public final void H() {
    }
}
