package defpackage;

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: renamed from: ᵱˈʒ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C4421 extends AbstractC2191 {

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public static final String[] f16821 = {"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: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final C3760 f16822;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public boolean f16823;

    public C4421(C1103 c1103) {
        super(c1103);
        this.f16822 = new C3760(this, ((C1103) ((ᵜᵪʾ) this).ˌᴵ).f4713);
    }

    /* JADX INFO: renamed from: ʿٴˇ, reason: contains not printable characters */
    public final void m9433() {
        int iDelete;
        C1103 c1103 = (C1103) ((ᵜᵪʾ) this).ˌᴵ;
        m6166();
        try {
            SQLiteDatabase sQLiteDatabaseM9435 = m9435();
            if (sQLiteDatabaseM9435 == null || (iDelete = sQLiteDatabaseM9435.delete("messages", null, null)) <= 0) {
                return;
            }
            C3485 c3485 = c1103.f4729;
            C1103.m3564(c3485);
            c3485.f13414.m4474(Integer.valueOf(iDelete), "Reset local analytics data. records");
        } catch (SQLiteException e) {
            C3485 c34852 = c1103.f4729;
            C1103.m3564(c34852);
            c34852.f13423.m4474(e, "Error resetting local analytics data. error");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:120:0x0170 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:121:0x0170 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:123:0x0170 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:43:0x00ae A[Catch: SQLiteException -> 0x0093, SQLiteDatabaseLockedException -> 0x009a, SQLiteFullException -> 0x009e, all -> 0x0154, TRY_ENTER, TryCatch #10 {all -> 0x0154, blocks: (B:30:0x0088, B:32:0x008e, B:43:0x00ae, B:45:0x00cf, B:47:0x00d8, B:49:0x00de, B:59:0x00f8, B:73:0x0120, B:75:0x0126, B:76:0x0129, B:93:0x015b, B:83:0x0144), top: B:109:0x0088 }] */
    /* JADX WARN: Removed duplicated region for block: B:58:0x00f4  */
    /* JADX WARN: Removed duplicated region for block: B:61:0x0103  */
    /* JADX WARN: Removed duplicated region for block: B:73:0x0120 A[Catch: all -> 0x0154, TRY_ENTER, TryCatch #10 {all -> 0x0154, blocks: (B:30:0x0088, B:32:0x008e, B:43:0x00ae, B:45:0x00cf, B:47:0x00d8, B:49:0x00de, B:59:0x00f8, B:73:0x0120, B:75:0x0126, B:76:0x0129, B:93:0x015b, B:83:0x0144), top: B:109:0x0088 }] */
    /* JADX WARN: Removed duplicated region for block: B:78:0x0139  */
    /* JADX WARN: Removed duplicated region for block: B:86:0x014b  */
    /* JADX WARN: Removed duplicated region for block: B:88:0x0150 A[PHI: r8 r10 r17
      0x0150: PHI (r8v5 int) = (r8v3 int), (r8v3 int), (r8v6 int) binds: [B:79:0x013c, B:96:0x016d, B:87:0x014e] A[DONT_GENERATE, DONT_INLINE]
      0x0150: PHI (r10v7 android.database.sqlite.SQLiteDatabase) = 
      (r10v5 android.database.sqlite.SQLiteDatabase)
      (r10v6 android.database.sqlite.SQLiteDatabase)
      (r10v8 android.database.sqlite.SQLiteDatabase)
     binds: [B:79:0x013c, B:96:0x016d, B:87:0x014e] A[DONT_GENERATE, DONT_INLINE]
      0x0150: PHI (r17v7 boolean) = (r17v4 boolean), (r17v5 boolean), (r17v8 boolean) binds: [B:79:0x013c, B:96:0x016d, B:87:0x014e] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:95:0x016a  */
    /* JADX INFO: renamed from: ˁˌᵩ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean m9434(int i, byte[] bArr) {
        SQLiteDatabase sQLiteDatabaseM9435;
        boolean z;
        boolean z2;
        Cursor cursorRawQuery;
        C1103 c1103 = (C1103) ((ᵜᵪʾ) this).ˌᴵ;
        m6166();
        boolean z3 = false;
        z3 = false;
        if (!this.f16823) {
            C3691 c3691 = c1103.f4726;
            C3485 c3485 = c1103.f4729;
            C0511 c0511 = AbstractC3818.f14493;
            Cursor cursor = null;
            cursor = null;
            C2847 c2847M5930 = c3691.m8314(null, c0511) ? c1103.m3569().m5930(null) : null;
            ContentValues contentValues = new ContentValues();
            contentValues.put("type", Integer.valueOf(i));
            contentValues.put("entry", bArr);
            if (c1103.f4726.m8314(null, c0511) && c2847M5930 != null) {
                contentValues.put("app_version", c2847M5930.f10915);
                contentValues.put("app_version_int", Long.valueOf(c2847M5930.f10914));
            }
            int i2 = 5;
            int i3 = 0;
            for (int i4 = 5; i3 < i4; i4 = 5) {
                try {
                    sQLiteDatabaseM9435 = m9435();
                } catch (SQLiteDatabaseLockedException unused) {
                    z = z3 ? 1 : 0;
                    sQLiteDatabaseM9435 = null;
                    cursorRawQuery = null;
                } catch (SQLiteFullException e) {
                    e = e;
                    z = z3 ? 1 : 0;
                    sQLiteDatabaseM9435 = null;
                    cursorRawQuery = null;
                } catch (SQLiteException e2) {
                    e = e2;
                    z = z3 ? 1 : 0;
                    z2 = true;
                    sQLiteDatabaseM9435 = null;
                    cursorRawQuery = null;
                } catch (Throwable th) {
                    th = th;
                    sQLiteDatabaseM9435 = null;
                }
                if (sQLiteDatabaseM9435 == null) {
                    this.f16823 = true;
                } else {
                    try {
                        sQLiteDatabaseM9435.beginTransaction();
                        cursorRawQuery = sQLiteDatabaseM9435.rawQuery("select count(1) from messages", null);
                        long j = 0;
                        if (cursorRawQuery == null) {
                            if (j < 100000) {
                            }
                            sQLiteDatabaseM9435.insertOrThrow("messages", null, contentValues);
                            sQLiteDatabaseM9435.setTransactionSuccessful();
                            sQLiteDatabaseM9435.endTransaction();
                            if (cursorRawQuery != null) {
                            }
                            sQLiteDatabaseM9435.close();
                            return z2;
                        }
                        try {
                            try {
                                if (cursorRawQuery.moveToFirst()) {
                                    j = cursorRawQuery.getLong(z3 ? 1 : 0);
                                }
                                if (j < 100000) {
                                    C1103.m3564(c3485);
                                    c3485.f13423.m4476("Data loss, local db full");
                                    long j2 = 100001 - j;
                                    long jDelete = sQLiteDatabaseM9435.delete("messages", "rowid in (select rowid from messages order by rowid asc limit ?)", new String[]{Long.toString(j2)});
                                    if (jDelete != j2) {
                                        C1103.m3564(c3485);
                                        C1623 c1623 = c3485.f13423;
                                        z = z3 ? 1 : 0;
                                        try {
                                            try {
                                                z2 = true;
                                            } catch (SQLiteDatabaseLockedException unused2) {
                                                SystemClock.sleep(i2);
                                                i2 += 20;
                                                if (cursorRawQuery != null) {
                                                    cursorRawQuery.close();
                                                }
                                                if (sQLiteDatabaseM9435 == null) {
                                                    sQLiteDatabaseM9435.close();
                                                }
                                                i3++;
                                                z3 = z;
                                            }
                                        } catch (SQLiteFullException e3) {
                                            e = e3;
                                            C1103.m3564(c3485);
                                            c3485.f13423.m4474(e, "Error writing entry; local database full");
                                            this.f16823 = true;
                                            if (cursorRawQuery != null) {
                                                cursorRawQuery.close();
                                            }
                                            if (sQLiteDatabaseM9435 != null) {
                                            }
                                            i3++;
                                            z3 = z;
                                        } catch (SQLiteException e4) {
                                            e = e4;
                                            z2 = true;
                                            if (sQLiteDatabaseM9435 != null && sQLiteDatabaseM9435.inTransaction()) {
                                                sQLiteDatabaseM9435.endTransaction();
                                            }
                                            C1103.m3564(c3485);
                                            c3485.f13423.m4474(e, "Error writing entry to local database");
                                            this.f16823 = z2;
                                            if (cursorRawQuery != null) {
                                                cursorRawQuery.close();
                                            }
                                            if (sQLiteDatabaseM9435 != null) {
                                            }
                                            i3++;
                                            z3 = z;
                                        }
                                        try {
                                            c1623.m4477("Different delete count than expected in local db. expected, received, difference", Long.valueOf(j2), Long.valueOf(jDelete), Long.valueOf(j2 - jDelete));
                                        } catch (SQLiteFullException e5) {
                                            e = e5;
                                            C1103.m3564(c3485);
                                            c3485.f13423.m4474(e, "Error writing entry; local database full");
                                            this.f16823 = true;
                                            if (cursorRawQuery != null) {
                                            }
                                            if (sQLiteDatabaseM9435 != null) {
                                            }
                                            i3++;
                                            z3 = z;
                                        } catch (SQLiteException e6) {
                                            e = e6;
                                            if (sQLiteDatabaseM9435 != null) {
                                                sQLiteDatabaseM9435.endTransaction();
                                            }
                                            C1103.m3564(c3485);
                                            c3485.f13423.m4474(e, "Error writing entry to local database");
                                            this.f16823 = z2;
                                            if (cursorRawQuery != null) {
                                            }
                                            if (sQLiteDatabaseM9435 != null) {
                                            }
                                            i3++;
                                            z3 = z;
                                        }
                                    } else {
                                        z = z3 ? 1 : 0;
                                        z2 = true;
                                    }
                                }
                                sQLiteDatabaseM9435.insertOrThrow("messages", null, contentValues);
                                sQLiteDatabaseM9435.setTransactionSuccessful();
                                sQLiteDatabaseM9435.endTransaction();
                                if (cursorRawQuery != null) {
                                    cursorRawQuery.close();
                                }
                                sQLiteDatabaseM9435.close();
                                return z2;
                            } catch (Throwable th2) {
                                th = th2;
                                cursor = cursorRawQuery;
                                if (cursor != null) {
                                    cursor.close();
                                }
                                if (sQLiteDatabaseM9435 != null) {
                                    sQLiteDatabaseM9435.close();
                                }
                                throw th;
                            }
                        } catch (SQLiteDatabaseLockedException unused3) {
                            z = z3 ? 1 : 0;
                            SystemClock.sleep(i2);
                            i2 += 20;
                            if (cursorRawQuery != null) {
                            }
                            if (sQLiteDatabaseM9435 == null) {
                            }
                            i3++;
                            z3 = z;
                        } catch (SQLiteFullException e7) {
                            e = e7;
                            z = z3 ? 1 : 0;
                            C1103.m3564(c3485);
                            c3485.f13423.m4474(e, "Error writing entry; local database full");
                            this.f16823 = true;
                            if (cursorRawQuery != null) {
                            }
                            if (sQLiteDatabaseM9435 != null) {
                            }
                            i3++;
                            z3 = z;
                        } catch (SQLiteException e8) {
                            e = e8;
                            z = z3 ? 1 : 0;
                            z2 = true;
                            if (sQLiteDatabaseM9435 != null) {
                            }
                            C1103.m3564(c3485);
                            c3485.f13423.m4474(e, "Error writing entry to local database");
                            this.f16823 = z2;
                            if (cursorRawQuery != null) {
                            }
                            if (sQLiteDatabaseM9435 != null) {
                            }
                            i3++;
                            z3 = z;
                        }
                    } catch (SQLiteDatabaseLockedException unused4) {
                        z = z3 ? 1 : 0;
                        cursorRawQuery = null;
                    } catch (SQLiteFullException e9) {
                        e = e9;
                        z = z3 ? 1 : 0;
                        cursorRawQuery = null;
                    } catch (SQLiteException e10) {
                        e = e10;
                        z = z3 ? 1 : 0;
                        z2 = true;
                        cursorRawQuery = null;
                    } catch (Throwable th3) {
                        th = th3;
                    }
                    i3++;
                    z3 = z;
                }
            }
            boolean z4 = z3 ? 1 : 0;
            C1103.m3564(c3485);
            c3485.f13414.m4476("Failed to write entry to local database");
            return z4;
        }
        return z3;
    }

    /* JADX INFO: renamed from: ˉᵵᵜ, reason: contains not printable characters */
    public final SQLiteDatabase m9435() {
        if (this.f16823) {
            return null;
        }
        SQLiteDatabase writableDatabase = this.f16822.getWritableDatabase();
        if (writableDatabase != null) {
            return writableDatabase;
        }
        this.f16823 = true;
        return null;
    }

    @Override // defpackage.AbstractC2191
    /* JADX INFO: renamed from: ˑˊᵲ */
    public final boolean mo4346() {
        return false;
    }

    /* JADX WARN: Removed duplicated region for block: B:29:0x006f A[PHI: r4
      0x006f: PHI (r4v4 int) = (r4v1 int), (r4v2 int), (r4v1 int) binds: [B:32:0x0080, B:28:0x006d, B:25:0x0066] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX INFO: renamed from: ᵠˏˏ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void m9436() {
        C1103 c1103 = (C1103) ((ᵜᵪʾ) this).ˌᴵ;
        m6166();
        if (!this.f16823 && c1103.f4713.getDatabasePath("google_app_measurement_local.db").exists()) {
            int i = 5;
            for (int i2 = 0; i2 < 5; i2++) {
                SQLiteDatabase sQLiteDatabase = null;
                try {
                    try {
                        SQLiteDatabase sQLiteDatabaseM9435 = m9435();
                        if (sQLiteDatabaseM9435 == null) {
                            this.f16823 = true;
                            return;
                        }
                        sQLiteDatabaseM9435.beginTransaction();
                        sQLiteDatabaseM9435.delete("messages", "type == ?", new String[]{Integer.toString(3)});
                        sQLiteDatabaseM9435.setTransactionSuccessful();
                        sQLiteDatabaseM9435.endTransaction();
                        sQLiteDatabaseM9435.close();
                        return;
                    } catch (SQLiteException e) {
                        if (0 != 0) {
                            try {
                                if (sQLiteDatabase.inTransaction()) {
                                    sQLiteDatabase.endTransaction();
                                }
                            } catch (Throwable th) {
                                if (0 != 0) {
                                    sQLiteDatabase.close();
                                }
                                throw th;
                            }
                        }
                        C3485 c3485 = c1103.f4729;
                        C1103.m3564(c3485);
                        c3485.f13423.m4474(e, "Error deleting app launch break from local database");
                        this.f16823 = true;
                        if (0 != 0) {
                            sQLiteDatabase.close();
                        }
                    }
                } catch (SQLiteDatabaseLockedException unused) {
                    SystemClock.sleep(i);
                    i += 20;
                    if (0 != 0) {
                        sQLiteDatabase.close();
                    }
                } catch (SQLiteFullException e2) {
                    C3485 c34852 = c1103.f4729;
                    C1103.m3564(c34852);
                    c34852.f13423.m4474(e2, "Error deleting app launch break from local database");
                    this.f16823 = true;
                    if (0 != 0) {
                    }
                }
            }
            C3485 c34853 = c1103.f4729;
            C1103.m3564(c34853);
            c34853.f13424.m4476("Error deleting app launch break from local database in reasonable time");
        }
    }
}
