package androidx.room; import android.os.Handler; import android.os.Looper; import android.os.SystemClock; import androidx.annotation.GuardedBy; import androidx.annotation.VisibleForTesting; import androidx.sqlite.db.SupportSQLiteDatabase; import androidx.sqlite.db.SupportSQLiteOpenHelper; import java.io.IOException; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import kotlin.Unit; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.SourceDebugExtension; @SourceDebugExtension({"SMAP\nAutoCloser.kt\nKotlin\n*S Kotlin\n*F\n+ 1 AutoCloser.kt\nandroidx/room/AutoCloser\n+ 2 fake.kt\nkotlin/jvm/internal/FakeKt\n*L\n1#1,228:1\n1#2:229\n*E\n"}) /* loaded from: classes.dex */ public final class AutoCloser { public static final Companion Companion = new Companion(null); public static final String autoCloseBug = "https://issuetracker.google.com/issues/new?component=413107&template=1096568"; private long autoCloseTimeoutInMs; private final Runnable autoCloser; @GuardedBy("lock") private SupportSQLiteDatabase delegateDatabase; public SupportSQLiteOpenHelper delegateOpenHelper; private final Runnable executeAutoCloser; private final Executor executor; private final Handler handler; @GuardedBy("lock") private long lastDecrementRefCountTimeStamp; private final Object lock; private boolean manuallyClosed; private Runnable onAutoCloseCallback; @GuardedBy("lock") private int refCount; public final SupportSQLiteDatabase getDelegateDatabase$room_runtime_release() { return this.delegateDatabase; } public final long getLastDecrementRefCountTimeStamp$room_runtime_release() { return this.lastDecrementRefCountTimeStamp; } public final Runnable getOnAutoCloseCallback$room_runtime_release() { return this.onAutoCloseCallback; } public final int getRefCount$room_runtime_release() { return this.refCount; } public final boolean isActive() { return !this.manuallyClosed; } public final void setAutoCloseCallback(Runnable onAutoClose) { Intrinsics.checkNotNullParameter(onAutoClose, "onAutoClose"); this.onAutoCloseCallback = onAutoClose; } public final void setDelegateDatabase$room_runtime_release(SupportSQLiteDatabase supportSQLiteDatabase) { this.delegateDatabase = supportSQLiteDatabase; } public final void setDelegateOpenHelper(SupportSQLiteOpenHelper supportSQLiteOpenHelper) { Intrinsics.checkNotNullParameter(supportSQLiteOpenHelper, ""); this.delegateOpenHelper = supportSQLiteOpenHelper; } public final void setLastDecrementRefCountTimeStamp$room_runtime_release(long j) { this.lastDecrementRefCountTimeStamp = j; } public final void setOnAutoCloseCallback$room_runtime_release(Runnable runnable) { this.onAutoCloseCallback = runnable; } public final void setRefCount$room_runtime_release(int i) { this.refCount = i; } public AutoCloser(long j, TimeUnit autoCloseTimeUnit, Executor autoCloseExecutor) { Intrinsics.checkNotNullParameter(autoCloseTimeUnit, "autoCloseTimeUnit"); Intrinsics.checkNotNullParameter(autoCloseExecutor, "autoCloseExecutor"); this.handler = new Handler(Looper.getMainLooper()); this.lock = new Object(); this.autoCloseTimeoutInMs = autoCloseTimeUnit.toMillis(j); this.executor = autoCloseExecutor; this.lastDecrementRefCountTimeStamp = SystemClock.uptimeMillis(); this.executeAutoCloser = new Runnable() { // from class: androidx.room.AutoCloser$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { AutoCloser.executeAutoCloser$lambda$0(AutoCloser.this); } }; this.autoCloser = new Runnable() { // from class: androidx.room.AutoCloser$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { AutoCloser.autoCloser$lambda$3(AutoCloser.this); } }; } public final SupportSQLiteOpenHelper getDelegateOpenHelper() { SupportSQLiteOpenHelper supportSQLiteOpenHelper = this.delegateOpenHelper; if (supportSQLiteOpenHelper != null) { return supportSQLiteOpenHelper; } Intrinsics.throwUninitializedPropertyAccessException("delegateOpenHelper"); return null; } /* JADX INFO: Access modifiers changed from: private */ public static final void executeAutoCloser$lambda$0(AutoCloser this$0) { Intrinsics.checkNotNullParameter(this$0, "this$0"); this$0.executor.execute(this$0.autoCloser); } /* JADX INFO: Access modifiers changed from: private */ public static final void autoCloser$lambda$3(AutoCloser this$0) { Unit unit; Intrinsics.checkNotNullParameter(this$0, "this$0"); synchronized (this$0.lock) { try { if (SystemClock.uptimeMillis() - this$0.lastDecrementRefCountTimeStamp < this$0.autoCloseTimeoutInMs) { return; } if (this$0.refCount != 0) { return; } Runnable runnable = this$0.onAutoCloseCallback; if (runnable != null) { runnable.run(); unit = Unit.INSTANCE; } else { unit = null; } if (unit == null) { throw new IllegalStateException("onAutoCloseCallback is null but it should have been set before use. Please file a bug against Room at: https://issuetracker.google.com/issues/new?component=413107&template=1096568".toString()); } SupportSQLiteDatabase supportSQLiteDatabase = this$0.delegateDatabase; if (supportSQLiteDatabase != null && supportSQLiteDatabase.isOpen()) { supportSQLiteDatabase.close(); } this$0.delegateDatabase = null; Unit unit2 = Unit.INSTANCE; } catch (Throwable th) { throw th; } } } public final void init(SupportSQLiteOpenHelper delegateOpenHelper) { Intrinsics.checkNotNullParameter(delegateOpenHelper, "delegateOpenHelper"); setDelegateOpenHelper(delegateOpenHelper); } public final V executeRefCountingFunction(Function1 block) { Intrinsics.checkNotNullParameter(block, "block"); try { return (V) block.invoke(incrementCountAndEnsureDbIsOpen()); } finally { decrementCountAndScheduleClose(); } } public final SupportSQLiteDatabase incrementCountAndEnsureDbIsOpen() { synchronized (this.lock) { this.handler.removeCallbacks(this.executeAutoCloser); this.refCount++; if (!(!this.manuallyClosed)) { throw new IllegalStateException("Attempting to open already closed database.".toString()); } SupportSQLiteDatabase supportSQLiteDatabase = this.delegateDatabase; if (supportSQLiteDatabase != null && supportSQLiteDatabase.isOpen()) { return supportSQLiteDatabase; } SupportSQLiteDatabase writableDatabase = getDelegateOpenHelper().getWritableDatabase(); this.delegateDatabase = writableDatabase; return writableDatabase; } } public final void decrementCountAndScheduleClose() { synchronized (this.lock) { try { int i = this.refCount; if (i <= 0) { throw new IllegalStateException("ref count is 0 or lower but we're supposed to decrement".toString()); } int i2 = i - 1; this.refCount = i2; if (i2 == 0) { if (this.delegateDatabase == null) { return; } else { this.handler.postDelayed(this.executeAutoCloser, this.autoCloseTimeoutInMs); } } Unit unit = Unit.INSTANCE; } catch (Throwable th) { throw th; } } } public final void closeDatabaseIfOpen() throws IOException { synchronized (this.lock) { try { this.manuallyClosed = true; SupportSQLiteDatabase supportSQLiteDatabase = this.delegateDatabase; if (supportSQLiteDatabase != null) { supportSQLiteDatabase.close(); } this.delegateDatabase = null; Unit unit = Unit.INSTANCE; } catch (Throwable th) { throw th; } } } @VisibleForTesting public final int getRefCountForTest$room_runtime_release() { int i; synchronized (this.lock) { i = this.refCount; } return i; } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } } }