package net.zetetic.database.sqlcipher;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
public abstract class SQLiteOpenHelper implements androidx.sqlite.db.SupportSQLiteOpenHelper {
    private static final java.lang.String l = "SQLiteOpenHelper";
    private final android.content.Context a;
    private final java.lang.String b;
    private final net.zetetic.database.sqlcipher.SQLiteDatabase.CursorFactory c;
    private final int d;
    private final int e;
    private net.zetetic.database.sqlcipher.SQLiteDatabase f;
    private byte[] g;
    private boolean h;
    private boolean i;
    private final net.zetetic.database.DatabaseErrorHandler j;
    private final net.zetetic.database.sqlcipher.SQLiteDatabaseHook k;

    public SQLiteOpenHelper(android.content.Context context, java.lang.String str, net.zetetic.database.sqlcipher.SQLiteDatabase.CursorFactory cursorFactory, int i) {
        this(context, str, cursorFactory, i, null);
    }

    private static byte[] a(java.lang.String str) {
        if (str == null || str.length() == 0) {
            return new byte[0];
        }
        java.nio.ByteBuffer byteBufferEncode = java.nio.charset.Charset.forName("UTF-8").encode(java.nio.CharBuffer.wrap(str));
        byte[] bArr = new byte[byteBufferEncode.limit()];
        byteBufferEncode.get(bArr);
        return bArr;
    }

    private net.zetetic.database.sqlcipher.SQLiteDatabase d(boolean z) {
        net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase = this.f;
        if (sQLiteDatabase != null) {
            if (!sQLiteDatabase.isOpen()) {
                this.f = null;
            } else if (!z || !this.f.n2()) {
                return this.f;
            }
        }
        if (this.h) {
            throw new java.lang.IllegalStateException("getDatabase called recursively");
        }
        net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabaseZ0 = this.f;
        try {
            this.h = true;
            if (sQLiteDatabaseZ0 == null) {
                java.lang.String path = this.b;
                if (path == null) {
                    sQLiteDatabaseZ0 = net.zetetic.database.sqlcipher.SQLiteDatabase.r(null);
                } else {
                    try {
                        if (!path.startsWith("file:")) {
                            path = this.a.getDatabasePath(path).getPath();
                        }
                        java.lang.String str = path;
                        java.io.File file = new java.io.File(new java.io.File(str).getParent());
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        sQLiteDatabaseZ0 = net.zetetic.database.sqlcipher.SQLiteDatabase.z0(str, this.g, this.c, this.i ? 805306368 : 268435456, this.j, this.k);
                    } catch (android.database.sqlite.SQLiteException e) {
                        if (z) {
                            throw e;
                        }
                        net.zetetic.database.Logger.c(l, "Couldn't open " + this.b + " for writing (will try read-only):", e);
                        sQLiteDatabaseZ0 = net.zetetic.database.sqlcipher.SQLiteDatabase.z0(this.a.getDatabasePath(this.b).getPath(), this.g, this.c, 1, this.j, this.k);
                    }
                }
            } else if (z && sQLiteDatabaseZ0.n2()) {
                sQLiteDatabaseZ0.R0();
            }
            r(sQLiteDatabaseZ0);
            int version = sQLiteDatabaseZ0.getVersion();
            if (version != this.d) {
                if (sQLiteDatabaseZ0.n2()) {
                    throw new android.database.sqlite.SQLiteException("Can't upgrade read-only database from version " + sQLiteDatabaseZ0.getVersion() + " to " + this.d + ": " + this.b);
                }
                if (version > 0 && version < this.e) {
                    java.io.File file2 = new java.io.File(sQLiteDatabaseZ0.getPath());
                    k(sQLiteDatabaseZ0);
                    sQLiteDatabaseZ0.close();
                    if (net.zetetic.database.sqlcipher.SQLiteDatabase.v(file2)) {
                        this.h = false;
                        net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabaseD = d(z);
                        this.h = false;
                        if (sQLiteDatabaseZ0 != this.f) {
                            sQLiteDatabaseZ0.close();
                        }
                        return sQLiteDatabaseD;
                    }
                    throw new java.lang.IllegalStateException("Unable to delete obsolete database " + this.b + " with version " + version);
                }
                sQLiteDatabaseZ0.z();
                try {
                    if (version == 0) {
                        u(sQLiteDatabaseZ0);
                    } else {
                        int i = this.d;
                        if (version > i) {
                            v(sQLiteDatabaseZ0, version, i);
                        } else {
                            C(sQLiteDatabaseZ0, version, i);
                        }
                    }
                    sQLiteDatabaseZ0.h2(this.d);
                    sQLiteDatabaseZ0.y1();
                    sQLiteDatabaseZ0.I1();
                } catch (java.lang.Throwable th) {
                    sQLiteDatabaseZ0.I1();
                    throw th;
                }
            }
            y(sQLiteDatabaseZ0);
            if (sQLiteDatabaseZ0.n2()) {
                net.zetetic.database.Logger.h(l, "Opened " + this.b + " in read-only mode");
            }
            this.f = sQLiteDatabaseZ0;
            this.h = false;
            return sQLiteDatabaseZ0;
        } catch (java.lang.Throwable th2) {
            this.h = false;
            if (sQLiteDatabaseZ0 == null || sQLiteDatabaseZ0 == this.f) {
                throw th2;
            }
            sQLiteDatabaseZ0.close();
            throw th2;
        }
    }

    public abstract void C(net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase, int i, int i2);

    public synchronized void close() {
        if (this.h) {
            throw new java.lang.IllegalStateException("Closed during initialization");
        }
        net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase = this.f;
        if (sQLiteDatabase != null && sQLiteDatabase.isOpen()) {
            this.f.close();
            this.f = null;
        }
    }

    /* JADX INFO: renamed from: e, reason: merged with bridge method [inline-methods] */
    public net.zetetic.database.sqlcipher.SQLiteDatabase getReadableDatabase() {
        net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabaseD;
        synchronized (this) {
            sQLiteDatabaseD = d(false);
        }
        return sQLiteDatabaseD;
    }

    public java.lang.String getDatabaseName() {
        return this.b;
    }

    /* JADX INFO: renamed from: i, reason: merged with bridge method [inline-methods] */
    public net.zetetic.database.sqlcipher.SQLiteDatabase getWritableDatabase() {
        net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabaseD;
        synchronized (this) {
            sQLiteDatabaseD = d(true);
        }
        return sQLiteDatabaseD;
    }

    public void setWriteAheadLoggingEnabled(boolean z) {
        synchronized (this) {
            try {
                if (this.i != z) {
                    net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase = this.f;
                    if (sQLiteDatabase != null && sQLiteDatabase.isOpen() && !this.f.n2()) {
                        if (z) {
                            this.f.E();
                        } else {
                            this.f.y();
                        }
                    }
                    this.i = z;
                }
            } catch (java.lang.Throwable th) {
                throw th;
            }
        }
    }

    public abstract void u(net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase);

    public void v(net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase, int i, int i2) {
        throw new android.database.sqlite.SQLiteException("Can't downgrade database from version " + i + " to " + i2);
    }

    public SQLiteOpenHelper(android.content.Context context, java.lang.String str, net.zetetic.database.sqlcipher.SQLiteDatabase.CursorFactory cursorFactory, int i, net.zetetic.database.DatabaseErrorHandler databaseErrorHandler) {
        this(context, str, cursorFactory, i, 0, databaseErrorHandler);
    }

    public SQLiteOpenHelper(android.content.Context context, java.lang.String str, net.zetetic.database.sqlcipher.SQLiteDatabase.CursorFactory cursorFactory, int i, int i2, net.zetetic.database.DatabaseErrorHandler databaseErrorHandler) {
        this(context, str, new byte[0], cursorFactory, i, i2, databaseErrorHandler, (net.zetetic.database.sqlcipher.SQLiteDatabaseHook) null, false);
    }

    public SQLiteOpenHelper(android.content.Context context, java.lang.String str, java.lang.String str2, net.zetetic.database.sqlcipher.SQLiteDatabase.CursorFactory cursorFactory, int i, int i2, net.zetetic.database.DatabaseErrorHandler databaseErrorHandler, net.zetetic.database.sqlcipher.SQLiteDatabaseHook sQLiteDatabaseHook, boolean z) {
        this(context, str, a(str2), cursorFactory, i, i2, databaseErrorHandler, sQLiteDatabaseHook, z);
    }

    public SQLiteOpenHelper(android.content.Context context, java.lang.String str, byte[] bArr, net.zetetic.database.sqlcipher.SQLiteDatabase.CursorFactory cursorFactory, int i, int i2, net.zetetic.database.DatabaseErrorHandler databaseErrorHandler, net.zetetic.database.sqlcipher.SQLiteDatabaseHook sQLiteDatabaseHook, boolean z) {
        if (i >= 1) {
            this.a = context;
            this.b = str;
            this.g = bArr;
            this.c = cursorFactory;
            this.d = i;
            this.j = databaseErrorHandler;
            this.k = sQLiteDatabaseHook;
            this.i = z;
            this.e = java.lang.Math.max(0, i2);
            return;
        }
        throw new java.lang.IllegalArgumentException("Version must be >= 1, was " + i);
    }

    public void k(net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase) {
    }

    public void r(net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase) {
    }

    public void y(net.zetetic.database.sqlcipher.SQLiteDatabase sQLiteDatabase) {
    }
}
