mirror of
https://github.com/samsonjs/media.git
synced 2026-04-15 12:55:46 +00:00
Add methods to DefaultDownloadIndex to set manual stop reason and not met requirements
PiperOrigin-RevId: 238960156
This commit is contained in:
parent
2959b5f9c2
commit
3e4c15aa15
3 changed files with 130 additions and 85 deletions
|
|
@ -81,6 +81,8 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||
private static final int COLUMN_INDEX_CUSTOM_METADATA = 14;
|
||||
|
||||
private static final String WHERE_ID_EQUALS = COLUMN_ID + " = ?";
|
||||
private static final String WHERE_STATE_TERMINAL =
|
||||
getStateQuery(DownloadState.STATE_COMPLETED, DownloadState.STATE_FAILED);
|
||||
|
||||
private static final String[] COLUMNS =
|
||||
new String[] {
|
||||
|
|
@ -139,6 +141,8 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||
+ COLUMN_CUSTOM_METADATA
|
||||
+ " BLOB NOT NULL)";
|
||||
|
||||
private static final String TRUE = "1";
|
||||
|
||||
private final DatabaseProvider databaseProvider;
|
||||
|
||||
private boolean initialized;
|
||||
|
|
@ -175,32 +179,15 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||
public DownloadStateCursor getDownloadStates(@DownloadState.State int... states)
|
||||
throws DatabaseIOException {
|
||||
ensureInitialized();
|
||||
try {
|
||||
String selection = null;
|
||||
if (states.length > 0) {
|
||||
StringBuilder selectionBuilder = new StringBuilder();
|
||||
selectionBuilder.append(COLUMN_STATE).append(" IN (");
|
||||
for (int i = 0; i < states.length; i++) {
|
||||
if (i > 0) {
|
||||
selectionBuilder.append(',');
|
||||
}
|
||||
selectionBuilder.append(states[i]);
|
||||
}
|
||||
selectionBuilder.append(')');
|
||||
selection = selectionBuilder.toString();
|
||||
}
|
||||
Cursor cursor = getCursor(selection, /* selectionArgs= */ null);
|
||||
return new DownloadStateCursorImpl(cursor);
|
||||
} catch (SQLiteException e) {
|
||||
throw new DatabaseIOException(e);
|
||||
}
|
||||
Cursor cursor = getCursor(getStateQuery(states), /* selectionArgs= */ null);
|
||||
return new DownloadStateCursorImpl(cursor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putDownloadState(DownloadState downloadState) throws DatabaseIOException {
|
||||
ensureInitialized();
|
||||
Assertions.checkState(downloadState.state != DownloadState.STATE_REMOVED);
|
||||
try {
|
||||
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(COLUMN_ID, downloadState.id);
|
||||
values.put(COLUMN_TYPE, downloadState.type);
|
||||
|
|
@ -218,6 +205,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||
values.put(COLUMN_UPDATE_TIME_MS, downloadState.updateTimeMs);
|
||||
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(downloadState.streamKeys));
|
||||
values.put(COLUMN_CUSTOM_METADATA, downloadState.customMetadata);
|
||||
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
|
||||
writableDatabase.replaceOrThrow(TABLE_NAME, /* nullColumnHack= */ null, values);
|
||||
} catch (SQLiteException e) {
|
||||
throw new DatabaseIOException(e);
|
||||
|
|
@ -234,6 +222,25 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the manual stop reason of the downloads in a terminal state ({@link
|
||||
* DownloadState#STATE_COMPLETED}, {@link DownloadState#STATE_FAILED}).
|
||||
*
|
||||
* @param manualStopReason The manual stop reason.
|
||||
* @throws DatabaseIOException If an error occurs updating the state.
|
||||
*/
|
||||
public void setManualStopReason(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, /* whereArgs= */ null);
|
||||
} catch (SQLException e) {
|
||||
throw new DatabaseIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureInitialized() throws DatabaseIOException {
|
||||
if (initialized) {
|
||||
return;
|
||||
|
|
@ -264,18 +271,39 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
|||
}
|
||||
}
|
||||
|
||||
private Cursor getCursor(@Nullable String selection, @Nullable String[] selectionArgs) {
|
||||
String sortOrder = COLUMN_START_TIME_MS + " ASC";
|
||||
return databaseProvider
|
||||
.getReadableDatabase()
|
||||
.query(
|
||||
TABLE_NAME,
|
||||
COLUMNS,
|
||||
selection,
|
||||
selectionArgs,
|
||||
/* groupBy= */ null,
|
||||
/* having= */ null,
|
||||
sortOrder);
|
||||
private Cursor getCursor(String selection, @Nullable String[] selectionArgs)
|
||||
throws DatabaseIOException {
|
||||
try {
|
||||
String sortOrder = COLUMN_START_TIME_MS + " ASC";
|
||||
return databaseProvider
|
||||
.getReadableDatabase()
|
||||
.query(
|
||||
TABLE_NAME,
|
||||
COLUMNS,
|
||||
selection,
|
||||
selectionArgs,
|
||||
/* groupBy= */ null,
|
||||
/* having= */ null,
|
||||
sortOrder);
|
||||
} catch (SQLiteException e) {
|
||||
throw new DatabaseIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getStateQuery(@DownloadState.State int... states) {
|
||||
if (states.length == 0) {
|
||||
return TRUE;
|
||||
}
|
||||
StringBuilder selectionBuilder = new StringBuilder();
|
||||
selectionBuilder.append(COLUMN_STATE).append(" IN (");
|
||||
for (int i = 0; i < states.length; i++) {
|
||||
if (i > 0) {
|
||||
selectionBuilder.append(',');
|
||||
}
|
||||
selectionBuilder.append(states[i]);
|
||||
}
|
||||
selectionBuilder.append(')');
|
||||
return selectionBuilder.toString();
|
||||
}
|
||||
|
||||
private static DownloadState getDownloadStateForCurrentRow(Cursor cursor) {
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ public class DefaultDownloadIndexTest {
|
|||
DownloadState downloadState1 =
|
||||
new DownloadStateBuilder("id1")
|
||||
.setStartTimeMs(0)
|
||||
.setState(DownloadState.STATE_REMOVED)
|
||||
.setState(DownloadState.STATE_REMOVING)
|
||||
.build();
|
||||
downloadIndex.putDownloadState(downloadState1);
|
||||
DownloadState downloadState2 =
|
||||
|
|
@ -173,7 +173,8 @@ public class DefaultDownloadIndexTest {
|
|||
downloadIndex.putDownloadState(downloadState3);
|
||||
|
||||
DownloadStateCursor cursor =
|
||||
downloadIndex.getDownloadStates(DownloadState.STATE_REMOVED, DownloadState.STATE_COMPLETED);
|
||||
downloadIndex.getDownloadStates(
|
||||
DownloadState.STATE_REMOVING, DownloadState.STATE_COMPLETED);
|
||||
|
||||
assertThat(cursor.getCount()).isEqualTo(2);
|
||||
cursor.moveToNext();
|
||||
|
|
@ -218,4 +219,56 @@ public class DefaultDownloadIndexTest {
|
|||
VersionTable.getVersion(writableDatabase, VersionTable.FEATURE_OFFLINE, INSTANCE_UID))
|
||||
.isEqualTo(DefaultDownloadIndex.TABLE_VERSION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setManualStopReason_setToReasonNone() 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(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 setManualStopReason_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(manualStopReason);
|
||||
|
||||
DownloadState readDownloadState = downloadIndex.getDownloadState(id);
|
||||
DownloadState expectedDownloadState =
|
||||
downloadStateBuilder.setManualStopReason(manualStopReason).build();
|
||||
DownloadStateTest.assertEqual(readDownloadState, expectedDownloadState);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setManualStopReason_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(notMetRequirements);
|
||||
|
||||
DownloadState readDownloadState = downloadIndex.getDownloadState(id);
|
||||
DownloadStateTest.assertEqual(readDownloadState, downloadState);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -312,60 +312,24 @@ public class DownloadStateTest {
|
|||
}
|
||||
|
||||
static void assertEqual(
|
||||
DownloadState downloadState, DownloadState expected, boolean compareTimeFields) {
|
||||
assertThat(areEqual(downloadState, expected, compareTimeFields)).isTrue();
|
||||
}
|
||||
|
||||
private static boolean areEqual(
|
||||
DownloadState downloadState, DownloadState that, boolean compareTimeFields) {
|
||||
if (downloadState.state != that.state) {
|
||||
return false;
|
||||
}
|
||||
if (Float.compare(that.downloadPercentage, downloadState.downloadPercentage) != 0) {
|
||||
return false;
|
||||
}
|
||||
if (downloadState.downloadedBytes != that.downloadedBytes) {
|
||||
return false;
|
||||
}
|
||||
if (downloadState.totalBytes != that.totalBytes) {
|
||||
return false;
|
||||
}
|
||||
assertThat(downloadState.state).isEqualTo(that.state);
|
||||
assertThat(downloadState.downloadPercentage).isEqualTo(that.downloadPercentage);
|
||||
assertThat(downloadState.downloadedBytes).isEqualTo(that.downloadedBytes);
|
||||
assertThat(downloadState.totalBytes).isEqualTo(that.totalBytes);
|
||||
if (compareTimeFields) {
|
||||
if (downloadState.startTimeMs != that.startTimeMs) {
|
||||
return false;
|
||||
}
|
||||
if (downloadState.updateTimeMs != that.updateTimeMs) {
|
||||
return false;
|
||||
}
|
||||
assertThat(downloadState.startTimeMs).isEqualTo(that.startTimeMs);
|
||||
assertThat(downloadState.updateTimeMs).isEqualTo(that.updateTimeMs);
|
||||
}
|
||||
if (downloadState.failureReason != that.failureReason) {
|
||||
return false;
|
||||
}
|
||||
if (downloadState.manualStopReason != that.manualStopReason) {
|
||||
return false;
|
||||
}
|
||||
if (downloadState.notMetRequirements != that.notMetRequirements) {
|
||||
return false;
|
||||
}
|
||||
if (!downloadState.id.equals(that.id)) {
|
||||
return false;
|
||||
}
|
||||
if (!downloadState.type.equals(that.type)) {
|
||||
return false;
|
||||
}
|
||||
if (!downloadState.uri.equals(that.uri)) {
|
||||
return false;
|
||||
}
|
||||
if (downloadState.cacheKey != null
|
||||
? !downloadState.cacheKey.equals(that.cacheKey)
|
||||
: that.cacheKey != null) {
|
||||
return false;
|
||||
}
|
||||
if (downloadState.streamKeys.length != that.streamKeys.length
|
||||
|| !Arrays.asList(downloadState.streamKeys).containsAll(Arrays.asList(that.streamKeys))) {
|
||||
return false;
|
||||
}
|
||||
return Arrays.equals(downloadState.customMetadata, that.customMetadata);
|
||||
assertThat(downloadState.failureReason).isEqualTo(that.failureReason);
|
||||
assertThat(downloadState.manualStopReason).isEqualTo(that.manualStopReason);
|
||||
assertThat(downloadState.notMetRequirements).isEqualTo(that.notMetRequirements);
|
||||
assertThat(downloadState.id).isEqualTo(that.id);
|
||||
assertThat(downloadState.type).isEqualTo(that.type);
|
||||
assertThat(downloadState.uri).isEqualTo(that.uri);
|
||||
assertThat(downloadState.cacheKey).isEqualTo(that.cacheKey);
|
||||
assertThat(Arrays.asList(downloadState.streamKeys)).containsExactlyElementsIn(that.streamKeys);
|
||||
assertThat(downloadState.customMetadata).isEqualTo(that.customMetadata);
|
||||
}
|
||||
|
||||
private DownloadAction createDownloadAction() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue