Commit 4bf42bd2 by eguven Committed by Oliver Woodman

Rename TaskState to DownloadState

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