package f5;

import android.database.Cursor;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
import java.util.Iterator;
import java.util.List;

/* loaded from: classes.dex */
public class t extends SupportSQLiteOpenHelper.a {

    /* renamed from: g, reason: collision with root package name */
    public static final a f17878g = new a(null);

    /* renamed from: c, reason: collision with root package name */
    private g f17879c;

    /* renamed from: d, reason: collision with root package name */
    private final b f17880d;

    /* renamed from: e, reason: collision with root package name */
    private final String f17881e;

    /* renamed from: f, reason: collision with root package name */
    private final String f17882f;

    /* loaded from: classes.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(kotlin.jvm.internal.k kVar) {
            this();
        }

        public final boolean a(SupportSQLiteDatabase db2) {
            kotlin.jvm.internal.t.g(db2, "db");
            Cursor query = db2.query("SELECT count(*) FROM sqlite_master WHERE name != 'android_metadata'");
            try {
                boolean z10 = false;
                if (query.moveToFirst()) {
                    if (query.getInt(0) == 0) {
                        z10 = true;
                    }
                }
                bn.c.a(query, null);
                return z10;
            } finally {
            }
        }

        public final boolean b(SupportSQLiteDatabase db2) {
            kotlin.jvm.internal.t.g(db2, "db");
            Cursor query = db2.query("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name='room_master_table'");
            try {
                boolean z10 = false;
                if (query.moveToFirst()) {
                    if (query.getInt(0) != 0) {
                        z10 = true;
                    }
                }
                bn.c.a(query, null);
                return z10;
            } finally {
            }
        }
    }

    /* loaded from: classes.dex */
    public static abstract class b {

        /* renamed from: a, reason: collision with root package name */
        public final int f17883a;

        public b(int i10) {
            this.f17883a = i10;
        }

        public abstract void a(SupportSQLiteDatabase supportSQLiteDatabase);

        public abstract void b(SupportSQLiteDatabase supportSQLiteDatabase);

        public abstract void c(SupportSQLiteDatabase supportSQLiteDatabase);

        public abstract void d(SupportSQLiteDatabase supportSQLiteDatabase);

        public abstract void e(SupportSQLiteDatabase supportSQLiteDatabase);

        public abstract void f(SupportSQLiteDatabase supportSQLiteDatabase);

        public abstract c g(SupportSQLiteDatabase supportSQLiteDatabase);
    }

    /* loaded from: classes.dex */
    public static class c {

        /* renamed from: a, reason: collision with root package name */
        public final boolean f17884a;

        /* renamed from: b, reason: collision with root package name */
        public final String f17885b;

        public c(boolean z10, String str) {
            this.f17884a = z10;
            this.f17885b = str;
        }
    }

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public t(g configuration, b delegate, String identityHash, String legacyHash) {
        super(delegate.f17883a);
        kotlin.jvm.internal.t.g(configuration, "configuration");
        kotlin.jvm.internal.t.g(delegate, "delegate");
        kotlin.jvm.internal.t.g(identityHash, "identityHash");
        kotlin.jvm.internal.t.g(legacyHash, "legacyHash");
        this.f17879c = configuration;
        this.f17880d = delegate;
        this.f17881e = identityHash;
        this.f17882f = legacyHash;
    }

    private final void h(SupportSQLiteDatabase supportSQLiteDatabase) {
        if (!f17878g.b(supportSQLiteDatabase)) {
            c g10 = this.f17880d.g(supportSQLiteDatabase);
            if (g10.f17884a) {
                this.f17880d.e(supportSQLiteDatabase);
                j(supportSQLiteDatabase);
                return;
            } else {
                throw new IllegalStateException("Pre-packaged database has an invalid schema: " + g10.f17885b);
            }
        }
        Cursor query = supportSQLiteDatabase.query(new o5.a("SELECT identity_hash FROM room_master_table WHERE id = 42 LIMIT 1"));
        try {
            String string = query.moveToFirst() ? query.getString(0) : null;
            bn.c.a(query, null);
            if (kotlin.jvm.internal.t.b(this.f17881e, string) || kotlin.jvm.internal.t.b(this.f17882f, string)) {
                return;
            }
            throw new IllegalStateException("Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number. Expected identity hash: " + this.f17881e + ", found: " + string);
        } catch (Throwable th2) {
            try {
                throw th2;
            } catch (Throwable th3) {
                bn.c.a(query, th2);
                throw th3;
            }
        }
    }

    private final void i(SupportSQLiteDatabase supportSQLiteDatabase) {
        supportSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)");
    }

    private final void j(SupportSQLiteDatabase supportSQLiteDatabase) {
        i(supportSQLiteDatabase);
        supportSQLiteDatabase.execSQL(s.a(this.f17881e));
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper.a
    public void b(SupportSQLiteDatabase db2) {
        kotlin.jvm.internal.t.g(db2, "db");
        super.b(db2);
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper.a
    public void d(SupportSQLiteDatabase db2) {
        kotlin.jvm.internal.t.g(db2, "db");
        boolean a10 = f17878g.a(db2);
        this.f17880d.a(db2);
        if (!a10) {
            c g10 = this.f17880d.g(db2);
            if (!g10.f17884a) {
                throw new IllegalStateException("Pre-packaged database has an invalid schema: " + g10.f17885b);
            }
        }
        j(db2);
        this.f17880d.c(db2);
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper.a
    public void e(SupportSQLiteDatabase db2, int i10, int i11) {
        kotlin.jvm.internal.t.g(db2, "db");
        g(db2, i10, i11);
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper.a
    public void f(SupportSQLiteDatabase db2) {
        kotlin.jvm.internal.t.g(db2, "db");
        super.f(db2);
        h(db2);
        this.f17880d.d(db2);
        this.f17879c = null;
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper.a
    public void g(SupportSQLiteDatabase db2, int i10, int i11) {
        List d10;
        kotlin.jvm.internal.t.g(db2, "db");
        g gVar = this.f17879c;
        boolean z10 = false;
        if (gVar != null && (d10 = gVar.f17813d.d(i10, i11)) != null) {
            this.f17880d.f(db2);
            Iterator it = d10.iterator();
            while (it.hasNext()) {
                ((g5.a) it.next()).a(db2);
            }
            c g10 = this.f17880d.g(db2);
            if (!g10.f17884a) {
                throw new IllegalStateException("Migration didn't properly handle: " + g10.f17885b);
            }
            this.f17880d.e(db2);
            j(db2);
            z10 = true;
        }
        if (z10) {
            return;
        }
        g gVar2 = this.f17879c;
        if (gVar2 != null && !gVar2.a(i10, i11)) {
            this.f17880d.b(db2);
            this.f17880d.a(db2);
            return;
        }
        throw new IllegalStateException("A migration from " + i10 + " to " + i11 + " was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.");
    }
}
