package d3;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteFullException;
import android.os.SystemClock;

/* JADX INFO: loaded from: classes.dex */
public final class S extends F {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final String[] f11702u = {"app_version", "ALTER TABLE messages ADD COLUMN app_version TEXT;", "app_version_int", "ALTER TABLE messages ADD COLUMN app_version_int INTEGER;"};

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

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

    public S(C0817r0 c0817r0) {
        super(c0817r0);
        this.f11703s = new C0802m(this, ((C0817r0) this.f5146q).f12142q);
    }

    @Override // d3.F
    public final boolean H() {
        return false;
    }

    public final SQLiteDatabase I() {
        if (this.f11704t) {
            return null;
        }
        SQLiteDatabase writableDatabase = this.f11703s.getWritableDatabase();
        if (writableDatabase != null) {
            return writableDatabase;
        }
        this.f11704t = true;
        return null;
    }

    public final void J() {
        int iDelete;
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        E();
        try {
            SQLiteDatabase sQLiteDatabaseI = I();
            if (sQLiteDatabaseI == null || (iDelete = sQLiteDatabaseI.delete("messages", null, null)) <= 0) {
                return;
            }
            Z z8 = c0817r0.f12150y;
            C0817r0.k(z8);
            z8.D.c(Integer.valueOf(iDelete), "Reset local analytics data. records");
        } catch (SQLiteException e8) {
            Z z9 = c0817r0.f12150y;
            C0817r0.k(z9);
            z9.f11875v.c(e8, "Error resetting local analytics data. error");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:32:0x0073 A[PHI: r4
      0x0073: PHI (r4v3 int) = (r4v1 int), (r4v1 int), (r4v4 int) binds: [B:28:0x006a, B:34:0x0083, B:31:0x0071] A[DONT_GENERATE, DONT_INLINE]] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void K() {
        E();
        if (this.f11704t) {
            return;
        }
        C0817r0 c0817r0 = (C0817r0) this.f5146q;
        if (c0817r0.f12142q.getDatabasePath("google_app_measurement_local.db").exists()) {
            int i = 5;
            for (int i5 = 0; i5 < 5; i5++) {
                SQLiteDatabase sQLiteDatabase = null;
                try {
                    SQLiteDatabase sQLiteDatabaseI = I();
                    if (sQLiteDatabaseI == null) {
                        this.f11704t = true;
                        return;
                    }
                    sQLiteDatabaseI.beginTransaction();
                    sQLiteDatabaseI.delete("messages", "type == ?", new String[]{Integer.toString(3)});
                    sQLiteDatabaseI.setTransactionSuccessful();
                    sQLiteDatabaseI.endTransaction();
                    sQLiteDatabaseI.close();
                    return;
                } catch (SQLiteDatabaseLockedException unused) {
                    SystemClock.sleep(i);
                    i += 20;
                    if (0 != 0) {
                        sQLiteDatabase.close();
                    }
                } catch (SQLiteFullException e8) {
                    Z z8 = c0817r0.f12150y;
                    C0817r0.k(z8);
                    z8.f11875v.c(e8, "Error deleting app launch break from local database");
                    this.f11704t = true;
                    if (0 != 0) {
                    }
                } catch (SQLiteException e9) {
                    if (0 != 0) {
                        try {
                            if (sQLiteDatabase.inTransaction()) {
                                sQLiteDatabase.endTransaction();
                            }
                        } catch (Throwable th) {
                            if (0 != 0) {
                                sQLiteDatabase.close();
                            }
                            throw th;
                        }
                    }
                    Z z9 = c0817r0.f12150y;
                    C0817r0.k(z9);
                    z9.f11875v.c(e9, "Error deleting app launch break from local database");
                    this.f11704t = true;
                    if (0 != 0) {
                        sQLiteDatabase.close();
                    }
                }
            }
            Z z10 = c0817r0.f12150y;
            C0817r0.k(z10);
            z10.f11878y.b("Error deleting app launch break from local database in reasonable time");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:120:0x0173 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:121:0x0173 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:123:0x0173 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:40:0x00a4 A[Catch: all -> 0x0094, SQLiteDatabaseLockedException -> 0x0097, SQLiteException -> 0x00f2, SQLiteFullException -> 0x00f6, TRY_ENTER, TRY_LEAVE, TryCatch #8 {all -> 0x0094, blocks: (B:31:0x0089, B:33:0x008f, B:40:0x00a4, B:42:0x00ab, B:44:0x00c8, B:46:0x00d3, B:61:0x00fd, B:75:0x0125, B:77:0x012b, B:78:0x012e, B:93:0x015e, B:85:0x0149), top: B:109:0x0089 }] */
    /* JADX WARN: Removed duplicated region for block: B:60:0x00fa  */
    /* JADX WARN: Removed duplicated region for block: B:63:0x0108  */
    /* JADX WARN: Removed duplicated region for block: B:75:0x0125 A[Catch: all -> 0x0094, TRY_ENTER, TryCatch #8 {all -> 0x0094, blocks: (B:31:0x0089, B:33:0x008f, B:40:0x00a4, B:42:0x00ab, B:44:0x00c8, B:46:0x00d3, B:61:0x00fd, B:75:0x0125, B:77:0x012b, B:78:0x012e, B:93:0x015e, B:85:0x0149), top: B:109:0x0089 }] */
    /* JADX WARN: Removed duplicated region for block: B:80:0x013e  */
    /* JADX WARN: Removed duplicated region for block: B:88:0x0150  */
    /* JADX WARN: Removed duplicated region for block: B:90:0x0155 A[PHI: r8 r11 r16
      0x0155: PHI (r8v5 int) = (r8v3 int), (r8v3 int), (r8v6 int) binds: [B:81:0x0141, B:96:0x0170, B:89:0x0153] A[DONT_GENERATE, DONT_INLINE]
      0x0155: PHI (r11v7 android.database.sqlite.SQLiteDatabase) = 
      (r11v5 android.database.sqlite.SQLiteDatabase)
      (r11v6 android.database.sqlite.SQLiteDatabase)
      (r11v8 android.database.sqlite.SQLiteDatabase)
     binds: [B:81:0x0141, B:96:0x0170, B:89:0x0153] A[DONT_GENERATE, DONT_INLINE]
      0x0155: PHI (r16v7 boolean) = (r16v4 boolean), (r16v5 boolean), (r16v8 boolean) binds: [B:81:0x0141, B:96:0x0170, B:89:0x0153] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:95:0x016d  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean L(int i, byte[] bArr) {
        SQLiteDatabase sQLiteDatabaseI;
        boolean z8;
        boolean z9;
        Cursor cursorRawQuery;
        E();
        boolean z10 = false;
        z10 = false;
        if (!this.f11704t) {
            C0817r0 c0817r0 = (C0817r0) this.f5146q;
            C0784g c0784g = c0817r0.f12148w;
            H h8 = I.f11570m1;
            Cursor cursor = null;
            cursor = null;
            X1 x1I = c0784g.R(null, h8) ? c0817r0.n().I(null) : null;
            ContentValues contentValues = new ContentValues();
            contentValues.put("type", Integer.valueOf(i));
            contentValues.put("entry", bArr);
            if (c0817r0.f12148w.R(null, h8) && x1I != null) {
                contentValues.put("app_version", x1I.f11833s);
                contentValues.put("app_version_int", Long.valueOf(x1I.f11840z));
            }
            int i5 = 5;
            int i8 = 0;
            int i9 = 5;
            while (true) {
                Z z11 = c0817r0.f12150y;
                if (i8 >= i5) {
                    boolean z12 = z10 ? 1 : 0;
                    C0817r0.k(z11);
                    z11.D.b("Failed to write entry to local database");
                    return z12;
                }
                try {
                    sQLiteDatabaseI = I();
                } catch (SQLiteDatabaseLockedException unused) {
                    z8 = z10 ? 1 : 0;
                    sQLiteDatabaseI = null;
                    cursorRawQuery = null;
                } catch (SQLiteFullException e8) {
                    e = e8;
                    z8 = z10 ? 1 : 0;
                    sQLiteDatabaseI = null;
                    cursorRawQuery = null;
                } catch (SQLiteException e9) {
                    e = e9;
                    z8 = z10 ? 1 : 0;
                    z9 = true;
                    sQLiteDatabaseI = null;
                    cursorRawQuery = null;
                } catch (Throwable th) {
                    th = th;
                    sQLiteDatabaseI = null;
                }
                if (sQLiteDatabaseI == null) {
                    this.f11704t = true;
                    break;
                }
                try {
                    sQLiteDatabaseI.beginTransaction();
                    cursorRawQuery = sQLiteDatabaseI.rawQuery("select count(1) from messages", null);
                    long j8 = 0;
                    if (cursorRawQuery == null) {
                        if (j8 < 100000) {
                        }
                        z9 = true;
                        sQLiteDatabaseI.insertOrThrow("messages", null, contentValues);
                        sQLiteDatabaseI.setTransactionSuccessful();
                        sQLiteDatabaseI.endTransaction();
                        if (cursorRawQuery != null) {
                        }
                        sQLiteDatabaseI.close();
                        return z9;
                    }
                    try {
                        try {
                            if (cursorRawQuery.moveToFirst()) {
                                j8 = cursorRawQuery.getLong(z10 ? 1 : 0);
                            }
                            if (j8 < 100000) {
                                C0817r0.k(z11);
                                X x8 = z11.f11875v;
                                z8 = z10 ? 1 : 0;
                                try {
                                    try {
                                        x8.b("Data loss, local db full");
                                        long j9 = 100001 - j8;
                                        long jDelete = sQLiteDatabaseI.delete("messages", "rowid in (select rowid from messages order by rowid asc limit ?)", new String[]{Long.toString(j9)});
                                        if (jDelete != j9) {
                                            C0817r0.k(z11);
                                            z9 = true;
                                            try {
                                                x8.e("Different delete count than expected in local db. expected, received, difference", Long.valueOf(j9), Long.valueOf(jDelete), Long.valueOf(j9 - jDelete));
                                            } catch (SQLiteFullException e10) {
                                                e = e10;
                                                C0817r0.k(z11);
                                                z11.f11875v.c(e, "Error writing entry; local database full");
                                                this.f11704t = true;
                                                if (cursorRawQuery != null) {
                                                }
                                                if (sQLiteDatabaseI != null) {
                                                }
                                                i8++;
                                                z10 = z8;
                                                i5 = 5;
                                            } catch (SQLiteException e11) {
                                                e = e11;
                                                if (sQLiteDatabaseI != null) {
                                                    sQLiteDatabaseI.endTransaction();
                                                }
                                                C0817r0.k(z11);
                                                z11.f11875v.c(e, "Error writing entry to local database");
                                                this.f11704t = z9;
                                                if (cursorRawQuery != null) {
                                                }
                                                if (sQLiteDatabaseI != null) {
                                                }
                                                i8++;
                                                z10 = z8;
                                                i5 = 5;
                                            }
                                        }
                                        sQLiteDatabaseI.insertOrThrow("messages", null, contentValues);
                                        sQLiteDatabaseI.setTransactionSuccessful();
                                        sQLiteDatabaseI.endTransaction();
                                        if (cursorRawQuery != null) {
                                            cursorRawQuery.close();
                                        }
                                        sQLiteDatabaseI.close();
                                        return z9;
                                    } catch (SQLiteDatabaseLockedException unused2) {
                                        SystemClock.sleep(i9);
                                        i9 += 20;
                                        if (cursorRawQuery != null) {
                                            cursorRawQuery.close();
                                        }
                                        if (sQLiteDatabaseI == null) {
                                            sQLiteDatabaseI.close();
                                        }
                                        i8++;
                                        z10 = z8;
                                        i5 = 5;
                                    }
                                } catch (SQLiteFullException e12) {
                                    e = e12;
                                    C0817r0.k(z11);
                                    z11.f11875v.c(e, "Error writing entry; local database full");
                                    this.f11704t = true;
                                    if (cursorRawQuery != null) {
                                        cursorRawQuery.close();
                                    }
                                    if (sQLiteDatabaseI != null) {
                                    }
                                    i8++;
                                    z10 = z8;
                                    i5 = 5;
                                } catch (SQLiteException e13) {
                                    e = e13;
                                    z9 = true;
                                    if (sQLiteDatabaseI != null && sQLiteDatabaseI.inTransaction()) {
                                        sQLiteDatabaseI.endTransaction();
                                    }
                                    C0817r0.k(z11);
                                    z11.f11875v.c(e, "Error writing entry to local database");
                                    this.f11704t = z9;
                                    if (cursorRawQuery != null) {
                                        cursorRawQuery.close();
                                    }
                                    if (sQLiteDatabaseI != null) {
                                    }
                                    i8++;
                                    z10 = z8;
                                    i5 = 5;
                                }
                            } else {
                                z8 = z10 ? 1 : 0;
                            }
                            z9 = true;
                            sQLiteDatabaseI.insertOrThrow("messages", null, contentValues);
                            sQLiteDatabaseI.setTransactionSuccessful();
                            sQLiteDatabaseI.endTransaction();
                            if (cursorRawQuery != null) {
                            }
                            sQLiteDatabaseI.close();
                            return z9;
                        } catch (Throwable th2) {
                            th = th2;
                            cursor = cursorRawQuery;
                            if (cursor != null) {
                                cursor.close();
                            }
                            if (sQLiteDatabaseI != null) {
                                sQLiteDatabaseI.close();
                            }
                            throw th;
                        }
                    } catch (SQLiteDatabaseLockedException unused3) {
                        z8 = z10 ? 1 : 0;
                        SystemClock.sleep(i9);
                        i9 += 20;
                        if (cursorRawQuery != null) {
                        }
                        if (sQLiteDatabaseI == null) {
                        }
                        i8++;
                        z10 = z8;
                        i5 = 5;
                    } catch (SQLiteFullException e14) {
                        e = e14;
                        z8 = z10 ? 1 : 0;
                        C0817r0.k(z11);
                        z11.f11875v.c(e, "Error writing entry; local database full");
                        this.f11704t = true;
                        if (cursorRawQuery != null) {
                        }
                        if (sQLiteDatabaseI != null) {
                        }
                        i8++;
                        z10 = z8;
                        i5 = 5;
                    } catch (SQLiteException e15) {
                        e = e15;
                        z8 = z10 ? 1 : 0;
                        z9 = true;
                        if (sQLiteDatabaseI != null) {
                        }
                        C0817r0.k(z11);
                        z11.f11875v.c(e, "Error writing entry to local database");
                        this.f11704t = z9;
                        if (cursorRawQuery != null) {
                        }
                        if (sQLiteDatabaseI != null) {
                        }
                        i8++;
                        z10 = z8;
                        i5 = 5;
                    }
                } catch (SQLiteDatabaseLockedException unused4) {
                    z8 = z10 ? 1 : 0;
                    cursorRawQuery = null;
                } catch (SQLiteFullException e16) {
                    e = e16;
                    z8 = z10 ? 1 : 0;
                    cursorRawQuery = null;
                } catch (SQLiteException e17) {
                    e = e17;
                    z8 = z10 ? 1 : 0;
                    z9 = true;
                    cursorRawQuery = null;
                } catch (Throwable th3) {
                    th = th3;
                }
                i8++;
                z10 = z8;
                i5 = 5;
            }
        }
        return z10;
    }
}
