Clean up offline class names
This change is intended to resolve overloading of "Download", where a DownloadTask could be an actual download task, or a remove task. Also cleaned up some documentation. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=194815058
This commit is contained in:
parent
c68e00e28f
commit
7723f5bdf1
@ -18,7 +18,7 @@ package com.google.android.exoplayer2.demo;
|
||||
import android.app.Notification;
|
||||
import android.util.Pair;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
|
||||
import com.google.android.exoplayer2.offline.DownloadService;
|
||||
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
|
||||
import com.google.android.exoplayer2.offline.ProgressiveDownloadAction;
|
||||
@ -75,21 +75,21 @@ public class DemoDownloadService extends DownloadService {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Notification getForegroundNotification(DownloadState[] downloadStates) {
|
||||
protected Notification getForegroundNotification(TaskState[] taskStates) {
|
||||
return DownloadNotificationUtil.createProgressNotification(
|
||||
downloadStates, this, R.drawable.exo_controls_play, CHANNEL_ID, null);
|
||||
taskStates, this, R.drawable.exo_controls_play, CHANNEL_ID, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStateChange(DownloadState downloadState) {
|
||||
int notificationId = FOREGROUND_NOTIFICATION_ID + 1 + downloadState.taskId;
|
||||
protected void onTaskStateChanged(TaskState taskState) {
|
||||
int notificationId = FOREGROUND_NOTIFICATION_ID + 1 + taskState.taskId;
|
||||
Notification downloadNotification =
|
||||
DownloadNotificationUtil.createDownloadFinishedNotification(
|
||||
downloadState,
|
||||
taskState,
|
||||
this,
|
||||
R.drawable.exo_controls_play,
|
||||
CHANNEL_ID,
|
||||
downloadState.downloadAction.data,
|
||||
taskState.action.data,
|
||||
new ErrorMessageProvider<Throwable>() {
|
||||
@Override
|
||||
public Pair<Integer, String> getErrorMessage(Throwable throwable) {
|
||||
|
@ -43,7 +43,7 @@ public abstract class DownloadAction {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes a {@link DownloadAction} from the {@code input}.
|
||||
* Deserializes an action from the {@code input}.
|
||||
*
|
||||
* @param version Version of the data.
|
||||
* @param input DataInputStream to read data from.
|
||||
@ -55,17 +55,17 @@ public abstract class DownloadAction {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes one {@code action} which was serialized by {@link
|
||||
* #serializeToStream(DownloadAction, OutputStream)} from the {@code input} using one of the
|
||||
* {@link Deserializer}s which supports the type of the action.
|
||||
* Deserializes one action that was serialized with {@link #serializeToStream(DownloadAction,
|
||||
* OutputStream)} from the {@code input}, using the {@link Deserializer}s that supports the
|
||||
* action's type.
|
||||
*
|
||||
* <p>The caller is responsible for closing the given {@link InputStream}.
|
||||
*
|
||||
* @param deserializers Array of {@link Deserializer}s to deserialize a {@link DownloadAction}.
|
||||
* @param deserializers {@link Deserializer}s for supported actions.
|
||||
* @param input Input stream to read serialized data.
|
||||
* @return The deserialized {@link DownloadAction}.
|
||||
* @throws IOException If there is an IO error from {@code input} or the action type isn't
|
||||
* supported by any of the {@code deserializers}.
|
||||
* @return The deserialized action.
|
||||
* @throws IOException If there is an IO error reading from {@code input}, or if the action type
|
||||
* isn't supported by any of the {@code deserializers}.
|
||||
*/
|
||||
public static DownloadAction deserializeFromStream(
|
||||
Deserializer[] deserializers, InputStream input) throws IOException {
|
||||
@ -79,10 +79,10 @@ public abstract class DownloadAction {
|
||||
*
|
||||
* <p>The caller is responsible for closing the given {@link InputStream}.
|
||||
*
|
||||
* @param deserializers Array of {@link Deserializer}s to deserialize a {@link DownloadAction}.
|
||||
* @param deserializers {@link Deserializer}s for supported actions.
|
||||
* @param input Input stream to read serialized data.
|
||||
* @param version Master version of the serialization. See {@link DownloadAction#MASTER_VERSION}.
|
||||
* @return The deserialized {@link DownloadAction}.
|
||||
* @return The deserialized action.
|
||||
* @throws IOException If there is an IO error from {@code input}.
|
||||
* @throws DownloadException If the action type isn't supported by any of the {@code
|
||||
* deserializers}.
|
||||
|
@ -15,17 +15,18 @@
|
||||
*/
|
||||
package com.google.android.exoplayer2.offline;
|
||||
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_CANCELED;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_ENDED;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_ERROR;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_QUEUED;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.DownloadState.STATE_STARTED;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.TaskState.STATE_CANCELED;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.TaskState.STATE_ENDED;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.TaskState.STATE_ERROR;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.TaskState.STATE_QUEUED;
|
||||
import static com.google.android.exoplayer2.offline.DownloadManager.TaskState.STATE_STARTED;
|
||||
|
||||
import android.os.ConditionVariable;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
import android.support.annotation.IntDef;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.offline.DownloadAction.Deserializer;
|
||||
@ -43,24 +44,24 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
||||
/**
|
||||
* Manages multiple stream download and remove requests.
|
||||
*
|
||||
* <p>By default downloads are stopped. Call {@link #startDownloads()} to start downloads.
|
||||
* <p>A download manager instance must be accessed only from the thread that created it, unless that
|
||||
* thread does not have a {@link Looper}. In that case, it must be accessed only from the
|
||||
* application's main thread. Registered listeners will be called on the same thread.
|
||||
*
|
||||
* <p>WARNING: Methods of this class must be called only on the main thread of the application.
|
||||
* <p>By default download tasks are stopped, so {@link #startDownloads()} must be called to start
|
||||
* them.
|
||||
*/
|
||||
public final class DownloadManager {
|
||||
|
||||
/**
|
||||
* Listener for download events. Listener methods are called on the main thread of the
|
||||
* application.
|
||||
*/
|
||||
/** Listener for {@link DownloadManager} events. */
|
||||
public interface DownloadListener {
|
||||
/**
|
||||
* Called on download state change.
|
||||
* Called when the state of a task changes.
|
||||
*
|
||||
* @param downloadManager The reporting instance.
|
||||
* @param downloadState The download task.
|
||||
* @param taskState The state of the task.
|
||||
*/
|
||||
void onStateChange(DownloadManager downloadManager, DownloadState downloadState);
|
||||
void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState);
|
||||
|
||||
/**
|
||||
* Called when there is no active task left.
|
||||
@ -70,9 +71,9 @@ public final class DownloadManager {
|
||||
void onIdle(DownloadManager downloadManager);
|
||||
}
|
||||
|
||||
/** The default maximum number of simultaneous downloads. */
|
||||
/** The default maximum number of simultaneous download tasks. */
|
||||
public static final int DEFAULT_MAX_SIMULTANEOUS_DOWNLOADS = 1;
|
||||
/** The default minimum number of times the downloads must be retried before failing. */
|
||||
/** The default minimum number of times a task must be retried before failing. */
|
||||
public static final int DEFAULT_MIN_RETRY_COUNT = 5;
|
||||
|
||||
private static final String TAG = "DownloadManager";
|
||||
@ -83,8 +84,8 @@ public final class DownloadManager {
|
||||
private final int minRetryCount;
|
||||
private final ActionFile actionFile;
|
||||
private final DownloadAction.Deserializer[] deserializers;
|
||||
private final ArrayList<DownloadTask> tasks;
|
||||
private final ArrayList<DownloadTask> activeDownloadTasks;
|
||||
private final ArrayList<Task> tasks;
|
||||
private final ArrayList<Task> activeDownloadTasks;
|
||||
private final Handler handler;
|
||||
private final HandlerThread fileIOThread;
|
||||
private final Handler fileIOHandler;
|
||||
@ -140,8 +141,8 @@ public final class DownloadManager {
|
||||
*
|
||||
* @param constructorHelper A {@link DownloaderConstructorHelper} to create {@link Downloader}s
|
||||
* for downloading data.
|
||||
* @param maxSimultaneousDownloads The maximum number of simultaneous downloads.
|
||||
* @param minRetryCount The minimum number of times the downloads must be retried before failing.
|
||||
* @param maxSimultaneousDownloads The maximum number of simultaneous download tasks.
|
||||
* @param minRetryCount The minimum number of times a task must be retried before failing.
|
||||
* @param actionFile The file in which active actions are saved.
|
||||
* @param deserializers Used to deserialize {@link DownloadAction}s.
|
||||
*/
|
||||
@ -151,8 +152,7 @@ public final class DownloadManager {
|
||||
int minRetryCount,
|
||||
File actionFile,
|
||||
Deserializer... deserializers) {
|
||||
Assertions.checkArgument(
|
||||
deserializers.length > 0, "At least one Deserializer should be given.");
|
||||
Assertions.checkArgument(deserializers.length > 0, "At least one Deserializer is required.");
|
||||
|
||||
this.downloaderConstructorHelper = constructorHelper;
|
||||
this.maxActiveDownloadTasks = maxSimultaneousDownloads;
|
||||
@ -177,7 +177,7 @@ public final class DownloadManager {
|
||||
listeners = new CopyOnWriteArraySet<>();
|
||||
|
||||
loadActions();
|
||||
logd("DownloadManager is created");
|
||||
logd("Created");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,7 +198,7 @@ public final class DownloadManager {
|
||||
});
|
||||
fileIOFinishedCondition.block();
|
||||
fileIOThread.quit();
|
||||
logd("DownloadManager is released");
|
||||
logd("Released");
|
||||
}
|
||||
|
||||
/** Stops all of the download tasks. Call {@link #startDownloads()} to restart tasks. */
|
||||
@ -240,12 +240,12 @@ public final class DownloadManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes one {@link DownloadAction} from {@code actionData} and calls {@link
|
||||
* Deserializes an action from {@code actionData}, and calls {@link
|
||||
* #handleAction(DownloadAction)}.
|
||||
*
|
||||
* @param actionData Serialized {@link DownloadAction} data.
|
||||
* @return The task id.
|
||||
* @throws IOException If an error occurs during handling action.
|
||||
* @param actionData Serialized version of the action to be executed.
|
||||
* @return The id of the newly created task.
|
||||
* @throws IOException If an error occurs deserializing the action.
|
||||
*/
|
||||
public int handleAction(byte[] actionData) throws IOException {
|
||||
ByteArrayInputStream input = new ByteArrayInputStream(actionData);
|
||||
@ -254,41 +254,40 @@ public final class DownloadManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the given {@link DownloadAction}. A task is created and added to the task queue. If
|
||||
* it's a remove action then this method cancels any download tasks which works on the same media
|
||||
* immediately.
|
||||
* Handles the given action. A task is created and added to the task queue. If it's a remove
|
||||
* action then any download tasks for the same media are immediately canceled.
|
||||
*
|
||||
* @param downloadAction Action to be executed.
|
||||
* @return The task id.
|
||||
* @param action The action to be executed.
|
||||
* @return The id of the newly created task.
|
||||
*/
|
||||
public int handleAction(DownloadAction downloadAction) {
|
||||
DownloadTask downloadTask = createDownloadTask(downloadAction);
|
||||
public int handleAction(DownloadAction action) {
|
||||
Task task = createTask(action);
|
||||
saveActions();
|
||||
if (downloadsStopped && !downloadAction.isRemoveAction) {
|
||||
logd("Can't start the task as downloads are stopped", downloadTask);
|
||||
if (downloadsStopped && !action.isRemoveAction) {
|
||||
logd("Can't start the task as downloads are stopped", task);
|
||||
} else {
|
||||
maybeStartTasks();
|
||||
}
|
||||
return downloadTask.id;
|
||||
return task.id;
|
||||
}
|
||||
|
||||
private DownloadTask createDownloadTask(DownloadAction downloadAction) {
|
||||
DownloadTask downloadTask = new DownloadTask(nextTaskId++, this, downloadAction, minRetryCount);
|
||||
tasks.add(downloadTask);
|
||||
logd("Task is added", downloadTask);
|
||||
notifyListenersTaskStateChange(downloadTask);
|
||||
return downloadTask;
|
||||
private Task createTask(DownloadAction action) {
|
||||
Task task = new Task(nextTaskId++, this, action, minRetryCount);
|
||||
tasks.add(task);
|
||||
logd("Task is added", task);
|
||||
notifyListenersTaskStateChange(task);
|
||||
return task;
|
||||
}
|
||||
|
||||
/** Returns number of tasks. */
|
||||
/** Returns the current number of tasks. */
|
||||
public int getTaskCount() {
|
||||
return tasks.size();
|
||||
}
|
||||
|
||||
/** Returns a {@link DownloadTask} for a task. */
|
||||
public DownloadState getDownloadState(int taskId) {
|
||||
/** Returns the state of a task, or null if no such task exists */
|
||||
public @Nullable TaskState getTaskState(int taskId) {
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
DownloadTask task = tasks.get(i);
|
||||
Task task = tasks.get(i);
|
||||
if (task.id == taskId) {
|
||||
return task.getDownloadState();
|
||||
}
|
||||
@ -296,14 +295,13 @@ public final class DownloadManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Returns {@link DownloadState}s for all tasks. */
|
||||
public DownloadState[] getDownloadStates() {
|
||||
return getDownloadStates(tasks);
|
||||
/** Returns the states of all current tasks. */
|
||||
public TaskState[] getAllTaskStates() {
|
||||
TaskState[] states = new TaskState[tasks.size()];
|
||||
for (int i = 0; i < states.length; i++) {
|
||||
states[i] = tasks.get(i).getDownloadState();
|
||||
}
|
||||
|
||||
/** Returns an array of {@link DownloadState}s for active download tasks. */
|
||||
public DownloadState[] getActiveDownloadStates() {
|
||||
return getDownloadStates(activeDownloadTasks);
|
||||
return states;
|
||||
}
|
||||
|
||||
/** Returns whether there are no active tasks. */
|
||||
@ -339,12 +337,12 @@ public final class DownloadManager {
|
||||
boolean skipDownloadActions = downloadsStopped
|
||||
|| activeDownloadTasks.size() == maxActiveDownloadTasks;
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
DownloadTask downloadTask = tasks.get(i);
|
||||
if (!downloadTask.canStart()) {
|
||||
Task task = tasks.get(i);
|
||||
if (!task.canStart()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DownloadAction downloadAction = downloadTask.downloadAction;
|
||||
DownloadAction downloadAction = task.action;
|
||||
boolean removeAction = downloadAction.isRemoveAction;
|
||||
if (!removeAction && skipDownloadActions) {
|
||||
continue;
|
||||
@ -352,14 +350,14 @@ public final class DownloadManager {
|
||||
|
||||
boolean canStartTask = true;
|
||||
for (int j = 0; j < i; j++) {
|
||||
DownloadTask task = tasks.get(j);
|
||||
if (task.downloadAction.isSameMedia(downloadAction)) {
|
||||
Task otherTask = tasks.get(j);
|
||||
if (otherTask.action.isSameMedia(downloadAction)) {
|
||||
if (removeAction) {
|
||||
canStartTask = false;
|
||||
logd(downloadTask + " clashes with " + task);
|
||||
task.cancel();
|
||||
logd(task + " clashes with " + otherTask);
|
||||
otherTask.cancel();
|
||||
// Continue loop to cancel any other preceding clashing tasks.
|
||||
} else if (task.downloadAction.isRemoveAction) {
|
||||
} else if (otherTask.action.isRemoveAction) {
|
||||
canStartTask = false;
|
||||
skipDownloadActions = true;
|
||||
break;
|
||||
@ -368,9 +366,9 @@ public final class DownloadManager {
|
||||
}
|
||||
|
||||
if (canStartTask) {
|
||||
downloadTask.start();
|
||||
task.start();
|
||||
if (!removeAction) {
|
||||
activeDownloadTasks.add(downloadTask);
|
||||
activeDownloadTasks.add(task);
|
||||
skipDownloadActions = activeDownloadTasks.size() == maxActiveDownloadTasks;
|
||||
}
|
||||
}
|
||||
@ -387,18 +385,18 @@ public final class DownloadManager {
|
||||
}
|
||||
}
|
||||
|
||||
private void onTaskStateChange(DownloadTask downloadTask) {
|
||||
private void onTaskStateChange(Task task) {
|
||||
if (released) {
|
||||
return;
|
||||
}
|
||||
logd("Task state is changed", downloadTask);
|
||||
boolean stopped = !downloadTask.isActive();
|
||||
logd("Task state is changed", task);
|
||||
boolean stopped = !task.isActive();
|
||||
if (stopped) {
|
||||
activeDownloadTasks.remove(downloadTask);
|
||||
activeDownloadTasks.remove(task);
|
||||
}
|
||||
notifyListenersTaskStateChange(downloadTask);
|
||||
if (downloadTask.isFinished()) {
|
||||
tasks.remove(downloadTask);
|
||||
notifyListenersTaskStateChange(task);
|
||||
if (task.isFinished()) {
|
||||
tasks.remove(task);
|
||||
saveActions();
|
||||
}
|
||||
if (stopped) {
|
||||
@ -407,10 +405,10 @@ public final class DownloadManager {
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyListenersTaskStateChange(DownloadTask downloadTask) {
|
||||
DownloadState downloadState = downloadTask.getDownloadState();
|
||||
private void notifyListenersTaskStateChange(Task task) {
|
||||
TaskState taskState = task.getDownloadState();
|
||||
for (DownloadListener listener : listeners) {
|
||||
listener.onStateChange(this, downloadState);
|
||||
listener.onTaskStateChanged(this, taskState);
|
||||
}
|
||||
}
|
||||
|
||||
@ -434,7 +432,7 @@ public final class DownloadManager {
|
||||
public void run() {
|
||||
try {
|
||||
for (DownloadAction action : actions) {
|
||||
createDownloadTask(action);
|
||||
createTask(action);
|
||||
}
|
||||
logd("Tasks are created.");
|
||||
maybeStartTasks();
|
||||
@ -454,7 +452,7 @@ public final class DownloadManager {
|
||||
}
|
||||
final DownloadAction[] actions = new DownloadAction[tasks.size()];
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
actions[i] = tasks.get(i).downloadAction;
|
||||
actions[i] = tasks.get(i).action;
|
||||
}
|
||||
fileIOHandler.post(new Runnable() {
|
||||
@Override
|
||||
@ -475,21 +473,12 @@ public final class DownloadManager {
|
||||
}
|
||||
}
|
||||
|
||||
private void logd(String message, DownloadTask task) {
|
||||
private void logd(String message, Task task) {
|
||||
logd(message + ": " + task);
|
||||
}
|
||||
|
||||
private static DownloadState[] getDownloadStates(ArrayList<DownloadTask> tasks) {
|
||||
DownloadState[] states = new DownloadState[tasks.size()];
|
||||
for (int i = 0; i < tasks.size(); i++) {
|
||||
DownloadTask task = tasks.get(i);
|
||||
states[i] = task.getDownloadState();
|
||||
}
|
||||
return states;
|
||||
}
|
||||
|
||||
/** Represents state of a download task. */
|
||||
public static final class DownloadState {
|
||||
/** Represents state of a task. */
|
||||
public static final class TaskState {
|
||||
|
||||
/**
|
||||
* Task states.
|
||||
@ -534,12 +523,13 @@ public final class DownloadManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** Unique id of the task. */
|
||||
/** The unique task id. */
|
||||
public final int taskId;
|
||||
/** The {@link DownloadAction} which is being executed. */
|
||||
public final DownloadAction downloadAction;
|
||||
/** The state of the task. See {@link State}. */
|
||||
/** The action being executed. */
|
||||
public final DownloadAction action;
|
||||
/** The state of the task. */
|
||||
public final @State int state;
|
||||
|
||||
/**
|
||||
* The estimated download percentage, or {@link C#PERCENTAGE_UNSET} if no estimate is available
|
||||
* or if this is a removal task.
|
||||
@ -547,18 +537,19 @@ public final class DownloadManager {
|
||||
public final float downloadPercentage;
|
||||
/** The total number of downloaded bytes. */
|
||||
public final long downloadedBytes;
|
||||
|
||||
/** If {@link #state} is {@link #STATE_ERROR} then this is the cause, otherwise null. */
|
||||
public final Throwable error;
|
||||
|
||||
private DownloadState(
|
||||
private TaskState(
|
||||
int taskId,
|
||||
DownloadAction downloadAction,
|
||||
DownloadAction action,
|
||||
@State int state,
|
||||
float downloadPercentage,
|
||||
long downloadedBytes,
|
||||
Throwable error) {
|
||||
this.taskId = taskId;
|
||||
this.downloadAction = downloadAction;
|
||||
this.action = action;
|
||||
this.state = state;
|
||||
this.downloadPercentage = downloadPercentage;
|
||||
this.downloadedBytes = downloadedBytes;
|
||||
@ -567,7 +558,7 @@ public final class DownloadManager {
|
||||
|
||||
}
|
||||
|
||||
private static final class DownloadTask implements Runnable {
|
||||
private static final class Task implements Runnable {
|
||||
|
||||
/**
|
||||
* Task states.
|
||||
@ -607,26 +598,26 @@ public final class DownloadManager {
|
||||
|
||||
private final int id;
|
||||
private final DownloadManager downloadManager;
|
||||
private final DownloadAction downloadAction;
|
||||
private final DownloadAction action;
|
||||
private final int minRetryCount;
|
||||
private volatile @InternalState int currentState;
|
||||
private volatile Downloader downloader;
|
||||
private Thread thread;
|
||||
private Throwable error;
|
||||
|
||||
private DownloadTask(
|
||||
int id, DownloadManager downloadManager, DownloadAction downloadAction, int minRetryCount) {
|
||||
private Task(
|
||||
int id, DownloadManager downloadManager, DownloadAction action, int minRetryCount) {
|
||||
this.id = id;
|
||||
this.downloadManager = downloadManager;
|
||||
this.downloadAction = downloadAction;
|
||||
this.action = action;
|
||||
this.currentState = STATE_QUEUED;
|
||||
this.minRetryCount = minRetryCount;
|
||||
}
|
||||
|
||||
public DownloadState getDownloadState() {
|
||||
public TaskState getDownloadState() {
|
||||
int externalState = getExternalState();
|
||||
return new DownloadState(
|
||||
id, downloadAction, externalState, getDownloadPercentage(), getDownloadedBytes(), error);
|
||||
return new TaskState(
|
||||
id, action, externalState, getDownloadPercentage(), getDownloadedBytes(), error);
|
||||
}
|
||||
|
||||
/** Returns whether the task is finished. */
|
||||
@ -662,11 +653,11 @@ public final class DownloadManager {
|
||||
if (!DEBUG) {
|
||||
return super.toString();
|
||||
}
|
||||
return downloadAction.type
|
||||
return action.type
|
||||
+ ' '
|
||||
+ (downloadAction.isRemoveAction ? "remove" : "download")
|
||||
+ (action.isRemoveAction ? "remove" : "download")
|
||||
+ ' '
|
||||
+ downloadAction.data
|
||||
+ action.data
|
||||
+ ' '
|
||||
+ getStateString();
|
||||
}
|
||||
@ -679,7 +670,7 @@ public final class DownloadManager {
|
||||
case STATE_STARTED_STOPPING:
|
||||
return "STOPPING";
|
||||
default:
|
||||
return DownloadState.getStateString(currentState);
|
||||
return TaskState.getStateString(currentState);
|
||||
}
|
||||
}
|
||||
|
||||
@ -740,7 +731,7 @@ public final class DownloadManager {
|
||||
this.error = error;
|
||||
boolean isInternalState = currentState != getExternalState();
|
||||
if (!isInternalState) {
|
||||
downloadManager.onTaskStateChange(DownloadTask.this);
|
||||
downloadManager.onTaskStateChange(this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -749,11 +740,11 @@ public final class DownloadManager {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
downloadManager.logd("Task is started", DownloadTask.this);
|
||||
downloadManager.logd("Task is started", this);
|
||||
Throwable error = null;
|
||||
try {
|
||||
downloader = downloadAction.createDownloader(downloadManager.downloaderConstructorHelper);
|
||||
if (downloadAction.isRemoveAction) {
|
||||
downloader = action.createDownloader(downloadManager.downloaderConstructorHelper);
|
||||
if (action.isRemoveAction) {
|
||||
downloader.remove();
|
||||
} else {
|
||||
int errorCount = 0;
|
||||
|
@ -25,7 +25,7 @@ import android.os.Looper;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.StringRes;
|
||||
import android.util.Log;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
|
||||
import com.google.android.exoplayer2.scheduler.Requirements;
|
||||
import com.google.android.exoplayer2.scheduler.RequirementsWatcher;
|
||||
import com.google.android.exoplayer2.scheduler.Scheduler;
|
||||
@ -272,21 +272,25 @@ public abstract class DownloadService extends Service {
|
||||
/**
|
||||
* Returns a notification to be displayed when this service running in the foreground.
|
||||
*
|
||||
* <p>This method is called when there is a download task state change and periodically while
|
||||
* there is an active download. Update interval can be set using {@link #DownloadService(int,
|
||||
* <p>This method is called when there is a task state change and periodically while there are
|
||||
* active tasks. The periodic update interval can be set using {@link #DownloadService(int,
|
||||
* long)}.
|
||||
*
|
||||
* <p>On API level 26 and above, it may be also called just before the service stops with an empty
|
||||
* {@code downloadStates} array, returned notification is used to satisfy system requirements for
|
||||
* foreground services.
|
||||
* <p>On API level 26 and above, this method may also be called just before the service stops,
|
||||
* with an empty {@code taskStates} array. The returned notification is used to satisfy system
|
||||
* requirements for foreground services.
|
||||
*
|
||||
* @param downloadStates DownloadState for all tasks.
|
||||
* @return A notification to be displayed when this service running in the foreground.
|
||||
* @param taskStates The states of all current tasks.
|
||||
* @return The foreground notification to display.
|
||||
*/
|
||||
protected abstract Notification getForegroundNotification(DownloadState[] downloadStates);
|
||||
protected abstract Notification getForegroundNotification(TaskState[] taskStates);
|
||||
|
||||
/** Called when the download state changes. */
|
||||
protected void onStateChange(DownloadState downloadState) {
|
||||
/**
|
||||
* Called when the state of a task changes.
|
||||
*
|
||||
* @param taskState The state of the task.
|
||||
*/
|
||||
protected void onTaskStateChanged(TaskState taskState) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@ -308,9 +312,9 @@ public abstract class DownloadService extends Service {
|
||||
|
||||
private final class DownloadListener implements DownloadManager.DownloadListener {
|
||||
@Override
|
||||
public void onStateChange(DownloadManager downloadManager, DownloadState downloadState) {
|
||||
DownloadService.this.onStateChange(downloadState);
|
||||
if (downloadState.state == DownloadState.STATE_STARTED) {
|
||||
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
|
||||
DownloadService.this.onTaskStateChanged(taskState);
|
||||
if (taskState.state == TaskState.STATE_STARTED) {
|
||||
foregroundNotificationUpdater.startPeriodicUpdates();
|
||||
} else {
|
||||
foregroundNotificationUpdater.update();
|
||||
@ -349,8 +353,8 @@ public abstract class DownloadService extends Service {
|
||||
}
|
||||
|
||||
public void update() {
|
||||
DownloadState[] downloadStates = downloadManager.getDownloadStates();
|
||||
startForeground(notificationId, getForegroundNotification(downloadStates));
|
||||
TaskState[] taskStates = downloadManager.getAllTaskStates();
|
||||
startForeground(notificationId, getForegroundNotification(taskStates));
|
||||
notificationDisplayed = true;
|
||||
if (periodicUpdatesStarted) {
|
||||
handler.removeCallbacks(this);
|
||||
|
@ -22,8 +22,8 @@ import android.os.ConditionVariable;
|
||||
import android.support.annotation.Nullable;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadListener;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState.State;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.TaskState.State;
|
||||
import com.google.android.exoplayer2.testutil.DummyMainThread;
|
||||
import com.google.android.exoplayer2.testutil.RobolectricUtil;
|
||||
import com.google.android.exoplayer2.upstream.DummyDataSource;
|
||||
@ -255,11 +255,11 @@ public class DownloadManagerTest {
|
||||
downloadAction1.post().assertDoesNotStart();
|
||||
downloadAction2.post().assertDoesNotStart();
|
||||
|
||||
DownloadState[] states = downloadManager.getDownloadStates();
|
||||
TaskState[] states = downloadManager.getAllTaskStates();
|
||||
assertThat(states).hasLength(3);
|
||||
assertThat(states[0].downloadAction).isEqualTo(removeAction);
|
||||
assertThat(states[1].downloadAction).isEqualTo(downloadAction1);
|
||||
assertThat(states[2].downloadAction).isEqualTo(downloadAction2);
|
||||
assertThat(states[0].action).isEqualTo(removeAction);
|
||||
assertThat(states[1].action).isEqualTo(downloadAction1);
|
||||
assertThat(states[2].action).isEqualTo(downloadAction2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -503,11 +503,11 @@ public class DownloadManagerTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStateChange(DownloadManager downloadManager, DownloadState downloadState) {
|
||||
if (downloadState.state == DownloadState.STATE_ERROR && downloadError == null) {
|
||||
downloadError = downloadState.error;
|
||||
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
|
||||
if (taskState.state == TaskState.STATE_ERROR && downloadError == null) {
|
||||
downloadError = taskState.error;
|
||||
}
|
||||
((FakeDownloadAction) downloadState.downloadAction).onStateChange(downloadState.state);
|
||||
((FakeDownloadAction) taskState.action).onStateChange(taskState.state);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -580,23 +580,23 @@ public class DownloadManagerTest {
|
||||
|
||||
private FakeDownloadAction assertStarted() throws InterruptedException {
|
||||
downloader.assertStarted(ASSERT_TRUE_TIMEOUT);
|
||||
return assertState(DownloadState.STATE_STARTED);
|
||||
return assertState(TaskState.STATE_STARTED);
|
||||
}
|
||||
|
||||
private FakeDownloadAction assertEnded() {
|
||||
return assertState(DownloadState.STATE_ENDED);
|
||||
return assertState(TaskState.STATE_ENDED);
|
||||
}
|
||||
|
||||
private FakeDownloadAction assertError() {
|
||||
return assertState(DownloadState.STATE_ERROR);
|
||||
return assertState(TaskState.STATE_ERROR);
|
||||
}
|
||||
|
||||
private FakeDownloadAction assertCancelled() {
|
||||
return assertState(DownloadState.STATE_CANCELED);
|
||||
return assertState(TaskState.STATE_CANCELED);
|
||||
}
|
||||
|
||||
private FakeDownloadAction assertStopped() {
|
||||
return assertState(DownloadState.STATE_QUEUED);
|
||||
return assertState(TaskState.STATE_QUEUED);
|
||||
}
|
||||
|
||||
private FakeDownloadAction assertState(@State int expectedState) {
|
||||
@ -619,13 +619,13 @@ public class DownloadManagerTest {
|
||||
if (i > 0) {
|
||||
sb.append(',');
|
||||
}
|
||||
sb.append(DownloadState.getStateString(receivedStates.get(i)));
|
||||
sb.append(TaskState.getStateString(receivedStates.get(i)));
|
||||
}
|
||||
fail(
|
||||
String.format(
|
||||
Locale.US,
|
||||
"expected:<%s> but was:<%s>",
|
||||
DownloadState.getStateString(expectedState),
|
||||
TaskState.getStateString(expectedState),
|
||||
sb));
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.Nullable;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
|
||||
import com.google.android.exoplayer2.offline.DownloadService;
|
||||
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
|
||||
import com.google.android.exoplayer2.scheduler.Requirements;
|
||||
@ -139,8 +139,7 @@ public class DownloadServiceDashTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Notification getForegroundNotification(
|
||||
DownloadState[] downloadStates) {
|
||||
protected Notification getForegroundNotification(TaskState[] taskStates) {
|
||||
return Mockito.mock(Notification.class);
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import com.google.android.exoplayer2.offline.DownloadManager;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadListener;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
|
||||
import com.google.android.exoplayer2.testutil.DummyMainThread;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -40,9 +39,10 @@ import java.util.concurrent.TimeUnit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStateChange(DownloadManager downloadManager, DownloadState downloadState) {
|
||||
if (downloadState.state == DownloadState.STATE_ERROR && downloadError == null) {
|
||||
downloadError = downloadState.error;
|
||||
public void onTaskStateChanged(
|
||||
DownloadManager downloadManager, DownloadManager.TaskState taskState) {
|
||||
if (taskState.state == DownloadManager.TaskState.STATE_ERROR && downloadError == null) {
|
||||
downloadError = taskState.error;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
|
||||
import com.google.android.exoplayer2.util.ErrorMessageProvider;
|
||||
|
||||
/** Helper class to create notifications for downloads using {@link DownloadManager}. */
|
||||
@ -32,18 +32,18 @@ public final class DownloadNotificationUtil {
|
||||
private DownloadNotificationUtil() {}
|
||||
|
||||
/**
|
||||
* Returns a progress notification for the given {@link DownloadState}s.
|
||||
* Returns a progress notification for the given {@link TaskState}s.
|
||||
*
|
||||
* @param downloadStates States of the downloads.
|
||||
* @param taskStates States of the downloads.
|
||||
* @param context Used to access resources.
|
||||
* @param smallIcon A small icon for the notification.
|
||||
* @param channelId The id of the notification channel to use. Only required for API level 26 and
|
||||
* above.
|
||||
* @param message An optional message to display on the notification.
|
||||
* @return A progress notification for the given {@link DownloadState}s.
|
||||
* @return A progress notification for the given {@link TaskState}s.
|
||||
*/
|
||||
public static @Nullable Notification createProgressNotification(
|
||||
DownloadState[] downloadStates,
|
||||
TaskState[] taskStates,
|
||||
Context context,
|
||||
int smallIcon,
|
||||
String channelId,
|
||||
@ -52,16 +52,15 @@ public final class DownloadNotificationUtil {
|
||||
int downloadTaskCount = 0;
|
||||
boolean allDownloadPercentagesUnknown = true;
|
||||
boolean haveDownloadedBytes = false;
|
||||
for (DownloadState downloadState : downloadStates) {
|
||||
if (downloadState.downloadAction.isRemoveAction
|
||||
|| downloadState.state != DownloadState.STATE_STARTED) {
|
||||
for (TaskState taskState : taskStates) {
|
||||
if (taskState.action.isRemoveAction || taskState.state != TaskState.STATE_STARTED) {
|
||||
continue;
|
||||
}
|
||||
if (downloadState.downloadPercentage != C.PERCENTAGE_UNSET) {
|
||||
if (taskState.downloadPercentage != C.PERCENTAGE_UNSET) {
|
||||
allDownloadPercentagesUnknown = false;
|
||||
totalPercentage += downloadState.downloadPercentage;
|
||||
totalPercentage += taskState.downloadPercentage;
|
||||
}
|
||||
haveDownloadedBytes |= downloadState.downloadedBytes > 0;
|
||||
haveDownloadedBytes |= taskState.downloadedBytes > 0;
|
||||
downloadTaskCount++;
|
||||
}
|
||||
|
||||
@ -79,11 +78,11 @@ public final class DownloadNotificationUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a notification for a {@link DownloadState} which is in either {@link
|
||||
* DownloadState#STATE_ENDED} or {@link DownloadState#STATE_ERROR} states. Returns null if it's
|
||||
* some other state or it's state of a remove action.
|
||||
* Returns a notification for a {@link TaskState} which is in either {@link TaskState#STATE_ENDED}
|
||||
* or {@link TaskState#STATE_ERROR} states. Returns null if it's some other state or it's state of
|
||||
* a remove action.
|
||||
*
|
||||
* @param downloadState State of the download.
|
||||
* @param taskState State of the download.
|
||||
* @param context Used to access resources.
|
||||
* @param smallIcon A small icon for the notifications.
|
||||
* @param channelId The id of the notification channel to use. Only required for API level 26 and
|
||||
@ -92,27 +91,26 @@ public final class DownloadNotificationUtil {
|
||||
* @param errorMessageProvider An optional {@link ErrorMessageProvider} for translating download
|
||||
* errors into readable error messages. If not null and there is a download error then the
|
||||
* error message is displayed instead of {@code message}.
|
||||
* @return A notification for a {@link DownloadState} which is in either {@link
|
||||
* DownloadState#STATE_ENDED} or {@link DownloadState#STATE_ERROR} states. Returns null if
|
||||
* it's some other state or it's state of a remove action.
|
||||
* @return A notification for a {@link TaskState} which is in either {@link TaskState#STATE_ENDED}
|
||||
* or {@link TaskState#STATE_ERROR} states. Returns null if it's some other state or it's
|
||||
* state of a remove action.
|
||||
*/
|
||||
public static @Nullable Notification createDownloadFinishedNotification(
|
||||
DownloadState downloadState,
|
||||
TaskState taskState,
|
||||
Context context,
|
||||
int smallIcon,
|
||||
String channelId,
|
||||
@Nullable String message,
|
||||
@Nullable ErrorMessageProvider<Throwable> errorMessageProvider) {
|
||||
if (downloadState.downloadAction.isRemoveAction
|
||||
|| (downloadState.state != DownloadState.STATE_ENDED
|
||||
&& downloadState.state != DownloadState.STATE_ERROR)) {
|
||||
if (taskState.action.isRemoveAction
|
||||
|| (taskState.state != TaskState.STATE_ENDED && taskState.state != TaskState.STATE_ERROR)) {
|
||||
return null;
|
||||
}
|
||||
if (downloadState.error != null && errorMessageProvider != null) {
|
||||
message = errorMessageProvider.getErrorMessage(downloadState.error).second;
|
||||
if (taskState.error != null && errorMessageProvider != null) {
|
||||
message = errorMessageProvider.getErrorMessage(taskState.error).second;
|
||||
}
|
||||
int titleStringId =
|
||||
downloadState.state == DownloadState.STATE_ENDED
|
||||
taskState.state == TaskState.STATE_ENDED
|
||||
? R.string.exo_download_completed
|
||||
: R.string.exo_download_failed;
|
||||
NotificationCompat.Builder notificationBuilder =
|
||||
|
Loading…
x
Reference in New Issue
Block a user