Commit a1c13ca6 by olly Committed by Oliver Woodman

Hack to instantiate ExoDatabaseProvider without a context

This will allow CachedContentIndex to start using database
storage without us having to change the SimpleCache and
CachedContentIndex constructors to require a Context or a
DatabaseProvider.

PiperOrigin-RevId: 230884501
parent 0cf43fc6
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
package com.google.android.exoplayer2.database; package com.google.android.exoplayer2.database;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper;
import android.database.Cursor; import android.database.Cursor;
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;
import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Log;
import java.io.File;
/** /**
* An {@link SQLiteOpenHelper} that provides instances of a standalone ExoPlayer database. * An {@link SQLiteOpenHelper} that provides instances of a standalone ExoPlayer database.
...@@ -37,10 +39,25 @@ public final class ExoDatabaseProvider extends SQLiteOpenHelper implements Datab ...@@ -37,10 +39,25 @@ public final class ExoDatabaseProvider extends SQLiteOpenHelper implements Datab
private static final int VERSION = 1; private static final int VERSION = 1;
private static final String TAG = "ExoDatabaseProvider"; private static final String TAG = "ExoDatabaseProvider";
/**
* Provides instances of the database located by passing {@link #DATABASE_NAME} to {@link
* Context#getDatabasePath(String)}.
*
* @param context Any context.
*/
public ExoDatabaseProvider(Context context) { public ExoDatabaseProvider(Context context) {
super(context.getApplicationContext(), DATABASE_NAME, /* factory= */ null, VERSION); super(context.getApplicationContext(), DATABASE_NAME, /* factory= */ null, VERSION);
} }
/**
* Provides instances of the database located at the specified file.
*
* @param file The database file.
*/
public ExoDatabaseProvider(File file) {
super(new DatabaseFileProvidingContext(file), file.getName(), /* factory= */ null, VERSION);
}
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
// Features create their own tables. // Features create their own tables.
...@@ -86,4 +103,26 @@ public final class ExoDatabaseProvider extends SQLiteOpenHelper implements Datab ...@@ -86,4 +103,26 @@ public final class ExoDatabaseProvider extends SQLiteOpenHelper implements Datab
} }
} }
} }
// 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
// to instantiate an ExoDatabaseProvider.
/**
* 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 final File file;
public DatabaseFileProvidingContext(File file) {
super(/* base= */ null);
this.file = file;
}
@Override
public File getDatabasePath(String name) {
return file;
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment