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

import a5.h;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.google.android.gms.measurement.internal.n;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import xyz.stream.utils.glide.UnsafeOkHttpGlideModule;

/* JADX INFO: loaded from: classes2.dex */
public final class n implements d5.c {

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

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

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

    public n(k kVar, String str) {
        this.f21784c = kVar;
        this.f21782a = -1L;
        com.google.android.gms.common.internal.w.checkNotEmpty(str);
        this.f21783b = str;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:44:0x011b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final List a() {
        Cursor cursor;
        int i10;
        int i11;
        Cursor cursorQuery;
        Object obj = this.f21784c;
        ArrayList arrayList = new ArrayList();
        Object obj2 = this.f21783b;
        try {
            i10 = 0;
            i11 = 3;
            cursorQuery = ((k) obj).E().query("raw_events", new String[]{"rowid", "name", "timestamp", "metadata_fingerprint", "data", "realtime"}, "app_id = ? and rowid > ?", new String[]{(String) obj2, String.valueOf(this.f21782a)}, null, null, "rowid", "1000");
            try {
            } catch (SQLiteException e10) {
                e = e10;
                cursor = cursorQuery;
                try {
                    ((k) obj).zzj().f21834f.a(p0.A((String) obj2), e, "Data loss. Error querying raw events batch. appId");
                    if (cursor != null) {
                        cursor.close();
                    }
                } catch (Throwable th) {
                    th = th;
                    if (cursor != null) {
                        cursor.close();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
                cursor = cursorQuery;
                if (cursor != null) {
                }
                throw th;
            }
        } catch (SQLiteException e11) {
            e = e11;
            cursor = null;
        } catch (Throwable th3) {
            th = th3;
            cursor = null;
        }
        if (!cursorQuery.moveToFirst()) {
            List listEmptyList = Collections.emptyList();
            cursorQuery.close();
            return listEmptyList;
        }
        while (true) {
            long j10 = cursorQuery.getLong(i10);
            long j11 = cursorQuery.getLong(i11);
            boolean z10 = cursorQuery.getLong(5) == 1 ? 1 : i10;
            byte[] blob = cursorQuery.getBlob(4);
            if (j10 > this.f21782a) {
                this.f21782a = j10;
            }
            try {
                com.google.android.gms.internal.measurement.m3 m3Var = (com.google.android.gms.internal.measurement.m3) j4.K(com.google.android.gms.internal.measurement.n3.C(), blob);
                String string = cursorQuery.getString(1);
                if (string == null) {
                    string = "";
                }
                m3Var.e();
                com.google.android.gms.internal.measurement.n3.x((com.google.android.gms.internal.measurement.n3) m3Var.f20099b, string);
                long j12 = cursorQuery.getLong(2);
                m3Var.e();
                com.google.android.gms.internal.measurement.n3.z(j12, (com.google.android.gms.internal.measurement.n3) m3Var.f20099b);
                arrayList.add(new l(j10, j11, z10, (com.google.android.gms.internal.measurement.n3) m3Var.c()));
            } catch (IOException e12) {
                ((k) obj).zzj().f21834f.a(p0.A((String) obj2), e12, "Data loss. Failed to merge raw event. appId");
            }
            if (!cursorQuery.moveToNext()) {
                break;
            }
            i11 = 3;
            i10 = 0;
        }
        cursorQuery.close();
        return arrayList;
    }

    @Override // d5.c
    public final void f(final a5.h hVar, d5.b bVar) {
        boolean zIsLoggable = Log.isLoggable("GlideDiskCache", 3);
        Object obj = this.f21783b;
        if (!zIsLoggable) {
            ((d5.c) obj).f(hVar, bVar);
            return;
        }
        File file = (File) this.f21784c;
        final zf.b bVar2 = new zf.b();
        UnsafeOkHttpGlideModule.F(file, bVar2);
        long[] jArr = {0};
        ((d5.c) obj).f(hVar, new androidx.media3.exoplayer.trackselection.d((Object) new boolean[]{false}, (Serializable) new boolean[]{false}, (Object) bVar, (Object) jArr));
        final zf.b bVar3 = new zf.b();
        UnsafeOkHttpGlideModule.F(file, bVar3);
        long j10 = bVar3.f37194c;
        long j11 = bVar2.f37194c;
        long j12 = j10 - j11;
        long j13 = bVar3.f37192a - bVar2.f37192a;
        long jMax = Math.max(0L, (j11 + jArr[0]) - j10);
        Objects.toString(hVar);
        UnsafeOkHttpGlideModule.G(this.f21782a);
        bVar2.b();
        UnsafeOkHttpGlideModule.G(jArr[0]);
        bVar3.b();
        UnsafeOkHttpGlideModule.E(j12);
        UnsafeOkHttpGlideModule.G(jMax);
        bVar2.c();
        bVar3.c();
        UnsafeOkHttpGlideModule.E(j13);
        bVar3.a();
        file.getAbsolutePath();
        long j14 = bVar3.f37194c;
        long j15 = this.f21782a;
        if (j14 <= j15) {
            long j16 = bVar3.f37192a;
            if (j16 <= j15 && j14 >= bVar2.f37194c && j16 >= bVar2.f37192a) {
                return;
            }
        }
        Handler handler = new Handler(Looper.getMainLooper());
        long[] jArr2 = UnsafeOkHttpGlideModule.f36139a;
        for (int i10 = 0; i10 < 3; i10++) {
            final long j17 = jArr2[i10];
            handler.postDelayed(new Runnable(bVar3, hVar, j17, bVar2) { // from class: zf.c

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

                /* JADX INFO: renamed from: c, reason: collision with root package name */
                public final /* synthetic */ h f37204c;

                /* JADX INFO: renamed from: d, reason: collision with root package name */
                public final /* synthetic */ b f37205d;

                {
                    this.f37205d = bVar2;
                }

                @Override // java.lang.Runnable
                public final void run() {
                    n nVar = this.f37202a;
                    File file2 = (File) nVar.f21784c;
                    b bVar4 = new b();
                    UnsafeOkHttpGlideModule.F(file2, bVar4);
                    b bVar5 = this.f37203b;
                    long jMax2 = Math.max(0L, bVar5.f37194c - bVar4.f37194c);
                    long jMax3 = Math.max(0L, bVar5.f37192a - bVar4.f37192a);
                    Objects.toString(this.f37204c);
                    UnsafeOkHttpGlideModule.G(nVar.f21782a);
                    this.f37205d.b();
                    bVar5.b();
                    bVar4.b();
                    UnsafeOkHttpGlideModule.G(jMax2);
                    bVar5.c();
                    bVar4.c();
                    UnsafeOkHttpGlideModule.G(jMax3);
                    bVar4.a();
                    file2.getAbsolutePath();
                }
            }, j17);
        }
    }

    @Override // d5.c
    public final File get(a5.h hVar) {
        return ((d5.c) this.f21783b).get(hVar);
    }

    public n(x4.d dVar, File file, long j10) {
        this.f21783b = dVar;
        this.f21784c = file;
        this.f21782a = j10;
    }
}
