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