Search in sources :

Example 11 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class DefaultDownloadIndexTest method releaseAndRecreateDownloadIndex_returnsTheSameDownload.

@Test
public void releaseAndRecreateDownloadIndex_returnsTheSameDownload() throws DatabaseIOException {
    String id = "id";
    Download download = new DownloadBuilder(id).build();
    downloadIndex.putDownload(download);
    downloadIndex = new DefaultDownloadIndex(databaseProvider);
    Download readDownload = downloadIndex.getDownload(id);
    assertThat(readDownload).isNotNull();
    assertEqual(readDownload, download);
}
Also used : DownloadBuilder(com.google.android.exoplayer2.testutil.DownloadBuilder) Test(org.junit.Test)

Example 12 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class DefaultDownloadIndexTest method addAndGetDownload_existingId_returnsUpdatedDownload.

@Test
public void addAndGetDownload_existingId_returnsUpdatedDownload() throws DatabaseIOException {
    String id = "id";
    DownloadBuilder downloadBuilder = new DownloadBuilder(id);
    downloadIndex.putDownload(downloadBuilder.build());
    Download download = downloadBuilder.setUri("different uri").setMimeType(MimeTypes.APPLICATION_MP4).setCacheKey("different cacheKey").setState(Download.STATE_FAILED).setPercentDownloaded(50).setBytesDownloaded(200).setContentLength(400).setFailureReason(FAILURE_REASON_UNKNOWN).setStopReason(0x12345678).setStartTimeMs(10).setUpdateTimeMs(20).setStreamKeys(new StreamKey(/* periodIndex= */
    0, /* groupIndex= */
    1, /* trackIndex= */
    2), new StreamKey(/* periodIndex= */
    3, /* groupIndex= */
    4, /* trackIndex= */
    5)).setCustomMetadata(new byte[] { 0, 1, 2, 3, 7, 8, 9, 10 }).setKeySetId(new byte[] { 0, 1, 2, 3 }).build();
    downloadIndex.putDownload(download);
    Download readDownload = downloadIndex.getDownload(id);
    assertThat(readDownload).isNotNull();
    assertEqual(readDownload, download);
}
Also used : DownloadBuilder(com.google.android.exoplayer2.testutil.DownloadBuilder) Test(org.junit.Test)

Example 13 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class DefaultDownloadIndexTest method downloadIndex_versionDowngradeWipesData.

@Test
public void downloadIndex_versionDowngradeWipesData() throws DatabaseIOException {
    Download download1 = new DownloadBuilder("id1").build();
    downloadIndex.putDownload(download1);
    DownloadCursor cursor = downloadIndex.getDownloads();
    assertThat(cursor.getCount()).isEqualTo(1);
    cursor.close();
    SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
    VersionTable.setVersion(writableDatabase, VersionTable.FEATURE_OFFLINE, EMPTY_NAME, Integer.MAX_VALUE);
    downloadIndex = new DefaultDownloadIndex(databaseProvider);
    cursor = downloadIndex.getDownloads();
    assertThat(cursor.getCount()).isEqualTo(0);
    cursor.close();
    assertThat(VersionTable.getVersion(writableDatabase, VersionTable.FEATURE_OFFLINE, EMPTY_NAME)).isEqualTo(DefaultDownloadIndex.TABLE_VERSION);
}
Also used : DownloadBuilder(com.google.android.exoplayer2.testutil.DownloadBuilder) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 14 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class DefaultDownloadIndex method ensureInitialized.

private void ensureInitialized() throws DatabaseIOException {
    synchronized (initializationLock) {
        if (initialized) {
            return;
        }
        try {
            SQLiteDatabase readableDatabase = databaseProvider.getReadableDatabase();
            int version = VersionTable.getVersion(readableDatabase, VersionTable.FEATURE_OFFLINE, name);
            if (version != TABLE_VERSION) {
                SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
                writableDatabase.beginTransactionNonExclusive();
                try {
                    VersionTable.setVersion(writableDatabase, VersionTable.FEATURE_OFFLINE, name, TABLE_VERSION);
                    List<Download> upgradedDownloads = version == 2 ? loadDownloadsFromVersion2(writableDatabase) : new ArrayList<>();
                    writableDatabase.execSQL("DROP TABLE IF EXISTS " + tableName);
                    writableDatabase.execSQL("CREATE TABLE " + tableName + " " + TABLE_SCHEMA);
                    for (Download download : upgradedDownloads) {
                        putDownloadInternal(download, writableDatabase);
                    }
                    writableDatabase.setTransactionSuccessful();
                } finally {
                    writableDatabase.endTransaction();
                }
            }
            initialized = true;
        } catch (SQLException e) {
            throw new DatabaseIOException(e);
        }
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException)

Example 15 with DatabaseIOException

use of com.google.android.exoplayer2.database.DatabaseIOException in project ExoPlayer by google.

the class DefaultDownloadIndex method setStopReason.

@Override
public void setStopReason(int stopReason) throws DatabaseIOException {
    ensureInitialized();
    try {
        ContentValues values = new ContentValues();
        values.put(COLUMN_STOP_REASON, stopReason);
        SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
        writableDatabase.update(tableName, values, WHERE_STATE_IS_TERMINAL, /* whereArgs= */
        null);
    } catch (SQLException e) {
        throw new DatabaseIOException(e);
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException)

Aggregations

SQLiteDatabase (android.database.sqlite.SQLiteDatabase)13 DatabaseIOException (com.google.android.exoplayer2.database.DatabaseIOException)12 SQLException (android.database.SQLException)11 DownloadBuilder (com.google.android.exoplayer2.testutil.DownloadBuilder)7 Test (org.junit.Test)7 WorkerThread (androidx.annotation.WorkerThread)6 ContentValues (android.content.ContentValues)5 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 HashMap (java.util.HashMap)1