- 28,932 files - Full Java source code - Smali files - Resources Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
256 lines
12 KiB
Java
256 lines
12 KiB
Java
package androidx.room;
|
|
|
|
import android.content.Context;
|
|
import android.util.Log;
|
|
import androidx.annotation.RequiresApi;
|
|
import androidx.room.RoomDatabase;
|
|
import androidx.room.util.DBUtil;
|
|
import androidx.room.util.FileUtil;
|
|
import androidx.sqlite.db.SupportSQLiteDatabase;
|
|
import androidx.sqlite.db.SupportSQLiteOpenHelper;
|
|
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory;
|
|
import androidx.sqlite.util.ProcessLock;
|
|
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;
|
|
import kotlin.Unit;
|
|
import kotlin.io.CloseableKt;
|
|
import kotlin.jvm.internal.Intrinsics;
|
|
import kotlin.ranges.RangesKt___RangesKt;
|
|
|
|
/* loaded from: classes.dex */
|
|
public final class SQLiteCopyOpenHelper implements SupportSQLiteOpenHelper, DelegatingOpenHelper {
|
|
private final Context context;
|
|
private final String copyFromAssetPath;
|
|
private final File copyFromFile;
|
|
private final Callable<InputStream> copyFromInputStream;
|
|
private DatabaseConfiguration databaseConfiguration;
|
|
private final int databaseVersion;
|
|
private final SupportSQLiteOpenHelper delegate;
|
|
private boolean verified;
|
|
|
|
@Override // androidx.room.DelegatingOpenHelper
|
|
public SupportSQLiteOpenHelper getDelegate() {
|
|
return this.delegate;
|
|
}
|
|
|
|
public final void setDatabaseConfiguration(DatabaseConfiguration databaseConfiguration) {
|
|
Intrinsics.checkNotNullParameter(databaseConfiguration, "databaseConfiguration");
|
|
this.databaseConfiguration = databaseConfiguration;
|
|
}
|
|
|
|
public SQLiteCopyOpenHelper(Context context, String str, File file, Callable<InputStream> callable, int i, SupportSQLiteOpenHelper delegate) {
|
|
Intrinsics.checkNotNullParameter(context, "context");
|
|
Intrinsics.checkNotNullParameter(delegate, "delegate");
|
|
this.context = context;
|
|
this.copyFromAssetPath = str;
|
|
this.copyFromFile = file;
|
|
this.copyFromInputStream = callable;
|
|
this.databaseVersion = i;
|
|
this.delegate = delegate;
|
|
}
|
|
|
|
@Override // androidx.sqlite.db.SupportSQLiteOpenHelper
|
|
public String getDatabaseName() {
|
|
return getDelegate().getDatabaseName();
|
|
}
|
|
|
|
@Override // androidx.sqlite.db.SupportSQLiteOpenHelper
|
|
@RequiresApi(api = 16)
|
|
public void setWriteAheadLoggingEnabled(boolean z) {
|
|
getDelegate().setWriteAheadLoggingEnabled(z);
|
|
}
|
|
|
|
@Override // androidx.sqlite.db.SupportSQLiteOpenHelper
|
|
public SupportSQLiteDatabase getWritableDatabase() {
|
|
if (!this.verified) {
|
|
verifyDatabaseFile(true);
|
|
this.verified = true;
|
|
}
|
|
return getDelegate().getWritableDatabase();
|
|
}
|
|
|
|
@Override // androidx.sqlite.db.SupportSQLiteOpenHelper
|
|
public SupportSQLiteDatabase getReadableDatabase() {
|
|
if (!this.verified) {
|
|
verifyDatabaseFile(false);
|
|
this.verified = true;
|
|
}
|
|
return getDelegate().getReadableDatabase();
|
|
}
|
|
|
|
@Override // androidx.sqlite.db.SupportSQLiteOpenHelper, java.io.Closeable, java.lang.AutoCloseable
|
|
public synchronized void close() {
|
|
getDelegate().close();
|
|
this.verified = false;
|
|
}
|
|
|
|
private final void verifyDatabaseFile(boolean z) {
|
|
String databaseName = getDatabaseName();
|
|
if (databaseName == null) {
|
|
throw new IllegalStateException("Required value was null.".toString());
|
|
}
|
|
File databaseFile = this.context.getDatabasePath(databaseName);
|
|
DatabaseConfiguration databaseConfiguration = this.databaseConfiguration;
|
|
DatabaseConfiguration databaseConfiguration2 = null;
|
|
if (databaseConfiguration == null) {
|
|
Intrinsics.throwUninitializedPropertyAccessException("databaseConfiguration");
|
|
databaseConfiguration = null;
|
|
}
|
|
ProcessLock processLock = new ProcessLock(databaseName, this.context.getFilesDir(), databaseConfiguration.multiInstanceInvalidation);
|
|
try {
|
|
ProcessLock.lock$default(processLock, false, 1, null);
|
|
if (!databaseFile.exists()) {
|
|
try {
|
|
Intrinsics.checkNotNullExpressionValue(databaseFile, "databaseFile");
|
|
copyDatabaseFile(databaseFile, z);
|
|
processLock.unlock();
|
|
return;
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Unable to copy database file.", e);
|
|
}
|
|
}
|
|
try {
|
|
Intrinsics.checkNotNullExpressionValue(databaseFile, "databaseFile");
|
|
int readVersion = DBUtil.readVersion(databaseFile);
|
|
if (readVersion == this.databaseVersion) {
|
|
processLock.unlock();
|
|
return;
|
|
}
|
|
DatabaseConfiguration databaseConfiguration3 = this.databaseConfiguration;
|
|
if (databaseConfiguration3 == null) {
|
|
Intrinsics.throwUninitializedPropertyAccessException("databaseConfiguration");
|
|
} else {
|
|
databaseConfiguration2 = databaseConfiguration3;
|
|
}
|
|
if (databaseConfiguration2.isMigrationRequired(readVersion, this.databaseVersion)) {
|
|
processLock.unlock();
|
|
return;
|
|
}
|
|
if (this.context.deleteDatabase(databaseName)) {
|
|
try {
|
|
copyDatabaseFile(databaseFile, z);
|
|
} catch (IOException e2) {
|
|
Log.w(Room.LOG_TAG, "Unable to copy database file.", e2);
|
|
}
|
|
} else {
|
|
Log.w(Room.LOG_TAG, "Failed to delete database file (" + databaseName + ") for a copy destructive migration.");
|
|
}
|
|
processLock.unlock();
|
|
return;
|
|
} catch (IOException e3) {
|
|
Log.w(Room.LOG_TAG, "Unable to read database version.", e3);
|
|
processLock.unlock();
|
|
return;
|
|
}
|
|
} catch (Throwable th) {
|
|
processLock.unlock();
|
|
throw th;
|
|
}
|
|
processLock.unlock();
|
|
throw th;
|
|
}
|
|
|
|
private final void copyDatabaseFile(File file, boolean z) throws IOException {
|
|
ReadableByteChannel newChannel;
|
|
if (this.copyFromAssetPath != null) {
|
|
newChannel = Channels.newChannel(this.context.getAssets().open(this.copyFromAssetPath));
|
|
Intrinsics.checkNotNullExpressionValue(newChannel, "newChannel(context.assets.open(copyFromAssetPath))");
|
|
} else if (this.copyFromFile != null) {
|
|
newChannel = new FileInputStream(this.copyFromFile).getChannel();
|
|
Intrinsics.checkNotNullExpressionValue(newChannel, "FileInputStream(copyFromFile).channel");
|
|
} else {
|
|
Callable<InputStream> callable = this.copyFromInputStream;
|
|
if (callable != null) {
|
|
try {
|
|
newChannel = Channels.newChannel(callable.call());
|
|
Intrinsics.checkNotNullExpressionValue(newChannel, "newChannel(inputStream)");
|
|
} catch (Exception e) {
|
|
throw new IOException("inputStreamCallable exception on call", e);
|
|
}
|
|
} else {
|
|
throw new IllegalStateException("copyFromAssetPath, copyFromFile and copyFromInputStream are all null!");
|
|
}
|
|
}
|
|
File intermediateFile = File.createTempFile("room-copy-helper", ".tmp", this.context.getCacheDir());
|
|
intermediateFile.deleteOnExit();
|
|
FileChannel output = new FileOutputStream(intermediateFile).getChannel();
|
|
Intrinsics.checkNotNullExpressionValue(output, "output");
|
|
FileUtil.copy(newChannel, output);
|
|
File parentFile = file.getParentFile();
|
|
if (parentFile != null && !parentFile.exists() && !parentFile.mkdirs()) {
|
|
throw new IOException("Failed to create directories for " + file.getAbsolutePath());
|
|
}
|
|
Intrinsics.checkNotNullExpressionValue(intermediateFile, "intermediateFile");
|
|
dispatchOnOpenPrepackagedDatabase(intermediateFile, z);
|
|
if (intermediateFile.renameTo(file)) {
|
|
return;
|
|
}
|
|
throw new IOException("Failed to move intermediate file (" + intermediateFile.getAbsolutePath() + ") to destination (" + file.getAbsolutePath() + ").");
|
|
}
|
|
|
|
private final void dispatchOnOpenPrepackagedDatabase(File file, boolean z) {
|
|
DatabaseConfiguration databaseConfiguration = this.databaseConfiguration;
|
|
if (databaseConfiguration == null) {
|
|
Intrinsics.throwUninitializedPropertyAccessException("databaseConfiguration");
|
|
databaseConfiguration = null;
|
|
}
|
|
if (databaseConfiguration.prepackagedDatabaseCallback == null) {
|
|
return;
|
|
}
|
|
SupportSQLiteOpenHelper createFrameworkOpenHelper = createFrameworkOpenHelper(file);
|
|
try {
|
|
SupportSQLiteDatabase writableDatabase = z ? createFrameworkOpenHelper.getWritableDatabase() : createFrameworkOpenHelper.getReadableDatabase();
|
|
DatabaseConfiguration databaseConfiguration2 = this.databaseConfiguration;
|
|
if (databaseConfiguration2 == null) {
|
|
Intrinsics.throwUninitializedPropertyAccessException("databaseConfiguration");
|
|
databaseConfiguration2 = null;
|
|
}
|
|
RoomDatabase.PrepackagedDatabaseCallback prepackagedDatabaseCallback = databaseConfiguration2.prepackagedDatabaseCallback;
|
|
Intrinsics.checkNotNull(prepackagedDatabaseCallback);
|
|
prepackagedDatabaseCallback.onOpenPrepackagedDatabase(writableDatabase);
|
|
Unit unit = Unit.INSTANCE;
|
|
CloseableKt.closeFinally(createFrameworkOpenHelper, null);
|
|
} finally {
|
|
}
|
|
}
|
|
|
|
private final SupportSQLiteOpenHelper createFrameworkOpenHelper(File file) {
|
|
final int coerceAtLeast;
|
|
try {
|
|
final int readVersion = DBUtil.readVersion(file);
|
|
FrameworkSQLiteOpenHelperFactory frameworkSQLiteOpenHelperFactory = new FrameworkSQLiteOpenHelperFactory();
|
|
SupportSQLiteOpenHelper.Configuration.Builder name = SupportSQLiteOpenHelper.Configuration.Companion.builder(this.context).name(file.getAbsolutePath());
|
|
coerceAtLeast = RangesKt___RangesKt.coerceAtLeast(readVersion, 1);
|
|
return frameworkSQLiteOpenHelperFactory.create(name.callback(new SupportSQLiteOpenHelper.Callback(coerceAtLeast) { // from class: androidx.room.SQLiteCopyOpenHelper$createFrameworkOpenHelper$configuration$1
|
|
@Override // androidx.sqlite.db.SupportSQLiteOpenHelper.Callback
|
|
public void onCreate(SupportSQLiteDatabase db) {
|
|
Intrinsics.checkNotNullParameter(db, "db");
|
|
}
|
|
|
|
@Override // androidx.sqlite.db.SupportSQLiteOpenHelper.Callback
|
|
public void onUpgrade(SupportSQLiteDatabase db, int i, int i2) {
|
|
Intrinsics.checkNotNullParameter(db, "db");
|
|
}
|
|
|
|
@Override // androidx.sqlite.db.SupportSQLiteOpenHelper.Callback
|
|
public void onOpen(SupportSQLiteDatabase db) {
|
|
Intrinsics.checkNotNullParameter(db, "db");
|
|
int i = readVersion;
|
|
if (i < 1) {
|
|
db.setVersion(i);
|
|
}
|
|
}
|
|
}).build());
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Malformed database file, unable to read version.", e);
|
|
}
|
|
}
|
|
}
|