Commit 8688bd2d by eguven Committed by Oliver Woodman

Rename DownloadState to Download

PiperOrigin-RevId: 242839480
parent f0cd144b
Showing with 216 additions and 228 deletions
...@@ -16,9 +16,9 @@ ...@@ -16,9 +16,9 @@
package com.google.android.exoplayer2.demo; package com.google.android.exoplayer2.demo;
import android.app.Notification; import android.app.Notification;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.DownloadState;
import com.google.android.exoplayer2.scheduler.PlatformScheduler; import com.google.android.exoplayer2.scheduler.PlatformScheduler;
import com.google.android.exoplayer2.ui.DownloadNotificationHelper; import com.google.android.exoplayer2.ui.DownloadNotificationHelper;
import com.google.android.exoplayer2.util.NotificationUtil; import com.google.android.exoplayer2.util.NotificationUtil;
...@@ -61,26 +61,26 @@ public class DemoDownloadService extends DownloadService { ...@@ -61,26 +61,26 @@ public class DemoDownloadService extends DownloadService {
} }
@Override @Override
protected Notification getForegroundNotification(DownloadState[] downloadStates) { protected Notification getForegroundNotification(Download[] downloads) {
return notificationHelper.buildProgressNotification( return notificationHelper.buildProgressNotification(
R.drawable.ic_download, /* contentIntent= */ null, /* message= */ null, downloadStates); R.drawable.ic_download, /* contentIntent= */ null, /* message= */ null, downloads);
} }
@Override @Override
protected void onDownloadStateChanged(DownloadState downloadState) { protected void onDownloadChanged(Download download) {
Notification notification; Notification notification;
if (downloadState.state == DownloadState.STATE_COMPLETED) { if (download.state == Download.STATE_COMPLETED) {
notification = notification =
notificationHelper.buildDownloadCompletedNotification( notificationHelper.buildDownloadCompletedNotification(
R.drawable.ic_download_done, R.drawable.ic_download_done,
/* contentIntent= */ null, /* contentIntent= */ null,
Util.fromUtf8Bytes(downloadState.action.data)); Util.fromUtf8Bytes(download.action.data));
} else if (downloadState.state == DownloadState.STATE_FAILED) { } else if (download.state == Download.STATE_FAILED) {
notification = notification =
notificationHelper.buildDownloadFailedNotification( notificationHelper.buildDownloadFailedNotification(
R.drawable.ic_download_done, R.drawable.ic_download_done,
/* contentIntent= */ null, /* contentIntent= */ null,
Util.fromUtf8Bytes(downloadState.action.data)); Util.fromUtf8Bytes(download.action.data));
} else { } else {
return; return;
} }
......
...@@ -25,12 +25,12 @@ import com.google.android.exoplayer2.C; ...@@ -25,12 +25,12 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.RenderersFactory; import com.google.android.exoplayer2.RenderersFactory;
import com.google.android.exoplayer2.offline.ActionFile; import com.google.android.exoplayer2.offline.ActionFile;
import com.google.android.exoplayer2.offline.DefaultDownloadIndex; import com.google.android.exoplayer2.offline.DefaultDownloadIndex;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.DownloadAction; import com.google.android.exoplayer2.offline.DownloadAction;
import com.google.android.exoplayer2.offline.DownloadCursor;
import com.google.android.exoplayer2.offline.DownloadHelper; import com.google.android.exoplayer2.offline.DownloadHelper;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.DownloadState;
import com.google.android.exoplayer2.offline.DownloadStateCursor;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo; import com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
...@@ -63,7 +63,7 @@ public class DownloadTracker implements DownloadManager.Listener { ...@@ -63,7 +63,7 @@ public class DownloadTracker implements DownloadManager.Listener {
private final Context context; private final Context context;
private final DataSource.Factory dataSourceFactory; private final DataSource.Factory dataSourceFactory;
private final CopyOnWriteArraySet<Listener> listeners; private final CopyOnWriteArraySet<Listener> listeners;
private final HashMap<Uri, DownloadState> downloadStates; private final HashMap<Uri, Download> downloads;
private final DefaultDownloadIndex downloadIndex; private final DefaultDownloadIndex downloadIndex;
@Nullable private StartDownloadDialogHelper startDownloadDialogHelper; @Nullable private StartDownloadDialogHelper startDownloadDialogHelper;
...@@ -74,7 +74,7 @@ public class DownloadTracker implements DownloadManager.Listener { ...@@ -74,7 +74,7 @@ public class DownloadTracker implements DownloadManager.Listener {
this.dataSourceFactory = dataSourceFactory; this.dataSourceFactory = dataSourceFactory;
this.downloadIndex = downloadIndex; this.downloadIndex = downloadIndex;
listeners = new CopyOnWriteArraySet<>(); listeners = new CopyOnWriteArraySet<>();
downloadStates = new HashMap<>(); downloads = new HashMap<>();
loadDownloads(); loadDownloads();
} }
...@@ -87,15 +87,15 @@ public class DownloadTracker implements DownloadManager.Listener { ...@@ -87,15 +87,15 @@ public class DownloadTracker implements DownloadManager.Listener {
} }
public boolean isDownloaded(Uri uri) { public boolean isDownloaded(Uri uri) {
DownloadState downloadState = downloadStates.get(uri); Download download = downloads.get(uri);
return downloadState != null && downloadState.state != DownloadState.STATE_FAILED; return download != null && download.state != Download.STATE_FAILED;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<StreamKey> getOfflineStreamKeys(Uri uri) { public List<StreamKey> getOfflineStreamKeys(Uri uri) {
DownloadState downloadState = downloadStates.get(uri); Download download = downloads.get(uri);
return downloadState != null && downloadState.state != DownloadState.STATE_FAILED return download != null && download.state != Download.STATE_FAILED
? downloadState.action.streamKeys ? download.action.streamKeys
: Collections.emptyList(); : Collections.emptyList();
} }
...@@ -105,10 +105,10 @@ public class DownloadTracker implements DownloadManager.Listener { ...@@ -105,10 +105,10 @@ public class DownloadTracker implements DownloadManager.Listener {
Uri uri, Uri uri,
String extension, String extension,
RenderersFactory renderersFactory) { RenderersFactory renderersFactory) {
DownloadState downloadState = downloadStates.get(uri); Download download = downloads.get(uri);
if (downloadState != null) { if (download != null) {
DownloadService.startWithRemoveDownload( DownloadService.startWithRemoveDownload(
context, DemoDownloadService.class, downloadState.action.id, /* foreground= */ false); context, DemoDownloadService.class, download.action.id, /* foreground= */ false);
} else { } else {
if (startDownloadDialogHelper != null) { if (startDownloadDialogHelper != null) {
startDownloadDialogHelper.release(); startDownloadDialogHelper.release();
...@@ -122,16 +122,16 @@ public class DownloadTracker implements DownloadManager.Listener { ...@@ -122,16 +122,16 @@ public class DownloadTracker implements DownloadManager.Listener {
// DownloadManager.Listener // DownloadManager.Listener
@Override @Override
public void onDownloadStateChanged(DownloadManager downloadManager, DownloadState downloadState) { public void onDownloadChanged(DownloadManager downloadManager, Download download) {
downloadStates.put(downloadState.action.uri, downloadState); downloads.put(download.action.uri, download);
for (Listener listener : listeners) { for (Listener listener : listeners) {
listener.onDownloadsChanged(); listener.onDownloadsChanged();
} }
} }
@Override @Override
public void onDownloadRemoved(DownloadManager downloadManager, DownloadState downloadState) { public void onDownloadRemoved(DownloadManager downloadManager, Download download) {
downloadStates.remove(downloadState.action.uri); downloads.remove(download.action.uri);
for (Listener listener : listeners) { for (Listener listener : listeners) {
listener.onDownloadsChanged(); listener.onDownloadsChanged();
} }
...@@ -140,10 +140,10 @@ public class DownloadTracker implements DownloadManager.Listener { ...@@ -140,10 +140,10 @@ public class DownloadTracker implements DownloadManager.Listener {
// Internal methods // Internal methods
private void loadDownloads() { private void loadDownloads() {
try (DownloadStateCursor loadedDownloadStates = downloadIndex.getDownloadStates()) { try (DownloadCursor loadedDownloads = downloadIndex.getDownloads()) {
while (loadedDownloadStates.moveToNext()) { while (loadedDownloads.moveToNext()) {
DownloadState downloadState = loadedDownloadStates.getDownloadState(); Download download = loadedDownloads.getDownload();
downloadStates.put(downloadState.action.uri, downloadState); downloads.put(download.action.uri, download);
} }
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "Failed to query download states", e); Log.w(TAG, "Failed to query download states", e);
......
...@@ -33,7 +33,7 @@ import java.util.ArrayList; ...@@ -33,7 +33,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* A {@link DownloadIndex} which uses SQLite to persist {@link DownloadState}s. * A {@link DownloadIndex} which uses SQLite to persist {@link Download}s.
* *
* <p class="caution">Database access may take a long time, do not call methods of this class from * <p class="caution">Database access may take a long time, do not call methods of this class from
* the application main thread. * the application main thread.
...@@ -88,7 +88,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -88,7 +88,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
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 = private static final String WHERE_STATE_TERMINAL =
getStateQuery(DownloadState.STATE_COMPLETED, DownloadState.STATE_FAILED); getStateQuery(Download.STATE_COMPLETED, Download.STATE_FAILED);
private static final String[] COLUMNS = private static final String[] COLUMNS =
new String[] { new String[] {
...@@ -153,8 +153,8 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -153,8 +153,8 @@ public final class DefaultDownloadIndex implements DownloadIndex {
private boolean initialized; private boolean initialized;
/** /**
* Creates a DefaultDownloadIndex which stores the {@link DownloadState}s on a SQLite database * Creates a DefaultDownloadIndex which stores the {@link Download}s on a SQLite database provided
* provided by {@code databaseProvider}. * by {@code databaseProvider}.
* *
* @param databaseProvider A DatabaseProvider which provides the database which will be used to * @param databaseProvider A DatabaseProvider which provides the database which will be used to
* store DownloadStatus table. * store DownloadStatus table.
...@@ -165,52 +165,51 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -165,52 +165,51 @@ public final class DefaultDownloadIndex implements DownloadIndex {
@Override @Override
@Nullable @Nullable
public DownloadState getDownloadState(String id) throws DatabaseIOException { public Download getDownload(String id) throws DatabaseIOException {
ensureInitialized(); ensureInitialized();
try (Cursor cursor = getCursor(WHERE_ID_EQUALS, new String[] {id})) { try (Cursor cursor = getCursor(WHERE_ID_EQUALS, new String[] {id})) {
if (cursor.getCount() == 0) { if (cursor.getCount() == 0) {
return null; return null;
} }
cursor.moveToNext(); cursor.moveToNext();
return getDownloadStateForCurrentRow(cursor); return getDownloadForCurrentRow(cursor);
} catch (SQLiteException e) { } catch (SQLiteException e) {
throw new DatabaseIOException(e); throw new DatabaseIOException(e);
} }
} }
@Override @Override
public DownloadStateCursor getDownloadStates(@DownloadState.State int... states) public DownloadCursor getDownloads(@Download.State int... states) throws DatabaseIOException {
throws DatabaseIOException {
ensureInitialized(); ensureInitialized();
Cursor cursor = getCursor(getStateQuery(states), /* selectionArgs= */ null); Cursor cursor = getCursor(getStateQuery(states), /* selectionArgs= */ null);
return new DownloadStateCursorImpl(cursor); return new DownloadCursorImpl(cursor);
} }
/** /**
* Adds or replaces a {@link DownloadState}. * Adds or replaces a {@link Download}.
* *
* @param downloadState The {@link DownloadState} to be added. * @param download The {@link Download} to be added.
* @throws DatabaseIOException If an error occurs setting the state. * @throws DatabaseIOException If an error occurs setting the state.
*/ */
public void putDownloadState(DownloadState downloadState) throws DatabaseIOException { public void putDownload(Download download) throws DatabaseIOException {
ensureInitialized(); ensureInitialized();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(COLUMN_ID, downloadState.action.id); values.put(COLUMN_ID, download.action.id);
values.put(COLUMN_TYPE, downloadState.action.type); values.put(COLUMN_TYPE, download.action.type);
values.put(COLUMN_URI, downloadState.action.uri.toString()); values.put(COLUMN_URI, download.action.uri.toString());
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(downloadState.action.streamKeys)); values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(download.action.streamKeys));
values.put(COLUMN_CUSTOM_CACHE_KEY, downloadState.action.customCacheKey); values.put(COLUMN_CUSTOM_CACHE_KEY, download.action.customCacheKey);
values.put(COLUMN_DATA, downloadState.action.data); values.put(COLUMN_DATA, download.action.data);
values.put(COLUMN_STATE, downloadState.state); values.put(COLUMN_STATE, download.state);
values.put(COLUMN_DOWNLOAD_PERCENTAGE, downloadState.getDownloadPercentage()); values.put(COLUMN_DOWNLOAD_PERCENTAGE, download.getDownloadPercentage());
values.put(COLUMN_DOWNLOADED_BYTES, downloadState.getDownloadedBytes()); values.put(COLUMN_DOWNLOADED_BYTES, download.getDownloadedBytes());
values.put(COLUMN_TOTAL_BYTES, downloadState.getTotalBytes()); values.put(COLUMN_TOTAL_BYTES, download.getTotalBytes());
values.put(COLUMN_FAILURE_REASON, downloadState.failureReason); values.put(COLUMN_FAILURE_REASON, download.failureReason);
values.put(COLUMN_STOP_FLAGS, 0); values.put(COLUMN_STOP_FLAGS, 0);
values.put(COLUMN_NOT_MET_REQUIREMENTS, 0); values.put(COLUMN_NOT_MET_REQUIREMENTS, 0);
values.put(COLUMN_MANUAL_STOP_REASON, downloadState.manualStopReason); values.put(COLUMN_MANUAL_STOP_REASON, download.manualStopReason);
values.put(COLUMN_START_TIME_MS, downloadState.startTimeMs); values.put(COLUMN_START_TIME_MS, download.startTimeMs);
values.put(COLUMN_UPDATE_TIME_MS, downloadState.updateTimeMs); values.put(COLUMN_UPDATE_TIME_MS, download.updateTimeMs);
try { try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase(); SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.replaceOrThrow(TABLE_NAME, /* nullColumnHack= */ null, values); writableDatabase.replaceOrThrow(TABLE_NAME, /* nullColumnHack= */ null, values);
...@@ -220,12 +219,12 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -220,12 +219,12 @@ public final class DefaultDownloadIndex implements DownloadIndex {
} }
/** /**
* Removes the {@link DownloadState} with the given {@code id}. * Removes the {@link Download} with the given {@code id}.
* *
* @param id ID of a {@link DownloadState}. * @param id ID of a {@link Download}.
* @throws DatabaseIOException If an error occurs removing the state. * @throws DatabaseIOException If an error occurs removing the state.
*/ */
public void removeDownloadState(String id) throws DatabaseIOException { public void removeDownload(String id) throws DatabaseIOException {
ensureInitialized(); ensureInitialized();
try { try {
databaseProvider.getWritableDatabase().delete(TABLE_NAME, WHERE_ID_EQUALS, new String[] {id}); databaseProvider.getWritableDatabase().delete(TABLE_NAME, WHERE_ID_EQUALS, new String[] {id});
...@@ -236,7 +235,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -236,7 +235,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
/** /**
* Sets the manual stop reason of the downloads in a terminal state ({@link * Sets the manual stop reason of the downloads in a terminal state ({@link
* DownloadState#STATE_COMPLETED}, {@link DownloadState#STATE_FAILED}). * Download#STATE_COMPLETED}, {@link Download#STATE_FAILED}).
* *
* @param manualStopReason The manual stop reason. * @param manualStopReason The manual stop reason.
* @throws DatabaseIOException If an error occurs updating the state. * @throws DatabaseIOException If an error occurs updating the state.
...@@ -255,12 +254,12 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -255,12 +254,12 @@ public final class DefaultDownloadIndex implements DownloadIndex {
/** /**
* Sets the manual stop reason of the download with the given {@code id} in a terminal state * 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}). * ({@link Download#STATE_COMPLETED}, {@link Download#STATE_FAILED}).
* *
* <p>If there's no {@link DownloadState} with the given {@code id} or it isn't in a terminal * <p>If there's no {@link Download} with the given {@code id} or it isn't in a terminal state,
* state, then nothing happens. * then nothing happens.
* *
* @param id ID of a {@link DownloadState}. * @param id ID of a {@link Download}.
* @param manualStopReason The manual stop reason. * @param manualStopReason The manual stop reason.
* @throws DatabaseIOException If an error occurs updating the state. * @throws DatabaseIOException If an error occurs updating the state.
*/ */
...@@ -326,7 +325,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -326,7 +325,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
} }
} }
private static String getStateQuery(@DownloadState.State int... states) { private static String getStateQuery(@Download.State int... states) {
if (states.length == 0) { if (states.length == 0) {
return TRUE; return TRUE;
} }
...@@ -342,7 +341,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -342,7 +341,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
return selectionBuilder.toString(); return selectionBuilder.toString();
} }
private static DownloadState getDownloadStateForCurrentRow(Cursor cursor) { private static Download getDownloadForCurrentRow(Cursor cursor) {
DownloadAction action = DownloadAction action =
new DownloadAction( new DownloadAction(
cursor.getString(COLUMN_INDEX_ID), cursor.getString(COLUMN_INDEX_ID),
...@@ -355,7 +354,7 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -355,7 +354,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
cachingCounters.alreadyCachedBytes = cursor.getLong(COLUMN_INDEX_DOWNLOADED_BYTES); cachingCounters.alreadyCachedBytes = cursor.getLong(COLUMN_INDEX_DOWNLOADED_BYTES);
cachingCounters.contentLength = cursor.getLong(COLUMN_INDEX_TOTAL_BYTES); cachingCounters.contentLength = cursor.getLong(COLUMN_INDEX_TOTAL_BYTES);
cachingCounters.percentage = cursor.getFloat(COLUMN_INDEX_DOWNLOAD_PERCENTAGE); cachingCounters.percentage = cursor.getFloat(COLUMN_INDEX_DOWNLOAD_PERCENTAGE);
return new DownloadState( return new Download(
action, action,
cursor.getInt(COLUMN_INDEX_STATE), cursor.getInt(COLUMN_INDEX_STATE),
cursor.getInt(COLUMN_INDEX_FAILURE_REASON), cursor.getInt(COLUMN_INDEX_FAILURE_REASON),
...@@ -401,17 +400,17 @@ public final class DefaultDownloadIndex implements DownloadIndex { ...@@ -401,17 +400,17 @@ public final class DefaultDownloadIndex implements DownloadIndex {
return streamKeys; return streamKeys;
} }
private static final class DownloadStateCursorImpl implements DownloadStateCursor { private static final class DownloadCursorImpl implements DownloadCursor {
private final Cursor cursor; private final Cursor cursor;
private DownloadStateCursorImpl(Cursor cursor) { private DownloadCursorImpl(Cursor cursor) {
this.cursor = cursor; this.cursor = cursor;
} }
@Override @Override
public DownloadState getDownloadState() { public Download getDownload() {
return getDownloadStateForCurrentRow(cursor); return getDownloadForCurrentRow(cursor);
} }
@Override @Override
......
...@@ -24,7 +24,7 @@ import java.lang.annotation.Retention; ...@@ -24,7 +24,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
/** Represents state of a download. */ /** Represents state of a download. */
public final class DownloadState { public final class Download {
/** /**
* Download states. One of {@link #STATE_QUEUED}, {@link #STATE_STOPPED}, {@link * Download states. One of {@link #STATE_QUEUED}, {@link #STATE_STOPPED}, {@link
...@@ -128,15 +128,15 @@ public final class DownloadState { ...@@ -128,15 +128,15 @@ public final class DownloadState {
/*package*/ CachingCounters counters; /*package*/ CachingCounters counters;
/** /**
* Creates a {@link DownloadState} using a {@link DownloadAction}. * Creates a {@link Download} using a {@link DownloadAction}.
* *
* @param action The {@link DownloadAction}. * @param action The {@link DownloadAction}.
*/ */
public DownloadState(DownloadAction action) { public Download(DownloadAction action) {
this(action, System.currentTimeMillis()); this(action, System.currentTimeMillis());
} }
private DownloadState(DownloadAction action, long currentTimeMs) { private Download(DownloadAction action, long currentTimeMs) {
this( this(
action, action,
/* state= */ STATE_QUEUED, /* state= */ STATE_QUEUED,
...@@ -147,7 +147,7 @@ public final class DownloadState { ...@@ -147,7 +147,7 @@ public final class DownloadState {
new CachingCounters()); new CachingCounters());
} }
/* package */ DownloadState( /* package */ Download(
DownloadAction action, DownloadAction action,
@State int state, @State int state,
@FailureReason int failureReason, @FailureReason int failureReason,
...@@ -170,15 +170,15 @@ public final class DownloadState { ...@@ -170,15 +170,15 @@ public final class DownloadState {
} }
/** /**
* Merges the given {@link DownloadAction} and creates a new {@link DownloadState}. The action * Merges the given {@link DownloadAction} and creates a new {@link Download}. The action must
* must have the same id and type. * have the same id and type.
* *
* @param newAction The {@link DownloadAction} to be merged. * @param newAction The {@link DownloadAction} to be merged.
* @param canStart Whether the download is eligible to be started. * @param canStart Whether the download is eligible to be started.
* @return A new {@link DownloadState}. * @return A new {@link Download}.
*/ */
public DownloadState copyWithMergedAction(DownloadAction newAction, boolean canStart) { public Download copyWithMergedAction(DownloadAction newAction, boolean canStart) {
return new DownloadState( return new Download(
action.copyWithMergedAction(newAction), action.copyWithMergedAction(newAction),
getNextState(state, canStart && manualStopReason == 0), getNextState(state, canStart && manualStopReason == 0),
FAILURE_REASON_NONE, FAILURE_REASON_NONE,
...@@ -193,8 +193,8 @@ public final class DownloadState { ...@@ -193,8 +193,8 @@ public final class DownloadState {
* *
* @param state The {@link State}. * @param state The {@link State}.
*/ */
public DownloadState copyWithState(@State int state) { public Download copyWithState(@State int state) {
return new DownloadState( return new Download(
action, action,
state, state,
FAILURE_REASON_NONE, FAILURE_REASON_NONE,
......
...@@ -18,19 +18,19 @@ package com.google.android.exoplayer2.offline; ...@@ -18,19 +18,19 @@ package com.google.android.exoplayer2.offline;
import java.io.Closeable; import java.io.Closeable;
/** Provides random read-write access to the result set returned by a database query. */ /** Provides random read-write access to the result set returned by a database query. */
public interface DownloadStateCursor extends Closeable { public interface DownloadCursor extends Closeable {
/** Returns the DownloadState at the current position. */ /** Returns the download at the current position. */
DownloadState getDownloadState(); Download getDownload();
/** Returns the numbers of DownloadStates in the cursor. */ /** Returns the numbers of downloads in the cursor. */
int getCount(); int getCount();
/** /**
* Returns the current position of the cursor in the DownloadState set. The value is zero-based. * Returns the current position of the cursor in the download set. The value is zero-based. When
* When the DownloadState set is first returned the cursor will be at positon -1, which is before * the download set is first returned the cursor will be at positon -1, which is before the first
* the first DownloadState. After the last DownloadState is returned another call to next() will * download. After the last download is returned another call to next() will leave the cursor past
* leave the cursor past the last entry, at a position of count(). * the last entry, at a position of count().
* *
* @return the current cursor position. * @return the current cursor position.
*/ */
...@@ -49,7 +49,7 @@ public interface DownloadStateCursor extends Closeable { ...@@ -49,7 +49,7 @@ public interface DownloadStateCursor extends Closeable {
boolean moveToPosition(int position); boolean moveToPosition(int position);
/** /**
* Move the cursor to the first DownloadState. * Move the cursor to the first download.
* *
* <p>This method will return false if the cursor is empty. * <p>This method will return false if the cursor is empty.
* *
...@@ -60,7 +60,7 @@ public interface DownloadStateCursor extends Closeable { ...@@ -60,7 +60,7 @@ public interface DownloadStateCursor extends Closeable {
} }
/** /**
* Move the cursor to the last DownloadState. * Move the cursor to the last download.
* *
* <p>This method will return false if the cursor is empty. * <p>This method will return false if the cursor is empty.
* *
...@@ -71,7 +71,7 @@ public interface DownloadStateCursor extends Closeable { ...@@ -71,7 +71,7 @@ public interface DownloadStateCursor extends Closeable {
} }
/** /**
* Move the cursor to the next DownloadState. * Move the cursor to the next download.
* *
* <p>This method will return false if the cursor is already past the last entry in the result * <p>This method will return false if the cursor is already past the last entry in the result
* set. * set.
...@@ -83,7 +83,7 @@ public interface DownloadStateCursor extends Closeable { ...@@ -83,7 +83,7 @@ public interface DownloadStateCursor extends Closeable {
} }
/** /**
* Move the cursor to the previous DownloadState. * Move the cursor to the previous download.
* *
* <p>This method will return false if the cursor is already before the first entry in the result * <p>This method will return false if the cursor is already before the first entry in the result
* set. * set.
...@@ -94,18 +94,18 @@ public interface DownloadStateCursor extends Closeable { ...@@ -94,18 +94,18 @@ public interface DownloadStateCursor extends Closeable {
return moveToPosition(getPosition() - 1); return moveToPosition(getPosition() - 1);
} }
/** Returns whether the cursor is pointing to the first DownloadState. */ /** Returns whether the cursor is pointing to the first download. */
default boolean isFirst() { default boolean isFirst() {
return getPosition() == 0 && getCount() != 0; return getPosition() == 0 && getCount() != 0;
} }
/** Returns whether the cursor is pointing to the last DownloadState. */ /** Returns whether the cursor is pointing to the last download. */
default boolean isLast() { default boolean isLast() {
int count = getCount(); int count = getCount();
return getPosition() == (count - 1) && count != 0; return getPosition() == (count - 1) && count != 0;
} }
/** Returns whether the cursor is pointing to the position before the first DownloadState. */ /** Returns whether the cursor is pointing to the position before the first download. */
default boolean isBeforeFirst() { default boolean isBeforeFirst() {
if (getCount() == 0) { if (getCount() == 0) {
return true; return true;
...@@ -113,7 +113,7 @@ public interface DownloadStateCursor extends Closeable { ...@@ -113,7 +113,7 @@ public interface DownloadStateCursor extends Closeable {
return getPosition() == -1; return getPosition() == -1;
} }
/** Returns whether the cursor is pointing to the position after the last DownloadState. */ /** Returns whether the cursor is pointing to the position after the last download. */
default boolean isAfterLast() { default boolean isAfterLast() {
if (getCount() == 0) { if (getCount() == 0) {
return true; return true;
......
...@@ -18,27 +18,26 @@ package com.google.android.exoplayer2.offline; ...@@ -18,27 +18,26 @@ package com.google.android.exoplayer2.offline;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
/** Persists {@link DownloadState}s. */ /** Persists {@link Download}s. */
public interface DownloadIndex { public interface DownloadIndex {
/** /**
* Returns the {@link DownloadState} with the given {@code id}, or null. * Returns the {@link Download} with the given {@code id}, or null.
* *
* @param id ID of a {@link DownloadState}. * @param id ID of a {@link Download}.
* @return The {@link DownloadState} with the given {@code id}, or null if a download state with * @return The {@link Download} with the given {@code id}, or null if a download state with this
* this id doesn't exist. * id doesn't exist.
* @throws IOException If an error occurs reading the state. * @throws IOException If an error occurs reading the state.
*/ */
@Nullable @Nullable
DownloadState getDownloadState(String id) throws IOException; Download getDownload(String id) throws IOException;
/** /**
* Returns a {@link DownloadStateCursor} to {@link DownloadState}s with the given {@code states}. * Returns a {@link DownloadCursor} to {@link Download}s with the given {@code states}.
* *
* @param states Returns only the {@link DownloadState}s with this states. If empty, returns all. * @param states Returns only the {@link Download}s with this states. If empty, returns all.
* @return A cursor to {@link DownloadState}s with the given {@code states}. * @return A cursor to {@link Download}s with the given {@code states}.
* @throws IOException If an error occurs reading the state. * @throws IOException If an error occurs reading the state.
*/ */
DownloadStateCursor getDownloadStates(@DownloadState.State int... states) throws IOException; DownloadCursor getDownloads(@Download.State int... states) throws IOException;
} }
...@@ -69,12 +69,12 @@ public final class DownloadIndexUtil { ...@@ -69,12 +69,12 @@ public final class DownloadIndexUtil {
*/ */
/* package */ static void mergeAction(DownloadAction action, DefaultDownloadIndex downloadIndex) /* package */ static void mergeAction(DownloadAction action, DefaultDownloadIndex downloadIndex)
throws IOException { throws IOException {
DownloadState downloadState = downloadIndex.getDownloadState(action.id); Download download = downloadIndex.getDownload(action.id);
if (downloadState != null) { if (download != null) {
downloadState = downloadState.copyWithMergedAction(action, /* canStart= */ true); download = download.copyWithMergedAction(action, /* canStart= */ true);
} else { } else {
downloadState = new DownloadState(action); download = new Download(action);
} }
downloadIndex.putDownloadState(downloadState); downloadIndex.putDownload(download);
} }
} }
...@@ -79,8 +79,8 @@ public abstract class DownloadService extends Service { ...@@ -79,8 +79,8 @@ public abstract class DownloadService extends Service {
* <li>{@link #KEY_CONTENT_ID} - The content id of a single download to stop. If omitted, all of * <li>{@link #KEY_CONTENT_ID} - The content id of a single download to stop. If omitted, all of
* the current downloads will be stopped. * the current downloads will be stopped.
* <li>{@link #KEY_STOP_REASON} - An application provided reason for stopping the download or * <li>{@link #KEY_STOP_REASON} - An application provided reason for stopping the download or
* downloads. Must not be {@link DownloadState#MANUAL_STOP_REASON_NONE}. If omitted, the * downloads. Must not be {@link Download#MANUAL_STOP_REASON_NONE}. If omitted, the stop
* stop reason will be {@link DownloadState#MANUAL_STOP_REASON_UNDEFINED}. * reason will be {@link Download#MANUAL_STOP_REASON_UNDEFINED}.
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}. * <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul> * </ul>
*/ */
...@@ -405,8 +405,7 @@ public abstract class DownloadService extends Service { ...@@ -405,8 +405,7 @@ public abstract class DownloadService extends Service {
break; break;
case ACTION_STOP: case ACTION_STOP:
contentId = intent.getStringExtra(KEY_CONTENT_ID); contentId = intent.getStringExtra(KEY_CONTENT_ID);
int stopReason = int stopReason = intent.getIntExtra(KEY_STOP_REASON, Download.MANUAL_STOP_REASON_UNDEFINED);
intent.getIntExtra(KEY_STOP_REASON, DownloadState.MANUAL_STOP_REASON_UNDEFINED);
if (contentId == null) { if (contentId == null) {
downloadManager.stopDownloads(stopReason); downloadManager.stopDownloads(stopReason);
} else { } else {
...@@ -477,7 +476,7 @@ public abstract class DownloadService extends Service { ...@@ -477,7 +476,7 @@ public abstract class DownloadService extends Service {
* downloads. The periodic update interval can be set using {@link #DownloadService(int, long)}. * downloads. The periodic update interval can be set using {@link #DownloadService(int, long)}.
* *
* <p>On API level 26 and above, this method may also be called just before the service stops, * <p>On API level 26 and above, this method may also be called just before the service stops,
* with an empty {@code downloadStates} array. The returned notification is used to satisfy system * with an empty {@code downloads} array. The returned notification is used to satisfy system
* requirements for foreground services. * requirements for foreground services.
* *
* <p>Download services that do not wish to run in the foreground should be created by setting the * <p>Download services that do not wish to run in the foreground should be created by setting the
...@@ -485,35 +484,35 @@ public abstract class DownloadService extends Service { ...@@ -485,35 +484,35 @@ public abstract class DownloadService extends Service {
* #FOREGROUND_NOTIFICATION_ID_NONE}. This method will not be called in this case, meaning it can * #FOREGROUND_NOTIFICATION_ID_NONE}. This method will not be called in this case, meaning it can
* be implemented to throw {@link UnsupportedOperationException}. * be implemented to throw {@link UnsupportedOperationException}.
* *
* @param downloadStates The states of all current downloads. * @param downloads The states of all current downloads.
* @return The foreground notification to display. * @return The foreground notification to display.
*/ */
protected abstract Notification getForegroundNotification(DownloadState[] downloadStates); protected abstract Notification getForegroundNotification(Download[] downloads);
/** /**
* Called when the state of a download changes. The default implementation is a no-op. * Called when the state of a download changes. The default implementation is a no-op.
* *
* @param downloadState The new state of the download. * @param download The new state of the download.
*/ */
protected void onDownloadStateChanged(DownloadState downloadState) { protected void onDownloadChanged(Download download) {
// Do nothing. // Do nothing.
} }
/** /**
* Called when a download is removed. The default implementation is a no-op. * Called when a download is removed. The default implementation is a no-op.
* *
* @param downloadState The last state of the download before it was removed. * @param download The last state of the download before it was removed.
*/ */
protected void onDownloadRemoved(DownloadState downloadState) { protected void onDownloadRemoved(Download download) {
// Do nothing. // Do nothing.
} }
private void notifyDownloadStateChange(DownloadState downloadState) { private void notifyDownloadChange(Download download) {
onDownloadStateChanged(downloadState); onDownloadChanged(download);
if (foregroundNotificationUpdater != null) { if (foregroundNotificationUpdater != null) {
if (downloadState.state == DownloadState.STATE_DOWNLOADING if (download.state == Download.STATE_DOWNLOADING
|| downloadState.state == DownloadState.STATE_REMOVING || download.state == Download.STATE_REMOVING
|| downloadState.state == DownloadState.STATE_RESTARTING) { || download.state == Download.STATE_RESTARTING) {
foregroundNotificationUpdater.startPeriodicUpdates(); foregroundNotificationUpdater.startPeriodicUpdates();
} else { } else {
foregroundNotificationUpdater.update(); foregroundNotificationUpdater.update();
...@@ -521,8 +520,8 @@ public abstract class DownloadService extends Service { ...@@ -521,8 +520,8 @@ public abstract class DownloadService extends Service {
} }
} }
private void notifyDownloadRemoved(DownloadState downloadState) { private void notifyDownloadRemoved(Download download) {
onDownloadRemoved(downloadState); onDownloadRemoved(download);
if (foregroundNotificationUpdater != null) { if (foregroundNotificationUpdater != null) {
foregroundNotificationUpdater.update(); foregroundNotificationUpdater.update();
} }
...@@ -582,8 +581,8 @@ public abstract class DownloadService extends Service { ...@@ -582,8 +581,8 @@ public abstract class DownloadService extends Service {
} }
public void update() { public void update() {
DownloadState[] downloadStates = downloadManager.getAllDownloadStates(); Download[] downloads = downloadManager.getAllDownloads();
startForeground(notificationId, getForegroundNotification(downloadStates)); startForeground(notificationId, getForegroundNotification(downloads));
notificationDisplayed = true; notificationDisplayed = true;
if (periodicUpdatesStarted) { if (periodicUpdatesStarted) {
handler.removeCallbacks(this); handler.removeCallbacks(this);
...@@ -641,17 +640,16 @@ public abstract class DownloadService extends Service { ...@@ -641,17 +640,16 @@ public abstract class DownloadService extends Service {
} }
@Override @Override
public void onDownloadStateChanged( public void onDownloadChanged(DownloadManager downloadManager, Download download) {
DownloadManager downloadManager, DownloadState downloadState) {
if (downloadService != null) { if (downloadService != null) {
downloadService.notifyDownloadStateChange(downloadState); downloadService.notifyDownloadChange(download);
} }
} }
@Override @Override
public void onDownloadRemoved(DownloadManager downloadManager, DownloadState downloadState) { public void onDownloadRemoved(DownloadManager downloadManager, Download download) {
if (downloadService != null) { if (downloadService != null) {
downloadService.notifyDownloadRemoved(downloadState); downloadService.notifyDownloadRemoved(download);
} }
} }
......
...@@ -23,13 +23,13 @@ import java.util.Collections; ...@@ -23,13 +23,13 @@ import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
* Builder for DownloadState. * Builder for {@link Download}.
* *
* <p>Defines default values for each field (except {@code id}) to facilitate DownloadState creation * <p>Defines default values for each field (except {@code id}) to facilitate {@link Download}
* for tests. Tests must avoid depending on the default values but explicitly set tested parameters * creation for tests. Tests must avoid depending on the default values but explicitly set tested
* during test initialization. * parameters during test initialization.
*/ */
class DownloadStateBuilder { class DownloadBuilder {
private final CachingCounters counters; private final CachingCounters counters;
private String id; private String id;
private String type; private String type;
...@@ -43,15 +43,15 @@ class DownloadStateBuilder { ...@@ -43,15 +43,15 @@ class DownloadStateBuilder {
private List<StreamKey> streamKeys; private List<StreamKey> streamKeys;
private byte[] customMetadata; private byte[] customMetadata;
DownloadStateBuilder(String id) { DownloadBuilder(String id) {
this(id, "type", Uri.parse("uri"), /* cacheKey= */ null, new byte[0], Collections.emptyList()); this(id, "type", Uri.parse("uri"), /* cacheKey= */ null, new byte[0], Collections.emptyList());
} }
DownloadStateBuilder(DownloadAction action) { DownloadBuilder(DownloadAction action) {
this(action.id, action.type, action.uri, action.customCacheKey, action.data, action.streamKeys); this(action.id, action.type, action.uri, action.customCacheKey, action.data, action.streamKeys);
} }
DownloadStateBuilder( DownloadBuilder(
String id, String id,
String type, String type,
Uri uri, Uri uri,
...@@ -62,8 +62,8 @@ class DownloadStateBuilder { ...@@ -62,8 +62,8 @@ class DownloadStateBuilder {
this.type = type; this.type = type;
this.uri = uri; this.uri = uri;
this.cacheKey = cacheKey; this.cacheKey = cacheKey;
this.state = DownloadState.STATE_QUEUED; this.state = Download.STATE_QUEUED;
this.failureReason = DownloadState.FAILURE_REASON_NONE; this.failureReason = Download.FAILURE_REASON_NONE;
this.startTimeMs = (long) 0; this.startTimeMs = (long) 0;
this.updateTimeMs = (long) 0; this.updateTimeMs = (long) 0;
this.streamKeys = streamKeys; this.streamKeys = streamKeys;
...@@ -71,90 +71,84 @@ class DownloadStateBuilder { ...@@ -71,90 +71,84 @@ class DownloadStateBuilder {
this.counters = new CachingCounters(); this.counters = new CachingCounters();
} }
public DownloadStateBuilder setId(String id) { public DownloadBuilder setId(String id) {
this.id = id; this.id = id;
return this; return this;
} }
public DownloadStateBuilder setType(String type) { public DownloadBuilder setType(String type) {
this.type = type; this.type = type;
return this; return this;
} }
public DownloadStateBuilder setUri(String uri) { public DownloadBuilder setUri(String uri) {
this.uri = Uri.parse(uri); this.uri = Uri.parse(uri);
return this; return this;
} }
public DownloadStateBuilder setUri(Uri uri) { public DownloadBuilder setUri(Uri uri) {
this.uri = uri; this.uri = uri;
return this; return this;
} }
public DownloadStateBuilder setCacheKey(@Nullable String cacheKey) { public DownloadBuilder setCacheKey(@Nullable String cacheKey) {
this.cacheKey = cacheKey; this.cacheKey = cacheKey;
return this; return this;
} }
public DownloadStateBuilder setState(int state) { public DownloadBuilder setState(int state) {
this.state = state; this.state = state;
return this; return this;
} }
public DownloadStateBuilder setDownloadPercentage(float downloadPercentage) { public DownloadBuilder setDownloadPercentage(float downloadPercentage) {
counters.percentage = downloadPercentage; counters.percentage = downloadPercentage;
return this; return this;
} }
public DownloadStateBuilder setDownloadedBytes(long downloadedBytes) { public DownloadBuilder setDownloadedBytes(long downloadedBytes) {
counters.alreadyCachedBytes = downloadedBytes; counters.alreadyCachedBytes = downloadedBytes;
return this; return this;
} }
public DownloadStateBuilder setTotalBytes(long totalBytes) { public DownloadBuilder setTotalBytes(long totalBytes) {
counters.contentLength = totalBytes; counters.contentLength = totalBytes;
return this; return this;
} }
public DownloadStateBuilder setFailureReason(int failureReason) { public DownloadBuilder setFailureReason(int failureReason) {
this.failureReason = failureReason; this.failureReason = failureReason;
return this; return this;
} }
public DownloadStateBuilder setManualStopReason(int manualStopReason) { public DownloadBuilder setManualStopReason(int manualStopReason) {
this.manualStopReason = manualStopReason; this.manualStopReason = manualStopReason;
return this; return this;
} }
public DownloadStateBuilder setStartTimeMs(long startTimeMs) { public DownloadBuilder setStartTimeMs(long startTimeMs) {
this.startTimeMs = startTimeMs; this.startTimeMs = startTimeMs;
return this; return this;
} }
public DownloadStateBuilder setUpdateTimeMs(long updateTimeMs) { public DownloadBuilder setUpdateTimeMs(long updateTimeMs) {
this.updateTimeMs = updateTimeMs; this.updateTimeMs = updateTimeMs;
return this; return this;
} }
public DownloadStateBuilder setStreamKeys(StreamKey... streamKeys) { public DownloadBuilder setStreamKeys(StreamKey... streamKeys) {
this.streamKeys = Arrays.asList(streamKeys); this.streamKeys = Arrays.asList(streamKeys);
return this; return this;
} }
public DownloadStateBuilder setCustomMetadata(byte[] customMetadata) { public DownloadBuilder setCustomMetadata(byte[] customMetadata) {
this.customMetadata = customMetadata; this.customMetadata = customMetadata;
return this; return this;
} }
public DownloadState build() { public Download build() {
DownloadAction action = new DownloadAction(id, type, uri, streamKeys, cacheKey, customMetadata); DownloadAction action = new DownloadAction(id, type, uri, streamKeys, cacheKey, customMetadata);
return new DownloadState( return new Download(
action, action, state, failureReason, manualStopReason, startTimeMs, updateTimeMs, counters);
state,
failureReason,
manualStopReason,
startTimeMs,
updateTimeMs,
counters);
} }
} }
...@@ -56,7 +56,7 @@ public class DownloadIndexUtilTest { ...@@ -56,7 +56,7 @@ public class DownloadIndexUtilTest {
} }
@Test @Test
public void addAction_nonExistingDownloadState_createsNewDownloadState() throws IOException { public void addAction_nonExistingDownload_createsNewDownload() throws IOException {
byte[] data = new byte[] {1, 2, 3, 4}; byte[] data = new byte[] {1, 2, 3, 4};
DownloadAction action = DownloadAction action =
new DownloadAction( new DownloadAction(
...@@ -71,11 +71,11 @@ public class DownloadIndexUtilTest { ...@@ -71,11 +71,11 @@ public class DownloadIndexUtilTest {
DownloadIndexUtil.mergeAction(action, downloadIndex); DownloadIndexUtil.mergeAction(action, downloadIndex);
assertDownloadIndexContainsAction(action, DownloadState.STATE_QUEUED); assertDownloadIndexContainsAction(action, Download.STATE_QUEUED);
} }
@Test @Test
public void addAction_existingDownloadState_createsMergedDownloadState() throws IOException { public void addAction_existingDownload_createsMergedDownload() throws IOException {
StreamKey streamKey1 = StreamKey streamKey1 =
new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5); new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5);
StreamKey streamKey2 = StreamKey streamKey2 =
...@@ -100,18 +100,18 @@ public class DownloadIndexUtilTest { ...@@ -100,18 +100,18 @@ public class DownloadIndexUtilTest {
DownloadIndexUtil.mergeAction(action2, downloadIndex); DownloadIndexUtil.mergeAction(action2, downloadIndex);
DownloadState downloadState = downloadIndex.getDownloadState(action2.id); Download download = downloadIndex.getDownload(action2.id);
assertThat(downloadState).isNotNull(); assertThat(download).isNotNull();
assertThat(downloadState.action.type).isEqualTo(action2.type); assertThat(download.action.type).isEqualTo(action2.type);
assertThat(downloadState.action.customCacheKey).isEqualTo(action2.customCacheKey); assertThat(download.action.customCacheKey).isEqualTo(action2.customCacheKey);
assertThat(downloadState.action.data).isEqualTo(action2.data); assertThat(download.action.data).isEqualTo(action2.data);
assertThat(downloadState.action.uri).isEqualTo(action2.uri); assertThat(download.action.uri).isEqualTo(action2.uri);
assertThat(downloadState.action.streamKeys).containsExactly(streamKey1, streamKey2); assertThat(download.action.streamKeys).containsExactly(streamKey1, streamKey2);
assertThat(downloadState.state).isEqualTo(DownloadState.STATE_QUEUED); assertThat(download.state).isEqualTo(Download.STATE_QUEUED);
} }
@Test @Test
public void upgradeActionFile_createsDownloadStates() throws IOException { public void upgradeActionFile_createsDownloads() throws IOException {
// Copy the test asset to a file. // Copy the test asset to a file.
byte[] actionFileBytes = byte[] actionFileBytes =
TestUtil.getByteArray( TestUtil.getByteArray(
...@@ -144,15 +144,15 @@ public class DownloadIndexUtilTest { ...@@ -144,15 +144,15 @@ public class DownloadIndexUtilTest {
ActionFile actionFile = new ActionFile(tempFile); ActionFile actionFile = new ActionFile(tempFile);
DownloadIndexUtil.mergeActionFile(actionFile, /* downloadIdProvider= */ null, downloadIndex); DownloadIndexUtil.mergeActionFile(actionFile, /* downloadIdProvider= */ null, downloadIndex);
assertDownloadIndexContainsAction(expectedAction1, DownloadState.STATE_QUEUED); assertDownloadIndexContainsAction(expectedAction1, Download.STATE_QUEUED);
assertDownloadIndexContainsAction(expectedAction2, DownloadState.STATE_QUEUED); assertDownloadIndexContainsAction(expectedAction2, Download.STATE_QUEUED);
} }
private void assertDownloadIndexContainsAction(DownloadAction action, int state) private void assertDownloadIndexContainsAction(DownloadAction action, int state)
throws IOException { throws IOException {
DownloadState downloadState = downloadIndex.getDownloadState(action.id); Download download = downloadIndex.getDownload(action.id);
assertThat(downloadState.action).isEqualTo(action); assertThat(download.action).isEqualTo(action);
assertThat(downloadState.state).isEqualTo(state); assertThat(download.state).isEqualTo(state);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
......
...@@ -20,7 +20,7 @@ import static com.google.common.truth.Truth.assertThat; ...@@ -20,7 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import android.net.Uri; import android.net.Uri;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.offline.DownloadState.State; import com.google.android.exoplayer2.offline.Download.State;
import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.testutil.DummyMainThread; import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable; import com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable;
...@@ -343,7 +343,7 @@ public class DownloadManagerTest { ...@@ -343,7 +343,7 @@ public class DownloadManagerTest {
TaskWrapper task3 = new DownloadRunner(uri3).postDownloadAction().postRemoveAction().getTask(); TaskWrapper task3 = new DownloadRunner(uri3).postDownloadAction().postRemoveAction().getTask();
task3.assertRemoving(); task3.assertRemoving();
DownloadState[] states = downloadManager.getAllDownloadStates(); Download[] states = downloadManager.getAllDownloads();
assertThat(states).hasLength(3); assertThat(states).hasLength(3);
String[] taskIds = {task1.taskId, task2.taskId, task3.taskId}; String[] taskIds = {task1.taskId, task2.taskId, task3.taskId};
...@@ -554,27 +554,27 @@ public class DownloadManagerTest { ...@@ -554,27 +554,27 @@ public class DownloadManagerTest {
} }
private TaskWrapper assertDownloading() { private TaskWrapper assertDownloading() {
return assertState(DownloadState.STATE_DOWNLOADING); return assertState(Download.STATE_DOWNLOADING);
} }
private TaskWrapper assertCompleted() { private TaskWrapper assertCompleted() {
return assertState(DownloadState.STATE_COMPLETED); return assertState(Download.STATE_COMPLETED);
} }
private TaskWrapper assertRemoving() { private TaskWrapper assertRemoving() {
return assertState(DownloadState.STATE_REMOVING); return assertState(Download.STATE_REMOVING);
} }
private TaskWrapper assertFailed() { private TaskWrapper assertFailed() {
return assertState(DownloadState.STATE_FAILED); return assertState(Download.STATE_FAILED);
} }
private TaskWrapper assertQueued() { private TaskWrapper assertQueued() {
return assertState(DownloadState.STATE_QUEUED); return assertState(Download.STATE_QUEUED);
} }
private TaskWrapper assertStopped() { private TaskWrapper assertStopped() {
return assertState(DownloadState.STATE_STOPPED); return assertState(Download.STATE_STOPPED);
} }
private TaskWrapper assertState(@State int expectedState) { private TaskWrapper assertState(@State int expectedState) {
......
...@@ -29,10 +29,10 @@ import androidx.test.core.app.ApplicationProvider; ...@@ -29,10 +29,10 @@ import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.offline.DefaultDownloadIndex; import com.google.android.exoplayer2.offline.DefaultDownloadIndex;
import com.google.android.exoplayer2.offline.DefaultDownloaderFactory; import com.google.android.exoplayer2.offline.DefaultDownloaderFactory;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.DownloadAction; import com.google.android.exoplayer2.offline.DownloadAction;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.DownloadState;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.scheduler.Requirements;
...@@ -141,7 +141,7 @@ public class DownloadServiceDashTest { ...@@ -141,7 +141,7 @@ public class DownloadServiceDashTest {
} }
@Override @Override
protected Notification getForegroundNotification(DownloadState[] downloadStates) { protected Notification getForegroundNotification(Download[] downloads) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
}; };
......
...@@ -23,7 +23,7 @@ import androidx.annotation.Nullable; ...@@ -23,7 +23,7 @@ import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.DownloadState; import com.google.android.exoplayer2.offline.Download;
/** Helper for creating download notifications. */ /** Helper for creating download notifications. */
public final class DownloadNotificationHelper { public final class DownloadNotificationHelper {
...@@ -49,37 +49,37 @@ public final class DownloadNotificationHelper { ...@@ -49,37 +49,37 @@ public final class DownloadNotificationHelper {
* @param smallIcon A small icon for the notification. * @param smallIcon A small icon for the notification.
* @param contentIntent An optional content intent to send when the notification is clicked. * @param contentIntent An optional content intent to send when the notification is clicked.
* @param message An optional message to display on the notification. * @param message An optional message to display on the notification.
* @param downloadStates The download states. * @param downloads The download states.
* @return The notification. * @return The notification.
*/ */
public Notification buildProgressNotification( public Notification buildProgressNotification(
@DrawableRes int smallIcon, @DrawableRes int smallIcon,
@Nullable PendingIntent contentIntent, @Nullable PendingIntent contentIntent,
@Nullable String message, @Nullable String message,
DownloadState[] downloadStates) { Download[] downloads) {
float totalPercentage = 0; float totalPercentage = 0;
int downloadTaskCount = 0; int downloadTaskCount = 0;
boolean allDownloadPercentagesUnknown = true; boolean allDownloadPercentagesUnknown = true;
boolean haveDownloadedBytes = false; boolean haveDownloadedBytes = false;
boolean haveDownloadTasks = false; boolean haveDownloadTasks = false;
boolean haveRemoveTasks = false; boolean haveRemoveTasks = false;
for (DownloadState downloadState : downloadStates) { for (Download download : downloads) {
if (downloadState.state == DownloadState.STATE_REMOVING if (download.state == Download.STATE_REMOVING
|| downloadState.state == DownloadState.STATE_RESTARTING) { || download.state == Download.STATE_RESTARTING) {
haveRemoveTasks = true; haveRemoveTasks = true;
continue; continue;
} }
if (downloadState.state != DownloadState.STATE_DOWNLOADING if (download.state != Download.STATE_DOWNLOADING
&& downloadState.state != DownloadState.STATE_COMPLETED) { && download.state != Download.STATE_COMPLETED) {
continue; continue;
} }
haveDownloadTasks = true; haveDownloadTasks = true;
float downloadPercentage = downloadState.getDownloadPercentage(); float downloadPercentage = download.getDownloadPercentage();
if (downloadPercentage != C.PERCENTAGE_UNSET) { if (downloadPercentage != C.PERCENTAGE_UNSET) {
allDownloadPercentagesUnknown = false; allDownloadPercentagesUnknown = false;
totalPercentage += downloadPercentage; totalPercentage += downloadPercentage;
} }
haveDownloadedBytes |= downloadState.getDownloadedBytes() > 0; haveDownloadedBytes |= download.getDownloadedBytes() > 0;
downloadTaskCount++; downloadTaskCount++;
} }
......
...@@ -20,7 +20,7 @@ import android.app.PendingIntent; ...@@ -20,7 +20,7 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.offline.DownloadState; import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
/** /**
...@@ -40,7 +40,7 @@ public final class DownloadNotificationUtil { ...@@ -40,7 +40,7 @@ public final class DownloadNotificationUtil {
* @param channelId The id of the notification channel to use. * @param channelId The id of the notification channel to use.
* @param contentIntent An optional content intent to send when the notification is clicked. * @param contentIntent An optional content intent to send when the notification is clicked.
* @param message An optional message to display on the notification. * @param message An optional message to display on the notification.
* @param downloadStates The download states. * @param downloads The download states.
* @return The notification. * @return The notification.
*/ */
public static Notification buildProgressNotification( public static Notification buildProgressNotification(
...@@ -49,9 +49,9 @@ public final class DownloadNotificationUtil { ...@@ -49,9 +49,9 @@ public final class DownloadNotificationUtil {
String channelId, String channelId,
@Nullable PendingIntent contentIntent, @Nullable PendingIntent contentIntent,
@Nullable String message, @Nullable String message,
DownloadState[] downloadStates) { Download[] downloads) {
return new DownloadNotificationHelper(context, channelId) return new DownloadNotificationHelper(context, channelId)
.buildProgressNotification(smallIcon, contentIntent, message, downloadStates); .buildProgressNotification(smallIcon, contentIntent, message, downloads);
} }
/** /**
......
...@@ -19,9 +19,9 @@ import static com.google.common.truth.Truth.assertThat; ...@@ -19,9 +19,9 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import android.os.ConditionVariable; import android.os.ConditionVariable;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.Download.State;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadState;
import com.google.android.exoplayer2.offline.DownloadState.State;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
...@@ -42,7 +42,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -42,7 +42,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
private final ConditionVariable initializedCondition; private final ConditionVariable initializedCondition;
private CountDownLatch downloadFinishedCondition; private CountDownLatch downloadFinishedCondition;
@DownloadState.FailureReason private int failureReason; @Download.FailureReason private int failureReason;
public TestDownloadManagerListener( public TestDownloadManagerListener(
DownloadManager downloadManager, DummyMainThread dummyMainThread) { DownloadManager downloadManager, DummyMainThread dummyMainThread) {
...@@ -69,16 +69,16 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -69,16 +69,16 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
} }
@Override @Override
public void onDownloadStateChanged(DownloadManager downloadManager, DownloadState downloadState) { public void onDownloadChanged(DownloadManager downloadManager, Download download) {
if (downloadState.state == DownloadState.STATE_FAILED) { if (download.state == Download.STATE_FAILED) {
failureReason = downloadState.failureReason; failureReason = download.failureReason;
} }
getStateQueue(downloadState.action.id).add(downloadState.state); getStateQueue(download.action.id).add(download.state);
} }
@Override @Override
public void onDownloadRemoved(DownloadManager downloadManager, DownloadState downloadState) { public void onDownloadRemoved(DownloadManager downloadManager, Download download) {
getStateQueue(downloadState.action.id).add(STATE_REMOVED); getStateQueue(download.action.id).add(STATE_REMOVED);
} }
@Override @Override
...@@ -94,8 +94,8 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -94,8 +94,8 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
*/ */
public void blockUntilTasksCompleteAndThrowAnyDownloadError() throws Throwable { public void blockUntilTasksCompleteAndThrowAnyDownloadError() throws Throwable {
blockUntilTasksComplete(); blockUntilTasksComplete();
if (failureReason != DownloadState.FAILURE_REASON_NONE) { if (failureReason != Download.FAILURE_REASON_NONE) {
throw new Exception("Failure reason: " + DownloadState.getFailureString(failureReason)); throw new Exception("Failure reason: " + Download.getFailureString(failureReason));
} }
} }
...@@ -152,9 +152,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -152,9 +152,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
} }
int receivedState = receivedStates.get(i); int receivedState = receivedStates.get(i);
String receivedStateString = String receivedStateString =
receivedState == STATE_REMOVED receivedState == STATE_REMOVED ? "REMOVED" : Download.getStateString(receivedState);
? "REMOVED"
: DownloadState.getStateString(receivedState);
sb.append(receivedStateString); sb.append(receivedStateString);
} }
fail( fail(
...@@ -162,7 +160,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -162,7 +160,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
Locale.US, Locale.US,
"for download (%s) expected:<%s> but was:<%s>", "for download (%s) expected:<%s> but was:<%s>",
taskId, taskId,
DownloadState.getStateString(expectedState), Download.getStateString(expectedState),
sb)); sb));
} }
} }
......
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