package f5;

import android.content.Context;
import android.util.Log;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.concurrent.Callable;

/* loaded from: classes.dex */
public final class x implements SupportSQLiteOpenHelper, h {

    /* renamed from: p, reason: collision with root package name */
    private final Context f17898p;

    /* renamed from: q, reason: collision with root package name */
    private final String f17899q;

    /* renamed from: r, reason: collision with root package name */
    private final File f17900r;

    /* renamed from: s, reason: collision with root package name */
    private final Callable f17901s;

    /* renamed from: t, reason: collision with root package name */
    private final int f17902t;

    /* renamed from: u, reason: collision with root package name */
    private final SupportSQLiteOpenHelper f17903u;

    /* renamed from: v, reason: collision with root package name */
    private g f17904v;

    /* renamed from: w, reason: collision with root package name */
    private boolean f17905w;

    public x(Context context, String str, File file, Callable callable, int i10, SupportSQLiteOpenHelper delegate) {
        kotlin.jvm.internal.t.g(context, "context");
        kotlin.jvm.internal.t.g(delegate, "delegate");
        this.f17898p = context;
        this.f17899q = str;
        this.f17900r = file;
        this.f17901s = callable;
        this.f17902t = i10;
        this.f17903u = delegate;
    }

    private final void b(File file, boolean z10) {
        ReadableByteChannel newChannel;
        if (this.f17899q != null) {
            newChannel = Channels.newChannel(this.f17898p.getAssets().open(this.f17899q));
            kotlin.jvm.internal.t.f(newChannel, "newChannel(context.assets.open(copyFromAssetPath))");
        } else if (this.f17900r != null) {
            newChannel = new FileInputStream(this.f17900r).getChannel();
            kotlin.jvm.internal.t.f(newChannel, "FileInputStream(copyFromFile).channel");
        } else {
            Callable callable = this.f17901s;
            if (callable == null) {
                throw new IllegalStateException("copyFromAssetPath, copyFromFile and copyFromInputStream are all null!");
            }
            try {
                newChannel = Channels.newChannel((InputStream) callable.call());
                kotlin.jvm.internal.t.f(newChannel, "newChannel(inputStream)");
            } catch (Exception e10) {
                throw new IOException("inputStreamCallable exception on call", e10);
            }
        }
        File intermediateFile = File.createTempFile("room-copy-helper", ".tmp", this.f17898p.getCacheDir());
        intermediateFile.deleteOnExit();
        FileChannel output = new FileOutputStream(intermediateFile).getChannel();
        kotlin.jvm.internal.t.f(output, "output");
        h5.c.a(newChannel, output);
        File parentFile = file.getParentFile();
        if (parentFile != null && !parentFile.exists() && !parentFile.mkdirs()) {
            throw new IOException("Failed to create directories for " + file.getAbsolutePath());
        }
        kotlin.jvm.internal.t.f(intermediateFile, "intermediateFile");
        c(intermediateFile, z10);
        if (intermediateFile.renameTo(file)) {
            return;
        }
        throw new IOException("Failed to move intermediate file (" + intermediateFile.getAbsolutePath() + ") to destination (" + file.getAbsolutePath() + ").");
    }

    private final void c(File file, boolean z10) {
        g gVar = this.f17904v;
        if (gVar == null) {
            kotlin.jvm.internal.t.y("databaseConfiguration");
            gVar = null;
        }
        gVar.getClass();
    }

    private final void e(boolean z10) {
        String databaseName = getDatabaseName();
        if (databaseName == null) {
            throw new IllegalStateException("Required value was null.".toString());
        }
        File databaseFile = this.f17898p.getDatabasePath(databaseName);
        g gVar = this.f17904v;
        g gVar2 = null;
        if (gVar == null) {
            kotlin.jvm.internal.t.y("databaseConfiguration");
            gVar = null;
        }
        q5.a aVar = new q5.a(databaseName, this.f17898p.getFilesDir(), gVar.f17828s);
        try {
            q5.a.c(aVar, false, 1, null);
            if (!databaseFile.exists()) {
                try {
                    kotlin.jvm.internal.t.f(databaseFile, "databaseFile");
                    b(databaseFile, z10);
                    aVar.d();
                    return;
                } catch (IOException e10) {
                    throw new RuntimeException("Unable to copy database file.", e10);
                }
            }
            try {
                kotlin.jvm.internal.t.f(databaseFile, "databaseFile");
                int c10 = h5.b.c(databaseFile);
                if (c10 == this.f17902t) {
                    aVar.d();
                    return;
                }
                g gVar3 = this.f17904v;
                if (gVar3 == null) {
                    kotlin.jvm.internal.t.y("databaseConfiguration");
                } else {
                    gVar2 = gVar3;
                }
                if (gVar2.a(c10, this.f17902t)) {
                    aVar.d();
                    return;
                }
                if (this.f17898p.deleteDatabase(databaseName)) {
                    try {
                        b(databaseFile, z10);
                    } catch (IOException e11) {
                        Log.w("ROOM", "Unable to copy database file.", e11);
                    }
                } else {
                    Log.w("ROOM", "Failed to delete database file (" + databaseName + ") for a copy destructive migration.");
                }
                aVar.d();
                return;
            } catch (IOException e12) {
                Log.w("ROOM", "Unable to read database version.", e12);
                aVar.d();
                return;
            }
        } catch (Throwable th2) {
            aVar.d();
            throw th2;
        }
        aVar.d();
        throw th2;
    }

    @Override // f5.h
    public SupportSQLiteOpenHelper a() {
        return this.f17903u;
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper, java.io.Closeable, java.lang.AutoCloseable
    public synchronized void close() {
        a().close();
        this.f17905w = false;
    }

    public final void d(g databaseConfiguration) {
        kotlin.jvm.internal.t.g(databaseConfiguration, "databaseConfiguration");
        this.f17904v = databaseConfiguration;
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper
    public String getDatabaseName() {
        return a().getDatabaseName();
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper
    public SupportSQLiteDatabase getWritableDatabase() {
        if (!this.f17905w) {
            e(true);
            this.f17905w = true;
        }
        return a().getWritableDatabase();
    }

    @Override // androidx.sqlite.db.SupportSQLiteOpenHelper
    public void setWriteAheadLoggingEnabled(boolean z10) {
        a().setWriteAheadLoggingEnabled(z10);
    }
}
