DatabaseFileProvidingContext: Support older API levels

On older API levels it's also necessary to implement
openOrCreateDatabase, to be called by SQLiteOpenHelper.

PiperOrigin-RevId: 231387559
This commit is contained in:
olly 2019-01-29 13:18:25 +00:00 committed by Oliver Woodman
parent 45433869e5
commit e0711c64b8

View File

@ -18,6 +18,7 @@ package com.google.android.exoplayer2.database;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
import android.database.Cursor; import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.SQLException; import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
@ -107,10 +108,7 @@ public final class ExoDatabaseProvider extends SQLiteOpenHelper implements Datab
// TODO: This is fragile. Stop using it if/when SQLiteOpenHelper can be instantiated without a // TODO: This is fragile. Stop using it if/when SQLiteOpenHelper can be instantiated without a
// context [Internal ref: b/123351819], or by injecting a Context into all components that need // context [Internal ref: b/123351819], or by injecting a Context into all components that need
// to instantiate an ExoDatabaseProvider. // to instantiate an ExoDatabaseProvider.
/** /** A {@link Context} that implements methods called by {@link SQLiteOpenHelper}. */
* A {@link Context} that only implements {@link #getDatabasePath(String)}. This is the only
* method used by {@link SQLiteOpenHelper}.
*/
private static class DatabaseFileProvidingContext extends ContextWrapper { private static class DatabaseFileProvidingContext extends ContextWrapper {
private final File file; private final File file;
@ -125,5 +123,28 @@ public final class ExoDatabaseProvider extends SQLiteOpenHelper implements Datab
public File getDatabasePath(String name) { public File getDatabasePath(String name) {
return file; return file;
} }
@Override
public SQLiteDatabase openOrCreateDatabase(
String name, int mode, SQLiteDatabase.CursorFactory factory) {
return openOrCreateDatabase(name, mode, factory, /* errorHandler= */ null);
}
@Override
public SQLiteDatabase openOrCreateDatabase(
String name,
int mode,
SQLiteDatabase.CursorFactory factory,
DatabaseErrorHandler errorHandler) {
File databasePath = getDatabasePath(name);
int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
}
if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
}
return SQLiteDatabase.openDatabase(databasePath.getPath(), factory, flags, errorHandler);
}
} }
} }