diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java index e84f90a811..10e01969bb 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java @@ -220,6 +220,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { /** * Removes the {@link DownloadState} with the given {@code id}. * + * @param id ID of a {@link DownloadState}. * @throws DatabaseIOException If an error occurs removing the state. */ public void removeDownloadState(String id) throws DatabaseIOException { @@ -250,6 +251,30 @@ public final class DefaultDownloadIndex implements DownloadIndex { } } + /** + * Sets the manual stop reason of the download with the given {@code id} in a terminal state + * ({@link DownloadState#STATE_COMPLETED}, {@link DownloadState#STATE_FAILED}). + * + *

If there's no {@link DownloadState} with the given {@code id} or it isn't in a terminal + * state, then nothing happens. + * + * @param id ID of a {@link DownloadState}. + * @param manualStopReason The manual stop reason. + * @throws DatabaseIOException If an error occurs updating the state. + */ + public void setManualStopReason(String id, int manualStopReason) throws DatabaseIOException { + ensureInitialized(); + try { + ContentValues values = new ContentValues(); + values.put(COLUMN_MANUAL_STOP_REASON, manualStopReason); + SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase(); + writableDatabase.update( + TABLE_NAME, values, WHERE_STATE_TERMINAL + " AND " + WHERE_ID_EQUALS, new String[] {id}); + } catch (SQLException e) { + throw new DatabaseIOException(e); + } + } + private void ensureInitialized() throws DatabaseIOException { if (initialized) { return; diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java index 37c3f69514..c537eb1152 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java @@ -221,7 +221,7 @@ public class DefaultDownloadIndexTest { } @Test - public void setManualStopReason_setToReasonNone() throws Exception { + public void setManualStopReason_setReasonToNone() throws Exception { String id = "id"; DownloadStateBuilder downloadStateBuilder = new DownloadStateBuilder(id) @@ -271,4 +271,57 @@ public class DefaultDownloadIndexTest { DownloadState readDownloadState = downloadIndex.getDownloadState(id); DownloadStateTest.assertEqual(readDownloadState, downloadState); } + + @Test + public void setSingleDownloadManualStopReason_setReasonToNone() throws Exception { + String id = "id"; + DownloadStateBuilder downloadStateBuilder = + new DownloadStateBuilder(id) + .setState(DownloadState.STATE_COMPLETED) + .setManualStopReason(0x12345678); + DownloadState downloadState = downloadStateBuilder.build(); + downloadIndex.putDownloadState(downloadState); + + downloadIndex.setManualStopReason(id, DownloadState.MANUAL_STOP_REASON_NONE); + + DownloadState readDownloadState = downloadIndex.getDownloadState(id); + DownloadState expectedDownloadState = + downloadStateBuilder.setManualStopReason(DownloadState.MANUAL_STOP_REASON_NONE).build(); + DownloadStateTest.assertEqual(readDownloadState, expectedDownloadState); + } + + @Test + public void setSingleDownloadManualStopReason_setReason() throws Exception { + String id = "id"; + DownloadStateBuilder downloadStateBuilder = + new DownloadStateBuilder(id) + .setState(DownloadState.STATE_FAILED) + .setFailureReason(DownloadState.FAILURE_REASON_UNKNOWN); + DownloadState downloadState = downloadStateBuilder.build(); + downloadIndex.putDownloadState(downloadState); + int manualStopReason = 0x12345678; + + downloadIndex.setManualStopReason(id, manualStopReason); + + DownloadState readDownloadState = downloadIndex.getDownloadState(id); + DownloadState expectedDownloadState = + downloadStateBuilder.setManualStopReason(manualStopReason).build(); + DownloadStateTest.assertEqual(readDownloadState, expectedDownloadState); + } + + @Test + public void setSingleDownloadManualStopReason_notTerminalState_doesNotSetManualStopReason() + throws Exception { + String id = "id"; + DownloadStateBuilder downloadStateBuilder = + new DownloadStateBuilder(id).setState(DownloadState.STATE_DOWNLOADING); + DownloadState downloadState = downloadStateBuilder.build(); + downloadIndex.putDownloadState(downloadState); + int notMetRequirements = 0x12345678; + + downloadIndex.setManualStopReason(id, notMetRequirements); + + DownloadState readDownloadState = downloadIndex.getDownloadState(id); + DownloadStateTest.assertEqual(readDownloadState, downloadState); + } }