mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +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 int COLUMN_INDEX_CUSTOM_METADATA = 14;
|
||||||
|
|
||||||
private static final String WHERE_ID_EQUALS = COLUMN_ID + " = ?";
|
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 =
|
private static final String[] COLUMNS =
|
||||||
new String[] {
|
new String[] {
|
||||||
|
|
@ -139,6 +141,8 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
||||||
+ COLUMN_CUSTOM_METADATA
|
+ COLUMN_CUSTOM_METADATA
|
||||||
+ " BLOB NOT NULL)";
|
+ " BLOB NOT NULL)";
|
||||||
|
|
||||||
|
private static final String TRUE = "1";
|
||||||
|
|
||||||
private final DatabaseProvider databaseProvider;
|
private final DatabaseProvider databaseProvider;
|
||||||
|
|
||||||
private boolean initialized;
|
private boolean initialized;
|
||||||
|
|
@ -175,32 +179,15 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
||||||
public DownloadStateCursor getDownloadStates(@DownloadState.State int... states)
|
public DownloadStateCursor getDownloadStates(@DownloadState.State int... states)
|
||||||
throws DatabaseIOException {
|
throws DatabaseIOException {
|
||||||
ensureInitialized();
|
ensureInitialized();
|
||||||
try {
|
Cursor cursor = getCursor(getStateQuery(states), /* selectionArgs= */ null);
|
||||||
String selection = null;
|
return new DownloadStateCursorImpl(cursor);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void putDownloadState(DownloadState downloadState) throws DatabaseIOException {
|
public void putDownloadState(DownloadState downloadState) throws DatabaseIOException {
|
||||||
ensureInitialized();
|
ensureInitialized();
|
||||||
|
Assertions.checkState(downloadState.state != DownloadState.STATE_REMOVED);
|
||||||
try {
|
try {
|
||||||
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
|
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(COLUMN_ID, downloadState.id);
|
values.put(COLUMN_ID, downloadState.id);
|
||||||
values.put(COLUMN_TYPE, downloadState.type);
|
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_UPDATE_TIME_MS, downloadState.updateTimeMs);
|
||||||
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(downloadState.streamKeys));
|
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(downloadState.streamKeys));
|
||||||
values.put(COLUMN_CUSTOM_METADATA, downloadState.customMetadata);
|
values.put(COLUMN_CUSTOM_METADATA, downloadState.customMetadata);
|
||||||
|
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
|
||||||
writableDatabase.replaceOrThrow(TABLE_NAME, /* nullColumnHack= */ null, values);
|
writableDatabase.replaceOrThrow(TABLE_NAME, /* nullColumnHack= */ null, values);
|
||||||
} catch (SQLiteException e) {
|
} catch (SQLiteException e) {
|
||||||
throw new DatabaseIOException(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 {
|
private void ensureInitialized() throws DatabaseIOException {
|
||||||
if (initialized) {
|
if (initialized) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -264,18 +271,39 @@ public final class DefaultDownloadIndex implements DownloadIndex {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Cursor getCursor(@Nullable String selection, @Nullable String[] selectionArgs) {
|
private Cursor getCursor(String selection, @Nullable String[] selectionArgs)
|
||||||
String sortOrder = COLUMN_START_TIME_MS + " ASC";
|
throws DatabaseIOException {
|
||||||
return databaseProvider
|
try {
|
||||||
.getReadableDatabase()
|
String sortOrder = COLUMN_START_TIME_MS + " ASC";
|
||||||
.query(
|
return databaseProvider
|
||||||
TABLE_NAME,
|
.getReadableDatabase()
|
||||||
COLUMNS,
|
.query(
|
||||||
selection,
|
TABLE_NAME,
|
||||||
selectionArgs,
|
COLUMNS,
|
||||||
/* groupBy= */ null,
|
selection,
|
||||||
/* having= */ null,
|
selectionArgs,
|
||||||
sortOrder);
|
/* 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) {
|
private static DownloadState getDownloadStateForCurrentRow(Cursor cursor) {
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ public class DefaultDownloadIndexTest {
|
||||||
DownloadState downloadState1 =
|
DownloadState downloadState1 =
|
||||||
new DownloadStateBuilder("id1")
|
new DownloadStateBuilder("id1")
|
||||||
.setStartTimeMs(0)
|
.setStartTimeMs(0)
|
||||||
.setState(DownloadState.STATE_REMOVED)
|
.setState(DownloadState.STATE_REMOVING)
|
||||||
.build();
|
.build();
|
||||||
downloadIndex.putDownloadState(downloadState1);
|
downloadIndex.putDownloadState(downloadState1);
|
||||||
DownloadState downloadState2 =
|
DownloadState downloadState2 =
|
||||||
|
|
@ -173,7 +173,8 @@ public class DefaultDownloadIndexTest {
|
||||||
downloadIndex.putDownloadState(downloadState3);
|
downloadIndex.putDownloadState(downloadState3);
|
||||||
|
|
||||||
DownloadStateCursor cursor =
|
DownloadStateCursor cursor =
|
||||||
downloadIndex.getDownloadStates(DownloadState.STATE_REMOVED, DownloadState.STATE_COMPLETED);
|
downloadIndex.getDownloadStates(
|
||||||
|
DownloadState.STATE_REMOVING, DownloadState.STATE_COMPLETED);
|
||||||
|
|
||||||
assertThat(cursor.getCount()).isEqualTo(2);
|
assertThat(cursor.getCount()).isEqualTo(2);
|
||||||
cursor.moveToNext();
|
cursor.moveToNext();
|
||||||
|
|
@ -218,4 +219,56 @@ public class DefaultDownloadIndexTest {
|
||||||
VersionTable.getVersion(writableDatabase, VersionTable.FEATURE_OFFLINE, INSTANCE_UID))
|
VersionTable.getVersion(writableDatabase, VersionTable.FEATURE_OFFLINE, INSTANCE_UID))
|
||||||
.isEqualTo(DefaultDownloadIndex.TABLE_VERSION);
|
.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(
|
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) {
|
DownloadState downloadState, DownloadState that, boolean compareTimeFields) {
|
||||||
if (downloadState.state != that.state) {
|
assertThat(downloadState.state).isEqualTo(that.state);
|
||||||
return false;
|
assertThat(downloadState.downloadPercentage).isEqualTo(that.downloadPercentage);
|
||||||
}
|
assertThat(downloadState.downloadedBytes).isEqualTo(that.downloadedBytes);
|
||||||
if (Float.compare(that.downloadPercentage, downloadState.downloadPercentage) != 0) {
|
assertThat(downloadState.totalBytes).isEqualTo(that.totalBytes);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (downloadState.downloadedBytes != that.downloadedBytes) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (downloadState.totalBytes != that.totalBytes) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (compareTimeFields) {
|
if (compareTimeFields) {
|
||||||
if (downloadState.startTimeMs != that.startTimeMs) {
|
assertThat(downloadState.startTimeMs).isEqualTo(that.startTimeMs);
|
||||||
return false;
|
assertThat(downloadState.updateTimeMs).isEqualTo(that.updateTimeMs);
|
||||||
}
|
|
||||||
if (downloadState.updateTimeMs != that.updateTimeMs) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (downloadState.failureReason != that.failureReason) {
|
assertThat(downloadState.failureReason).isEqualTo(that.failureReason);
|
||||||
return false;
|
assertThat(downloadState.manualStopReason).isEqualTo(that.manualStopReason);
|
||||||
}
|
assertThat(downloadState.notMetRequirements).isEqualTo(that.notMetRequirements);
|
||||||
if (downloadState.manualStopReason != that.manualStopReason) {
|
assertThat(downloadState.id).isEqualTo(that.id);
|
||||||
return false;
|
assertThat(downloadState.type).isEqualTo(that.type);
|
||||||
}
|
assertThat(downloadState.uri).isEqualTo(that.uri);
|
||||||
if (downloadState.notMetRequirements != that.notMetRequirements) {
|
assertThat(downloadState.cacheKey).isEqualTo(that.cacheKey);
|
||||||
return false;
|
assertThat(Arrays.asList(downloadState.streamKeys)).containsExactlyElementsIn(that.streamKeys);
|
||||||
}
|
assertThat(downloadState.customMetadata).isEqualTo(that.customMetadata);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private DownloadAction createDownloadAction() {
|
private DownloadAction createDownloadAction() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue