package androidx.room;

import android.database.Cursor;
import androidx.work.impl.WorkDatabase_Impl;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import kotlin.Pair;
import kotlin.collections.builders.ListBuilder;

/* JADX INFO: loaded from: classes.dex */
public final class r0 extends l3.c {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public e f4137b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final p0 f4139d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final String f4140e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final String f4141f;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public r0(e eVar, androidx.work.impl.n nVar) {
        super(nVar.f4121a);
        nb.g.i(eVar, "configuration");
        this.f4138c = eVar.f4064e;
        this.f4137b = eVar;
        this.f4139d = nVar;
        this.f4140e = "c103703e120ae8cc73c9248622f3cd1e";
        this.f4141f = "49f946663a8deb7054212b8adda248c6";
    }

    @Override // l3.c
    public final void b(m3.c cVar) {
    }

    /* JADX WARN: Removed duplicated region for block: B:10:0x001e  */
    @Override // l3.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c(m3.c cVar) throws IOException {
        boolean z10;
        Cursor cursorC = cVar.C(new l3.a("SELECT count(*) FROM sqlite_master WHERE name != 'android_metadata'", null));
        try {
            if (cursorC.moveToFirst()) {
                z10 = cursorC.getInt(0) == 0;
            }
            ua.b.g(cursorC, null);
            p0 p0Var = this.f4139d;
            p0Var.a(cVar);
            if (!z10) {
                q0 q0VarB = p0Var.b(cVar);
                if (!q0VarB.f4134a) {
                    throw new IllegalStateException("Pre-packaged database has an invalid schema: " + q0VarB.f4135b);
                }
            }
            cVar.j("CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)");
            String str = this.f4140e;
            nb.g.i(str, "hash");
            cVar.j("INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '" + str + "')");
            WorkDatabase_Impl workDatabase_Impl = ((androidx.work.impl.n) p0Var).f4619b;
            if (workDatabase_Impl.mCallbacks != null) {
                int size = workDatabase_Impl.mCallbacks.size();
                for (int i10 = 0; i10 < size; i10++) {
                    ((j0) workDatabase_Impl.mCallbacks.get(i10)).onCreate(cVar);
                }
            }
            List list = this.f4138c;
            if (list != null) {
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    ((j0) it.next()).onCreate(cVar);
                }
            }
        } catch (Throwable th) {
            try {
                throw th;
            } catch (Throwable th2) {
                ua.b.g(cursorC, th);
                throw th2;
            }
        }
    }

    @Override // l3.c
    public final void d(m3.c cVar, int i10, int i11) throws Exception {
        f(cVar, i10, i11);
    }

    /* JADX WARN: Removed duplicated region for block: B:10:0x001e  */
    @Override // l3.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void e(m3.c cVar) throws Exception {
        boolean z10;
        Cursor cursorC = cVar.C(new l3.a("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name='room_master_table'", null));
        try {
            if (cursorC.moveToFirst()) {
                z10 = cursorC.getInt(0) != 0;
            }
            ua.b.g(cursorC, null);
            String str = this.f4140e;
            p0 p0Var = this.f4139d;
            if (z10) {
                cursorC = cVar.C(new l3.a("SELECT identity_hash FROM room_master_table WHERE id = 42 LIMIT 1", null));
                try {
                    String string = cursorC.moveToFirst() ? cursorC.getString(0) : null;
                    ua.b.g(cursorC, null);
                    if (!nb.g.c(str, string) && !nb.g.c(this.f4141f, string)) {
                        throw new IllegalStateException(defpackage.e.k("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: ", str, ", found: ", string));
                    }
                } finally {
                }
            } else {
                q0 q0VarB = p0Var.b(cVar);
                if (!q0VarB.f4134a) {
                    throw new IllegalStateException("Pre-packaged database has an invalid schema: " + q0VarB.f4135b);
                }
                cVar.j("CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)");
                nb.g.i(str, "hash");
                cVar.j("INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '" + str + "')");
            }
            WorkDatabase_Impl workDatabase_Impl = ((androidx.work.impl.n) p0Var).f4619b;
            workDatabase_Impl.mDatabase = cVar;
            cVar.j("PRAGMA foreign_keys = ON");
            workDatabase_Impl.internalInitInvalidationTracker(cVar);
            if (workDatabase_Impl.mCallbacks != null) {
                int size = workDatabase_Impl.mCallbacks.size();
                for (int i10 = 0; i10 < size; i10++) {
                    ((j0) workDatabase_Impl.mCallbacks.get(i10)).onOpen(cVar);
                }
            }
            List list = this.f4138c;
            if (list != null) {
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    ((j0) it.next()).onOpen(cVar);
                }
            }
            this.f4137b = null;
        } finally {
            try {
                throw th;
            } finally {
            }
        }
    }

    @Override // l3.c
    public final void f(m3.c cVar, int i10, int i11) throws Exception {
        e eVar = this.f4137b;
        p0 p0Var = this.f4139d;
        if (eVar != null) {
            l0 l0Var = eVar.f4063d;
            l0Var.getClass();
            List listA = androidx.room.util.d.a(l0Var, i10, i11);
            if (listA != null) {
                ((androidx.work.impl.n) p0Var).getClass();
                androidx.room.util.b.a(new androidx.room.driver.a(cVar));
                Iterator it = listA.iterator();
                while (it.hasNext()) {
                    ((g3.a) it.next()).migrate(new androidx.room.driver.a(cVar));
                }
                q0 q0VarB = p0Var.b(cVar);
                if (!q0VarB.f4134a) {
                    throw new IllegalStateException("Migration didn't properly handle: " + q0VarB.f4135b);
                }
                cVar.j("CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)");
                String str = this.f4140e;
                nb.g.i(str, "hash");
                cVar.j("INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '" + str + "')");
                return;
            }
        }
        e eVar2 = this.f4137b;
        if (eVar2 == null || androidx.room.util.d.b(eVar2, i10, i11)) {
            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.");
        }
        if (eVar2.f4078s) {
            Cursor cursorC = cVar.C(new l3.a("SELECT name, type FROM sqlite_master WHERE type = 'table' OR type = 'view'", null));
            try {
                ListBuilder listBuilder = new ListBuilder();
                while (cursorC.moveToNext()) {
                    String string = cursorC.getString(0);
                    nb.g.f(string);
                    if (!kotlin.text.h.j0(string, "sqlite_", false) && !nb.g.c(string, "android_metadata")) {
                        listBuilder.add(new Pair(string, Boolean.valueOf(nb.g.c(cursorC.getString(1), "view"))));
                    }
                }
                listBuilder.m();
                ua.b.g(cursorC, null);
                Iterator it2 = listBuilder.iterator();
                while (true) {
                    ld.a aVar = (ld.a) it2;
                    if (!aVar.hasNext()) {
                        break;
                    }
                    Pair pair = (Pair) aVar.next();
                    String str2 = (String) pair.getFirst();
                    if (((Boolean) pair.getSecond()).booleanValue()) {
                        cVar.j("DROP VIEW IF EXISTS " + str2);
                    } else {
                        cVar.j("DROP TABLE IF EXISTS " + str2);
                    }
                }
            } catch (Throwable th) {
                try {
                    throw th;
                } catch (Throwable th2) {
                    ua.b.g(cursorC, th);
                    throw th2;
                }
            }
        } else {
            androidx.work.impl.n nVar = (androidx.work.impl.n) p0Var;
            nVar.getClass();
            cVar.j("DROP TABLE IF EXISTS `Dependency`");
            cVar.j("DROP TABLE IF EXISTS `WorkSpec`");
            cVar.j("DROP TABLE IF EXISTS `WorkTag`");
            cVar.j("DROP TABLE IF EXISTS `SystemIdInfo`");
            cVar.j("DROP TABLE IF EXISTS `WorkName`");
            cVar.j("DROP TABLE IF EXISTS `WorkProgress`");
            cVar.j("DROP TABLE IF EXISTS `Preference`");
            WorkDatabase_Impl workDatabase_Impl = nVar.f4619b;
            if (workDatabase_Impl.mCallbacks != null) {
                int size = workDatabase_Impl.mCallbacks.size();
                for (int i12 = 0; i12 < size; i12++) {
                    ((j0) workDatabase_Impl.mCallbacks.get(i12)).onDestructiveMigration(cVar);
                }
            }
        }
        List list = this.f4138c;
        if (list != null) {
            Iterator it3 = list.iterator();
            while (it3.hasNext()) {
                ((j0) it3.next()).onDestructiveMigration(cVar);
            }
        }
        p0Var.a(cVar);
    }
}
