package androidx.sqlite.util; import android.util.Log; import io.sentry.protocol.SentryStackFrame; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import kotlin.Metadata; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; /* compiled from: ProcessLock.kt */ @Metadata(d1 = {"\u00004\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0003\b\u0007\u0018\u0000 \u00112\u00020\u0001:\u0001\u0011B\u001d\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007¢\u0006\u0002\u0010\bJ\u0010\u0010\u000e\u001a\u00020\u000f2\b\b\u0002\u0010\u0006\u001a\u00020\u0007J\u0006\u0010\u0010\u001a\u00020\u000fR\u0010\u0010\t\u001a\u0004\u0018\u00010\nX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u000b\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\f\u001a\u00020\r8\u0002X\u0083\u0004¢\u0006\u0002\n\u0000¨\u0006\u0012"}, d2 = {"Landroidx/sqlite/util/ProcessLock;", "", "name", "", "lockDir", "Ljava/io/File;", "processLock", "", "(Ljava/lang/String;Ljava/io/File;Z)V", "lockChannel", "Ljava/nio/channels/FileChannel;", "lockFile", "threadLock", "Ljava/util/concurrent/locks/Lock;", SentryStackFrame.JsonKeys.LOCK, "", "unlock", "Companion", "sqlite-framework_release"}, k = 1, mv = {1, 7, 1}, xi = 48) /* loaded from: classes2.dex */ public final class ProcessLock { private static final String TAG = "SupportSQLiteLock"; private FileChannel lockChannel; private final File lockFile; private final boolean processLock; private final Lock threadLock; /* renamed from: Companion, reason: from kotlin metadata */ public static final Companion INSTANCE = new Companion(null); private static final Map threadLocksMap = new HashMap(); public ProcessLock(String name, File lockDir, boolean z) { Intrinsics.checkNotNullParameter(name, "name"); Intrinsics.checkNotNullParameter(lockDir, "lockDir"); this.processLock = z; File file = new File(lockDir, name + ".lck"); this.lockFile = file; Companion companion = INSTANCE; String absolutePath = file.getAbsolutePath(); Intrinsics.checkNotNullExpressionValue(absolutePath, "lockFile.absolutePath"); this.threadLock = companion.getThreadLock(absolutePath); } public static /* synthetic */ void lock$default(ProcessLock processLock, boolean z, int i, Object obj) { if ((i & 1) != 0) { z = processLock.processLock; } processLock.lock(z); } public final void lock(boolean processLock) { this.threadLock.lock(); if (processLock) { try { File parentFile = this.lockFile.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } FileChannel channel = new FileOutputStream(this.lockFile).getChannel(); channel.lock(); this.lockChannel = channel; } catch (IOException e) { this.lockChannel = null; Log.w(TAG, "Unable to grab file lock.", e); } } } public final void unlock() { try { FileChannel fileChannel = this.lockChannel; if (fileChannel != null) { fileChannel.close(); } } catch (IOException unused) { } this.threadLock.unlock(); } /* compiled from: ProcessLock.kt */ @Metadata(d1 = {"\u0000\u001e\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010%\n\u0002\u0018\u0002\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u0010\u0010\b\u001a\u00020\u00072\u0006\u0010\t\u001a\u00020\u0004H\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u0082T¢\u0006\u0002\n\u0000R\u001a\u0010\u0005\u001a\u000e\u0012\u0004\u0012\u00020\u0004\u0012\u0004\u0012\u00020\u00070\u0006X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\n"}, d2 = {"Landroidx/sqlite/util/ProcessLock$Companion;", "", "()V", "TAG", "", "threadLocksMap", "", "Ljava/util/concurrent/locks/Lock;", "getThreadLock", "key", "sqlite-framework_release"}, k = 1, mv = {1, 7, 1}, xi = 48) /* loaded from: classes2.dex */ public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } /* JADX INFO: Access modifiers changed from: private */ public final Lock getThreadLock(String key) { Lock lock; synchronized (ProcessLock.threadLocksMap) { Map map = ProcessLock.threadLocksMap; Object obj = map.get(key); if (obj == null) { obj = (Lock) new ReentrantLock(); map.put(key, obj); } lock = (Lock) obj; } return lock; } } }