package Z1;

import O1.w;
import a2.C0397a;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.util.Pair;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/* JADX INFO: loaded from: classes.dex */
public final class g extends SQLiteOpenHelper implements AutoCloseable {

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public static final /* synthetic */ int f7405w = 0;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final Context f7406q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final P.g f7407r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final B7.a f7408s;

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final C0397a f7410u;

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public g(Context context, String str, final P.g gVar, final B7.a aVar) {
        String string;
        super(context, str, null, aVar.f1039r, new DatabaseErrorHandler() { // from class: Z1.d
            @Override // android.database.DatabaseErrorHandler
            public final void onCorruption(SQLiteDatabase sQLiteDatabase) {
                int i = g.f7405w;
                P6.g.b(sQLiteDatabase);
                c cVarO = W3.b.o(gVar, sQLiteDatabase);
                aVar.getClass();
                Log.e("SupportSQLite", "Corruption reported by sqlite on database: " + cVarO + ".path");
                SQLiteDatabase sQLiteDatabase2 = cVarO.f7394q;
                if (!sQLiteDatabase2.isOpen()) {
                    String path = sQLiteDatabase2.getPath();
                    if (path != null) {
                        B7.a.h(path);
                        return;
                    }
                    return;
                }
                List<Pair<String, String>> attachedDbs = null;
                try {
                    try {
                        attachedDbs = sQLiteDatabase2.getAttachedDbs();
                    } finally {
                        if (attachedDbs != null) {
                            Iterator<T> it = attachedDbs.iterator();
                            while (it.hasNext()) {
                                Object obj = ((Pair) it.next()).second;
                                P6.g.d(obj, "second");
                                B7.a.h((String) obj);
                            }
                        } else {
                            String path2 = sQLiteDatabase2.getPath();
                            if (path2 != null) {
                                B7.a.h(path2);
                            }
                        }
                    }
                } catch (SQLiteException unused) {
                }
                try {
                    cVarO.close();
                } catch (IOException unused2) {
                }
                if (attachedDbs != null) {
                    return;
                }
            }
        });
        P6.g.e(aVar, "callback");
        this.f7406q = context;
        this.f7407r = gVar;
        this.f7408s = aVar;
        if (str == null) {
            string = UUID.randomUUID().toString();
            P6.g.d(string, "toString(...)");
        } else {
            string = str;
        }
        this.f7410u = new C0397a(string, context.getCacheDir(), false);
    }

    public final Y1.a a(boolean z8) {
        C0397a c0397a = this.f7410u;
        try {
            c0397a.a((this.f7411v || getDatabaseName() == null) ? false : true);
            this.f7409t = false;
            SQLiteDatabase sQLiteDatabaseB = b(z8);
            if (!this.f7409t) {
                c cVarO = W3.b.o(this.f7407r, sQLiteDatabaseB);
                c0397a.b();
                return cVarO;
            }
            close();
            Y1.a aVarA = a(z8);
            c0397a.b();
            return aVarA;
        } catch (Throwable th) {
            c0397a.b();
            throw th;
        }
    }

    public final SQLiteDatabase b(boolean z8) throws Throwable {
        SQLiteDatabase readableDatabase;
        File parentFile;
        String databaseName = getDatabaseName();
        boolean z9 = this.f7411v;
        if (databaseName != null && !z9 && (parentFile = this.f7406q.getDatabasePath(databaseName).getParentFile()) != null) {
            parentFile.mkdirs();
            if (!parentFile.isDirectory()) {
                Log.w("SupportSQLite", "Invalid database parent file, not a directory: " + parentFile);
            }
        }
        try {
            if (z8) {
                SQLiteDatabase writableDatabase = getWritableDatabase();
                P6.g.b(writableDatabase);
                return writableDatabase;
            }
            SQLiteDatabase readableDatabase2 = getReadableDatabase();
            P6.g.b(readableDatabase2);
            return readableDatabase2;
        } catch (Throwable unused) {
            try {
                Thread.sleep(500L);
            } catch (InterruptedException unused2) {
            }
            try {
                if (z8) {
                    readableDatabase = getWritableDatabase();
                    P6.g.b(readableDatabase);
                } else {
                    readableDatabase = getReadableDatabase();
                    P6.g.b(readableDatabase);
                }
                return readableDatabase;
            } catch (Throwable th) {
                th = th;
                if (th instanceof e) {
                    e eVar = (e) th;
                    int iOrdinal = eVar.f7397q.ordinal();
                    th = eVar.f7398r;
                    if (iOrdinal == 0 || iOrdinal == 1 || iOrdinal == 2 || iOrdinal == 3) {
                        throw th;
                    }
                    if (iOrdinal != 4) {
                        throw new A6.b();
                    }
                    if (!(th instanceof SQLiteException)) {
                        throw th;
                    }
                }
                boolean z10 = th instanceof SQLiteException;
                throw th;
            }
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper, java.lang.AutoCloseable
    public final void close() {
        C0397a c0397a = this.f7410u;
        try {
            c0397a.a(c0397a.f7593a);
            super.close();
            this.f7407r.f5246r = null;
            this.f7411v = false;
        } finally {
            c0397a.b();
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onConfigure(SQLiteDatabase sQLiteDatabase) {
        P6.g.e(sQLiteDatabase, "db");
        boolean z8 = this.f7409t;
        B7.a aVar = this.f7408s;
        if (!z8 && aVar.f1039r != sQLiteDatabase.getVersion()) {
            sQLiteDatabase.setMaxSqlCacheSize(1);
        }
        try {
            W3.b.o(this.f7407r, sQLiteDatabase);
            aVar.getClass();
        } catch (Throwable th) {
            throw new e(f.f7399q, th);
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onCreate(SQLiteDatabase sQLiteDatabase) {
        P6.g.e(sQLiteDatabase, "sqLiteDatabase");
        try {
            B7.a aVar = this.f7408s;
            c cVarO = W3.b.o(this.f7407r, sQLiteDatabase);
            aVar.getClass();
            ((w) aVar.f1040s).d(new R1.a(cVarO));
        } catch (Throwable th) {
            throw new e(f.f7400r, th);
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onDowngrade(SQLiteDatabase sQLiteDatabase, int i, int i5) {
        P6.g.e(sQLiteDatabase, "db");
        this.f7409t = true;
        try {
            this.f7408s.n(W3.b.o(this.f7407r, sQLiteDatabase), i, i5);
        } catch (Throwable th) {
            throw new e(f.f7402t, th);
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onOpen(SQLiteDatabase sQLiteDatabase) {
        P6.g.e(sQLiteDatabase, "db");
        if (!this.f7409t) {
            try {
                B7.a aVar = this.f7408s;
                c cVarO = W3.b.o(this.f7407r, sQLiteDatabase);
                aVar.getClass();
                R1.a aVar2 = new R1.a(cVarO);
                w wVar = (w) aVar.f1040s;
                wVar.f(aVar2);
                wVar.f5085g = cVarO;
            } catch (Throwable th) {
                throw new e(f.f7403u, th);
            }
        }
        this.f7411v = true;
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i5) {
        P6.g.e(sQLiteDatabase, "sqLiteDatabase");
        this.f7409t = true;
        try {
            this.f7408s.n(W3.b.o(this.f7407r, sQLiteDatabase), i, i5);
        } catch (Throwable th) {
            throw new e(f.f7401s, th);
        }
    }
}
