package o;

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.Pair;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class IC extends SQLiteOpenHelper {
    public static final /* synthetic */ int s = 0;
    public final Context l;
    public final C0247Jn m;
    public final C0692a8 n;

    /* renamed from: o, reason: collision with root package name */
    public final boolean f154o;
    public boolean p;
    public final SY q;
    public boolean r;

    public IC(Context context, String str, final C0247Jn c0247Jn, final C0692a8 c0692a8, boolean z) {
        super(context, str, null, c0692a8.b, new DatabaseErrorHandler(c0692a8, c0247Jn) { // from class: o.GC
            public final /* synthetic */ C0247Jn a;

            {
                this.a = c0247Jn;
            }

            @Override // android.database.DatabaseErrorHandler
            public final void onCorruption(SQLiteDatabase sQLiteDatabase) {
                int i = IC.s;
                C0247Jn c0247Jn2 = this.a;
                FC fc = (FC) c0247Jn2.m;
                if (fc == null || !F3.c(fc.l, sQLiteDatabase)) {
                    fc = new FC(sQLiteDatabase);
                    c0247Jn2.m = fc;
                }
                SQLiteDatabase sQLiteDatabase2 = fc.l;
                if (!sQLiteDatabase2.isOpen()) {
                    String path = sQLiteDatabase2.getPath();
                    if (path != null) {
                        C0692a8.e(path);
                        return;
                    }
                    return;
                }
                List list = null;
                try {
                    try {
                        list = fc.m;
                    } catch (SQLiteException unused) {
                    }
                    try {
                        fc.close();
                    } catch (IOException unused2) {
                    }
                    if (list != null) {
                        return;
                    }
                } finally {
                    if (list != null) {
                        Iterator it = list.iterator();
                        while (it.hasNext()) {
                            C0692a8.e((String) ((Pair) it.next()).second);
                        }
                    } else {
                        String path2 = sQLiteDatabase2.getPath();
                        if (path2 != null) {
                            C0692a8.e(path2);
                        }
                    }
                }
            }
        });
        this.l = context;
        this.m = c0247Jn;
        this.n = c0692a8;
        this.f154o = z;
        this.q = new SY(str == null ? UUID.randomUUID().toString() : str, context.getCacheDir(), false);
    }

    public final FC b(boolean z) throws IOException {
        SY sy = this.q;
        try {
            sy.a((this.r || getDatabaseName() == null) ? false : true);
            this.p = false;
            SQLiteDatabase sQLiteDatabaseD = d(z);
            if (!this.p) {
                FC fcC = c(sQLiteDatabaseD);
                sy.b();
                return fcC;
            }
            close();
            FC fcB = b(z);
            sy.b();
            return fcB;
        } catch (Throwable th) {
            sy.b();
            throw th;
        }
    }

    public final FC c(SQLiteDatabase sQLiteDatabase) {
        C0247Jn c0247Jn = this.m;
        FC fc = (FC) c0247Jn.m;
        if (fc != null && F3.c(fc.l, sQLiteDatabase)) {
            return fc;
        }
        FC fc2 = new FC(sQLiteDatabase);
        c0247Jn.m = fc2;
        return fc2;
    }

    @Override // android.database.sqlite.SQLiteOpenHelper, java.lang.AutoCloseable
    public final void close() {
        SY sy = this.q;
        try {
            sy.a(sy.a);
            super.close();
            this.m.m = null;
            this.r = false;
        } finally {
            sy.b();
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r6v0, types: [boolean] */
    /* JADX WARN: Type inference failed for: r6v1 */
    /* JADX WARN: Type inference failed for: r6v12, types: [android.database.sqlite.SQLiteDatabase] */
    /* JADX WARN: Type inference failed for: r6v14 */
    /* JADX WARN: Type inference failed for: r6v15 */
    /* JADX WARN: Type inference failed for: r6v16 */
    /* JADX WARN: Type inference failed for: r6v17 */
    /* JADX WARN: Type inference failed for: r6v2 */
    /* JADX WARN: Type inference failed for: r6v9, types: [android.database.sqlite.SQLiteDatabase] */
    public final SQLiteDatabase d(boolean z) throws Throwable {
        File parentFile;
        String databaseName = getDatabaseName();
        Context context = this.l;
        if (databaseName != null && (parentFile = context.getDatabasePath(databaseName).getParentFile()) != null) {
            parentFile.mkdirs();
            if (!parentFile.isDirectory()) {
                parentFile.toString();
            }
        }
        try {
            z = z != 0 ? getWritableDatabase() : getReadableDatabase();
            return z;
        } catch (Throwable unused) {
            super.close();
            try {
                Thread.sleep(500L);
            } catch (InterruptedException unused2) {
            }
            try {
                z = z != 0 ? getWritableDatabase() : getReadableDatabase();
                return z;
            } catch (Throwable th) {
                super.close();
                if (th instanceof HC) {
                    HC hc = th;
                    int iW = AbstractC2462sn.w(hc.l);
                    Throwable th2 = hc.m;
                    if (iW == 0 || iW == 1 || iW == 2 || iW == 3 || !(th2 instanceof SQLiteException)) {
                        throw th2;
                    }
                } else if (!(th instanceof SQLiteException) || databaseName == null || !this.f154o) {
                    throw th;
                }
                context.deleteDatabase(databaseName);
                try {
                    return z != 0 ? getWritableDatabase() : getReadableDatabase();
                } catch (HC e) {
                    throw e.m;
                }
            }
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onConfigure(SQLiteDatabase sQLiteDatabase) {
        try {
            C0692a8 c0692a8 = this.n;
            c(sQLiteDatabase);
            c0692a8.getClass();
        } catch (Throwable th) {
            throw new HC(1, th);
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onCreate(SQLiteDatabase sQLiteDatabase) {
        try {
            this.n.j(c(sQLiteDatabase));
        } catch (Throwable th) {
            throw new HC(2, th);
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onDowngrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
        this.p = true;
        try {
            this.n.l(c(sQLiteDatabase), i, i2);
        } catch (Throwable th) {
            throw new HC(4, th);
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onOpen(SQLiteDatabase sQLiteDatabase) {
        if (!this.p) {
            try {
                this.n.k(c(sQLiteDatabase));
            } catch (Throwable th) {
                throw new HC(5, th);
            }
        }
        this.r = true;
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public final void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
        this.p = true;
        try {
            this.n.l(c(sQLiteDatabase), i, i2);
        } catch (Throwable th) {
            throw new HC(3, th);
        }
    }
}
