Commit 4bf42bd2 by eguven Committed by Oliver Woodman

Rename TaskState to DownloadState

PiperOrigin-RevId: 225145311
parent 230a798f
......@@ -27,6 +27,7 @@
([#5169](https://github.com/google/ExoPlayer/issues/5169)).
* DownloadManager:
* Create only one task for all DownloadActions for the same content.
* Rename TaskState to DownloadState.
### 2.9.2 ###
......
......@@ -17,7 +17,7 @@ package com.google.android.exoplayer2.demo;
import android.app.Notification;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.scheduler.PlatformScheduler;
import com.google.android.exoplayer2.ui.DownloadNotificationUtil;
......@@ -50,40 +50,40 @@ public class DemoDownloadService extends DownloadService {
}
@Override
protected Notification getForegroundNotification(TaskState[] taskStates) {
protected Notification getForegroundNotification(DownloadState[] downloadStates) {
return DownloadNotificationUtil.buildProgressNotification(
/* context= */ this,
R.drawable.ic_download,
CHANNEL_ID,
/* contentIntent= */ null,
/* message= */ null,
taskStates);
downloadStates);
}
@Override
protected void onTaskStateChanged(TaskState taskState) {
if (taskState.action.isRemoveAction) {
protected void onDownloadStateChanged(DownloadState downloadState) {
if (downloadState.action.isRemoveAction) {
return;
}
Notification notification = null;
if (taskState.state == TaskState.STATE_COMPLETED) {
if (downloadState.state == DownloadState.STATE_COMPLETED) {
notification =
DownloadNotificationUtil.buildDownloadCompletedNotification(
/* context= */ this,
R.drawable.ic_download_done,
CHANNEL_ID,
/* contentIntent= */ null,
Util.fromUtf8Bytes(taskState.action.data));
} else if (taskState.state == TaskState.STATE_FAILED) {
Util.fromUtf8Bytes(downloadState.action.data));
} else if (downloadState.state == DownloadState.STATE_FAILED) {
notification =
DownloadNotificationUtil.buildDownloadFailedNotification(
/* context= */ this,
R.drawable.ic_download_done,
CHANNEL_ID,
/* contentIntent= */ null,
Util.fromUtf8Bytes(taskState.action.data));
Util.fromUtf8Bytes(downloadState.action.data));
}
int notificationId = FOREGROUND_NOTIFICATION_ID + 1 + taskState.taskId;
int notificationId = FOREGROUND_NOTIFICATION_ID + 1 + downloadState.id;
NotificationUtil.setNotification(this, notificationId, notification);
}
}
......@@ -37,7 +37,7 @@ import com.google.android.exoplayer2.offline.ActionFile;
import com.google.android.exoplayer2.offline.DownloadAction;
import com.google.android.exoplayer2.offline.DownloadHelper;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.ProgressiveDownloadHelper;
import com.google.android.exoplayer2.offline.StreamKey;
......@@ -144,11 +144,11 @@ public class DownloadTracker implements DownloadManager.Listener {
}
@Override
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
DownloadAction action = taskState.action;
public void onDownloadStateChanged(DownloadManager downloadManager, DownloadState downloadState) {
DownloadAction action = downloadState.action;
Uri uri = action.uri;
if ((action.isRemoveAction && taskState.state == TaskState.STATE_COMPLETED)
|| (!action.isRemoveAction && taskState.state == TaskState.STATE_FAILED)) {
if ((action.isRemoveAction && downloadState.state == DownloadState.STATE_COMPLETED)
|| (!action.isRemoveAction && downloadState.state == DownloadState.STATE_FAILED)) {
// A download has been removed, or has failed. Stop tracking it.
if (trackedDownloadStates.remove(uri) != null) {
handleTrackedDownloadStatesChanged();
......
......@@ -24,7 +24,7 @@ import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.scheduler.RequirementsWatcher;
import com.google.android.exoplayer2.scheduler.Scheduler;
......@@ -71,9 +71,9 @@ public abstract class DownloadService extends Service {
private static final String TAG = "DownloadService";
private static final boolean DEBUG = false;
// Keep the requirements helper for each DownloadService as long as there are tasks (and the
// process is running). This allows tasks to resume when there's no scheduler. It may also allow
// tasks the resume more quickly than when relying on the scheduler alone.
// Keep the requirements helper for each DownloadService as long as there are downloads (and the
// process is running). This allows downloads to resume when there's no scheduler. It may also
// allow downloads the resume more quickly than when relying on the scheduler alone.
private static final HashMap<Class<? extends DownloadService>, RequirementsHelper>
requirementsHelpers = new HashMap<>();
private static final Requirements DEFAULT_REQUIREMENTS =
......@@ -99,7 +99,7 @@ public abstract class DownloadService extends Service {
* <p>If {@code foregroundNotificationId} isn't {@link #FOREGROUND_NOTIFICATION_ID_NONE} (value
* {@value #FOREGROUND_NOTIFICATION_ID_NONE}) the service runs in the foreground with {@link
* #DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL}. In that case {@link
* #getForegroundNotification(TaskState[])} should be overridden in the subclass.
* #getForegroundNotification(DownloadState[])} should be overridden in the subclass.
*
* @param foregroundNotificationId The notification id for the foreground notification, or {@link
* #FOREGROUND_NOTIFICATION_ID_NONE} (value {@value #FOREGROUND_NOTIFICATION_ID_NONE})
......@@ -110,7 +110,7 @@ public abstract class DownloadService extends Service {
/**
* Creates a DownloadService which will run in the foreground. {@link
* #getForegroundNotification(TaskState[])} should be overridden in the subclass.
* #getForegroundNotification(DownloadState[])} should be overridden in the subclass.
*
* @param foregroundNotificationId The notification id for the foreground notification, must not
* be 0.
......@@ -128,7 +128,7 @@ public abstract class DownloadService extends Service {
/**
* Creates a DownloadService which will run in the foreground. {@link
* #getForegroundNotification(TaskState[])} should be overridden in the subclass.
* #getForegroundNotification(DownloadState[])} should be overridden in the subclass.
*
* @param foregroundNotificationId The notification id for the foreground notification. Must not
* be 0.
......@@ -338,29 +338,29 @@ public abstract class DownloadService extends Service {
*
* <p>Returns a notification to be displayed when this service running in the foreground.
*
* <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,
* <p>This method is called when there is a download state change and periodically while there are
* active 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 taskStates} array. The returned notification is used to satisfy system
* with an empty {@code downloadStates} array. The returned notification is used to satisfy system
* requirements for foreground services.
*
* @param taskStates The states of all current tasks.
* @param downloadStates The states of all current downloads.
* @return The foreground notification to display.
*/
protected Notification getForegroundNotification(TaskState[] taskStates) {
protected Notification getForegroundNotification(DownloadState[] downloadStates) {
throw new IllegalStateException(
getClass().getName()
+ " is started in the foreground but getForegroundNotification() is not implemented.");
}
/**
* Called when the state of a task changes.
* Called when the state of a download changes.
*
* @param taskState The state of the task.
* @param downloadState The state of the download.
*/
protected void onTaskStateChanged(TaskState taskState) {
protected void onDownloadStateChanged(DownloadState downloadState) {
// Do nothing.
}
......@@ -428,10 +428,11 @@ public abstract class DownloadService extends Service {
}
@Override
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
DownloadService.this.onTaskStateChanged(taskState);
public void onDownloadStateChanged(
DownloadManager downloadManager, DownloadState downloadState) {
DownloadService.this.onDownloadStateChanged(downloadState);
if (foregroundNotificationUpdater != null) {
if (taskState.state == TaskState.STATE_STARTED) {
if (downloadState.state == DownloadState.STATE_STARTED) {
foregroundNotificationUpdater.startPeriodicUpdates();
} else {
foregroundNotificationUpdater.update();
......@@ -471,8 +472,8 @@ public abstract class DownloadService extends Service {
}
public void update() {
TaskState[] taskStates = downloadManager.getAllTaskStates();
startForeground(notificationId, getForegroundNotification(taskStates));
DownloadState[] downloadStates = downloadManager.getAllDownloadStates();
startForeground(notificationId, getForegroundNotification(downloadStates));
notificationDisplayed = true;
if (periodicUpdatesStarted) {
handler.removeCallbacks(this);
......
......@@ -20,8 +20,8 @@ import static org.junit.Assert.fail;
import android.net.Uri;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState.State;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState.State;
import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.RobolectricUtil;
import com.google.android.exoplayer2.testutil.TestDownloadManagerListener;
......@@ -371,11 +371,11 @@ public class DownloadManagerTest {
TaskWrapper task2 = new DownloadRunner(uri2).postDownloadAction().getTask();
TaskWrapper task3 = new DownloadRunner(uri3).postRemoveAction().getTask();
TaskState[] states = downloadManager.getAllTaskStates();
DownloadState[] states = downloadManager.getAllDownloadStates();
assertThat(states).hasLength(3);
int[] taskIds = {task1.taskId, task2.taskId, task3.taskId};
int[] stateTaskIds = {states[0].taskId, states[1].taskId, states[2].taskId};
int[] stateTaskIds = {states[0].id, states[1].id, states[2].id};
assertThat(stateTaskIds).isEqualTo(taskIds);
}
......@@ -522,19 +522,19 @@ public class DownloadManagerTest {
}
private TaskWrapper assertStarted() throws InterruptedException {
return assertState(TaskState.STATE_STARTED);
return assertState(DownloadState.STATE_STARTED);
}
private TaskWrapper assertCompleted() {
return assertState(TaskState.STATE_COMPLETED);
return assertState(DownloadState.STATE_COMPLETED);
}
private TaskWrapper assertFailed() {
return assertState(TaskState.STATE_FAILED);
return assertState(DownloadState.STATE_FAILED);
}
private TaskWrapper assertQueued() {
return assertState(TaskState.STATE_QUEUED);
return assertState(DownloadState.STATE_QUEUED);
}
private TaskWrapper assertState(@State int expectedState) {
......
......@@ -23,7 +23,7 @@ import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
/** Helper for creating download notifications. */
public final class DownloadNotificationUtil {
......@@ -33,7 +33,7 @@ public final class DownloadNotificationUtil {
private DownloadNotificationUtil() {}
/**
* Returns a progress notification for the given task states.
* Returns a progress notification for the given download states.
*
* @param context A context for accessing resources.
* @param smallIcon A small icon for the notification.
......@@ -41,7 +41,7 @@ public final class DownloadNotificationUtil {
* above.
* @param contentIntent An optional content intent to send when the notification is clicked.
* @param message An optional message to display on the notification.
* @param taskStates The task states.
* @param downloadStates The download states.
* @return The notification.
*/
public static Notification buildProgressNotification(
......@@ -50,28 +50,28 @@ public final class DownloadNotificationUtil {
String channelId,
@Nullable PendingIntent contentIntent,
@Nullable String message,
TaskState[] taskStates) {
DownloadState[] downloadStates) {
float totalPercentage = 0;
int downloadTaskCount = 0;
boolean allDownloadPercentagesUnknown = true;
boolean haveDownloadedBytes = false;
boolean haveDownloadTasks = false;
boolean haveRemoveTasks = false;
for (TaskState taskState : taskStates) {
if (taskState.state != TaskState.STATE_STARTED
&& taskState.state != TaskState.STATE_COMPLETED) {
for (DownloadState downloadState : downloadStates) {
if (downloadState.state != DownloadState.STATE_STARTED
&& downloadState.state != DownloadState.STATE_COMPLETED) {
continue;
}
if (taskState.action.isRemoveAction) {
if (downloadState.action.isRemoveAction) {
haveRemoveTasks = true;
continue;
}
haveDownloadTasks = true;
if (taskState.downloadPercentage != C.PERCENTAGE_UNSET) {
if (downloadState.downloadPercentage != C.PERCENTAGE_UNSET) {
allDownloadPercentagesUnknown = false;
totalPercentage += taskState.downloadPercentage;
totalPercentage += downloadState.downloadPercentage;
}
haveDownloadedBytes |= taskState.downloadedBytes > 0;
haveDownloadedBytes |= downloadState.downloadedBytes > 0;
downloadTaskCount++;
}
......
......@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.testutil;
import static com.google.common.truth.Truth.assertThat;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
......@@ -56,12 +57,11 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
}
@Override
public void onTaskStateChanged(
DownloadManager downloadManager, DownloadManager.TaskState taskState) {
if (taskState.state == DownloadManager.TaskState.STATE_FAILED && downloadError == null) {
downloadError = taskState.error;
public void onDownloadStateChanged(DownloadManager downloadManager, DownloadState downloadState) {
if (downloadState.state == DownloadState.STATE_FAILED && downloadError == null) {
downloadError = downloadState.error;
}
getStateQueue(taskState.taskId).add(taskState.state);
getStateQueue(downloadState.id).add(downloadState.state);
}
@Override
......
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