package androidx.sqlite.db.framework; 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 androidx.annotation.RequiresApi; import androidx.sqlite.db.SupportSQLiteCompat; import androidx.sqlite.db.SupportSQLiteDatabase; import androidx.sqlite.db.SupportSQLiteOpenHelper; import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper; import androidx.sqlite.util.ProcessLock; import java.io.File; import java.util.UUID; import kotlin.Lazy; import kotlin.LazyKt__LazyJVMKt; import kotlin.jvm.functions.Function0; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.SourceDebugExtension; /* loaded from: classes.dex */ public final class FrameworkSQLiteOpenHelper implements SupportSQLiteOpenHelper { public static final Companion Companion = new Companion(null); private static final String TAG = "SupportSQLite"; private final boolean allowDataLossOnRecovery; private final SupportSQLiteOpenHelper.Callback callback; private final Context context; private final Lazy lazyDelegate; private final String name; private final boolean useNoBackupDirectory; private boolean writeAheadLoggingEnabled; /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */ public FrameworkSQLiteOpenHelper(Context context, String str, SupportSQLiteOpenHelper.Callback callback) { this(context, str, callback, false, false, 24, null); Intrinsics.checkNotNullParameter(context, "context"); Intrinsics.checkNotNullParameter(callback, "callback"); } /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */ public FrameworkSQLiteOpenHelper(Context context, String str, SupportSQLiteOpenHelper.Callback callback, boolean z) { this(context, str, callback, z, false, 16, null); Intrinsics.checkNotNullParameter(context, "context"); Intrinsics.checkNotNullParameter(callback, "callback"); } @Override // androidx.sqlite.db.SupportSQLiteOpenHelper public String getDatabaseName() { return this.name; } public FrameworkSQLiteOpenHelper(Context context, String str, SupportSQLiteOpenHelper.Callback callback, boolean z, boolean z2) { Lazy lazy; Intrinsics.checkNotNullParameter(context, "context"); Intrinsics.checkNotNullParameter(callback, "callback"); this.context = context; this.name = str; this.callback = callback; this.useNoBackupDirectory = z; this.allowDataLossOnRecovery = z2; lazy = LazyKt__LazyJVMKt.lazy(new Function0() { // from class: androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$lazyDelegate$1 { super(0); } @Override // kotlin.jvm.functions.Function0 public final FrameworkSQLiteOpenHelper.OpenHelper invoke() { String str2; FrameworkSQLiteOpenHelper.OpenHelper openHelper; Context context2; String str3; SupportSQLiteOpenHelper.Callback callback2; boolean z3; boolean z4; boolean z5; Context context3; String str4; Context context4; SupportSQLiteOpenHelper.Callback callback3; boolean z6; str2 = FrameworkSQLiteOpenHelper.this.name; if (str2 != null) { z5 = FrameworkSQLiteOpenHelper.this.useNoBackupDirectory; if (z5) { context3 = FrameworkSQLiteOpenHelper.this.context; File noBackupFilesDir = SupportSQLiteCompat.Api21Impl.getNoBackupFilesDir(context3); str4 = FrameworkSQLiteOpenHelper.this.name; File file = new File(noBackupFilesDir, str4); context4 = FrameworkSQLiteOpenHelper.this.context; String absolutePath = file.getAbsolutePath(); FrameworkSQLiteOpenHelper.DBRefHolder dBRefHolder = new FrameworkSQLiteOpenHelper.DBRefHolder(null); callback3 = FrameworkSQLiteOpenHelper.this.callback; z6 = FrameworkSQLiteOpenHelper.this.allowDataLossOnRecovery; openHelper = new FrameworkSQLiteOpenHelper.OpenHelper(context4, absolutePath, dBRefHolder, callback3, z6); z4 = FrameworkSQLiteOpenHelper.this.writeAheadLoggingEnabled; SupportSQLiteCompat.Api16Impl.setWriteAheadLoggingEnabled(openHelper, z4); return openHelper; } } context2 = FrameworkSQLiteOpenHelper.this.context; str3 = FrameworkSQLiteOpenHelper.this.name; FrameworkSQLiteOpenHelper.DBRefHolder dBRefHolder2 = new FrameworkSQLiteOpenHelper.DBRefHolder(null); callback2 = FrameworkSQLiteOpenHelper.this.callback; z3 = FrameworkSQLiteOpenHelper.this.allowDataLossOnRecovery; openHelper = new FrameworkSQLiteOpenHelper.OpenHelper(context2, str3, dBRefHolder2, callback2, z3); z4 = FrameworkSQLiteOpenHelper.this.writeAheadLoggingEnabled; SupportSQLiteCompat.Api16Impl.setWriteAheadLoggingEnabled(openHelper, z4); return openHelper; } }); this.lazyDelegate = lazy; } public /* synthetic */ FrameworkSQLiteOpenHelper(Context context, String str, SupportSQLiteOpenHelper.Callback callback, boolean z, boolean z2, int i, DefaultConstructorMarker defaultConstructorMarker) { this(context, str, callback, (i & 8) != 0 ? false : z, (i & 16) != 0 ? false : z2); } private final OpenHelper getDelegate() { return (OpenHelper) this.lazyDelegate.getValue(); } @Override // androidx.sqlite.db.SupportSQLiteOpenHelper @RequiresApi(api = 16) public void setWriteAheadLoggingEnabled(boolean z) { if (this.lazyDelegate.isInitialized()) { SupportSQLiteCompat.Api16Impl.setWriteAheadLoggingEnabled(getDelegate(), z); } this.writeAheadLoggingEnabled = z; } @Override // androidx.sqlite.db.SupportSQLiteOpenHelper public SupportSQLiteDatabase getWritableDatabase() { return getDelegate().getSupportDatabase(true); } @Override // androidx.sqlite.db.SupportSQLiteOpenHelper public SupportSQLiteDatabase getReadableDatabase() { return getDelegate().getSupportDatabase(false); } @Override // androidx.sqlite.db.SupportSQLiteOpenHelper, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.lazyDelegate.isInitialized()) { getDelegate().close(); } } public static final class OpenHelper extends SQLiteOpenHelper { public static final Companion Companion = new Companion(null); private final boolean allowDataLossOnRecovery; private final SupportSQLiteOpenHelper.Callback callback; private final Context context; private final DBRefHolder dbRef; private final ProcessLock lock; private boolean migrated; private boolean opened; public enum CallbackName { ON_CONFIGURE, ON_CREATE, ON_UPGRADE, ON_DOWNGRADE, ON_OPEN } public /* synthetic */ class WhenMappings { public static final /* synthetic */ int[] $EnumSwitchMapping$0; static { int[] iArr = new int[CallbackName.values().length]; try { iArr[CallbackName.ON_CONFIGURE.ordinal()] = 1; } catch (NoSuchFieldError unused) { } try { iArr[CallbackName.ON_CREATE.ordinal()] = 2; } catch (NoSuchFieldError unused2) { } try { iArr[CallbackName.ON_UPGRADE.ordinal()] = 3; } catch (NoSuchFieldError unused3) { } try { iArr[CallbackName.ON_DOWNGRADE.ordinal()] = 4; } catch (NoSuchFieldError unused4) { } try { iArr[CallbackName.ON_OPEN.ordinal()] = 5; } catch (NoSuchFieldError unused5) { } $EnumSwitchMapping$0 = iArr; } } public final boolean getAllowDataLossOnRecovery() { return this.allowDataLossOnRecovery; } public final SupportSQLiteOpenHelper.Callback getCallback() { return this.callback; } public final Context getContext() { return this.context; } public final DBRefHolder getDbRef() { return this.dbRef; } /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ public OpenHelper(Context context, String str, final DBRefHolder dbRef, final SupportSQLiteOpenHelper.Callback callback, boolean z) { super(context, str, null, callback.version, new DatabaseErrorHandler() { // from class: androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper$$ExternalSyntheticLambda0 @Override // android.database.DatabaseErrorHandler public final void onCorruption(SQLiteDatabase sQLiteDatabase) { FrameworkSQLiteOpenHelper.OpenHelper._init_$lambda$0(SupportSQLiteOpenHelper.Callback.this, dbRef, sQLiteDatabase); } }); Intrinsics.checkNotNullParameter(context, "context"); Intrinsics.checkNotNullParameter(dbRef, "dbRef"); Intrinsics.checkNotNullParameter(callback, "callback"); this.context = context; this.dbRef = dbRef; this.callback = callback; this.allowDataLossOnRecovery = z; if (str == null) { str = UUID.randomUUID().toString(); Intrinsics.checkNotNullExpressionValue(str, "randomUUID().toString()"); } this.lock = new ProcessLock(str, context.getCacheDir(), false); } /* JADX INFO: Access modifiers changed from: private */ public static final void _init_$lambda$0(SupportSQLiteOpenHelper.Callback callback, DBRefHolder dbRef, SQLiteDatabase dbObj) { Intrinsics.checkNotNullParameter(callback, "$callback"); Intrinsics.checkNotNullParameter(dbRef, "$dbRef"); Companion companion = Companion; Intrinsics.checkNotNullExpressionValue(dbObj, "dbObj"); callback.onCorruption(companion.getWrappedDb(dbRef, dbObj)); } public final SupportSQLiteDatabase getSupportDatabase(boolean z) { try { this.lock.lock((this.opened || getDatabaseName() == null) ? false : true); this.migrated = false; SQLiteDatabase innerGetDatabase = innerGetDatabase(z); if (this.migrated) { close(); SupportSQLiteDatabase supportDatabase = getSupportDatabase(z); this.lock.unlock(); return supportDatabase; } FrameworkSQLiteDatabase wrappedDb = getWrappedDb(innerGetDatabase); this.lock.unlock(); return wrappedDb; } catch (Throwable th) { this.lock.unlock(); throw th; } } private final SQLiteDatabase innerGetDatabase(boolean z) { File parentFile; String databaseName = getDatabaseName(); boolean z2 = this.opened; if (databaseName != null && !z2 && (parentFile = this.context.getDatabasePath(databaseName).getParentFile()) != null) { parentFile.mkdirs(); if (!parentFile.isDirectory()) { Log.w(FrameworkSQLiteOpenHelper.TAG, "Invalid database parent file, not a directory: " + parentFile); } } try { return getWritableOrReadableDatabase(z); } catch (Throwable unused) { super.close(); try { Thread.sleep(500L); } catch (InterruptedException unused2) { } try { return getWritableOrReadableDatabase(z); } catch (Throwable th) { super.close(); if (th instanceof CallbackException) { CallbackException callbackException = th; Throwable cause = callbackException.getCause(); int i = WhenMappings.$EnumSwitchMapping$0[callbackException.getCallbackName().ordinal()]; if (i == 1) { throw cause; } if (i == 2) { throw cause; } if (i == 3) { throw cause; } if (i == 4) { throw cause; } if (!(cause instanceof SQLiteException)) { throw cause; } } else { if (!(th instanceof SQLiteException)) { throw th; } if (databaseName == null || !this.allowDataLossOnRecovery) { throw th; } } this.context.deleteDatabase(databaseName); try { return getWritableOrReadableDatabase(z); } catch (CallbackException e) { throw e.getCause(); } } } } private final SQLiteDatabase getWritableOrReadableDatabase(boolean z) { if (z) { SQLiteDatabase writableDatabase = super.getWritableDatabase(); Intrinsics.checkNotNullExpressionValue(writableDatabase, "{\n super.…eDatabase()\n }"); return writableDatabase; } SQLiteDatabase readableDatabase = super.getReadableDatabase(); Intrinsics.checkNotNullExpressionValue(readableDatabase, "{\n super.…eDatabase()\n }"); return readableDatabase; } public final FrameworkSQLiteDatabase getWrappedDb(SQLiteDatabase sqLiteDatabase) { Intrinsics.checkNotNullParameter(sqLiteDatabase, "sqLiteDatabase"); return Companion.getWrappedDb(this.dbRef, sqLiteDatabase); } @Override // android.database.sqlite.SQLiteOpenHelper public void onCreate(SQLiteDatabase sqLiteDatabase) { Intrinsics.checkNotNullParameter(sqLiteDatabase, "sqLiteDatabase"); try { this.callback.onCreate(getWrappedDb(sqLiteDatabase)); } catch (Throwable th) { throw new CallbackException(CallbackName.ON_CREATE, th); } } @Override // android.database.sqlite.SQLiteOpenHelper public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) { Intrinsics.checkNotNullParameter(sqLiteDatabase, "sqLiteDatabase"); this.migrated = true; try { this.callback.onUpgrade(getWrappedDb(sqLiteDatabase), i, i2); } catch (Throwable th) { throw new CallbackException(CallbackName.ON_UPGRADE, th); } } @Override // android.database.sqlite.SQLiteOpenHelper public void onConfigure(SQLiteDatabase db) { Intrinsics.checkNotNullParameter(db, "db"); if (!this.migrated && this.callback.version != db.getVersion()) { db.setMaxSqlCacheSize(1); } try { this.callback.onConfigure(getWrappedDb(db)); } catch (Throwable th) { throw new CallbackException(CallbackName.ON_CONFIGURE, th); } } @Override // android.database.sqlite.SQLiteOpenHelper public void onDowngrade(SQLiteDatabase db, int i, int i2) { Intrinsics.checkNotNullParameter(db, "db"); this.migrated = true; try { this.callback.onDowngrade(getWrappedDb(db), i, i2); } catch (Throwable th) { throw new CallbackException(CallbackName.ON_DOWNGRADE, th); } } @Override // android.database.sqlite.SQLiteOpenHelper public void onOpen(SQLiteDatabase db) { Intrinsics.checkNotNullParameter(db, "db"); if (!this.migrated) { try { this.callback.onOpen(getWrappedDb(db)); } catch (Throwable th) { throw new CallbackException(CallbackName.ON_OPEN, th); } } this.opened = true; } @Override // android.database.sqlite.SQLiteOpenHelper, java.lang.AutoCloseable public void close() { try { ProcessLock.lock$default(this.lock, false, 1, null); super.close(); this.dbRef.setDb(null); this.opened = false; } finally { this.lock.unlock(); } } public static final class CallbackException extends RuntimeException { private final CallbackName callbackName; private final Throwable cause; public final CallbackName getCallbackName() { return this.callbackName; } @Override // java.lang.Throwable public Throwable getCause() { return this.cause; } /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ public CallbackException(CallbackName callbackName, Throwable cause) { super(cause); Intrinsics.checkNotNullParameter(callbackName, "callbackName"); Intrinsics.checkNotNullParameter(cause, "cause"); this.callbackName = callbackName; this.cause = cause; } } @SourceDebugExtension({"SMAP\nFrameworkSQLiteOpenHelper.kt\nKotlin\n*S Kotlin\n*F\n+ 1 FrameworkSQLiteOpenHelper.kt\nandroidx/sqlite/db/framework/FrameworkSQLiteOpenHelper$OpenHelper$Companion\n+ 2 fake.kt\nkotlin/jvm/internal/FakeKt\n*L\n1#1,342:1\n1#2:343\n*E\n"}) public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } public final FrameworkSQLiteDatabase getWrappedDb(DBRefHolder refHolder, SQLiteDatabase sqLiteDatabase) { Intrinsics.checkNotNullParameter(refHolder, "refHolder"); Intrinsics.checkNotNullParameter(sqLiteDatabase, "sqLiteDatabase"); FrameworkSQLiteDatabase db = refHolder.getDb(); if (db != null && db.isDelegate(sqLiteDatabase)) { return db; } FrameworkSQLiteDatabase frameworkSQLiteDatabase = new FrameworkSQLiteDatabase(sqLiteDatabase); refHolder.setDb(frameworkSQLiteDatabase); return frameworkSQLiteDatabase; } } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } } public static final class DBRefHolder { private FrameworkSQLiteDatabase db; public final FrameworkSQLiteDatabase getDb() { return this.db; } public final void setDb(FrameworkSQLiteDatabase frameworkSQLiteDatabase) { this.db = frameworkSQLiteDatabase; } public DBRefHolder(FrameworkSQLiteDatabase frameworkSQLiteDatabase) { this.db = frameworkSQLiteDatabase; } } }